2 * Copyright © 2016 Bas Nieuwenhuizen
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 #include "ac_nir_to_llvm.h"
25 #include "ac_llvm_build.h"
26 #include "ac_llvm_util.h"
27 #include "ac_binary.h"
30 #include "../vulkan/radv_descriptor_set.h"
31 #include "util/bitscan.h"
32 #include <llvm-c/Transforms/Scalar.h>
33 #include "ac_shader_info.h"
34 #include "ac_exp_param.h"
36 enum radeon_llvm_calling_convention
{
37 RADEON_LLVM_AMDGPU_VS
= 87,
38 RADEON_LLVM_AMDGPU_GS
= 88,
39 RADEON_LLVM_AMDGPU_PS
= 89,
40 RADEON_LLVM_AMDGPU_CS
= 90,
43 #define CONST_ADDR_SPACE 2
44 #define LOCAL_ADDR_SPACE 3
46 #define RADEON_LLVM_MAX_INPUTS (VARYING_SLOT_VAR31 + 1)
47 #define RADEON_LLVM_MAX_OUTPUTS (VARYING_SLOT_VAR31 + 1)
56 struct nir_to_llvm_context
{
57 struct ac_llvm_context ac
;
58 const struct ac_nir_compiler_options
*options
;
59 struct ac_shader_variant_info
*shader_info
;
60 unsigned max_workgroup_size
;
61 LLVMContextRef context
;
63 LLVMBuilderRef builder
;
64 LLVMValueRef main_function
;
66 struct hash_table
*defs
;
67 struct hash_table
*phis
;
69 LLVMValueRef descriptor_sets
[AC_UD_MAX_SETS
];
70 LLVMValueRef ring_offsets
;
71 LLVMValueRef push_constants
;
72 LLVMValueRef num_work_groups
;
73 LLVMValueRef workgroup_ids
;
74 LLVMValueRef local_invocation_ids
;
77 LLVMValueRef vertex_buffers
;
78 LLVMValueRef base_vertex
;
79 LLVMValueRef start_instance
;
80 LLVMValueRef draw_index
;
81 LLVMValueRef vertex_id
;
82 LLVMValueRef rel_auto_id
;
83 LLVMValueRef vs_prim_id
;
84 LLVMValueRef instance_id
;
85 LLVMValueRef ls_out_layout
;
86 LLVMValueRef es2gs_offset
;
88 LLVMValueRef tcs_offchip_layout
;
89 LLVMValueRef tcs_out_offsets
;
90 LLVMValueRef tcs_out_layout
;
91 LLVMValueRef tcs_in_layout
;
93 LLVMValueRef tess_factor_offset
;
94 LLVMValueRef tcs_patch_id
;
95 LLVMValueRef tcs_rel_ids
;
96 LLVMValueRef tes_rel_patch_id
;
97 LLVMValueRef tes_patch_id
;
101 LLVMValueRef gsvs_ring_stride
;
102 LLVMValueRef gsvs_num_entries
;
103 LLVMValueRef gs2vs_offset
;
104 LLVMValueRef gs_wave_id
;
105 LLVMValueRef gs_vtx_offset
[6];
106 LLVMValueRef gs_prim_id
, gs_invocation_id
;
108 LLVMValueRef esgs_ring
;
109 LLVMValueRef gsvs_ring
;
110 LLVMValueRef hs_ring_tess_offchip
;
111 LLVMValueRef hs_ring_tess_factor
;
113 LLVMValueRef prim_mask
;
114 LLVMValueRef sample_pos_offset
;
115 LLVMValueRef persp_sample
, persp_center
, persp_centroid
;
116 LLVMValueRef linear_sample
, linear_center
, linear_centroid
;
117 LLVMValueRef front_face
;
118 LLVMValueRef ancillary
;
119 LLVMValueRef sample_coverage
;
120 LLVMValueRef frag_pos
[4];
122 LLVMBasicBlockRef continue_block
;
123 LLVMBasicBlockRef break_block
;
143 LLVMValueRef i1false
;
144 LLVMValueRef i32zero
;
146 LLVMValueRef f32zero
;
148 LLVMValueRef v4f32empty
;
150 unsigned uniform_md_kind
;
151 LLVMValueRef empty_md
;
152 gl_shader_stage stage
;
155 LLVMValueRef inputs
[RADEON_LLVM_MAX_INPUTS
* 4];
156 LLVMValueRef outputs
[RADEON_LLVM_MAX_OUTPUTS
* 4];
158 LLVMValueRef shared_memory
;
160 uint64_t output_mask
;
162 LLVMValueRef
*locals
;
164 uint8_t num_output_clips
;
165 uint8_t num_output_culls
;
167 bool has_ds_bpermute
;
169 bool is_gs_copy_shader
;
170 LLVMValueRef gs_next_vertex
;
171 unsigned gs_max_out_vertices
;
173 unsigned tes_primitive_mode
;
174 uint64_t tess_outputs_written
;
175 uint64_t tess_patch_outputs_written
;
178 static LLVMValueRef
get_sampler_desc(struct nir_to_llvm_context
*ctx
,
179 const 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
);
257 LLVMTypeRef types
[MAX_ARGS
];
258 LLVMValueRef
*assign
[MAX_ARGS
];
259 unsigned array_params_mask
;
261 uint8_t user_sgpr_count
;
263 uint8_t num_user_sgprs_used
;
264 uint8_t num_sgprs_used
;
265 uint8_t num_vgprs_used
;
269 add_argument(struct arg_info
*info
,
270 LLVMTypeRef type
, LLVMValueRef
*param_ptr
)
272 assert(info
->count
< MAX_ARGS
);
273 info
->assign
[info
->count
] = param_ptr
;
274 info
->types
[info
->count
] = type
;
279 add_sgpr_argument(struct arg_info
*info
,
280 LLVMTypeRef type
, LLVMValueRef
*param_ptr
)
282 add_argument(info
, type
, param_ptr
);
283 info
->num_sgprs_used
+= llvm_get_type_size(type
) / 4;
288 add_user_sgpr_argument(struct arg_info
*info
,
290 LLVMValueRef
*param_ptr
)
292 add_sgpr_argument(info
, type
, param_ptr
);
293 info
->num_user_sgprs_used
+= llvm_get_type_size(type
) / 4;
294 info
->user_sgpr_count
++;
298 add_vgpr_argument(struct arg_info
*info
,
300 LLVMValueRef
*param_ptr
)
302 add_argument(info
, type
, param_ptr
);
303 info
->num_vgprs_used
+= llvm_get_type_size(type
) / 4;
307 add_user_sgpr_array_argument(struct arg_info
*info
,
309 LLVMValueRef
*param_ptr
)
311 info
->array_params_mask
|= (1 << info
->count
);
312 add_user_sgpr_argument(info
, type
, param_ptr
);
315 static void assign_arguments(LLVMValueRef main_function
,
316 struct arg_info
*info
)
319 for (i
= 0; i
< info
->count
; i
++) {
321 *info
->assign
[i
] = LLVMGetParam(main_function
, i
);
326 create_llvm_function(LLVMContextRef ctx
, LLVMModuleRef module
,
327 LLVMBuilderRef builder
, LLVMTypeRef
*return_types
,
328 unsigned num_return_elems
,
329 struct arg_info
*args
,
330 unsigned max_workgroup_size
,
333 LLVMTypeRef main_function_type
, ret_type
;
334 LLVMBasicBlockRef main_function_body
;
336 if (num_return_elems
)
337 ret_type
= LLVMStructTypeInContext(ctx
, return_types
,
338 num_return_elems
, true);
340 ret_type
= LLVMVoidTypeInContext(ctx
);
342 /* Setup the function */
344 LLVMFunctionType(ret_type
, args
->types
, args
->count
, 0);
345 LLVMValueRef main_function
=
346 LLVMAddFunction(module
, "main", main_function_type
);
348 LLVMAppendBasicBlockInContext(ctx
, main_function
, "main_body");
349 LLVMPositionBuilderAtEnd(builder
, main_function_body
);
351 LLVMSetFunctionCallConv(main_function
, RADEON_LLVM_AMDGPU_CS
);
352 for (unsigned i
= 0; i
< args
->sgpr_count
; ++i
) {
353 if (args
->array_params_mask
& (1 << i
)) {
354 LLVMValueRef P
= LLVMGetParam(main_function
, i
);
355 ac_add_function_attr(ctx
, main_function
, i
+ 1, AC_FUNC_ATTR_BYVAL
);
356 ac_add_attr_dereferenceable(P
, UINT64_MAX
);
359 ac_add_function_attr(ctx
, main_function
, i
+ 1, AC_FUNC_ATTR_INREG
);
363 if (max_workgroup_size
) {
364 ac_llvm_add_target_dep_function_attr(main_function
,
365 "amdgpu-max-work-group-size",
369 /* These were copied from some LLVM test. */
370 LLVMAddTargetDependentFunctionAttr(main_function
,
371 "less-precise-fpmad",
373 LLVMAddTargetDependentFunctionAttr(main_function
,
376 LLVMAddTargetDependentFunctionAttr(main_function
,
379 LLVMAddTargetDependentFunctionAttr(main_function
,
383 return main_function
;
386 static LLVMTypeRef
const_array(LLVMTypeRef elem_type
, int num_elements
)
388 return LLVMPointerType(LLVMArrayType(elem_type
, num_elements
),
392 static LLVMValueRef
get_shared_memory_ptr(struct nir_to_llvm_context
*ctx
,
400 offset
= LLVMConstInt(ctx
->i32
, idx
* 16, false);
402 ptr
= ctx
->shared_memory
;
403 ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &offset
, 1, "");
404 addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
405 ptr
= LLVMBuildBitCast(ctx
->builder
, ptr
, LLVMPointerType(type
, addr_space
), "");
409 static LLVMTypeRef
to_integer_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
411 if (t
== ctx
->f16
|| t
== ctx
->i16
)
413 else if (t
== ctx
->f32
|| t
== ctx
->i32
)
415 else if (t
== ctx
->f64
|| t
== ctx
->i64
)
418 unreachable("Unhandled integer size");
421 static LLVMTypeRef
to_integer_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
423 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
424 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
425 return LLVMVectorType(to_integer_type_scalar(ctx
, elem_type
),
426 LLVMGetVectorSize(t
));
428 return to_integer_type_scalar(ctx
, t
);
431 static LLVMValueRef
to_integer(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
433 LLVMTypeRef type
= LLVMTypeOf(v
);
434 return LLVMBuildBitCast(ctx
->builder
, v
, to_integer_type(ctx
, type
), "");
437 static LLVMTypeRef
to_float_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
439 if (t
== ctx
->i16
|| t
== ctx
->f16
)
441 else if (t
== ctx
->i32
|| t
== ctx
->f32
)
443 else if (t
== ctx
->i64
|| t
== ctx
->f64
)
446 unreachable("Unhandled float size");
449 static LLVMTypeRef
to_float_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
451 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
452 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
453 return LLVMVectorType(to_float_type_scalar(ctx
, elem_type
),
454 LLVMGetVectorSize(t
));
456 return to_float_type_scalar(ctx
, t
);
459 static LLVMValueRef
to_float(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
461 LLVMTypeRef type
= LLVMTypeOf(v
);
462 return LLVMBuildBitCast(ctx
->builder
, v
, to_float_type(ctx
, type
), "");
465 static int get_elem_bits(struct ac_llvm_context
*ctx
, LLVMTypeRef type
)
467 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
468 type
= LLVMGetElementType(type
);
470 if (LLVMGetTypeKind(type
) == LLVMIntegerTypeKind
)
471 return LLVMGetIntTypeWidth(type
);
473 if (type
== ctx
->f16
)
475 if (type
== ctx
->f32
)
477 if (type
== ctx
->f64
)
480 unreachable("Unhandled type kind in get_elem_bits");
483 static LLVMValueRef
unpack_param(struct nir_to_llvm_context
*ctx
,
484 LLVMValueRef param
, unsigned rshift
,
487 LLVMValueRef value
= param
;
489 value
= LLVMBuildLShr(ctx
->builder
, value
,
490 LLVMConstInt(ctx
->i32
, rshift
, false), "");
492 if (rshift
+ bitwidth
< 32) {
493 unsigned mask
= (1 << bitwidth
) - 1;
494 value
= LLVMBuildAnd(ctx
->builder
, value
,
495 LLVMConstInt(ctx
->i32
, mask
, false), "");
500 static LLVMValueRef
get_rel_patch_id(struct nir_to_llvm_context
*ctx
)
502 switch (ctx
->stage
) {
503 case MESA_SHADER_TESS_CTRL
:
504 return unpack_param(ctx
, ctx
->tcs_rel_ids
, 0, 8);
505 case MESA_SHADER_TESS_EVAL
:
506 return ctx
->tes_rel_patch_id
;
509 unreachable("Illegal stage");
513 /* Tessellation shaders pass outputs to the next shader using LDS.
515 * LS outputs = TCS inputs
516 * TCS outputs = TES inputs
519 * - TCS inputs for patch 0
520 * - TCS inputs for patch 1
521 * - TCS inputs for patch 2 = get_tcs_in_current_patch_offset (if RelPatchID==2)
523 * - TCS outputs for patch 0 = get_tcs_out_patch0_offset
524 * - Per-patch TCS outputs for patch 0 = get_tcs_out_patch0_patch_data_offset
525 * - TCS outputs for patch 1
526 * - Per-patch TCS outputs for patch 1
527 * - TCS outputs for patch 2 = get_tcs_out_current_patch_offset (if RelPatchID==2)
528 * - Per-patch TCS outputs for patch 2 = get_tcs_out_current_patch_data_offset (if RelPatchID==2)
531 * All three shaders VS(LS), TCS, TES share the same LDS space.
534 get_tcs_in_patch_stride(struct nir_to_llvm_context
*ctx
)
536 if (ctx
->stage
== MESA_SHADER_VERTEX
)
537 return unpack_param(ctx
, ctx
->ls_out_layout
, 0, 13);
538 else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
539 return unpack_param(ctx
, ctx
->tcs_in_layout
, 0, 13);
547 get_tcs_out_patch_stride(struct nir_to_llvm_context
*ctx
)
549 return unpack_param(ctx
, ctx
->tcs_out_layout
, 0, 13);
553 get_tcs_out_patch0_offset(struct nir_to_llvm_context
*ctx
)
555 return LLVMBuildMul(ctx
->builder
,
556 unpack_param(ctx
, ctx
->tcs_out_offsets
, 0, 16),
557 LLVMConstInt(ctx
->i32
, 4, false), "");
561 get_tcs_out_patch0_patch_data_offset(struct nir_to_llvm_context
*ctx
)
563 return LLVMBuildMul(ctx
->builder
,
564 unpack_param(ctx
, ctx
->tcs_out_offsets
, 16, 16),
565 LLVMConstInt(ctx
->i32
, 4, false), "");
569 get_tcs_in_current_patch_offset(struct nir_to_llvm_context
*ctx
)
571 LLVMValueRef patch_stride
= get_tcs_in_patch_stride(ctx
);
572 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
574 return LLVMBuildMul(ctx
->builder
, patch_stride
, rel_patch_id
, "");
578 get_tcs_out_current_patch_offset(struct nir_to_llvm_context
*ctx
)
580 LLVMValueRef patch0_offset
= get_tcs_out_patch0_offset(ctx
);
581 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
582 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
584 return LLVMBuildAdd(ctx
->builder
, patch0_offset
,
585 LLVMBuildMul(ctx
->builder
, patch_stride
,
591 get_tcs_out_current_patch_data_offset(struct nir_to_llvm_context
*ctx
)
593 LLVMValueRef patch0_patch_data_offset
=
594 get_tcs_out_patch0_patch_data_offset(ctx
);
595 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
596 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
598 return LLVMBuildAdd(ctx
->builder
, patch0_patch_data_offset
,
599 LLVMBuildMul(ctx
->builder
, patch_stride
,
604 static void set_userdata_location(struct ac_userdata_info
*ud_info
, uint8_t *sgpr_idx
, uint8_t num_sgprs
)
606 ud_info
->sgpr_idx
= *sgpr_idx
;
607 ud_info
->num_sgprs
= num_sgprs
;
608 ud_info
->indirect
= false;
609 ud_info
->indirect_offset
= 0;
610 *sgpr_idx
+= num_sgprs
;
613 static void set_userdata_location_shader(struct nir_to_llvm_context
*ctx
,
614 int idx
, uint8_t *sgpr_idx
, uint8_t num_sgprs
)
616 set_userdata_location(&ctx
->shader_info
->user_sgprs_locs
.shader_data
[idx
], sgpr_idx
, num_sgprs
);
620 static void set_userdata_location_indirect(struct ac_userdata_info
*ud_info
, uint8_t sgpr_idx
, uint8_t num_sgprs
,
621 uint32_t indirect_offset
)
623 ud_info
->sgpr_idx
= sgpr_idx
;
624 ud_info
->num_sgprs
= num_sgprs
;
625 ud_info
->indirect
= true;
626 ud_info
->indirect_offset
= indirect_offset
;
629 static void declare_tess_lds(struct nir_to_llvm_context
*ctx
)
631 unsigned lds_size
= ctx
->options
->chip_class
>= CIK
? 65536 : 32768;
632 ctx
->lds
= LLVMBuildIntToPtr(ctx
->builder
, ctx
->i32zero
,
633 LLVMPointerType(LLVMArrayType(ctx
->i32
, lds_size
/ 4), LOCAL_ADDR_SPACE
),
637 struct user_sgpr_info
{
638 bool need_ring_offsets
;
640 bool indirect_all_descriptor_sets
;
643 static void allocate_user_sgprs(struct nir_to_llvm_context
*ctx
,
644 struct user_sgpr_info
*user_sgpr_info
)
646 memset(user_sgpr_info
, 0, sizeof(struct user_sgpr_info
));
648 /* until we sort out scratch/global buffers always assign ring offsets for gs/vs/es */
649 if (ctx
->stage
== MESA_SHADER_GEOMETRY
||
650 ctx
->stage
== MESA_SHADER_VERTEX
||
651 ctx
->stage
== MESA_SHADER_TESS_CTRL
||
652 ctx
->stage
== MESA_SHADER_TESS_EVAL
||
653 ctx
->is_gs_copy_shader
)
654 user_sgpr_info
->need_ring_offsets
= true;
656 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&&
657 ctx
->shader_info
->info
.ps
.needs_sample_positions
)
658 user_sgpr_info
->need_ring_offsets
= true;
660 /* 2 user sgprs will nearly always be allocated for scratch/rings */
661 if (ctx
->options
->supports_spill
|| user_sgpr_info
->need_ring_offsets
) {
662 user_sgpr_info
->sgpr_count
+= 2;
665 switch (ctx
->stage
) {
666 case MESA_SHADER_COMPUTE
:
667 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.cs
.grid_components_used
;
669 case MESA_SHADER_FRAGMENT
:
670 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.ps
.needs_sample_positions
;
672 case MESA_SHADER_VERTEX
:
673 if (!ctx
->is_gs_copy_shader
) {
674 user_sgpr_info
->sgpr_count
+= ctx
->shader_info
->info
.vs
.has_vertex_buffers
? 2 : 0;
675 if (ctx
->shader_info
->info
.vs
.needs_draw_id
) {
676 user_sgpr_info
->sgpr_count
+= 3;
678 user_sgpr_info
->sgpr_count
+= 2;
681 if (ctx
->options
->key
.vs
.as_ls
)
682 user_sgpr_info
->sgpr_count
++;
684 case MESA_SHADER_TESS_CTRL
:
685 user_sgpr_info
->sgpr_count
+= 4;
687 case MESA_SHADER_TESS_EVAL
:
688 user_sgpr_info
->sgpr_count
+= 1;
690 case MESA_SHADER_GEOMETRY
:
691 user_sgpr_info
->sgpr_count
+= 2;
697 if (ctx
->shader_info
->info
.needs_push_constants
)
698 user_sgpr_info
->sgpr_count
+= 2;
700 uint32_t remaining_sgprs
= 16 - user_sgpr_info
->sgpr_count
;
701 if (remaining_sgprs
/ 2 < util_bitcount(ctx
->shader_info
->info
.desc_set_used_mask
)) {
702 user_sgpr_info
->sgpr_count
+= 2;
703 user_sgpr_info
->indirect_all_descriptor_sets
= true;
705 user_sgpr_info
->sgpr_count
+= util_bitcount(ctx
->shader_info
->info
.desc_set_used_mask
) * 2;
709 static void create_function(struct nir_to_llvm_context
*ctx
)
711 unsigned num_sets
= ctx
->options
->layout
? ctx
->options
->layout
->num_sets
: 0;
712 uint8_t user_sgpr_idx
;
713 struct user_sgpr_info user_sgpr_info
;
714 struct arg_info args
= {};
715 LLVMValueRef desc_sets
;
717 allocate_user_sgprs(ctx
, &user_sgpr_info
);
718 if (user_sgpr_info
.need_ring_offsets
&& !ctx
->options
->supports_spill
) {
719 add_user_sgpr_argument(&args
, const_array(ctx
->v16i8
, 16), &ctx
->ring_offsets
); /* address of rings */
722 /* 1 for each descriptor set */
723 if (!user_sgpr_info
.indirect_all_descriptor_sets
) {
724 for (unsigned i
= 0; i
< num_sets
; ++i
) {
725 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& (1 << ctx
->stage
)) {
726 add_user_sgpr_array_argument(&args
, const_array(ctx
->i8
, 1024 * 1024), &ctx
->descriptor_sets
[i
]);
730 add_user_sgpr_array_argument(&args
, const_array(const_array(ctx
->i8
, 1024 * 1024), 32), &desc_sets
);
732 if (ctx
->shader_info
->info
.needs_push_constants
) {
733 /* 1 for push constants and dynamic descriptors */
734 add_user_sgpr_array_argument(&args
, const_array(ctx
->i8
, 1024 * 1024), &ctx
->push_constants
);
737 switch (ctx
->stage
) {
738 case MESA_SHADER_COMPUTE
:
739 if (ctx
->shader_info
->info
.cs
.grid_components_used
)
740 add_user_sgpr_argument(&args
, LLVMVectorType(ctx
->i32
, ctx
->shader_info
->info
.cs
.grid_components_used
), &ctx
->num_work_groups
); /* grid size */
741 add_sgpr_argument(&args
, LLVMVectorType(ctx
->i32
, 3), &ctx
->workgroup_ids
);
742 add_sgpr_argument(&args
, ctx
->i32
, &ctx
->tg_size
);
743 add_vgpr_argument(&args
, LLVMVectorType(ctx
->i32
, 3), &ctx
->local_invocation_ids
);
745 case MESA_SHADER_VERTEX
:
746 if (!ctx
->is_gs_copy_shader
) {
747 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
)
748 add_user_sgpr_argument(&args
, const_array(ctx
->v16i8
, 16), &ctx
->vertex_buffers
); /* vertex buffers */
749 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->base_vertex
); // base vertex
750 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->start_instance
);// start instance
751 if (ctx
->shader_info
->info
.vs
.needs_draw_id
)
752 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->draw_index
); // draw id
754 if (ctx
->options
->key
.vs
.as_es
)
755 add_sgpr_argument(&args
, ctx
->i32
, &ctx
->es2gs_offset
); // es2gs offset
756 else if (ctx
->options
->key
.vs
.as_ls
)
757 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->ls_out_layout
); // ls out layout
758 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->vertex_id
); // vertex id
759 if (!ctx
->is_gs_copy_shader
) {
760 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->rel_auto_id
); // rel auto id
761 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->vs_prim_id
); // vs prim id
762 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->instance_id
); // instance id
765 case MESA_SHADER_TESS_CTRL
:
766 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->tcs_offchip_layout
); // tcs offchip layout
767 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->tcs_out_offsets
); // tcs out offsets
768 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->tcs_out_layout
); // tcs out layout
769 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->tcs_in_layout
); // tcs in layout
770 add_sgpr_argument(&args
, ctx
->i32
, &ctx
->oc_lds
); // param oc lds
771 add_sgpr_argument(&args
, ctx
->i32
, &ctx
->tess_factor_offset
); // tess factor offset
772 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->tcs_patch_id
); // patch id
773 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->tcs_rel_ids
); // rel ids;
775 case MESA_SHADER_TESS_EVAL
:
776 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->tcs_offchip_layout
); // tcs offchip layout
777 if (ctx
->options
->key
.tes
.as_es
) {
778 add_sgpr_argument(&args
, ctx
->i32
, &ctx
->oc_lds
); // OC LDS
779 add_sgpr_argument(&args
, ctx
->i32
, NULL
); //
780 add_sgpr_argument(&args
, ctx
->i32
, &ctx
->es2gs_offset
); // es2gs offset
782 add_sgpr_argument(&args
, ctx
->i32
, NULL
); //
783 add_sgpr_argument(&args
, ctx
->i32
, &ctx
->oc_lds
); // OC LDS
785 add_vgpr_argument(&args
, ctx
->f32
, &ctx
->tes_u
); // tes_u
786 add_vgpr_argument(&args
, ctx
->f32
, &ctx
->tes_v
); // tes_v
787 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->tes_rel_patch_id
); // tes rel patch id
788 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->tes_patch_id
); // tes patch id
790 case MESA_SHADER_GEOMETRY
:
791 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->gsvs_ring_stride
); // gsvs stride
792 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->gsvs_num_entries
); // gsvs num entires
793 add_sgpr_argument(&args
, ctx
->i32
, &ctx
->gs2vs_offset
); // gs2vs offset
794 add_sgpr_argument(&args
, ctx
->i32
, &ctx
->gs_wave_id
); // wave id
795 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->gs_vtx_offset
[0]); // vtx0
796 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->gs_vtx_offset
[1]); // vtx1
797 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->gs_prim_id
); // prim id
798 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->gs_vtx_offset
[2]);
799 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->gs_vtx_offset
[3]);
800 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->gs_vtx_offset
[4]);
801 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->gs_vtx_offset
[5]);
802 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->gs_invocation_id
);
804 case MESA_SHADER_FRAGMENT
:
805 if (ctx
->shader_info
->info
.ps
.needs_sample_positions
)
806 add_user_sgpr_argument(&args
, ctx
->i32
, &ctx
->sample_pos_offset
); /* sample position offset */
807 add_sgpr_argument(&args
, ctx
->i32
, &ctx
->prim_mask
); /* prim mask */
808 add_vgpr_argument(&args
, ctx
->v2i32
, &ctx
->persp_sample
); /* persp sample */
809 add_vgpr_argument(&args
, ctx
->v2i32
, &ctx
->persp_center
); /* persp center */
810 add_vgpr_argument(&args
, ctx
->v2i32
, &ctx
->persp_centroid
); /* persp centroid */
811 add_vgpr_argument(&args
, ctx
->v3i32
, NULL
); /* persp pull model */
812 add_vgpr_argument(&args
, ctx
->v2i32
, &ctx
->linear_sample
); /* linear sample */
813 add_vgpr_argument(&args
, ctx
->v2i32
, &ctx
->linear_center
); /* linear center */
814 add_vgpr_argument(&args
, ctx
->v2i32
, &ctx
->linear_centroid
); /* linear centroid */
815 add_vgpr_argument(&args
, ctx
->f32
, NULL
); /* line stipple tex */
816 add_vgpr_argument(&args
, ctx
->f32
, &ctx
->frag_pos
[0]); /* pos x float */
817 add_vgpr_argument(&args
, ctx
->f32
, &ctx
->frag_pos
[1]); /* pos y float */
818 add_vgpr_argument(&args
, ctx
->f32
, &ctx
->frag_pos
[2]); /* pos z float */
819 add_vgpr_argument(&args
, ctx
->f32
, &ctx
->frag_pos
[3]); /* pos w float */
820 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->front_face
); /* front face */
821 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->ancillary
); /* ancillary */
822 add_vgpr_argument(&args
, ctx
->i32
, &ctx
->sample_coverage
); /* sample coverage */
823 add_vgpr_argument(&args
, ctx
->i32
, NULL
); /* fixed pt */
826 unreachable("Shader stage not implemented");
829 ctx
->main_function
= create_llvm_function(
830 ctx
->context
, ctx
->module
, ctx
->builder
, NULL
, 0, &args
,
831 ctx
->max_workgroup_size
,
832 ctx
->options
->unsafe_math
);
833 set_llvm_calling_convention(ctx
->main_function
, ctx
->stage
);
836 ctx
->shader_info
->num_input_vgprs
= 0;
837 ctx
->shader_info
->num_input_sgprs
= ctx
->shader_info
->num_user_sgprs
=
838 ctx
->options
->supports_spill
? 2 : 0;
840 ctx
->shader_info
->num_user_sgprs
+= args
.num_user_sgprs_used
;
841 ctx
->shader_info
->num_input_sgprs
+= args
.num_sgprs_used
;
843 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
844 ctx
->shader_info
->num_input_vgprs
= args
.num_vgprs_used
;
846 assign_arguments(ctx
->main_function
, &args
);
850 if (ctx
->options
->supports_spill
|| user_sgpr_info
.need_ring_offsets
) {
851 set_userdata_location_shader(ctx
, AC_UD_SCRATCH_RING_OFFSETS
, &user_sgpr_idx
, 2);
852 if (ctx
->options
->supports_spill
) {
853 ctx
->ring_offsets
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.implicit.buffer.ptr",
854 LLVMPointerType(ctx
->i8
, CONST_ADDR_SPACE
),
855 NULL
, 0, AC_FUNC_ATTR_READNONE
);
856 ctx
->ring_offsets
= LLVMBuildBitCast(ctx
->builder
, ctx
->ring_offsets
,
857 const_array(ctx
->v16i8
, 16), "");
861 if (!user_sgpr_info
.indirect_all_descriptor_sets
) {
862 for (unsigned i
= 0; i
< num_sets
; ++i
) {
863 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& (1 << ctx
->stage
)) {
864 set_userdata_location(&ctx
->shader_info
->user_sgprs_locs
.descriptor_sets
[i
], &user_sgpr_idx
, 2);
866 ctx
->descriptor_sets
[i
] = NULL
;
869 uint32_t desc_sgpr_idx
= user_sgpr_idx
;
870 set_userdata_location_shader(ctx
, AC_UD_INDIRECT_DESCRIPTOR_SETS
, &user_sgpr_idx
, 2);
872 for (unsigned i
= 0; i
< num_sets
; ++i
) {
873 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& (1 << ctx
->stage
)) {
874 set_userdata_location_indirect(&ctx
->shader_info
->user_sgprs_locs
.descriptor_sets
[i
], desc_sgpr_idx
, 2, i
* 8);
875 ctx
->descriptor_sets
[i
] = ac_build_indexed_load_const(&ctx
->ac
, desc_sets
, LLVMConstInt(ctx
->i32
, i
, false));
878 ctx
->descriptor_sets
[i
] = NULL
;
880 ctx
->shader_info
->need_indirect_descriptor_sets
= true;
883 if (ctx
->shader_info
->info
.needs_push_constants
) {
884 set_userdata_location_shader(ctx
, AC_UD_PUSH_CONSTANTS
, &user_sgpr_idx
, 2);
887 switch (ctx
->stage
) {
888 case MESA_SHADER_COMPUTE
:
889 if (ctx
->shader_info
->info
.cs
.grid_components_used
) {
890 set_userdata_location_shader(ctx
, AC_UD_CS_GRID_SIZE
, &user_sgpr_idx
, ctx
->shader_info
->info
.cs
.grid_components_used
);
893 case MESA_SHADER_VERTEX
:
894 if (!ctx
->is_gs_copy_shader
) {
895 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
) {
896 set_userdata_location_shader(ctx
, AC_UD_VS_VERTEX_BUFFERS
, &user_sgpr_idx
, 2);
899 if (ctx
->shader_info
->info
.vs
.needs_draw_id
)
902 set_userdata_location_shader(ctx
, AC_UD_VS_BASE_VERTEX_START_INSTANCE
, &user_sgpr_idx
, vs_num
);
904 if (ctx
->options
->key
.vs
.as_ls
) {
905 set_userdata_location_shader(ctx
, AC_UD_VS_LS_TCS_IN_LAYOUT
, &user_sgpr_idx
, 1);
907 if (ctx
->options
->key
.vs
.as_ls
)
908 declare_tess_lds(ctx
);
910 case MESA_SHADER_TESS_CTRL
:
911 set_userdata_location_shader(ctx
, AC_UD_TCS_OFFCHIP_LAYOUT
, &user_sgpr_idx
, 4);
912 declare_tess_lds(ctx
);
914 case MESA_SHADER_TESS_EVAL
:
915 set_userdata_location_shader(ctx
, AC_UD_TES_OFFCHIP_LAYOUT
, &user_sgpr_idx
, 1);
917 case MESA_SHADER_GEOMETRY
:
918 set_userdata_location_shader(ctx
, AC_UD_GS_VS_RING_STRIDE_ENTRIES
, &user_sgpr_idx
, 2);
920 case MESA_SHADER_FRAGMENT
:
921 if (ctx
->shader_info
->info
.ps
.needs_sample_positions
) {
922 set_userdata_location_shader(ctx
, AC_UD_PS_SAMPLE_POS_OFFSET
, &user_sgpr_idx
, 1);
926 unreachable("Shader stage not implemented");
930 static void setup_types(struct nir_to_llvm_context
*ctx
)
932 LLVMValueRef args
[4];
934 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->context
);
935 ctx
->i1
= LLVMIntTypeInContext(ctx
->context
, 1);
936 ctx
->i8
= LLVMIntTypeInContext(ctx
->context
, 8);
937 ctx
->i16
= LLVMIntTypeInContext(ctx
->context
, 16);
938 ctx
->i32
= LLVMIntTypeInContext(ctx
->context
, 32);
939 ctx
->i64
= LLVMIntTypeInContext(ctx
->context
, 64);
940 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
941 ctx
->v3i32
= LLVMVectorType(ctx
->i32
, 3);
942 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
943 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
944 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
945 ctx
->f16
= LLVMHalfTypeInContext(ctx
->context
);
946 ctx
->f64
= LLVMDoubleTypeInContext(ctx
->context
);
947 ctx
->v2f32
= LLVMVectorType(ctx
->f32
, 2);
948 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
949 ctx
->v16i8
= LLVMVectorType(ctx
->i8
, 16);
951 ctx
->i1false
= LLVMConstInt(ctx
->i1
, 0, false);
952 ctx
->i1true
= LLVMConstInt(ctx
->i1
, 1, false);
953 ctx
->i32zero
= LLVMConstInt(ctx
->i32
, 0, false);
954 ctx
->i32one
= LLVMConstInt(ctx
->i32
, 1, false);
955 ctx
->f32zero
= LLVMConstReal(ctx
->f32
, 0.0);
956 ctx
->f32one
= LLVMConstReal(ctx
->f32
, 1.0);
958 args
[0] = ctx
->f32zero
;
959 args
[1] = ctx
->f32zero
;
960 args
[2] = ctx
->f32zero
;
961 args
[3] = ctx
->f32one
;
962 ctx
->v4f32empty
= LLVMConstVector(args
, 4);
964 ctx
->uniform_md_kind
=
965 LLVMGetMDKindIDInContext(ctx
->context
, "amdgpu.uniform", 14);
966 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
968 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
971 static int get_llvm_num_components(LLVMValueRef value
)
973 LLVMTypeRef type
= LLVMTypeOf(value
);
974 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
975 ? LLVMGetVectorSize(type
)
977 return num_components
;
980 static LLVMValueRef
llvm_extract_elem(struct nir_to_llvm_context
*ctx
,
984 int count
= get_llvm_num_components(value
);
986 assert(index
< count
);
990 return LLVMBuildExtractElement(ctx
->builder
, value
,
991 LLVMConstInt(ctx
->i32
, index
, false), "");
994 static LLVMValueRef
trim_vector(struct nir_to_llvm_context
*ctx
,
995 LLVMValueRef value
, unsigned count
)
997 unsigned num_components
= get_llvm_num_components(value
);
998 if (count
== num_components
)
1001 LLVMValueRef masks
[] = {
1002 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
1003 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false)};
1006 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
1009 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
1010 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
1014 build_store_values_extended(struct nir_to_llvm_context
*ctx
,
1015 LLVMValueRef
*values
,
1016 unsigned value_count
,
1017 unsigned value_stride
,
1020 LLVMBuilderRef builder
= ctx
->builder
;
1023 if (value_count
== 1) {
1024 LLVMBuildStore(builder
, vec
, values
[0]);
1028 for (i
= 0; i
< value_count
; i
++) {
1029 LLVMValueRef ptr
= values
[i
* value_stride
];
1030 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
1031 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
1032 LLVMBuildStore(builder
, value
, ptr
);
1036 static LLVMTypeRef
get_def_type(struct nir_to_llvm_context
*ctx
,
1037 const nir_ssa_def
*def
)
1039 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->context
, def
->bit_size
);
1040 if (def
->num_components
> 1) {
1041 type
= LLVMVectorType(type
, def
->num_components
);
1046 static LLVMValueRef
get_src(struct nir_to_llvm_context
*ctx
, nir_src src
)
1049 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->defs
, src
.ssa
);
1050 return (LLVMValueRef
)entry
->data
;
1054 static LLVMBasicBlockRef
get_block(struct nir_to_llvm_context
*ctx
,
1055 const struct nir_block
*b
)
1057 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->defs
, b
);
1058 return (LLVMBasicBlockRef
)entry
->data
;
1061 static LLVMValueRef
get_alu_src(struct nir_to_llvm_context
*ctx
,
1063 unsigned num_components
)
1065 LLVMValueRef value
= get_src(ctx
, src
.src
);
1066 bool need_swizzle
= false;
1069 LLVMTypeRef type
= LLVMTypeOf(value
);
1070 unsigned src_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
1071 ? LLVMGetVectorSize(type
)
1074 for (unsigned i
= 0; i
< num_components
; ++i
) {
1075 assert(src
.swizzle
[i
] < src_components
);
1076 if (src
.swizzle
[i
] != i
)
1077 need_swizzle
= true;
1080 if (need_swizzle
|| num_components
!= src_components
) {
1081 LLVMValueRef masks
[] = {
1082 LLVMConstInt(ctx
->i32
, src
.swizzle
[0], false),
1083 LLVMConstInt(ctx
->i32
, src
.swizzle
[1], false),
1084 LLVMConstInt(ctx
->i32
, src
.swizzle
[2], false),
1085 LLVMConstInt(ctx
->i32
, src
.swizzle
[3], false)};
1087 if (src_components
> 1 && num_components
== 1) {
1088 value
= LLVMBuildExtractElement(ctx
->builder
, value
,
1090 } else if (src_components
== 1 && num_components
> 1) {
1091 LLVMValueRef values
[] = {value
, value
, value
, value
};
1092 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
1094 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
1095 value
= LLVMBuildShuffleVector(ctx
->builder
, value
, value
,
1099 assert(!src
.negate
);
1104 static LLVMValueRef
emit_int_cmp(struct nir_to_llvm_context
*ctx
,
1105 LLVMIntPredicate pred
, LLVMValueRef src0
,
1108 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
1109 return LLVMBuildSelect(ctx
->builder
, result
,
1110 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
1111 LLVMConstInt(ctx
->i32
, 0, false), "");
1114 static LLVMValueRef
emit_float_cmp(struct nir_to_llvm_context
*ctx
,
1115 LLVMRealPredicate pred
, LLVMValueRef src0
,
1118 LLVMValueRef result
;
1119 src0
= to_float(&ctx
->ac
, src0
);
1120 src1
= to_float(&ctx
->ac
, src1
);
1121 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
1122 return LLVMBuildSelect(ctx
->builder
, result
,
1123 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
1124 LLVMConstInt(ctx
->i32
, 0, false), "");
1127 static LLVMValueRef
emit_intrin_1f_param(struct nir_to_llvm_context
*ctx
,
1129 LLVMTypeRef result_type
,
1133 LLVMValueRef params
[] = {
1134 to_float(&ctx
->ac
, src0
),
1137 sprintf(name
, "%s.f%d", intrin
, get_elem_bits(&ctx
->ac
, result_type
));
1138 return ac_build_intrinsic(&ctx
->ac
, name
, result_type
, params
, 1, AC_FUNC_ATTR_READNONE
);
1141 static LLVMValueRef
emit_intrin_2f_param(struct nir_to_llvm_context
*ctx
,
1143 LLVMTypeRef result_type
,
1144 LLVMValueRef src0
, LLVMValueRef src1
)
1147 LLVMValueRef params
[] = {
1148 to_float(&ctx
->ac
, src0
),
1149 to_float(&ctx
->ac
, src1
),
1152 sprintf(name
, "%s.f%d", intrin
, get_elem_bits(&ctx
->ac
, result_type
));
1153 return ac_build_intrinsic(&ctx
->ac
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
1156 static LLVMValueRef
emit_intrin_3f_param(struct nir_to_llvm_context
*ctx
,
1158 LLVMTypeRef result_type
,
1159 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
1162 LLVMValueRef params
[] = {
1163 to_float(&ctx
->ac
, src0
),
1164 to_float(&ctx
->ac
, src1
),
1165 to_float(&ctx
->ac
, src2
),
1168 sprintf(name
, "%s.f%d", intrin
, get_elem_bits(&ctx
->ac
, result_type
));
1169 return ac_build_intrinsic(&ctx
->ac
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
1172 static LLVMValueRef
emit_bcsel(struct nir_to_llvm_context
*ctx
,
1173 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
1175 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
1177 return LLVMBuildSelect(ctx
->builder
, v
, src1
, src2
, "");
1180 static LLVMValueRef
emit_find_lsb(struct nir_to_llvm_context
*ctx
,
1183 LLVMValueRef params
[2] = {
1186 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
1187 * add special code to check for x=0. The reason is that
1188 * the LLVM behavior for x=0 is different from what we
1191 * The hardware already implements the correct behavior.
1193 LLVMConstInt(ctx
->i1
, 1, false),
1195 return ac_build_intrinsic(&ctx
->ac
, "llvm.cttz.i32", ctx
->i32
, params
, 2, AC_FUNC_ATTR_READNONE
);
1198 static LLVMValueRef
emit_ifind_msb(struct nir_to_llvm_context
*ctx
,
1201 return ac_build_imsb(&ctx
->ac
, src0
, ctx
->i32
);
1204 static LLVMValueRef
emit_ufind_msb(struct nir_to_llvm_context
*ctx
,
1207 return ac_build_umsb(&ctx
->ac
, src0
, ctx
->i32
);
1210 static LLVMValueRef
emit_minmax_int(struct nir_to_llvm_context
*ctx
,
1211 LLVMIntPredicate pred
,
1212 LLVMValueRef src0
, LLVMValueRef src1
)
1214 return LLVMBuildSelect(ctx
->builder
,
1215 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
1220 static LLVMValueRef
emit_iabs(struct nir_to_llvm_context
*ctx
,
1223 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
1224 LLVMBuildNeg(ctx
->builder
, src0
, ""));
1227 static LLVMValueRef
emit_fsign(struct nir_to_llvm_context
*ctx
,
1230 LLVMValueRef cmp
, val
;
1232 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, ctx
->f32zero
, "");
1233 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->f32one
, src0
, "");
1234 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, ctx
->f32zero
, "");
1235 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(ctx
->f32
, -1.0), "");
1239 static LLVMValueRef
emit_isign(struct nir_to_llvm_context
*ctx
,
1242 LLVMValueRef cmp
, val
;
1244 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, ctx
->i32zero
, "");
1245 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->i32one
, src0
, "");
1246 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, ctx
->i32zero
, "");
1247 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(ctx
->i32
, -1, true), "");
1251 static LLVMValueRef
emit_ffract(struct nir_to_llvm_context
*ctx
,
1254 const char *intr
= "llvm.floor.f32";
1255 LLVMValueRef fsrc0
= to_float(&ctx
->ac
, src0
);
1256 LLVMValueRef params
[] = {
1259 LLVMValueRef floor
= ac_build_intrinsic(&ctx
->ac
, intr
,
1260 ctx
->f32
, params
, 1,
1261 AC_FUNC_ATTR_READNONE
);
1262 return LLVMBuildFSub(ctx
->builder
, fsrc0
, floor
, "");
1265 static LLVMValueRef
emit_uint_carry(struct nir_to_llvm_context
*ctx
,
1267 LLVMValueRef src0
, LLVMValueRef src1
)
1269 LLVMTypeRef ret_type
;
1270 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
1272 LLVMValueRef params
[] = { src0
, src1
};
1273 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
1276 res
= ac_build_intrinsic(&ctx
->ac
, intrin
, ret_type
,
1277 params
, 2, AC_FUNC_ATTR_READNONE
);
1279 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
1280 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
1284 static LLVMValueRef
emit_b2f(struct nir_to_llvm_context
*ctx
,
1287 return LLVMBuildAnd(ctx
->builder
, src0
, LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""), "");
1290 static LLVMValueRef
emit_f2b(struct ac_llvm_context
*ctx
,
1293 src0
= to_float(ctx
, src0
);
1294 return LLVMBuildSExt(ctx
->builder
,
1295 LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
, src0
, ctx
->f32_0
, ""),
1299 static LLVMValueRef
emit_b2i(struct ac_llvm_context
*ctx
,
1302 return LLVMBuildAnd(ctx
->builder
, src0
, ctx
->i32_1
, "");
1305 static LLVMValueRef
emit_i2b(struct ac_llvm_context
*ctx
,
1308 return LLVMBuildSExt(ctx
->builder
,
1309 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
, ctx
->i32_0
, ""),
1313 static LLVMValueRef
emit_f2f16(struct nir_to_llvm_context
*ctx
,
1316 LLVMValueRef result
;
1319 src0
= to_float(&ctx
->ac
, src0
);
1320 result
= LLVMBuildFPTrunc(ctx
->builder
, src0
, ctx
->f16
, "");
1322 /* TODO SI/CIK options here */
1323 if (ctx
->options
->chip_class
>= VI
) {
1324 LLVMValueRef args
[2];
1325 /* Check if the result is a denormal - and flush to 0 if so. */
1327 args
[1] = LLVMConstInt(ctx
->i32
, N_SUBNORMAL
| P_SUBNORMAL
, false);
1328 cond
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.class.f16", ctx
->i1
, args
, 2, AC_FUNC_ATTR_READNONE
);
1331 /* need to convert back up to f32 */
1332 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
1334 if (ctx
->options
->chip_class
>= VI
)
1335 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32zero
, result
, "");
1340 static LLVMValueRef
emit_umul_high(struct nir_to_llvm_context
*ctx
,
1341 LLVMValueRef src0
, LLVMValueRef src1
)
1343 LLVMValueRef dst64
, result
;
1344 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
1345 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
1347 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1348 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1349 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1353 static LLVMValueRef
emit_imul_high(struct nir_to_llvm_context
*ctx
,
1354 LLVMValueRef src0
, LLVMValueRef src1
)
1356 LLVMValueRef dst64
, result
;
1357 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
1358 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
1360 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1361 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1362 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1366 static LLVMValueRef
emit_bitfield_extract(struct nir_to_llvm_context
*ctx
,
1368 const LLVMValueRef srcs
[3])
1370 LLVMValueRef result
;
1371 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
1373 result
= ac_build_bfe(&ctx
->ac
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
1374 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
1378 static LLVMValueRef
emit_bitfield_insert(struct nir_to_llvm_context
*ctx
,
1379 LLVMValueRef src0
, LLVMValueRef src1
,
1380 LLVMValueRef src2
, LLVMValueRef src3
)
1382 LLVMValueRef bfi_args
[3], result
;
1384 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
1385 LLVMBuildSub(ctx
->builder
,
1386 LLVMBuildShl(ctx
->builder
,
1391 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
1394 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
1397 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
1398 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
1400 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
1401 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
1402 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
1404 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
1408 static LLVMValueRef
emit_pack_half_2x16(struct nir_to_llvm_context
*ctx
,
1411 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1413 LLVMValueRef comp
[2];
1415 src0
= to_float(&ctx
->ac
, src0
);
1416 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32zero
, "");
1417 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32one
, "");
1418 for (i
= 0; i
< 2; i
++) {
1419 comp
[i
] = LLVMBuildFPTrunc(ctx
->builder
, comp
[i
], ctx
->f16
, "");
1420 comp
[i
] = LLVMBuildBitCast(ctx
->builder
, comp
[i
], ctx
->i16
, "");
1421 comp
[i
] = LLVMBuildZExt(ctx
->builder
, comp
[i
], ctx
->i32
, "");
1424 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
1425 comp
[0] = LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
1430 static LLVMValueRef
emit_unpack_half_2x16(struct nir_to_llvm_context
*ctx
,
1433 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1434 LLVMValueRef temps
[2], result
, val
;
1437 for (i
= 0; i
< 2; i
++) {
1438 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
1439 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
1440 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
1441 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
1444 result
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), temps
[0],
1446 result
= LLVMBuildInsertElement(ctx
->builder
, result
, temps
[1],
1451 static LLVMValueRef
emit_ddxy(struct nir_to_llvm_context
*ctx
,
1457 LLVMValueRef result
;
1458 ctx
->has_ddxy
= true;
1460 if (!ctx
->lds
&& !ctx
->has_ds_bpermute
)
1461 ctx
->lds
= LLVMAddGlobalInAddressSpace(ctx
->module
,
1462 LLVMArrayType(ctx
->i32
, 64),
1463 "ddxy_lds", LOCAL_ADDR_SPACE
);
1465 if (op
== nir_op_fddx_fine
|| op
== nir_op_fddx
)
1466 mask
= AC_TID_MASK_LEFT
;
1467 else if (op
== nir_op_fddy_fine
|| op
== nir_op_fddy
)
1468 mask
= AC_TID_MASK_TOP
;
1470 mask
= AC_TID_MASK_TOP_LEFT
;
1472 /* for DDX we want to next X pixel, DDY next Y pixel. */
1473 if (op
== nir_op_fddx_fine
||
1474 op
== nir_op_fddx_coarse
||
1480 result
= ac_build_ddxy(&ctx
->ac
, ctx
->has_ds_bpermute
,
1481 mask
, idx
, ctx
->lds
,
1487 * this takes an I,J coordinate pair,
1488 * and works out the X and Y derivatives.
1489 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
1491 static LLVMValueRef
emit_ddxy_interp(
1492 struct nir_to_llvm_context
*ctx
,
1493 LLVMValueRef interp_ij
)
1495 LLVMValueRef result
[4], a
;
1498 for (i
= 0; i
< 2; i
++) {
1499 a
= LLVMBuildExtractElement(ctx
->builder
, interp_ij
,
1500 LLVMConstInt(ctx
->i32
, i
, false), "");
1501 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
1502 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
1504 return ac_build_gather_values(&ctx
->ac
, result
, 4);
1507 static void visit_alu(struct nir_to_llvm_context
*ctx
, const nir_alu_instr
*instr
)
1509 LLVMValueRef src
[4], result
= NULL
;
1510 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
1511 unsigned src_components
;
1512 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
1514 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
1515 switch (instr
->op
) {
1521 case nir_op_pack_half_2x16
:
1524 case nir_op_unpack_half_2x16
:
1528 src_components
= num_components
;
1531 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1532 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
1534 switch (instr
->op
) {
1540 src
[0] = to_float(&ctx
->ac
, src
[0]);
1541 result
= LLVMBuildFNeg(ctx
->builder
, src
[0], "");
1544 result
= LLVMBuildNeg(ctx
->builder
, src
[0], "");
1547 result
= LLVMBuildNot(ctx
->builder
, src
[0], "");
1550 result
= LLVMBuildAdd(ctx
->builder
, src
[0], src
[1], "");
1553 src
[0] = to_float(&ctx
->ac
, src
[0]);
1554 src
[1] = to_float(&ctx
->ac
, src
[1]);
1555 result
= LLVMBuildFAdd(ctx
->builder
, src
[0], src
[1], "");
1558 src
[0] = to_float(&ctx
->ac
, src
[0]);
1559 src
[1] = to_float(&ctx
->ac
, src
[1]);
1560 result
= LLVMBuildFSub(ctx
->builder
, src
[0], src
[1], "");
1563 result
= LLVMBuildSub(ctx
->builder
, src
[0], src
[1], "");
1566 result
= LLVMBuildMul(ctx
->builder
, src
[0], src
[1], "");
1569 result
= LLVMBuildSRem(ctx
->builder
, src
[0], src
[1], "");
1572 result
= LLVMBuildURem(ctx
->builder
, src
[0], src
[1], "");
1575 src
[0] = to_float(&ctx
->ac
, src
[0]);
1576 src
[1] = to_float(&ctx
->ac
, src
[1]);
1577 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1578 result
= emit_intrin_1f_param(ctx
, "llvm.floor",
1579 to_float_type(&ctx
->ac
, def_type
), result
);
1580 result
= LLVMBuildFMul(ctx
->builder
, src
[1] , result
, "");
1581 result
= LLVMBuildFSub(ctx
->builder
, src
[0], result
, "");
1584 src
[0] = to_float(&ctx
->ac
, src
[0]);
1585 src
[1] = to_float(&ctx
->ac
, src
[1]);
1586 result
= LLVMBuildFRem(ctx
->builder
, src
[0], src
[1], "");
1589 result
= LLVMBuildSRem(ctx
->builder
, src
[0], src
[1], "");
1592 result
= LLVMBuildSDiv(ctx
->builder
, src
[0], src
[1], "");
1595 result
= LLVMBuildUDiv(ctx
->builder
, src
[0], src
[1], "");
1598 src
[0] = to_float(&ctx
->ac
, src
[0]);
1599 src
[1] = to_float(&ctx
->ac
, src
[1]);
1600 result
= LLVMBuildFMul(ctx
->builder
, src
[0], src
[1], "");
1603 src
[0] = to_float(&ctx
->ac
, src
[0]);
1604 src
[1] = to_float(&ctx
->ac
, src
[1]);
1605 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1608 src
[0] = to_float(&ctx
->ac
, src
[0]);
1609 result
= ac_build_fdiv(&ctx
->ac
, ctx
->f32one
, src
[0]);
1612 result
= LLVMBuildAnd(ctx
->builder
, src
[0], src
[1], "");
1615 result
= LLVMBuildOr(ctx
->builder
, src
[0], src
[1], "");
1618 result
= LLVMBuildXor(ctx
->builder
, src
[0], src
[1], "");
1621 result
= LLVMBuildShl(ctx
->builder
, src
[0], src
[1], "");
1624 result
= LLVMBuildAShr(ctx
->builder
, src
[0], src
[1], "");
1627 result
= LLVMBuildLShr(ctx
->builder
, src
[0], src
[1], "");
1630 result
= emit_int_cmp(ctx
, LLVMIntSLT
, src
[0], src
[1]);
1633 result
= emit_int_cmp(ctx
, LLVMIntNE
, src
[0], src
[1]);
1636 result
= emit_int_cmp(ctx
, LLVMIntEQ
, src
[0], src
[1]);
1639 result
= emit_int_cmp(ctx
, LLVMIntSGE
, src
[0], src
[1]);
1642 result
= emit_int_cmp(ctx
, LLVMIntULT
, src
[0], src
[1]);
1645 result
= emit_int_cmp(ctx
, LLVMIntUGE
, src
[0], src
[1]);
1648 result
= emit_float_cmp(ctx
, LLVMRealUEQ
, src
[0], src
[1]);
1651 result
= emit_float_cmp(ctx
, LLVMRealUNE
, src
[0], src
[1]);
1654 result
= emit_float_cmp(ctx
, LLVMRealULT
, src
[0], src
[1]);
1657 result
= emit_float_cmp(ctx
, LLVMRealUGE
, src
[0], src
[1]);
1660 result
= emit_intrin_1f_param(ctx
, "llvm.fabs",
1661 to_float_type(&ctx
->ac
, def_type
), src
[0]);
1664 result
= emit_iabs(ctx
, src
[0]);
1667 result
= emit_minmax_int(ctx
, LLVMIntSGT
, src
[0], src
[1]);
1670 result
= emit_minmax_int(ctx
, LLVMIntSLT
, src
[0], src
[1]);
1673 result
= emit_minmax_int(ctx
, LLVMIntUGT
, src
[0], src
[1]);
1676 result
= emit_minmax_int(ctx
, LLVMIntULT
, src
[0], src
[1]);
1679 result
= emit_isign(ctx
, src
[0]);
1682 src
[0] = to_float(&ctx
->ac
, src
[0]);
1683 result
= emit_fsign(ctx
, src
[0]);
1686 result
= emit_intrin_1f_param(ctx
, "llvm.floor",
1687 to_float_type(&ctx
->ac
, def_type
), src
[0]);
1690 result
= emit_intrin_1f_param(ctx
, "llvm.trunc",
1691 to_float_type(&ctx
->ac
, def_type
), src
[0]);
1694 result
= emit_intrin_1f_param(ctx
, "llvm.ceil",
1695 to_float_type(&ctx
->ac
, def_type
), src
[0]);
1697 case nir_op_fround_even
:
1698 result
= emit_intrin_1f_param(ctx
, "llvm.rint",
1699 to_float_type(&ctx
->ac
, def_type
),src
[0]);
1702 result
= emit_ffract(ctx
, src
[0]);
1705 result
= emit_intrin_1f_param(ctx
, "llvm.sin",
1706 to_float_type(&ctx
->ac
, def_type
), src
[0]);
1709 result
= emit_intrin_1f_param(ctx
, "llvm.cos",
1710 to_float_type(&ctx
->ac
, def_type
), src
[0]);
1713 result
= emit_intrin_1f_param(ctx
, "llvm.sqrt",
1714 to_float_type(&ctx
->ac
, def_type
), src
[0]);
1717 result
= emit_intrin_1f_param(ctx
, "llvm.exp2",
1718 to_float_type(&ctx
->ac
, def_type
), src
[0]);
1721 result
= emit_intrin_1f_param(ctx
, "llvm.log2",
1722 to_float_type(&ctx
->ac
, def_type
), src
[0]);
1725 result
= emit_intrin_1f_param(ctx
, "llvm.sqrt",
1726 to_float_type(&ctx
->ac
, def_type
), src
[0]);
1727 result
= ac_build_fdiv(&ctx
->ac
, ctx
->f32one
, result
);
1730 result
= emit_intrin_2f_param(ctx
, "llvm.pow",
1731 to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1734 result
= emit_intrin_2f_param(ctx
, "llvm.maxnum",
1735 to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1736 if (instr
->dest
.dest
.ssa
.bit_size
== 32)
1737 result
= emit_intrin_1f_param(ctx
, "llvm.canonicalize",
1738 to_float_type(&ctx
->ac
, def_type
),
1742 result
= emit_intrin_2f_param(ctx
, "llvm.minnum",
1743 to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1744 if (instr
->dest
.dest
.ssa
.bit_size
== 32)
1745 result
= emit_intrin_1f_param(ctx
, "llvm.canonicalize",
1746 to_float_type(&ctx
->ac
, def_type
),
1750 result
= emit_intrin_3f_param(ctx
, "llvm.fma",
1751 to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1], src
[2]);
1753 case nir_op_ibitfield_extract
:
1754 result
= emit_bitfield_extract(ctx
, true, src
);
1756 case nir_op_ubitfield_extract
:
1757 result
= emit_bitfield_extract(ctx
, false, src
);
1759 case nir_op_bitfield_insert
:
1760 result
= emit_bitfield_insert(ctx
, src
[0], src
[1], src
[2], src
[3]);
1762 case nir_op_bitfield_reverse
:
1763 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.bitreverse.i32", ctx
->i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1765 case nir_op_bit_count
:
1766 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i32", ctx
->i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1771 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1772 src
[i
] = to_integer(&ctx
->ac
, src
[i
]);
1773 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
1777 src
[0] = to_float(&ctx
->ac
, src
[0]);
1778 result
= LLVMBuildFPToSI(ctx
->builder
, src
[0], def_type
, "");
1782 src
[0] = to_float(&ctx
->ac
, src
[0]);
1783 result
= LLVMBuildFPToUI(ctx
->builder
, src
[0], def_type
, "");
1787 result
= LLVMBuildSIToFP(ctx
->builder
, src
[0], to_float_type(&ctx
->ac
, def_type
), "");
1791 result
= LLVMBuildUIToFP(ctx
->builder
, src
[0], to_float_type(&ctx
->ac
, def_type
), "");
1794 result
= LLVMBuildFPExt(ctx
->builder
, src
[0], to_float_type(&ctx
->ac
, def_type
), "");
1797 result
= LLVMBuildFPTrunc(ctx
->builder
, src
[0], to_float_type(&ctx
->ac
, def_type
), "");
1801 if (get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < get_elem_bits(&ctx
->ac
, def_type
))
1802 result
= LLVMBuildZExt(ctx
->builder
, src
[0], def_type
, "");
1804 result
= LLVMBuildTrunc(ctx
->builder
, src
[0], def_type
, "");
1808 if (get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < get_elem_bits(&ctx
->ac
, def_type
))
1809 result
= LLVMBuildSExt(ctx
->builder
, src
[0], def_type
, "");
1811 result
= LLVMBuildTrunc(ctx
->builder
, src
[0], def_type
, "");
1814 result
= emit_bcsel(ctx
, src
[0], src
[1], src
[2]);
1816 case nir_op_find_lsb
:
1817 result
= emit_find_lsb(ctx
, src
[0]);
1819 case nir_op_ufind_msb
:
1820 result
= emit_ufind_msb(ctx
, src
[0]);
1822 case nir_op_ifind_msb
:
1823 result
= emit_ifind_msb(ctx
, src
[0]);
1825 case nir_op_uadd_carry
:
1826 result
= emit_uint_carry(ctx
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
1828 case nir_op_usub_borrow
:
1829 result
= emit_uint_carry(ctx
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
1832 result
= emit_b2f(ctx
, src
[0]);
1835 result
= emit_f2b(&ctx
->ac
, src
[0]);
1838 result
= emit_b2i(&ctx
->ac
, src
[0]);
1841 result
= emit_i2b(&ctx
->ac
, src
[0]);
1843 case nir_op_fquantize2f16
:
1844 result
= emit_f2f16(ctx
, src
[0]);
1846 case nir_op_umul_high
:
1847 result
= emit_umul_high(ctx
, src
[0], src
[1]);
1849 case nir_op_imul_high
:
1850 result
= emit_imul_high(ctx
, src
[0], src
[1]);
1852 case nir_op_pack_half_2x16
:
1853 result
= emit_pack_half_2x16(ctx
, src
[0]);
1855 case nir_op_unpack_half_2x16
:
1856 result
= emit_unpack_half_2x16(ctx
, src
[0]);
1860 case nir_op_fddx_fine
:
1861 case nir_op_fddy_fine
:
1862 case nir_op_fddx_coarse
:
1863 case nir_op_fddy_coarse
:
1864 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
1867 fprintf(stderr
, "Unknown NIR alu instr: ");
1868 nir_print_instr(&instr
->instr
, stderr
);
1869 fprintf(stderr
, "\n");
1874 assert(instr
->dest
.dest
.is_ssa
);
1875 result
= to_integer(&ctx
->ac
, result
);
1876 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.dest
.ssa
,
1881 static void visit_load_const(struct nir_to_llvm_context
*ctx
,
1882 const nir_load_const_instr
*instr
)
1884 LLVMValueRef values
[4], value
= NULL
;
1885 LLVMTypeRef element_type
=
1886 LLVMIntTypeInContext(ctx
->context
, instr
->def
.bit_size
);
1888 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1889 switch (instr
->def
.bit_size
) {
1891 values
[i
] = LLVMConstInt(element_type
,
1892 instr
->value
.u32
[i
], false);
1895 values
[i
] = LLVMConstInt(element_type
,
1896 instr
->value
.u64
[i
], false);
1900 "unsupported nir load_const bit_size: %d\n",
1901 instr
->def
.bit_size
);
1905 if (instr
->def
.num_components
> 1) {
1906 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1910 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, value
);
1913 static LLVMValueRef
cast_ptr(struct nir_to_llvm_context
*ctx
, LLVMValueRef ptr
,
1916 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
1917 return LLVMBuildBitCast(ctx
->builder
, ptr
,
1918 LLVMPointerType(type
, addr_space
), "");
1922 get_buffer_size(struct nir_to_llvm_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1925 LLVMBuildExtractElement(ctx
->builder
, descriptor
,
1926 LLVMConstInt(ctx
->i32
, 2, false), "");
1929 if (ctx
->options
->chip_class
>= VI
&& in_elements
) {
1930 /* On VI, the descriptor contains the size in bytes,
1931 * but TXQ must return the size in elements.
1932 * The stride is always non-zero for resources using TXQ.
1934 LLVMValueRef stride
=
1935 LLVMBuildExtractElement(ctx
->builder
, descriptor
,
1936 LLVMConstInt(ctx
->i32
, 1, false), "");
1937 stride
= LLVMBuildLShr(ctx
->builder
, stride
,
1938 LLVMConstInt(ctx
->i32
, 16, false), "");
1939 stride
= LLVMBuildAnd(ctx
->builder
, stride
,
1940 LLVMConstInt(ctx
->i32
, 0x3fff, false), "");
1942 size
= LLVMBuildUDiv(ctx
->builder
, size
, stride
, "");
1948 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
1951 static void build_int_type_name(
1953 char *buf
, unsigned bufsize
)
1955 assert(bufsize
>= 6);
1957 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
1958 snprintf(buf
, bufsize
, "v%ui32",
1959 LLVMGetVectorSize(type
));
1964 static LLVMValueRef
radv_lower_gather4_integer(struct nir_to_llvm_context
*ctx
,
1965 struct ac_image_args
*args
,
1966 const nir_tex_instr
*instr
)
1968 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
1969 LLVMValueRef coord
= args
->addr
;
1970 LLVMValueRef half_texel
[2];
1971 LLVMValueRef compare_cube_wa
;
1972 LLVMValueRef result
;
1974 unsigned coord_vgpr_index
= (unsigned)args
->offset
+ (unsigned)args
->compare
;
1978 struct ac_image_args txq_args
= { 0 };
1980 txq_args
.da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
1981 txq_args
.opcode
= ac_image_get_resinfo
;
1982 txq_args
.dmask
= 0xf;
1983 txq_args
.addr
= ctx
->i32zero
;
1984 txq_args
.resource
= args
->resource
;
1985 LLVMValueRef size
= ac_build_image_opcode(&ctx
->ac
, &txq_args
);
1987 for (c
= 0; c
< 2; c
++) {
1988 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
1989 LLVMConstInt(ctx
->i32
, c
, false), "");
1990 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1991 half_texel
[c
] = ac_build_fdiv(&ctx
->ac
, ctx
->f32one
, half_texel
[c
]);
1992 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1993 LLVMConstReal(ctx
->f32
, -0.5), "");
1997 LLVMValueRef orig_coords
= args
->addr
;
1999 for (c
= 0; c
< 2; c
++) {
2001 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, coord_vgpr_index
+ c
, 0);
2002 tmp
= LLVMBuildExtractElement(ctx
->builder
, coord
, index
, "");
2003 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
2004 tmp
= LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
2005 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
2006 coord
= LLVMBuildInsertElement(ctx
->builder
, coord
, tmp
, index
, "");
2011 * Apparantly cube has issue with integer types that the workaround doesn't solve,
2012 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
2013 * workaround by sampling using a scaled type and converting.
2014 * This is taken from amdgpu-pro shaders.
2016 /* NOTE this produces some ugly code compared to amdgpu-pro,
2017 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
2018 * and then reads them back. -pro generates two selects,
2019 * one s_cmp for the descriptor rewriting
2020 * one v_cmp for the coordinate and result changes.
2022 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
2023 LLVMValueRef tmp
, tmp2
;
2025 /* workaround 8/8/8/8 uint/sint cube gather bug */
2026 /* first detect it then change to a scaled read and f2i */
2027 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32one
, "");
2030 /* extract the DATA_FORMAT */
2031 tmp
= ac_build_bfe(&ctx
->ac
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
2032 LLVMConstInt(ctx
->i32
, 6, false), false);
2034 /* is the DATA_FORMAT == 8_8_8_8 */
2035 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
2037 if (stype
== GLSL_TYPE_UINT
)
2038 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
2039 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
2040 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
2042 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
2043 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
2044 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
2046 /* replace the NUM FORMAT in the descriptor */
2047 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT_GFX6
, false), "");
2048 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
2050 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32one
, "");
2052 /* don't modify the coordinates for this case */
2053 coord
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, orig_coords
, coord
, "");
2056 result
= ac_build_image_opcode(&ctx
->ac
, args
);
2058 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
2059 LLVMValueRef tmp
, tmp2
;
2061 /* if the cube workaround is in place, f2i the result. */
2062 for (c
= 0; c
< 4; c
++) {
2063 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
2064 if (stype
== GLSL_TYPE_UINT
)
2065 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
2067 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
2068 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
2069 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
2070 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
2071 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
2072 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
2078 static LLVMValueRef
build_tex_intrinsic(struct nir_to_llvm_context
*ctx
,
2079 const nir_tex_instr
*instr
,
2081 struct ac_image_args
*args
)
2083 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
2084 return ac_build_buffer_load_format(&ctx
->ac
,
2087 LLVMConstInt(ctx
->i32
, 0, false),
2091 args
->opcode
= ac_image_sample
;
2092 args
->compare
= instr
->is_shadow
;
2094 switch (instr
->op
) {
2096 case nir_texop_txf_ms
:
2097 case nir_texop_samples_identical
:
2098 args
->opcode
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
? ac_image_load
: ac_image_load_mip
;
2099 args
->compare
= false;
2100 args
->offset
= false;
2107 args
->level_zero
= true;
2112 case nir_texop_query_levels
:
2113 args
->opcode
= ac_image_get_resinfo
;
2116 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
2117 args
->level_zero
= true;
2123 args
->opcode
= ac_image_gather4
;
2124 args
->level_zero
= true;
2127 args
->opcode
= ac_image_get_lod
;
2128 args
->compare
= false;
2129 args
->offset
= false;
2135 if (instr
->op
== nir_texop_tg4
) {
2136 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
2137 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
2138 return radv_lower_gather4_integer(ctx
, args
, instr
);
2141 return ac_build_image_opcode(&ctx
->ac
, args
);
2144 static LLVMValueRef
visit_vulkan_resource_index(struct nir_to_llvm_context
*ctx
,
2145 nir_intrinsic_instr
*instr
)
2147 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
2148 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
2149 unsigned binding
= nir_intrinsic_binding(instr
);
2150 LLVMValueRef desc_ptr
= ctx
->descriptor_sets
[desc_set
];
2151 struct radv_pipeline_layout
*pipeline_layout
= ctx
->options
->layout
;
2152 struct radv_descriptor_set_layout
*layout
= pipeline_layout
->set
[desc_set
].layout
;
2153 unsigned base_offset
= layout
->binding
[binding
].offset
;
2154 LLVMValueRef offset
, stride
;
2156 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
2157 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
2158 unsigned idx
= pipeline_layout
->set
[desc_set
].dynamic_offset_start
+
2159 layout
->binding
[binding
].dynamic_offset_offset
;
2160 desc_ptr
= ctx
->push_constants
;
2161 base_offset
= pipeline_layout
->push_constant_size
+ 16 * idx
;
2162 stride
= LLVMConstInt(ctx
->i32
, 16, false);
2164 stride
= LLVMConstInt(ctx
->i32
, layout
->binding
[binding
].size
, false);
2166 offset
= LLVMConstInt(ctx
->i32
, base_offset
, false);
2167 index
= LLVMBuildMul(ctx
->builder
, index
, stride
, "");
2168 offset
= LLVMBuildAdd(ctx
->builder
, offset
, index
, "");
2170 desc_ptr
= ac_build_gep0(&ctx
->ac
, desc_ptr
, offset
);
2171 desc_ptr
= cast_ptr(ctx
, desc_ptr
, ctx
->v4i32
);
2172 LLVMSetMetadata(desc_ptr
, ctx
->uniform_md_kind
, ctx
->empty_md
);
2174 return LLVMBuildLoad(ctx
->builder
, desc_ptr
, "");
2177 static LLVMValueRef
visit_load_push_constant(struct nir_to_llvm_context
*ctx
,
2178 nir_intrinsic_instr
*instr
)
2180 LLVMValueRef ptr
, addr
;
2182 addr
= LLVMConstInt(ctx
->i32
, nir_intrinsic_base(instr
), 0);
2183 addr
= LLVMBuildAdd(ctx
->builder
, addr
, get_src(ctx
, instr
->src
[0]), "");
2185 ptr
= ac_build_gep0(&ctx
->ac
, ctx
->push_constants
, addr
);
2186 ptr
= cast_ptr(ctx
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
2188 return LLVMBuildLoad(ctx
->builder
, ptr
, "");
2191 static LLVMValueRef
visit_get_buffer_size(struct nir_to_llvm_context
*ctx
,
2192 const nir_intrinsic_instr
*instr
)
2194 LLVMValueRef desc
= get_src(ctx
, instr
->src
[0]);
2196 return get_buffer_size(ctx
, desc
, false);
2198 static void visit_store_ssbo(struct nir_to_llvm_context
*ctx
,
2199 nir_intrinsic_instr
*instr
)
2201 const char *store_name
;
2202 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
2203 LLVMTypeRef data_type
= ctx
->f32
;
2204 int elem_size_mult
= get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 32;
2205 int components_32bit
= elem_size_mult
* instr
->num_components
;
2206 unsigned writemask
= nir_intrinsic_write_mask(instr
);
2207 LLVMValueRef base_data
, base_offset
;
2208 LLVMValueRef params
[6];
2210 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
2211 ctx
->shader_info
->fs
.writes_memory
= true;
2213 params
[1] = get_src(ctx
, instr
->src
[1]);
2214 params
[2] = LLVMConstInt(ctx
->i32
, 0, false); /* vindex */
2215 params
[4] = ctx
->i1false
; /* glc */
2216 params
[5] = ctx
->i1false
; /* slc */
2218 if (components_32bit
> 1)
2219 data_type
= LLVMVectorType(ctx
->f32
, components_32bit
);
2221 base_data
= to_float(&ctx
->ac
, src_data
);
2222 base_data
= trim_vector(ctx
, base_data
, instr
->num_components
);
2223 base_data
= LLVMBuildBitCast(ctx
->builder
, base_data
,
2225 base_offset
= get_src(ctx
, instr
->src
[2]); /* voffset */
2229 LLVMValueRef offset
;
2231 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
2233 /* Due to an LLVM limitation, split 3-element writes
2234 * into a 2-element and a 1-element write. */
2236 writemask
|= 1 << (start
+ 2);
2240 start
*= elem_size_mult
;
2241 count
*= elem_size_mult
;
2244 writemask
|= ((1u << (count
- 4)) - 1u) << (start
+ 4);
2249 store_name
= "llvm.amdgcn.buffer.store.v4f32";
2251 } else if (count
== 2) {
2252 tmp
= LLVMBuildExtractElement(ctx
->builder
,
2253 base_data
, LLVMConstInt(ctx
->i32
, start
, false), "");
2254 data
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), tmp
,
2257 tmp
= LLVMBuildExtractElement(ctx
->builder
,
2258 base_data
, LLVMConstInt(ctx
->i32
, start
+ 1, false), "");
2259 data
= LLVMBuildInsertElement(ctx
->builder
, data
, tmp
,
2261 store_name
= "llvm.amdgcn.buffer.store.v2f32";
2265 if (get_llvm_num_components(base_data
) > 1)
2266 data
= LLVMBuildExtractElement(ctx
->builder
, base_data
,
2267 LLVMConstInt(ctx
->i32
, start
, false), "");
2270 store_name
= "llvm.amdgcn.buffer.store.f32";
2273 offset
= base_offset
;
2275 offset
= LLVMBuildAdd(ctx
->builder
, offset
, LLVMConstInt(ctx
->i32
, start
* 4, false), "");
2279 ac_build_intrinsic(&ctx
->ac
, store_name
,
2280 ctx
->voidt
, params
, 6, 0);
2284 static LLVMValueRef
visit_atomic_ssbo(struct nir_to_llvm_context
*ctx
,
2285 const nir_intrinsic_instr
*instr
)
2288 LLVMValueRef params
[6];
2290 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
2291 ctx
->shader_info
->fs
.writes_memory
= true;
2293 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
2294 params
[arg_count
++] = llvm_extract_elem(ctx
, get_src(ctx
, instr
->src
[3]), 0);
2296 params
[arg_count
++] = llvm_extract_elem(ctx
, get_src(ctx
, instr
->src
[2]), 0);
2297 params
[arg_count
++] = get_src(ctx
, instr
->src
[0]);
2298 params
[arg_count
++] = LLVMConstInt(ctx
->i32
, 0, false); /* vindex */
2299 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
2300 params
[arg_count
++] = ctx
->i1false
; /* slc */
2302 switch (instr
->intrinsic
) {
2303 case nir_intrinsic_ssbo_atomic_add
:
2304 name
= "llvm.amdgcn.buffer.atomic.add";
2306 case nir_intrinsic_ssbo_atomic_imin
:
2307 name
= "llvm.amdgcn.buffer.atomic.smin";
2309 case nir_intrinsic_ssbo_atomic_umin
:
2310 name
= "llvm.amdgcn.buffer.atomic.umin";
2312 case nir_intrinsic_ssbo_atomic_imax
:
2313 name
= "llvm.amdgcn.buffer.atomic.smax";
2315 case nir_intrinsic_ssbo_atomic_umax
:
2316 name
= "llvm.amdgcn.buffer.atomic.umax";
2318 case nir_intrinsic_ssbo_atomic_and
:
2319 name
= "llvm.amdgcn.buffer.atomic.and";
2321 case nir_intrinsic_ssbo_atomic_or
:
2322 name
= "llvm.amdgcn.buffer.atomic.or";
2324 case nir_intrinsic_ssbo_atomic_xor
:
2325 name
= "llvm.amdgcn.buffer.atomic.xor";
2327 case nir_intrinsic_ssbo_atomic_exchange
:
2328 name
= "llvm.amdgcn.buffer.atomic.swap";
2330 case nir_intrinsic_ssbo_atomic_comp_swap
:
2331 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
2337 return ac_build_intrinsic(&ctx
->ac
, name
, ctx
->i32
, params
, arg_count
, 0);
2340 static LLVMValueRef
visit_load_buffer(struct nir_to_llvm_context
*ctx
,
2341 const nir_intrinsic_instr
*instr
)
2343 LLVMValueRef results
[2];
2344 int load_components
;
2345 int num_components
= instr
->num_components
;
2346 if (instr
->dest
.ssa
.bit_size
== 64)
2347 num_components
*= 2;
2349 for (int i
= 0; i
< num_components
; i
+= load_components
) {
2350 load_components
= MIN2(num_components
- i
, 4);
2351 const char *load_name
;
2352 LLVMTypeRef data_type
= ctx
->f32
;
2353 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, i
* 4, false);
2354 offset
= LLVMBuildAdd(ctx
->builder
, get_src(ctx
, instr
->src
[1]), offset
, "");
2356 if (load_components
== 3)
2357 data_type
= LLVMVectorType(ctx
->f32
, 4);
2358 else if (load_components
> 1)
2359 data_type
= LLVMVectorType(ctx
->f32
, load_components
);
2361 if (load_components
>= 3)
2362 load_name
= "llvm.amdgcn.buffer.load.v4f32";
2363 else if (load_components
== 2)
2364 load_name
= "llvm.amdgcn.buffer.load.v2f32";
2365 else if (load_components
== 1)
2366 load_name
= "llvm.amdgcn.buffer.load.f32";
2368 unreachable("unhandled number of components");
2370 LLVMValueRef params
[] = {
2371 get_src(ctx
, instr
->src
[0]),
2372 LLVMConstInt(ctx
->i32
, 0, false),
2378 results
[i
] = ac_build_intrinsic(&ctx
->ac
, load_name
, data_type
, params
, 5, 0);
2382 LLVMValueRef ret
= results
[0];
2383 if (num_components
> 4 || num_components
== 3) {
2384 LLVMValueRef masks
[] = {
2385 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
2386 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false),
2387 LLVMConstInt(ctx
->i32
, 4, false), LLVMConstInt(ctx
->i32
, 5, false),
2388 LLVMConstInt(ctx
->i32
, 6, false), LLVMConstInt(ctx
->i32
, 7, false)
2391 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
2392 ret
= LLVMBuildShuffleVector(ctx
->builder
, results
[0],
2393 results
[num_components
> 4 ? 1 : 0], swizzle
, "");
2396 return LLVMBuildBitCast(ctx
->builder
, ret
,
2397 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2400 static LLVMValueRef
visit_load_ubo_buffer(struct nir_to_llvm_context
*ctx
,
2401 const nir_intrinsic_instr
*instr
)
2403 LLVMValueRef results
[8], ret
;
2404 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
2405 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
2406 int num_components
= instr
->num_components
;
2408 rsrc
= LLVMBuildBitCast(ctx
->builder
, rsrc
, LLVMVectorType(ctx
->i8
, 16), "");
2410 if (instr
->dest
.ssa
.bit_size
== 64)
2411 num_components
*= 2;
2413 for (unsigned i
= 0; i
< num_components
; ++i
) {
2414 LLVMValueRef params
[] = {
2416 LLVMBuildAdd(ctx
->builder
, LLVMConstInt(ctx
->i32
, 4 * i
, 0),
2419 results
[i
] = ac_build_intrinsic(&ctx
->ac
, "llvm.SI.load.const", ctx
->f32
,
2421 AC_FUNC_ATTR_READNONE
|
2422 AC_FUNC_ATTR_LEGACY
);
2426 ret
= ac_build_gather_values(&ctx
->ac
, results
, instr
->num_components
);
2427 return LLVMBuildBitCast(ctx
->builder
, ret
,
2428 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2432 radv_get_deref_offset(struct nir_to_llvm_context
*ctx
, nir_deref_var
*deref
,
2433 bool vs_in
, unsigned *vertex_index_out
,
2434 LLVMValueRef
*vertex_index_ref
,
2435 unsigned *const_out
, LLVMValueRef
*indir_out
)
2437 unsigned const_offset
= 0;
2438 nir_deref
*tail
= &deref
->deref
;
2439 LLVMValueRef offset
= NULL
;
2441 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
2443 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2444 if (vertex_index_out
)
2445 *vertex_index_out
= deref_array
->base_offset
;
2447 if (vertex_index_ref
) {
2448 LLVMValueRef vtx
= LLVMConstInt(ctx
->i32
, deref_array
->base_offset
, false);
2449 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
2450 vtx
= LLVMBuildAdd(ctx
->builder
, vtx
, get_src(ctx
, deref_array
->indirect
), "");
2452 *vertex_index_ref
= vtx
;
2456 if (deref
->var
->data
.compact
) {
2457 assert(tail
->child
->deref_type
== nir_deref_type_array
);
2458 assert(glsl_type_is_scalar(glsl_without_array(deref
->var
->type
)));
2459 nir_deref_array
*deref_array
= nir_deref_as_array(tail
->child
);
2460 /* We always lower indirect dereferences for "compact" array vars. */
2461 assert(deref_array
->deref_array_type
== nir_deref_array_type_direct
);
2463 const_offset
= deref_array
->base_offset
;
2467 while (tail
->child
!= NULL
) {
2468 const struct glsl_type
*parent_type
= tail
->type
;
2471 if (tail
->deref_type
== nir_deref_type_array
) {
2472 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2473 LLVMValueRef index
, stride
, local_offset
;
2474 unsigned size
= glsl_count_attribute_slots(tail
->type
, vs_in
);
2476 const_offset
+= size
* deref_array
->base_offset
;
2477 if (deref_array
->deref_array_type
== nir_deref_array_type_direct
)
2480 assert(deref_array
->deref_array_type
== nir_deref_array_type_indirect
);
2481 index
= get_src(ctx
, deref_array
->indirect
);
2482 stride
= LLVMConstInt(ctx
->i32
, size
, 0);
2483 local_offset
= LLVMBuildMul(ctx
->builder
, stride
, index
, "");
2486 offset
= LLVMBuildAdd(ctx
->builder
, offset
, local_offset
, "");
2488 offset
= local_offset
;
2489 } else if (tail
->deref_type
== nir_deref_type_struct
) {
2490 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
2492 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
2493 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
2494 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
2497 unreachable("unsupported deref type");
2501 if (const_offset
&& offset
)
2502 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
2503 LLVMConstInt(ctx
->i32
, const_offset
, 0),
2506 *const_out
= const_offset
;
2507 *indir_out
= offset
;
2511 lds_load(struct nir_to_llvm_context
*ctx
,
2512 LLVMValueRef dw_addr
)
2515 value
= ac_build_indexed_load(&ctx
->ac
, ctx
->lds
, dw_addr
, false);
2520 lds_store(struct nir_to_llvm_context
*ctx
,
2521 LLVMValueRef dw_addr
, LLVMValueRef value
)
2523 value
= LLVMBuildBitCast(ctx
->builder
, value
, ctx
->i32
, "");
2524 ac_build_indexed_store(&ctx
->ac
, ctx
->lds
,
2528 /* The offchip buffer layout for TCS->TES is
2530 * - attribute 0 of patch 0 vertex 0
2531 * - attribute 0 of patch 0 vertex 1
2532 * - attribute 0 of patch 0 vertex 2
2534 * - attribute 0 of patch 1 vertex 0
2535 * - attribute 0 of patch 1 vertex 1
2537 * - attribute 1 of patch 0 vertex 0
2538 * - attribute 1 of patch 0 vertex 1
2540 * - per patch attribute 0 of patch 0
2541 * - per patch attribute 0 of patch 1
2544 * Note that every attribute has 4 components.
2546 static LLVMValueRef
get_tcs_tes_buffer_address(struct nir_to_llvm_context
*ctx
,
2547 LLVMValueRef vertex_index
,
2548 LLVMValueRef param_index
)
2550 LLVMValueRef base_addr
, vertices_per_patch
, num_patches
, total_vertices
;
2551 LLVMValueRef param_stride
, constant16
;
2552 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
2554 vertices_per_patch
= unpack_param(ctx
, ctx
->tcs_offchip_layout
, 9, 6);
2555 num_patches
= unpack_param(ctx
, ctx
->tcs_offchip_layout
, 0, 9);
2556 total_vertices
= LLVMBuildMul(ctx
->builder
, vertices_per_patch
,
2559 constant16
= LLVMConstInt(ctx
->i32
, 16, false);
2561 base_addr
= LLVMBuildMul(ctx
->builder
, rel_patch_id
,
2562 vertices_per_patch
, "");
2564 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2567 param_stride
= total_vertices
;
2569 base_addr
= rel_patch_id
;
2570 param_stride
= num_patches
;
2573 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2574 LLVMBuildMul(ctx
->builder
, param_index
,
2575 param_stride
, ""), "");
2577 base_addr
= LLVMBuildMul(ctx
->builder
, base_addr
, constant16
, "");
2579 if (!vertex_index
) {
2580 LLVMValueRef patch_data_offset
=
2581 unpack_param(ctx
, ctx
->tcs_offchip_layout
, 16, 16);
2583 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2584 patch_data_offset
, "");
2589 static LLVMValueRef
get_tcs_tes_buffer_address_params(struct nir_to_llvm_context
*ctx
,
2591 unsigned const_index
,
2593 LLVMValueRef vertex_index
,
2594 LLVMValueRef indir_index
)
2596 LLVMValueRef param_index
;
2599 param_index
= LLVMBuildAdd(ctx
->builder
, LLVMConstInt(ctx
->i32
, param
, false),
2602 if (const_index
&& !is_compact
)
2603 param
+= const_index
;
2604 param_index
= LLVMConstInt(ctx
->i32
, param
, false);
2606 return get_tcs_tes_buffer_address(ctx
, vertex_index
, param_index
);
2610 mark_tess_output(struct nir_to_llvm_context
*ctx
,
2611 bool is_patch
, uint32_t param
)
2615 ctx
->tess_patch_outputs_written
|= (1ull << param
);
2617 ctx
->tess_outputs_written
|= (1ull << param
);
2621 get_dw_address(struct nir_to_llvm_context
*ctx
,
2622 LLVMValueRef dw_addr
,
2624 unsigned const_index
,
2625 bool compact_const_index
,
2626 LLVMValueRef vertex_index
,
2627 LLVMValueRef stride
,
2628 LLVMValueRef indir_index
)
2633 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2634 LLVMBuildMul(ctx
->builder
,
2640 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2641 LLVMBuildMul(ctx
->builder
, indir_index
,
2642 LLVMConstInt(ctx
->i32
, 4, false), ""), "");
2643 else if (const_index
&& !compact_const_index
)
2644 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2645 LLVMConstInt(ctx
->i32
, const_index
, false), "");
2647 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2648 LLVMConstInt(ctx
->i32
, param
* 4, false), "");
2650 if (const_index
&& compact_const_index
)
2651 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2652 LLVMConstInt(ctx
->i32
, const_index
, false), "");
2657 load_tcs_input(struct nir_to_llvm_context
*ctx
,
2658 nir_intrinsic_instr
*instr
)
2660 LLVMValueRef dw_addr
, stride
;
2661 unsigned const_index
;
2662 LLVMValueRef vertex_index
;
2663 LLVMValueRef indir_index
;
2665 LLVMValueRef value
[4], result
;
2666 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2667 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2668 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2669 radv_get_deref_offset(ctx
, instr
->variables
[0],
2670 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2671 &const_index
, &indir_index
);
2673 stride
= unpack_param(ctx
, ctx
->tcs_in_layout
, 13, 8);
2674 dw_addr
= get_tcs_in_current_patch_offset(ctx
);
2675 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2678 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
2679 value
[i
] = lds_load(ctx
, dw_addr
);
2680 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2683 result
= ac_build_gather_values(&ctx
->ac
, value
, instr
->num_components
);
2684 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2689 load_tcs_output(struct nir_to_llvm_context
*ctx
,
2690 nir_intrinsic_instr
*instr
)
2692 LLVMValueRef dw_addr
, stride
;
2693 LLVMValueRef value
[4], result
;
2694 LLVMValueRef vertex_index
= NULL
;
2695 LLVMValueRef indir_index
= NULL
;
2696 unsigned const_index
= 0;
2698 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2699 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2700 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2701 radv_get_deref_offset(ctx
, instr
->variables
[0],
2702 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2703 &const_index
, &indir_index
);
2705 if (!instr
->variables
[0]->var
->data
.patch
) {
2706 stride
= unpack_param(ctx
, ctx
->tcs_out_layout
, 13, 8);
2707 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
2709 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
2712 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2715 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
2716 value
[i
] = lds_load(ctx
, dw_addr
);
2717 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2720 result
= ac_build_gather_values(&ctx
->ac
, value
, instr
->num_components
);
2721 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2726 store_tcs_output(struct nir_to_llvm_context
*ctx
,
2727 nir_intrinsic_instr
*instr
,
2731 LLVMValueRef stride
, dw_addr
;
2732 LLVMValueRef buf_addr
= NULL
;
2733 LLVMValueRef vertex_index
= NULL
;
2734 LLVMValueRef indir_index
= NULL
;
2735 unsigned const_index
= 0;
2737 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2738 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2740 radv_get_deref_offset(ctx
, instr
->variables
[0],
2741 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2742 &const_index
, &indir_index
);
2744 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2745 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CLIP_DIST0
&&
2746 is_compact
&& const_index
> 3) {
2751 if (!instr
->variables
[0]->var
->data
.patch
) {
2752 stride
= unpack_param(ctx
, ctx
->tcs_out_layout
, 13, 8);
2753 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
2755 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
2758 mark_tess_output(ctx
, instr
->variables
[0]->var
->data
.patch
, param
);
2760 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2762 buf_addr
= get_tcs_tes_buffer_address_params(ctx
, param
, const_index
, is_compact
,
2763 vertex_index
, indir_index
);
2765 unsigned base
= is_compact
? const_index
: 0;
2766 for (unsigned chan
= 0; chan
< 8; chan
++) {
2767 bool is_tess_factor
= false;
2768 if (!(writemask
& (1 << chan
)))
2770 LLVMValueRef value
= llvm_extract_elem(ctx
, src
, chan
);
2772 lds_store(ctx
, dw_addr
, value
);
2774 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_TESS_LEVEL_INNER
||
2775 instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_TESS_LEVEL_OUTER
)
2776 is_tess_factor
= true;
2778 if (!is_tess_factor
&& writemask
!= 0xF)
2779 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, value
, 1,
2780 buf_addr
, ctx
->oc_lds
,
2781 4 * (base
+ chan
), 1, 0, true, false);
2783 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2787 if (writemask
== 0xF) {
2788 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, src
, 4,
2789 buf_addr
, ctx
->oc_lds
,
2790 (base
* 4), 1, 0, true, false);
2795 load_tes_input(struct nir_to_llvm_context
*ctx
,
2796 const nir_intrinsic_instr
*instr
)
2798 LLVMValueRef buf_addr
;
2799 LLVMValueRef result
;
2800 LLVMValueRef vertex_index
= NULL
;
2801 LLVMValueRef indir_index
= NULL
;
2802 unsigned const_index
= 0;
2804 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2805 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2807 radv_get_deref_offset(ctx
, instr
->variables
[0],
2808 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2809 &const_index
, &indir_index
);
2810 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2811 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CLIP_DIST0
&&
2812 is_compact
&& const_index
> 3) {
2816 buf_addr
= get_tcs_tes_buffer_address_params(ctx
, param
, const_index
,
2817 is_compact
, vertex_index
, indir_index
);
2819 result
= ac_build_buffer_load(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, instr
->num_components
, NULL
,
2820 buf_addr
, ctx
->oc_lds
, is_compact
? (4 * const_index
) : 0, 1, 0, true, false);
2821 result
= trim_vector(ctx
, result
, instr
->num_components
);
2822 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2827 load_gs_input(struct nir_to_llvm_context
*ctx
,
2828 nir_intrinsic_instr
*instr
)
2830 LLVMValueRef indir_index
, vtx_offset
;
2831 unsigned const_index
;
2832 LLVMValueRef args
[9];
2833 unsigned param
, vtx_offset_param
;
2834 LLVMValueRef value
[4], result
;
2835 unsigned vertex_index
;
2836 radv_get_deref_offset(ctx
, instr
->variables
[0],
2837 false, &vertex_index
, NULL
,
2838 &const_index
, &indir_index
);
2839 vtx_offset_param
= vertex_index
;
2840 assert(vtx_offset_param
< 6);
2841 vtx_offset
= LLVMBuildMul(ctx
->builder
, ctx
->gs_vtx_offset
[vtx_offset_param
],
2842 LLVMConstInt(ctx
->i32
, 4, false), "");
2844 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2845 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
2847 args
[0] = ctx
->esgs_ring
;
2848 args
[1] = vtx_offset
;
2849 args
[2] = LLVMConstInt(ctx
->i32
, (param
* 4 + i
+ const_index
) * 256, false);
2850 args
[3] = ctx
->i32zero
;
2851 args
[4] = ctx
->i32one
; /* OFFEN */
2852 args
[5] = ctx
->i32zero
; /* IDXEN */
2853 args
[6] = ctx
->i32one
; /* GLC */
2854 args
[7] = ctx
->i32zero
; /* SLC */
2855 args
[8] = ctx
->i32zero
; /* TFE */
2857 value
[i
] = ac_build_intrinsic(&ctx
->ac
, "llvm.SI.buffer.load.dword.i32.i32",
2859 AC_FUNC_ATTR_READONLY
|
2860 AC_FUNC_ATTR_LEGACY
);
2862 result
= ac_build_gather_values(&ctx
->ac
, value
, instr
->num_components
);
2867 static LLVMValueRef
visit_load_var(struct nir_to_llvm_context
*ctx
,
2868 nir_intrinsic_instr
*instr
)
2870 LLVMValueRef values
[8];
2871 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2872 int ve
= instr
->dest
.ssa
.num_components
;
2873 LLVMValueRef indir_index
;
2875 unsigned const_index
;
2876 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
2877 instr
->variables
[0]->var
->data
.mode
== nir_var_shader_in
;
2878 radv_get_deref_offset(ctx
, instr
->variables
[0], vs_in
, NULL
, NULL
,
2879 &const_index
, &indir_index
);
2881 if (instr
->dest
.ssa
.bit_size
== 64)
2884 switch (instr
->variables
[0]->var
->data
.mode
) {
2885 case nir_var_shader_in
:
2886 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
2887 return load_tcs_input(ctx
, instr
);
2888 if (ctx
->stage
== MESA_SHADER_TESS_EVAL
)
2889 return load_tes_input(ctx
, instr
);
2890 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
2891 return load_gs_input(ctx
, instr
);
2893 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2895 unsigned count
= glsl_count_attribute_slots(
2896 instr
->variables
[0]->var
->type
,
2897 ctx
->stage
== MESA_SHADER_VERTEX
);
2899 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2900 &ctx
->ac
, ctx
->inputs
+ idx
+ chan
, count
,
2903 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2907 values
[chan
] = ctx
->inputs
[idx
+ chan
+ const_index
* 4];
2911 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2913 unsigned count
= glsl_count_attribute_slots(
2914 instr
->variables
[0]->var
->type
, false);
2916 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2917 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2920 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2924 values
[chan
] = LLVMBuildLoad(ctx
->builder
, ctx
->locals
[idx
+ chan
+ const_index
* 4], "");
2928 case nir_var_shader_out
:
2929 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
2930 return load_tcs_output(ctx
, instr
);
2931 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2933 unsigned count
= glsl_count_attribute_slots(
2934 instr
->variables
[0]->var
->type
, false);
2936 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2937 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
2940 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2944 values
[chan
] = LLVMBuildLoad(ctx
->builder
,
2945 ctx
->outputs
[idx
+ chan
+ const_index
* 4],
2950 case nir_var_shared
: {
2951 LLVMValueRef ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
2952 LLVMValueRef derived_ptr
;
2955 indir_index
= LLVMBuildMul(ctx
->builder
, indir_index
, LLVMConstInt(ctx
->i32
, 4, false), "");
2957 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2958 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, chan
, false);
2960 index
= LLVMBuildAdd(ctx
->builder
, index
, indir_index
, "");
2961 derived_ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, "");
2963 values
[chan
] = LLVMBuildLoad(ctx
->builder
, derived_ptr
, "");
2968 unreachable("unhandle variable mode");
2970 ret
= ac_build_gather_values(&ctx
->ac
, values
, ve
);
2971 return LLVMBuildBitCast(ctx
->builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2975 visit_store_var(struct nir_to_llvm_context
*ctx
,
2976 nir_intrinsic_instr
*instr
)
2978 LLVMValueRef temp_ptr
, value
;
2979 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2980 LLVMValueRef src
= to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
2981 int writemask
= instr
->const_index
[0];
2982 LLVMValueRef indir_index
;
2983 unsigned const_index
;
2984 radv_get_deref_offset(ctx
, instr
->variables
[0], false,
2985 NULL
, NULL
, &const_index
, &indir_index
);
2987 if (get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64) {
2988 int old_writemask
= writemask
;
2990 src
= LLVMBuildBitCast(ctx
->builder
, src
,
2991 LLVMVectorType(ctx
->f32
, get_llvm_num_components(src
) * 2),
2995 for (unsigned chan
= 0; chan
< 4; chan
++) {
2996 if (old_writemask
& (1 << chan
))
2997 writemask
|= 3u << (2 * chan
);
3001 switch (instr
->variables
[0]->var
->data
.mode
) {
3002 case nir_var_shader_out
:
3004 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3005 store_tcs_output(ctx
, instr
, src
, writemask
);
3009 for (unsigned chan
= 0; chan
< 8; chan
++) {
3011 if (!(writemask
& (1 << chan
)))
3014 value
= llvm_extract_elem(ctx
, src
, chan
);
3016 if (instr
->variables
[0]->var
->data
.compact
)
3019 unsigned count
= glsl_count_attribute_slots(
3020 instr
->variables
[0]->var
->type
, false);
3022 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3023 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
3026 if (get_llvm_num_components(tmp_vec
) > 1) {
3027 tmp_vec
= LLVMBuildInsertElement(ctx
->builder
, tmp_vec
,
3028 value
, indir_index
, "");
3031 build_store_values_extended(ctx
, ctx
->outputs
+ idx
+ chan
,
3032 count
, stride
, tmp_vec
);
3035 temp_ptr
= ctx
->outputs
[idx
+ chan
+ const_index
* stride
];
3037 LLVMBuildStore(ctx
->builder
, value
, temp_ptr
);
3042 for (unsigned chan
= 0; chan
< 8; chan
++) {
3043 if (!(writemask
& (1 << chan
)))
3046 value
= llvm_extract_elem(ctx
, src
, chan
);
3048 unsigned count
= glsl_count_attribute_slots(
3049 instr
->variables
[0]->var
->type
, false);
3051 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
3052 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
3055 tmp_vec
= LLVMBuildInsertElement(ctx
->builder
, tmp_vec
,
3056 value
, indir_index
, "");
3057 build_store_values_extended(ctx
, ctx
->locals
+ idx
+ chan
,
3060 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
3062 LLVMBuildStore(ctx
->builder
, value
, temp_ptr
);
3066 case nir_var_shared
: {
3067 LLVMValueRef ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
3070 indir_index
= LLVMBuildMul(ctx
->builder
, indir_index
, LLVMConstInt(ctx
->i32
, 4, false), "");
3072 for (unsigned chan
= 0; chan
< 8; chan
++) {
3073 if (!(writemask
& (1 << chan
)))
3075 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, chan
, false);
3076 LLVMValueRef derived_ptr
;
3079 index
= LLVMBuildAdd(ctx
->builder
, index
, indir_index
, "");
3081 value
= llvm_extract_elem(ctx
, src
, chan
);
3082 derived_ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, "");
3083 LLVMBuildStore(ctx
->builder
,
3084 to_integer(&ctx
->ac
, value
), derived_ptr
);
3093 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
3096 case GLSL_SAMPLER_DIM_BUF
:
3098 case GLSL_SAMPLER_DIM_1D
:
3099 return array
? 2 : 1;
3100 case GLSL_SAMPLER_DIM_2D
:
3101 return array
? 3 : 2;
3102 case GLSL_SAMPLER_DIM_MS
:
3103 return array
? 4 : 3;
3104 case GLSL_SAMPLER_DIM_3D
:
3105 case GLSL_SAMPLER_DIM_CUBE
:
3107 case GLSL_SAMPLER_DIM_RECT
:
3108 case GLSL_SAMPLER_DIM_SUBPASS
:
3110 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
3120 /* Adjust the sample index according to FMASK.
3122 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3123 * which is the identity mapping. Each nibble says which physical sample
3124 * should be fetched to get that sample.
3126 * For example, 0x11111100 means there are only 2 samples stored and
3127 * the second sample covers 3/4 of the pixel. When reading samples 0
3128 * and 1, return physical sample 0 (determined by the first two 0s
3129 * in FMASK), otherwise return physical sample 1.
3131 * The sample index should be adjusted as follows:
3132 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
3134 static LLVMValueRef
adjust_sample_index_using_fmask(struct nir_to_llvm_context
*ctx
,
3135 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
3136 LLVMValueRef coord_z
,
3137 LLVMValueRef sample_index
,
3138 LLVMValueRef fmask_desc_ptr
)
3140 LLVMValueRef fmask_load_address
[4];
3143 fmask_load_address
[0] = coord_x
;
3144 fmask_load_address
[1] = coord_y
;
3146 fmask_load_address
[2] = coord_z
;
3147 fmask_load_address
[3] = LLVMGetUndef(ctx
->i32
);
3150 struct ac_image_args args
= {0};
3152 args
.opcode
= ac_image_load
;
3153 args
.da
= coord_z
? true : false;
3154 args
.resource
= fmask_desc_ptr
;
3156 args
.addr
= ac_build_gather_values(&ctx
->ac
, fmask_load_address
, coord_z
? 4 : 2);
3158 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
3160 res
= to_integer(&ctx
->ac
, res
);
3161 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
3162 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
3164 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
3168 LLVMValueRef sample_index4
=
3169 LLVMBuildMul(ctx
->builder
, sample_index
, four
, "");
3170 LLVMValueRef shifted_fmask
=
3171 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
3172 LLVMValueRef final_sample
=
3173 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
3175 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3176 * resource descriptor is 0 (invalid),
3178 LLVMValueRef fmask_desc
=
3179 LLVMBuildBitCast(ctx
->builder
, fmask_desc_ptr
,
3182 LLVMValueRef fmask_word1
=
3183 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
3186 LLVMValueRef word1_is_nonzero
=
3187 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3188 fmask_word1
, ctx
->i32zero
, "");
3190 /* Replace the MSAA sample index. */
3192 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
3193 final_sample
, sample_index
, "");
3194 return sample_index
;
3197 static LLVMValueRef
get_image_coords(struct nir_to_llvm_context
*ctx
,
3198 const nir_intrinsic_instr
*instr
)
3200 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
3201 if(instr
->variables
[0]->deref
.child
)
3202 type
= instr
->variables
[0]->deref
.child
->type
;
3204 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
3205 LLVMValueRef coords
[4];
3206 LLVMValueRef masks
[] = {
3207 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
3208 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false),
3211 LLVMValueRef sample_index
= llvm_extract_elem(ctx
, get_src(ctx
, instr
->src
[1]), 0);
3214 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
3215 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3216 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3217 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
3218 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3220 count
= image_type_to_components_count(dim
,
3221 glsl_sampler_type_is_array(type
));
3224 LLVMValueRef fmask_load_address
[3];
3227 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[0], "");
3228 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[1], "");
3229 if (glsl_sampler_type_is_array(type
))
3230 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[2], "");
3232 fmask_load_address
[2] = NULL
;
3234 for (chan
= 0; chan
< 2; ++chan
)
3235 fmask_load_address
[chan
] = LLVMBuildAdd(ctx
->builder
, fmask_load_address
[chan
], LLVMBuildFPToUI(ctx
->builder
, ctx
->frag_pos
[chan
], ctx
->i32
, ""), "");
3237 sample_index
= adjust_sample_index_using_fmask(ctx
,
3238 fmask_load_address
[0],
3239 fmask_load_address
[1],
3240 fmask_load_address
[2],
3242 get_sampler_desc(ctx
, instr
->variables
[0], DESC_FMASK
));
3245 if (instr
->src
[0].ssa
->num_components
)
3246 res
= LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[0], "");
3253 for (chan
= 0; chan
< count
; ++chan
) {
3254 coords
[chan
] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[chan
], "");
3258 for (chan
= 0; chan
< count
; ++chan
)
3259 coords
[chan
] = LLVMBuildAdd(ctx
->builder
, coords
[chan
], LLVMBuildFPToUI(ctx
->builder
, ctx
->frag_pos
[chan
], ctx
->i32
, ""), "");
3262 coords
[count
] = sample_index
;
3267 coords
[3] = LLVMGetUndef(ctx
->i32
);
3270 res
= ac_build_gather_values(&ctx
->ac
, coords
, count
);
3275 static LLVMValueRef
visit_image_load(struct nir_to_llvm_context
*ctx
,
3276 const nir_intrinsic_instr
*instr
)
3278 LLVMValueRef params
[7];
3280 char intrinsic_name
[64];
3281 const nir_variable
*var
= instr
->variables
[0]->var
;
3282 const struct glsl_type
*type
= var
->type
;
3283 if(instr
->variables
[0]->deref
.child
)
3284 type
= instr
->variables
[0]->deref
.child
->type
;
3286 type
= glsl_without_array(type
);
3287 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3288 params
[0] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
3289 params
[1] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
3290 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
3291 params
[2] = LLVMConstInt(ctx
->i32
, 0, false); /* voffset */
3292 params
[3] = ctx
->i1false
; /* glc */
3293 params
[4] = ctx
->i1false
; /* slc */
3294 res
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.load.format.v4f32", ctx
->v4f32
,
3297 res
= trim_vector(ctx
, res
, instr
->dest
.ssa
.num_components
);
3298 res
= to_integer(&ctx
->ac
, res
);
3300 bool is_da
= glsl_sampler_type_is_array(type
) ||
3301 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3302 LLVMValueRef da
= is_da
? ctx
->i1true
: ctx
->i1false
;
3303 LLVMValueRef glc
= ctx
->i1false
;
3304 LLVMValueRef slc
= ctx
->i1false
;
3306 params
[0] = get_image_coords(ctx
, instr
);
3307 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
3308 params
[2] = LLVMConstInt(ctx
->i32
, 15, false); /* dmask */
3309 if (HAVE_LLVM
<= 0x0309) {
3310 params
[3] = ctx
->i1false
; /* r128 */
3315 LLVMValueRef lwe
= ctx
->i1false
;
3322 ac_get_image_intr_name("llvm.amdgcn.image.load",
3323 ctx
->v4f32
, /* vdata */
3324 LLVMTypeOf(params
[0]), /* coords */
3325 LLVMTypeOf(params
[1]), /* rsrc */
3326 intrinsic_name
, sizeof(intrinsic_name
));
3328 res
= ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->v4f32
,
3329 params
, 7, AC_FUNC_ATTR_READONLY
);
3331 return to_integer(&ctx
->ac
, res
);
3334 static void visit_image_store(struct nir_to_llvm_context
*ctx
,
3335 nir_intrinsic_instr
*instr
)
3337 LLVMValueRef params
[8];
3338 char intrinsic_name
[64];
3339 const nir_variable
*var
= instr
->variables
[0]->var
;
3340 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3342 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
3343 ctx
->shader_info
->fs
.writes_memory
= true;
3345 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3346 params
[0] = to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2])); /* data */
3347 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
3348 params
[2] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
3349 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
3350 params
[3] = LLVMConstInt(ctx
->i32
, 0, false); /* voffset */
3351 params
[4] = ctx
->i1false
; /* glc */
3352 params
[5] = ctx
->i1false
; /* slc */
3353 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->voidt
,
3356 bool is_da
= glsl_sampler_type_is_array(type
) ||
3357 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3358 LLVMValueRef da
= is_da
? ctx
->i1true
: ctx
->i1false
;
3359 LLVMValueRef glc
= ctx
->i1false
;
3360 LLVMValueRef slc
= ctx
->i1false
;
3362 params
[0] = to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2]));
3363 params
[1] = get_image_coords(ctx
, instr
); /* coords */
3364 params
[2] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
3365 params
[3] = LLVMConstInt(ctx
->i32
, 15, false); /* dmask */
3366 if (HAVE_LLVM
<= 0x0309) {
3367 params
[4] = ctx
->i1false
; /* r128 */
3372 LLVMValueRef lwe
= ctx
->i1false
;
3379 ac_get_image_intr_name("llvm.amdgcn.image.store",
3380 LLVMTypeOf(params
[0]), /* vdata */
3381 LLVMTypeOf(params
[1]), /* coords */
3382 LLVMTypeOf(params
[2]), /* rsrc */
3383 intrinsic_name
, sizeof(intrinsic_name
));
3385 ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->voidt
,
3391 static LLVMValueRef
visit_image_atomic(struct nir_to_llvm_context
*ctx
,
3392 const nir_intrinsic_instr
*instr
)
3394 LLVMValueRef params
[6];
3395 int param_count
= 0;
3396 const nir_variable
*var
= instr
->variables
[0]->var
;
3398 const char *base_name
= "llvm.amdgcn.image.atomic";
3399 const char *atomic_name
;
3400 LLVMValueRef coords
;
3401 char intrinsic_name
[32], coords_type
[8];
3402 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3404 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
3405 ctx
->shader_info
->fs
.writes_memory
= true;
3407 params
[param_count
++] = get_src(ctx
, instr
->src
[2]);
3408 if (instr
->intrinsic
== nir_intrinsic_image_atomic_comp_swap
)
3409 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
3411 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3412 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
3413 coords
= params
[param_count
++] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
3414 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
3415 params
[param_count
++] = ctx
->i32zero
; /* voffset */
3416 params
[param_count
++] = ctx
->i1false
; /* glc */
3417 params
[param_count
++] = ctx
->i1false
; /* slc */
3419 bool da
= glsl_sampler_type_is_array(type
) ||
3420 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3422 coords
= params
[param_count
++] = get_image_coords(ctx
, instr
);
3423 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
3424 params
[param_count
++] = ctx
->i1false
; /* r128 */
3425 params
[param_count
++] = da
? ctx
->i1true
: ctx
->i1false
; /* da */
3426 params
[param_count
++] = ctx
->i1false
; /* slc */
3429 switch (instr
->intrinsic
) {
3430 case nir_intrinsic_image_atomic_add
:
3431 atomic_name
= "add";
3433 case nir_intrinsic_image_atomic_min
:
3434 atomic_name
= "smin";
3436 case nir_intrinsic_image_atomic_max
:
3437 atomic_name
= "smax";
3439 case nir_intrinsic_image_atomic_and
:
3440 atomic_name
= "and";
3442 case nir_intrinsic_image_atomic_or
:
3445 case nir_intrinsic_image_atomic_xor
:
3446 atomic_name
= "xor";
3448 case nir_intrinsic_image_atomic_exchange
:
3449 atomic_name
= "swap";
3451 case nir_intrinsic_image_atomic_comp_swap
:
3452 atomic_name
= "cmpswap";
3457 build_int_type_name(LLVMTypeOf(coords
),
3458 coords_type
, sizeof(coords_type
));
3460 snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3461 "%s.%s.%s", base_name
, atomic_name
, coords_type
);
3462 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->i32
, params
, param_count
, 0);
3465 static LLVMValueRef
visit_image_size(struct nir_to_llvm_context
*ctx
,
3466 const nir_intrinsic_instr
*instr
)
3469 const nir_variable
*var
= instr
->variables
[0]->var
;
3470 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
3471 bool da
= glsl_sampler_type_is_array(var
->type
) ||
3472 glsl_get_sampler_dim(var
->type
) == GLSL_SAMPLER_DIM_CUBE
;
3473 if(instr
->variables
[0]->deref
.child
)
3474 type
= instr
->variables
[0]->deref
.child
->type
;
3476 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
3477 return get_buffer_size(ctx
, get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
), true);
3479 struct ac_image_args args
= { 0 };
3483 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
3484 args
.opcode
= ac_image_get_resinfo
;
3485 args
.addr
= ctx
->i32zero
;
3487 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
3489 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
3490 glsl_sampler_type_is_array(type
)) {
3491 LLVMValueRef two
= LLVMConstInt(ctx
->i32
, 2, false);
3492 LLVMValueRef six
= LLVMConstInt(ctx
->i32
, 6, false);
3493 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->builder
, res
, two
, "");
3494 z
= LLVMBuildSDiv(ctx
->builder
, z
, six
, "");
3495 res
= LLVMBuildInsertElement(ctx
->builder
, res
, z
, two
, "");
3500 #define NOOP_WAITCNT 0xf7f
3501 #define LGKM_CNT 0x07f
3502 #define VM_CNT 0xf70
3504 static void emit_waitcnt(struct nir_to_llvm_context
*ctx
,
3507 LLVMValueRef args
[1] = {
3508 LLVMConstInt(ctx
->i32
, simm16
, false),
3510 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.s.waitcnt",
3511 ctx
->voidt
, args
, 1, 0);
3514 static void emit_barrier(struct nir_to_llvm_context
*ctx
)
3516 /* SI only (thanks to a hw bug workaround):
3517 * The real barrier instruction isn’t needed, because an entire patch
3518 * always fits into a single wave.
3520 if (ctx
->options
->chip_class
== SI
&&
3521 ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3522 emit_waitcnt(ctx
, LGKM_CNT
& VM_CNT
);
3525 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.s.barrier",
3526 ctx
->voidt
, NULL
, 0, AC_FUNC_ATTR_CONVERGENT
);
3529 static void emit_discard_if(struct nir_to_llvm_context
*ctx
,
3530 const nir_intrinsic_instr
*instr
)
3533 ctx
->shader_info
->fs
.can_discard
= true;
3535 cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3536 get_src(ctx
, instr
->src
[0]),
3539 cond
= LLVMBuildSelect(ctx
->builder
, cond
,
3540 LLVMConstReal(ctx
->f32
, -1.0f
),
3542 ac_build_kill(&ctx
->ac
, cond
);
3546 visit_load_local_invocation_index(struct nir_to_llvm_context
*ctx
)
3548 LLVMValueRef result
;
3549 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
3550 result
= LLVMBuildAnd(ctx
->builder
, ctx
->tg_size
,
3551 LLVMConstInt(ctx
->i32
, 0xfc0, false), "");
3553 return LLVMBuildAdd(ctx
->builder
, result
, thread_id
, "");
3556 static LLVMValueRef
visit_var_atomic(struct nir_to_llvm_context
*ctx
,
3557 const nir_intrinsic_instr
*instr
)
3559 LLVMValueRef ptr
, result
;
3560 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
3561 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
3562 ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
3564 if (instr
->intrinsic
== nir_intrinsic_var_atomic_comp_swap
) {
3565 LLVMValueRef src1
= get_src(ctx
, instr
->src
[1]);
3566 result
= LLVMBuildAtomicCmpXchg(ctx
->builder
,
3568 LLVMAtomicOrderingSequentiallyConsistent
,
3569 LLVMAtomicOrderingSequentiallyConsistent
,
3572 LLVMAtomicRMWBinOp op
;
3573 switch (instr
->intrinsic
) {
3574 case nir_intrinsic_var_atomic_add
:
3575 op
= LLVMAtomicRMWBinOpAdd
;
3577 case nir_intrinsic_var_atomic_umin
:
3578 op
= LLVMAtomicRMWBinOpUMin
;
3580 case nir_intrinsic_var_atomic_umax
:
3581 op
= LLVMAtomicRMWBinOpUMax
;
3583 case nir_intrinsic_var_atomic_imin
:
3584 op
= LLVMAtomicRMWBinOpMin
;
3586 case nir_intrinsic_var_atomic_imax
:
3587 op
= LLVMAtomicRMWBinOpMax
;
3589 case nir_intrinsic_var_atomic_and
:
3590 op
= LLVMAtomicRMWBinOpAnd
;
3592 case nir_intrinsic_var_atomic_or
:
3593 op
= LLVMAtomicRMWBinOpOr
;
3595 case nir_intrinsic_var_atomic_xor
:
3596 op
= LLVMAtomicRMWBinOpXor
;
3598 case nir_intrinsic_var_atomic_exchange
:
3599 op
= LLVMAtomicRMWBinOpXchg
;
3605 result
= LLVMBuildAtomicRMW(ctx
->builder
, op
, ptr
, to_integer(&ctx
->ac
, src
),
3606 LLVMAtomicOrderingSequentiallyConsistent
,
3612 #define INTERP_CENTER 0
3613 #define INTERP_CENTROID 1
3614 #define INTERP_SAMPLE 2
3616 static LLVMValueRef
lookup_interp_param(struct nir_to_llvm_context
*ctx
,
3617 enum glsl_interp_mode interp
, unsigned location
)
3620 case INTERP_MODE_FLAT
:
3623 case INTERP_MODE_SMOOTH
:
3624 case INTERP_MODE_NONE
:
3625 if (location
== INTERP_CENTER
)
3626 return ctx
->persp_center
;
3627 else if (location
== INTERP_CENTROID
)
3628 return ctx
->persp_centroid
;
3629 else if (location
== INTERP_SAMPLE
)
3630 return ctx
->persp_sample
;
3632 case INTERP_MODE_NOPERSPECTIVE
:
3633 if (location
== INTERP_CENTER
)
3634 return ctx
->linear_center
;
3635 else if (location
== INTERP_CENTROID
)
3636 return ctx
->linear_centroid
;
3637 else if (location
== INTERP_SAMPLE
)
3638 return ctx
->linear_sample
;
3644 static LLVMValueRef
load_sample_position(struct nir_to_llvm_context
*ctx
,
3645 LLVMValueRef sample_id
)
3647 LLVMValueRef result
;
3648 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_PS_SAMPLE_POSITIONS
, false));
3650 ptr
= LLVMBuildBitCast(ctx
->builder
, ptr
,
3651 const_array(ctx
->v2f32
, 64), "");
3653 sample_id
= LLVMBuildAdd(ctx
->builder
, sample_id
, ctx
->sample_pos_offset
, "");
3654 result
= ac_build_indexed_load(&ctx
->ac
, ptr
, sample_id
, false);
3659 static LLVMValueRef
load_sample_pos(struct nir_to_llvm_context
*ctx
)
3661 LLVMValueRef values
[2];
3663 values
[0] = emit_ffract(ctx
, ctx
->frag_pos
[0]);
3664 values
[1] = emit_ffract(ctx
, ctx
->frag_pos
[1]);
3665 return ac_build_gather_values(&ctx
->ac
, values
, 2);
3668 static LLVMValueRef
visit_interp(struct nir_to_llvm_context
*ctx
,
3669 const nir_intrinsic_instr
*instr
)
3671 LLVMValueRef result
[2];
3672 LLVMValueRef interp_param
, attr_number
;
3675 LLVMValueRef src_c0
, src_c1
;
3677 int input_index
= instr
->variables
[0]->var
->data
.location
- VARYING_SLOT_VAR0
;
3678 switch (instr
->intrinsic
) {
3679 case nir_intrinsic_interp_var_at_centroid
:
3680 location
= INTERP_CENTROID
;
3682 case nir_intrinsic_interp_var_at_sample
:
3683 case nir_intrinsic_interp_var_at_offset
:
3684 location
= INTERP_CENTER
;
3685 src0
= get_src(ctx
, instr
->src
[0]);
3691 if (instr
->intrinsic
== nir_intrinsic_interp_var_at_offset
) {
3692 src_c0
= to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32zero
, ""));
3693 src_c1
= to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32one
, ""));
3694 } else if (instr
->intrinsic
== nir_intrinsic_interp_var_at_sample
) {
3695 LLVMValueRef sample_position
;
3696 LLVMValueRef halfval
= LLVMConstReal(ctx
->f32
, 0.5f
);
3698 /* fetch sample ID */
3699 sample_position
= load_sample_position(ctx
, src0
);
3701 src_c0
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->i32zero
, "");
3702 src_c0
= LLVMBuildFSub(ctx
->builder
, src_c0
, halfval
, "");
3703 src_c1
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->i32one
, "");
3704 src_c1
= LLVMBuildFSub(ctx
->builder
, src_c1
, halfval
, "");
3706 interp_param
= lookup_interp_param(ctx
, instr
->variables
[0]->var
->data
.interpolation
, location
);
3707 attr_number
= LLVMConstInt(ctx
->i32
, input_index
, false);
3709 if (location
== INTERP_SAMPLE
|| location
== INTERP_CENTER
) {
3710 LLVMValueRef ij_out
[2];
3711 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
, interp_param
);
3714 * take the I then J parameters, and the DDX/Y for it, and
3715 * calculate the IJ inputs for the interpolator.
3716 * temp1 = ddx * offset/sample.x + I;
3717 * interp_param.I = ddy * offset/sample.y + temp1;
3718 * temp1 = ddx * offset/sample.x + J;
3719 * interp_param.J = ddy * offset/sample.y + temp1;
3721 for (unsigned i
= 0; i
< 2; i
++) {
3722 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->i32
, i
, false);
3723 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->i32
, i
+ 2, false);
3724 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->builder
,
3725 ddxy_out
, ix_ll
, "");
3726 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->builder
,
3727 ddxy_out
, iy_ll
, "");
3728 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->builder
,
3729 interp_param
, ix_ll
, "");
3730 LLVMValueRef temp1
, temp2
;
3732 interp_el
= LLVMBuildBitCast(ctx
->builder
, interp_el
,
3735 temp1
= LLVMBuildFMul(ctx
->builder
, ddx_el
, src_c0
, "");
3736 temp1
= LLVMBuildFAdd(ctx
->builder
, temp1
, interp_el
, "");
3738 temp2
= LLVMBuildFMul(ctx
->builder
, ddy_el
, src_c1
, "");
3739 temp2
= LLVMBuildFAdd(ctx
->builder
, temp2
, temp1
, "");
3741 ij_out
[i
] = LLVMBuildBitCast(ctx
->builder
,
3742 temp2
, ctx
->i32
, "");
3744 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
3748 for (chan
= 0; chan
< 2; chan
++) {
3749 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
3752 interp_param
= LLVMBuildBitCast(ctx
->builder
,
3753 interp_param
, LLVMVectorType(ctx
->f32
, 2), "");
3754 LLVMValueRef i
= LLVMBuildExtractElement(
3755 ctx
->builder
, interp_param
, ctx
->i32zero
, "");
3756 LLVMValueRef j
= LLVMBuildExtractElement(
3757 ctx
->builder
, interp_param
, ctx
->i32one
, "");
3759 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
3760 llvm_chan
, attr_number
,
3761 ctx
->prim_mask
, i
, j
);
3763 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
3764 LLVMConstInt(ctx
->i32
, 2, false),
3765 llvm_chan
, attr_number
,
3769 return ac_build_gather_values(&ctx
->ac
, result
, 2);
3773 visit_emit_vertex(struct nir_to_llvm_context
*ctx
,
3774 const nir_intrinsic_instr
*instr
)
3776 LLVMValueRef gs_next_vertex
;
3777 LLVMValueRef can_emit
, kill
;
3780 assert(instr
->const_index
[0] == 0);
3781 /* Write vertex attribute values to GSVS ring */
3782 gs_next_vertex
= LLVMBuildLoad(ctx
->builder
,
3783 ctx
->gs_next_vertex
,
3786 /* If this thread has already emitted the declared maximum number of
3787 * vertices, kill it: excessive vertex emissions are not supposed to
3788 * have any effect, and GS threads have no externally observable
3789 * effects other than emitting vertices.
3791 can_emit
= LLVMBuildICmp(ctx
->builder
, LLVMIntULT
, gs_next_vertex
,
3792 LLVMConstInt(ctx
->i32
, ctx
->gs_max_out_vertices
, false), "");
3794 kill
= LLVMBuildSelect(ctx
->builder
, can_emit
,
3795 LLVMConstReal(ctx
->f32
, 1.0f
),
3796 LLVMConstReal(ctx
->f32
, -1.0f
), "");
3797 ac_build_kill(&ctx
->ac
, kill
);
3799 /* loop num outputs */
3801 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
3802 LLVMValueRef
*out_ptr
= &ctx
->outputs
[i
* 4];
3807 if (!(ctx
->output_mask
& (1ull << i
)))
3810 if (i
== VARYING_SLOT_CLIP_DIST0
) {
3811 /* pack clip and cull into a single set of slots */
3812 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
3816 for (unsigned j
= 0; j
< length
; j
++) {
3817 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
,
3819 LLVMValueRef voffset
= LLVMConstInt(ctx
->i32
, (slot
* 4 + j
) * ctx
->gs_max_out_vertices
, false);
3820 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, gs_next_vertex
, "");
3821 voffset
= LLVMBuildMul(ctx
->builder
, voffset
, LLVMConstInt(ctx
->i32
, 4, false), "");
3823 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->i32
, "");
3825 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->gsvs_ring
,
3827 voffset
, ctx
->gs2vs_offset
, 0,
3833 gs_next_vertex
= LLVMBuildAdd(ctx
->builder
, gs_next_vertex
,
3835 LLVMBuildStore(ctx
->builder
, gs_next_vertex
, ctx
->gs_next_vertex
);
3837 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_EMIT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
3841 visit_end_primitive(struct nir_to_llvm_context
*ctx
,
3842 const nir_intrinsic_instr
*instr
)
3844 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_CUT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
3848 visit_load_tess_coord(struct nir_to_llvm_context
*ctx
,
3849 const nir_intrinsic_instr
*instr
)
3851 LLVMValueRef coord
[4] = {
3858 if (ctx
->tes_primitive_mode
== GL_TRIANGLES
)
3859 coord
[2] = LLVMBuildFSub(ctx
->builder
, ctx
->f32one
,
3860 LLVMBuildFAdd(ctx
->builder
, coord
[0], coord
[1], ""), "");
3862 LLVMValueRef result
= ac_build_gather_values(&ctx
->ac
, coord
, instr
->num_components
);
3863 return LLVMBuildBitCast(ctx
->builder
, result
,
3864 get_def_type(ctx
, &instr
->dest
.ssa
), "");
3867 static void visit_intrinsic(struct nir_to_llvm_context
*ctx
,
3868 nir_intrinsic_instr
*instr
)
3870 LLVMValueRef result
= NULL
;
3872 switch (instr
->intrinsic
) {
3873 case nir_intrinsic_load_work_group_id
: {
3874 result
= ctx
->workgroup_ids
;
3877 case nir_intrinsic_load_base_vertex
: {
3878 result
= ctx
->base_vertex
;
3881 case nir_intrinsic_load_vertex_id_zero_base
: {
3882 result
= ctx
->vertex_id
;
3885 case nir_intrinsic_load_local_invocation_id
: {
3886 result
= ctx
->local_invocation_ids
;
3889 case nir_intrinsic_load_base_instance
:
3890 result
= ctx
->start_instance
;
3892 case nir_intrinsic_load_draw_id
:
3893 result
= ctx
->draw_index
;
3895 case nir_intrinsic_load_invocation_id
:
3896 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
3897 result
= unpack_param(ctx
, ctx
->tcs_rel_ids
, 8, 5);
3899 result
= ctx
->gs_invocation_id
;
3901 case nir_intrinsic_load_primitive_id
:
3902 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
3903 ctx
->shader_info
->gs
.uses_prim_id
= true;
3904 result
= ctx
->gs_prim_id
;
3905 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3906 ctx
->shader_info
->tcs
.uses_prim_id
= true;
3907 result
= ctx
->tcs_patch_id
;
3908 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
3909 ctx
->shader_info
->tcs
.uses_prim_id
= true;
3910 result
= ctx
->tes_patch_id
;
3912 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
3914 case nir_intrinsic_load_sample_id
:
3915 ctx
->shader_info
->fs
.force_persample
= true;
3916 result
= unpack_param(ctx
, ctx
->ancillary
, 8, 4);
3918 case nir_intrinsic_load_sample_pos
:
3919 ctx
->shader_info
->fs
.force_persample
= true;
3920 result
= load_sample_pos(ctx
);
3922 case nir_intrinsic_load_sample_mask_in
:
3923 result
= ctx
->sample_coverage
;
3925 case nir_intrinsic_load_front_face
:
3926 result
= ctx
->front_face
;
3928 case nir_intrinsic_load_instance_id
:
3929 result
= ctx
->instance_id
;
3930 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
3931 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
3933 case nir_intrinsic_load_num_work_groups
:
3934 result
= ctx
->num_work_groups
;
3936 case nir_intrinsic_load_local_invocation_index
:
3937 result
= visit_load_local_invocation_index(ctx
);
3939 case nir_intrinsic_load_push_constant
:
3940 result
= visit_load_push_constant(ctx
, instr
);
3942 case nir_intrinsic_vulkan_resource_index
:
3943 result
= visit_vulkan_resource_index(ctx
, instr
);
3945 case nir_intrinsic_store_ssbo
:
3946 visit_store_ssbo(ctx
, instr
);
3948 case nir_intrinsic_load_ssbo
:
3949 result
= visit_load_buffer(ctx
, instr
);
3951 case nir_intrinsic_ssbo_atomic_add
:
3952 case nir_intrinsic_ssbo_atomic_imin
:
3953 case nir_intrinsic_ssbo_atomic_umin
:
3954 case nir_intrinsic_ssbo_atomic_imax
:
3955 case nir_intrinsic_ssbo_atomic_umax
:
3956 case nir_intrinsic_ssbo_atomic_and
:
3957 case nir_intrinsic_ssbo_atomic_or
:
3958 case nir_intrinsic_ssbo_atomic_xor
:
3959 case nir_intrinsic_ssbo_atomic_exchange
:
3960 case nir_intrinsic_ssbo_atomic_comp_swap
:
3961 result
= visit_atomic_ssbo(ctx
, instr
);
3963 case nir_intrinsic_load_ubo
:
3964 result
= visit_load_ubo_buffer(ctx
, instr
);
3966 case nir_intrinsic_get_buffer_size
:
3967 result
= visit_get_buffer_size(ctx
, instr
);
3969 case nir_intrinsic_load_var
:
3970 result
= visit_load_var(ctx
, instr
);
3972 case nir_intrinsic_store_var
:
3973 visit_store_var(ctx
, instr
);
3975 case nir_intrinsic_image_load
:
3976 result
= visit_image_load(ctx
, instr
);
3978 case nir_intrinsic_image_store
:
3979 visit_image_store(ctx
, instr
);
3981 case nir_intrinsic_image_atomic_add
:
3982 case nir_intrinsic_image_atomic_min
:
3983 case nir_intrinsic_image_atomic_max
:
3984 case nir_intrinsic_image_atomic_and
:
3985 case nir_intrinsic_image_atomic_or
:
3986 case nir_intrinsic_image_atomic_xor
:
3987 case nir_intrinsic_image_atomic_exchange
:
3988 case nir_intrinsic_image_atomic_comp_swap
:
3989 result
= visit_image_atomic(ctx
, instr
);
3991 case nir_intrinsic_image_size
:
3992 result
= visit_image_size(ctx
, instr
);
3994 case nir_intrinsic_discard
:
3995 ctx
->shader_info
->fs
.can_discard
= true;
3996 ac_build_intrinsic(&ctx
->ac
, "llvm.AMDGPU.kilp",
3998 NULL
, 0, AC_FUNC_ATTR_LEGACY
);
4000 case nir_intrinsic_discard_if
:
4001 emit_discard_if(ctx
, instr
);
4003 case nir_intrinsic_memory_barrier
:
4004 emit_waitcnt(ctx
, VM_CNT
);
4006 case nir_intrinsic_barrier
:
4009 case nir_intrinsic_var_atomic_add
:
4010 case nir_intrinsic_var_atomic_imin
:
4011 case nir_intrinsic_var_atomic_umin
:
4012 case nir_intrinsic_var_atomic_imax
:
4013 case nir_intrinsic_var_atomic_umax
:
4014 case nir_intrinsic_var_atomic_and
:
4015 case nir_intrinsic_var_atomic_or
:
4016 case nir_intrinsic_var_atomic_xor
:
4017 case nir_intrinsic_var_atomic_exchange
:
4018 case nir_intrinsic_var_atomic_comp_swap
:
4019 result
= visit_var_atomic(ctx
, instr
);
4021 case nir_intrinsic_interp_var_at_centroid
:
4022 case nir_intrinsic_interp_var_at_sample
:
4023 case nir_intrinsic_interp_var_at_offset
:
4024 result
= visit_interp(ctx
, instr
);
4026 case nir_intrinsic_emit_vertex
:
4027 visit_emit_vertex(ctx
, instr
);
4029 case nir_intrinsic_end_primitive
:
4030 visit_end_primitive(ctx
, instr
);
4032 case nir_intrinsic_load_tess_coord
:
4033 result
= visit_load_tess_coord(ctx
, instr
);
4035 case nir_intrinsic_load_patch_vertices_in
:
4036 result
= LLVMConstInt(ctx
->i32
, ctx
->options
->key
.tcs
.input_vertices
, false);
4039 fprintf(stderr
, "Unknown intrinsic: ");
4040 nir_print_instr(&instr
->instr
, stderr
);
4041 fprintf(stderr
, "\n");
4045 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4049 static LLVMValueRef
get_sampler_desc(struct nir_to_llvm_context
*ctx
,
4050 const nir_deref_var
*deref
,
4051 enum desc_type desc_type
)
4053 unsigned desc_set
= deref
->var
->data
.descriptor_set
;
4054 LLVMValueRef list
= ctx
->descriptor_sets
[desc_set
];
4055 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[desc_set
].layout
;
4056 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ deref
->var
->data
.binding
;
4057 unsigned offset
= binding
->offset
;
4058 unsigned stride
= binding
->size
;
4060 LLVMBuilderRef builder
= ctx
->builder
;
4062 LLVMValueRef index
= NULL
;
4063 unsigned constant_index
= 0;
4065 assert(deref
->var
->data
.binding
< layout
->binding_count
);
4067 switch (desc_type
) {
4079 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
4089 unreachable("invalid desc_type\n");
4092 if (deref
->deref
.child
) {
4093 const nir_deref_array
*child
=
4094 (const nir_deref_array
*)deref
->deref
.child
;
4096 assert(child
->deref_array_type
!= nir_deref_array_type_wildcard
);
4097 offset
+= child
->base_offset
* stride
;
4098 if (child
->deref_array_type
== nir_deref_array_type_indirect
) {
4099 index
= get_src(ctx
, child
->indirect
);
4102 constant_index
= child
->base_offset
;
4104 if (desc_type
== DESC_SAMPLER
&& binding
->immutable_samplers_offset
&&
4105 (!index
|| binding
->immutable_samplers_equal
)) {
4106 if (binding
->immutable_samplers_equal
)
4109 const uint32_t *samplers
= radv_immutable_samplers(layout
, binding
);
4111 LLVMValueRef constants
[] = {
4112 LLVMConstInt(ctx
->i32
, samplers
[constant_index
* 4 + 0], 0),
4113 LLVMConstInt(ctx
->i32
, samplers
[constant_index
* 4 + 1], 0),
4114 LLVMConstInt(ctx
->i32
, samplers
[constant_index
* 4 + 2], 0),
4115 LLVMConstInt(ctx
->i32
, samplers
[constant_index
* 4 + 3], 0),
4117 return ac_build_gather_values(&ctx
->ac
, constants
, 4);
4120 assert(stride
% type_size
== 0);
4123 index
= ctx
->i32zero
;
4125 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, stride
/ type_size
, 0), "");
4127 list
= ac_build_gep0(&ctx
->ac
, list
, LLVMConstInt(ctx
->i32
, offset
, 0));
4128 list
= LLVMBuildPointerCast(builder
, list
, const_array(type
, 0), "");
4130 return ac_build_indexed_load_const(&ctx
->ac
, list
, index
);
4133 static void set_tex_fetch_args(struct nir_to_llvm_context
*ctx
,
4134 struct ac_image_args
*args
,
4135 const nir_tex_instr
*instr
,
4137 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
4138 LLVMValueRef
*param
, unsigned count
,
4141 unsigned is_rect
= 0;
4142 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
4144 if (op
== nir_texop_lod
)
4146 /* Pad to power of two vector */
4147 while (count
< util_next_power_of_two(count
))
4148 param
[count
++] = LLVMGetUndef(ctx
->i32
);
4151 args
->addr
= ac_build_gather_values(&ctx
->ac
, param
, count
);
4153 args
->addr
= param
[0];
4155 args
->resource
= res_ptr
;
4156 args
->sampler
= samp_ptr
;
4158 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
&& op
== nir_texop_txf
) {
4159 args
->addr
= param
[0];
4163 args
->dmask
= dmask
;
4164 args
->unorm
= is_rect
;
4168 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
4171 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
4172 * filtering manually. The driver sets img7 to a mask clearing
4173 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
4174 * s_and_b32 samp0, samp0, img7
4177 * The ANISO_OVERRIDE sampler field enables this fix in TA.
4179 static LLVMValueRef
sici_fix_sampler_aniso(struct nir_to_llvm_context
*ctx
,
4180 LLVMValueRef res
, LLVMValueRef samp
)
4182 LLVMBuilderRef builder
= ctx
->builder
;
4183 LLVMValueRef img7
, samp0
;
4185 if (ctx
->options
->chip_class
>= VI
)
4188 img7
= LLVMBuildExtractElement(builder
, res
,
4189 LLVMConstInt(ctx
->i32
, 7, 0), "");
4190 samp0
= LLVMBuildExtractElement(builder
, samp
,
4191 LLVMConstInt(ctx
->i32
, 0, 0), "");
4192 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
4193 return LLVMBuildInsertElement(builder
, samp
, samp0
,
4194 LLVMConstInt(ctx
->i32
, 0, 0), "");
4197 static void tex_fetch_ptrs(struct nir_to_llvm_context
*ctx
,
4198 nir_tex_instr
*instr
,
4199 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
4200 LLVMValueRef
*fmask_ptr
)
4202 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
4203 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_BUFFER
);
4205 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_IMAGE
);
4208 *samp_ptr
= get_sampler_desc(ctx
, instr
->sampler
, DESC_SAMPLER
);
4210 *samp_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_SAMPLER
);
4211 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
4212 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
4214 if (fmask_ptr
&& !instr
->sampler
&& (instr
->op
== nir_texop_txf_ms
||
4215 instr
->op
== nir_texop_samples_identical
))
4216 *fmask_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_FMASK
);
4219 static LLVMValueRef
apply_round_slice(struct nir_to_llvm_context
*ctx
,
4222 coord
= to_float(&ctx
->ac
, coord
);
4223 coord
= ac_build_intrinsic(&ctx
->ac
, "llvm.rint.f32", ctx
->f32
, &coord
, 1, 0);
4224 coord
= to_integer(&ctx
->ac
, coord
);
4228 static void visit_tex(struct nir_to_llvm_context
*ctx
, nir_tex_instr
*instr
)
4230 LLVMValueRef result
= NULL
;
4231 struct ac_image_args args
= { 0 };
4232 unsigned dmask
= 0xf;
4233 LLVMValueRef address
[16];
4234 LLVMValueRef coords
[5];
4235 LLVMValueRef coord
= NULL
, lod
= NULL
, comparator
= NULL
;
4236 LLVMValueRef bias
= NULL
, offsets
= NULL
;
4237 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
, sample_index
= NULL
;
4238 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
4239 LLVMValueRef derivs
[6];
4240 unsigned chan
, count
= 0;
4241 unsigned const_src
= 0, num_deriv_comp
= 0;
4242 bool lod_is_zero
= false;
4243 tex_fetch_ptrs(ctx
, instr
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
4245 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
4246 switch (instr
->src
[i
].src_type
) {
4247 case nir_tex_src_coord
:
4248 coord
= get_src(ctx
, instr
->src
[i
].src
);
4250 case nir_tex_src_projector
:
4252 case nir_tex_src_comparator
:
4253 comparator
= get_src(ctx
, instr
->src
[i
].src
);
4255 case nir_tex_src_offset
:
4256 offsets
= get_src(ctx
, instr
->src
[i
].src
);
4259 case nir_tex_src_bias
:
4260 bias
= get_src(ctx
, instr
->src
[i
].src
);
4262 case nir_tex_src_lod
: {
4263 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
4265 if (val
&& val
->i32
[0] == 0)
4267 lod
= get_src(ctx
, instr
->src
[i
].src
);
4270 case nir_tex_src_ms_index
:
4271 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
4273 case nir_tex_src_ms_mcs
:
4275 case nir_tex_src_ddx
:
4276 ddx
= get_src(ctx
, instr
->src
[i
].src
);
4277 num_deriv_comp
= instr
->src
[i
].src
.ssa
->num_components
;
4279 case nir_tex_src_ddy
:
4280 ddy
= get_src(ctx
, instr
->src
[i
].src
);
4282 case nir_tex_src_texture_offset
:
4283 case nir_tex_src_sampler_offset
:
4284 case nir_tex_src_plane
:
4290 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
4291 result
= get_buffer_size(ctx
, res_ptr
, true);
4295 if (instr
->op
== nir_texop_texture_samples
) {
4296 LLVMValueRef res
, samples
, is_msaa
;
4297 res
= LLVMBuildBitCast(ctx
->builder
, res_ptr
, ctx
->v8i32
, "");
4298 samples
= LLVMBuildExtractElement(ctx
->builder
, res
,
4299 LLVMConstInt(ctx
->i32
, 3, false), "");
4300 is_msaa
= LLVMBuildLShr(ctx
->builder
, samples
,
4301 LLVMConstInt(ctx
->i32
, 28, false), "");
4302 is_msaa
= LLVMBuildAnd(ctx
->builder
, is_msaa
,
4303 LLVMConstInt(ctx
->i32
, 0xe, false), "");
4304 is_msaa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, is_msaa
,
4305 LLVMConstInt(ctx
->i32
, 0xe, false), "");
4307 samples
= LLVMBuildLShr(ctx
->builder
, samples
,
4308 LLVMConstInt(ctx
->i32
, 16, false), "");
4309 samples
= LLVMBuildAnd(ctx
->builder
, samples
,
4310 LLVMConstInt(ctx
->i32
, 0xf, false), "");
4311 samples
= LLVMBuildShl(ctx
->builder
, ctx
->i32one
,
4313 samples
= LLVMBuildSelect(ctx
->builder
, is_msaa
, samples
,
4320 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
4321 coords
[chan
] = llvm_extract_elem(ctx
, coord
, chan
);
4323 if (offsets
&& instr
->op
!= nir_texop_txf
) {
4324 LLVMValueRef offset
[3], pack
;
4325 for (chan
= 0; chan
< 3; ++chan
)
4326 offset
[chan
] = ctx
->i32zero
;
4329 for (chan
= 0; chan
< get_llvm_num_components(offsets
); chan
++) {
4330 offset
[chan
] = llvm_extract_elem(ctx
, offsets
, chan
);
4331 offset
[chan
] = LLVMBuildAnd(ctx
->builder
, offset
[chan
],
4332 LLVMConstInt(ctx
->i32
, 0x3f, false), "");
4334 offset
[chan
] = LLVMBuildShl(ctx
->builder
, offset
[chan
],
4335 LLVMConstInt(ctx
->i32
, chan
* 8, false), "");
4337 pack
= LLVMBuildOr(ctx
->builder
, offset
[0], offset
[1], "");
4338 pack
= LLVMBuildOr(ctx
->builder
, pack
, offset
[2], "");
4339 address
[count
++] = pack
;
4342 /* pack LOD bias value */
4343 if (instr
->op
== nir_texop_txb
&& bias
) {
4344 address
[count
++] = bias
;
4347 /* Pack depth comparison value */
4348 if (instr
->is_shadow
&& comparator
) {
4349 address
[count
++] = llvm_extract_elem(ctx
, comparator
, 0);
4352 /* pack derivatives */
4354 switch (instr
->sampler_dim
) {
4355 case GLSL_SAMPLER_DIM_3D
:
4356 case GLSL_SAMPLER_DIM_CUBE
:
4359 case GLSL_SAMPLER_DIM_2D
:
4363 case GLSL_SAMPLER_DIM_1D
:
4368 for (unsigned i
= 0; i
< num_deriv_comp
; i
++) {
4369 derivs
[i
] = to_float(&ctx
->ac
, llvm_extract_elem(ctx
, ddx
, i
));
4370 derivs
[num_deriv_comp
+ i
] = to_float(&ctx
->ac
, llvm_extract_elem(ctx
, ddy
, i
));
4374 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& coord
) {
4375 if (instr
->is_array
&& instr
->op
!= nir_texop_lod
)
4376 coords
[3] = apply_round_slice(ctx
, coords
[3]);
4377 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
4378 coords
[chan
] = to_float(&ctx
->ac
, coords
[chan
]);
4379 if (instr
->coord_components
== 3)
4380 coords
[3] = LLVMGetUndef(ctx
->f32
);
4381 ac_prepare_cube_coords(&ctx
->ac
,
4382 instr
->op
== nir_texop_txd
, instr
->is_array
,
4389 for (unsigned i
= 0; i
< num_deriv_comp
* 2; i
++)
4390 address
[count
++] = derivs
[i
];
4393 /* Pack texture coordinates */
4395 address
[count
++] = coords
[0];
4396 if (instr
->coord_components
> 1) {
4397 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&& instr
->is_array
&& instr
->op
!= nir_texop_txf
) {
4398 coords
[1] = apply_round_slice(ctx
, coords
[1]);
4400 address
[count
++] = coords
[1];
4402 if (instr
->coord_components
> 2) {
4403 /* This seems like a bit of a hack - but it passes Vulkan CTS with it */
4404 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_3D
&&
4405 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_CUBE
&&
4406 instr
->op
!= nir_texop_txf
) {
4407 coords
[2] = apply_round_slice(ctx
, coords
[2]);
4409 address
[count
++] = coords
[2];
4414 if (lod
&& ((instr
->op
== nir_texop_txl
&& !lod_is_zero
) ||
4415 instr
->op
== nir_texop_txf
)) {
4416 address
[count
++] = lod
;
4417 } else if (instr
->op
== nir_texop_txf_ms
&& sample_index
) {
4418 address
[count
++] = sample_index
;
4419 } else if(instr
->op
== nir_texop_txs
) {
4422 address
[count
++] = lod
;
4424 address
[count
++] = ctx
->i32zero
;
4427 for (chan
= 0; chan
< count
; chan
++) {
4428 address
[chan
] = LLVMBuildBitCast(ctx
->builder
,
4429 address
[chan
], ctx
->i32
, "");
4432 if (instr
->op
== nir_texop_samples_identical
) {
4433 LLVMValueRef txf_address
[4];
4434 struct ac_image_args txf_args
= { 0 };
4435 unsigned txf_count
= count
;
4436 memcpy(txf_address
, address
, sizeof(txf_address
));
4438 if (!instr
->is_array
)
4439 txf_address
[2] = ctx
->i32zero
;
4440 txf_address
[3] = ctx
->i32zero
;
4442 set_tex_fetch_args(ctx
, &txf_args
, instr
, nir_texop_txf
,
4444 txf_address
, txf_count
, 0xf);
4446 result
= build_tex_intrinsic(ctx
, instr
, false, &txf_args
);
4448 result
= LLVMBuildExtractElement(ctx
->builder
, result
, ctx
->i32zero
, "");
4449 result
= emit_int_cmp(ctx
, LLVMIntEQ
, result
, ctx
->i32zero
);
4453 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
4454 instr
->op
!= nir_texop_txs
) {
4455 unsigned sample_chan
= instr
->is_array
? 3 : 2;
4456 address
[sample_chan
] = adjust_sample_index_using_fmask(ctx
,
4459 instr
->is_array
? address
[2] : NULL
,
4460 address
[sample_chan
],
4464 if (offsets
&& instr
->op
== nir_texop_txf
) {
4465 nir_const_value
*const_offset
=
4466 nir_src_as_const_value(instr
->src
[const_src
].src
);
4467 int num_offsets
= instr
->src
[const_src
].src
.ssa
->num_components
;
4468 assert(const_offset
);
4469 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
4470 if (num_offsets
> 2)
4471 address
[2] = LLVMBuildAdd(ctx
->builder
,
4472 address
[2], LLVMConstInt(ctx
->i32
, const_offset
->i32
[2], false), "");
4473 if (num_offsets
> 1)
4474 address
[1] = LLVMBuildAdd(ctx
->builder
,
4475 address
[1], LLVMConstInt(ctx
->i32
, const_offset
->i32
[1], false), "");
4476 address
[0] = LLVMBuildAdd(ctx
->builder
,
4477 address
[0], LLVMConstInt(ctx
->i32
, const_offset
->i32
[0], false), "");
4481 /* TODO TG4 support */
4482 if (instr
->op
== nir_texop_tg4
) {
4483 if (instr
->is_shadow
)
4486 dmask
= 1 << instr
->component
;
4488 set_tex_fetch_args(ctx
, &args
, instr
, instr
->op
,
4489 res_ptr
, samp_ptr
, address
, count
, dmask
);
4491 result
= build_tex_intrinsic(ctx
, instr
, lod_is_zero
, &args
);
4493 if (instr
->op
== nir_texop_query_levels
)
4494 result
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, 3, false), "");
4495 else if (instr
->is_shadow
&& instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&& instr
->op
!= nir_texop_tg4
)
4496 result
= LLVMBuildExtractElement(ctx
->builder
, result
, ctx
->i32zero
, "");
4497 else if (instr
->op
== nir_texop_txs
&&
4498 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
4500 LLVMValueRef two
= LLVMConstInt(ctx
->i32
, 2, false);
4501 LLVMValueRef six
= LLVMConstInt(ctx
->i32
, 6, false);
4502 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->builder
, result
, two
, "");
4503 z
= LLVMBuildSDiv(ctx
->builder
, z
, six
, "");
4504 result
= LLVMBuildInsertElement(ctx
->builder
, result
, z
, two
, "");
4505 } else if (instr
->dest
.ssa
.num_components
!= 4)
4506 result
= trim_vector(ctx
, result
, instr
->dest
.ssa
.num_components
);
4510 assert(instr
->dest
.is_ssa
);
4511 result
= to_integer(&ctx
->ac
, result
);
4512 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4517 static void visit_phi(struct nir_to_llvm_context
*ctx
, nir_phi_instr
*instr
)
4519 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
4520 LLVMValueRef result
= LLVMBuildPhi(ctx
->builder
, type
, "");
4522 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4523 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
4526 static void visit_post_phi(struct nir_to_llvm_context
*ctx
,
4527 nir_phi_instr
*instr
,
4528 LLVMValueRef llvm_phi
)
4530 nir_foreach_phi_src(src
, instr
) {
4531 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
4532 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
4534 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
4538 static void phi_post_pass(struct nir_to_llvm_context
*ctx
)
4540 struct hash_entry
*entry
;
4541 hash_table_foreach(ctx
->phis
, entry
) {
4542 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
4543 (LLVMValueRef
)entry
->data
);
4548 static void visit_ssa_undef(struct nir_to_llvm_context
*ctx
,
4549 const nir_ssa_undef_instr
*instr
)
4551 unsigned num_components
= instr
->def
.num_components
;
4554 if (num_components
== 1)
4555 undef
= LLVMGetUndef(ctx
->i32
);
4557 undef
= LLVMGetUndef(LLVMVectorType(ctx
->i32
, num_components
));
4559 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, undef
);
4562 static void visit_jump(struct nir_to_llvm_context
*ctx
,
4563 const nir_jump_instr
*instr
)
4565 switch (instr
->type
) {
4566 case nir_jump_break
:
4567 LLVMBuildBr(ctx
->builder
, ctx
->break_block
);
4568 LLVMClearInsertionPosition(ctx
->builder
);
4570 case nir_jump_continue
:
4571 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
4572 LLVMClearInsertionPosition(ctx
->builder
);
4575 fprintf(stderr
, "Unknown NIR jump instr: ");
4576 nir_print_instr(&instr
->instr
, stderr
);
4577 fprintf(stderr
, "\n");
4582 static void visit_cf_list(struct nir_to_llvm_context
*ctx
,
4583 struct exec_list
*list
);
4585 static void visit_block(struct nir_to_llvm_context
*ctx
, nir_block
*block
)
4587 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->builder
);
4588 nir_foreach_instr(instr
, block
)
4590 switch (instr
->type
) {
4591 case nir_instr_type_alu
:
4592 visit_alu(ctx
, nir_instr_as_alu(instr
));
4594 case nir_instr_type_load_const
:
4595 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
4597 case nir_instr_type_intrinsic
:
4598 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
4600 case nir_instr_type_tex
:
4601 visit_tex(ctx
, nir_instr_as_tex(instr
));
4603 case nir_instr_type_phi
:
4604 visit_phi(ctx
, nir_instr_as_phi(instr
));
4606 case nir_instr_type_ssa_undef
:
4607 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
4609 case nir_instr_type_jump
:
4610 visit_jump(ctx
, nir_instr_as_jump(instr
));
4613 fprintf(stderr
, "Unknown NIR instr type: ");
4614 nir_print_instr(instr
, stderr
);
4615 fprintf(stderr
, "\n");
4620 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
4623 static void visit_if(struct nir_to_llvm_context
*ctx
, nir_if
*if_stmt
)
4625 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
4627 LLVMBasicBlockRef merge_block
=
4628 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
4629 LLVMBasicBlockRef if_block
=
4630 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
4631 LLVMBasicBlockRef else_block
= merge_block
;
4632 if (!exec_list_is_empty(&if_stmt
->else_list
))
4633 else_block
= LLVMAppendBasicBlockInContext(
4634 ctx
->context
, ctx
->main_function
, "");
4636 LLVMValueRef cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, value
,
4637 LLVMConstInt(ctx
->i32
, 0, false), "");
4638 LLVMBuildCondBr(ctx
->builder
, cond
, if_block
, else_block
);
4640 LLVMPositionBuilderAtEnd(ctx
->builder
, if_block
);
4641 visit_cf_list(ctx
, &if_stmt
->then_list
);
4642 if (LLVMGetInsertBlock(ctx
->builder
))
4643 LLVMBuildBr(ctx
->builder
, merge_block
);
4645 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
4646 LLVMPositionBuilderAtEnd(ctx
->builder
, else_block
);
4647 visit_cf_list(ctx
, &if_stmt
->else_list
);
4648 if (LLVMGetInsertBlock(ctx
->builder
))
4649 LLVMBuildBr(ctx
->builder
, merge_block
);
4652 LLVMPositionBuilderAtEnd(ctx
->builder
, merge_block
);
4655 static void visit_loop(struct nir_to_llvm_context
*ctx
, nir_loop
*loop
)
4657 LLVMBasicBlockRef continue_parent
= ctx
->continue_block
;
4658 LLVMBasicBlockRef break_parent
= ctx
->break_block
;
4660 ctx
->continue_block
=
4661 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
4663 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
4665 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
4666 LLVMPositionBuilderAtEnd(ctx
->builder
, ctx
->continue_block
);
4667 visit_cf_list(ctx
, &loop
->body
);
4669 if (LLVMGetInsertBlock(ctx
->builder
))
4670 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
4671 LLVMPositionBuilderAtEnd(ctx
->builder
, ctx
->break_block
);
4673 ctx
->continue_block
= continue_parent
;
4674 ctx
->break_block
= break_parent
;
4677 static void visit_cf_list(struct nir_to_llvm_context
*ctx
,
4678 struct exec_list
*list
)
4680 foreach_list_typed(nir_cf_node
, node
, node
, list
)
4682 switch (node
->type
) {
4683 case nir_cf_node_block
:
4684 visit_block(ctx
, nir_cf_node_as_block(node
));
4687 case nir_cf_node_if
:
4688 visit_if(ctx
, nir_cf_node_as_if(node
));
4691 case nir_cf_node_loop
:
4692 visit_loop(ctx
, nir_cf_node_as_loop(node
));
4702 handle_vs_input_decl(struct nir_to_llvm_context
*ctx
,
4703 struct nir_variable
*variable
)
4705 LLVMValueRef t_list_ptr
= ctx
->vertex_buffers
;
4706 LLVMValueRef t_offset
;
4707 LLVMValueRef t_list
;
4709 LLVMValueRef buffer_index
;
4710 int index
= variable
->data
.location
- VERT_ATTRIB_GENERIC0
;
4711 int idx
= variable
->data
.location
;
4712 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, true);
4714 variable
->data
.driver_location
= idx
* 4;
4716 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << index
)) {
4717 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->instance_id
,
4718 ctx
->start_instance
, "");
4719 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
4720 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
4722 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->vertex_id
,
4723 ctx
->base_vertex
, "");
4725 for (unsigned i
= 0; i
< attrib_count
; ++i
, ++idx
) {
4726 t_offset
= LLVMConstInt(ctx
->i32
, index
+ i
, false);
4728 t_list
= ac_build_indexed_load_const(&ctx
->ac
, t_list_ptr
, t_offset
);
4730 input
= ac_build_buffer_load_format(&ctx
->ac
, t_list
,
4732 LLVMConstInt(ctx
->i32
, 0, false),
4735 for (unsigned chan
= 0; chan
< 4; chan
++) {
4736 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
4737 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
, chan
)] =
4738 to_integer(&ctx
->ac
, LLVMBuildExtractElement(ctx
->builder
,
4739 input
, llvm_chan
, ""));
4744 static void interp_fs_input(struct nir_to_llvm_context
*ctx
,
4746 LLVMValueRef interp_param
,
4747 LLVMValueRef prim_mask
,
4748 LLVMValueRef result
[4])
4750 LLVMValueRef attr_number
;
4753 bool interp
= interp_param
!= NULL
;
4755 attr_number
= LLVMConstInt(ctx
->i32
, attr
, false);
4757 /* fs.constant returns the param from the middle vertex, so it's not
4758 * really useful for flat shading. It's meant to be used for custom
4759 * interpolation (but the intrinsic can't fetch from the other two
4762 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
4763 * to do the right thing. The only reason we use fs.constant is that
4764 * fs.interp cannot be used on integers, because they can be equal
4768 interp_param
= LLVMBuildBitCast(ctx
->builder
, interp_param
,
4769 LLVMVectorType(ctx
->f32
, 2), "");
4771 i
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
4773 j
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
4777 for (chan
= 0; chan
< 4; chan
++) {
4778 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
4781 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
4786 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
4787 LLVMConstInt(ctx
->i32
, 2, false),
4796 handle_fs_input_decl(struct nir_to_llvm_context
*ctx
,
4797 struct nir_variable
*variable
)
4799 int idx
= variable
->data
.location
;
4800 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4801 LLVMValueRef interp
;
4803 variable
->data
.driver_location
= idx
* 4;
4804 ctx
->input_mask
|= ((1ull << attrib_count
) - 1) << variable
->data
.location
;
4806 if (glsl_get_base_type(glsl_without_array(variable
->type
)) == GLSL_TYPE_FLOAT
) {
4807 unsigned interp_type
;
4808 if (variable
->data
.sample
) {
4809 interp_type
= INTERP_SAMPLE
;
4810 ctx
->shader_info
->fs
.force_persample
= true;
4811 } else if (variable
->data
.centroid
)
4812 interp_type
= INTERP_CENTROID
;
4814 interp_type
= INTERP_CENTER
;
4816 interp
= lookup_interp_param(ctx
, variable
->data
.interpolation
, interp_type
);
4820 for (unsigned i
= 0; i
< attrib_count
; ++i
)
4821 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
+ i
, 0)] = interp
;
4826 handle_shader_input_decl(struct nir_to_llvm_context
*ctx
,
4827 struct nir_variable
*variable
)
4829 switch (ctx
->stage
) {
4830 case MESA_SHADER_VERTEX
:
4831 handle_vs_input_decl(ctx
, variable
);
4833 case MESA_SHADER_FRAGMENT
:
4834 handle_fs_input_decl(ctx
, variable
);
4843 handle_fs_inputs_pre(struct nir_to_llvm_context
*ctx
,
4844 struct nir_shader
*nir
)
4847 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_INPUTS
; ++i
) {
4848 LLVMValueRef interp_param
;
4849 LLVMValueRef
*inputs
= ctx
->inputs
+radeon_llvm_reg_index_soa(i
, 0);
4851 if (!(ctx
->input_mask
& (1ull << i
)))
4854 if (i
>= VARYING_SLOT_VAR0
|| i
== VARYING_SLOT_PNTC
||
4855 i
== VARYING_SLOT_PRIMITIVE_ID
|| i
== VARYING_SLOT_LAYER
) {
4856 interp_param
= *inputs
;
4857 interp_fs_input(ctx
, index
, interp_param
, ctx
->prim_mask
,
4861 ctx
->shader_info
->fs
.flat_shaded_mask
|= 1u << index
;
4863 } else if (i
== VARYING_SLOT_POS
) {
4864 for(int i
= 0; i
< 3; ++i
)
4865 inputs
[i
] = ctx
->frag_pos
[i
];
4867 inputs
[3] = ac_build_fdiv(&ctx
->ac
, ctx
->f32one
, ctx
->frag_pos
[3]);
4870 ctx
->shader_info
->fs
.num_interp
= index
;
4871 if (ctx
->input_mask
& (1 << VARYING_SLOT_PNTC
))
4872 ctx
->shader_info
->fs
.has_pcoord
= true;
4873 if (ctx
->input_mask
& (1 << VARYING_SLOT_PRIMITIVE_ID
))
4874 ctx
->shader_info
->fs
.prim_id_input
= true;
4875 if (ctx
->input_mask
& (1 << VARYING_SLOT_LAYER
))
4876 ctx
->shader_info
->fs
.layer_input
= true;
4877 ctx
->shader_info
->fs
.input_mask
= ctx
->input_mask
>> VARYING_SLOT_VAR0
;
4881 ac_build_alloca(struct nir_to_llvm_context
*ctx
,
4885 LLVMBuilderRef builder
= ctx
->builder
;
4886 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
4887 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
4888 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
4889 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
4890 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ctx
->context
);
4894 LLVMPositionBuilderBefore(first_builder
, first_instr
);
4896 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
4899 res
= LLVMBuildAlloca(first_builder
, type
, name
);
4900 LLVMBuildStore(builder
, LLVMConstNull(type
), res
);
4902 LLVMDisposeBuilder(first_builder
);
4907 static LLVMValueRef
si_build_alloca_undef(struct nir_to_llvm_context
*ctx
,
4911 LLVMValueRef ptr
= ac_build_alloca(ctx
, type
, name
);
4912 LLVMBuildStore(ctx
->builder
, LLVMGetUndef(type
), ptr
);
4917 handle_shader_output_decl(struct nir_to_llvm_context
*ctx
,
4918 struct nir_variable
*variable
)
4920 int idx
= variable
->data
.location
+ variable
->data
.index
;
4921 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4922 uint64_t mask_attribs
;
4923 variable
->data
.driver_location
= idx
* 4;
4925 /* tess ctrl has it's own load/store paths for outputs */
4926 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
4929 mask_attribs
= ((1ull << attrib_count
) - 1) << idx
;
4930 if (ctx
->stage
== MESA_SHADER_VERTEX
||
4931 ctx
->stage
== MESA_SHADER_TESS_EVAL
||
4932 ctx
->stage
== MESA_SHADER_GEOMETRY
) {
4933 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
4934 int length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
4935 if (ctx
->stage
== MESA_SHADER_VERTEX
) {
4936 ctx
->shader_info
->vs
.outinfo
.clip_dist_mask
= (1 << ctx
->num_output_clips
) - 1;
4937 ctx
->shader_info
->vs
.outinfo
.cull_dist_mask
= (1 << ctx
->num_output_culls
) - 1;
4939 if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
4940 ctx
->shader_info
->tes
.outinfo
.clip_dist_mask
= (1 << ctx
->num_output_clips
) - 1;
4941 ctx
->shader_info
->tes
.outinfo
.cull_dist_mask
= (1 << ctx
->num_output_culls
) - 1;
4948 mask_attribs
= 1ull << idx
;
4952 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
4953 for (unsigned chan
= 0; chan
< 4; chan
++) {
4954 ctx
->outputs
[radeon_llvm_reg_index_soa(idx
+ i
, chan
)] =
4955 si_build_alloca_undef(ctx
, ctx
->f32
, "");
4958 ctx
->output_mask
|= mask_attribs
;
4962 setup_locals(struct nir_to_llvm_context
*ctx
,
4963 struct nir_function
*func
)
4966 ctx
->num_locals
= 0;
4967 nir_foreach_variable(variable
, &func
->impl
->locals
) {
4968 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4969 variable
->data
.driver_location
= ctx
->num_locals
* 4;
4970 ctx
->num_locals
+= attrib_count
;
4972 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
4976 for (i
= 0; i
< ctx
->num_locals
; i
++) {
4977 for (j
= 0; j
< 4; j
++) {
4978 ctx
->locals
[i
* 4 + j
] =
4979 si_build_alloca_undef(ctx
, ctx
->f32
, "temp");
4985 emit_float_saturate(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
, float lo
, float hi
)
4987 v
= to_float(&ctx
->ac
, v
);
4988 v
= emit_intrin_2f_param(ctx
, "llvm.maxnum.f32", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, lo
));
4989 return emit_intrin_2f_param(ctx
, "llvm.minnum.f32", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, hi
));
4993 static LLVMValueRef
emit_pack_int16(struct nir_to_llvm_context
*ctx
,
4994 LLVMValueRef src0
, LLVMValueRef src1
)
4996 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
4997 LLVMValueRef comp
[2];
4999 comp
[0] = LLVMBuildAnd(ctx
->builder
, src0
, LLVMConstInt(ctx
-> i32
, 65535, 0), "");
5000 comp
[1] = LLVMBuildAnd(ctx
->builder
, src1
, LLVMConstInt(ctx
-> i32
, 65535, 0), "");
5001 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
5002 return LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
5005 /* Initialize arguments for the shader export intrinsic */
5007 si_llvm_init_export_args(struct nir_to_llvm_context
*ctx
,
5008 LLVMValueRef
*values
,
5010 struct ac_export_args
*args
)
5012 /* Default is 0xf. Adjusted below depending on the format. */
5013 args
->enabled_channels
= 0xf;
5015 /* Specify whether the EXEC mask represents the valid mask */
5016 args
->valid_mask
= 0;
5018 /* Specify whether this is the last export */
5021 /* Specify the target we are exporting */
5022 args
->target
= target
;
5024 args
->compr
= false;
5025 args
->out
[0] = LLVMGetUndef(ctx
->f32
);
5026 args
->out
[1] = LLVMGetUndef(ctx
->f32
);
5027 args
->out
[2] = LLVMGetUndef(ctx
->f32
);
5028 args
->out
[3] = LLVMGetUndef(ctx
->f32
);
5033 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&& target
>= V_008DFC_SQ_EXP_MRT
) {
5034 LLVMValueRef val
[4];
5035 unsigned index
= target
- V_008DFC_SQ_EXP_MRT
;
5036 unsigned col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * index
)) & 0xf;
5037 bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> index
) & 1;
5039 switch(col_format
) {
5040 case V_028714_SPI_SHADER_ZERO
:
5041 args
->enabled_channels
= 0; /* writemask */
5042 args
->target
= V_008DFC_SQ_EXP_NULL
;
5045 case V_028714_SPI_SHADER_32_R
:
5046 args
->enabled_channels
= 1;
5047 args
->out
[0] = values
[0];
5050 case V_028714_SPI_SHADER_32_GR
:
5051 args
->enabled_channels
= 0x3;
5052 args
->out
[0] = values
[0];
5053 args
->out
[1] = values
[1];
5056 case V_028714_SPI_SHADER_32_AR
:
5057 args
->enabled_channels
= 0x9;
5058 args
->out
[0] = values
[0];
5059 args
->out
[3] = values
[3];
5062 case V_028714_SPI_SHADER_FP16_ABGR
:
5065 for (unsigned chan
= 0; chan
< 2; chan
++) {
5066 LLVMValueRef pack_args
[2] = {
5068 values
[2 * chan
+ 1]
5070 LLVMValueRef packed
;
5072 packed
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, pack_args
);
5073 args
->out
[chan
] = packed
;
5077 case V_028714_SPI_SHADER_UNORM16_ABGR
:
5078 for (unsigned chan
= 0; chan
< 4; chan
++) {
5079 val
[chan
] = ac_build_clamp(&ctx
->ac
, values
[chan
]);
5080 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
5081 LLVMConstReal(ctx
->f32
, 65535), "");
5082 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
5083 LLVMConstReal(ctx
->f32
, 0.5), "");
5084 val
[chan
] = LLVMBuildFPToUI(ctx
->builder
, val
[chan
],
5089 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5090 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5093 case V_028714_SPI_SHADER_SNORM16_ABGR
:
5094 for (unsigned chan
= 0; chan
< 4; chan
++) {
5095 val
[chan
] = emit_float_saturate(ctx
, values
[chan
], -1, 1);
5096 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
5097 LLVMConstReal(ctx
->f32
, 32767), "");
5099 /* If positive, add 0.5, else add -0.5. */
5100 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
5101 LLVMBuildSelect(ctx
->builder
,
5102 LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
,
5103 val
[chan
], ctx
->f32zero
, ""),
5104 LLVMConstReal(ctx
->f32
, 0.5),
5105 LLVMConstReal(ctx
->f32
, -0.5), ""), "");
5106 val
[chan
] = LLVMBuildFPToSI(ctx
->builder
, val
[chan
], ctx
->i32
, "");
5110 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5111 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5114 case V_028714_SPI_SHADER_UINT16_ABGR
: {
5115 LLVMValueRef max
= LLVMConstInt(ctx
->i32
, is_int8
? 255 : 65535, 0);
5117 for (unsigned chan
= 0; chan
< 4; chan
++) {
5118 val
[chan
] = to_integer(&ctx
->ac
, values
[chan
]);
5119 val
[chan
] = emit_minmax_int(ctx
, LLVMIntULT
, val
[chan
], max
);
5123 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5124 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5128 case V_028714_SPI_SHADER_SINT16_ABGR
: {
5129 LLVMValueRef max
= LLVMConstInt(ctx
->i32
, is_int8
? 127 : 32767, 0);
5130 LLVMValueRef min
= LLVMConstInt(ctx
->i32
, is_int8
? -128 : -32768, 0);
5133 for (unsigned chan
= 0; chan
< 4; chan
++) {
5134 val
[chan
] = to_integer(&ctx
->ac
, values
[chan
]);
5135 val
[chan
] = emit_minmax_int(ctx
, LLVMIntSLT
, val
[chan
], max
);
5136 val
[chan
] = emit_minmax_int(ctx
, LLVMIntSGT
, val
[chan
], min
);
5140 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5141 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5146 case V_028714_SPI_SHADER_32_ABGR
:
5147 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
5151 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
5153 for (unsigned i
= 0; i
< 4; ++i
)
5154 args
->out
[i
] = to_float(&ctx
->ac
, args
->out
[i
]);
5158 handle_vs_outputs_post(struct nir_to_llvm_context
*ctx
,
5159 bool export_prim_id
,
5160 struct ac_vs_output_info
*outinfo
)
5162 uint32_t param_count
= 0;
5164 unsigned pos_idx
, num_pos_exports
= 0;
5165 struct ac_export_args args
, pos_args
[4] = {};
5166 LLVMValueRef psize_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
5169 memset(outinfo
->vs_output_param_offset
, AC_EXP_PARAM_UNDEFINED
,
5170 sizeof(outinfo
->vs_output_param_offset
));
5172 if (ctx
->output_mask
& (1ull << VARYING_SLOT_CLIP_DIST0
)) {
5173 LLVMValueRef slots
[8];
5176 if (outinfo
->cull_dist_mask
)
5177 outinfo
->cull_dist_mask
<<= ctx
->num_output_clips
;
5179 i
= VARYING_SLOT_CLIP_DIST0
;
5180 for (j
= 0; j
< ctx
->num_output_clips
+ ctx
->num_output_culls
; j
++)
5181 slots
[j
] = to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
5182 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5184 for (i
= ctx
->num_output_clips
+ ctx
->num_output_culls
; i
< 8; i
++)
5185 slots
[i
] = LLVMGetUndef(ctx
->f32
);
5187 if (ctx
->num_output_clips
+ ctx
->num_output_culls
> 4) {
5188 target
= V_008DFC_SQ_EXP_POS
+ 3;
5189 si_llvm_init_export_args(ctx
, &slots
[4], target
, &args
);
5190 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5191 &args
, sizeof(args
));
5194 target
= V_008DFC_SQ_EXP_POS
+ 2;
5195 si_llvm_init_export_args(ctx
, &slots
[0], target
, &args
);
5196 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5197 &args
, sizeof(args
));
5201 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5202 LLVMValueRef values
[4];
5203 if (!(ctx
->output_mask
& (1ull << i
)))
5206 for (unsigned j
= 0; j
< 4; j
++)
5207 values
[j
] = to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
5208 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5210 if (i
== VARYING_SLOT_POS
) {
5211 target
= V_008DFC_SQ_EXP_POS
;
5212 } else if (i
== VARYING_SLOT_CLIP_DIST0
) {
5214 } else if (i
== VARYING_SLOT_PSIZ
) {
5215 outinfo
->writes_pointsize
= true;
5216 psize_value
= values
[0];
5218 } else if (i
== VARYING_SLOT_LAYER
) {
5219 outinfo
->writes_layer
= true;
5220 layer_value
= values
[0];
5221 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5222 outinfo
->vs_output_param_offset
[VARYING_SLOT_LAYER
] = param_count
;
5224 } else if (i
== VARYING_SLOT_VIEWPORT
) {
5225 outinfo
->writes_viewport_index
= true;
5226 viewport_index_value
= values
[0];
5228 } else if (i
== VARYING_SLOT_PRIMITIVE_ID
) {
5229 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5230 outinfo
->vs_output_param_offset
[VARYING_SLOT_PRIMITIVE_ID
] = param_count
;
5232 } else if (i
>= VARYING_SLOT_VAR0
) {
5233 outinfo
->export_mask
|= 1u << (i
- VARYING_SLOT_VAR0
);
5234 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5235 outinfo
->vs_output_param_offset
[i
] = param_count
;
5239 si_llvm_init_export_args(ctx
, values
, target
, &args
);
5241 if (target
>= V_008DFC_SQ_EXP_POS
&&
5242 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
5243 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5244 &args
, sizeof(args
));
5246 ac_build_export(&ctx
->ac
, &args
);
5250 /* We need to add the position output manually if it's missing. */
5251 if (!pos_args
[0].out
[0]) {
5252 pos_args
[0].enabled_channels
= 0xf;
5253 pos_args
[0].valid_mask
= 0;
5254 pos_args
[0].done
= 0;
5255 pos_args
[0].target
= V_008DFC_SQ_EXP_POS
;
5256 pos_args
[0].compr
= 0;
5257 pos_args
[0].out
[0] = ctx
->f32zero
; /* X */
5258 pos_args
[0].out
[1] = ctx
->f32zero
; /* Y */
5259 pos_args
[0].out
[2] = ctx
->f32zero
; /* Z */
5260 pos_args
[0].out
[3] = ctx
->f32one
; /* W */
5263 uint32_t mask
= ((outinfo
->writes_pointsize
== true ? 1 : 0) |
5264 (outinfo
->writes_layer
== true ? 4 : 0) |
5265 (outinfo
->writes_viewport_index
== true ? 8 : 0));
5267 pos_args
[1].enabled_channels
= mask
;
5268 pos_args
[1].valid_mask
= 0;
5269 pos_args
[1].done
= 0;
5270 pos_args
[1].target
= V_008DFC_SQ_EXP_POS
+ 1;
5271 pos_args
[1].compr
= 0;
5272 pos_args
[1].out
[0] = ctx
->f32zero
; /* X */
5273 pos_args
[1].out
[1] = ctx
->f32zero
; /* Y */
5274 pos_args
[1].out
[2] = ctx
->f32zero
; /* Z */
5275 pos_args
[1].out
[3] = ctx
->f32zero
; /* W */
5277 if (outinfo
->writes_pointsize
== true)
5278 pos_args
[1].out
[0] = psize_value
;
5279 if (outinfo
->writes_layer
== true)
5280 pos_args
[1].out
[2] = layer_value
;
5281 if (outinfo
->writes_viewport_index
== true)
5282 pos_args
[1].out
[3] = viewport_index_value
;
5284 for (i
= 0; i
< 4; i
++) {
5285 if (pos_args
[i
].out
[0])
5290 for (i
= 0; i
< 4; i
++) {
5291 if (!pos_args
[i
].out
[0])
5294 /* Specify the target we are exporting */
5295 pos_args
[i
].target
= V_008DFC_SQ_EXP_POS
+ pos_idx
++;
5296 if (pos_idx
== num_pos_exports
)
5297 pos_args
[i
].done
= 1;
5298 ac_build_export(&ctx
->ac
, &pos_args
[i
]);
5302 if (export_prim_id
) {
5303 LLVMValueRef values
[4];
5304 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5305 outinfo
->vs_output_param_offset
[VARYING_SLOT_PRIMITIVE_ID
] = param_count
;
5308 values
[0] = ctx
->vs_prim_id
;
5309 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(2,
5310 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
5311 for (unsigned j
= 1; j
< 4; j
++)
5312 values
[j
] = ctx
->f32zero
;
5313 si_llvm_init_export_args(ctx
, values
, target
, &args
);
5314 ac_build_export(&ctx
->ac
, &args
);
5315 outinfo
->export_prim_id
= true;
5318 outinfo
->pos_exports
= num_pos_exports
;
5319 outinfo
->param_exports
= param_count
;
5323 handle_es_outputs_post(struct nir_to_llvm_context
*ctx
,
5324 struct ac_es_output_info
*outinfo
)
5327 uint64_t max_output_written
= 0;
5328 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5329 LLVMValueRef
*out_ptr
= &ctx
->outputs
[i
* 4];
5333 if (!(ctx
->output_mask
& (1ull << i
)))
5336 if (i
== VARYING_SLOT_CLIP_DIST0
)
5337 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
5339 param_index
= shader_io_get_unique_index(i
);
5341 max_output_written
= MAX2(param_index
+ (length
> 4), max_output_written
);
5343 for (j
= 0; j
< length
; j
++) {
5344 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], "");
5345 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->i32
, "");
5347 ac_build_buffer_store_dword(&ctx
->ac
,
5350 NULL
, ctx
->es2gs_offset
,
5351 (4 * param_index
+ j
) * 4,
5355 outinfo
->esgs_itemsize
= (max_output_written
+ 1) * 16;
5359 handle_ls_outputs_post(struct nir_to_llvm_context
*ctx
)
5361 LLVMValueRef vertex_id
= ctx
->rel_auto_id
;
5362 LLVMValueRef vertex_dw_stride
= unpack_param(ctx
, ctx
->ls_out_layout
, 13, 8);
5363 LLVMValueRef base_dw_addr
= LLVMBuildMul(ctx
->builder
, vertex_id
,
5364 vertex_dw_stride
, "");
5366 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5367 LLVMValueRef
*out_ptr
= &ctx
->outputs
[i
* 4];
5370 if (!(ctx
->output_mask
& (1ull << i
)))
5373 if (i
== VARYING_SLOT_CLIP_DIST0
)
5374 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
5375 int param
= shader_io_get_unique_index(i
);
5376 mark_tess_output(ctx
, false, param
);
5378 mark_tess_output(ctx
, false, param
+ 1);
5379 LLVMValueRef dw_addr
= LLVMBuildAdd(ctx
->builder
, base_dw_addr
,
5380 LLVMConstInt(ctx
->i32
, param
* 4, false),
5382 for (unsigned j
= 0; j
< length
; j
++) {
5383 lds_store(ctx
, dw_addr
,
5384 LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], ""));
5385 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
, ctx
->i32one
, "");
5390 struct ac_build_if_state
5392 struct nir_to_llvm_context
*ctx
;
5393 LLVMValueRef condition
;
5394 LLVMBasicBlockRef entry_block
;
5395 LLVMBasicBlockRef true_block
;
5396 LLVMBasicBlockRef false_block
;
5397 LLVMBasicBlockRef merge_block
;
5400 static LLVMBasicBlockRef
5401 ac_build_insert_new_block(struct nir_to_llvm_context
*ctx
, const char *name
)
5403 LLVMBasicBlockRef current_block
;
5404 LLVMBasicBlockRef next_block
;
5405 LLVMBasicBlockRef new_block
;
5407 /* get current basic block */
5408 current_block
= LLVMGetInsertBlock(ctx
->builder
);
5410 /* chqeck if there's another block after this one */
5411 next_block
= LLVMGetNextBasicBlock(current_block
);
5413 /* insert the new block before the next block */
5414 new_block
= LLVMInsertBasicBlockInContext(ctx
->context
, next_block
, name
);
5417 /* append new block after current block */
5418 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
5419 new_block
= LLVMAppendBasicBlockInContext(ctx
->context
, function
, name
);
5425 ac_nir_build_if(struct ac_build_if_state
*ifthen
,
5426 struct nir_to_llvm_context
*ctx
,
5427 LLVMValueRef condition
)
5429 LLVMBasicBlockRef block
= LLVMGetInsertBlock(ctx
->builder
);
5431 memset(ifthen
, 0, sizeof *ifthen
);
5433 ifthen
->condition
= condition
;
5434 ifthen
->entry_block
= block
;
5436 /* create endif/merge basic block for the phi functions */
5437 ifthen
->merge_block
= ac_build_insert_new_block(ctx
, "endif-block");
5439 /* create/insert true_block before merge_block */
5440 ifthen
->true_block
=
5441 LLVMInsertBasicBlockInContext(ctx
->context
,
5442 ifthen
->merge_block
,
5445 /* successive code goes into the true block */
5446 LLVMPositionBuilderAtEnd(ctx
->builder
, ifthen
->true_block
);
5450 * End a conditional.
5453 ac_nir_build_endif(struct ac_build_if_state
*ifthen
)
5455 LLVMBuilderRef builder
= ifthen
->ctx
->builder
;
5457 /* Insert branch to the merge block from current block */
5458 LLVMBuildBr(builder
, ifthen
->merge_block
);
5461 * Now patch in the various branch instructions.
5464 /* Insert the conditional branch instruction at the end of entry_block */
5465 LLVMPositionBuilderAtEnd(builder
, ifthen
->entry_block
);
5466 if (ifthen
->false_block
) {
5467 /* we have an else clause */
5468 LLVMBuildCondBr(builder
, ifthen
->condition
,
5469 ifthen
->true_block
, ifthen
->false_block
);
5472 /* no else clause */
5473 LLVMBuildCondBr(builder
, ifthen
->condition
,
5474 ifthen
->true_block
, ifthen
->merge_block
);
5477 /* Resume building code at end of the ifthen->merge_block */
5478 LLVMPositionBuilderAtEnd(builder
, ifthen
->merge_block
);
5482 write_tess_factors(struct nir_to_llvm_context
*ctx
)
5484 unsigned stride
, outer_comps
, inner_comps
;
5485 struct ac_build_if_state if_ctx
, inner_if_ctx
;
5486 LLVMValueRef invocation_id
= unpack_param(ctx
, ctx
->tcs_rel_ids
, 8, 5);
5487 LLVMValueRef rel_patch_id
= unpack_param(ctx
, ctx
->tcs_rel_ids
, 0, 8);
5488 unsigned tess_inner_index
, tess_outer_index
;
5489 LLVMValueRef lds_base
, lds_inner
, lds_outer
, byteoffset
, buffer
;
5490 LLVMValueRef out
[6], vec0
, vec1
, tf_base
, inner
[4], outer
[4];
5494 switch (ctx
->options
->key
.tcs
.primitive_mode
) {
5514 ac_nir_build_if(&if_ctx
, ctx
,
5515 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
5516 invocation_id
, ctx
->i32zero
, ""));
5518 tess_inner_index
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
5519 tess_outer_index
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
5521 mark_tess_output(ctx
, true, tess_inner_index
);
5522 mark_tess_output(ctx
, true, tess_outer_index
);
5523 lds_base
= get_tcs_out_current_patch_data_offset(ctx
);
5524 lds_inner
= LLVMBuildAdd(ctx
->builder
, lds_base
,
5525 LLVMConstInt(ctx
->i32
, tess_inner_index
* 4, false), "");
5526 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_base
,
5527 LLVMConstInt(ctx
->i32
, tess_outer_index
* 4, false), "");
5529 for (i
= 0; i
< 4; i
++) {
5530 inner
[i
] = LLVMGetUndef(ctx
->i32
);
5531 outer
[i
] = LLVMGetUndef(ctx
->i32
);
5535 if (ctx
->options
->key
.tcs
.primitive_mode
== GL_ISOLINES
) {
5536 outer
[0] = out
[1] = lds_load(ctx
, lds_outer
);
5537 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_outer
,
5538 LLVMConstInt(ctx
->i32
, 1, false), "");
5539 outer
[1] = out
[0] = lds_load(ctx
, lds_outer
);
5541 for (i
= 0; i
< outer_comps
; i
++) {
5543 lds_load(ctx
, lds_outer
);
5544 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_outer
,
5545 LLVMConstInt(ctx
->i32
, 1, false), "");
5547 for (i
= 0; i
< inner_comps
; i
++) {
5548 inner
[i
] = out
[outer_comps
+i
] =
5549 lds_load(ctx
, lds_inner
);
5550 lds_inner
= LLVMBuildAdd(ctx
->builder
, lds_inner
,
5551 LLVMConstInt(ctx
->i32
, 1, false), "");
5555 /* Convert the outputs to vectors for stores. */
5556 vec0
= ac_build_gather_values(&ctx
->ac
, out
, MIN2(stride
, 4));
5560 vec1
= ac_build_gather_values(&ctx
->ac
, out
+ 4, stride
- 4);
5563 buffer
= ctx
->hs_ring_tess_factor
;
5564 tf_base
= ctx
->tess_factor_offset
;
5565 byteoffset
= LLVMBuildMul(ctx
->builder
, rel_patch_id
,
5566 LLVMConstInt(ctx
->i32
, 4 * stride
, false), "");
5568 ac_nir_build_if(&inner_if_ctx
, ctx
,
5569 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
5570 rel_patch_id
, ctx
->i32zero
, ""));
5572 /* Store the dynamic HS control word. */
5573 ac_build_buffer_store_dword(&ctx
->ac
, buffer
,
5574 LLVMConstInt(ctx
->i32
, 0x80000000, false),
5575 1, ctx
->i32zero
, tf_base
,
5576 0, 1, 0, true, false);
5577 ac_nir_build_endif(&inner_if_ctx
);
5579 /* Store the tessellation factors. */
5580 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec0
,
5581 MIN2(stride
, 4), byteoffset
, tf_base
,
5582 4, 1, 0, true, false);
5584 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec1
,
5585 stride
- 4, byteoffset
, tf_base
,
5586 20, 1, 0, true, false);
5588 //TODO store to offchip for TES to read - only if TES reads them
5590 LLVMValueRef inner_vec
, outer_vec
, tf_outer_offset
;
5591 LLVMValueRef tf_inner_offset
;
5592 unsigned param_outer
, param_inner
;
5594 param_outer
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
5595 tf_outer_offset
= get_tcs_tes_buffer_address(ctx
, NULL
,
5596 LLVMConstInt(ctx
->i32
, param_outer
, 0));
5598 outer_vec
= ac_build_gather_values(&ctx
->ac
, outer
,
5599 util_next_power_of_two(outer_comps
));
5601 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, outer_vec
,
5602 outer_comps
, tf_outer_offset
,
5603 ctx
->oc_lds
, 0, 1, 0, true, false);
5605 param_inner
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
5606 tf_inner_offset
= get_tcs_tes_buffer_address(ctx
, NULL
,
5607 LLVMConstInt(ctx
->i32
, param_inner
, 0));
5609 inner_vec
= inner_comps
== 1 ? inner
[0] :
5610 ac_build_gather_values(&ctx
->ac
, inner
, inner_comps
);
5611 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, inner_vec
,
5612 inner_comps
, tf_inner_offset
,
5613 ctx
->oc_lds
, 0, 1, 0, true, false);
5616 ac_nir_build_endif(&if_ctx
);
5620 handle_tcs_outputs_post(struct nir_to_llvm_context
*ctx
)
5622 write_tess_factors(ctx
);
5626 si_export_mrt_color(struct nir_to_llvm_context
*ctx
,
5627 LLVMValueRef
*color
, unsigned param
, bool is_last
,
5628 struct ac_export_args
*args
)
5631 si_llvm_init_export_args(ctx
, color
, param
,
5635 args
->valid_mask
= 1; /* whether the EXEC mask is valid */
5636 args
->done
= 1; /* DONE bit */
5637 } else if (!args
->enabled_channels
)
5638 return false; /* unnecessary NULL export */
5644 si_export_mrt_z(struct nir_to_llvm_context
*ctx
,
5645 LLVMValueRef depth
, LLVMValueRef stencil
,
5646 LLVMValueRef samplemask
)
5648 struct ac_export_args args
;
5650 args
.enabled_channels
= 0;
5651 args
.valid_mask
= 1;
5653 args
.target
= V_008DFC_SQ_EXP_MRTZ
;
5656 args
.out
[0] = LLVMGetUndef(ctx
->f32
); /* R, depth */
5657 args
.out
[1] = LLVMGetUndef(ctx
->f32
); /* G, stencil test val[0:7], stencil op val[8:15] */
5658 args
.out
[2] = LLVMGetUndef(ctx
->f32
); /* B, sample mask */
5659 args
.out
[3] = LLVMGetUndef(ctx
->f32
); /* A, alpha to mask */
5662 args
.out
[0] = depth
;
5663 args
.enabled_channels
|= 0x1;
5667 args
.out
[1] = stencil
;
5668 args
.enabled_channels
|= 0x2;
5672 args
.out
[2] = samplemask
;
5673 args
.enabled_channels
|= 0x4;
5676 /* SI (except OLAND) has a bug that it only looks
5677 * at the X writemask component. */
5678 if (ctx
->options
->chip_class
== SI
&&
5679 ctx
->options
->family
!= CHIP_OLAND
)
5680 args
.enabled_channels
|= 0x1;
5682 ac_build_export(&ctx
->ac
, &args
);
5686 handle_fs_outputs_post(struct nir_to_llvm_context
*ctx
)
5689 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
5690 struct ac_export_args color_args
[8];
5692 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5693 LLVMValueRef values
[4];
5695 if (!(ctx
->output_mask
& (1ull << i
)))
5698 if (i
== FRAG_RESULT_DEPTH
) {
5699 ctx
->shader_info
->fs
.writes_z
= true;
5700 depth
= to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
5701 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
5702 } else if (i
== FRAG_RESULT_STENCIL
) {
5703 ctx
->shader_info
->fs
.writes_stencil
= true;
5704 stencil
= to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
5705 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
5706 } else if (i
== FRAG_RESULT_SAMPLE_MASK
) {
5707 ctx
->shader_info
->fs
.writes_sample_mask
= true;
5708 samplemask
= to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
5709 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
5712 for (unsigned j
= 0; j
< 4; j
++)
5713 values
[j
] = to_float(&ctx
->ac
, LLVMBuildLoad(ctx
->builder
,
5714 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5716 if (!ctx
->shader_info
->fs
.writes_z
&& !ctx
->shader_info
->fs
.writes_stencil
&& !ctx
->shader_info
->fs
.writes_sample_mask
)
5717 last
= ctx
->output_mask
<= ((1ull << (i
+ 1)) - 1);
5719 bool ret
= si_export_mrt_color(ctx
, values
, V_008DFC_SQ_EXP_MRT
+ (i
- FRAG_RESULT_DATA0
), last
, &color_args
[index
]);
5725 for (unsigned i
= 0; i
< index
; i
++)
5726 ac_build_export(&ctx
->ac
, &color_args
[i
]);
5727 if (depth
|| stencil
|| samplemask
)
5728 si_export_mrt_z(ctx
, depth
, stencil
, samplemask
);
5730 si_export_mrt_color(ctx
, NULL
, V_008DFC_SQ_EXP_NULL
, true, &color_args
[0]);
5731 ac_build_export(&ctx
->ac
, &color_args
[0]);
5734 ctx
->shader_info
->fs
.output_mask
= index
? ((1ull << index
) - 1) : 0;
5738 emit_gs_epilogue(struct nir_to_llvm_context
*ctx
)
5740 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_NOP
| AC_SENDMSG_GS_DONE
, ctx
->gs_wave_id
);
5744 handle_shader_outputs_post(struct nir_to_llvm_context
*ctx
)
5746 switch (ctx
->stage
) {
5747 case MESA_SHADER_VERTEX
:
5748 if (ctx
->options
->key
.vs
.as_ls
)
5749 handle_ls_outputs_post(ctx
);
5750 else if (ctx
->options
->key
.vs
.as_es
)
5751 handle_es_outputs_post(ctx
, &ctx
->shader_info
->vs
.es_info
);
5753 handle_vs_outputs_post(ctx
, ctx
->options
->key
.vs
.export_prim_id
,
5754 &ctx
->shader_info
->vs
.outinfo
);
5756 case MESA_SHADER_FRAGMENT
:
5757 handle_fs_outputs_post(ctx
);
5759 case MESA_SHADER_GEOMETRY
:
5760 emit_gs_epilogue(ctx
);
5762 case MESA_SHADER_TESS_CTRL
:
5763 handle_tcs_outputs_post(ctx
);
5765 case MESA_SHADER_TESS_EVAL
:
5766 if (ctx
->options
->key
.tes
.as_es
)
5767 handle_es_outputs_post(ctx
, &ctx
->shader_info
->tes
.es_info
);
5769 handle_vs_outputs_post(ctx
, ctx
->options
->key
.tes
.export_prim_id
,
5770 &ctx
->shader_info
->tes
.outinfo
);
5778 handle_shared_compute_var(struct nir_to_llvm_context
*ctx
,
5779 struct nir_variable
*variable
, uint32_t *offset
, int idx
)
5781 unsigned size
= glsl_count_attribute_slots(variable
->type
, false);
5782 variable
->data
.driver_location
= *offset
;
5786 static void ac_llvm_finalize_module(struct nir_to_llvm_context
* ctx
)
5788 LLVMPassManagerRef passmgr
;
5789 /* Create the pass manager */
5790 passmgr
= LLVMCreateFunctionPassManagerForModule(
5793 /* This pass should eliminate all the load and store instructions */
5794 LLVMAddPromoteMemoryToRegisterPass(passmgr
);
5796 /* Add some optimization passes */
5797 LLVMAddScalarReplAggregatesPass(passmgr
);
5798 LLVMAddLICMPass(passmgr
);
5799 LLVMAddAggressiveDCEPass(passmgr
);
5800 LLVMAddCFGSimplificationPass(passmgr
);
5801 LLVMAddInstructionCombiningPass(passmgr
);
5804 LLVMInitializeFunctionPassManager(passmgr
);
5805 LLVMRunFunctionPassManager(passmgr
, ctx
->main_function
);
5806 LLVMFinalizeFunctionPassManager(passmgr
);
5808 LLVMDisposeBuilder(ctx
->builder
);
5809 LLVMDisposePassManager(passmgr
);
5813 ac_nir_eliminate_const_vs_outputs(struct nir_to_llvm_context
*ctx
)
5815 struct ac_vs_output_info
*outinfo
;
5817 switch (ctx
->stage
) {
5818 case MESA_SHADER_FRAGMENT
:
5819 case MESA_SHADER_COMPUTE
:
5820 case MESA_SHADER_TESS_CTRL
:
5821 case MESA_SHADER_GEOMETRY
:
5823 case MESA_SHADER_VERTEX
:
5824 if (ctx
->options
->key
.vs
.as_ls
||
5825 ctx
->options
->key
.vs
.as_es
)
5827 outinfo
= &ctx
->shader_info
->vs
.outinfo
;
5829 case MESA_SHADER_TESS_EVAL
:
5830 if (ctx
->options
->key
.vs
.as_es
)
5832 outinfo
= &ctx
->shader_info
->tes
.outinfo
;
5835 unreachable("Unhandled shader type");
5838 ac_optimize_vs_outputs(&ctx
->ac
,
5840 outinfo
->vs_output_param_offset
,
5842 &outinfo
->param_exports
);
5846 ac_setup_rings(struct nir_to_llvm_context
*ctx
)
5848 if ((ctx
->stage
== MESA_SHADER_VERTEX
&& ctx
->options
->key
.vs
.as_es
) ||
5849 (ctx
->stage
== MESA_SHADER_TESS_EVAL
&& ctx
->options
->key
.tes
.as_es
)) {
5850 ctx
->esgs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_ESGS_VS
, false));
5853 if (ctx
->is_gs_copy_shader
) {
5854 ctx
->gsvs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_GSVS_VS
, false));
5856 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
5858 ctx
->esgs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_ESGS_GS
, false));
5859 ctx
->gsvs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_GSVS_GS
, false));
5861 ctx
->gsvs_ring
= LLVMBuildBitCast(ctx
->builder
, ctx
->gsvs_ring
, ctx
->v4i32
, "");
5863 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->gsvs_num_entries
, LLVMConstInt(ctx
->i32
, 2, false), "");
5864 tmp
= LLVMBuildExtractElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->i32one
, "");
5865 tmp
= LLVMBuildOr(ctx
->builder
, tmp
, ctx
->gsvs_ring_stride
, "");
5866 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, tmp
, ctx
->i32one
, "");
5868 ctx
->gsvs_ring
= LLVMBuildBitCast(ctx
->builder
, ctx
->gsvs_ring
, ctx
->v16i8
, "");
5871 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
5872 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
5873 ctx
->hs_ring_tess_offchip
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_HS_TESS_OFFCHIP
, false));
5874 ctx
->hs_ring_tess_factor
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_HS_TESS_FACTOR
, false));
5879 ac_nir_get_max_workgroup_size(enum chip_class chip_class
,
5880 const struct nir_shader
*nir
)
5882 switch (nir
->stage
) {
5883 case MESA_SHADER_TESS_CTRL
:
5884 return chip_class
>= CIK
? 128 : 64;
5885 case MESA_SHADER_GEOMETRY
:
5887 case MESA_SHADER_COMPUTE
:
5893 unsigned max_workgroup_size
= nir
->info
.cs
.local_size
[0] *
5894 nir
->info
.cs
.local_size
[1] *
5895 nir
->info
.cs
.local_size
[2];
5896 return max_workgroup_size
;
5900 LLVMModuleRef
ac_translate_nir_to_llvm(LLVMTargetMachineRef tm
,
5901 struct nir_shader
*nir
,
5902 struct ac_shader_variant_info
*shader_info
,
5903 const struct ac_nir_compiler_options
*options
)
5905 struct nir_to_llvm_context ctx
= {0};
5906 struct nir_function
*func
;
5908 ctx
.options
= options
;
5909 ctx
.shader_info
= shader_info
;
5910 ctx
.context
= LLVMContextCreate();
5911 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
5913 ac_llvm_context_init(&ctx
.ac
, ctx
.context
);
5914 ctx
.ac
.module
= ctx
.module
;
5916 ctx
.has_ds_bpermute
= ctx
.options
->chip_class
>= VI
;
5918 memset(shader_info
, 0, sizeof(*shader_info
));
5920 ac_nir_shader_info_pass(nir
, options
, &shader_info
->info
);
5922 LLVMSetTarget(ctx
.module
, options
->supports_spill
? "amdgcn-mesa-mesa3d" : "amdgcn--");
5924 LLVMTargetDataRef data_layout
= LLVMCreateTargetDataLayout(tm
);
5925 char *data_layout_str
= LLVMCopyStringRepOfTargetData(data_layout
);
5926 LLVMSetDataLayout(ctx
.module
, data_layout_str
);
5927 LLVMDisposeTargetData(data_layout
);
5928 LLVMDisposeMessage(data_layout_str
);
5932 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
5933 ctx
.ac
.builder
= ctx
.builder
;
5934 ctx
.stage
= nir
->stage
;
5935 ctx
.max_workgroup_size
= ac_nir_get_max_workgroup_size(ctx
.options
->chip_class
, nir
);
5937 for (i
= 0; i
< AC_UD_MAX_SETS
; i
++)
5938 shader_info
->user_sgprs_locs
.descriptor_sets
[i
].sgpr_idx
= -1;
5939 for (i
= 0; i
< AC_UD_MAX_UD
; i
++)
5940 shader_info
->user_sgprs_locs
.shader_data
[i
].sgpr_idx
= -1;
5942 create_function(&ctx
);
5944 if (nir
->stage
== MESA_SHADER_COMPUTE
) {
5946 nir_foreach_variable(variable
, &nir
->shared
)
5950 uint32_t shared_size
= 0;
5952 LLVMTypeRef i8p
= LLVMPointerType(ctx
.i8
, LOCAL_ADDR_SPACE
);
5953 nir_foreach_variable(variable
, &nir
->shared
) {
5954 handle_shared_compute_var(&ctx
, variable
, &shared_size
, idx
);
5959 var
= LLVMAddGlobalInAddressSpace(ctx
.module
,
5960 LLVMArrayType(ctx
.i8
, shared_size
),
5963 LLVMSetAlignment(var
, 4);
5964 ctx
.shared_memory
= LLVMBuildBitCast(ctx
.builder
, var
, i8p
, "");
5966 } else if (nir
->stage
== MESA_SHADER_GEOMETRY
) {
5967 ctx
.gs_next_vertex
= ac_build_alloca(&ctx
, ctx
.i32
, "gs_next_vertex");
5969 ctx
.gs_max_out_vertices
= nir
->info
.gs
.vertices_out
;
5970 } else if (nir
->stage
== MESA_SHADER_TESS_EVAL
) {
5971 ctx
.tes_primitive_mode
= nir
->info
.tess
.primitive_mode
;
5974 ac_setup_rings(&ctx
);
5976 nir_foreach_variable(variable
, &nir
->inputs
)
5977 handle_shader_input_decl(&ctx
, variable
);
5979 if (nir
->stage
== MESA_SHADER_FRAGMENT
)
5980 handle_fs_inputs_pre(&ctx
, nir
);
5982 ctx
.num_output_clips
= nir
->info
.clip_distance_array_size
;
5983 ctx
.num_output_culls
= nir
->info
.cull_distance_array_size
;
5985 nir_foreach_variable(variable
, &nir
->outputs
)
5986 handle_shader_output_decl(&ctx
, variable
);
5988 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
5989 _mesa_key_pointer_equal
);
5990 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
5991 _mesa_key_pointer_equal
);
5993 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
5995 setup_locals(&ctx
, func
);
5997 visit_cf_list(&ctx
, &func
->impl
->body
);
5998 phi_post_pass(&ctx
);
6000 handle_shader_outputs_post(&ctx
);
6001 LLVMBuildRetVoid(ctx
.builder
);
6003 ac_llvm_finalize_module(&ctx
);
6005 ac_nir_eliminate_const_vs_outputs(&ctx
);
6007 ralloc_free(ctx
.defs
);
6008 ralloc_free(ctx
.phis
);
6010 if (nir
->stage
== MESA_SHADER_GEOMETRY
) {
6011 unsigned addclip
= ctx
.num_output_clips
+ ctx
.num_output_culls
> 4;
6012 shader_info
->gs
.gsvs_vertex_size
= (util_bitcount64(ctx
.output_mask
) + addclip
) * 16;
6013 shader_info
->gs
.max_gsvs_emit_size
= shader_info
->gs
.gsvs_vertex_size
*
6014 nir
->info
.gs
.vertices_out
;
6015 } else if (nir
->stage
== MESA_SHADER_TESS_CTRL
) {
6016 shader_info
->tcs
.outputs_written
= ctx
.tess_outputs_written
;
6017 shader_info
->tcs
.patch_outputs_written
= ctx
.tess_patch_outputs_written
;
6018 } else if (nir
->stage
== MESA_SHADER_VERTEX
&& ctx
.options
->key
.vs
.as_ls
) {
6019 shader_info
->vs
.outputs_written
= ctx
.tess_outputs_written
;
6025 static void ac_diagnostic_handler(LLVMDiagnosticInfoRef di
, void *context
)
6027 unsigned *retval
= (unsigned *)context
;
6028 LLVMDiagnosticSeverity severity
= LLVMGetDiagInfoSeverity(di
);
6029 char *description
= LLVMGetDiagInfoDescription(di
);
6031 if (severity
== LLVMDSError
) {
6033 fprintf(stderr
, "LLVM triggered Diagnostic Handler: %s\n",
6037 LLVMDisposeMessage(description
);
6040 static unsigned ac_llvm_compile(LLVMModuleRef M
,
6041 struct ac_shader_binary
*binary
,
6042 LLVMTargetMachineRef tm
)
6044 unsigned retval
= 0;
6046 LLVMContextRef llvm_ctx
;
6047 LLVMMemoryBufferRef out_buffer
;
6048 unsigned buffer_size
;
6049 const char *buffer_data
;
6052 /* Setup Diagnostic Handler*/
6053 llvm_ctx
= LLVMGetModuleContext(M
);
6055 LLVMContextSetDiagnosticHandler(llvm_ctx
, ac_diagnostic_handler
,
6059 mem_err
= LLVMTargetMachineEmitToMemoryBuffer(tm
, M
, LLVMObjectFile
,
6062 /* Process Errors/Warnings */
6064 fprintf(stderr
, "%s: %s", __FUNCTION__
, err
);
6070 /* Extract Shader Code*/
6071 buffer_size
= LLVMGetBufferSize(out_buffer
);
6072 buffer_data
= LLVMGetBufferStart(out_buffer
);
6074 ac_elf_read(buffer_data
, buffer_size
, binary
);
6077 LLVMDisposeMemoryBuffer(out_buffer
);
6083 static void ac_compile_llvm_module(LLVMTargetMachineRef tm
,
6084 LLVMModuleRef llvm_module
,
6085 struct ac_shader_binary
*binary
,
6086 struct ac_shader_config
*config
,
6087 struct ac_shader_variant_info
*shader_info
,
6088 gl_shader_stage stage
,
6089 bool dump_shader
, bool supports_spill
)
6092 ac_dump_module(llvm_module
);
6094 memset(binary
, 0, sizeof(*binary
));
6095 int v
= ac_llvm_compile(llvm_module
, binary
, tm
);
6097 fprintf(stderr
, "compile failed\n");
6101 fprintf(stderr
, "disasm:\n%s\n", binary
->disasm_string
);
6103 ac_shader_binary_read_config(binary
, config
, 0, supports_spill
);
6105 LLVMContextRef ctx
= LLVMGetModuleContext(llvm_module
);
6106 LLVMDisposeModule(llvm_module
);
6107 LLVMContextDispose(ctx
);
6109 if (stage
== MESA_SHADER_FRAGMENT
) {
6110 shader_info
->num_input_vgprs
= 0;
6111 if (G_0286CC_PERSP_SAMPLE_ENA(config
->spi_ps_input_addr
))
6112 shader_info
->num_input_vgprs
+= 2;
6113 if (G_0286CC_PERSP_CENTER_ENA(config
->spi_ps_input_addr
))
6114 shader_info
->num_input_vgprs
+= 2;
6115 if (G_0286CC_PERSP_CENTROID_ENA(config
->spi_ps_input_addr
))
6116 shader_info
->num_input_vgprs
+= 2;
6117 if (G_0286CC_PERSP_PULL_MODEL_ENA(config
->spi_ps_input_addr
))
6118 shader_info
->num_input_vgprs
+= 3;
6119 if (G_0286CC_LINEAR_SAMPLE_ENA(config
->spi_ps_input_addr
))
6120 shader_info
->num_input_vgprs
+= 2;
6121 if (G_0286CC_LINEAR_CENTER_ENA(config
->spi_ps_input_addr
))
6122 shader_info
->num_input_vgprs
+= 2;
6123 if (G_0286CC_LINEAR_CENTROID_ENA(config
->spi_ps_input_addr
))
6124 shader_info
->num_input_vgprs
+= 2;
6125 if (G_0286CC_LINE_STIPPLE_TEX_ENA(config
->spi_ps_input_addr
))
6126 shader_info
->num_input_vgprs
+= 1;
6127 if (G_0286CC_POS_X_FLOAT_ENA(config
->spi_ps_input_addr
))
6128 shader_info
->num_input_vgprs
+= 1;
6129 if (G_0286CC_POS_Y_FLOAT_ENA(config
->spi_ps_input_addr
))
6130 shader_info
->num_input_vgprs
+= 1;
6131 if (G_0286CC_POS_Z_FLOAT_ENA(config
->spi_ps_input_addr
))
6132 shader_info
->num_input_vgprs
+= 1;
6133 if (G_0286CC_POS_W_FLOAT_ENA(config
->spi_ps_input_addr
))
6134 shader_info
->num_input_vgprs
+= 1;
6135 if (G_0286CC_FRONT_FACE_ENA(config
->spi_ps_input_addr
))
6136 shader_info
->num_input_vgprs
+= 1;
6137 if (G_0286CC_ANCILLARY_ENA(config
->spi_ps_input_addr
))
6138 shader_info
->num_input_vgprs
+= 1;
6139 if (G_0286CC_SAMPLE_COVERAGE_ENA(config
->spi_ps_input_addr
))
6140 shader_info
->num_input_vgprs
+= 1;
6141 if (G_0286CC_POS_FIXED_PT_ENA(config
->spi_ps_input_addr
))
6142 shader_info
->num_input_vgprs
+= 1;
6144 config
->num_vgprs
= MAX2(config
->num_vgprs
, shader_info
->num_input_vgprs
);
6146 /* +3 for scratch wave offset and VCC */
6147 config
->num_sgprs
= MAX2(config
->num_sgprs
,
6148 shader_info
->num_input_sgprs
+ 3);
6151 void ac_compile_nir_shader(LLVMTargetMachineRef tm
,
6152 struct ac_shader_binary
*binary
,
6153 struct ac_shader_config
*config
,
6154 struct ac_shader_variant_info
*shader_info
,
6155 struct nir_shader
*nir
,
6156 const struct ac_nir_compiler_options
*options
,
6160 LLVMModuleRef llvm_module
= ac_translate_nir_to_llvm(tm
, nir
, shader_info
,
6163 ac_compile_llvm_module(tm
, llvm_module
, binary
, config
, shader_info
, nir
->stage
, dump_shader
, options
->supports_spill
);
6164 switch (nir
->stage
) {
6165 case MESA_SHADER_COMPUTE
:
6166 for (int i
= 0; i
< 3; ++i
)
6167 shader_info
->cs
.block_size
[i
] = nir
->info
.cs
.local_size
[i
];
6169 case MESA_SHADER_FRAGMENT
:
6170 shader_info
->fs
.early_fragment_test
= nir
->info
.fs
.early_fragment_tests
;
6172 case MESA_SHADER_GEOMETRY
:
6173 shader_info
->gs
.vertices_in
= nir
->info
.gs
.vertices_in
;
6174 shader_info
->gs
.vertices_out
= nir
->info
.gs
.vertices_out
;
6175 shader_info
->gs
.output_prim
= nir
->info
.gs
.output_primitive
;
6176 shader_info
->gs
.invocations
= nir
->info
.gs
.invocations
;
6178 case MESA_SHADER_TESS_EVAL
:
6179 shader_info
->tes
.primitive_mode
= nir
->info
.tess
.primitive_mode
;
6180 shader_info
->tes
.spacing
= nir
->info
.tess
.spacing
;
6181 shader_info
->tes
.ccw
= nir
->info
.tess
.ccw
;
6182 shader_info
->tes
.point_mode
= nir
->info
.tess
.point_mode
;
6183 shader_info
->tes
.as_es
= options
->key
.tes
.as_es
;
6185 case MESA_SHADER_TESS_CTRL
:
6186 shader_info
->tcs
.tcs_vertices_out
= nir
->info
.tess
.tcs_vertices_out
;
6188 case MESA_SHADER_VERTEX
:
6189 shader_info
->vs
.as_es
= options
->key
.vs
.as_es
;
6190 shader_info
->vs
.as_ls
= options
->key
.vs
.as_ls
;
6191 /* in LS mode we need at least 1, invocation id needs 3, handled elsewhere */
6192 if (options
->key
.vs
.as_ls
)
6193 shader_info
->vs
.vgpr_comp_cnt
= MAX2(1, shader_info
->vs
.vgpr_comp_cnt
);
6201 ac_gs_copy_shader_emit(struct nir_to_llvm_context
*ctx
)
6203 LLVMValueRef args
[9];
6204 args
[0] = ctx
->gsvs_ring
;
6205 args
[1] = LLVMBuildMul(ctx
->builder
, ctx
->vertex_id
, LLVMConstInt(ctx
->i32
, 4, false), "");
6206 args
[3] = ctx
->i32zero
;
6207 args
[4] = ctx
->i32one
; /* OFFEN */
6208 args
[5] = ctx
->i32zero
; /* IDXEN */
6209 args
[6] = ctx
->i32one
; /* GLC */
6210 args
[7] = ctx
->i32one
; /* SLC */
6211 args
[8] = ctx
->i32zero
; /* TFE */
6215 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
6219 if (!(ctx
->output_mask
& (1ull << i
)))
6222 if (i
== VARYING_SLOT_CLIP_DIST0
) {
6223 /* unpack clip and cull from a single set of slots */
6224 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
6229 for (unsigned j
= 0; j
< length
; j
++) {
6231 args
[2] = LLVMConstInt(ctx
->i32
,
6233 ctx
->gs_max_out_vertices
* 16 * 4, false);
6235 value
= ac_build_intrinsic(&ctx
->ac
,
6236 "llvm.SI.buffer.load.dword.i32.i32",
6238 AC_FUNC_ATTR_READONLY
|
6239 AC_FUNC_ATTR_LEGACY
);
6241 LLVMBuildStore(ctx
->builder
,
6242 to_float(&ctx
->ac
, value
), ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)]);
6246 handle_vs_outputs_post(ctx
, false, &ctx
->shader_info
->vs
.outinfo
);
6249 void ac_create_gs_copy_shader(LLVMTargetMachineRef tm
,
6250 struct nir_shader
*geom_shader
,
6251 struct ac_shader_binary
*binary
,
6252 struct ac_shader_config
*config
,
6253 struct ac_shader_variant_info
*shader_info
,
6254 const struct ac_nir_compiler_options
*options
,
6257 struct nir_to_llvm_context ctx
= {0};
6258 ctx
.context
= LLVMContextCreate();
6259 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
6260 ctx
.options
= options
;
6261 ctx
.shader_info
= shader_info
;
6263 ac_llvm_context_init(&ctx
.ac
, ctx
.context
);
6264 ctx
.ac
.module
= ctx
.module
;
6266 ctx
.is_gs_copy_shader
= true;
6267 LLVMSetTarget(ctx
.module
, "amdgcn--");
6270 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
6271 ctx
.ac
.builder
= ctx
.builder
;
6272 ctx
.stage
= MESA_SHADER_VERTEX
;
6274 create_function(&ctx
);
6276 ctx
.gs_max_out_vertices
= geom_shader
->info
.gs
.vertices_out
;
6277 ac_setup_rings(&ctx
);
6279 ctx
.num_output_clips
= geom_shader
->info
.clip_distance_array_size
;
6280 ctx
.num_output_culls
= geom_shader
->info
.cull_distance_array_size
;
6282 nir_foreach_variable(variable
, &geom_shader
->outputs
)
6283 handle_shader_output_decl(&ctx
, variable
);
6285 ac_gs_copy_shader_emit(&ctx
);
6287 LLVMBuildRetVoid(ctx
.builder
);
6289 ac_llvm_finalize_module(&ctx
);
6291 ac_compile_llvm_module(tm
, ctx
.module
, binary
, config
, shader_info
,
6293 dump_shader
, options
->supports_spill
);