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
;
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
, bool unsafe_math
)
262 LLVMTypeRef main_function_type
, ret_type
;
263 LLVMBasicBlockRef main_function_body
;
265 if (num_return_elems
)
266 ret_type
= LLVMStructTypeInContext(ctx
, return_types
,
267 num_return_elems
, true);
269 ret_type
= LLVMVoidTypeInContext(ctx
);
271 /* Setup the function */
273 LLVMFunctionType(ret_type
, param_types
, param_count
, 0);
274 LLVMValueRef main_function
=
275 LLVMAddFunction(module
, "main", main_function_type
);
277 LLVMAppendBasicBlockInContext(ctx
, main_function
, "main_body");
278 LLVMPositionBuilderAtEnd(builder
, main_function_body
);
280 LLVMSetFunctionCallConv(main_function
, RADEON_LLVM_AMDGPU_CS
);
281 for (unsigned i
= 0; i
< sgpr_params
; ++i
) {
282 if (array_params_mask
& (1 << i
)) {
283 LLVMValueRef P
= LLVMGetParam(main_function
, i
);
284 ac_add_function_attr(ctx
, main_function
, i
+ 1, AC_FUNC_ATTR_BYVAL
);
285 ac_add_attr_dereferenceable(P
, UINT64_MAX
);
288 ac_add_function_attr(ctx
, main_function
, i
+ 1, AC_FUNC_ATTR_INREG
);
293 /* These were copied from some LLVM test. */
294 LLVMAddTargetDependentFunctionAttr(main_function
,
295 "less-precise-fpmad",
297 LLVMAddTargetDependentFunctionAttr(main_function
,
300 LLVMAddTargetDependentFunctionAttr(main_function
,
303 LLVMAddTargetDependentFunctionAttr(main_function
,
307 return main_function
;
310 static LLVMTypeRef
const_array(LLVMTypeRef elem_type
, int num_elements
)
312 return LLVMPointerType(LLVMArrayType(elem_type
, num_elements
),
316 static LLVMValueRef
get_shared_memory_ptr(struct nir_to_llvm_context
*ctx
,
324 offset
= LLVMConstInt(ctx
->i32
, idx
* 16, false);
326 ptr
= ctx
->shared_memory
;
327 ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &offset
, 1, "");
328 addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
329 ptr
= LLVMBuildBitCast(ctx
->builder
, ptr
, LLVMPointerType(type
, addr_space
), "");
333 static LLVMTypeRef
to_integer_type_scalar(struct nir_to_llvm_context
*ctx
, LLVMTypeRef t
)
335 if (t
== ctx
->f16
|| t
== ctx
->i16
)
337 else if (t
== ctx
->f32
|| t
== ctx
->i32
)
339 else if (t
== ctx
->f64
|| t
== ctx
->i64
)
342 unreachable("Unhandled integer size");
345 static LLVMTypeRef
to_integer_type(struct nir_to_llvm_context
*ctx
, LLVMTypeRef t
)
347 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
348 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
349 return LLVMVectorType(to_integer_type_scalar(ctx
, elem_type
),
350 LLVMGetVectorSize(t
));
352 return to_integer_type_scalar(ctx
, t
);
355 static LLVMValueRef
to_integer(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
)
357 LLVMTypeRef type
= LLVMTypeOf(v
);
358 return LLVMBuildBitCast(ctx
->builder
, v
, to_integer_type(ctx
, type
), "");
361 static LLVMTypeRef
to_float_type_scalar(struct nir_to_llvm_context
*ctx
, LLVMTypeRef t
)
363 if (t
== ctx
->i16
|| t
== ctx
->f16
)
365 else if (t
== ctx
->i32
|| t
== ctx
->f32
)
367 else if (t
== ctx
->i64
|| t
== ctx
->f64
)
370 unreachable("Unhandled float size");
373 static LLVMTypeRef
to_float_type(struct nir_to_llvm_context
*ctx
, LLVMTypeRef t
)
375 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
376 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
377 return LLVMVectorType(to_float_type_scalar(ctx
, elem_type
),
378 LLVMGetVectorSize(t
));
380 return to_float_type_scalar(ctx
, t
);
383 static LLVMValueRef
to_float(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
)
385 LLVMTypeRef type
= LLVMTypeOf(v
);
386 return LLVMBuildBitCast(ctx
->builder
, v
, to_float_type(ctx
, type
), "");
389 static int get_elem_bits(struct nir_to_llvm_context
*ctx
, LLVMTypeRef type
)
391 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
392 type
= LLVMGetElementType(type
);
394 if (LLVMGetTypeKind(type
) == LLVMIntegerTypeKind
)
395 return LLVMGetIntTypeWidth(type
);
397 if (type
== ctx
->f16
)
399 if (type
== ctx
->f32
)
401 if (type
== ctx
->f64
)
404 unreachable("Unhandled type kind in get_elem_bits");
407 static LLVMValueRef
unpack_param(struct nir_to_llvm_context
*ctx
,
408 LLVMValueRef param
, unsigned rshift
,
411 LLVMValueRef value
= param
;
413 value
= LLVMBuildLShr(ctx
->builder
, value
,
414 LLVMConstInt(ctx
->i32
, rshift
, false), "");
416 if (rshift
+ bitwidth
< 32) {
417 unsigned mask
= (1 << bitwidth
) - 1;
418 value
= LLVMBuildAnd(ctx
->builder
, value
,
419 LLVMConstInt(ctx
->i32
, mask
, false), "");
424 static LLVMValueRef
get_rel_patch_id(struct nir_to_llvm_context
*ctx
)
426 switch (ctx
->stage
) {
427 case MESA_SHADER_TESS_CTRL
:
428 return unpack_param(ctx
, ctx
->tcs_rel_ids
, 0, 8);
429 case MESA_SHADER_TESS_EVAL
:
430 return ctx
->tes_rel_patch_id
;
433 unreachable("Illegal stage");
437 /* Tessellation shaders pass outputs to the next shader using LDS.
439 * LS outputs = TCS inputs
440 * TCS outputs = TES inputs
443 * - TCS inputs for patch 0
444 * - TCS inputs for patch 1
445 * - TCS inputs for patch 2 = get_tcs_in_current_patch_offset (if RelPatchID==2)
447 * - TCS outputs for patch 0 = get_tcs_out_patch0_offset
448 * - Per-patch TCS outputs for patch 0 = get_tcs_out_patch0_patch_data_offset
449 * - TCS outputs for patch 1
450 * - Per-patch TCS outputs for patch 1
451 * - TCS outputs for patch 2 = get_tcs_out_current_patch_offset (if RelPatchID==2)
452 * - Per-patch TCS outputs for patch 2 = get_tcs_out_current_patch_data_offset (if RelPatchID==2)
455 * All three shaders VS(LS), TCS, TES share the same LDS space.
458 get_tcs_in_patch_stride(struct nir_to_llvm_context
*ctx
)
460 if (ctx
->stage
== MESA_SHADER_VERTEX
)
461 return unpack_param(ctx
, ctx
->ls_out_layout
, 0, 13);
462 else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
463 return unpack_param(ctx
, ctx
->tcs_in_layout
, 0, 13);
471 get_tcs_out_patch_stride(struct nir_to_llvm_context
*ctx
)
473 return unpack_param(ctx
, ctx
->tcs_out_layout
, 0, 13);
477 get_tcs_out_patch0_offset(struct nir_to_llvm_context
*ctx
)
479 return LLVMBuildMul(ctx
->builder
,
480 unpack_param(ctx
, ctx
->tcs_out_offsets
, 0, 16),
481 LLVMConstInt(ctx
->i32
, 4, false), "");
485 get_tcs_out_patch0_patch_data_offset(struct nir_to_llvm_context
*ctx
)
487 return LLVMBuildMul(ctx
->builder
,
488 unpack_param(ctx
, ctx
->tcs_out_offsets
, 16, 16),
489 LLVMConstInt(ctx
->i32
, 4, false), "");
493 get_tcs_in_current_patch_offset(struct nir_to_llvm_context
*ctx
)
495 LLVMValueRef patch_stride
= get_tcs_in_patch_stride(ctx
);
496 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
498 return LLVMBuildMul(ctx
->builder
, patch_stride
, rel_patch_id
, "");
502 get_tcs_out_current_patch_offset(struct nir_to_llvm_context
*ctx
)
504 LLVMValueRef patch0_offset
= get_tcs_out_patch0_offset(ctx
);
505 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
506 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
508 return LLVMBuildAdd(ctx
->builder
, patch0_offset
,
509 LLVMBuildMul(ctx
->builder
, patch_stride
,
515 get_tcs_out_current_patch_data_offset(struct nir_to_llvm_context
*ctx
)
517 LLVMValueRef patch0_patch_data_offset
=
518 get_tcs_out_patch0_patch_data_offset(ctx
);
519 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
520 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
522 return LLVMBuildAdd(ctx
->builder
, patch0_patch_data_offset
,
523 LLVMBuildMul(ctx
->builder
, patch_stride
,
528 static void set_userdata_location(struct ac_userdata_info
*ud_info
, uint8_t sgpr_idx
, uint8_t num_sgprs
)
530 ud_info
->sgpr_idx
= sgpr_idx
;
531 ud_info
->num_sgprs
= num_sgprs
;
532 ud_info
->indirect
= false;
533 ud_info
->indirect_offset
= 0;
536 static void set_userdata_location_shader(struct nir_to_llvm_context
*ctx
,
537 int idx
, uint8_t sgpr_idx
, uint8_t num_sgprs
)
539 set_userdata_location(&ctx
->shader_info
->user_sgprs_locs
.shader_data
[idx
], sgpr_idx
, num_sgprs
);
543 static void set_userdata_location_indirect(struct ac_userdata_info
*ud_info
, uint8_t sgpr_idx
, uint8_t num_sgprs
,
544 uint32_t indirect_offset
)
546 ud_info
->sgpr_idx
= sgpr_idx
;
547 ud_info
->num_sgprs
= num_sgprs
;
548 ud_info
->indirect
= true;
549 ud_info
->indirect_offset
= indirect_offset
;
552 static void declare_tess_lds(struct nir_to_llvm_context
*ctx
)
554 unsigned lds_size
= ctx
->options
->chip_class
>= CIK
? 65536 : 32768;
555 ctx
->lds
= LLVMBuildIntToPtr(ctx
->builder
, ctx
->i32zero
,
556 LLVMPointerType(LLVMArrayType(ctx
->i32
, lds_size
/ 4), LOCAL_ADDR_SPACE
),
560 struct user_sgpr_info
{
561 bool need_ring_offsets
;
563 bool indirect_all_descriptor_sets
;
566 static void allocate_user_sgprs(struct nir_to_llvm_context
*ctx
,
567 struct user_sgpr_info
*user_sgpr_info
)
569 memset(user_sgpr_info
, 0, sizeof(struct user_sgpr_info
));
571 /* until we sort out scratch/global buffers always assign ring offsets for gs/vs/es */
572 if (ctx
->stage
== MESA_SHADER_GEOMETRY
||
573 ctx
->stage
== MESA_SHADER_VERTEX
||
574 ctx
->stage
== MESA_SHADER_TESS_CTRL
||
575 ctx
->stage
== MESA_SHADER_TESS_EVAL
||
576 ctx
->is_gs_copy_shader
)
577 user_sgpr_info
->need_ring_offsets
= true;
579 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&&
580 ctx
->shader_info
->info
.ps
.needs_sample_positions
)
581 user_sgpr_info
->need_ring_offsets
= true;
583 /* 2 user sgprs will nearly always be allocated for scratch/rings */
584 if (ctx
->options
->supports_spill
|| user_sgpr_info
->need_ring_offsets
) {
585 user_sgpr_info
->sgpr_count
+= 2;
588 switch (ctx
->stage
) {
589 case MESA_SHADER_COMPUTE
:
590 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.cs
.grid_components_used
;
592 case MESA_SHADER_FRAGMENT
:
593 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.ps
.needs_sample_positions
;
595 case MESA_SHADER_VERTEX
:
596 if (!ctx
->is_gs_copy_shader
) {
597 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.vs
.has_vertex_buffers
? 2 : 0;
598 if (ctx
->shader_info
->info
.vs
.needs_draw_id
) {
599 user_sgpr_info
->sgpr_count
+= 3;
601 user_sgpr_info
->sgpr_count
+= 2;
604 if (ctx
->options
->key
.vs
.as_ls
)
605 user_sgpr_info
->sgpr_count
++;
607 case MESA_SHADER_TESS_CTRL
:
608 user_sgpr_info
->sgpr_count
+= 4;
610 case MESA_SHADER_TESS_EVAL
:
611 user_sgpr_info
->sgpr_count
+= 1;
613 case MESA_SHADER_GEOMETRY
:
614 user_sgpr_info
->sgpr_count
+= 2;
620 if (ctx
->shader_info
->info
.needs_push_constants
)
621 user_sgpr_info
->sgpr_count
+= 2;
623 uint32_t remaining_sgprs
= 16 - user_sgpr_info
->sgpr_count
;
624 if (remaining_sgprs
/ 2 < util_bitcount(ctx
->shader_info
->info
.desc_set_used_mask
)) {
625 user_sgpr_info
->sgpr_count
+= 2;
626 user_sgpr_info
->indirect_all_descriptor_sets
= true;
628 user_sgpr_info
->sgpr_count
+= util_bitcount(ctx
->shader_info
->info
.desc_set_used_mask
) * 2;
632 static void create_function(struct nir_to_llvm_context
*ctx
)
634 LLVMTypeRef arg_types
[23];
635 unsigned arg_idx
= 0;
636 unsigned array_params_mask
= 0;
637 unsigned sgpr_count
= 0, user_sgpr_count
;
639 unsigned num_sets
= ctx
->options
->layout
? ctx
->options
->layout
->num_sets
: 0;
640 unsigned user_sgpr_idx
;
641 struct user_sgpr_info user_sgpr_info
;
643 allocate_user_sgprs(ctx
, &user_sgpr_info
);
644 if (user_sgpr_info
.need_ring_offsets
&& !ctx
->options
->supports_spill
) {
645 arg_types
[arg_idx
++] = const_array(ctx
->v16i8
, 16); /* address of rings */
648 /* 1 for each descriptor set */
649 if (!user_sgpr_info
.indirect_all_descriptor_sets
) {
650 for (unsigned i
= 0; i
< num_sets
; ++i
) {
651 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& (1 << ctx
->stage
)) {
652 array_params_mask
|= (1 << arg_idx
);
653 arg_types
[arg_idx
++] = const_array(ctx
->i8
, 1024 * 1024);
657 array_params_mask
|= (1 << arg_idx
);
658 arg_types
[arg_idx
++] = const_array(const_array(ctx
->i8
, 1024 * 1024), 32);
661 if (ctx
->shader_info
->info
.needs_push_constants
) {
662 /* 1 for push constants and dynamic descriptors */
663 array_params_mask
|= (1 << arg_idx
);
664 arg_types
[arg_idx
++] = const_array(ctx
->i8
, 1024 * 1024);
667 switch (ctx
->stage
) {
668 case MESA_SHADER_COMPUTE
:
669 if (ctx
->shader_info
->info
.cs
.grid_components_used
)
670 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, ctx
->shader_info
->info
.cs
.grid_components_used
); /* grid size */
671 user_sgpr_count
= arg_idx
;
672 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3);
673 arg_types
[arg_idx
++] = ctx
->i32
;
674 sgpr_count
= arg_idx
;
676 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3);
678 case MESA_SHADER_VERTEX
:
679 if (!ctx
->is_gs_copy_shader
) {
680 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
)
681 arg_types
[arg_idx
++] = const_array(ctx
->v16i8
, 16); /* vertex buffers */
682 arg_types
[arg_idx
++] = ctx
->i32
; // base vertex
683 arg_types
[arg_idx
++] = ctx
->i32
; // start instance
684 if (ctx
->shader_info
->info
.vs
.needs_draw_id
)
685 arg_types
[arg_idx
++] = ctx
->i32
; // draw index
687 user_sgpr_count
= arg_idx
;
688 if (ctx
->options
->key
.vs
.as_es
)
689 arg_types
[arg_idx
++] = ctx
->i32
; //es2gs offset
690 else if (ctx
->options
->key
.vs
.as_ls
) {
691 arg_types
[arg_idx
++] = ctx
->i32
; //ls out layout
694 sgpr_count
= arg_idx
;
695 arg_types
[arg_idx
++] = ctx
->i32
; // vertex id
696 if (!ctx
->is_gs_copy_shader
) {
697 arg_types
[arg_idx
++] = ctx
->i32
; // rel auto id
698 arg_types
[arg_idx
++] = ctx
->i32
; // vs prim id
699 arg_types
[arg_idx
++] = ctx
->i32
; // instance id
702 case MESA_SHADER_TESS_CTRL
:
703 arg_types
[arg_idx
++] = ctx
->i32
; // tcs offchip layout
704 arg_types
[arg_idx
++] = ctx
->i32
; // tcs out offsets
705 arg_types
[arg_idx
++] = ctx
->i32
; // tcs out layout
706 arg_types
[arg_idx
++] = ctx
->i32
; // tcs in layout
707 user_sgpr_count
= arg_idx
;
708 arg_types
[arg_idx
++] = ctx
->i32
; // param oc lds
709 arg_types
[arg_idx
++] = ctx
->i32
; // tess factor offset
710 sgpr_count
= arg_idx
;
711 arg_types
[arg_idx
++] = ctx
->i32
; // patch id
712 arg_types
[arg_idx
++] = ctx
->i32
; // rel ids;
714 case MESA_SHADER_TESS_EVAL
:
715 arg_types
[arg_idx
++] = ctx
->i32
; // tcs offchip layout
716 user_sgpr_count
= arg_idx
;
717 if (ctx
->options
->key
.tes
.as_es
) {
718 arg_types
[arg_idx
++] = ctx
->i32
; // OC LDS
719 arg_types
[arg_idx
++] = ctx
->i32
; //
720 arg_types
[arg_idx
++] = ctx
->i32
; // es2gs offset
722 arg_types
[arg_idx
++] = ctx
->i32
; //
723 arg_types
[arg_idx
++] = ctx
->i32
; // OC LDS
725 sgpr_count
= arg_idx
;
726 arg_types
[arg_idx
++] = ctx
->f32
; // tes_u
727 arg_types
[arg_idx
++] = ctx
->f32
; // tes_v
728 arg_types
[arg_idx
++] = ctx
->i32
; // tes rel patch id
729 arg_types
[arg_idx
++] = ctx
->i32
; // tes patch id
731 case MESA_SHADER_GEOMETRY
:
732 arg_types
[arg_idx
++] = ctx
->i32
; // gsvs stride
733 arg_types
[arg_idx
++] = ctx
->i32
; // gsvs num entires
734 user_sgpr_count
= arg_idx
;
735 arg_types
[arg_idx
++] = ctx
->i32
; // gs2vs offset
736 arg_types
[arg_idx
++] = ctx
->i32
; // wave id
737 sgpr_count
= arg_idx
;
738 arg_types
[arg_idx
++] = ctx
->i32
; // vtx0
739 arg_types
[arg_idx
++] = ctx
->i32
; // vtx1
740 arg_types
[arg_idx
++] = ctx
->i32
; // prim id
741 arg_types
[arg_idx
++] = ctx
->i32
; // vtx2
742 arg_types
[arg_idx
++] = ctx
->i32
; // vtx3
743 arg_types
[arg_idx
++] = ctx
->i32
; // vtx4
744 arg_types
[arg_idx
++] = ctx
->i32
; // vtx5
745 arg_types
[arg_idx
++] = ctx
->i32
; // GS instance id
747 case MESA_SHADER_FRAGMENT
:
748 if (ctx
->shader_info
->info
.ps
.needs_sample_positions
)
749 arg_types
[arg_idx
++] = ctx
->i32
; /* sample position offset */
750 user_sgpr_count
= arg_idx
;
751 arg_types
[arg_idx
++] = ctx
->i32
; /* prim mask */
752 sgpr_count
= arg_idx
;
753 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp sample */
754 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp center */
755 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp centroid */
756 arg_types
[arg_idx
++] = ctx
->v3i32
; /* persp pull model */
757 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear sample */
758 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear center */
759 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear centroid */
760 arg_types
[arg_idx
++] = ctx
->f32
; /* line stipple tex */
761 arg_types
[arg_idx
++] = ctx
->f32
; /* pos x float */
762 arg_types
[arg_idx
++] = ctx
->f32
; /* pos y float */
763 arg_types
[arg_idx
++] = ctx
->f32
; /* pos z float */
764 arg_types
[arg_idx
++] = ctx
->f32
; /* pos w float */
765 arg_types
[arg_idx
++] = ctx
->i32
; /* front face */
766 arg_types
[arg_idx
++] = ctx
->i32
; /* ancillary */
767 arg_types
[arg_idx
++] = ctx
->i32
; /* sample coverage */
768 arg_types
[arg_idx
++] = ctx
->i32
; /* fixed pt */
771 unreachable("Shader stage not implemented");
774 ctx
->main_function
= create_llvm_function(
775 ctx
->context
, ctx
->module
, ctx
->builder
, NULL
, 0, arg_types
,
776 arg_idx
, array_params_mask
, sgpr_count
, ctx
->options
->unsafe_math
);
777 set_llvm_calling_convention(ctx
->main_function
, ctx
->stage
);
779 ctx
->shader_info
->num_input_sgprs
= 0;
780 ctx
->shader_info
->num_input_vgprs
= 0;
782 ctx
->shader_info
->num_user_sgprs
= ctx
->options
->supports_spill
? 2 : 0;
783 for (i
= 0; i
< user_sgpr_count
; i
++)
784 ctx
->shader_info
->num_user_sgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
786 ctx
->shader_info
->num_input_sgprs
= ctx
->shader_info
->num_user_sgprs
;
787 for (; i
< sgpr_count
; i
++)
788 ctx
->shader_info
->num_input_sgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
790 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
791 for (; i
< arg_idx
; ++i
)
792 ctx
->shader_info
->num_input_vgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
797 if (ctx
->options
->supports_spill
|| user_sgpr_info
.need_ring_offsets
) {
798 set_userdata_location_shader(ctx
, AC_UD_SCRATCH_RING_OFFSETS
, user_sgpr_idx
, 2);
800 if (ctx
->options
->supports_spill
) {
801 ctx
->ring_offsets
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.implicit.buffer.ptr",
802 LLVMPointerType(ctx
->i8
, CONST_ADDR_SPACE
),
803 NULL
, 0, AC_FUNC_ATTR_READNONE
);
804 ctx
->ring_offsets
= LLVMBuildBitCast(ctx
->builder
, ctx
->ring_offsets
,
805 const_array(ctx
->v16i8
, 16), "");
807 ctx
->ring_offsets
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
810 if (!user_sgpr_info
.indirect_all_descriptor_sets
) {
811 for (unsigned i
= 0; i
< num_sets
; ++i
) {
812 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& (1 << ctx
->stage
)) {
813 set_userdata_location(&ctx
->shader_info
->user_sgprs_locs
.descriptor_sets
[i
], user_sgpr_idx
, 2);
815 ctx
->descriptor_sets
[i
] =
816 LLVMGetParam(ctx
->main_function
, arg_idx
++);
818 ctx
->descriptor_sets
[i
] = NULL
;
821 uint32_t desc_sgpr_idx
= user_sgpr_idx
;
822 LLVMValueRef desc_sets
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
823 set_userdata_location_shader(ctx
, AC_UD_INDIRECT_DESCRIPTOR_SETS
, user_sgpr_idx
, 2);
826 for (unsigned i
= 0; i
< num_sets
; ++i
) {
827 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& (1 << ctx
->stage
)) {
828 set_userdata_location_indirect(&ctx
->shader_info
->user_sgprs_locs
.descriptor_sets
[i
], desc_sgpr_idx
, 2, i
* 8);
829 ctx
->descriptor_sets
[i
] = ac_build_indexed_load_const(&ctx
->ac
, desc_sets
, LLVMConstInt(ctx
->i32
, i
, false));
832 ctx
->descriptor_sets
[i
] = NULL
;
834 ctx
->shader_info
->need_indirect_descriptor_sets
= true;
837 if (ctx
->shader_info
->info
.needs_push_constants
) {
838 ctx
->push_constants
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
839 set_userdata_location_shader(ctx
, AC_UD_PUSH_CONSTANTS
, user_sgpr_idx
, 2);
843 switch (ctx
->stage
) {
844 case MESA_SHADER_COMPUTE
:
845 if (ctx
->shader_info
->info
.cs
.grid_components_used
) {
846 set_userdata_location_shader(ctx
, AC_UD_CS_GRID_SIZE
, user_sgpr_idx
, ctx
->shader_info
->info
.cs
.grid_components_used
);
847 user_sgpr_idx
+= ctx
->shader_info
->info
.cs
.grid_components_used
;
848 ctx
->num_work_groups
=
849 LLVMGetParam(ctx
->main_function
, arg_idx
++);
852 LLVMGetParam(ctx
->main_function
, arg_idx
++);
854 LLVMGetParam(ctx
->main_function
, arg_idx
++);
855 ctx
->local_invocation_ids
=
856 LLVMGetParam(ctx
->main_function
, arg_idx
++);
858 case MESA_SHADER_VERTEX
:
859 if (!ctx
->is_gs_copy_shader
) {
860 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
) {
861 set_userdata_location_shader(ctx
, AC_UD_VS_VERTEX_BUFFERS
, user_sgpr_idx
, 2);
863 ctx
->vertex_buffers
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
866 if (ctx
->shader_info
->info
.vs
.needs_draw_id
)
869 set_userdata_location_shader(ctx
, AC_UD_VS_BASE_VERTEX_START_INSTANCE
, user_sgpr_idx
, vs_num
);
870 user_sgpr_idx
+= vs_num
;
872 ctx
->base_vertex
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
873 ctx
->start_instance
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
874 if (ctx
->shader_info
->info
.vs
.needs_draw_id
)
875 ctx
->draw_index
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
877 if (ctx
->options
->key
.vs
.as_es
)
878 ctx
->es2gs_offset
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
879 else if (ctx
->options
->key
.vs
.as_ls
) {
880 set_userdata_location_shader(ctx
, AC_UD_VS_LS_TCS_IN_LAYOUT
, user_sgpr_idx
, 1);
882 ctx
->ls_out_layout
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
884 ctx
->vertex_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
885 if (!ctx
->is_gs_copy_shader
) {
886 ctx
->rel_auto_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
887 ctx
->vs_prim_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
888 ctx
->instance_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
890 if (ctx
->options
->key
.vs
.as_ls
)
891 declare_tess_lds(ctx
);
893 case MESA_SHADER_TESS_CTRL
:
894 set_userdata_location_shader(ctx
, AC_UD_TCS_OFFCHIP_LAYOUT
, user_sgpr_idx
, 4);
896 ctx
->tcs_offchip_layout
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
897 ctx
->tcs_out_offsets
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
898 ctx
->tcs_out_layout
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
899 ctx
->tcs_in_layout
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
900 ctx
->oc_lds
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
901 ctx
->tess_factor_offset
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
902 ctx
->tcs_patch_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
903 ctx
->tcs_rel_ids
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
905 declare_tess_lds(ctx
);
907 case MESA_SHADER_TESS_EVAL
:
908 set_userdata_location_shader(ctx
, AC_UD_TES_OFFCHIP_LAYOUT
, user_sgpr_idx
, 1);
910 ctx
->tcs_offchip_layout
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
911 if (ctx
->options
->key
.tes
.as_es
) {
912 ctx
->oc_lds
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
914 ctx
->es2gs_offset
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
917 ctx
->oc_lds
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
919 ctx
->tes_u
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
920 ctx
->tes_v
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
921 ctx
->tes_rel_patch_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
922 ctx
->tes_patch_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
924 case MESA_SHADER_GEOMETRY
:
925 set_userdata_location_shader(ctx
, AC_UD_GS_VS_RING_STRIDE_ENTRIES
, user_sgpr_idx
, 2);
927 ctx
->gsvs_ring_stride
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
928 ctx
->gsvs_num_entries
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
929 ctx
->gs2vs_offset
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
930 ctx
->gs_wave_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
931 ctx
->gs_vtx_offset
[0] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
932 ctx
->gs_vtx_offset
[1] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
933 ctx
->gs_prim_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
934 ctx
->gs_vtx_offset
[2] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
935 ctx
->gs_vtx_offset
[3] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
936 ctx
->gs_vtx_offset
[4] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
937 ctx
->gs_vtx_offset
[5] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
938 ctx
->gs_invocation_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
940 case MESA_SHADER_FRAGMENT
:
941 if (ctx
->shader_info
->info
.ps
.needs_sample_positions
) {
942 set_userdata_location_shader(ctx
, AC_UD_PS_SAMPLE_POS_OFFSET
, user_sgpr_idx
, 1);
944 ctx
->sample_pos_offset
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
946 ctx
->prim_mask
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
947 ctx
->persp_sample
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
948 ctx
->persp_center
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
949 ctx
->persp_centroid
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
951 ctx
->linear_sample
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
952 ctx
->linear_center
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
953 ctx
->linear_centroid
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
954 arg_idx
++; /* line stipple */
955 ctx
->frag_pos
[0] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
956 ctx
->frag_pos
[1] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
957 ctx
->frag_pos
[2] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
958 ctx
->frag_pos
[3] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
959 ctx
->front_face
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
960 ctx
->ancillary
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
961 ctx
->sample_coverage
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
964 unreachable("Shader stage not implemented");
968 static void setup_types(struct nir_to_llvm_context
*ctx
)
970 LLVMValueRef args
[4];
972 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->context
);
973 ctx
->i1
= LLVMIntTypeInContext(ctx
->context
, 1);
974 ctx
->i8
= LLVMIntTypeInContext(ctx
->context
, 8);
975 ctx
->i16
= LLVMIntTypeInContext(ctx
->context
, 16);
976 ctx
->i32
= LLVMIntTypeInContext(ctx
->context
, 32);
977 ctx
->i64
= LLVMIntTypeInContext(ctx
->context
, 64);
978 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
979 ctx
->v3i32
= LLVMVectorType(ctx
->i32
, 3);
980 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
981 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
982 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
983 ctx
->f16
= LLVMHalfTypeInContext(ctx
->context
);
984 ctx
->f64
= LLVMDoubleTypeInContext(ctx
->context
);
985 ctx
->v2f32
= LLVMVectorType(ctx
->f32
, 2);
986 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
987 ctx
->v16i8
= LLVMVectorType(ctx
->i8
, 16);
989 ctx
->i1false
= LLVMConstInt(ctx
->i1
, 0, false);
990 ctx
->i1true
= LLVMConstInt(ctx
->i1
, 1, false);
991 ctx
->i32zero
= LLVMConstInt(ctx
->i32
, 0, false);
992 ctx
->i32one
= LLVMConstInt(ctx
->i32
, 1, false);
993 ctx
->f32zero
= LLVMConstReal(ctx
->f32
, 0.0);
994 ctx
->f32one
= LLVMConstReal(ctx
->f32
, 1.0);
996 args
[0] = ctx
->f32zero
;
997 args
[1] = ctx
->f32zero
;
998 args
[2] = ctx
->f32zero
;
999 args
[3] = ctx
->f32one
;
1000 ctx
->v4f32empty
= LLVMConstVector(args
, 4);
1002 ctx
->uniform_md_kind
=
1003 LLVMGetMDKindIDInContext(ctx
->context
, "amdgpu.uniform", 14);
1004 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
1006 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
1009 static int get_llvm_num_components(LLVMValueRef value
)
1011 LLVMTypeRef type
= LLVMTypeOf(value
);
1012 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
1013 ? LLVMGetVectorSize(type
)
1015 return num_components
;
1018 static LLVMValueRef
llvm_extract_elem(struct nir_to_llvm_context
*ctx
,
1022 int count
= get_llvm_num_components(value
);
1024 assert(index
< count
);
1028 return LLVMBuildExtractElement(ctx
->builder
, value
,
1029 LLVMConstInt(ctx
->i32
, index
, false), "");
1032 static LLVMValueRef
trim_vector(struct nir_to_llvm_context
*ctx
,
1033 LLVMValueRef value
, unsigned count
)
1035 unsigned num_components
= get_llvm_num_components(value
);
1036 if (count
== num_components
)
1039 LLVMValueRef masks
[] = {
1040 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
1041 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false)};
1044 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
1047 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
1048 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
1052 build_store_values_extended(struct nir_to_llvm_context
*ctx
,
1053 LLVMValueRef
*values
,
1054 unsigned value_count
,
1055 unsigned value_stride
,
1058 LLVMBuilderRef builder
= ctx
->builder
;
1061 if (value_count
== 1) {
1062 LLVMBuildStore(builder
, vec
, values
[0]);
1066 for (i
= 0; i
< value_count
; i
++) {
1067 LLVMValueRef ptr
= values
[i
* value_stride
];
1068 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
1069 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
1070 LLVMBuildStore(builder
, value
, ptr
);
1074 static LLVMTypeRef
get_def_type(struct nir_to_llvm_context
*ctx
,
1077 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->context
, def
->bit_size
);
1078 if (def
->num_components
> 1) {
1079 type
= LLVMVectorType(type
, def
->num_components
);
1084 static LLVMValueRef
get_src(struct nir_to_llvm_context
*ctx
, nir_src src
)
1087 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->defs
, src
.ssa
);
1088 return (LLVMValueRef
)entry
->data
;
1092 static LLVMBasicBlockRef
get_block(struct nir_to_llvm_context
*ctx
,
1093 struct nir_block
*b
)
1095 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->defs
, b
);
1096 return (LLVMBasicBlockRef
)entry
->data
;
1099 static LLVMValueRef
get_alu_src(struct nir_to_llvm_context
*ctx
,
1101 unsigned num_components
)
1103 LLVMValueRef value
= get_src(ctx
, src
.src
);
1104 bool need_swizzle
= false;
1107 LLVMTypeRef type
= LLVMTypeOf(value
);
1108 unsigned src_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
1109 ? LLVMGetVectorSize(type
)
1112 for (unsigned i
= 0; i
< num_components
; ++i
) {
1113 assert(src
.swizzle
[i
] < src_components
);
1114 if (src
.swizzle
[i
] != i
)
1115 need_swizzle
= true;
1118 if (need_swizzle
|| num_components
!= src_components
) {
1119 LLVMValueRef masks
[] = {
1120 LLVMConstInt(ctx
->i32
, src
.swizzle
[0], false),
1121 LLVMConstInt(ctx
->i32
, src
.swizzle
[1], false),
1122 LLVMConstInt(ctx
->i32
, src
.swizzle
[2], false),
1123 LLVMConstInt(ctx
->i32
, src
.swizzle
[3], false)};
1125 if (src_components
> 1 && num_components
== 1) {
1126 value
= LLVMBuildExtractElement(ctx
->builder
, value
,
1128 } else if (src_components
== 1 && num_components
> 1) {
1129 LLVMValueRef values
[] = {value
, value
, value
, value
};
1130 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
1132 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
1133 value
= LLVMBuildShuffleVector(ctx
->builder
, value
, value
,
1137 assert(!src
.negate
);
1142 static LLVMValueRef
emit_int_cmp(struct nir_to_llvm_context
*ctx
,
1143 LLVMIntPredicate pred
, LLVMValueRef src0
,
1146 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
1147 return LLVMBuildSelect(ctx
->builder
, result
,
1148 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
1149 LLVMConstInt(ctx
->i32
, 0, false), "");
1152 static LLVMValueRef
emit_float_cmp(struct nir_to_llvm_context
*ctx
,
1153 LLVMRealPredicate pred
, LLVMValueRef src0
,
1156 LLVMValueRef result
;
1157 src0
= to_float(ctx
, src0
);
1158 src1
= to_float(ctx
, src1
);
1159 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
1160 return LLVMBuildSelect(ctx
->builder
, result
,
1161 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
1162 LLVMConstInt(ctx
->i32
, 0, false), "");
1165 static LLVMValueRef
emit_intrin_1f_param(struct nir_to_llvm_context
*ctx
,
1167 LLVMTypeRef result_type
,
1171 LLVMValueRef params
[] = {
1172 to_float(ctx
, src0
),
1175 sprintf(name
, "%s.f%d", intrin
, get_elem_bits(ctx
, result_type
));
1176 return ac_build_intrinsic(&ctx
->ac
, name
, result_type
, params
, 1, AC_FUNC_ATTR_READNONE
);
1179 static LLVMValueRef
emit_intrin_2f_param(struct nir_to_llvm_context
*ctx
,
1181 LLVMTypeRef result_type
,
1182 LLVMValueRef src0
, LLVMValueRef src1
)
1185 LLVMValueRef params
[] = {
1186 to_float(ctx
, src0
),
1187 to_float(ctx
, src1
),
1190 sprintf(name
, "%s.f%d", intrin
, get_elem_bits(ctx
, result_type
));
1191 return ac_build_intrinsic(&ctx
->ac
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
1194 static LLVMValueRef
emit_intrin_3f_param(struct nir_to_llvm_context
*ctx
,
1196 LLVMTypeRef result_type
,
1197 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
1200 LLVMValueRef params
[] = {
1201 to_float(ctx
, src0
),
1202 to_float(ctx
, src1
),
1203 to_float(ctx
, src2
),
1206 sprintf(name
, "%s.f%d", intrin
, get_elem_bits(ctx
, result_type
));
1207 return ac_build_intrinsic(&ctx
->ac
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
1210 static LLVMValueRef
emit_bcsel(struct nir_to_llvm_context
*ctx
,
1211 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
1213 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
1215 return LLVMBuildSelect(ctx
->builder
, v
, src1
, src2
, "");
1218 static LLVMValueRef
emit_find_lsb(struct nir_to_llvm_context
*ctx
,
1221 LLVMValueRef params
[2] = {
1224 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
1225 * add special code to check for x=0. The reason is that
1226 * the LLVM behavior for x=0 is different from what we
1229 * The hardware already implements the correct behavior.
1231 LLVMConstInt(ctx
->i32
, 1, false),
1233 return ac_build_intrinsic(&ctx
->ac
, "llvm.cttz.i32", ctx
->i32
, params
, 2, AC_FUNC_ATTR_READNONE
);
1236 static LLVMValueRef
emit_ifind_msb(struct nir_to_llvm_context
*ctx
,
1239 return ac_build_imsb(&ctx
->ac
, src0
, ctx
->i32
);
1242 static LLVMValueRef
emit_ufind_msb(struct nir_to_llvm_context
*ctx
,
1245 return ac_build_umsb(&ctx
->ac
, src0
, ctx
->i32
);
1248 static LLVMValueRef
emit_minmax_int(struct nir_to_llvm_context
*ctx
,
1249 LLVMIntPredicate pred
,
1250 LLVMValueRef src0
, LLVMValueRef src1
)
1252 return LLVMBuildSelect(ctx
->builder
,
1253 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
1258 static LLVMValueRef
emit_iabs(struct nir_to_llvm_context
*ctx
,
1261 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
1262 LLVMBuildNeg(ctx
->builder
, src0
, ""));
1265 static LLVMValueRef
emit_fsign(struct nir_to_llvm_context
*ctx
,
1268 LLVMValueRef cmp
, val
;
1270 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, ctx
->f32zero
, "");
1271 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->f32one
, src0
, "");
1272 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, ctx
->f32zero
, "");
1273 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(ctx
->f32
, -1.0), "");
1277 static LLVMValueRef
emit_isign(struct nir_to_llvm_context
*ctx
,
1280 LLVMValueRef cmp
, val
;
1282 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, ctx
->i32zero
, "");
1283 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->i32one
, src0
, "");
1284 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, ctx
->i32zero
, "");
1285 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(ctx
->i32
, -1, true), "");
1289 static LLVMValueRef
emit_ffract(struct nir_to_llvm_context
*ctx
,
1292 const char *intr
= "llvm.floor.f32";
1293 LLVMValueRef fsrc0
= to_float(ctx
, src0
);
1294 LLVMValueRef params
[] = {
1297 LLVMValueRef floor
= ac_build_intrinsic(&ctx
->ac
, intr
,
1298 ctx
->f32
, params
, 1,
1299 AC_FUNC_ATTR_READNONE
);
1300 return LLVMBuildFSub(ctx
->builder
, fsrc0
, floor
, "");
1303 static LLVMValueRef
emit_uint_carry(struct nir_to_llvm_context
*ctx
,
1305 LLVMValueRef src0
, LLVMValueRef src1
)
1307 LLVMTypeRef ret_type
;
1308 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
1310 LLVMValueRef params
[] = { src0
, src1
};
1311 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
1314 res
= ac_build_intrinsic(&ctx
->ac
, intrin
, ret_type
,
1315 params
, 2, AC_FUNC_ATTR_READNONE
);
1317 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
1318 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
1322 static LLVMValueRef
emit_b2f(struct nir_to_llvm_context
*ctx
,
1325 return LLVMBuildAnd(ctx
->builder
, src0
, LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""), "");
1328 static LLVMValueRef
emit_f2f16(struct nir_to_llvm_context
*ctx
,
1331 LLVMValueRef result
;
1334 src0
= to_float(ctx
, src0
);
1335 result
= LLVMBuildFPTrunc(ctx
->builder
, src0
, ctx
->f16
, "");
1337 /* TODO SI/CIK options here */
1338 if (ctx
->options
->chip_class
>= VI
) {
1339 LLVMValueRef args
[2];
1340 /* Check if the result is a denormal - and flush to 0 if so. */
1342 args
[1] = LLVMConstInt(ctx
->i32
, N_SUBNORMAL
| P_SUBNORMAL
, false);
1343 cond
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.class.f16", ctx
->i1
, args
, 2, AC_FUNC_ATTR_READNONE
);
1346 /* need to convert back up to f32 */
1347 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
1349 if (ctx
->options
->chip_class
>= VI
)
1350 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32zero
, result
, "");
1355 static LLVMValueRef
emit_umul_high(struct nir_to_llvm_context
*ctx
,
1356 LLVMValueRef src0
, LLVMValueRef src1
)
1358 LLVMValueRef dst64
, result
;
1359 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
1360 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
1362 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1363 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1364 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1368 static LLVMValueRef
emit_imul_high(struct nir_to_llvm_context
*ctx
,
1369 LLVMValueRef src0
, LLVMValueRef src1
)
1371 LLVMValueRef dst64
, result
;
1372 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
1373 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
1375 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1376 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1377 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1381 static LLVMValueRef
emit_bitfield_extract(struct nir_to_llvm_context
*ctx
,
1383 LLVMValueRef srcs
[3])
1385 LLVMValueRef result
;
1386 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
1388 result
= ac_build_bfe(&ctx
->ac
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
1389 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
1393 static LLVMValueRef
emit_bitfield_insert(struct nir_to_llvm_context
*ctx
,
1394 LLVMValueRef src0
, LLVMValueRef src1
,
1395 LLVMValueRef src2
, LLVMValueRef src3
)
1397 LLVMValueRef bfi_args
[3], result
;
1399 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
1400 LLVMBuildSub(ctx
->builder
,
1401 LLVMBuildShl(ctx
->builder
,
1406 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
1409 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
1412 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
1413 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
1415 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
1416 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
1417 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
1419 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
1423 static LLVMValueRef
emit_pack_half_2x16(struct nir_to_llvm_context
*ctx
,
1426 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1428 LLVMValueRef comp
[2];
1430 src0
= to_float(ctx
, src0
);
1431 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32zero
, "");
1432 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32one
, "");
1433 for (i
= 0; i
< 2; i
++) {
1434 comp
[i
] = LLVMBuildFPTrunc(ctx
->builder
, comp
[i
], ctx
->f16
, "");
1435 comp
[i
] = LLVMBuildBitCast(ctx
->builder
, comp
[i
], ctx
->i16
, "");
1436 comp
[i
] = LLVMBuildZExt(ctx
->builder
, comp
[i
], ctx
->i32
, "");
1439 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
1440 comp
[0] = LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
1445 static LLVMValueRef
emit_unpack_half_2x16(struct nir_to_llvm_context
*ctx
,
1448 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1449 LLVMValueRef temps
[2], result
, val
;
1452 for (i
= 0; i
< 2; i
++) {
1453 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
1454 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
1455 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
1456 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
1459 result
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), temps
[0],
1461 result
= LLVMBuildInsertElement(ctx
->builder
, result
, temps
[1],
1466 static LLVMValueRef
emit_ddxy(struct nir_to_llvm_context
*ctx
,
1472 LLVMValueRef result
;
1473 ctx
->has_ddxy
= true;
1475 if (!ctx
->lds
&& !ctx
->has_ds_bpermute
)
1476 ctx
->lds
= LLVMAddGlobalInAddressSpace(ctx
->module
,
1477 LLVMArrayType(ctx
->i32
, 64),
1478 "ddxy_lds", LOCAL_ADDR_SPACE
);
1480 if (op
== nir_op_fddx_fine
|| op
== nir_op_fddx
)
1481 mask
= AC_TID_MASK_LEFT
;
1482 else if (op
== nir_op_fddy_fine
|| op
== nir_op_fddy
)
1483 mask
= AC_TID_MASK_TOP
;
1485 mask
= AC_TID_MASK_TOP_LEFT
;
1487 /* for DDX we want to next X pixel, DDY next Y pixel. */
1488 if (op
== nir_op_fddx_fine
||
1489 op
== nir_op_fddx_coarse
||
1495 result
= ac_build_ddxy(&ctx
->ac
, ctx
->has_ds_bpermute
,
1496 mask
, idx
, ctx
->lds
,
1502 * this takes an I,J coordinate pair,
1503 * and works out the X and Y derivatives.
1504 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
1506 static LLVMValueRef
emit_ddxy_interp(
1507 struct nir_to_llvm_context
*ctx
,
1508 LLVMValueRef interp_ij
)
1510 LLVMValueRef result
[4], a
;
1513 for (i
= 0; i
< 2; i
++) {
1514 a
= LLVMBuildExtractElement(ctx
->builder
, interp_ij
,
1515 LLVMConstInt(ctx
->i32
, i
, false), "");
1516 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
1517 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
1519 return ac_build_gather_values(&ctx
->ac
, result
, 4);
1522 static void visit_alu(struct nir_to_llvm_context
*ctx
, nir_alu_instr
*instr
)
1524 LLVMValueRef src
[4], result
= NULL
;
1525 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
1526 unsigned src_components
;
1527 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
1529 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
1530 switch (instr
->op
) {
1536 case nir_op_pack_half_2x16
:
1539 case nir_op_unpack_half_2x16
:
1543 src_components
= num_components
;
1546 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1547 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
1549 switch (instr
->op
) {
1555 src
[0] = to_float(ctx
, src
[0]);
1556 result
= LLVMBuildFNeg(ctx
->builder
, src
[0], "");
1559 result
= LLVMBuildNeg(ctx
->builder
, src
[0], "");
1562 result
= LLVMBuildNot(ctx
->builder
, src
[0], "");
1565 result
= LLVMBuildAdd(ctx
->builder
, src
[0], src
[1], "");
1568 src
[0] = to_float(ctx
, src
[0]);
1569 src
[1] = to_float(ctx
, src
[1]);
1570 result
= LLVMBuildFAdd(ctx
->builder
, src
[0], src
[1], "");
1573 src
[0] = to_float(ctx
, src
[0]);
1574 src
[1] = to_float(ctx
, src
[1]);
1575 result
= LLVMBuildFSub(ctx
->builder
, src
[0], src
[1], "");
1578 result
= LLVMBuildSub(ctx
->builder
, src
[0], src
[1], "");
1581 result
= LLVMBuildMul(ctx
->builder
, src
[0], src
[1], "");
1584 result
= LLVMBuildSRem(ctx
->builder
, src
[0], src
[1], "");
1587 result
= LLVMBuildURem(ctx
->builder
, src
[0], src
[1], "");
1590 src
[0] = to_float(ctx
, src
[0]);
1591 src
[1] = to_float(ctx
, src
[1]);
1592 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1593 result
= emit_intrin_1f_param(ctx
, "llvm.floor",
1594 to_float_type(ctx
, def_type
), result
);
1595 result
= LLVMBuildFMul(ctx
->builder
, src
[1] , result
, "");
1596 result
= LLVMBuildFSub(ctx
->builder
, src
[0], result
, "");
1599 src
[0] = to_float(ctx
, src
[0]);
1600 src
[1] = to_float(ctx
, src
[1]);
1601 result
= LLVMBuildFRem(ctx
->builder
, src
[0], src
[1], "");
1604 result
= LLVMBuildSRem(ctx
->builder
, src
[0], src
[1], "");
1607 result
= LLVMBuildSDiv(ctx
->builder
, src
[0], src
[1], "");
1610 result
= LLVMBuildUDiv(ctx
->builder
, src
[0], src
[1], "");
1613 src
[0] = to_float(ctx
, src
[0]);
1614 src
[1] = to_float(ctx
, src
[1]);
1615 result
= LLVMBuildFMul(ctx
->builder
, src
[0], src
[1], "");
1618 src
[0] = to_float(ctx
, src
[0]);
1619 src
[1] = to_float(ctx
, src
[1]);
1620 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1623 src
[0] = to_float(ctx
, src
[0]);
1624 result
= ac_build_fdiv(&ctx
->ac
, ctx
->f32one
, src
[0]);
1627 result
= LLVMBuildAnd(ctx
->builder
, src
[0], src
[1], "");
1630 result
= LLVMBuildOr(ctx
->builder
, src
[0], src
[1], "");
1633 result
= LLVMBuildXor(ctx
->builder
, src
[0], src
[1], "");
1636 result
= LLVMBuildShl(ctx
->builder
, src
[0], src
[1], "");
1639 result
= LLVMBuildAShr(ctx
->builder
, src
[0], src
[1], "");
1642 result
= LLVMBuildLShr(ctx
->builder
, src
[0], src
[1], "");
1645 result
= emit_int_cmp(ctx
, LLVMIntSLT
, src
[0], src
[1]);
1648 result
= emit_int_cmp(ctx
, LLVMIntNE
, src
[0], src
[1]);
1651 result
= emit_int_cmp(ctx
, LLVMIntEQ
, src
[0], src
[1]);
1654 result
= emit_int_cmp(ctx
, LLVMIntSGE
, src
[0], src
[1]);
1657 result
= emit_int_cmp(ctx
, LLVMIntULT
, src
[0], src
[1]);
1660 result
= emit_int_cmp(ctx
, LLVMIntUGE
, src
[0], src
[1]);
1663 result
= emit_float_cmp(ctx
, LLVMRealUEQ
, src
[0], src
[1]);
1666 result
= emit_float_cmp(ctx
, LLVMRealUNE
, src
[0], src
[1]);
1669 result
= emit_float_cmp(ctx
, LLVMRealULT
, src
[0], src
[1]);
1672 result
= emit_float_cmp(ctx
, LLVMRealUGE
, src
[0], src
[1]);
1675 result
= emit_intrin_1f_param(ctx
, "llvm.fabs",
1676 to_float_type(ctx
, def_type
), src
[0]);
1679 result
= emit_iabs(ctx
, src
[0]);
1682 result
= emit_minmax_int(ctx
, LLVMIntSGT
, src
[0], src
[1]);
1685 result
= emit_minmax_int(ctx
, LLVMIntSLT
, src
[0], src
[1]);
1688 result
= emit_minmax_int(ctx
, LLVMIntUGT
, src
[0], src
[1]);
1691 result
= emit_minmax_int(ctx
, LLVMIntULT
, src
[0], src
[1]);
1694 result
= emit_isign(ctx
, src
[0]);
1697 src
[0] = to_float(ctx
, src
[0]);
1698 result
= emit_fsign(ctx
, src
[0]);
1701 result
= emit_intrin_1f_param(ctx
, "llvm.floor",
1702 to_float_type(ctx
, def_type
), src
[0]);
1705 result
= emit_intrin_1f_param(ctx
, "llvm.trunc",
1706 to_float_type(ctx
, def_type
), src
[0]);
1709 result
= emit_intrin_1f_param(ctx
, "llvm.ceil",
1710 to_float_type(ctx
, def_type
), src
[0]);
1712 case nir_op_fround_even
:
1713 result
= emit_intrin_1f_param(ctx
, "llvm.rint",
1714 to_float_type(ctx
, def_type
),src
[0]);
1717 result
= emit_ffract(ctx
, src
[0]);
1720 result
= emit_intrin_1f_param(ctx
, "llvm.sin",
1721 to_float_type(ctx
, def_type
), src
[0]);
1724 result
= emit_intrin_1f_param(ctx
, "llvm.cos",
1725 to_float_type(ctx
, def_type
), src
[0]);
1728 result
= emit_intrin_1f_param(ctx
, "llvm.sqrt",
1729 to_float_type(ctx
, def_type
), src
[0]);
1732 result
= emit_intrin_1f_param(ctx
, "llvm.exp2",
1733 to_float_type(ctx
, def_type
), src
[0]);
1736 result
= emit_intrin_1f_param(ctx
, "llvm.log2",
1737 to_float_type(ctx
, def_type
), src
[0]);
1740 result
= emit_intrin_1f_param(ctx
, "llvm.sqrt",
1741 to_float_type(ctx
, def_type
), src
[0]);
1742 result
= ac_build_fdiv(&ctx
->ac
, ctx
->f32one
, result
);
1745 result
= emit_intrin_2f_param(ctx
, "llvm.pow",
1746 to_float_type(ctx
, def_type
), src
[0], src
[1]);
1749 result
= emit_intrin_2f_param(ctx
, "llvm.maxnum",
1750 to_float_type(ctx
, def_type
), src
[0], src
[1]);
1751 if (instr
->dest
.dest
.ssa
.bit_size
== 32)
1752 result
= emit_intrin_1f_param(ctx
, "llvm.canonicalize",
1753 to_float_type(ctx
, def_type
),
1757 result
= emit_intrin_2f_param(ctx
, "llvm.minnum",
1758 to_float_type(ctx
, def_type
), src
[0], src
[1]);
1759 if (instr
->dest
.dest
.ssa
.bit_size
== 32)
1760 result
= emit_intrin_1f_param(ctx
, "llvm.canonicalize",
1761 to_float_type(ctx
, def_type
),
1765 result
= emit_intrin_3f_param(ctx
, "llvm.fma",
1766 to_float_type(ctx
, def_type
), src
[0], src
[1], src
[2]);
1768 case nir_op_ibitfield_extract
:
1769 result
= emit_bitfield_extract(ctx
, true, src
);
1771 case nir_op_ubitfield_extract
:
1772 result
= emit_bitfield_extract(ctx
, false, src
);
1774 case nir_op_bitfield_insert
:
1775 result
= emit_bitfield_insert(ctx
, src
[0], src
[1], src
[2], src
[3]);
1777 case nir_op_bitfield_reverse
:
1778 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.bitreverse.i32", ctx
->i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1780 case nir_op_bit_count
:
1781 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i32", ctx
->i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1786 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1787 src
[i
] = to_integer(ctx
, src
[i
]);
1788 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
1792 src
[0] = to_float(ctx
, src
[0]);
1793 result
= LLVMBuildFPToSI(ctx
->builder
, src
[0], def_type
, "");
1797 src
[0] = to_float(ctx
, src
[0]);
1798 result
= LLVMBuildFPToUI(ctx
->builder
, src
[0], def_type
, "");
1802 result
= LLVMBuildSIToFP(ctx
->builder
, src
[0], to_float_type(ctx
, def_type
), "");
1806 result
= LLVMBuildUIToFP(ctx
->builder
, src
[0], to_float_type(ctx
, def_type
), "");
1809 result
= LLVMBuildFPExt(ctx
->builder
, src
[0], to_float_type(ctx
, def_type
), "");
1812 result
= LLVMBuildFPTrunc(ctx
->builder
, src
[0], to_float_type(ctx
, def_type
), "");
1816 if (get_elem_bits(ctx
, LLVMTypeOf(src
[0])) < get_elem_bits(ctx
, def_type
))
1817 result
= LLVMBuildZExt(ctx
->builder
, src
[0], def_type
, "");
1819 result
= LLVMBuildTrunc(ctx
->builder
, src
[0], def_type
, "");
1823 if (get_elem_bits(ctx
, LLVMTypeOf(src
[0])) < get_elem_bits(ctx
, def_type
))
1824 result
= LLVMBuildSExt(ctx
->builder
, src
[0], def_type
, "");
1826 result
= LLVMBuildTrunc(ctx
->builder
, src
[0], def_type
, "");
1829 result
= emit_bcsel(ctx
, src
[0], src
[1], src
[2]);
1831 case nir_op_find_lsb
:
1832 result
= emit_find_lsb(ctx
, src
[0]);
1834 case nir_op_ufind_msb
:
1835 result
= emit_ufind_msb(ctx
, src
[0]);
1837 case nir_op_ifind_msb
:
1838 result
= emit_ifind_msb(ctx
, src
[0]);
1840 case nir_op_uadd_carry
:
1841 result
= emit_uint_carry(ctx
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
1843 case nir_op_usub_borrow
:
1844 result
= emit_uint_carry(ctx
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
1847 result
= emit_b2f(ctx
, src
[0]);
1849 case nir_op_fquantize2f16
:
1850 result
= emit_f2f16(ctx
, src
[0]);
1852 case nir_op_umul_high
:
1853 result
= emit_umul_high(ctx
, src
[0], src
[1]);
1855 case nir_op_imul_high
:
1856 result
= emit_imul_high(ctx
, src
[0], src
[1]);
1858 case nir_op_pack_half_2x16
:
1859 result
= emit_pack_half_2x16(ctx
, src
[0]);
1861 case nir_op_unpack_half_2x16
:
1862 result
= emit_unpack_half_2x16(ctx
, src
[0]);
1866 case nir_op_fddx_fine
:
1867 case nir_op_fddy_fine
:
1868 case nir_op_fddx_coarse
:
1869 case nir_op_fddy_coarse
:
1870 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
1873 fprintf(stderr
, "Unknown NIR alu instr: ");
1874 nir_print_instr(&instr
->instr
, stderr
);
1875 fprintf(stderr
, "\n");
1880 assert(instr
->dest
.dest
.is_ssa
);
1881 result
= to_integer(ctx
, result
);
1882 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.dest
.ssa
,
1887 static void visit_load_const(struct nir_to_llvm_context
*ctx
,
1888 nir_load_const_instr
*instr
)
1890 LLVMValueRef values
[4], value
= NULL
;
1891 LLVMTypeRef element_type
=
1892 LLVMIntTypeInContext(ctx
->context
, instr
->def
.bit_size
);
1894 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1895 switch (instr
->def
.bit_size
) {
1897 values
[i
] = LLVMConstInt(element_type
,
1898 instr
->value
.u32
[i
], false);
1901 values
[i
] = LLVMConstInt(element_type
,
1902 instr
->value
.u64
[i
], false);
1906 "unsupported nir load_const bit_size: %d\n",
1907 instr
->def
.bit_size
);
1911 if (instr
->def
.num_components
> 1) {
1912 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1916 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, value
);
1919 static LLVMValueRef
cast_ptr(struct nir_to_llvm_context
*ctx
, LLVMValueRef ptr
,
1922 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
1923 return LLVMBuildBitCast(ctx
->builder
, ptr
,
1924 LLVMPointerType(type
, addr_space
), "");
1928 get_buffer_size(struct nir_to_llvm_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1931 LLVMBuildExtractElement(ctx
->builder
, descriptor
,
1932 LLVMConstInt(ctx
->i32
, 2, false), "");
1935 if (ctx
->options
->chip_class
>= VI
&& in_elements
) {
1936 /* On VI, the descriptor contains the size in bytes,
1937 * but TXQ must return the size in elements.
1938 * The stride is always non-zero for resources using TXQ.
1940 LLVMValueRef stride
=
1941 LLVMBuildExtractElement(ctx
->builder
, descriptor
,
1942 LLVMConstInt(ctx
->i32
, 1, false), "");
1943 stride
= LLVMBuildLShr(ctx
->builder
, stride
,
1944 LLVMConstInt(ctx
->i32
, 16, false), "");
1945 stride
= LLVMBuildAnd(ctx
->builder
, stride
,
1946 LLVMConstInt(ctx
->i32
, 0x3fff, false), "");
1948 size
= LLVMBuildUDiv(ctx
->builder
, size
, stride
, "");
1954 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
1957 static void build_int_type_name(
1959 char *buf
, unsigned bufsize
)
1961 assert(bufsize
>= 6);
1963 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
1964 snprintf(buf
, bufsize
, "v%ui32",
1965 LLVMGetVectorSize(type
));
1970 static LLVMValueRef
radv_lower_gather4_integer(struct nir_to_llvm_context
*ctx
,
1971 struct ac_image_args
*args
,
1972 nir_tex_instr
*instr
)
1974 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
1975 LLVMValueRef coord
= args
->addr
;
1976 LLVMValueRef half_texel
[2];
1977 LLVMValueRef compare_cube_wa
;
1978 LLVMValueRef result
;
1980 unsigned coord_vgpr_index
= (unsigned)args
->offset
+ (unsigned)args
->compare
;
1984 struct ac_image_args txq_args
= { 0 };
1986 txq_args
.da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
1987 txq_args
.opcode
= ac_image_get_resinfo
;
1988 txq_args
.dmask
= 0xf;
1989 txq_args
.addr
= ctx
->i32zero
;
1990 txq_args
.resource
= args
->resource
;
1991 LLVMValueRef size
= ac_build_image_opcode(&ctx
->ac
, &txq_args
);
1993 for (c
= 0; c
< 2; c
++) {
1994 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
1995 LLVMConstInt(ctx
->i32
, c
, false), "");
1996 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1997 half_texel
[c
] = ac_build_fdiv(&ctx
->ac
, ctx
->f32one
, half_texel
[c
]);
1998 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1999 LLVMConstReal(ctx
->f32
, -0.5), "");
2003 LLVMValueRef orig_coords
= args
->addr
;
2005 for (c
= 0; c
< 2; c
++) {
2007 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, coord_vgpr_index
+ c
, 0);
2008 tmp
= LLVMBuildExtractElement(ctx
->builder
, coord
, index
, "");
2009 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
2010 tmp
= LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
2011 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
2012 coord
= LLVMBuildInsertElement(ctx
->builder
, coord
, tmp
, index
, "");
2017 * Apparantly cube has issue with integer types that the workaround doesn't solve,
2018 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
2019 * workaround by sampling using a scaled type and converting.
2020 * This is taken from amdgpu-pro shaders.
2022 /* NOTE this produces some ugly code compared to amdgpu-pro,
2023 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
2024 * and then reads them back. -pro generates two selects,
2025 * one s_cmp for the descriptor rewriting
2026 * one v_cmp for the coordinate and result changes.
2028 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
2029 LLVMValueRef tmp
, tmp2
;
2031 /* workaround 8/8/8/8 uint/sint cube gather bug */
2032 /* first detect it then change to a scaled read and f2i */
2033 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32one
, "");
2036 /* extract the DATA_FORMAT */
2037 tmp
= ac_build_bfe(&ctx
->ac
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
2038 LLVMConstInt(ctx
->i32
, 6, false), false);
2040 /* is the DATA_FORMAT == 8_8_8_8 */
2041 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
2043 if (stype
== GLSL_TYPE_UINT
)
2044 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
2045 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
2046 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
2048 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
2049 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
2050 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
2052 /* replace the NUM FORMAT in the descriptor */
2053 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT_GFX6
, false), "");
2054 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
2056 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32one
, "");
2058 /* don't modify the coordinates for this case */
2059 coord
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, orig_coords
, coord
, "");
2062 result
= ac_build_image_opcode(&ctx
->ac
, args
);
2064 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
2065 LLVMValueRef tmp
, tmp2
;
2067 /* if the cube workaround is in place, f2i the result. */
2068 for (c
= 0; c
< 4; c
++) {
2069 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
2070 if (stype
== GLSL_TYPE_UINT
)
2071 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
2073 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
2074 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
2075 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
2076 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
2077 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
2078 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
2084 static LLVMValueRef
build_tex_intrinsic(struct nir_to_llvm_context
*ctx
,
2085 nir_tex_instr
*instr
,
2087 struct ac_image_args
*args
)
2089 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
2090 return ac_build_buffer_load_format(&ctx
->ac
,
2093 LLVMConstInt(ctx
->i32
, 0, false),
2097 args
->opcode
= ac_image_sample
;
2098 args
->compare
= instr
->is_shadow
;
2100 switch (instr
->op
) {
2102 case nir_texop_txf_ms
:
2103 case nir_texop_samples_identical
:
2104 args
->opcode
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
? ac_image_load
: ac_image_load_mip
;
2105 args
->compare
= false;
2106 args
->offset
= false;
2113 args
->level_zero
= true;
2118 case nir_texop_query_levels
:
2119 args
->opcode
= ac_image_get_resinfo
;
2122 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
2123 args
->level_zero
= true;
2129 args
->opcode
= ac_image_gather4
;
2130 args
->level_zero
= true;
2133 args
->opcode
= ac_image_get_lod
;
2134 args
->compare
= false;
2135 args
->offset
= false;
2141 if (instr
->op
== nir_texop_tg4
) {
2142 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
2143 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
2144 return radv_lower_gather4_integer(ctx
, args
, instr
);
2147 return ac_build_image_opcode(&ctx
->ac
, args
);
2150 static LLVMValueRef
visit_vulkan_resource_index(struct nir_to_llvm_context
*ctx
,
2151 nir_intrinsic_instr
*instr
)
2153 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
2154 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
2155 unsigned binding
= nir_intrinsic_binding(instr
);
2156 LLVMValueRef desc_ptr
= ctx
->descriptor_sets
[desc_set
];
2157 struct radv_pipeline_layout
*pipeline_layout
= ctx
->options
->layout
;
2158 struct radv_descriptor_set_layout
*layout
= pipeline_layout
->set
[desc_set
].layout
;
2159 unsigned base_offset
= layout
->binding
[binding
].offset
;
2160 LLVMValueRef offset
, stride
;
2162 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
2163 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
2164 unsigned idx
= pipeline_layout
->set
[desc_set
].dynamic_offset_start
+
2165 layout
->binding
[binding
].dynamic_offset_offset
;
2166 desc_ptr
= ctx
->push_constants
;
2167 base_offset
= pipeline_layout
->push_constant_size
+ 16 * idx
;
2168 stride
= LLVMConstInt(ctx
->i32
, 16, false);
2170 stride
= LLVMConstInt(ctx
->i32
, layout
->binding
[binding
].size
, false);
2172 offset
= LLVMConstInt(ctx
->i32
, base_offset
, false);
2173 index
= LLVMBuildMul(ctx
->builder
, index
, stride
, "");
2174 offset
= LLVMBuildAdd(ctx
->builder
, offset
, index
, "");
2176 desc_ptr
= ac_build_gep0(&ctx
->ac
, desc_ptr
, offset
);
2177 desc_ptr
= cast_ptr(ctx
, desc_ptr
, ctx
->v4i32
);
2178 LLVMSetMetadata(desc_ptr
, ctx
->uniform_md_kind
, ctx
->empty_md
);
2180 return LLVMBuildLoad(ctx
->builder
, desc_ptr
, "");
2183 static LLVMValueRef
visit_load_push_constant(struct nir_to_llvm_context
*ctx
,
2184 nir_intrinsic_instr
*instr
)
2186 LLVMValueRef ptr
, addr
;
2188 addr
= LLVMConstInt(ctx
->i32
, nir_intrinsic_base(instr
), 0);
2189 addr
= LLVMBuildAdd(ctx
->builder
, addr
, get_src(ctx
, instr
->src
[0]), "");
2191 ptr
= ac_build_gep0(&ctx
->ac
, ctx
->push_constants
, addr
);
2192 ptr
= cast_ptr(ctx
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
2194 return LLVMBuildLoad(ctx
->builder
, ptr
, "");
2197 static LLVMValueRef
visit_get_buffer_size(struct nir_to_llvm_context
*ctx
,
2198 nir_intrinsic_instr
*instr
)
2200 LLVMValueRef desc
= get_src(ctx
, instr
->src
[0]);
2202 return get_buffer_size(ctx
, desc
, false);
2204 static void visit_store_ssbo(struct nir_to_llvm_context
*ctx
,
2205 nir_intrinsic_instr
*instr
)
2207 const char *store_name
;
2208 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
2209 LLVMTypeRef data_type
= ctx
->f32
;
2210 int elem_size_mult
= get_elem_bits(ctx
, LLVMTypeOf(src_data
)) / 32;
2211 int components_32bit
= elem_size_mult
* instr
->num_components
;
2212 unsigned writemask
= nir_intrinsic_write_mask(instr
);
2213 LLVMValueRef base_data
, base_offset
;
2214 LLVMValueRef params
[6];
2216 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
2217 ctx
->shader_info
->fs
.writes_memory
= true;
2219 params
[1] = get_src(ctx
, instr
->src
[1]);
2220 params
[2] = LLVMConstInt(ctx
->i32
, 0, false); /* vindex */
2221 params
[4] = ctx
->i1false
; /* glc */
2222 params
[5] = ctx
->i1false
; /* slc */
2224 if (components_32bit
> 1)
2225 data_type
= LLVMVectorType(ctx
->f32
, components_32bit
);
2227 base_data
= to_float(ctx
, src_data
);
2228 base_data
= trim_vector(ctx
, base_data
, instr
->num_components
);
2229 base_data
= LLVMBuildBitCast(ctx
->builder
, base_data
,
2231 base_offset
= get_src(ctx
, instr
->src
[2]); /* voffset */
2235 LLVMValueRef offset
;
2237 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
2239 /* Due to an LLVM limitation, split 3-element writes
2240 * into a 2-element and a 1-element write. */
2242 writemask
|= 1 << (start
+ 2);
2246 start
*= elem_size_mult
;
2247 count
*= elem_size_mult
;
2250 writemask
|= ((1u << (count
- 4)) - 1u) << (start
+ 4);
2255 store_name
= "llvm.amdgcn.buffer.store.v4f32";
2257 } else if (count
== 2) {
2258 tmp
= LLVMBuildExtractElement(ctx
->builder
,
2259 base_data
, LLVMConstInt(ctx
->i32
, start
, false), "");
2260 data
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), tmp
,
2263 tmp
= LLVMBuildExtractElement(ctx
->builder
,
2264 base_data
, LLVMConstInt(ctx
->i32
, start
+ 1, false), "");
2265 data
= LLVMBuildInsertElement(ctx
->builder
, data
, tmp
,
2267 store_name
= "llvm.amdgcn.buffer.store.v2f32";
2271 if (get_llvm_num_components(base_data
) > 1)
2272 data
= LLVMBuildExtractElement(ctx
->builder
, base_data
,
2273 LLVMConstInt(ctx
->i32
, start
, false), "");
2276 store_name
= "llvm.amdgcn.buffer.store.f32";
2279 offset
= base_offset
;
2281 offset
= LLVMBuildAdd(ctx
->builder
, offset
, LLVMConstInt(ctx
->i32
, start
* 4, false), "");
2285 ac_build_intrinsic(&ctx
->ac
, store_name
,
2286 ctx
->voidt
, params
, 6, 0);
2290 static LLVMValueRef
visit_atomic_ssbo(struct nir_to_llvm_context
*ctx
,
2291 nir_intrinsic_instr
*instr
)
2294 LLVMValueRef params
[6];
2296 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
2297 ctx
->shader_info
->fs
.writes_memory
= true;
2299 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
2300 params
[arg_count
++] = llvm_extract_elem(ctx
, get_src(ctx
, instr
->src
[3]), 0);
2302 params
[arg_count
++] = llvm_extract_elem(ctx
, get_src(ctx
, instr
->src
[2]), 0);
2303 params
[arg_count
++] = get_src(ctx
, instr
->src
[0]);
2304 params
[arg_count
++] = LLVMConstInt(ctx
->i32
, 0, false); /* vindex */
2305 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
2306 params
[arg_count
++] = ctx
->i1false
; /* slc */
2308 switch (instr
->intrinsic
) {
2309 case nir_intrinsic_ssbo_atomic_add
:
2310 name
= "llvm.amdgcn.buffer.atomic.add";
2312 case nir_intrinsic_ssbo_atomic_imin
:
2313 name
= "llvm.amdgcn.buffer.atomic.smin";
2315 case nir_intrinsic_ssbo_atomic_umin
:
2316 name
= "llvm.amdgcn.buffer.atomic.umin";
2318 case nir_intrinsic_ssbo_atomic_imax
:
2319 name
= "llvm.amdgcn.buffer.atomic.smax";
2321 case nir_intrinsic_ssbo_atomic_umax
:
2322 name
= "llvm.amdgcn.buffer.atomic.umax";
2324 case nir_intrinsic_ssbo_atomic_and
:
2325 name
= "llvm.amdgcn.buffer.atomic.and";
2327 case nir_intrinsic_ssbo_atomic_or
:
2328 name
= "llvm.amdgcn.buffer.atomic.or";
2330 case nir_intrinsic_ssbo_atomic_xor
:
2331 name
= "llvm.amdgcn.buffer.atomic.xor";
2333 case nir_intrinsic_ssbo_atomic_exchange
:
2334 name
= "llvm.amdgcn.buffer.atomic.swap";
2336 case nir_intrinsic_ssbo_atomic_comp_swap
:
2337 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
2343 return ac_build_intrinsic(&ctx
->ac
, name
, ctx
->i32
, params
, arg_count
, 0);
2346 static LLVMValueRef
visit_load_buffer(struct nir_to_llvm_context
*ctx
,
2347 nir_intrinsic_instr
*instr
)
2349 LLVMValueRef results
[2];
2350 int load_components
;
2351 int num_components
= instr
->num_components
;
2352 if (instr
->dest
.ssa
.bit_size
== 64)
2353 num_components
*= 2;
2355 for (int i
= 0; i
< num_components
; i
+= load_components
) {
2356 load_components
= MIN2(num_components
- i
, 4);
2357 const char *load_name
;
2358 LLVMTypeRef data_type
= ctx
->f32
;
2359 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, i
* 4, false);
2360 offset
= LLVMBuildAdd(ctx
->builder
, get_src(ctx
, instr
->src
[1]), offset
, "");
2362 if (load_components
== 3)
2363 data_type
= LLVMVectorType(ctx
->f32
, 4);
2364 else if (load_components
> 1)
2365 data_type
= LLVMVectorType(ctx
->f32
, load_components
);
2367 if (load_components
>= 3)
2368 load_name
= "llvm.amdgcn.buffer.load.v4f32";
2369 else if (load_components
== 2)
2370 load_name
= "llvm.amdgcn.buffer.load.v2f32";
2371 else if (load_components
== 1)
2372 load_name
= "llvm.amdgcn.buffer.load.f32";
2374 unreachable("unhandled number of components");
2376 LLVMValueRef params
[] = {
2377 get_src(ctx
, instr
->src
[0]),
2378 LLVMConstInt(ctx
->i32
, 0, false),
2384 results
[i
] = ac_build_intrinsic(&ctx
->ac
, load_name
, data_type
, params
, 5, 0);
2388 LLVMValueRef ret
= results
[0];
2389 if (num_components
> 4 || num_components
== 3) {
2390 LLVMValueRef masks
[] = {
2391 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
2392 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false),
2393 LLVMConstInt(ctx
->i32
, 4, false), LLVMConstInt(ctx
->i32
, 5, false),
2394 LLVMConstInt(ctx
->i32
, 6, false), LLVMConstInt(ctx
->i32
, 7, false)
2397 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
2398 ret
= LLVMBuildShuffleVector(ctx
->builder
, results
[0],
2399 results
[num_components
> 4 ? 1 : 0], swizzle
, "");
2402 return LLVMBuildBitCast(ctx
->builder
, ret
,
2403 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2406 static LLVMValueRef
visit_load_ubo_buffer(struct nir_to_llvm_context
*ctx
,
2407 nir_intrinsic_instr
*instr
)
2409 LLVMValueRef results
[8], ret
;
2410 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
2411 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
2412 int num_components
= instr
->num_components
;
2414 rsrc
= LLVMBuildBitCast(ctx
->builder
, rsrc
, LLVMVectorType(ctx
->i8
, 16), "");
2416 if (instr
->dest
.ssa
.bit_size
== 64)
2417 num_components
*= 2;
2419 for (unsigned i
= 0; i
< num_components
; ++i
) {
2420 LLVMValueRef params
[] = {
2422 LLVMBuildAdd(ctx
->builder
, LLVMConstInt(ctx
->i32
, 4 * i
, 0),
2425 results
[i
] = ac_build_intrinsic(&ctx
->ac
, "llvm.SI.load.const", ctx
->f32
,
2427 AC_FUNC_ATTR_READNONE
|
2428 AC_FUNC_ATTR_LEGACY
);
2432 ret
= ac_build_gather_values(&ctx
->ac
, results
, instr
->num_components
);
2433 return LLVMBuildBitCast(ctx
->builder
, ret
,
2434 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2438 radv_get_deref_offset(struct nir_to_llvm_context
*ctx
, nir_deref_var
*deref
,
2439 bool vs_in
, unsigned *vertex_index_out
,
2440 LLVMValueRef
*vertex_index_ref
,
2441 unsigned *const_out
, LLVMValueRef
*indir_out
)
2443 unsigned const_offset
= 0;
2444 nir_deref
*tail
= &deref
->deref
;
2445 LLVMValueRef offset
= NULL
;
2447 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
2449 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2450 if (vertex_index_out
)
2451 *vertex_index_out
= deref_array
->base_offset
;
2453 if (vertex_index_ref
) {
2454 LLVMValueRef vtx
= LLVMConstInt(ctx
->i32
, deref_array
->base_offset
, false);
2455 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
2456 vtx
= LLVMBuildAdd(ctx
->builder
, vtx
, get_src(ctx
, deref_array
->indirect
), "");
2458 *vertex_index_ref
= vtx
;
2462 if (deref
->var
->data
.compact
) {
2463 assert(tail
->child
->deref_type
== nir_deref_type_array
);
2464 assert(glsl_type_is_scalar(glsl_without_array(deref
->var
->type
)));
2465 nir_deref_array
*deref_array
= nir_deref_as_array(tail
->child
);
2466 /* We always lower indirect dereferences for "compact" array vars. */
2467 assert(deref_array
->deref_array_type
== nir_deref_array_type_direct
);
2469 const_offset
= deref_array
->base_offset
;
2473 while (tail
->child
!= NULL
) {
2474 const struct glsl_type
*parent_type
= tail
->type
;
2477 if (tail
->deref_type
== nir_deref_type_array
) {
2478 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2479 LLVMValueRef index
, stride
, local_offset
;
2480 unsigned size
= glsl_count_attribute_slots(tail
->type
, vs_in
);
2482 const_offset
+= size
* deref_array
->base_offset
;
2483 if (deref_array
->deref_array_type
== nir_deref_array_type_direct
)
2486 assert(deref_array
->deref_array_type
== nir_deref_array_type_indirect
);
2487 index
= get_src(ctx
, deref_array
->indirect
);
2488 stride
= LLVMConstInt(ctx
->i32
, size
, 0);
2489 local_offset
= LLVMBuildMul(ctx
->builder
, stride
, index
, "");
2492 offset
= LLVMBuildAdd(ctx
->builder
, offset
, local_offset
, "");
2494 offset
= local_offset
;
2495 } else if (tail
->deref_type
== nir_deref_type_struct
) {
2496 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
2498 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
2499 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
2500 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
2503 unreachable("unsupported deref type");
2507 if (const_offset
&& offset
)
2508 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
2509 LLVMConstInt(ctx
->i32
, const_offset
, 0),
2512 *const_out
= const_offset
;
2513 *indir_out
= offset
;
2517 lds_load(struct nir_to_llvm_context
*ctx
,
2518 LLVMValueRef dw_addr
)
2521 value
= ac_build_indexed_load(&ctx
->ac
, ctx
->lds
, dw_addr
, false);
2526 lds_store(struct nir_to_llvm_context
*ctx
,
2527 LLVMValueRef dw_addr
, LLVMValueRef value
)
2529 value
= LLVMBuildBitCast(ctx
->builder
, value
, ctx
->i32
, "");
2530 ac_build_indexed_store(&ctx
->ac
, ctx
->lds
,
2534 /* The offchip buffer layout for TCS->TES is
2536 * - attribute 0 of patch 0 vertex 0
2537 * - attribute 0 of patch 0 vertex 1
2538 * - attribute 0 of patch 0 vertex 2
2540 * - attribute 0 of patch 1 vertex 0
2541 * - attribute 0 of patch 1 vertex 1
2543 * - attribute 1 of patch 0 vertex 0
2544 * - attribute 1 of patch 0 vertex 1
2546 * - per patch attribute 0 of patch 0
2547 * - per patch attribute 0 of patch 1
2550 * Note that every attribute has 4 components.
2552 static LLVMValueRef
get_tcs_tes_buffer_address(struct nir_to_llvm_context
*ctx
,
2553 LLVMValueRef vertex_index
,
2554 LLVMValueRef param_index
)
2556 LLVMValueRef base_addr
, vertices_per_patch
, num_patches
, total_vertices
;
2557 LLVMValueRef param_stride
, constant16
;
2558 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
2560 vertices_per_patch
= unpack_param(ctx
, ctx
->tcs_offchip_layout
, 9, 6);
2561 num_patches
= unpack_param(ctx
, ctx
->tcs_offchip_layout
, 0, 9);
2562 total_vertices
= LLVMBuildMul(ctx
->builder
, vertices_per_patch
,
2565 constant16
= LLVMConstInt(ctx
->i32
, 16, false);
2567 base_addr
= LLVMBuildMul(ctx
->builder
, rel_patch_id
,
2568 vertices_per_patch
, "");
2570 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2573 param_stride
= total_vertices
;
2575 base_addr
= rel_patch_id
;
2576 param_stride
= num_patches
;
2579 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2580 LLVMBuildMul(ctx
->builder
, param_index
,
2581 param_stride
, ""), "");
2583 base_addr
= LLVMBuildMul(ctx
->builder
, base_addr
, constant16
, "");
2585 if (!vertex_index
) {
2586 LLVMValueRef patch_data_offset
=
2587 unpack_param(ctx
, ctx
->tcs_offchip_layout
, 16, 16);
2589 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2590 patch_data_offset
, "");
2595 static LLVMValueRef
get_tcs_tes_buffer_address_params(struct nir_to_llvm_context
*ctx
,
2597 unsigned const_index
,
2599 LLVMValueRef vertex_index
,
2600 LLVMValueRef indir_index
)
2602 LLVMValueRef param_index
;
2605 param_index
= LLVMBuildAdd(ctx
->builder
, LLVMConstInt(ctx
->i32
, param
, false),
2608 if (const_index
&& !is_compact
)
2609 param
+= const_index
;
2610 param_index
= LLVMConstInt(ctx
->i32
, param
, false);
2612 return get_tcs_tes_buffer_address(ctx
, vertex_index
, param_index
);
2616 mark_tess_output(struct nir_to_llvm_context
*ctx
,
2617 bool is_patch
, uint32_t param
)
2621 ctx
->tess_patch_outputs_written
|= (1ull << param
);
2623 ctx
->tess_outputs_written
|= (1ull << param
);
2627 get_dw_address(struct nir_to_llvm_context
*ctx
,
2628 LLVMValueRef dw_addr
,
2630 unsigned const_index
,
2631 bool compact_const_index
,
2632 LLVMValueRef vertex_index
,
2633 LLVMValueRef stride
,
2634 LLVMValueRef indir_index
)
2639 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2640 LLVMBuildMul(ctx
->builder
,
2646 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2647 LLVMBuildMul(ctx
->builder
, indir_index
,
2648 LLVMConstInt(ctx
->i32
, 4, false), ""), "");
2649 else if (const_index
&& !compact_const_index
)
2650 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2651 LLVMConstInt(ctx
->i32
, const_index
, false), "");
2653 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2654 LLVMConstInt(ctx
->i32
, param
* 4, false), "");
2656 if (const_index
&& compact_const_index
)
2657 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2658 LLVMConstInt(ctx
->i32
, const_index
, false), "");
2663 load_tcs_input(struct nir_to_llvm_context
*ctx
,
2664 nir_intrinsic_instr
*instr
)
2666 LLVMValueRef dw_addr
, stride
;
2667 unsigned const_index
;
2668 LLVMValueRef vertex_index
;
2669 LLVMValueRef indir_index
;
2671 LLVMValueRef value
[4], result
;
2672 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2673 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2674 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2675 radv_get_deref_offset(ctx
, instr
->variables
[0],
2676 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2677 &const_index
, &indir_index
);
2679 stride
= unpack_param(ctx
, ctx
->tcs_in_layout
, 13, 8);
2680 dw_addr
= get_tcs_in_current_patch_offset(ctx
);
2681 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2684 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
2685 value
[i
] = lds_load(ctx
, dw_addr
);
2686 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2689 result
= ac_build_gather_values(&ctx
->ac
, value
, instr
->num_components
);
2690 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2695 load_tcs_output(struct nir_to_llvm_context
*ctx
,
2696 nir_intrinsic_instr
*instr
)
2698 LLVMValueRef dw_addr
, stride
;
2699 LLVMValueRef value
[4], result
;
2700 LLVMValueRef vertex_index
= NULL
;
2701 LLVMValueRef indir_index
= NULL
;
2702 unsigned const_index
= 0;
2704 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2705 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2706 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2707 radv_get_deref_offset(ctx
, instr
->variables
[0],
2708 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2709 &const_index
, &indir_index
);
2711 if (!instr
->variables
[0]->var
->data
.patch
) {
2712 stride
= unpack_param(ctx
, ctx
->tcs_out_layout
, 13, 8);
2713 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
2715 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
2718 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2721 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
2722 value
[i
] = lds_load(ctx
, dw_addr
);
2723 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2726 result
= ac_build_gather_values(&ctx
->ac
, value
, instr
->num_components
);
2727 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2732 store_tcs_output(struct nir_to_llvm_context
*ctx
,
2733 nir_intrinsic_instr
*instr
,
2737 LLVMValueRef stride
, dw_addr
;
2738 LLVMValueRef buf_addr
= NULL
;
2739 LLVMValueRef vertex_index
= NULL
;
2740 LLVMValueRef indir_index
= NULL
;
2741 unsigned const_index
= 0;
2743 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2744 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2746 radv_get_deref_offset(ctx
, instr
->variables
[0],
2747 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2748 &const_index
, &indir_index
);
2750 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2751 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CLIP_DIST0
&&
2752 is_compact
&& const_index
> 3) {
2757 if (!instr
->variables
[0]->var
->data
.patch
) {
2758 stride
= unpack_param(ctx
, ctx
->tcs_out_layout
, 13, 8);
2759 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
2761 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
2764 mark_tess_output(ctx
, instr
->variables
[0]->var
->data
.patch
, param
);
2766 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2768 buf_addr
= get_tcs_tes_buffer_address_params(ctx
, param
, const_index
, is_compact
,
2769 vertex_index
, indir_index
);
2771 unsigned base
= is_compact
? const_index
: 0;
2772 for (unsigned chan
= 0; chan
< 8; chan
++) {
2773 bool is_tess_factor
= false;
2774 if (!(writemask
& (1 << chan
)))
2776 LLVMValueRef value
= llvm_extract_elem(ctx
, src
, chan
);
2778 lds_store(ctx
, dw_addr
, value
);
2780 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_TESS_LEVEL_INNER
||
2781 instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_TESS_LEVEL_OUTER
)
2782 is_tess_factor
= true;
2784 if (!is_tess_factor
&& writemask
!= 0xF)
2785 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, value
, 1,
2786 buf_addr
, ctx
->oc_lds
,
2787 4 * (base
+ chan
), 1, 0, true, false);
2789 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2793 if (writemask
== 0xF) {
2794 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, src
, 4,
2795 buf_addr
, ctx
->oc_lds
,
2796 (base
* 4), 1, 0, true, false);
2801 load_tes_input(struct nir_to_llvm_context
*ctx
,
2802 nir_intrinsic_instr
*instr
)
2804 LLVMValueRef buf_addr
;
2805 LLVMValueRef result
;
2806 LLVMValueRef vertex_index
= NULL
;
2807 LLVMValueRef indir_index
= NULL
;
2808 unsigned const_index
= 0;
2810 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2811 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2813 radv_get_deref_offset(ctx
, instr
->variables
[0],
2814 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2815 &const_index
, &indir_index
);
2816 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2817 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CLIP_DIST0
&&
2818 is_compact
&& const_index
> 3) {
2822 buf_addr
= get_tcs_tes_buffer_address_params(ctx
, param
, const_index
,
2823 is_compact
, vertex_index
, indir_index
);
2825 result
= ac_build_buffer_load(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, instr
->num_components
, NULL
,
2826 buf_addr
, ctx
->oc_lds
, is_compact
? (4 * const_index
) : 0, 1, 0, true, false);
2827 result
= trim_vector(ctx
, result
, instr
->num_components
);
2828 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2833 load_gs_input(struct nir_to_llvm_context
*ctx
,
2834 nir_intrinsic_instr
*instr
)
2836 LLVMValueRef indir_index
, vtx_offset
;
2837 unsigned const_index
;
2838 LLVMValueRef args
[9];
2839 unsigned param
, vtx_offset_param
;
2840 LLVMValueRef value
[4], result
;
2841 unsigned vertex_index
;
2842 radv_get_deref_offset(ctx
, instr
->variables
[0],
2843 false, &vertex_index
, NULL
,
2844 &const_index
, &indir_index
);
2845 vtx_offset_param
= vertex_index
;
2846 assert(vtx_offset_param
< 6);
2847 vtx_offset
= LLVMBuildMul(ctx
->builder
, ctx
->gs_vtx_offset
[vtx_offset_param
],
2848 LLVMConstInt(ctx
->i32
, 4, false), "");
2850 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2851 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
2853 args
[0] = ctx
->esgs_ring
;
2854 args
[1] = vtx_offset
;
2855 args
[2] = LLVMConstInt(ctx
->i32
, (param
* 4 + i
+ const_index
) * 256, false);
2856 args
[3] = ctx
->i32zero
;
2857 args
[4] = ctx
->i32one
; /* OFFEN */
2858 args
[5] = ctx
->i32zero
; /* IDXEN */
2859 args
[6] = ctx
->i32one
; /* GLC */
2860 args
[7] = ctx
->i32zero
; /* SLC */
2861 args
[8] = ctx
->i32zero
; /* TFE */
2863 value
[i
] = ac_build_intrinsic(&ctx
->ac
, "llvm.SI.buffer.load.dword.i32.i32",
2865 AC_FUNC_ATTR_READONLY
|
2866 AC_FUNC_ATTR_LEGACY
);
2868 result
= ac_build_gather_values(&ctx
->ac
, value
, instr
->num_components
);
2873 static LLVMValueRef
visit_load_var(struct nir_to_llvm_context
*ctx
,
2874 nir_intrinsic_instr
*instr
)
2876 LLVMValueRef values
[8];
2877 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2878 int ve
= instr
->dest
.ssa
.num_components
;
2879 LLVMValueRef indir_index
;
2881 unsigned const_index
;
2882 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
2883 instr
->variables
[0]->var
->data
.mode
== nir_var_shader_in
;
2884 radv_get_deref_offset(ctx
, instr
->variables
[0], vs_in
, NULL
, NULL
,
2885 &const_index
, &indir_index
);
2887 if (instr
->dest
.ssa
.bit_size
== 64)
2890 switch (instr
->variables
[0]->var
->data
.mode
) {
2891 case nir_var_shader_in
:
2892 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
2893 return load_tcs_input(ctx
, instr
);
2894 if (ctx
->stage
== MESA_SHADER_TESS_EVAL
)
2895 return load_tes_input(ctx
, instr
);
2896 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
2897 return load_gs_input(ctx
, instr
);
2899 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2901 unsigned count
= glsl_count_attribute_slots(
2902 instr
->variables
[0]->var
->type
,
2903 ctx
->stage
== MESA_SHADER_VERTEX
);
2905 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2906 &ctx
->ac
, ctx
->inputs
+ idx
+ chan
, count
,
2909 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2913 values
[chan
] = ctx
->inputs
[idx
+ chan
+ const_index
* 4];
2917 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2919 unsigned count
= glsl_count_attribute_slots(
2920 instr
->variables
[0]->var
->type
, false);
2922 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2923 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2926 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2930 values
[chan
] = LLVMBuildLoad(ctx
->builder
, ctx
->locals
[idx
+ chan
+ const_index
* 4], "");
2934 case nir_var_shader_out
:
2935 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
2936 return load_tcs_output(ctx
, instr
);
2937 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2939 unsigned count
= glsl_count_attribute_slots(
2940 instr
->variables
[0]->var
->type
, false);
2942 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2943 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
2946 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2950 values
[chan
] = LLVMBuildLoad(ctx
->builder
,
2951 ctx
->outputs
[idx
+ chan
+ const_index
* 4],
2956 case nir_var_shared
: {
2957 LLVMValueRef ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
2958 LLVMValueRef derived_ptr
;
2961 indir_index
= LLVMBuildMul(ctx
->builder
, indir_index
, LLVMConstInt(ctx
->i32
, 4, false), "");
2963 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2964 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, chan
, false);
2966 index
= LLVMBuildAdd(ctx
->builder
, index
, indir_index
, "");
2967 derived_ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, "");
2969 values
[chan
] = LLVMBuildLoad(ctx
->builder
, derived_ptr
, "");
2974 unreachable("unhandle variable mode");
2976 ret
= ac_build_gather_values(&ctx
->ac
, values
, ve
);
2977 return LLVMBuildBitCast(ctx
->builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2981 visit_store_var(struct nir_to_llvm_context
*ctx
,
2982 nir_intrinsic_instr
*instr
)
2984 LLVMValueRef temp_ptr
, value
;
2985 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2986 LLVMValueRef src
= to_float(ctx
, get_src(ctx
, instr
->src
[0]));
2987 int writemask
= instr
->const_index
[0];
2988 LLVMValueRef indir_index
;
2989 unsigned const_index
;
2990 radv_get_deref_offset(ctx
, instr
->variables
[0], false,
2991 NULL
, NULL
, &const_index
, &indir_index
);
2993 if (get_elem_bits(ctx
, LLVMTypeOf(src
)) == 64) {
2994 int old_writemask
= writemask
;
2996 src
= LLVMBuildBitCast(ctx
->builder
, src
,
2997 LLVMVectorType(ctx
->f32
, get_llvm_num_components(src
) * 2),
3001 for (unsigned chan
= 0; chan
< 4; chan
++) {
3002 if (old_writemask
& (1 << chan
))
3003 writemask
|= 3u << (2 * chan
);
3007 switch (instr
->variables
[0]->var
->data
.mode
) {
3008 case nir_var_shader_out
:
3010 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3011 store_tcs_output(ctx
, instr
, src
, writemask
);
3015 for (unsigned chan
= 0; chan
< 8; chan
++) {
3017 if (!(writemask
& (1 << chan
)))
3020 value
= llvm_extract_elem(ctx
, src
, chan
);
3022 if (instr
->variables
[0]->var
->data
.compact
)
3025 unsigned count
= glsl_count_attribute_slots(
3026 instr
->variables
[0]->var
->type
, false);
3028 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3029 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
3032 if (get_llvm_num_components(tmp_vec
) > 1) {
3033 tmp_vec
= LLVMBuildInsertElement(ctx
->builder
, tmp_vec
,
3034 value
, indir_index
, "");
3037 build_store_values_extended(ctx
, ctx
->outputs
+ idx
+ chan
,
3038 count
, stride
, tmp_vec
);
3041 temp_ptr
= ctx
->outputs
[idx
+ chan
+ const_index
* stride
];
3043 LLVMBuildStore(ctx
->builder
, value
, temp_ptr
);
3048 for (unsigned chan
= 0; chan
< 8; chan
++) {
3049 if (!(writemask
& (1 << chan
)))
3052 value
= llvm_extract_elem(ctx
, src
, chan
);
3054 unsigned count
= glsl_count_attribute_slots(
3055 instr
->variables
[0]->var
->type
, false);
3057 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3058 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
3061 tmp_vec
= LLVMBuildInsertElement(ctx
->builder
, tmp_vec
,
3062 value
, indir_index
, "");
3063 build_store_values_extended(ctx
, ctx
->locals
+ idx
+ chan
,
3066 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
3068 LLVMBuildStore(ctx
->builder
, value
, temp_ptr
);
3072 case nir_var_shared
: {
3073 LLVMValueRef ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
3076 indir_index
= LLVMBuildMul(ctx
->builder
, indir_index
, LLVMConstInt(ctx
->i32
, 4, false), "");
3078 for (unsigned chan
= 0; chan
< 8; chan
++) {
3079 if (!(writemask
& (1 << chan
)))
3081 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, chan
, false);
3082 LLVMValueRef derived_ptr
;
3085 index
= LLVMBuildAdd(ctx
->builder
, index
, indir_index
, "");
3087 value
= llvm_extract_elem(ctx
, src
, chan
);
3088 derived_ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, "");
3089 LLVMBuildStore(ctx
->builder
,
3090 to_integer(ctx
, value
), derived_ptr
);
3099 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
3102 case GLSL_SAMPLER_DIM_BUF
:
3104 case GLSL_SAMPLER_DIM_1D
:
3105 return array
? 2 : 1;
3106 case GLSL_SAMPLER_DIM_2D
:
3107 return array
? 3 : 2;
3108 case GLSL_SAMPLER_DIM_MS
:
3109 return array
? 4 : 3;
3110 case GLSL_SAMPLER_DIM_3D
:
3111 case GLSL_SAMPLER_DIM_CUBE
:
3113 case GLSL_SAMPLER_DIM_RECT
:
3114 case GLSL_SAMPLER_DIM_SUBPASS
:
3116 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
3126 /* Adjust the sample index according to FMASK.
3128 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3129 * which is the identity mapping. Each nibble says which physical sample
3130 * should be fetched to get that sample.
3132 * For example, 0x11111100 means there are only 2 samples stored and
3133 * the second sample covers 3/4 of the pixel. When reading samples 0
3134 * and 1, return physical sample 0 (determined by the first two 0s
3135 * in FMASK), otherwise return physical sample 1.
3137 * The sample index should be adjusted as follows:
3138 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
3140 static LLVMValueRef
adjust_sample_index_using_fmask(struct nir_to_llvm_context
*ctx
,
3141 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
3142 LLVMValueRef coord_z
,
3143 LLVMValueRef sample_index
,
3144 LLVMValueRef fmask_desc_ptr
)
3146 LLVMValueRef fmask_load_address
[4];
3149 fmask_load_address
[0] = coord_x
;
3150 fmask_load_address
[1] = coord_y
;
3152 fmask_load_address
[2] = coord_z
;
3153 fmask_load_address
[3] = LLVMGetUndef(ctx
->i32
);
3156 struct ac_image_args args
= {0};
3158 args
.opcode
= ac_image_load
;
3159 args
.da
= coord_z
? true : false;
3160 args
.resource
= fmask_desc_ptr
;
3162 args
.addr
= ac_build_gather_values(&ctx
->ac
, fmask_load_address
, coord_z
? 4 : 2);
3164 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
3166 res
= to_integer(ctx
, res
);
3167 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
3168 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
3170 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
3174 LLVMValueRef sample_index4
=
3175 LLVMBuildMul(ctx
->builder
, sample_index
, four
, "");
3176 LLVMValueRef shifted_fmask
=
3177 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
3178 LLVMValueRef final_sample
=
3179 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
3181 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3182 * resource descriptor is 0 (invalid),
3184 LLVMValueRef fmask_desc
=
3185 LLVMBuildBitCast(ctx
->builder
, fmask_desc_ptr
,
3188 LLVMValueRef fmask_word1
=
3189 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
3192 LLVMValueRef word1_is_nonzero
=
3193 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3194 fmask_word1
, ctx
->i32zero
, "");
3196 /* Replace the MSAA sample index. */
3198 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
3199 final_sample
, sample_index
, "");
3200 return sample_index
;
3203 static LLVMValueRef
get_image_coords(struct nir_to_llvm_context
*ctx
,
3204 nir_intrinsic_instr
*instr
)
3206 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
3207 if(instr
->variables
[0]->deref
.child
)
3208 type
= instr
->variables
[0]->deref
.child
->type
;
3210 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
3211 LLVMValueRef coords
[4];
3212 LLVMValueRef masks
[] = {
3213 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
3214 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false),
3217 LLVMValueRef sample_index
= llvm_extract_elem(ctx
, get_src(ctx
, instr
->src
[1]), 0);
3220 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
3221 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3222 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3223 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
3224 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3226 count
= image_type_to_components_count(dim
,
3227 glsl_sampler_type_is_array(type
));
3230 LLVMValueRef fmask_load_address
[3];
3233 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[0], "");
3234 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[1], "");
3235 if (glsl_sampler_type_is_array(type
))
3236 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[2], "");
3238 fmask_load_address
[2] = NULL
;
3240 for (chan
= 0; chan
< 2; ++chan
)
3241 fmask_load_address
[chan
] = LLVMBuildAdd(ctx
->builder
, fmask_load_address
[chan
], LLVMBuildFPToUI(ctx
->builder
, ctx
->frag_pos
[chan
], ctx
->i32
, ""), "");
3243 sample_index
= adjust_sample_index_using_fmask(ctx
,
3244 fmask_load_address
[0],
3245 fmask_load_address
[1],
3246 fmask_load_address
[2],
3248 get_sampler_desc(ctx
, instr
->variables
[0], DESC_FMASK
));
3251 if (instr
->src
[0].ssa
->num_components
)
3252 res
= LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[0], "");
3259 for (chan
= 0; chan
< count
; ++chan
) {
3260 coords
[chan
] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[chan
], "");
3264 for (chan
= 0; chan
< count
; ++chan
)
3265 coords
[chan
] = LLVMBuildAdd(ctx
->builder
, coords
[chan
], LLVMBuildFPToUI(ctx
->builder
, ctx
->frag_pos
[chan
], ctx
->i32
, ""), "");
3268 coords
[count
] = sample_index
;
3273 coords
[3] = LLVMGetUndef(ctx
->i32
);
3276 res
= ac_build_gather_values(&ctx
->ac
, coords
, count
);
3281 static LLVMValueRef
visit_image_load(struct nir_to_llvm_context
*ctx
,
3282 nir_intrinsic_instr
*instr
)
3284 LLVMValueRef params
[7];
3286 char intrinsic_name
[64];
3287 const nir_variable
*var
= instr
->variables
[0]->var
;
3288 const struct glsl_type
*type
= var
->type
;
3289 if(instr
->variables
[0]->deref
.child
)
3290 type
= instr
->variables
[0]->deref
.child
->type
;
3292 type
= glsl_without_array(type
);
3293 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3294 params
[0] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
3295 params
[1] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
3296 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
3297 params
[2] = LLVMConstInt(ctx
->i32
, 0, false); /* voffset */
3298 params
[3] = ctx
->i1false
; /* glc */
3299 params
[4] = ctx
->i1false
; /* slc */
3300 res
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.load.format.v4f32", ctx
->v4f32
,
3303 res
= trim_vector(ctx
, res
, instr
->dest
.ssa
.num_components
);
3304 res
= to_integer(ctx
, res
);
3306 bool is_da
= glsl_sampler_type_is_array(type
) ||
3307 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3308 LLVMValueRef da
= is_da
? ctx
->i1true
: ctx
->i1false
;
3309 LLVMValueRef glc
= ctx
->i1false
;
3310 LLVMValueRef slc
= ctx
->i1false
;
3312 params
[0] = get_image_coords(ctx
, instr
);
3313 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
3314 params
[2] = LLVMConstInt(ctx
->i32
, 15, false); /* dmask */
3315 if (HAVE_LLVM
<= 0x0309) {
3316 params
[3] = ctx
->i1false
; /* r128 */
3321 LLVMValueRef lwe
= ctx
->i1false
;
3328 ac_get_image_intr_name("llvm.amdgcn.image.load",
3329 ctx
->v4f32
, /* vdata */
3330 LLVMTypeOf(params
[0]), /* coords */
3331 LLVMTypeOf(params
[1]), /* rsrc */
3332 intrinsic_name
, sizeof(intrinsic_name
));
3334 res
= ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->v4f32
,
3335 params
, 7, AC_FUNC_ATTR_READONLY
);
3337 return to_integer(ctx
, res
);
3340 static void visit_image_store(struct nir_to_llvm_context
*ctx
,
3341 nir_intrinsic_instr
*instr
)
3343 LLVMValueRef params
[8];
3344 char intrinsic_name
[64];
3345 const nir_variable
*var
= instr
->variables
[0]->var
;
3346 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3348 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
3349 ctx
->shader_info
->fs
.writes_memory
= true;
3351 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3352 params
[0] = to_float(ctx
, get_src(ctx
, instr
->src
[2])); /* data */
3353 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
3354 params
[2] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
3355 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
3356 params
[3] = LLVMConstInt(ctx
->i32
, 0, false); /* voffset */
3357 params
[4] = ctx
->i1false
; /* glc */
3358 params
[5] = ctx
->i1false
; /* slc */
3359 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->voidt
,
3362 bool is_da
= glsl_sampler_type_is_array(type
) ||
3363 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3364 LLVMValueRef da
= is_da
? ctx
->i1true
: ctx
->i1false
;
3365 LLVMValueRef glc
= ctx
->i1false
;
3366 LLVMValueRef slc
= ctx
->i1false
;
3368 params
[0] = to_float(ctx
, get_src(ctx
, instr
->src
[2]));
3369 params
[1] = get_image_coords(ctx
, instr
); /* coords */
3370 params
[2] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
3371 params
[3] = LLVMConstInt(ctx
->i32
, 15, false); /* dmask */
3372 if (HAVE_LLVM
<= 0x0309) {
3373 params
[4] = ctx
->i1false
; /* r128 */
3378 LLVMValueRef lwe
= ctx
->i1false
;
3385 ac_get_image_intr_name("llvm.amdgcn.image.store",
3386 LLVMTypeOf(params
[0]), /* vdata */
3387 LLVMTypeOf(params
[1]), /* coords */
3388 LLVMTypeOf(params
[2]), /* rsrc */
3389 intrinsic_name
, sizeof(intrinsic_name
));
3391 ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->voidt
,
3397 static LLVMValueRef
visit_image_atomic(struct nir_to_llvm_context
*ctx
,
3398 nir_intrinsic_instr
*instr
)
3400 LLVMValueRef params
[6];
3401 int param_count
= 0;
3402 const nir_variable
*var
= instr
->variables
[0]->var
;
3404 const char *base_name
= "llvm.amdgcn.image.atomic";
3405 const char *atomic_name
;
3406 LLVMValueRef coords
;
3407 char intrinsic_name
[32], coords_type
[8];
3408 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3410 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
3411 ctx
->shader_info
->fs
.writes_memory
= true;
3413 params
[param_count
++] = get_src(ctx
, instr
->src
[2]);
3414 if (instr
->intrinsic
== nir_intrinsic_image_atomic_comp_swap
)
3415 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
3417 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3418 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
3419 coords
= params
[param_count
++] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
3420 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
3421 params
[param_count
++] = ctx
->i32zero
; /* voffset */
3422 params
[param_count
++] = ctx
->i1false
; /* glc */
3423 params
[param_count
++] = ctx
->i1false
; /* slc */
3425 bool da
= glsl_sampler_type_is_array(type
) ||
3426 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3428 coords
= params
[param_count
++] = get_image_coords(ctx
, instr
);
3429 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
3430 params
[param_count
++] = ctx
->i1false
; /* r128 */
3431 params
[param_count
++] = da
? ctx
->i1true
: ctx
->i1false
; /* da */
3432 params
[param_count
++] = ctx
->i1false
; /* slc */
3435 switch (instr
->intrinsic
) {
3436 case nir_intrinsic_image_atomic_add
:
3437 atomic_name
= "add";
3439 case nir_intrinsic_image_atomic_min
:
3440 atomic_name
= "smin";
3442 case nir_intrinsic_image_atomic_max
:
3443 atomic_name
= "smax";
3445 case nir_intrinsic_image_atomic_and
:
3446 atomic_name
= "and";
3448 case nir_intrinsic_image_atomic_or
:
3451 case nir_intrinsic_image_atomic_xor
:
3452 atomic_name
= "xor";
3454 case nir_intrinsic_image_atomic_exchange
:
3455 atomic_name
= "swap";
3457 case nir_intrinsic_image_atomic_comp_swap
:
3458 atomic_name
= "cmpswap";
3463 build_int_type_name(LLVMTypeOf(coords
),
3464 coords_type
, sizeof(coords_type
));
3466 snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3467 "%s.%s.%s", base_name
, atomic_name
, coords_type
);
3468 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->i32
, params
, param_count
, 0);
3471 static LLVMValueRef
visit_image_size(struct nir_to_llvm_context
*ctx
,
3472 nir_intrinsic_instr
*instr
)
3475 const nir_variable
*var
= instr
->variables
[0]->var
;
3476 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
3477 bool da
= glsl_sampler_type_is_array(var
->type
) ||
3478 glsl_get_sampler_dim(var
->type
) == GLSL_SAMPLER_DIM_CUBE
;
3479 if(instr
->variables
[0]->deref
.child
)
3480 type
= instr
->variables
[0]->deref
.child
->type
;
3482 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
3483 return get_buffer_size(ctx
, get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
), true);
3485 struct ac_image_args args
= { 0 };
3489 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
3490 args
.opcode
= ac_image_get_resinfo
;
3491 args
.addr
= ctx
->i32zero
;
3493 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
3495 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
3496 glsl_sampler_type_is_array(type
)) {
3497 LLVMValueRef two
= LLVMConstInt(ctx
->i32
, 2, false);
3498 LLVMValueRef six
= LLVMConstInt(ctx
->i32
, 6, false);
3499 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->builder
, res
, two
, "");
3500 z
= LLVMBuildSDiv(ctx
->builder
, z
, six
, "");
3501 res
= LLVMBuildInsertElement(ctx
->builder
, res
, z
, two
, "");
3506 #define NOOP_WAITCNT 0xf7f
3507 #define LGKM_CNT 0x07f
3508 #define VM_CNT 0xf70
3510 static void emit_waitcnt(struct nir_to_llvm_context
*ctx
,
3513 LLVMValueRef args
[1] = {
3514 LLVMConstInt(ctx
->i32
, simm16
, false),
3516 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.s.waitcnt",
3517 ctx
->voidt
, args
, 1, 0);
3520 static void emit_barrier(struct nir_to_llvm_context
*ctx
)
3522 /* SI only (thanks to a hw bug workaround):
3523 * The real barrier instruction isn’t needed, because an entire patch
3524 * always fits into a single wave.
3526 if (ctx
->options
->chip_class
== SI
&&
3527 ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3528 emit_waitcnt(ctx
, LGKM_CNT
& VM_CNT
);
3531 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.s.barrier",
3532 ctx
->voidt
, NULL
, 0, AC_FUNC_ATTR_CONVERGENT
);
3535 static void emit_discard_if(struct nir_to_llvm_context
*ctx
,
3536 nir_intrinsic_instr
*instr
)
3539 ctx
->shader_info
->fs
.can_discard
= true;
3541 cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3542 get_src(ctx
, instr
->src
[0]),
3545 cond
= LLVMBuildSelect(ctx
->builder
, cond
,
3546 LLVMConstReal(ctx
->f32
, -1.0f
),
3548 ac_build_kill(&ctx
->ac
, cond
);
3552 visit_load_local_invocation_index(struct nir_to_llvm_context
*ctx
)
3554 LLVMValueRef result
;
3555 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
3556 result
= LLVMBuildAnd(ctx
->builder
, ctx
->tg_size
,
3557 LLVMConstInt(ctx
->i32
, 0xfc0, false), "");
3559 return LLVMBuildAdd(ctx
->builder
, result
, thread_id
, "");
3562 static LLVMValueRef
visit_var_atomic(struct nir_to_llvm_context
*ctx
,
3563 nir_intrinsic_instr
*instr
)
3565 LLVMValueRef ptr
, result
;
3566 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
3567 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
3568 ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
3570 if (instr
->intrinsic
== nir_intrinsic_var_atomic_comp_swap
) {
3571 LLVMValueRef src1
= get_src(ctx
, instr
->src
[1]);
3572 result
= LLVMBuildAtomicCmpXchg(ctx
->builder
,
3574 LLVMAtomicOrderingSequentiallyConsistent
,
3575 LLVMAtomicOrderingSequentiallyConsistent
,
3578 LLVMAtomicRMWBinOp op
;
3579 switch (instr
->intrinsic
) {
3580 case nir_intrinsic_var_atomic_add
:
3581 op
= LLVMAtomicRMWBinOpAdd
;
3583 case nir_intrinsic_var_atomic_umin
:
3584 op
= LLVMAtomicRMWBinOpUMin
;
3586 case nir_intrinsic_var_atomic_umax
:
3587 op
= LLVMAtomicRMWBinOpUMax
;
3589 case nir_intrinsic_var_atomic_imin
:
3590 op
= LLVMAtomicRMWBinOpMin
;
3592 case nir_intrinsic_var_atomic_imax
:
3593 op
= LLVMAtomicRMWBinOpMax
;
3595 case nir_intrinsic_var_atomic_and
:
3596 op
= LLVMAtomicRMWBinOpAnd
;
3598 case nir_intrinsic_var_atomic_or
:
3599 op
= LLVMAtomicRMWBinOpOr
;
3601 case nir_intrinsic_var_atomic_xor
:
3602 op
= LLVMAtomicRMWBinOpXor
;
3604 case nir_intrinsic_var_atomic_exchange
:
3605 op
= LLVMAtomicRMWBinOpXchg
;
3611 result
= LLVMBuildAtomicRMW(ctx
->builder
, op
, ptr
, to_integer(ctx
, src
),
3612 LLVMAtomicOrderingSequentiallyConsistent
,
3618 #define INTERP_CENTER 0
3619 #define INTERP_CENTROID 1
3620 #define INTERP_SAMPLE 2
3622 static LLVMValueRef
lookup_interp_param(struct nir_to_llvm_context
*ctx
,
3623 enum glsl_interp_mode interp
, unsigned location
)
3626 case INTERP_MODE_FLAT
:
3629 case INTERP_MODE_SMOOTH
:
3630 case INTERP_MODE_NONE
:
3631 if (location
== INTERP_CENTER
)
3632 return ctx
->persp_center
;
3633 else if (location
== INTERP_CENTROID
)
3634 return ctx
->persp_centroid
;
3635 else if (location
== INTERP_SAMPLE
)
3636 return ctx
->persp_sample
;
3638 case INTERP_MODE_NOPERSPECTIVE
:
3639 if (location
== INTERP_CENTER
)
3640 return ctx
->linear_center
;
3641 else if (location
== INTERP_CENTROID
)
3642 return ctx
->linear_centroid
;
3643 else if (location
== INTERP_SAMPLE
)
3644 return ctx
->linear_sample
;
3650 static LLVMValueRef
load_sample_position(struct nir_to_llvm_context
*ctx
,
3651 LLVMValueRef sample_id
)
3653 LLVMValueRef result
;
3654 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_PS_SAMPLE_POSITIONS
, false));
3656 ptr
= LLVMBuildBitCast(ctx
->builder
, ptr
,
3657 const_array(ctx
->v2f32
, 64), "");
3659 sample_id
= LLVMBuildAdd(ctx
->builder
, sample_id
, ctx
->sample_pos_offset
, "");
3660 result
= ac_build_indexed_load(&ctx
->ac
, ptr
, sample_id
, false);
3665 static LLVMValueRef
load_sample_pos(struct nir_to_llvm_context
*ctx
)
3667 LLVMValueRef values
[2];
3669 values
[0] = emit_ffract(ctx
, ctx
->frag_pos
[0]);
3670 values
[1] = emit_ffract(ctx
, ctx
->frag_pos
[1]);
3671 return ac_build_gather_values(&ctx
->ac
, values
, 2);
3674 static LLVMValueRef
visit_interp(struct nir_to_llvm_context
*ctx
,
3675 nir_intrinsic_instr
*instr
)
3677 LLVMValueRef result
[2];
3678 LLVMValueRef interp_param
, attr_number
;
3681 LLVMValueRef src_c0
, src_c1
;
3683 int input_index
= instr
->variables
[0]->var
->data
.location
- VARYING_SLOT_VAR0
;
3684 switch (instr
->intrinsic
) {
3685 case nir_intrinsic_interp_var_at_centroid
:
3686 location
= INTERP_CENTROID
;
3688 case nir_intrinsic_interp_var_at_sample
:
3689 case nir_intrinsic_interp_var_at_offset
:
3690 location
= INTERP_CENTER
;
3691 src0
= get_src(ctx
, instr
->src
[0]);
3697 if (instr
->intrinsic
== nir_intrinsic_interp_var_at_offset
) {
3698 src_c0
= to_float(ctx
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32zero
, ""));
3699 src_c1
= to_float(ctx
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32one
, ""));
3700 } else if (instr
->intrinsic
== nir_intrinsic_interp_var_at_sample
) {
3701 LLVMValueRef sample_position
;
3702 LLVMValueRef halfval
= LLVMConstReal(ctx
->f32
, 0.5f
);
3704 /* fetch sample ID */
3705 sample_position
= load_sample_position(ctx
, src0
);
3707 src_c0
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->i32zero
, "");
3708 src_c0
= LLVMBuildFSub(ctx
->builder
, src_c0
, halfval
, "");
3709 src_c1
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->i32one
, "");
3710 src_c1
= LLVMBuildFSub(ctx
->builder
, src_c1
, halfval
, "");
3712 interp_param
= lookup_interp_param(ctx
, instr
->variables
[0]->var
->data
.interpolation
, location
);
3713 attr_number
= LLVMConstInt(ctx
->i32
, input_index
, false);
3715 if (location
== INTERP_SAMPLE
|| location
== INTERP_CENTER
) {
3716 LLVMValueRef ij_out
[2];
3717 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
, interp_param
);
3720 * take the I then J parameters, and the DDX/Y for it, and
3721 * calculate the IJ inputs for the interpolator.
3722 * temp1 = ddx * offset/sample.x + I;
3723 * interp_param.I = ddy * offset/sample.y + temp1;
3724 * temp1 = ddx * offset/sample.x + J;
3725 * interp_param.J = ddy * offset/sample.y + temp1;
3727 for (unsigned i
= 0; i
< 2; i
++) {
3728 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->i32
, i
, false);
3729 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->i32
, i
+ 2, false);
3730 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->builder
,
3731 ddxy_out
, ix_ll
, "");
3732 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->builder
,
3733 ddxy_out
, iy_ll
, "");
3734 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->builder
,
3735 interp_param
, ix_ll
, "");
3736 LLVMValueRef temp1
, temp2
;
3738 interp_el
= LLVMBuildBitCast(ctx
->builder
, interp_el
,
3741 temp1
= LLVMBuildFMul(ctx
->builder
, ddx_el
, src_c0
, "");
3742 temp1
= LLVMBuildFAdd(ctx
->builder
, temp1
, interp_el
, "");
3744 temp2
= LLVMBuildFMul(ctx
->builder
, ddy_el
, src_c1
, "");
3745 temp2
= LLVMBuildFAdd(ctx
->builder
, temp2
, temp1
, "");
3747 ij_out
[i
] = LLVMBuildBitCast(ctx
->builder
,
3748 temp2
, ctx
->i32
, "");
3750 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
3754 for (chan
= 0; chan
< 2; chan
++) {
3755 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
3758 interp_param
= LLVMBuildBitCast(ctx
->builder
,
3759 interp_param
, LLVMVectorType(ctx
->f32
, 2), "");
3760 LLVMValueRef i
= LLVMBuildExtractElement(
3761 ctx
->builder
, interp_param
, ctx
->i32zero
, "");
3762 LLVMValueRef j
= LLVMBuildExtractElement(
3763 ctx
->builder
, interp_param
, ctx
->i32one
, "");
3765 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
3766 llvm_chan
, attr_number
,
3767 ctx
->prim_mask
, i
, j
);
3769 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
3770 LLVMConstInt(ctx
->i32
, 2, false),
3771 llvm_chan
, attr_number
,
3775 return ac_build_gather_values(&ctx
->ac
, result
, 2);
3779 visit_emit_vertex(struct nir_to_llvm_context
*ctx
,
3780 nir_intrinsic_instr
*instr
)
3782 LLVMValueRef gs_next_vertex
;
3783 LLVMValueRef can_emit
, kill
;
3786 assert(instr
->const_index
[0] == 0);
3787 /* Write vertex attribute values to GSVS ring */
3788 gs_next_vertex
= LLVMBuildLoad(ctx
->builder
,
3789 ctx
->gs_next_vertex
,
3792 /* If this thread has already emitted the declared maximum number of
3793 * vertices, kill it: excessive vertex emissions are not supposed to
3794 * have any effect, and GS threads have no externally observable
3795 * effects other than emitting vertices.
3797 can_emit
= LLVMBuildICmp(ctx
->builder
, LLVMIntULT
, gs_next_vertex
,
3798 LLVMConstInt(ctx
->i32
, ctx
->gs_max_out_vertices
, false), "");
3800 kill
= LLVMBuildSelect(ctx
->builder
, can_emit
,
3801 LLVMConstReal(ctx
->f32
, 1.0f
),
3802 LLVMConstReal(ctx
->f32
, -1.0f
), "");
3803 ac_build_kill(&ctx
->ac
, kill
);
3805 /* loop num outputs */
3807 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
3808 LLVMValueRef
*out_ptr
= &ctx
->outputs
[i
* 4];
3813 if (!(ctx
->output_mask
& (1ull << i
)))
3816 if (i
== VARYING_SLOT_CLIP_DIST0
) {
3817 /* pack clip and cull into a single set of slots */
3818 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
3822 for (unsigned j
= 0; j
< length
; j
++) {
3823 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
,
3825 LLVMValueRef voffset
= LLVMConstInt(ctx
->i32
, (slot
* 4 + j
) * ctx
->gs_max_out_vertices
, false);
3826 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, gs_next_vertex
, "");
3827 voffset
= LLVMBuildMul(ctx
->builder
, voffset
, LLVMConstInt(ctx
->i32
, 4, false), "");
3829 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->i32
, "");
3831 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->gsvs_ring
,
3833 voffset
, ctx
->gs2vs_offset
, 0,
3839 gs_next_vertex
= LLVMBuildAdd(ctx
->builder
, gs_next_vertex
,
3841 LLVMBuildStore(ctx
->builder
, gs_next_vertex
, ctx
->gs_next_vertex
);
3843 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_EMIT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
3847 visit_end_primitive(struct nir_to_llvm_context
*ctx
,
3848 nir_intrinsic_instr
*instr
)
3850 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_CUT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
3854 visit_load_tess_coord(struct nir_to_llvm_context
*ctx
,
3855 nir_intrinsic_instr
*instr
)
3857 LLVMValueRef coord
[4] = {
3864 if (ctx
->tes_primitive_mode
== GL_TRIANGLES
)
3865 coord
[2] = LLVMBuildFSub(ctx
->builder
, ctx
->f32one
,
3866 LLVMBuildFAdd(ctx
->builder
, coord
[0], coord
[1], ""), "");
3868 LLVMValueRef result
= ac_build_gather_values(&ctx
->ac
, coord
, instr
->num_components
);
3869 return LLVMBuildBitCast(ctx
->builder
, result
,
3870 get_def_type(ctx
, &instr
->dest
.ssa
), "");
3873 static void visit_intrinsic(struct nir_to_llvm_context
*ctx
,
3874 nir_intrinsic_instr
*instr
)
3876 LLVMValueRef result
= NULL
;
3878 switch (instr
->intrinsic
) {
3879 case nir_intrinsic_load_work_group_id
: {
3880 result
= ctx
->workgroup_ids
;
3883 case nir_intrinsic_load_base_vertex
: {
3884 result
= ctx
->base_vertex
;
3887 case nir_intrinsic_load_vertex_id_zero_base
: {
3888 result
= ctx
->vertex_id
;
3891 case nir_intrinsic_load_local_invocation_id
: {
3892 result
= ctx
->local_invocation_ids
;
3895 case nir_intrinsic_load_base_instance
:
3896 result
= ctx
->start_instance
;
3898 case nir_intrinsic_load_draw_id
:
3899 result
= ctx
->draw_index
;
3901 case nir_intrinsic_load_invocation_id
:
3902 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
3903 result
= unpack_param(ctx
, ctx
->tcs_rel_ids
, 8, 5);
3905 result
= ctx
->gs_invocation_id
;
3907 case nir_intrinsic_load_primitive_id
:
3908 if (ctx
->stage
== MESA_SHADER_GEOMETRY
)
3909 result
= ctx
->gs_prim_id
;
3910 else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
3911 result
= ctx
->tcs_patch_id
;
3912 else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
)
3913 result
= ctx
->tes_patch_id
;
3915 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
3917 case nir_intrinsic_load_sample_id
:
3918 ctx
->shader_info
->fs
.force_persample
= true;
3919 result
= unpack_param(ctx
, ctx
->ancillary
, 8, 4);
3921 case nir_intrinsic_load_sample_pos
:
3922 ctx
->shader_info
->fs
.force_persample
= true;
3923 result
= load_sample_pos(ctx
);
3925 case nir_intrinsic_load_sample_mask_in
:
3926 result
= ctx
->sample_coverage
;
3928 case nir_intrinsic_load_front_face
:
3929 result
= ctx
->front_face
;
3931 case nir_intrinsic_load_instance_id
:
3932 result
= ctx
->instance_id
;
3933 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
3934 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
3936 case nir_intrinsic_load_num_work_groups
:
3937 result
= ctx
->num_work_groups
;
3939 case nir_intrinsic_load_local_invocation_index
:
3940 result
= visit_load_local_invocation_index(ctx
);
3942 case nir_intrinsic_load_push_constant
:
3943 result
= visit_load_push_constant(ctx
, instr
);
3945 case nir_intrinsic_vulkan_resource_index
:
3946 result
= visit_vulkan_resource_index(ctx
, instr
);
3948 case nir_intrinsic_store_ssbo
:
3949 visit_store_ssbo(ctx
, instr
);
3951 case nir_intrinsic_load_ssbo
:
3952 result
= visit_load_buffer(ctx
, instr
);
3954 case nir_intrinsic_ssbo_atomic_add
:
3955 case nir_intrinsic_ssbo_atomic_imin
:
3956 case nir_intrinsic_ssbo_atomic_umin
:
3957 case nir_intrinsic_ssbo_atomic_imax
:
3958 case nir_intrinsic_ssbo_atomic_umax
:
3959 case nir_intrinsic_ssbo_atomic_and
:
3960 case nir_intrinsic_ssbo_atomic_or
:
3961 case nir_intrinsic_ssbo_atomic_xor
:
3962 case nir_intrinsic_ssbo_atomic_exchange
:
3963 case nir_intrinsic_ssbo_atomic_comp_swap
:
3964 result
= visit_atomic_ssbo(ctx
, instr
);
3966 case nir_intrinsic_load_ubo
:
3967 result
= visit_load_ubo_buffer(ctx
, instr
);
3969 case nir_intrinsic_get_buffer_size
:
3970 result
= visit_get_buffer_size(ctx
, instr
);
3972 case nir_intrinsic_load_var
:
3973 result
= visit_load_var(ctx
, instr
);
3975 case nir_intrinsic_store_var
:
3976 visit_store_var(ctx
, instr
);
3978 case nir_intrinsic_image_load
:
3979 result
= visit_image_load(ctx
, instr
);
3981 case nir_intrinsic_image_store
:
3982 visit_image_store(ctx
, instr
);
3984 case nir_intrinsic_image_atomic_add
:
3985 case nir_intrinsic_image_atomic_min
:
3986 case nir_intrinsic_image_atomic_max
:
3987 case nir_intrinsic_image_atomic_and
:
3988 case nir_intrinsic_image_atomic_or
:
3989 case nir_intrinsic_image_atomic_xor
:
3990 case nir_intrinsic_image_atomic_exchange
:
3991 case nir_intrinsic_image_atomic_comp_swap
:
3992 result
= visit_image_atomic(ctx
, instr
);
3994 case nir_intrinsic_image_size
:
3995 result
= visit_image_size(ctx
, instr
);
3997 case nir_intrinsic_discard
:
3998 ctx
->shader_info
->fs
.can_discard
= true;
3999 ac_build_intrinsic(&ctx
->ac
, "llvm.AMDGPU.kilp",
4001 NULL
, 0, AC_FUNC_ATTR_LEGACY
);
4003 case nir_intrinsic_discard_if
:
4004 emit_discard_if(ctx
, instr
);
4006 case nir_intrinsic_memory_barrier
:
4007 emit_waitcnt(ctx
, VM_CNT
);
4009 case nir_intrinsic_barrier
:
4012 case nir_intrinsic_var_atomic_add
:
4013 case nir_intrinsic_var_atomic_imin
:
4014 case nir_intrinsic_var_atomic_umin
:
4015 case nir_intrinsic_var_atomic_imax
:
4016 case nir_intrinsic_var_atomic_umax
:
4017 case nir_intrinsic_var_atomic_and
:
4018 case nir_intrinsic_var_atomic_or
:
4019 case nir_intrinsic_var_atomic_xor
:
4020 case nir_intrinsic_var_atomic_exchange
:
4021 case nir_intrinsic_var_atomic_comp_swap
:
4022 result
= visit_var_atomic(ctx
, instr
);
4024 case nir_intrinsic_interp_var_at_centroid
:
4025 case nir_intrinsic_interp_var_at_sample
:
4026 case nir_intrinsic_interp_var_at_offset
:
4027 result
= visit_interp(ctx
, instr
);
4029 case nir_intrinsic_emit_vertex
:
4030 visit_emit_vertex(ctx
, instr
);
4032 case nir_intrinsic_end_primitive
:
4033 visit_end_primitive(ctx
, instr
);
4035 case nir_intrinsic_load_tess_coord
:
4036 result
= visit_load_tess_coord(ctx
, instr
);
4038 case nir_intrinsic_load_patch_vertices_in
:
4039 result
= LLVMConstInt(ctx
->i32
, ctx
->options
->key
.tcs
.input_vertices
, false);
4042 fprintf(stderr
, "Unknown intrinsic: ");
4043 nir_print_instr(&instr
->instr
, stderr
);
4044 fprintf(stderr
, "\n");
4048 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4052 static LLVMValueRef
get_sampler_desc(struct nir_to_llvm_context
*ctx
,
4053 nir_deref_var
*deref
,
4054 enum desc_type desc_type
)
4056 unsigned desc_set
= deref
->var
->data
.descriptor_set
;
4057 LLVMValueRef list
= ctx
->descriptor_sets
[desc_set
];
4058 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[desc_set
].layout
;
4059 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ deref
->var
->data
.binding
;
4060 unsigned offset
= binding
->offset
;
4061 unsigned stride
= binding
->size
;
4063 LLVMBuilderRef builder
= ctx
->builder
;
4065 LLVMValueRef index
= NULL
;
4066 unsigned constant_index
= 0;
4068 assert(deref
->var
->data
.binding
< layout
->binding_count
);
4070 switch (desc_type
) {
4082 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
4092 unreachable("invalid desc_type\n");
4095 if (deref
->deref
.child
) {
4096 nir_deref_array
*child
= (nir_deref_array
*)deref
->deref
.child
;
4098 assert(child
->deref_array_type
!= nir_deref_array_type_wildcard
);
4099 offset
+= child
->base_offset
* stride
;
4100 if (child
->deref_array_type
== nir_deref_array_type_indirect
) {
4101 index
= get_src(ctx
, child
->indirect
);
4104 constant_index
= child
->base_offset
;
4106 if (desc_type
== DESC_SAMPLER
&& binding
->immutable_samplers_offset
&&
4107 (!index
|| binding
->immutable_samplers_equal
)) {
4108 if (binding
->immutable_samplers_equal
)
4111 const uint32_t *samplers
= radv_immutable_samplers(layout
, binding
);
4113 LLVMValueRef constants
[] = {
4114 LLVMConstInt(ctx
->i32
, samplers
[constant_index
* 4 + 0], 0),
4115 LLVMConstInt(ctx
->i32
, samplers
[constant_index
* 4 + 1], 0),
4116 LLVMConstInt(ctx
->i32
, samplers
[constant_index
* 4 + 2], 0),
4117 LLVMConstInt(ctx
->i32
, samplers
[constant_index
* 4 + 3], 0),
4119 return ac_build_gather_values(&ctx
->ac
, constants
, 4);
4122 assert(stride
% type_size
== 0);
4125 index
= ctx
->i32zero
;
4127 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, stride
/ type_size
, 0), "");
4129 list
= ac_build_gep0(&ctx
->ac
, list
, LLVMConstInt(ctx
->i32
, offset
, 0));
4130 list
= LLVMBuildPointerCast(builder
, list
, const_array(type
, 0), "");
4132 return ac_build_indexed_load_const(&ctx
->ac
, list
, index
);
4135 static void set_tex_fetch_args(struct nir_to_llvm_context
*ctx
,
4136 struct ac_image_args
*args
,
4137 nir_tex_instr
*instr
,
4139 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
4140 LLVMValueRef
*param
, unsigned count
,
4143 unsigned is_rect
= 0;
4144 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
4146 if (op
== nir_texop_lod
)
4148 /* Pad to power of two vector */
4149 while (count
< util_next_power_of_two(count
))
4150 param
[count
++] = LLVMGetUndef(ctx
->i32
);
4153 args
->addr
= ac_build_gather_values(&ctx
->ac
, param
, count
);
4155 args
->addr
= param
[0];
4157 args
->resource
= res_ptr
;
4158 args
->sampler
= samp_ptr
;
4160 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
&& op
== nir_texop_txf
) {
4161 args
->addr
= param
[0];
4165 args
->dmask
= dmask
;
4166 args
->unorm
= is_rect
;
4170 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
4173 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
4174 * filtering manually. The driver sets img7 to a mask clearing
4175 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
4176 * s_and_b32 samp0, samp0, img7
4179 * The ANISO_OVERRIDE sampler field enables this fix in TA.
4181 static LLVMValueRef
sici_fix_sampler_aniso(struct nir_to_llvm_context
*ctx
,
4182 LLVMValueRef res
, LLVMValueRef samp
)
4184 LLVMBuilderRef builder
= ctx
->builder
;
4185 LLVMValueRef img7
, samp0
;
4187 if (ctx
->options
->chip_class
>= VI
)
4190 img7
= LLVMBuildExtractElement(builder
, res
,
4191 LLVMConstInt(ctx
->i32
, 7, 0), "");
4192 samp0
= LLVMBuildExtractElement(builder
, samp
,
4193 LLVMConstInt(ctx
->i32
, 0, 0), "");
4194 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
4195 return LLVMBuildInsertElement(builder
, samp
, samp0
,
4196 LLVMConstInt(ctx
->i32
, 0, 0), "");
4199 static void tex_fetch_ptrs(struct nir_to_llvm_context
*ctx
,
4200 nir_tex_instr
*instr
,
4201 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
4202 LLVMValueRef
*fmask_ptr
)
4204 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
4205 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_BUFFER
);
4207 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_IMAGE
);
4210 *samp_ptr
= get_sampler_desc(ctx
, instr
->sampler
, DESC_SAMPLER
);
4212 *samp_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_SAMPLER
);
4213 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
4214 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
4216 if (fmask_ptr
&& !instr
->sampler
&& (instr
->op
== nir_texop_txf_ms
||
4217 instr
->op
== nir_texop_samples_identical
))
4218 *fmask_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_FMASK
);
4221 static LLVMValueRef
apply_round_slice(struct nir_to_llvm_context
*ctx
,
4224 coord
= to_float(ctx
, coord
);
4225 coord
= ac_build_intrinsic(&ctx
->ac
, "llvm.rint.f32", ctx
->f32
, &coord
, 1, 0);
4226 coord
= to_integer(ctx
, coord
);
4230 static void visit_tex(struct nir_to_llvm_context
*ctx
, nir_tex_instr
*instr
)
4232 LLVMValueRef result
= NULL
;
4233 struct ac_image_args args
= { 0 };
4234 unsigned dmask
= 0xf;
4235 LLVMValueRef address
[16];
4236 LLVMValueRef coords
[5];
4237 LLVMValueRef coord
= NULL
, lod
= NULL
, comparator
= NULL
;
4238 LLVMValueRef bias
= NULL
, offsets
= NULL
;
4239 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
, sample_index
= NULL
;
4240 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
4241 LLVMValueRef derivs
[6];
4242 unsigned chan
, count
= 0;
4243 unsigned const_src
= 0, num_deriv_comp
= 0;
4244 bool lod_is_zero
= false;
4245 tex_fetch_ptrs(ctx
, instr
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
4247 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
4248 switch (instr
->src
[i
].src_type
) {
4249 case nir_tex_src_coord
:
4250 coord
= get_src(ctx
, instr
->src
[i
].src
);
4252 case nir_tex_src_projector
:
4254 case nir_tex_src_comparator
:
4255 comparator
= get_src(ctx
, instr
->src
[i
].src
);
4257 case nir_tex_src_offset
:
4258 offsets
= get_src(ctx
, instr
->src
[i
].src
);
4261 case nir_tex_src_bias
:
4262 bias
= get_src(ctx
, instr
->src
[i
].src
);
4264 case nir_tex_src_lod
: {
4265 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
4267 if (val
&& val
->i32
[0] == 0)
4269 lod
= get_src(ctx
, instr
->src
[i
].src
);
4272 case nir_tex_src_ms_index
:
4273 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
4275 case nir_tex_src_ms_mcs
:
4277 case nir_tex_src_ddx
:
4278 ddx
= get_src(ctx
, instr
->src
[i
].src
);
4279 num_deriv_comp
= instr
->src
[i
].src
.ssa
->num_components
;
4281 case nir_tex_src_ddy
:
4282 ddy
= get_src(ctx
, instr
->src
[i
].src
);
4284 case nir_tex_src_texture_offset
:
4285 case nir_tex_src_sampler_offset
:
4286 case nir_tex_src_plane
:
4292 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
4293 result
= get_buffer_size(ctx
, res_ptr
, true);
4297 if (instr
->op
== nir_texop_texture_samples
) {
4298 LLVMValueRef res
, samples
, is_msaa
;
4299 res
= LLVMBuildBitCast(ctx
->builder
, res_ptr
, ctx
->v8i32
, "");
4300 samples
= LLVMBuildExtractElement(ctx
->builder
, res
,
4301 LLVMConstInt(ctx
->i32
, 3, false), "");
4302 is_msaa
= LLVMBuildLShr(ctx
->builder
, samples
,
4303 LLVMConstInt(ctx
->i32
, 28, false), "");
4304 is_msaa
= LLVMBuildAnd(ctx
->builder
, is_msaa
,
4305 LLVMConstInt(ctx
->i32
, 0xe, false), "");
4306 is_msaa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, is_msaa
,
4307 LLVMConstInt(ctx
->i32
, 0xe, false), "");
4309 samples
= LLVMBuildLShr(ctx
->builder
, samples
,
4310 LLVMConstInt(ctx
->i32
, 16, false), "");
4311 samples
= LLVMBuildAnd(ctx
->builder
, samples
,
4312 LLVMConstInt(ctx
->i32
, 0xf, false), "");
4313 samples
= LLVMBuildShl(ctx
->builder
, ctx
->i32one
,
4315 samples
= LLVMBuildSelect(ctx
->builder
, is_msaa
, samples
,
4322 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
4323 coords
[chan
] = llvm_extract_elem(ctx
, coord
, chan
);
4325 if (offsets
&& instr
->op
!= nir_texop_txf
) {
4326 LLVMValueRef offset
[3], pack
;
4327 for (chan
= 0; chan
< 3; ++chan
)
4328 offset
[chan
] = ctx
->i32zero
;
4331 for (chan
= 0; chan
< get_llvm_num_components(offsets
); chan
++) {
4332 offset
[chan
] = llvm_extract_elem(ctx
, offsets
, chan
);
4333 offset
[chan
] = LLVMBuildAnd(ctx
->builder
, offset
[chan
],
4334 LLVMConstInt(ctx
->i32
, 0x3f, false), "");
4336 offset
[chan
] = LLVMBuildShl(ctx
->builder
, offset
[chan
],
4337 LLVMConstInt(ctx
->i32
, chan
* 8, false), "");
4339 pack
= LLVMBuildOr(ctx
->builder
, offset
[0], offset
[1], "");
4340 pack
= LLVMBuildOr(ctx
->builder
, pack
, offset
[2], "");
4341 address
[count
++] = pack
;
4344 /* pack LOD bias value */
4345 if (instr
->op
== nir_texop_txb
&& bias
) {
4346 address
[count
++] = bias
;
4349 /* Pack depth comparison value */
4350 if (instr
->is_shadow
&& comparator
) {
4351 address
[count
++] = llvm_extract_elem(ctx
, comparator
, 0);
4354 /* pack derivatives */
4356 switch (instr
->sampler_dim
) {
4357 case GLSL_SAMPLER_DIM_3D
:
4358 case GLSL_SAMPLER_DIM_CUBE
:
4361 case GLSL_SAMPLER_DIM_2D
:
4365 case GLSL_SAMPLER_DIM_1D
:
4370 for (unsigned i
= 0; i
< num_deriv_comp
; i
++) {
4371 derivs
[i
] = to_float(ctx
, llvm_extract_elem(ctx
, ddx
, i
));
4372 derivs
[num_deriv_comp
+ i
] = to_float(ctx
, llvm_extract_elem(ctx
, ddy
, i
));
4376 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& coord
) {
4377 if (instr
->is_array
&& instr
->op
!= nir_texop_lod
)
4378 coords
[3] = apply_round_slice(ctx
, coords
[3]);
4379 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
4380 coords
[chan
] = to_float(ctx
, coords
[chan
]);
4381 if (instr
->coord_components
== 3)
4382 coords
[3] = LLVMGetUndef(ctx
->f32
);
4383 ac_prepare_cube_coords(&ctx
->ac
,
4384 instr
->op
== nir_texop_txd
, instr
->is_array
,
4391 for (unsigned i
= 0; i
< num_deriv_comp
* 2; i
++)
4392 address
[count
++] = derivs
[i
];
4395 /* Pack texture coordinates */
4397 address
[count
++] = coords
[0];
4398 if (instr
->coord_components
> 1) {
4399 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&& instr
->is_array
&& instr
->op
!= nir_texop_txf
) {
4400 coords
[1] = apply_round_slice(ctx
, coords
[1]);
4402 address
[count
++] = coords
[1];
4404 if (instr
->coord_components
> 2) {
4405 /* This seems like a bit of a hack - but it passes Vulkan CTS with it */
4406 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_3D
&&
4407 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_CUBE
&&
4408 instr
->op
!= nir_texop_txf
) {
4409 coords
[2] = apply_round_slice(ctx
, coords
[2]);
4411 address
[count
++] = coords
[2];
4416 if (lod
&& ((instr
->op
== nir_texop_txl
&& !lod_is_zero
) ||
4417 instr
->op
== nir_texop_txf
)) {
4418 address
[count
++] = lod
;
4419 } else if (instr
->op
== nir_texop_txf_ms
&& sample_index
) {
4420 address
[count
++] = sample_index
;
4421 } else if(instr
->op
== nir_texop_txs
) {
4424 address
[count
++] = lod
;
4426 address
[count
++] = ctx
->i32zero
;
4429 for (chan
= 0; chan
< count
; chan
++) {
4430 address
[chan
] = LLVMBuildBitCast(ctx
->builder
,
4431 address
[chan
], ctx
->i32
, "");
4434 if (instr
->op
== nir_texop_samples_identical
) {
4435 LLVMValueRef txf_address
[4];
4436 struct ac_image_args txf_args
= { 0 };
4437 unsigned txf_count
= count
;
4438 memcpy(txf_address
, address
, sizeof(txf_address
));
4440 if (!instr
->is_array
)
4441 txf_address
[2] = ctx
->i32zero
;
4442 txf_address
[3] = ctx
->i32zero
;
4444 set_tex_fetch_args(ctx
, &txf_args
, instr
, nir_texop_txf
,
4446 txf_address
, txf_count
, 0xf);
4448 result
= build_tex_intrinsic(ctx
, instr
, false, &txf_args
);
4450 result
= LLVMBuildExtractElement(ctx
->builder
, result
, ctx
->i32zero
, "");
4451 result
= emit_int_cmp(ctx
, LLVMIntEQ
, result
, ctx
->i32zero
);
4455 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
4456 instr
->op
!= nir_texop_txs
) {
4457 unsigned sample_chan
= instr
->is_array
? 3 : 2;
4458 address
[sample_chan
] = adjust_sample_index_using_fmask(ctx
,
4461 instr
->is_array
? address
[2] : NULL
,
4462 address
[sample_chan
],
4466 if (offsets
&& instr
->op
== nir_texop_txf
) {
4467 nir_const_value
*const_offset
=
4468 nir_src_as_const_value(instr
->src
[const_src
].src
);
4469 int num_offsets
= instr
->src
[const_src
].src
.ssa
->num_components
;
4470 assert(const_offset
);
4471 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
4472 if (num_offsets
> 2)
4473 address
[2] = LLVMBuildAdd(ctx
->builder
,
4474 address
[2], LLVMConstInt(ctx
->i32
, const_offset
->i32
[2], false), "");
4475 if (num_offsets
> 1)
4476 address
[1] = LLVMBuildAdd(ctx
->builder
,
4477 address
[1], LLVMConstInt(ctx
->i32
, const_offset
->i32
[1], false), "");
4478 address
[0] = LLVMBuildAdd(ctx
->builder
,
4479 address
[0], LLVMConstInt(ctx
->i32
, const_offset
->i32
[0], false), "");
4483 /* TODO TG4 support */
4484 if (instr
->op
== nir_texop_tg4
) {
4485 if (instr
->is_shadow
)
4488 dmask
= 1 << instr
->component
;
4490 set_tex_fetch_args(ctx
, &args
, instr
, instr
->op
,
4491 res_ptr
, samp_ptr
, address
, count
, dmask
);
4493 result
= build_tex_intrinsic(ctx
, instr
, lod_is_zero
, &args
);
4495 if (instr
->op
== nir_texop_query_levels
)
4496 result
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, 3, false), "");
4497 else if (instr
->is_shadow
&& instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&& instr
->op
!= nir_texop_tg4
)
4498 result
= LLVMBuildExtractElement(ctx
->builder
, result
, ctx
->i32zero
, "");
4499 else if (instr
->op
== nir_texop_txs
&&
4500 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
4502 LLVMValueRef two
= LLVMConstInt(ctx
->i32
, 2, false);
4503 LLVMValueRef six
= LLVMConstInt(ctx
->i32
, 6, false);
4504 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->builder
, result
, two
, "");
4505 z
= LLVMBuildSDiv(ctx
->builder
, z
, six
, "");
4506 result
= LLVMBuildInsertElement(ctx
->builder
, result
, z
, two
, "");
4507 } else if (instr
->dest
.ssa
.num_components
!= 4)
4508 result
= trim_vector(ctx
, result
, instr
->dest
.ssa
.num_components
);
4512 assert(instr
->dest
.is_ssa
);
4513 result
= to_integer(ctx
, result
);
4514 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4519 static void visit_phi(struct nir_to_llvm_context
*ctx
, nir_phi_instr
*instr
)
4521 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
4522 LLVMValueRef result
= LLVMBuildPhi(ctx
->builder
, type
, "");
4524 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4525 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
4528 static void visit_post_phi(struct nir_to_llvm_context
*ctx
,
4529 nir_phi_instr
*instr
,
4530 LLVMValueRef llvm_phi
)
4532 nir_foreach_phi_src(src
, instr
) {
4533 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
4534 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
4536 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
4540 static void phi_post_pass(struct nir_to_llvm_context
*ctx
)
4542 struct hash_entry
*entry
;
4543 hash_table_foreach(ctx
->phis
, entry
) {
4544 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
4545 (LLVMValueRef
)entry
->data
);
4550 static void visit_ssa_undef(struct nir_to_llvm_context
*ctx
,
4551 nir_ssa_undef_instr
*instr
)
4553 unsigned num_components
= instr
->def
.num_components
;
4556 if (num_components
== 1)
4557 undef
= LLVMGetUndef(ctx
->i32
);
4559 undef
= LLVMGetUndef(LLVMVectorType(ctx
->i32
, num_components
));
4561 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, undef
);
4564 static void visit_jump(struct nir_to_llvm_context
*ctx
,
4565 nir_jump_instr
*instr
)
4567 switch (instr
->type
) {
4568 case nir_jump_break
:
4569 LLVMBuildBr(ctx
->builder
, ctx
->break_block
);
4570 LLVMClearInsertionPosition(ctx
->builder
);
4572 case nir_jump_continue
:
4573 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
4574 LLVMClearInsertionPosition(ctx
->builder
);
4577 fprintf(stderr
, "Unknown NIR jump instr: ");
4578 nir_print_instr(&instr
->instr
, stderr
);
4579 fprintf(stderr
, "\n");
4584 static void visit_cf_list(struct nir_to_llvm_context
*ctx
,
4585 struct exec_list
*list
);
4587 static void visit_block(struct nir_to_llvm_context
*ctx
, nir_block
*block
)
4589 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->builder
);
4590 nir_foreach_instr(instr
, block
)
4592 switch (instr
->type
) {
4593 case nir_instr_type_alu
:
4594 visit_alu(ctx
, nir_instr_as_alu(instr
));
4596 case nir_instr_type_load_const
:
4597 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
4599 case nir_instr_type_intrinsic
:
4600 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
4602 case nir_instr_type_tex
:
4603 visit_tex(ctx
, nir_instr_as_tex(instr
));
4605 case nir_instr_type_phi
:
4606 visit_phi(ctx
, nir_instr_as_phi(instr
));
4608 case nir_instr_type_ssa_undef
:
4609 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
4611 case nir_instr_type_jump
:
4612 visit_jump(ctx
, nir_instr_as_jump(instr
));
4615 fprintf(stderr
, "Unknown NIR instr type: ");
4616 nir_print_instr(instr
, stderr
);
4617 fprintf(stderr
, "\n");
4622 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
4625 static void visit_if(struct nir_to_llvm_context
*ctx
, nir_if
*if_stmt
)
4627 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
4629 LLVMBasicBlockRef merge_block
=
4630 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
4631 LLVMBasicBlockRef if_block
=
4632 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
4633 LLVMBasicBlockRef else_block
= merge_block
;
4634 if (!exec_list_is_empty(&if_stmt
->else_list
))
4635 else_block
= LLVMAppendBasicBlockInContext(
4636 ctx
->context
, ctx
->main_function
, "");
4638 LLVMValueRef cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, value
,
4639 LLVMConstInt(ctx
->i32
, 0, false), "");
4640 LLVMBuildCondBr(ctx
->builder
, cond
, if_block
, else_block
);
4642 LLVMPositionBuilderAtEnd(ctx
->builder
, if_block
);
4643 visit_cf_list(ctx
, &if_stmt
->then_list
);
4644 if (LLVMGetInsertBlock(ctx
->builder
))
4645 LLVMBuildBr(ctx
->builder
, merge_block
);
4647 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
4648 LLVMPositionBuilderAtEnd(ctx
->builder
, else_block
);
4649 visit_cf_list(ctx
, &if_stmt
->else_list
);
4650 if (LLVMGetInsertBlock(ctx
->builder
))
4651 LLVMBuildBr(ctx
->builder
, merge_block
);
4654 LLVMPositionBuilderAtEnd(ctx
->builder
, merge_block
);
4657 static void visit_loop(struct nir_to_llvm_context
*ctx
, nir_loop
*loop
)
4659 LLVMBasicBlockRef continue_parent
= ctx
->continue_block
;
4660 LLVMBasicBlockRef break_parent
= ctx
->break_block
;
4662 ctx
->continue_block
=
4663 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
4665 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
4667 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
4668 LLVMPositionBuilderAtEnd(ctx
->builder
, ctx
->continue_block
);
4669 visit_cf_list(ctx
, &loop
->body
);
4671 if (LLVMGetInsertBlock(ctx
->builder
))
4672 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
4673 LLVMPositionBuilderAtEnd(ctx
->builder
, ctx
->break_block
);
4675 ctx
->continue_block
= continue_parent
;
4676 ctx
->break_block
= break_parent
;
4679 static void visit_cf_list(struct nir_to_llvm_context
*ctx
,
4680 struct exec_list
*list
)
4682 foreach_list_typed(nir_cf_node
, node
, node
, list
)
4684 switch (node
->type
) {
4685 case nir_cf_node_block
:
4686 visit_block(ctx
, nir_cf_node_as_block(node
));
4689 case nir_cf_node_if
:
4690 visit_if(ctx
, nir_cf_node_as_if(node
));
4693 case nir_cf_node_loop
:
4694 visit_loop(ctx
, nir_cf_node_as_loop(node
));
4704 handle_vs_input_decl(struct nir_to_llvm_context
*ctx
,
4705 struct nir_variable
*variable
)
4707 LLVMValueRef t_list_ptr
= ctx
->vertex_buffers
;
4708 LLVMValueRef t_offset
;
4709 LLVMValueRef t_list
;
4711 LLVMValueRef buffer_index
;
4712 int index
= variable
->data
.location
- VERT_ATTRIB_GENERIC0
;
4713 int idx
= variable
->data
.location
;
4714 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, true);
4716 variable
->data
.driver_location
= idx
* 4;
4718 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << index
)) {
4719 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->instance_id
,
4720 ctx
->start_instance
, "");
4721 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
4722 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
4724 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->vertex_id
,
4725 ctx
->base_vertex
, "");
4727 for (unsigned i
= 0; i
< attrib_count
; ++i
, ++idx
) {
4728 t_offset
= LLVMConstInt(ctx
->i32
, index
+ i
, false);
4730 t_list
= ac_build_indexed_load_const(&ctx
->ac
, t_list_ptr
, t_offset
);
4732 input
= ac_build_buffer_load_format(&ctx
->ac
, t_list
,
4734 LLVMConstInt(ctx
->i32
, 0, false),
4737 for (unsigned chan
= 0; chan
< 4; chan
++) {
4738 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
4739 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
, chan
)] =
4740 to_integer(ctx
, LLVMBuildExtractElement(ctx
->builder
,
4741 input
, llvm_chan
, ""));
4746 static void interp_fs_input(struct nir_to_llvm_context
*ctx
,
4748 LLVMValueRef interp_param
,
4749 LLVMValueRef prim_mask
,
4750 LLVMValueRef result
[4])
4752 LLVMValueRef attr_number
;
4755 bool interp
= interp_param
!= NULL
;
4757 attr_number
= LLVMConstInt(ctx
->i32
, attr
, false);
4759 /* fs.constant returns the param from the middle vertex, so it's not
4760 * really useful for flat shading. It's meant to be used for custom
4761 * interpolation (but the intrinsic can't fetch from the other two
4764 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
4765 * to do the right thing. The only reason we use fs.constant is that
4766 * fs.interp cannot be used on integers, because they can be equal
4770 interp_param
= LLVMBuildBitCast(ctx
->builder
, interp_param
,
4771 LLVMVectorType(ctx
->f32
, 2), "");
4773 i
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
4775 j
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
4779 for (chan
= 0; chan
< 4; chan
++) {
4780 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
4783 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
4788 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
4789 LLVMConstInt(ctx
->i32
, 2, false),
4798 handle_fs_input_decl(struct nir_to_llvm_context
*ctx
,
4799 struct nir_variable
*variable
)
4801 int idx
= variable
->data
.location
;
4802 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4803 LLVMValueRef interp
;
4805 variable
->data
.driver_location
= idx
* 4;
4806 ctx
->input_mask
|= ((1ull << attrib_count
) - 1) << variable
->data
.location
;
4808 if (glsl_get_base_type(glsl_without_array(variable
->type
)) == GLSL_TYPE_FLOAT
) {
4809 unsigned interp_type
;
4810 if (variable
->data
.sample
) {
4811 interp_type
= INTERP_SAMPLE
;
4812 ctx
->shader_info
->fs
.force_persample
= true;
4813 } else if (variable
->data
.centroid
)
4814 interp_type
= INTERP_CENTROID
;
4816 interp_type
= INTERP_CENTER
;
4818 interp
= lookup_interp_param(ctx
, variable
->data
.interpolation
, interp_type
);
4822 for (unsigned i
= 0; i
< attrib_count
; ++i
)
4823 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
+ i
, 0)] = interp
;
4828 handle_shader_input_decl(struct nir_to_llvm_context
*ctx
,
4829 struct nir_variable
*variable
)
4831 switch (ctx
->stage
) {
4832 case MESA_SHADER_VERTEX
:
4833 handle_vs_input_decl(ctx
, variable
);
4835 case MESA_SHADER_FRAGMENT
:
4836 handle_fs_input_decl(ctx
, variable
);
4845 handle_fs_inputs_pre(struct nir_to_llvm_context
*ctx
,
4846 struct nir_shader
*nir
)
4849 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_INPUTS
; ++i
) {
4850 LLVMValueRef interp_param
;
4851 LLVMValueRef
*inputs
= ctx
->inputs
+radeon_llvm_reg_index_soa(i
, 0);
4853 if (!(ctx
->input_mask
& (1ull << i
)))
4856 if (i
>= VARYING_SLOT_VAR0
|| i
== VARYING_SLOT_PNTC
||
4857 i
== VARYING_SLOT_PRIMITIVE_ID
|| i
== VARYING_SLOT_LAYER
) {
4858 interp_param
= *inputs
;
4859 interp_fs_input(ctx
, index
, interp_param
, ctx
->prim_mask
,
4863 ctx
->shader_info
->fs
.flat_shaded_mask
|= 1u << index
;
4865 } else if (i
== VARYING_SLOT_POS
) {
4866 for(int i
= 0; i
< 3; ++i
)
4867 inputs
[i
] = ctx
->frag_pos
[i
];
4869 inputs
[3] = ac_build_fdiv(&ctx
->ac
, ctx
->f32one
, ctx
->frag_pos
[3]);
4872 ctx
->shader_info
->fs
.num_interp
= index
;
4873 if (ctx
->input_mask
& (1 << VARYING_SLOT_PNTC
))
4874 ctx
->shader_info
->fs
.has_pcoord
= true;
4875 if (ctx
->input_mask
& (1 << VARYING_SLOT_PRIMITIVE_ID
))
4876 ctx
->shader_info
->fs
.prim_id_input
= true;
4877 if (ctx
->input_mask
& (1 << VARYING_SLOT_LAYER
))
4878 ctx
->shader_info
->fs
.layer_input
= true;
4879 ctx
->shader_info
->fs
.input_mask
= ctx
->input_mask
>> VARYING_SLOT_VAR0
;
4883 ac_build_alloca(struct nir_to_llvm_context
*ctx
,
4887 LLVMBuilderRef builder
= ctx
->builder
;
4888 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
4889 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
4890 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
4891 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
4892 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ctx
->context
);
4896 LLVMPositionBuilderBefore(first_builder
, first_instr
);
4898 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
4901 res
= LLVMBuildAlloca(first_builder
, type
, name
);
4902 LLVMBuildStore(builder
, LLVMConstNull(type
), res
);
4904 LLVMDisposeBuilder(first_builder
);
4909 static LLVMValueRef
si_build_alloca_undef(struct nir_to_llvm_context
*ctx
,
4913 LLVMValueRef ptr
= ac_build_alloca(ctx
, type
, name
);
4914 LLVMBuildStore(ctx
->builder
, LLVMGetUndef(type
), ptr
);
4919 handle_shader_output_decl(struct nir_to_llvm_context
*ctx
,
4920 struct nir_variable
*variable
)
4922 int idx
= variable
->data
.location
+ variable
->data
.index
;
4923 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4924 uint64_t mask_attribs
;
4925 variable
->data
.driver_location
= idx
* 4;
4927 /* tess ctrl has it's own load/store paths for outputs */
4928 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
4931 mask_attribs
= ((1ull << attrib_count
) - 1) << idx
;
4932 if (ctx
->stage
== MESA_SHADER_VERTEX
||
4933 ctx
->stage
== MESA_SHADER_TESS_EVAL
||
4934 ctx
->stage
== MESA_SHADER_GEOMETRY
) {
4935 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
4936 int length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
4937 if (ctx
->stage
== MESA_SHADER_VERTEX
) {
4938 ctx
->shader_info
->vs
.outinfo
.clip_dist_mask
= (1 << ctx
->num_output_clips
) - 1;
4939 ctx
->shader_info
->vs
.outinfo
.cull_dist_mask
= (1 << ctx
->num_output_culls
) - 1;
4941 if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
4942 ctx
->shader_info
->tes
.outinfo
.clip_dist_mask
= (1 << ctx
->num_output_clips
) - 1;
4943 ctx
->shader_info
->tes
.outinfo
.cull_dist_mask
= (1 << ctx
->num_output_culls
) - 1;
4950 mask_attribs
= 1ull << idx
;
4954 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
4955 for (unsigned chan
= 0; chan
< 4; chan
++) {
4956 ctx
->outputs
[radeon_llvm_reg_index_soa(idx
+ i
, chan
)] =
4957 si_build_alloca_undef(ctx
, ctx
->f32
, "");
4960 ctx
->output_mask
|= mask_attribs
;
4964 setup_locals(struct nir_to_llvm_context
*ctx
,
4965 struct nir_function
*func
)
4968 ctx
->num_locals
= 0;
4969 nir_foreach_variable(variable
, &func
->impl
->locals
) {
4970 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4971 variable
->data
.driver_location
= ctx
->num_locals
* 4;
4972 ctx
->num_locals
+= attrib_count
;
4974 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
4978 for (i
= 0; i
< ctx
->num_locals
; i
++) {
4979 for (j
= 0; j
< 4; j
++) {
4980 ctx
->locals
[i
* 4 + j
] =
4981 si_build_alloca_undef(ctx
, ctx
->f32
, "temp");
4987 emit_float_saturate(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
, float lo
, float hi
)
4989 v
= to_float(ctx
, v
);
4990 v
= emit_intrin_2f_param(ctx
, "llvm.maxnum.f32", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, lo
));
4991 return emit_intrin_2f_param(ctx
, "llvm.minnum.f32", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, hi
));
4995 static LLVMValueRef
emit_pack_int16(struct nir_to_llvm_context
*ctx
,
4996 LLVMValueRef src0
, LLVMValueRef src1
)
4998 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
4999 LLVMValueRef comp
[2];
5001 comp
[0] = LLVMBuildAnd(ctx
->builder
, src0
, LLVMConstInt(ctx
-> i32
, 65535, 0), "");
5002 comp
[1] = LLVMBuildAnd(ctx
->builder
, src1
, LLVMConstInt(ctx
-> i32
, 65535, 0), "");
5003 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
5004 return LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
5007 /* Initialize arguments for the shader export intrinsic */
5009 si_llvm_init_export_args(struct nir_to_llvm_context
*ctx
,
5010 LLVMValueRef
*values
,
5012 struct ac_export_args
*args
)
5014 /* Default is 0xf. Adjusted below depending on the format. */
5015 args
->enabled_channels
= 0xf;
5017 /* Specify whether the EXEC mask represents the valid mask */
5018 args
->valid_mask
= 0;
5020 /* Specify whether this is the last export */
5023 /* Specify the target we are exporting */
5024 args
->target
= target
;
5026 args
->compr
= false;
5027 args
->out
[0] = LLVMGetUndef(ctx
->f32
);
5028 args
->out
[1] = LLVMGetUndef(ctx
->f32
);
5029 args
->out
[2] = LLVMGetUndef(ctx
->f32
);
5030 args
->out
[3] = LLVMGetUndef(ctx
->f32
);
5035 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&& target
>= V_008DFC_SQ_EXP_MRT
) {
5036 LLVMValueRef val
[4];
5037 unsigned index
= target
- V_008DFC_SQ_EXP_MRT
;
5038 unsigned col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * index
)) & 0xf;
5039 bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> index
) & 1;
5041 switch(col_format
) {
5042 case V_028714_SPI_SHADER_ZERO
:
5043 args
->enabled_channels
= 0; /* writemask */
5044 args
->target
= V_008DFC_SQ_EXP_NULL
;
5047 case V_028714_SPI_SHADER_32_R
:
5048 args
->enabled_channels
= 1;
5049 args
->out
[0] = values
[0];
5052 case V_028714_SPI_SHADER_32_GR
:
5053 args
->enabled_channels
= 0x3;
5054 args
->out
[0] = values
[0];
5055 args
->out
[1] = values
[1];
5058 case V_028714_SPI_SHADER_32_AR
:
5059 args
->enabled_channels
= 0x9;
5060 args
->out
[0] = values
[0];
5061 args
->out
[3] = values
[3];
5064 case V_028714_SPI_SHADER_FP16_ABGR
:
5067 for (unsigned chan
= 0; chan
< 2; chan
++) {
5068 LLVMValueRef pack_args
[2] = {
5070 values
[2 * chan
+ 1]
5072 LLVMValueRef packed
;
5074 packed
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, pack_args
);
5075 args
->out
[chan
] = packed
;
5079 case V_028714_SPI_SHADER_UNORM16_ABGR
:
5080 for (unsigned chan
= 0; chan
< 4; chan
++) {
5081 val
[chan
] = ac_build_clamp(&ctx
->ac
, values
[chan
]);
5082 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
5083 LLVMConstReal(ctx
->f32
, 65535), "");
5084 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
5085 LLVMConstReal(ctx
->f32
, 0.5), "");
5086 val
[chan
] = LLVMBuildFPToUI(ctx
->builder
, val
[chan
],
5091 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5092 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5095 case V_028714_SPI_SHADER_SNORM16_ABGR
:
5096 for (unsigned chan
= 0; chan
< 4; chan
++) {
5097 val
[chan
] = emit_float_saturate(ctx
, values
[chan
], -1, 1);
5098 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
5099 LLVMConstReal(ctx
->f32
, 32767), "");
5101 /* If positive, add 0.5, else add -0.5. */
5102 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
5103 LLVMBuildSelect(ctx
->builder
,
5104 LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
,
5105 val
[chan
], ctx
->f32zero
, ""),
5106 LLVMConstReal(ctx
->f32
, 0.5),
5107 LLVMConstReal(ctx
->f32
, -0.5), ""), "");
5108 val
[chan
] = LLVMBuildFPToSI(ctx
->builder
, val
[chan
], ctx
->i32
, "");
5112 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5113 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5116 case V_028714_SPI_SHADER_UINT16_ABGR
: {
5117 LLVMValueRef max
= LLVMConstInt(ctx
->i32
, is_int8
? 255 : 65535, 0);
5119 for (unsigned chan
= 0; chan
< 4; chan
++) {
5120 val
[chan
] = to_integer(ctx
, values
[chan
]);
5121 val
[chan
] = emit_minmax_int(ctx
, LLVMIntULT
, val
[chan
], max
);
5125 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5126 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5130 case V_028714_SPI_SHADER_SINT16_ABGR
: {
5131 LLVMValueRef max
= LLVMConstInt(ctx
->i32
, is_int8
? 127 : 32767, 0);
5132 LLVMValueRef min
= LLVMConstInt(ctx
->i32
, is_int8
? -128 : -32768, 0);
5135 for (unsigned chan
= 0; chan
< 4; chan
++) {
5136 val
[chan
] = to_integer(ctx
, values
[chan
]);
5137 val
[chan
] = emit_minmax_int(ctx
, LLVMIntSLT
, val
[chan
], max
);
5138 val
[chan
] = emit_minmax_int(ctx
, LLVMIntSGT
, val
[chan
], min
);
5142 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5143 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5148 case V_028714_SPI_SHADER_32_ABGR
:
5149 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
5153 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
5155 for (unsigned i
= 0; i
< 4; ++i
)
5156 args
->out
[i
] = to_float(ctx
, args
->out
[i
]);
5160 handle_vs_outputs_post(struct nir_to_llvm_context
*ctx
,
5161 struct ac_vs_output_info
*outinfo
)
5163 uint32_t param_count
= 0;
5165 unsigned pos_idx
, num_pos_exports
= 0;
5166 struct ac_export_args args
, pos_args
[4] = {};
5167 LLVMValueRef psize_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
5170 memset(outinfo
->vs_output_param_offset
, AC_EXP_PARAM_UNDEFINED
,
5171 sizeof(outinfo
->vs_output_param_offset
));
5173 if (ctx
->output_mask
& (1ull << VARYING_SLOT_CLIP_DIST0
)) {
5174 LLVMValueRef slots
[8];
5177 if (outinfo
->cull_dist_mask
)
5178 outinfo
->cull_dist_mask
<<= ctx
->num_output_clips
;
5180 i
= VARYING_SLOT_CLIP_DIST0
;
5181 for (j
= 0; j
< ctx
->num_output_clips
+ ctx
->num_output_culls
; j
++)
5182 slots
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
5183 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5185 for (i
= ctx
->num_output_clips
+ ctx
->num_output_culls
; i
< 8; i
++)
5186 slots
[i
] = LLVMGetUndef(ctx
->f32
);
5188 if (ctx
->num_output_clips
+ ctx
->num_output_culls
> 4) {
5189 target
= V_008DFC_SQ_EXP_POS
+ 3;
5190 si_llvm_init_export_args(ctx
, &slots
[4], target
, &args
);
5191 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5192 &args
, sizeof(args
));
5195 target
= V_008DFC_SQ_EXP_POS
+ 2;
5196 si_llvm_init_export_args(ctx
, &slots
[0], target
, &args
);
5197 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5198 &args
, sizeof(args
));
5202 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5203 LLVMValueRef values
[4];
5204 if (!(ctx
->output_mask
& (1ull << i
)))
5207 for (unsigned j
= 0; j
< 4; j
++)
5208 values
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
5209 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5211 if (i
== VARYING_SLOT_POS
) {
5212 target
= V_008DFC_SQ_EXP_POS
;
5213 } else if (i
== VARYING_SLOT_CLIP_DIST0
) {
5215 } else if (i
== VARYING_SLOT_PSIZ
) {
5216 outinfo
->writes_pointsize
= true;
5217 psize_value
= values
[0];
5219 } else if (i
== VARYING_SLOT_LAYER
) {
5220 outinfo
->writes_layer
= true;
5221 layer_value
= values
[0];
5222 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5223 outinfo
->vs_output_param_offset
[VARYING_SLOT_LAYER
] = param_count
;
5225 } else if (i
== VARYING_SLOT_VIEWPORT
) {
5226 outinfo
->writes_viewport_index
= true;
5227 viewport_index_value
= values
[0];
5229 } else if (i
== VARYING_SLOT_PRIMITIVE_ID
) {
5230 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5231 outinfo
->vs_output_param_offset
[VARYING_SLOT_PRIMITIVE_ID
] = param_count
;
5233 } else if (i
>= VARYING_SLOT_VAR0
) {
5234 outinfo
->export_mask
|= 1u << (i
- VARYING_SLOT_VAR0
);
5235 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5236 outinfo
->vs_output_param_offset
[i
] = param_count
;
5240 si_llvm_init_export_args(ctx
, values
, target
, &args
);
5242 if (target
>= V_008DFC_SQ_EXP_POS
&&
5243 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
5244 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5245 &args
, sizeof(args
));
5247 ac_build_export(&ctx
->ac
, &args
);
5251 /* We need to add the position output manually if it's missing. */
5252 if (!pos_args
[0].out
[0]) {
5253 pos_args
[0].enabled_channels
= 0xf;
5254 pos_args
[0].valid_mask
= 0;
5255 pos_args
[0].done
= 0;
5256 pos_args
[0].target
= V_008DFC_SQ_EXP_POS
;
5257 pos_args
[0].compr
= 0;
5258 pos_args
[0].out
[0] = ctx
->f32zero
; /* X */
5259 pos_args
[0].out
[1] = ctx
->f32zero
; /* Y */
5260 pos_args
[0].out
[2] = ctx
->f32zero
; /* Z */
5261 pos_args
[0].out
[3] = ctx
->f32one
; /* W */
5264 uint32_t mask
= ((outinfo
->writes_pointsize
== true ? 1 : 0) |
5265 (outinfo
->writes_layer
== true ? 4 : 0) |
5266 (outinfo
->writes_viewport_index
== true ? 8 : 0));
5268 pos_args
[1].enabled_channels
= mask
;
5269 pos_args
[1].valid_mask
= 0;
5270 pos_args
[1].done
= 0;
5271 pos_args
[1].target
= V_008DFC_SQ_EXP_POS
+ 1;
5272 pos_args
[1].compr
= 0;
5273 pos_args
[1].out
[0] = ctx
->f32zero
; /* X */
5274 pos_args
[1].out
[1] = ctx
->f32zero
; /* Y */
5275 pos_args
[1].out
[2] = ctx
->f32zero
; /* Z */
5276 pos_args
[1].out
[3] = ctx
->f32zero
; /* W */
5278 if (outinfo
->writes_pointsize
== true)
5279 pos_args
[1].out
[0] = psize_value
;
5280 if (outinfo
->writes_layer
== true)
5281 pos_args
[1].out
[2] = layer_value
;
5282 if (outinfo
->writes_viewport_index
== true)
5283 pos_args
[1].out
[3] = viewport_index_value
;
5285 for (i
= 0; i
< 4; i
++) {
5286 if (pos_args
[i
].out
[0])
5291 for (i
= 0; i
< 4; i
++) {
5292 if (!pos_args
[i
].out
[0])
5295 /* Specify the target we are exporting */
5296 pos_args
[i
].target
= V_008DFC_SQ_EXP_POS
+ pos_idx
++;
5297 if (pos_idx
== num_pos_exports
)
5298 pos_args
[i
].done
= 1;
5299 ac_build_export(&ctx
->ac
, &pos_args
[i
]);
5302 outinfo
->pos_exports
= num_pos_exports
;
5303 outinfo
->param_exports
= param_count
;
5307 handle_es_outputs_post(struct nir_to_llvm_context
*ctx
,
5308 struct ac_es_output_info
*outinfo
)
5311 uint64_t max_output_written
= 0;
5312 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5313 LLVMValueRef
*out_ptr
= &ctx
->outputs
[i
* 4];
5317 if (!(ctx
->output_mask
& (1ull << i
)))
5320 if (i
== VARYING_SLOT_CLIP_DIST0
)
5321 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
5323 param_index
= shader_io_get_unique_index(i
);
5325 max_output_written
= MAX2(param_index
+ (length
> 4), max_output_written
);
5327 for (j
= 0; j
< length
; j
++) {
5328 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], "");
5329 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->i32
, "");
5331 ac_build_buffer_store_dword(&ctx
->ac
,
5334 NULL
, ctx
->es2gs_offset
,
5335 (4 * param_index
+ j
) * 4,
5339 outinfo
->esgs_itemsize
= (max_output_written
+ 1) * 16;
5343 handle_ls_outputs_post(struct nir_to_llvm_context
*ctx
)
5345 LLVMValueRef vertex_id
= ctx
->rel_auto_id
;
5346 LLVMValueRef vertex_dw_stride
= unpack_param(ctx
, ctx
->ls_out_layout
, 13, 8);
5347 LLVMValueRef base_dw_addr
= LLVMBuildMul(ctx
->builder
, vertex_id
,
5348 vertex_dw_stride
, "");
5350 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5351 LLVMValueRef
*out_ptr
= &ctx
->outputs
[i
* 4];
5354 if (!(ctx
->output_mask
& (1ull << i
)))
5357 if (i
== VARYING_SLOT_CLIP_DIST0
)
5358 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
5359 int param
= shader_io_get_unique_index(i
);
5360 mark_tess_output(ctx
, false, param
);
5362 mark_tess_output(ctx
, false, param
+ 1);
5363 LLVMValueRef dw_addr
= LLVMBuildAdd(ctx
->builder
, base_dw_addr
,
5364 LLVMConstInt(ctx
->i32
, param
* 4, false),
5366 for (unsigned j
= 0; j
< length
; j
++) {
5367 lds_store(ctx
, dw_addr
,
5368 LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], ""));
5369 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
, ctx
->i32one
, "");
5374 struct ac_build_if_state
5376 struct nir_to_llvm_context
*ctx
;
5377 LLVMValueRef condition
;
5378 LLVMBasicBlockRef entry_block
;
5379 LLVMBasicBlockRef true_block
;
5380 LLVMBasicBlockRef false_block
;
5381 LLVMBasicBlockRef merge_block
;
5384 static LLVMBasicBlockRef
5385 ac_build_insert_new_block(struct nir_to_llvm_context
*ctx
, const char *name
)
5387 LLVMBasicBlockRef current_block
;
5388 LLVMBasicBlockRef next_block
;
5389 LLVMBasicBlockRef new_block
;
5391 /* get current basic block */
5392 current_block
= LLVMGetInsertBlock(ctx
->builder
);
5394 /* chqeck if there's another block after this one */
5395 next_block
= LLVMGetNextBasicBlock(current_block
);
5397 /* insert the new block before the next block */
5398 new_block
= LLVMInsertBasicBlockInContext(ctx
->context
, next_block
, name
);
5401 /* append new block after current block */
5402 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
5403 new_block
= LLVMAppendBasicBlockInContext(ctx
->context
, function
, name
);
5409 ac_nir_build_if(struct ac_build_if_state
*ifthen
,
5410 struct nir_to_llvm_context
*ctx
,
5411 LLVMValueRef condition
)
5413 LLVMBasicBlockRef block
= LLVMGetInsertBlock(ctx
->builder
);
5415 memset(ifthen
, 0, sizeof *ifthen
);
5417 ifthen
->condition
= condition
;
5418 ifthen
->entry_block
= block
;
5420 /* create endif/merge basic block for the phi functions */
5421 ifthen
->merge_block
= ac_build_insert_new_block(ctx
, "endif-block");
5423 /* create/insert true_block before merge_block */
5424 ifthen
->true_block
=
5425 LLVMInsertBasicBlockInContext(ctx
->context
,
5426 ifthen
->merge_block
,
5429 /* successive code goes into the true block */
5430 LLVMPositionBuilderAtEnd(ctx
->builder
, ifthen
->true_block
);
5434 * End a conditional.
5437 ac_nir_build_endif(struct ac_build_if_state
*ifthen
)
5439 LLVMBuilderRef builder
= ifthen
->ctx
->builder
;
5441 /* Insert branch to the merge block from current block */
5442 LLVMBuildBr(builder
, ifthen
->merge_block
);
5445 * Now patch in the various branch instructions.
5448 /* Insert the conditional branch instruction at the end of entry_block */
5449 LLVMPositionBuilderAtEnd(builder
, ifthen
->entry_block
);
5450 if (ifthen
->false_block
) {
5451 /* we have an else clause */
5452 LLVMBuildCondBr(builder
, ifthen
->condition
,
5453 ifthen
->true_block
, ifthen
->false_block
);
5456 /* no else clause */
5457 LLVMBuildCondBr(builder
, ifthen
->condition
,
5458 ifthen
->true_block
, ifthen
->merge_block
);
5461 /* Resume building code at end of the ifthen->merge_block */
5462 LLVMPositionBuilderAtEnd(builder
, ifthen
->merge_block
);
5466 write_tess_factors(struct nir_to_llvm_context
*ctx
)
5468 unsigned stride
, outer_comps
, inner_comps
;
5469 struct ac_build_if_state if_ctx
, inner_if_ctx
;
5470 LLVMValueRef invocation_id
= unpack_param(ctx
, ctx
->tcs_rel_ids
, 8, 5);
5471 LLVMValueRef rel_patch_id
= unpack_param(ctx
, ctx
->tcs_rel_ids
, 0, 8);
5472 unsigned tess_inner_index
, tess_outer_index
;
5473 LLVMValueRef lds_base
, lds_inner
, lds_outer
, byteoffset
, buffer
;
5474 LLVMValueRef out
[6], vec0
, vec1
, tf_base
, inner
[4], outer
[4];
5478 switch (ctx
->options
->key
.tcs
.primitive_mode
) {
5498 ac_nir_build_if(&if_ctx
, ctx
,
5499 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
5500 invocation_id
, ctx
->i32zero
, ""));
5502 tess_inner_index
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
5503 tess_outer_index
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
5505 mark_tess_output(ctx
, true, tess_inner_index
);
5506 mark_tess_output(ctx
, true, tess_outer_index
);
5507 lds_base
= get_tcs_out_current_patch_data_offset(ctx
);
5508 lds_inner
= LLVMBuildAdd(ctx
->builder
, lds_base
,
5509 LLVMConstInt(ctx
->i32
, tess_inner_index
* 4, false), "");
5510 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_base
,
5511 LLVMConstInt(ctx
->i32
, tess_outer_index
* 4, false), "");
5513 for (i
= 0; i
< 4; i
++) {
5514 inner
[i
] = LLVMGetUndef(ctx
->i32
);
5515 outer
[i
] = LLVMGetUndef(ctx
->i32
);
5519 if (ctx
->options
->key
.tcs
.primitive_mode
== GL_ISOLINES
) {
5520 outer
[0] = out
[1] = lds_load(ctx
, lds_outer
);
5521 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_outer
,
5522 LLVMConstInt(ctx
->i32
, 1, false), "");
5523 outer
[1] = out
[0] = lds_load(ctx
, lds_outer
);
5525 for (i
= 0; i
< outer_comps
; i
++) {
5527 lds_load(ctx
, lds_outer
);
5528 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_outer
,
5529 LLVMConstInt(ctx
->i32
, 1, false), "");
5531 for (i
= 0; i
< inner_comps
; i
++) {
5532 inner
[i
] = out
[outer_comps
+i
] =
5533 lds_load(ctx
, lds_inner
);
5534 lds_inner
= LLVMBuildAdd(ctx
->builder
, lds_inner
,
5535 LLVMConstInt(ctx
->i32
, 1, false), "");
5539 /* Convert the outputs to vectors for stores. */
5540 vec0
= ac_build_gather_values(&ctx
->ac
, out
, MIN2(stride
, 4));
5544 vec1
= ac_build_gather_values(&ctx
->ac
, out
+ 4, stride
- 4);
5547 buffer
= ctx
->hs_ring_tess_factor
;
5548 tf_base
= ctx
->tess_factor_offset
;
5549 byteoffset
= LLVMBuildMul(ctx
->builder
, rel_patch_id
,
5550 LLVMConstInt(ctx
->i32
, 4 * stride
, false), "");
5552 ac_nir_build_if(&inner_if_ctx
, ctx
,
5553 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
5554 rel_patch_id
, ctx
->i32zero
, ""));
5556 /* Store the dynamic HS control word. */
5557 ac_build_buffer_store_dword(&ctx
->ac
, buffer
,
5558 LLVMConstInt(ctx
->i32
, 0x80000000, false),
5559 1, ctx
->i32zero
, tf_base
,
5560 0, 1, 0, true, false);
5561 ac_nir_build_endif(&inner_if_ctx
);
5563 /* Store the tessellation factors. */
5564 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec0
,
5565 MIN2(stride
, 4), byteoffset
, tf_base
,
5566 4, 1, 0, true, false);
5568 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec1
,
5569 stride
- 4, byteoffset
, tf_base
,
5570 20, 1, 0, true, false);
5572 //TODO store to offchip for TES to read - only if TES reads them
5574 LLVMValueRef inner_vec
, outer_vec
, tf_outer_offset
;
5575 LLVMValueRef tf_inner_offset
;
5576 unsigned param_outer
, param_inner
;
5578 param_outer
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
5579 tf_outer_offset
= get_tcs_tes_buffer_address(ctx
, NULL
,
5580 LLVMConstInt(ctx
->i32
, param_outer
, 0));
5582 outer_vec
= ac_build_gather_values(&ctx
->ac
, outer
,
5583 util_next_power_of_two(outer_comps
));
5585 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, outer_vec
,
5586 outer_comps
, tf_outer_offset
,
5587 ctx
->oc_lds
, 0, 1, 0, true, false);
5589 param_inner
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
5590 tf_inner_offset
= get_tcs_tes_buffer_address(ctx
, NULL
,
5591 LLVMConstInt(ctx
->i32
, param_inner
, 0));
5593 inner_vec
= inner_comps
== 1 ? inner
[0] :
5594 ac_build_gather_values(&ctx
->ac
, inner
, inner_comps
);
5595 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, inner_vec
,
5596 inner_comps
, tf_inner_offset
,
5597 ctx
->oc_lds
, 0, 1, 0, true, false);
5600 ac_nir_build_endif(&if_ctx
);
5604 handle_tcs_outputs_post(struct nir_to_llvm_context
*ctx
)
5606 write_tess_factors(ctx
);
5610 si_export_mrt_color(struct nir_to_llvm_context
*ctx
,
5611 LLVMValueRef
*color
, unsigned param
, bool is_last
,
5612 struct ac_export_args
*args
)
5615 si_llvm_init_export_args(ctx
, color
, param
,
5619 args
->valid_mask
= 1; /* whether the EXEC mask is valid */
5620 args
->done
= 1; /* DONE bit */
5621 } else if (!args
->enabled_channels
)
5622 return false; /* unnecessary NULL export */
5628 si_export_mrt_z(struct nir_to_llvm_context
*ctx
,
5629 LLVMValueRef depth
, LLVMValueRef stencil
,
5630 LLVMValueRef samplemask
)
5632 struct ac_export_args args
;
5634 args
.enabled_channels
= 0;
5635 args
.valid_mask
= 1;
5637 args
.target
= V_008DFC_SQ_EXP_MRTZ
;
5640 args
.out
[0] = LLVMGetUndef(ctx
->f32
); /* R, depth */
5641 args
.out
[1] = LLVMGetUndef(ctx
->f32
); /* G, stencil test val[0:7], stencil op val[8:15] */
5642 args
.out
[2] = LLVMGetUndef(ctx
->f32
); /* B, sample mask */
5643 args
.out
[3] = LLVMGetUndef(ctx
->f32
); /* A, alpha to mask */
5646 args
.out
[0] = depth
;
5647 args
.enabled_channels
|= 0x1;
5651 args
.out
[1] = stencil
;
5652 args
.enabled_channels
|= 0x2;
5656 args
.out
[2] = samplemask
;
5657 args
.enabled_channels
|= 0x4;
5660 /* SI (except OLAND) has a bug that it only looks
5661 * at the X writemask component. */
5662 if (ctx
->options
->chip_class
== SI
&&
5663 ctx
->options
->family
!= CHIP_OLAND
)
5664 args
.enabled_channels
|= 0x1;
5666 ac_build_export(&ctx
->ac
, &args
);
5670 handle_fs_outputs_post(struct nir_to_llvm_context
*ctx
)
5673 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
5674 struct ac_export_args color_args
[8];
5676 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5677 LLVMValueRef values
[4];
5679 if (!(ctx
->output_mask
& (1ull << i
)))
5682 if (i
== FRAG_RESULT_DEPTH
) {
5683 ctx
->shader_info
->fs
.writes_z
= true;
5684 depth
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
5685 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
5686 } else if (i
== FRAG_RESULT_STENCIL
) {
5687 ctx
->shader_info
->fs
.writes_stencil
= true;
5688 stencil
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
5689 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
5690 } else if (i
== FRAG_RESULT_SAMPLE_MASK
) {
5691 ctx
->shader_info
->fs
.writes_sample_mask
= true;
5692 samplemask
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
5693 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
5696 for (unsigned j
= 0; j
< 4; j
++)
5697 values
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
5698 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5700 if (!ctx
->shader_info
->fs
.writes_z
&& !ctx
->shader_info
->fs
.writes_stencil
&& !ctx
->shader_info
->fs
.writes_sample_mask
)
5701 last
= ctx
->output_mask
<= ((1ull << (i
+ 1)) - 1);
5703 bool ret
= si_export_mrt_color(ctx
, values
, V_008DFC_SQ_EXP_MRT
+ (i
- FRAG_RESULT_DATA0
), last
, &color_args
[index
]);
5709 for (unsigned i
= 0; i
< index
; i
++)
5710 ac_build_export(&ctx
->ac
, &color_args
[i
]);
5711 if (depth
|| stencil
|| samplemask
)
5712 si_export_mrt_z(ctx
, depth
, stencil
, samplemask
);
5714 si_export_mrt_color(ctx
, NULL
, V_008DFC_SQ_EXP_NULL
, true, &color_args
[0]);
5715 ac_build_export(&ctx
->ac
, &color_args
[0]);
5718 ctx
->shader_info
->fs
.output_mask
= index
? ((1ull << index
) - 1) : 0;
5722 emit_gs_epilogue(struct nir_to_llvm_context
*ctx
)
5724 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_NOP
| AC_SENDMSG_GS_DONE
, ctx
->gs_wave_id
);
5728 handle_shader_outputs_post(struct nir_to_llvm_context
*ctx
)
5730 switch (ctx
->stage
) {
5731 case MESA_SHADER_VERTEX
:
5732 if (ctx
->options
->key
.vs
.as_ls
)
5733 handle_ls_outputs_post(ctx
);
5734 else if (ctx
->options
->key
.vs
.as_es
)
5735 handle_es_outputs_post(ctx
, &ctx
->shader_info
->vs
.es_info
);
5737 handle_vs_outputs_post(ctx
, &ctx
->shader_info
->vs
.outinfo
);
5739 case MESA_SHADER_FRAGMENT
:
5740 handle_fs_outputs_post(ctx
);
5742 case MESA_SHADER_GEOMETRY
:
5743 emit_gs_epilogue(ctx
);
5745 case MESA_SHADER_TESS_CTRL
:
5746 handle_tcs_outputs_post(ctx
);
5748 case MESA_SHADER_TESS_EVAL
:
5749 if (ctx
->options
->key
.tes
.as_es
)
5750 handle_es_outputs_post(ctx
, &ctx
->shader_info
->tes
.es_info
);
5752 handle_vs_outputs_post(ctx
, &ctx
->shader_info
->tes
.outinfo
);
5760 handle_shared_compute_var(struct nir_to_llvm_context
*ctx
,
5761 struct nir_variable
*variable
, uint32_t *offset
, int idx
)
5763 unsigned size
= glsl_count_attribute_slots(variable
->type
, false);
5764 variable
->data
.driver_location
= *offset
;
5768 static void ac_llvm_finalize_module(struct nir_to_llvm_context
* ctx
)
5770 LLVMPassManagerRef passmgr
;
5771 /* Create the pass manager */
5772 passmgr
= LLVMCreateFunctionPassManagerForModule(
5775 /* This pass should eliminate all the load and store instructions */
5776 LLVMAddPromoteMemoryToRegisterPass(passmgr
);
5778 /* Add some optimization passes */
5779 LLVMAddScalarReplAggregatesPass(passmgr
);
5780 LLVMAddLICMPass(passmgr
);
5781 LLVMAddAggressiveDCEPass(passmgr
);
5782 LLVMAddCFGSimplificationPass(passmgr
);
5783 LLVMAddInstructionCombiningPass(passmgr
);
5786 LLVMInitializeFunctionPassManager(passmgr
);
5787 LLVMRunFunctionPassManager(passmgr
, ctx
->main_function
);
5788 LLVMFinalizeFunctionPassManager(passmgr
);
5790 LLVMDisposeBuilder(ctx
->builder
);
5791 LLVMDisposePassManager(passmgr
);
5795 ac_nir_eliminate_const_vs_outputs(struct nir_to_llvm_context
*ctx
)
5797 struct ac_vs_output_info
*outinfo
;
5799 if (ctx
->stage
== MESA_SHADER_FRAGMENT
||
5800 ctx
->stage
== MESA_SHADER_COMPUTE
||
5801 ctx
->stage
== MESA_SHADER_TESS_CTRL
||
5802 ctx
->stage
== MESA_SHADER_GEOMETRY
)
5805 if (ctx
->stage
== MESA_SHADER_VERTEX
) {
5806 if (ctx
->options
->key
.vs
.as_ls
||
5807 ctx
->options
->key
.vs
.as_es
)
5809 outinfo
= &ctx
->shader_info
->vs
.outinfo
;
5812 if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
5813 if (ctx
->options
->key
.vs
.as_es
)
5815 outinfo
= &ctx
->shader_info
->tes
.outinfo
;
5818 ac_optimize_vs_outputs(&ctx
->ac
,
5820 outinfo
->vs_output_param_offset
,
5822 &outinfo
->param_exports
);
5826 ac_setup_rings(struct nir_to_llvm_context
*ctx
)
5828 if ((ctx
->stage
== MESA_SHADER_VERTEX
&& ctx
->options
->key
.vs
.as_es
) ||
5829 (ctx
->stage
== MESA_SHADER_TESS_EVAL
&& ctx
->options
->key
.tes
.as_es
)) {
5830 ctx
->esgs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_ESGS_VS
, false));
5833 if (ctx
->is_gs_copy_shader
) {
5834 ctx
->gsvs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_GSVS_VS
, false));
5836 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
5838 ctx
->esgs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_ESGS_GS
, false));
5839 ctx
->gsvs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_GSVS_GS
, false));
5841 ctx
->gsvs_ring
= LLVMBuildBitCast(ctx
->builder
, ctx
->gsvs_ring
, ctx
->v4i32
, "");
5843 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->gsvs_num_entries
, LLVMConstInt(ctx
->i32
, 2, false), "");
5844 tmp
= LLVMBuildExtractElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->i32one
, "");
5845 tmp
= LLVMBuildOr(ctx
->builder
, tmp
, ctx
->gsvs_ring_stride
, "");
5846 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, tmp
, ctx
->i32one
, "");
5848 ctx
->gsvs_ring
= LLVMBuildBitCast(ctx
->builder
, ctx
->gsvs_ring
, ctx
->v16i8
, "");
5851 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
5852 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
5853 ctx
->hs_ring_tess_offchip
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_HS_TESS_OFFCHIP
, false));
5854 ctx
->hs_ring_tess_factor
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_HS_TESS_FACTOR
, false));
5859 LLVMModuleRef
ac_translate_nir_to_llvm(LLVMTargetMachineRef tm
,
5860 struct nir_shader
*nir
,
5861 struct ac_shader_variant_info
*shader_info
,
5862 const struct ac_nir_compiler_options
*options
)
5864 struct nir_to_llvm_context ctx
= {0};
5865 struct nir_function
*func
;
5867 ctx
.options
= options
;
5868 ctx
.shader_info
= shader_info
;
5869 ctx
.context
= LLVMContextCreate();
5870 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
5872 ac_llvm_context_init(&ctx
.ac
, ctx
.context
);
5873 ctx
.ac
.module
= ctx
.module
;
5875 ctx
.has_ds_bpermute
= ctx
.options
->chip_class
>= VI
;
5877 memset(shader_info
, 0, sizeof(*shader_info
));
5879 ac_nir_shader_info_pass(nir
, options
, &shader_info
->info
);
5881 LLVMSetTarget(ctx
.module
, options
->supports_spill
? "amdgcn-mesa-mesa3d" : "amdgcn--");
5883 LLVMTargetDataRef data_layout
= LLVMCreateTargetDataLayout(tm
);
5884 char *data_layout_str
= LLVMCopyStringRepOfTargetData(data_layout
);
5885 LLVMSetDataLayout(ctx
.module
, data_layout_str
);
5886 LLVMDisposeTargetData(data_layout
);
5887 LLVMDisposeMessage(data_layout_str
);
5891 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
5892 ctx
.ac
.builder
= ctx
.builder
;
5893 ctx
.stage
= nir
->stage
;
5895 for (i
= 0; i
< AC_UD_MAX_SETS
; i
++)
5896 shader_info
->user_sgprs_locs
.descriptor_sets
[i
].sgpr_idx
= -1;
5897 for (i
= 0; i
< AC_UD_MAX_UD
; i
++)
5898 shader_info
->user_sgprs_locs
.shader_data
[i
].sgpr_idx
= -1;
5900 create_function(&ctx
);
5902 if (nir
->stage
== MESA_SHADER_COMPUTE
) {
5904 nir_foreach_variable(variable
, &nir
->shared
)
5908 uint32_t shared_size
= 0;
5910 LLVMTypeRef i8p
= LLVMPointerType(ctx
.i8
, LOCAL_ADDR_SPACE
);
5911 nir_foreach_variable(variable
, &nir
->shared
) {
5912 handle_shared_compute_var(&ctx
, variable
, &shared_size
, idx
);
5917 var
= LLVMAddGlobalInAddressSpace(ctx
.module
,
5918 LLVMArrayType(ctx
.i8
, shared_size
),
5921 LLVMSetAlignment(var
, 4);
5922 ctx
.shared_memory
= LLVMBuildBitCast(ctx
.builder
, var
, i8p
, "");
5924 } else if (nir
->stage
== MESA_SHADER_GEOMETRY
) {
5925 ctx
.gs_next_vertex
= ac_build_alloca(&ctx
, ctx
.i32
, "gs_next_vertex");
5927 ctx
.gs_max_out_vertices
= nir
->info
.gs
.vertices_out
;
5928 } else if (nir
->stage
== MESA_SHADER_TESS_EVAL
) {
5929 ctx
.tes_primitive_mode
= nir
->info
.tess
.primitive_mode
;
5932 ac_setup_rings(&ctx
);
5934 nir_foreach_variable(variable
, &nir
->inputs
)
5935 handle_shader_input_decl(&ctx
, variable
);
5937 if (nir
->stage
== MESA_SHADER_FRAGMENT
)
5938 handle_fs_inputs_pre(&ctx
, nir
);
5940 ctx
.num_output_clips
= nir
->info
.clip_distance_array_size
;
5941 ctx
.num_output_culls
= nir
->info
.cull_distance_array_size
;
5943 nir_foreach_variable(variable
, &nir
->outputs
)
5944 handle_shader_output_decl(&ctx
, variable
);
5946 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
5947 _mesa_key_pointer_equal
);
5948 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
5949 _mesa_key_pointer_equal
);
5951 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
5953 setup_locals(&ctx
, func
);
5955 visit_cf_list(&ctx
, &func
->impl
->body
);
5956 phi_post_pass(&ctx
);
5958 handle_shader_outputs_post(&ctx
);
5959 LLVMBuildRetVoid(ctx
.builder
);
5961 ac_llvm_finalize_module(&ctx
);
5963 ac_nir_eliminate_const_vs_outputs(&ctx
);
5965 ralloc_free(ctx
.defs
);
5966 ralloc_free(ctx
.phis
);
5968 if (nir
->stage
== MESA_SHADER_GEOMETRY
) {
5969 unsigned addclip
= ctx
.num_output_clips
+ ctx
.num_output_culls
> 4;
5970 shader_info
->gs
.gsvs_vertex_size
= (util_bitcount64(ctx
.output_mask
) + addclip
) * 16;
5971 shader_info
->gs
.max_gsvs_emit_size
= shader_info
->gs
.gsvs_vertex_size
*
5972 nir
->info
.gs
.vertices_out
;
5973 } else if (nir
->stage
== MESA_SHADER_TESS_CTRL
) {
5974 shader_info
->tcs
.outputs_written
= ctx
.tess_outputs_written
;
5975 shader_info
->tcs
.patch_outputs_written
= ctx
.tess_patch_outputs_written
;
5976 } else if (nir
->stage
== MESA_SHADER_VERTEX
&& ctx
.options
->key
.vs
.as_ls
) {
5977 shader_info
->vs
.outputs_written
= ctx
.tess_outputs_written
;
5983 static void ac_diagnostic_handler(LLVMDiagnosticInfoRef di
, void *context
)
5985 unsigned *retval
= (unsigned *)context
;
5986 LLVMDiagnosticSeverity severity
= LLVMGetDiagInfoSeverity(di
);
5987 char *description
= LLVMGetDiagInfoDescription(di
);
5989 if (severity
== LLVMDSError
) {
5991 fprintf(stderr
, "LLVM triggered Diagnostic Handler: %s\n",
5995 LLVMDisposeMessage(description
);
5998 static unsigned ac_llvm_compile(LLVMModuleRef M
,
5999 struct ac_shader_binary
*binary
,
6000 LLVMTargetMachineRef tm
)
6002 unsigned retval
= 0;
6004 LLVMContextRef llvm_ctx
;
6005 LLVMMemoryBufferRef out_buffer
;
6006 unsigned buffer_size
;
6007 const char *buffer_data
;
6010 /* Setup Diagnostic Handler*/
6011 llvm_ctx
= LLVMGetModuleContext(M
);
6013 LLVMContextSetDiagnosticHandler(llvm_ctx
, ac_diagnostic_handler
,
6017 mem_err
= LLVMTargetMachineEmitToMemoryBuffer(tm
, M
, LLVMObjectFile
,
6020 /* Process Errors/Warnings */
6022 fprintf(stderr
, "%s: %s", __FUNCTION__
, err
);
6028 /* Extract Shader Code*/
6029 buffer_size
= LLVMGetBufferSize(out_buffer
);
6030 buffer_data
= LLVMGetBufferStart(out_buffer
);
6032 ac_elf_read(buffer_data
, buffer_size
, binary
);
6035 LLVMDisposeMemoryBuffer(out_buffer
);
6041 static void ac_compile_llvm_module(LLVMTargetMachineRef tm
,
6042 LLVMModuleRef llvm_module
,
6043 struct ac_shader_binary
*binary
,
6044 struct ac_shader_config
*config
,
6045 struct ac_shader_variant_info
*shader_info
,
6046 gl_shader_stage stage
,
6047 bool dump_shader
, bool supports_spill
)
6050 ac_dump_module(llvm_module
);
6052 memset(binary
, 0, sizeof(*binary
));
6053 int v
= ac_llvm_compile(llvm_module
, binary
, tm
);
6055 fprintf(stderr
, "compile failed\n");
6059 fprintf(stderr
, "disasm:\n%s\n", binary
->disasm_string
);
6061 ac_shader_binary_read_config(binary
, config
, 0, supports_spill
);
6063 LLVMContextRef ctx
= LLVMGetModuleContext(llvm_module
);
6064 LLVMDisposeModule(llvm_module
);
6065 LLVMContextDispose(ctx
);
6067 if (stage
== MESA_SHADER_FRAGMENT
) {
6068 shader_info
->num_input_vgprs
= 0;
6069 if (G_0286CC_PERSP_SAMPLE_ENA(config
->spi_ps_input_addr
))
6070 shader_info
->num_input_vgprs
+= 2;
6071 if (G_0286CC_PERSP_CENTER_ENA(config
->spi_ps_input_addr
))
6072 shader_info
->num_input_vgprs
+= 2;
6073 if (G_0286CC_PERSP_CENTROID_ENA(config
->spi_ps_input_addr
))
6074 shader_info
->num_input_vgprs
+= 2;
6075 if (G_0286CC_PERSP_PULL_MODEL_ENA(config
->spi_ps_input_addr
))
6076 shader_info
->num_input_vgprs
+= 3;
6077 if (G_0286CC_LINEAR_SAMPLE_ENA(config
->spi_ps_input_addr
))
6078 shader_info
->num_input_vgprs
+= 2;
6079 if (G_0286CC_LINEAR_CENTER_ENA(config
->spi_ps_input_addr
))
6080 shader_info
->num_input_vgprs
+= 2;
6081 if (G_0286CC_LINEAR_CENTROID_ENA(config
->spi_ps_input_addr
))
6082 shader_info
->num_input_vgprs
+= 2;
6083 if (G_0286CC_LINE_STIPPLE_TEX_ENA(config
->spi_ps_input_addr
))
6084 shader_info
->num_input_vgprs
+= 1;
6085 if (G_0286CC_POS_X_FLOAT_ENA(config
->spi_ps_input_addr
))
6086 shader_info
->num_input_vgprs
+= 1;
6087 if (G_0286CC_POS_Y_FLOAT_ENA(config
->spi_ps_input_addr
))
6088 shader_info
->num_input_vgprs
+= 1;
6089 if (G_0286CC_POS_Z_FLOAT_ENA(config
->spi_ps_input_addr
))
6090 shader_info
->num_input_vgprs
+= 1;
6091 if (G_0286CC_POS_W_FLOAT_ENA(config
->spi_ps_input_addr
))
6092 shader_info
->num_input_vgprs
+= 1;
6093 if (G_0286CC_FRONT_FACE_ENA(config
->spi_ps_input_addr
))
6094 shader_info
->num_input_vgprs
+= 1;
6095 if (G_0286CC_ANCILLARY_ENA(config
->spi_ps_input_addr
))
6096 shader_info
->num_input_vgprs
+= 1;
6097 if (G_0286CC_SAMPLE_COVERAGE_ENA(config
->spi_ps_input_addr
))
6098 shader_info
->num_input_vgprs
+= 1;
6099 if (G_0286CC_POS_FIXED_PT_ENA(config
->spi_ps_input_addr
))
6100 shader_info
->num_input_vgprs
+= 1;
6102 config
->num_vgprs
= MAX2(config
->num_vgprs
, shader_info
->num_input_vgprs
);
6104 /* +3 for scratch wave offset and VCC */
6105 config
->num_sgprs
= MAX2(config
->num_sgprs
,
6106 shader_info
->num_input_sgprs
+ 3);
6109 void ac_compile_nir_shader(LLVMTargetMachineRef tm
,
6110 struct ac_shader_binary
*binary
,
6111 struct ac_shader_config
*config
,
6112 struct ac_shader_variant_info
*shader_info
,
6113 struct nir_shader
*nir
,
6114 const struct ac_nir_compiler_options
*options
,
6118 LLVMModuleRef llvm_module
= ac_translate_nir_to_llvm(tm
, nir
, shader_info
,
6121 ac_compile_llvm_module(tm
, llvm_module
, binary
, config
, shader_info
, nir
->stage
, dump_shader
, options
->supports_spill
);
6122 switch (nir
->stage
) {
6123 case MESA_SHADER_COMPUTE
:
6124 for (int i
= 0; i
< 3; ++i
)
6125 shader_info
->cs
.block_size
[i
] = nir
->info
.cs
.local_size
[i
];
6127 case MESA_SHADER_FRAGMENT
:
6128 shader_info
->fs
.early_fragment_test
= nir
->info
.fs
.early_fragment_tests
;
6130 case MESA_SHADER_GEOMETRY
:
6131 shader_info
->gs
.vertices_in
= nir
->info
.gs
.vertices_in
;
6132 shader_info
->gs
.vertices_out
= nir
->info
.gs
.vertices_out
;
6133 shader_info
->gs
.output_prim
= nir
->info
.gs
.output_primitive
;
6134 shader_info
->gs
.invocations
= nir
->info
.gs
.invocations
;
6136 case MESA_SHADER_TESS_EVAL
:
6137 shader_info
->tes
.primitive_mode
= nir
->info
.tess
.primitive_mode
;
6138 shader_info
->tes
.spacing
= nir
->info
.tess
.spacing
;
6139 shader_info
->tes
.ccw
= nir
->info
.tess
.ccw
;
6140 shader_info
->tes
.point_mode
= nir
->info
.tess
.point_mode
;
6141 shader_info
->tes
.as_es
= options
->key
.tes
.as_es
;
6143 case MESA_SHADER_TESS_CTRL
:
6144 shader_info
->tcs
.tcs_vertices_out
= nir
->info
.tess
.tcs_vertices_out
;
6146 case MESA_SHADER_VERTEX
:
6147 shader_info
->vs
.as_es
= options
->key
.vs
.as_es
;
6148 shader_info
->vs
.as_ls
= options
->key
.vs
.as_ls
;
6149 /* in LS mode we need at least 1, invocation id needs 3, handled elsewhere */
6150 if (options
->key
.vs
.as_ls
)
6151 shader_info
->vs
.vgpr_comp_cnt
= MAX2(1, shader_info
->vs
.vgpr_comp_cnt
);
6159 ac_gs_copy_shader_emit(struct nir_to_llvm_context
*ctx
)
6161 LLVMValueRef args
[9];
6162 args
[0] = ctx
->gsvs_ring
;
6163 args
[1] = LLVMBuildMul(ctx
->builder
, ctx
->vertex_id
, LLVMConstInt(ctx
->i32
, 4, false), "");
6164 args
[3] = ctx
->i32zero
;
6165 args
[4] = ctx
->i32one
; /* OFFEN */
6166 args
[5] = ctx
->i32zero
; /* IDXEN */
6167 args
[6] = ctx
->i32one
; /* GLC */
6168 args
[7] = ctx
->i32one
; /* SLC */
6169 args
[8] = ctx
->i32zero
; /* TFE */
6173 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
6177 if (!(ctx
->output_mask
& (1ull << i
)))
6180 if (i
== VARYING_SLOT_CLIP_DIST0
) {
6181 /* unpack clip and cull from a single set of slots */
6182 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
6187 for (unsigned j
= 0; j
< length
; j
++) {
6189 args
[2] = LLVMConstInt(ctx
->i32
,
6191 ctx
->gs_max_out_vertices
* 16 * 4, false);
6193 value
= ac_build_intrinsic(&ctx
->ac
,
6194 "llvm.SI.buffer.load.dword.i32.i32",
6196 AC_FUNC_ATTR_READONLY
|
6197 AC_FUNC_ATTR_LEGACY
);
6199 LLVMBuildStore(ctx
->builder
,
6200 to_float(ctx
, value
), ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)]);
6204 handle_vs_outputs_post(ctx
, &ctx
->shader_info
->vs
.outinfo
);
6207 void ac_create_gs_copy_shader(LLVMTargetMachineRef tm
,
6208 struct nir_shader
*geom_shader
,
6209 struct ac_shader_binary
*binary
,
6210 struct ac_shader_config
*config
,
6211 struct ac_shader_variant_info
*shader_info
,
6212 const struct ac_nir_compiler_options
*options
,
6215 struct nir_to_llvm_context ctx
= {0};
6216 ctx
.context
= LLVMContextCreate();
6217 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
6218 ctx
.options
= options
;
6219 ctx
.shader_info
= shader_info
;
6221 ac_llvm_context_init(&ctx
.ac
, ctx
.context
);
6222 ctx
.ac
.module
= ctx
.module
;
6224 ctx
.is_gs_copy_shader
= true;
6225 LLVMSetTarget(ctx
.module
, "amdgcn--");
6228 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
6229 ctx
.ac
.builder
= ctx
.builder
;
6230 ctx
.stage
= MESA_SHADER_VERTEX
;
6232 create_function(&ctx
);
6234 ctx
.gs_max_out_vertices
= geom_shader
->info
.gs
.vertices_out
;
6235 ac_setup_rings(&ctx
);
6237 ctx
.num_output_clips
= geom_shader
->info
.clip_distance_array_size
;
6238 ctx
.num_output_culls
= geom_shader
->info
.cull_distance_array_size
;
6240 nir_foreach_variable(variable
, &geom_shader
->outputs
)
6241 handle_shader_output_decl(&ctx
, variable
);
6243 ac_gs_copy_shader_emit(&ctx
);
6245 LLVMBuildRetVoid(ctx
.builder
);
6247 ac_llvm_finalize_module(&ctx
);
6249 ac_compile_llvm_module(tm
, ctx
.module
, binary
, config
, shader_info
,
6251 dump_shader
, options
->supports_spill
);