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_util.h"
26 #include "ac_binary.h"
29 #include "../vulkan/radv_descriptor_set.h"
30 #include "util/bitscan.h"
31 #include <llvm-c/Transforms/Scalar.h>
33 enum radeon_llvm_calling_convention
{
34 RADEON_LLVM_AMDGPU_VS
= 87,
35 RADEON_LLVM_AMDGPU_GS
= 88,
36 RADEON_LLVM_AMDGPU_PS
= 89,
37 RADEON_LLVM_AMDGPU_CS
= 90,
40 #define CONST_ADDR_SPACE 2
41 #define LOCAL_ADDR_SPACE 3
43 #define RADEON_LLVM_MAX_INPUTS (VARYING_SLOT_VAR31 + 1)
44 #define RADEON_LLVM_MAX_OUTPUTS (VARYING_SLOT_VAR31 + 1)
47 #define SENDMSG_GS_DONE 3
49 #define SENDMSG_GS_OP_NOP (0 << 4)
50 #define SENDMSG_GS_OP_CUT (1 << 4)
51 #define SENDMSG_GS_OP_EMIT (2 << 4)
52 #define SENDMSG_GS_OP_EMIT_CUT (3 << 4)
61 struct nir_to_llvm_context
{
62 struct ac_llvm_context ac
;
63 const struct ac_nir_compiler_options
*options
;
64 struct ac_shader_variant_info
*shader_info
;
66 LLVMContextRef context
;
68 LLVMBuilderRef builder
;
69 LLVMValueRef main_function
;
71 struct hash_table
*defs
;
72 struct hash_table
*phis
;
74 LLVMValueRef descriptor_sets
[AC_UD_MAX_SETS
];
75 LLVMValueRef ring_offsets
;
76 LLVMValueRef push_constants
;
77 LLVMValueRef num_work_groups
;
78 LLVMValueRef workgroup_ids
;
79 LLVMValueRef local_invocation_ids
;
82 LLVMValueRef vertex_buffers
;
83 LLVMValueRef base_vertex
;
84 LLVMValueRef start_instance
;
85 LLVMValueRef vertex_id
;
86 LLVMValueRef rel_auto_id
;
87 LLVMValueRef vs_prim_id
;
88 LLVMValueRef instance_id
;
90 LLVMValueRef es2gs_offset
;
92 LLVMValueRef gsvs_ring_stride
;
93 LLVMValueRef gsvs_num_entries
;
94 LLVMValueRef gs2vs_offset
;
95 LLVMValueRef gs_wave_id
;
96 LLVMValueRef gs_vtx_offset
[6];
97 LLVMValueRef gs_prim_id
, gs_invocation_id
;
99 LLVMValueRef esgs_ring
;
100 LLVMValueRef gsvs_ring
;
102 LLVMValueRef prim_mask
;
103 LLVMValueRef sample_positions
;
104 LLVMValueRef persp_sample
, persp_center
, persp_centroid
;
105 LLVMValueRef linear_sample
, linear_center
, linear_centroid
;
106 LLVMValueRef front_face
;
107 LLVMValueRef ancillary
;
108 LLVMValueRef frag_pos
[4];
110 LLVMBasicBlockRef continue_block
;
111 LLVMBasicBlockRef break_block
;
129 LLVMValueRef i32zero
;
131 LLVMValueRef f32zero
;
133 LLVMValueRef v4f32empty
;
135 unsigned range_md_kind
;
136 unsigned uniform_md_kind
;
137 unsigned invariant_load_md_kind
;
138 LLVMValueRef empty_md
;
139 gl_shader_stage stage
;
142 LLVMValueRef inputs
[RADEON_LLVM_MAX_INPUTS
* 4];
143 LLVMValueRef outputs
[RADEON_LLVM_MAX_OUTPUTS
* 4];
145 LLVMValueRef shared_memory
;
147 uint64_t output_mask
;
149 LLVMValueRef
*locals
;
154 bool has_ds_bpermute
;
156 bool is_gs_copy_shader
;
157 LLVMValueRef gs_next_vertex
;
158 unsigned gs_max_out_vertices
;
162 LLVMValueRef args
[12];
164 LLVMTypeRef dst_type
;
168 static LLVMValueRef
get_sampler_desc(struct nir_to_llvm_context
*ctx
,
169 nir_deref_var
*deref
,
170 enum desc_type desc_type
);
171 static unsigned radeon_llvm_reg_index_soa(unsigned index
, unsigned chan
)
173 return (index
* 4) + chan
;
176 static unsigned llvm_get_type_size(LLVMTypeRef type
)
178 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
181 case LLVMIntegerTypeKind
:
182 return LLVMGetIntTypeWidth(type
) / 8;
183 case LLVMFloatTypeKind
:
185 case LLVMPointerTypeKind
:
187 case LLVMVectorTypeKind
:
188 return LLVMGetVectorSize(type
) *
189 llvm_get_type_size(LLVMGetElementType(type
));
196 static void set_llvm_calling_convention(LLVMValueRef func
,
197 gl_shader_stage stage
)
199 enum radeon_llvm_calling_convention calling_conv
;
202 case MESA_SHADER_VERTEX
:
203 case MESA_SHADER_TESS_CTRL
:
204 case MESA_SHADER_TESS_EVAL
:
205 calling_conv
= RADEON_LLVM_AMDGPU_VS
;
207 case MESA_SHADER_GEOMETRY
:
208 calling_conv
= RADEON_LLVM_AMDGPU_GS
;
210 case MESA_SHADER_FRAGMENT
:
211 calling_conv
= RADEON_LLVM_AMDGPU_PS
;
213 case MESA_SHADER_COMPUTE
:
214 calling_conv
= RADEON_LLVM_AMDGPU_CS
;
217 unreachable("Unhandle shader type");
220 LLVMSetFunctionCallConv(func
, calling_conv
);
224 create_llvm_function(LLVMContextRef ctx
, LLVMModuleRef module
,
225 LLVMBuilderRef builder
, LLVMTypeRef
*return_types
,
226 unsigned num_return_elems
, LLVMTypeRef
*param_types
,
227 unsigned param_count
, unsigned array_params_mask
,
228 unsigned sgpr_params
, bool unsafe_math
)
230 LLVMTypeRef main_function_type
, ret_type
;
231 LLVMBasicBlockRef main_function_body
;
233 if (num_return_elems
)
234 ret_type
= LLVMStructTypeInContext(ctx
, return_types
,
235 num_return_elems
, true);
237 ret_type
= LLVMVoidTypeInContext(ctx
);
239 /* Setup the function */
241 LLVMFunctionType(ret_type
, param_types
, param_count
, 0);
242 LLVMValueRef main_function
=
243 LLVMAddFunction(module
, "main", main_function_type
);
245 LLVMAppendBasicBlockInContext(ctx
, main_function
, "main_body");
246 LLVMPositionBuilderAtEnd(builder
, main_function_body
);
248 LLVMSetFunctionCallConv(main_function
, RADEON_LLVM_AMDGPU_CS
);
249 for (unsigned i
= 0; i
< sgpr_params
; ++i
) {
250 if (array_params_mask
& (1 << i
)) {
251 LLVMValueRef P
= LLVMGetParam(main_function
, i
);
252 ac_add_function_attr(main_function
, i
+ 1, AC_FUNC_ATTR_BYVAL
);
253 ac_add_attr_dereferenceable(P
, UINT64_MAX
);
256 ac_add_function_attr(main_function
, i
+ 1, AC_FUNC_ATTR_INREG
);
261 /* These were copied from some LLVM test. */
262 LLVMAddTargetDependentFunctionAttr(main_function
,
263 "less-precise-fpmad",
265 LLVMAddTargetDependentFunctionAttr(main_function
,
268 LLVMAddTargetDependentFunctionAttr(main_function
,
271 LLVMAddTargetDependentFunctionAttr(main_function
,
275 return main_function
;
278 static LLVMTypeRef
const_array(LLVMTypeRef elem_type
, int num_elements
)
280 return LLVMPointerType(LLVMArrayType(elem_type
, num_elements
),
284 static LLVMValueRef
get_shared_memory_ptr(struct nir_to_llvm_context
*ctx
,
292 offset
= LLVMConstInt(ctx
->i32
, idx
, false);
294 ptr
= ctx
->shared_memory
;
295 ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &offset
, 1, "");
296 addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
297 ptr
= LLVMBuildBitCast(ctx
->builder
, ptr
, LLVMPointerType(type
, addr_space
), "");
301 static LLVMValueRef
to_integer(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
)
303 LLVMTypeRef type
= LLVMTypeOf(v
);
304 if (type
== ctx
->f32
) {
305 return LLVMBuildBitCast(ctx
->builder
, v
, ctx
->i32
, "");
306 } else if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
307 LLVMTypeRef elem_type
= LLVMGetElementType(type
);
308 if (elem_type
== ctx
->f32
) {
309 LLVMTypeRef nt
= LLVMVectorType(ctx
->i32
, LLVMGetVectorSize(type
));
310 return LLVMBuildBitCast(ctx
->builder
, v
, nt
, "");
316 static LLVMValueRef
to_float(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
)
318 LLVMTypeRef type
= LLVMTypeOf(v
);
319 if (type
== ctx
->i32
) {
320 return LLVMBuildBitCast(ctx
->builder
, v
, ctx
->f32
, "");
321 } else if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
322 LLVMTypeRef elem_type
= LLVMGetElementType(type
);
323 if (elem_type
== ctx
->i32
) {
324 LLVMTypeRef nt
= LLVMVectorType(ctx
->f32
, LLVMGetVectorSize(type
));
325 return LLVMBuildBitCast(ctx
->builder
, v
, nt
, "");
331 static LLVMValueRef
unpack_param(struct nir_to_llvm_context
*ctx
,
332 LLVMValueRef param
, unsigned rshift
,
335 LLVMValueRef value
= param
;
337 value
= LLVMBuildLShr(ctx
->builder
, value
,
338 LLVMConstInt(ctx
->i32
, rshift
, false), "");
340 if (rshift
+ bitwidth
< 32) {
341 unsigned mask
= (1 << bitwidth
) - 1;
342 value
= LLVMBuildAnd(ctx
->builder
, value
,
343 LLVMConstInt(ctx
->i32
, mask
, false), "");
348 static LLVMValueRef
build_gep0(struct nir_to_llvm_context
*ctx
,
349 LLVMValueRef base_ptr
, LLVMValueRef index
)
351 LLVMValueRef indices
[2] = {
355 return LLVMBuildGEP(ctx
->builder
, base_ptr
,
359 static LLVMValueRef
build_indexed_load(struct nir_to_llvm_context
*ctx
,
360 LLVMValueRef base_ptr
, LLVMValueRef index
,
363 LLVMValueRef pointer
;
364 pointer
= build_gep0(ctx
, base_ptr
, index
);
366 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
367 return LLVMBuildLoad(ctx
->builder
, pointer
, "");
370 static LLVMValueRef
build_indexed_load_const(struct nir_to_llvm_context
*ctx
,
371 LLVMValueRef base_ptr
, LLVMValueRef index
)
373 LLVMValueRef result
= build_indexed_load(ctx
, base_ptr
, index
, true);
374 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
378 static void build_tbuffer_store(struct nir_to_llvm_context
*ctx
,
381 unsigned num_channels
,
383 LLVMValueRef soffset
,
384 unsigned inst_offset
,
393 LLVMValueRef args
[] = {
396 LLVMConstInt(ctx
->i32
, num_channels
, 0),
399 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
400 LLVMConstInt(ctx
->i32
, dfmt
, 0),
401 LLVMConstInt(ctx
->i32
, nfmt
, 0),
402 LLVMConstInt(ctx
->i32
, offen
, 0),
403 LLVMConstInt(ctx
->i32
, idxen
, 0),
404 LLVMConstInt(ctx
->i32
, glc
, 0),
405 LLVMConstInt(ctx
->i32
, slc
, 0),
406 LLVMConstInt(ctx
->i32
, tfe
, 0)
409 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
410 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
411 const char *types
[] = {"i32", "v2i32", "v4i32"};
413 snprintf(name
, sizeof(name
), "llvm.SI.tbuffer.store.%s", types
[func
]);
415 ac_emit_llvm_intrinsic(&ctx
->ac
, name
, ctx
->voidt
,
416 args
, ARRAY_SIZE(args
), 0);
420 static void set_userdata_location(struct ac_userdata_info
*ud_info
, uint8_t sgpr_idx
, uint8_t num_sgprs
)
422 ud_info
->sgpr_idx
= sgpr_idx
;
423 ud_info
->num_sgprs
= num_sgprs
;
424 ud_info
->indirect
= false;
425 ud_info
->indirect_offset
= 0;
428 static void set_userdata_location_shader(struct nir_to_llvm_context
*ctx
,
429 int idx
, uint8_t sgpr_idx
, uint8_t num_sgprs
)
431 set_userdata_location(&ctx
->shader_info
->user_sgprs_locs
.shader_data
[idx
], sgpr_idx
, num_sgprs
);
435 static void set_userdata_location_indirect(struct ac_userdata_info
*ud_info
, uint8_t sgpr_idx
, uint8_t num_sgprs
,
436 uint32_t indirect_offset
)
438 ud_info
->sgpr_idx
= sgpr_idx
;
439 ud_info
->num_sgprs
= num_sgprs
;
440 ud_info
->indirect
= true;
441 ud_info
->indirect_offset
= indirect_offset
;
445 static void create_function(struct nir_to_llvm_context
*ctx
)
447 LLVMTypeRef arg_types
[23];
448 unsigned arg_idx
= 0;
449 unsigned array_params_mask
= 0;
450 unsigned sgpr_count
= 0, user_sgpr_count
;
452 unsigned num_sets
= ctx
->options
->layout
? ctx
->options
->layout
->num_sets
: 0;
453 unsigned user_sgpr_idx
;
454 bool need_push_constants
;
455 bool need_ring_offsets
= false;
457 /* until we sort out scratch/global buffers always assign ring offsets for gs/vs/es */
458 if (ctx
->stage
== MESA_SHADER_GEOMETRY
||
459 ctx
->stage
== MESA_SHADER_VERTEX
||
460 ctx
->is_gs_copy_shader
)
461 need_ring_offsets
= true;
463 need_push_constants
= true;
464 if (!ctx
->options
->layout
)
465 need_push_constants
= false;
466 else if (!ctx
->options
->layout
->push_constant_size
&&
467 !ctx
->options
->layout
->dynamic_offset_count
)
468 need_push_constants
= false;
470 if (need_ring_offsets
&& !ctx
->options
->supports_spill
) {
471 arg_types
[arg_idx
++] = const_array(ctx
->v16i8
, 8); /* address of rings */
474 /* 1 for each descriptor set */
475 for (unsigned i
= 0; i
< num_sets
; ++i
) {
476 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& (1 << ctx
->stage
)) {
477 array_params_mask
|= (1 << arg_idx
);
478 arg_types
[arg_idx
++] = const_array(ctx
->i8
, 1024 * 1024);
482 if (need_push_constants
) {
483 /* 1 for push constants and dynamic descriptors */
484 array_params_mask
|= (1 << arg_idx
);
485 arg_types
[arg_idx
++] = const_array(ctx
->i8
, 1024 * 1024);
488 switch (ctx
->stage
) {
489 case MESA_SHADER_COMPUTE
:
490 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3); /* grid size */
491 user_sgpr_count
= arg_idx
;
492 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3);
493 arg_types
[arg_idx
++] = ctx
->i32
;
494 sgpr_count
= arg_idx
;
496 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3);
498 case MESA_SHADER_VERTEX
:
499 if (!ctx
->is_gs_copy_shader
) {
500 arg_types
[arg_idx
++] = const_array(ctx
->v16i8
, 16); /* vertex buffers */
501 arg_types
[arg_idx
++] = ctx
->i32
; // base vertex
502 arg_types
[arg_idx
++] = ctx
->i32
; // start instance
504 user_sgpr_count
= arg_idx
;
505 if (ctx
->options
->key
.vs
.as_es
)
506 arg_types
[arg_idx
++] = ctx
->i32
; //es2gs offset
507 sgpr_count
= arg_idx
;
508 arg_types
[arg_idx
++] = ctx
->i32
; // vertex id
509 if (!ctx
->is_gs_copy_shader
) {
510 arg_types
[arg_idx
++] = ctx
->i32
; // rel auto id
511 arg_types
[arg_idx
++] = ctx
->i32
; // vs prim id
512 arg_types
[arg_idx
++] = ctx
->i32
; // instance id
515 case MESA_SHADER_GEOMETRY
:
516 arg_types
[arg_idx
++] = ctx
->i32
; // gsvs stride
517 arg_types
[arg_idx
++] = ctx
->i32
; // gsvs num entires
518 user_sgpr_count
= arg_idx
;
519 arg_types
[arg_idx
++] = ctx
->i32
; // gs2vs offset
520 arg_types
[arg_idx
++] = ctx
->i32
; // wave id
521 sgpr_count
= arg_idx
;
522 arg_types
[arg_idx
++] = ctx
->i32
; // vtx0
523 arg_types
[arg_idx
++] = ctx
->i32
; // vtx1
524 arg_types
[arg_idx
++] = ctx
->i32
; // prim id
525 arg_types
[arg_idx
++] = ctx
->i32
; // vtx2
526 arg_types
[arg_idx
++] = ctx
->i32
; // vtx3
527 arg_types
[arg_idx
++] = ctx
->i32
; // vtx4
528 arg_types
[arg_idx
++] = ctx
->i32
; // vtx5
529 arg_types
[arg_idx
++] = ctx
->i32
; // GS instance id
531 case MESA_SHADER_FRAGMENT
:
532 arg_types
[arg_idx
++] = const_array(ctx
->f32
, 32); /* sample positions */
533 user_sgpr_count
= arg_idx
;
534 arg_types
[arg_idx
++] = ctx
->i32
; /* prim mask */
535 sgpr_count
= arg_idx
;
536 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp sample */
537 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp center */
538 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp centroid */
539 arg_types
[arg_idx
++] = ctx
->v3i32
; /* persp pull model */
540 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear sample */
541 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear center */
542 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear centroid */
543 arg_types
[arg_idx
++] = ctx
->f32
; /* line stipple tex */
544 arg_types
[arg_idx
++] = ctx
->f32
; /* pos x float */
545 arg_types
[arg_idx
++] = ctx
->f32
; /* pos y float */
546 arg_types
[arg_idx
++] = ctx
->f32
; /* pos z float */
547 arg_types
[arg_idx
++] = ctx
->f32
; /* pos w float */
548 arg_types
[arg_idx
++] = ctx
->i32
; /* front face */
549 arg_types
[arg_idx
++] = ctx
->i32
; /* ancillary */
550 arg_types
[arg_idx
++] = ctx
->f32
; /* sample coverage */
551 arg_types
[arg_idx
++] = ctx
->i32
; /* fixed pt */
554 unreachable("Shader stage not implemented");
557 ctx
->main_function
= create_llvm_function(
558 ctx
->context
, ctx
->module
, ctx
->builder
, NULL
, 0, arg_types
,
559 arg_idx
, array_params_mask
, sgpr_count
, ctx
->options
->unsafe_math
);
560 set_llvm_calling_convention(ctx
->main_function
, ctx
->stage
);
562 ctx
->shader_info
->num_input_sgprs
= 0;
563 ctx
->shader_info
->num_input_vgprs
= 0;
565 ctx
->shader_info
->num_user_sgprs
= ctx
->options
->supports_spill
? 2 : 0;
566 for (i
= 0; i
< user_sgpr_count
; i
++)
567 ctx
->shader_info
->num_user_sgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
569 ctx
->shader_info
->num_input_sgprs
= ctx
->shader_info
->num_user_sgprs
;
570 for (; i
< sgpr_count
; i
++)
571 ctx
->shader_info
->num_input_sgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
573 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
574 for (; i
< arg_idx
; ++i
)
575 ctx
->shader_info
->num_input_vgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
580 if (ctx
->options
->supports_spill
|| need_ring_offsets
) {
581 set_userdata_location_shader(ctx
, AC_UD_SCRATCH_RING_OFFSETS
, user_sgpr_idx
, 2);
583 if (ctx
->options
->supports_spill
) {
584 ctx
->ring_offsets
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.amdgcn.implicit.buffer.ptr",
585 LLVMPointerType(ctx
->i8
, CONST_ADDR_SPACE
),
586 NULL
, 0, AC_FUNC_ATTR_READNONE
);
587 ctx
->ring_offsets
= LLVMBuildBitCast(ctx
->builder
, ctx
->ring_offsets
,
588 const_array(ctx
->v16i8
, 8), "");
590 ctx
->ring_offsets
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
593 for (unsigned i
= 0; i
< num_sets
; ++i
) {
594 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& (1 << ctx
->stage
)) {
595 set_userdata_location(&ctx
->shader_info
->user_sgprs_locs
.descriptor_sets
[i
], user_sgpr_idx
, 2);
597 ctx
->descriptor_sets
[i
] =
598 LLVMGetParam(ctx
->main_function
, arg_idx
++);
600 ctx
->descriptor_sets
[i
] = NULL
;
603 if (need_push_constants
) {
604 ctx
->push_constants
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
605 set_userdata_location_shader(ctx
, AC_UD_PUSH_CONSTANTS
, user_sgpr_idx
, 2);
609 switch (ctx
->stage
) {
610 case MESA_SHADER_COMPUTE
:
611 set_userdata_location_shader(ctx
, AC_UD_CS_GRID_SIZE
, user_sgpr_idx
, 3);
613 ctx
->num_work_groups
=
614 LLVMGetParam(ctx
->main_function
, arg_idx
++);
616 LLVMGetParam(ctx
->main_function
, arg_idx
++);
618 LLVMGetParam(ctx
->main_function
, arg_idx
++);
619 ctx
->local_invocation_ids
=
620 LLVMGetParam(ctx
->main_function
, arg_idx
++);
622 case MESA_SHADER_VERTEX
:
623 if (!ctx
->is_gs_copy_shader
) {
624 set_userdata_location_shader(ctx
, AC_UD_VS_VERTEX_BUFFERS
, user_sgpr_idx
, 2);
626 ctx
->vertex_buffers
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
627 set_userdata_location_shader(ctx
, AC_UD_VS_BASE_VERTEX_START_INSTANCE
, user_sgpr_idx
, 2);
629 ctx
->base_vertex
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
630 ctx
->start_instance
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
632 if (ctx
->options
->key
.vs
.as_es
)
633 ctx
->es2gs_offset
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
634 ctx
->vertex_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
635 if (!ctx
->is_gs_copy_shader
) {
636 ctx
->rel_auto_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
637 ctx
->vs_prim_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
638 ctx
->instance_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
641 case MESA_SHADER_GEOMETRY
:
642 set_userdata_location_shader(ctx
, AC_UD_GS_VS_RING_STRIDE_ENTRIES
, user_sgpr_idx
, 2);
644 ctx
->gsvs_ring_stride
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
645 ctx
->gsvs_num_entries
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
646 ctx
->gs2vs_offset
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
647 ctx
->gs_wave_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
648 ctx
->gs_vtx_offset
[0] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
649 ctx
->gs_vtx_offset
[1] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
650 ctx
->gs_prim_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
651 ctx
->gs_vtx_offset
[2] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
652 ctx
->gs_vtx_offset
[3] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
653 ctx
->gs_vtx_offset
[4] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
654 ctx
->gs_vtx_offset
[5] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
655 ctx
->gs_invocation_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
657 case MESA_SHADER_FRAGMENT
:
658 set_userdata_location_shader(ctx
, AC_UD_PS_SAMPLE_POS
, user_sgpr_idx
, 2);
660 ctx
->sample_positions
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
661 ctx
->prim_mask
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
662 ctx
->persp_sample
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
663 ctx
->persp_center
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
664 ctx
->persp_centroid
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
666 ctx
->linear_sample
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
667 ctx
->linear_center
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
668 ctx
->linear_centroid
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
669 arg_idx
++; /* line stipple */
670 ctx
->frag_pos
[0] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
671 ctx
->frag_pos
[1] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
672 ctx
->frag_pos
[2] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
673 ctx
->frag_pos
[3] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
674 ctx
->front_face
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
675 ctx
->ancillary
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
678 unreachable("Shader stage not implemented");
682 static void setup_types(struct nir_to_llvm_context
*ctx
)
684 LLVMValueRef args
[4];
686 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->context
);
687 ctx
->i1
= LLVMIntTypeInContext(ctx
->context
, 1);
688 ctx
->i8
= LLVMIntTypeInContext(ctx
->context
, 8);
689 ctx
->i16
= LLVMIntTypeInContext(ctx
->context
, 16);
690 ctx
->i32
= LLVMIntTypeInContext(ctx
->context
, 32);
691 ctx
->i64
= LLVMIntTypeInContext(ctx
->context
, 64);
692 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
693 ctx
->v3i32
= LLVMVectorType(ctx
->i32
, 3);
694 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
695 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
696 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
697 ctx
->f16
= LLVMHalfTypeInContext(ctx
->context
);
698 ctx
->v2f32
= LLVMVectorType(ctx
->f32
, 2);
699 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
700 ctx
->v16i8
= LLVMVectorType(ctx
->i8
, 16);
702 ctx
->i32zero
= LLVMConstInt(ctx
->i32
, 0, false);
703 ctx
->i32one
= LLVMConstInt(ctx
->i32
, 1, false);
704 ctx
->f32zero
= LLVMConstReal(ctx
->f32
, 0.0);
705 ctx
->f32one
= LLVMConstReal(ctx
->f32
, 1.0);
707 args
[0] = ctx
->f32zero
;
708 args
[1] = ctx
->f32zero
;
709 args
[2] = ctx
->f32zero
;
710 args
[3] = ctx
->f32one
;
711 ctx
->v4f32empty
= LLVMConstVector(args
, 4);
713 ctx
->range_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
715 ctx
->invariant_load_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
716 "invariant.load", 14);
717 ctx
->uniform_md_kind
=
718 LLVMGetMDKindIDInContext(ctx
->context
, "amdgpu.uniform", 14);
719 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
721 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
724 static int get_llvm_num_components(LLVMValueRef value
)
726 LLVMTypeRef type
= LLVMTypeOf(value
);
727 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
728 ? LLVMGetVectorSize(type
)
730 return num_components
;
733 static LLVMValueRef
llvm_extract_elem(struct nir_to_llvm_context
*ctx
,
737 int count
= get_llvm_num_components(value
);
739 assert(index
< count
);
743 return LLVMBuildExtractElement(ctx
->builder
, value
,
744 LLVMConstInt(ctx
->i32
, index
, false), "");
747 static LLVMValueRef
trim_vector(struct nir_to_llvm_context
*ctx
,
748 LLVMValueRef value
, unsigned count
)
750 unsigned num_components
= get_llvm_num_components(value
);
751 if (count
== num_components
)
754 LLVMValueRef masks
[] = {
755 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
756 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false)};
759 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
762 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
763 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
767 build_store_values_extended(struct nir_to_llvm_context
*ctx
,
768 LLVMValueRef
*values
,
769 unsigned value_count
,
770 unsigned value_stride
,
773 LLVMBuilderRef builder
= ctx
->builder
;
776 if (value_count
== 1) {
777 LLVMBuildStore(builder
, vec
, values
[0]);
781 for (i
= 0; i
< value_count
; i
++) {
782 LLVMValueRef ptr
= values
[i
* value_stride
];
783 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
784 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
785 LLVMBuildStore(builder
, value
, ptr
);
789 static LLVMTypeRef
get_def_type(struct nir_to_llvm_context
*ctx
,
792 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->context
, def
->bit_size
);
793 if (def
->num_components
> 1) {
794 type
= LLVMVectorType(type
, def
->num_components
);
799 static LLVMValueRef
get_src(struct nir_to_llvm_context
*ctx
, nir_src src
)
802 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->defs
, src
.ssa
);
803 return (LLVMValueRef
)entry
->data
;
807 static LLVMBasicBlockRef
get_block(struct nir_to_llvm_context
*ctx
,
810 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->defs
, b
);
811 return (LLVMBasicBlockRef
)entry
->data
;
814 static LLVMValueRef
get_alu_src(struct nir_to_llvm_context
*ctx
,
816 unsigned num_components
)
818 LLVMValueRef value
= get_src(ctx
, src
.src
);
819 bool need_swizzle
= false;
822 LLVMTypeRef type
= LLVMTypeOf(value
);
823 unsigned src_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
824 ? LLVMGetVectorSize(type
)
827 for (unsigned i
= 0; i
< num_components
; ++i
) {
828 assert(src
.swizzle
[i
] < src_components
);
829 if (src
.swizzle
[i
] != i
)
833 if (need_swizzle
|| num_components
!= src_components
) {
834 LLVMValueRef masks
[] = {
835 LLVMConstInt(ctx
->i32
, src
.swizzle
[0], false),
836 LLVMConstInt(ctx
->i32
, src
.swizzle
[1], false),
837 LLVMConstInt(ctx
->i32
, src
.swizzle
[2], false),
838 LLVMConstInt(ctx
->i32
, src
.swizzle
[3], false)};
840 if (src_components
> 1 && num_components
== 1) {
841 value
= LLVMBuildExtractElement(ctx
->builder
, value
,
843 } else if (src_components
== 1 && num_components
> 1) {
844 LLVMValueRef values
[] = {value
, value
, value
, value
};
845 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
847 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
848 value
= LLVMBuildShuffleVector(ctx
->builder
, value
, value
,
857 static LLVMValueRef
emit_int_cmp(struct nir_to_llvm_context
*ctx
,
858 LLVMIntPredicate pred
, LLVMValueRef src0
,
861 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
862 return LLVMBuildSelect(ctx
->builder
, result
,
863 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
864 LLVMConstInt(ctx
->i32
, 0, false), "");
867 static LLVMValueRef
emit_float_cmp(struct nir_to_llvm_context
*ctx
,
868 LLVMRealPredicate pred
, LLVMValueRef src0
,
872 src0
= to_float(ctx
, src0
);
873 src1
= to_float(ctx
, src1
);
874 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
875 return LLVMBuildSelect(ctx
->builder
, result
,
876 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
877 LLVMConstInt(ctx
->i32
, 0, false), "");
880 static LLVMValueRef
emit_intrin_1f_param(struct nir_to_llvm_context
*ctx
,
884 LLVMValueRef params
[] = {
887 return ac_emit_llvm_intrinsic(&ctx
->ac
, intrin
, ctx
->f32
, params
, 1, AC_FUNC_ATTR_READNONE
);
890 static LLVMValueRef
emit_intrin_2f_param(struct nir_to_llvm_context
*ctx
,
892 LLVMValueRef src0
, LLVMValueRef src1
)
894 LLVMValueRef params
[] = {
898 return ac_emit_llvm_intrinsic(&ctx
->ac
, intrin
, ctx
->f32
, params
, 2, AC_FUNC_ATTR_READNONE
);
901 static LLVMValueRef
emit_intrin_3f_param(struct nir_to_llvm_context
*ctx
,
903 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
905 LLVMValueRef params
[] = {
910 return ac_emit_llvm_intrinsic(&ctx
->ac
, intrin
, ctx
->f32
, params
, 3, AC_FUNC_ATTR_READNONE
);
913 static LLVMValueRef
emit_bcsel(struct nir_to_llvm_context
*ctx
,
914 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
916 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
918 return LLVMBuildSelect(ctx
->builder
, v
, src1
, src2
, "");
921 static LLVMValueRef
emit_find_lsb(struct nir_to_llvm_context
*ctx
,
924 LLVMValueRef params
[2] = {
927 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
928 * add special code to check for x=0. The reason is that
929 * the LLVM behavior for x=0 is different from what we
932 * The hardware already implements the correct behavior.
934 LLVMConstInt(ctx
->i32
, 1, false),
936 return ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.cttz.i32", ctx
->i32
, params
, 2, AC_FUNC_ATTR_READNONE
);
939 static LLVMValueRef
emit_ifind_msb(struct nir_to_llvm_context
*ctx
,
942 LLVMValueRef msb
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.AMDGPU.flbit.i32",
944 AC_FUNC_ATTR_READNONE
);
946 /* The HW returns the last bit index from MSB, but NIR wants
947 * the index from LSB. Invert it by doing "31 - msb". */
948 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
951 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
952 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
953 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
954 src0
, ctx
->i32zero
, ""),
955 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
956 src0
, all_ones
, ""), "");
958 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
961 static LLVMValueRef
emit_ufind_msb(struct nir_to_llvm_context
*ctx
,
964 LLVMValueRef args
[2] = {
968 LLVMValueRef msb
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.ctlz.i32",
969 ctx
->i32
, args
, ARRAY_SIZE(args
),
970 AC_FUNC_ATTR_READNONE
);
972 /* The HW returns the last bit index from MSB, but NIR wants
973 * the index from LSB. Invert it by doing "31 - msb". */
974 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
977 return LLVMBuildSelect(ctx
->builder
,
978 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src0
,
980 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
983 static LLVMValueRef
emit_minmax_int(struct nir_to_llvm_context
*ctx
,
984 LLVMIntPredicate pred
,
985 LLVMValueRef src0
, LLVMValueRef src1
)
987 return LLVMBuildSelect(ctx
->builder
,
988 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
993 static LLVMValueRef
emit_iabs(struct nir_to_llvm_context
*ctx
,
996 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
997 LLVMBuildNeg(ctx
->builder
, src0
, ""));
1000 static LLVMValueRef
emit_fsign(struct nir_to_llvm_context
*ctx
,
1003 LLVMValueRef cmp
, val
;
1005 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, ctx
->f32zero
, "");
1006 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->f32one
, src0
, "");
1007 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, ctx
->f32zero
, "");
1008 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(ctx
->f32
, -1.0), "");
1012 static LLVMValueRef
emit_isign(struct nir_to_llvm_context
*ctx
,
1015 LLVMValueRef cmp
, val
;
1017 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, ctx
->i32zero
, "");
1018 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->i32one
, src0
, "");
1019 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, ctx
->i32zero
, "");
1020 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(ctx
->i32
, -1, true), "");
1024 static LLVMValueRef
emit_ffract(struct nir_to_llvm_context
*ctx
,
1027 const char *intr
= "llvm.floor.f32";
1028 LLVMValueRef fsrc0
= to_float(ctx
, src0
);
1029 LLVMValueRef params
[] = {
1032 LLVMValueRef floor
= ac_emit_llvm_intrinsic(&ctx
->ac
, intr
,
1033 ctx
->f32
, params
, 1,
1034 AC_FUNC_ATTR_READNONE
);
1035 return LLVMBuildFSub(ctx
->builder
, fsrc0
, floor
, "");
1038 static LLVMValueRef
emit_uint_carry(struct nir_to_llvm_context
*ctx
,
1040 LLVMValueRef src0
, LLVMValueRef src1
)
1042 LLVMTypeRef ret_type
;
1043 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
1045 LLVMValueRef params
[] = { src0
, src1
};
1046 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
1049 res
= ac_emit_llvm_intrinsic(&ctx
->ac
, intrin
, ret_type
,
1050 params
, 2, AC_FUNC_ATTR_READNONE
);
1052 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
1053 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
1057 static LLVMValueRef
emit_b2f(struct nir_to_llvm_context
*ctx
,
1060 return LLVMBuildAnd(ctx
->builder
, src0
, LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""), "");
1063 static LLVMValueRef
emit_umul_high(struct nir_to_llvm_context
*ctx
,
1064 LLVMValueRef src0
, LLVMValueRef src1
)
1066 LLVMValueRef dst64
, result
;
1067 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
1068 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
1070 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1071 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1072 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1076 static LLVMValueRef
emit_imul_high(struct nir_to_llvm_context
*ctx
,
1077 LLVMValueRef src0
, LLVMValueRef src1
)
1079 LLVMValueRef dst64
, result
;
1080 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
1081 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
1083 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1084 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1085 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1089 static LLVMValueRef
emit_bitfield_extract(struct nir_to_llvm_context
*ctx
,
1091 LLVMValueRef srcs
[3])
1093 LLVMValueRef result
;
1094 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
1095 result
= ac_emit_llvm_intrinsic(&ctx
->ac
, intrin
, ctx
->i32
, srcs
, 3, AC_FUNC_ATTR_READNONE
);
1097 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
1101 static LLVMValueRef
emit_bitfield_insert(struct nir_to_llvm_context
*ctx
,
1102 LLVMValueRef src0
, LLVMValueRef src1
,
1103 LLVMValueRef src2
, LLVMValueRef src3
)
1105 LLVMValueRef bfi_args
[3], result
;
1107 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
1108 LLVMBuildSub(ctx
->builder
,
1109 LLVMBuildShl(ctx
->builder
,
1114 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
1117 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
1120 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
1121 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
1123 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
1124 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
1125 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
1127 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
1131 static LLVMValueRef
emit_pack_half_2x16(struct nir_to_llvm_context
*ctx
,
1134 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1136 LLVMValueRef comp
[2];
1138 src0
= to_float(ctx
, src0
);
1139 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32zero
, "");
1140 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32one
, "");
1141 for (i
= 0; i
< 2; i
++) {
1142 comp
[i
] = LLVMBuildFPTrunc(ctx
->builder
, comp
[i
], ctx
->f16
, "");
1143 comp
[i
] = LLVMBuildBitCast(ctx
->builder
, comp
[i
], ctx
->i16
, "");
1144 comp
[i
] = LLVMBuildZExt(ctx
->builder
, comp
[i
], ctx
->i32
, "");
1147 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
1148 comp
[0] = LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
1153 static LLVMValueRef
emit_unpack_half_2x16(struct nir_to_llvm_context
*ctx
,
1156 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1157 LLVMValueRef temps
[2], result
, val
;
1160 for (i
= 0; i
< 2; i
++) {
1161 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
1162 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
1163 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
1164 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
1167 result
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), temps
[0],
1169 result
= LLVMBuildInsertElement(ctx
->builder
, result
, temps
[1],
1175 * Set range metadata on an instruction. This can only be used on load and
1176 * call instructions. If you know an instruction can only produce the values
1177 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
1178 * \p lo is the minimum value inclusive.
1179 * \p hi is the maximum value exclusive.
1181 static void set_range_metadata(struct nir_to_llvm_context
*ctx
,
1182 LLVMValueRef value
, unsigned lo
, unsigned hi
)
1184 LLVMValueRef range_md
, md_args
[2];
1185 LLVMTypeRef type
= LLVMTypeOf(value
);
1186 LLVMContextRef context
= LLVMGetTypeContext(type
);
1188 md_args
[0] = LLVMConstInt(type
, lo
, false);
1189 md_args
[1] = LLVMConstInt(type
, hi
, false);
1190 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
1191 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
1194 static LLVMValueRef
get_thread_id(struct nir_to_llvm_context
*ctx
)
1197 LLVMValueRef tid_args
[2];
1198 tid_args
[0] = LLVMConstInt(ctx
->i32
, 0xffffffff, false);
1199 tid_args
[1] = ctx
->i32zero
;
1200 tid_args
[1] = ac_emit_llvm_intrinsic(&ctx
->ac
,
1201 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
1202 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
1204 tid
= ac_emit_llvm_intrinsic(&ctx
->ac
,
1205 "llvm.amdgcn.mbcnt.hi", ctx
->i32
,
1206 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
1207 set_range_metadata(ctx
, tid
, 0, 64);
1212 * SI implements derivatives using the local data store (LDS)
1213 * All writes to the LDS happen in all executing threads at
1214 * the same time. TID is the Thread ID for the current
1215 * thread and is a value between 0 and 63, representing
1216 * the thread's position in the wavefront.
1218 * For the pixel shader threads are grouped into quads of four pixels.
1219 * The TIDs of the pixels of a quad are:
1227 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
1228 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
1229 * the current pixel's column, and masking with 0xfffffffe yields the TID
1230 * of the left pixel of the current pixel's row.
1232 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
1233 * adding 2 yields the TID of the pixel below the top pixel.
1235 /* masks for thread ID. */
1236 #define TID_MASK_TOP_LEFT 0xfffffffc
1237 #define TID_MASK_TOP 0xfffffffd
1238 #define TID_MASK_LEFT 0xfffffffe
1239 static LLVMValueRef
emit_ddxy(struct nir_to_llvm_context
*ctx
,
1243 LLVMValueRef tl
, trbl
, result
;
1244 LLVMValueRef tl_tid
, trbl_tid
;
1245 LLVMValueRef args
[2];
1246 LLVMValueRef thread_id
;
1249 ctx
->has_ddxy
= true;
1251 if (!ctx
->lds
&& !ctx
->has_ds_bpermute
)
1252 ctx
->lds
= LLVMAddGlobalInAddressSpace(ctx
->module
,
1253 LLVMArrayType(ctx
->i32
, 64),
1254 "ddxy_lds", LOCAL_ADDR_SPACE
);
1256 thread_id
= get_thread_id(ctx
);
1257 if (op
== nir_op_fddx_fine
|| op
== nir_op_fddx
)
1258 mask
= TID_MASK_LEFT
;
1259 else if (op
== nir_op_fddy_fine
|| op
== nir_op_fddy
)
1260 mask
= TID_MASK_TOP
;
1262 mask
= TID_MASK_TOP_LEFT
;
1264 tl_tid
= LLVMBuildAnd(ctx
->builder
, thread_id
,
1265 LLVMConstInt(ctx
->i32
, mask
, false), "");
1266 /* for DDX we want to next X pixel, DDY next Y pixel. */
1267 if (op
== nir_op_fddx_fine
||
1268 op
== nir_op_fddx_coarse
||
1274 trbl_tid
= LLVMBuildAdd(ctx
->builder
, tl_tid
,
1275 LLVMConstInt(ctx
->i32
, idx
, false), "");
1277 if (ctx
->has_ds_bpermute
) {
1278 args
[0] = LLVMBuildMul(ctx
->builder
, tl_tid
,
1279 LLVMConstInt(ctx
->i32
, 4, false), "");
1281 tl
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.amdgcn.ds.bpermute",
1283 AC_FUNC_ATTR_READNONE
);
1285 args
[0] = LLVMBuildMul(ctx
->builder
, trbl_tid
,
1286 LLVMConstInt(ctx
->i32
, 4, false), "");
1287 trbl
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.amdgcn.ds.bpermute",
1289 AC_FUNC_ATTR_READNONE
);
1291 LLVMValueRef store_ptr
, load_ptr0
, load_ptr1
;
1293 store_ptr
= build_gep0(ctx
, ctx
->lds
, thread_id
);
1294 load_ptr0
= build_gep0(ctx
, ctx
->lds
, tl_tid
);
1295 load_ptr1
= build_gep0(ctx
, ctx
->lds
, trbl_tid
);
1297 LLVMBuildStore(ctx
->builder
, src0
, store_ptr
);
1298 tl
= LLVMBuildLoad(ctx
->builder
, load_ptr0
, "");
1299 trbl
= LLVMBuildLoad(ctx
->builder
, load_ptr1
, "");
1301 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, ctx
->f32
, "");
1302 trbl
= LLVMBuildBitCast(ctx
->builder
, trbl
, ctx
->f32
, "");
1303 result
= LLVMBuildFSub(ctx
->builder
, trbl
, tl
, "");
1308 * this takes an I,J coordinate pair,
1309 * and works out the X and Y derivatives.
1310 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
1312 static LLVMValueRef
emit_ddxy_interp(
1313 struct nir_to_llvm_context
*ctx
,
1314 LLVMValueRef interp_ij
)
1316 LLVMValueRef result
[4], a
;
1319 for (i
= 0; i
< 2; i
++) {
1320 a
= LLVMBuildExtractElement(ctx
->builder
, interp_ij
,
1321 LLVMConstInt(ctx
->i32
, i
, false), "");
1322 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
1323 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
1325 return ac_build_gather_values(&ctx
->ac
, result
, 4);
1328 static void visit_alu(struct nir_to_llvm_context
*ctx
, nir_alu_instr
*instr
)
1330 LLVMValueRef src
[4], result
= NULL
;
1331 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
1332 unsigned src_components
;
1334 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
1335 switch (instr
->op
) {
1341 case nir_op_pack_half_2x16
:
1344 case nir_op_unpack_half_2x16
:
1348 src_components
= num_components
;
1351 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1352 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
1354 switch (instr
->op
) {
1360 src
[0] = to_float(ctx
, src
[0]);
1361 result
= LLVMBuildFNeg(ctx
->builder
, src
[0], "");
1364 result
= LLVMBuildNeg(ctx
->builder
, src
[0], "");
1367 result
= LLVMBuildNot(ctx
->builder
, src
[0], "");
1370 result
= LLVMBuildAdd(ctx
->builder
, src
[0], src
[1], "");
1373 src
[0] = to_float(ctx
, src
[0]);
1374 src
[1] = to_float(ctx
, src
[1]);
1375 result
= LLVMBuildFAdd(ctx
->builder
, src
[0], src
[1], "");
1378 src
[0] = to_float(ctx
, src
[0]);
1379 src
[1] = to_float(ctx
, src
[1]);
1380 result
= LLVMBuildFSub(ctx
->builder
, src
[0], src
[1], "");
1383 result
= LLVMBuildSub(ctx
->builder
, src
[0], src
[1], "");
1386 result
= LLVMBuildMul(ctx
->builder
, src
[0], src
[1], "");
1389 result
= LLVMBuildSRem(ctx
->builder
, src
[0], src
[1], "");
1392 result
= LLVMBuildURem(ctx
->builder
, src
[0], src
[1], "");
1395 src
[0] = to_float(ctx
, src
[0]);
1396 src
[1] = to_float(ctx
, src
[1]);
1397 result
= ac_emit_fdiv(&ctx
->ac
, src
[0], src
[1]);
1398 result
= emit_intrin_1f_param(ctx
, "llvm.floor.f32", result
);
1399 result
= LLVMBuildFMul(ctx
->builder
, src
[1] , result
, "");
1400 result
= LLVMBuildFSub(ctx
->builder
, src
[0], result
, "");
1403 src
[0] = to_float(ctx
, src
[0]);
1404 src
[1] = to_float(ctx
, src
[1]);
1405 result
= LLVMBuildFRem(ctx
->builder
, src
[0], src
[1], "");
1408 result
= LLVMBuildSRem(ctx
->builder
, src
[0], src
[1], "");
1411 result
= LLVMBuildSDiv(ctx
->builder
, src
[0], src
[1], "");
1414 result
= LLVMBuildUDiv(ctx
->builder
, src
[0], src
[1], "");
1417 src
[0] = to_float(ctx
, src
[0]);
1418 src
[1] = to_float(ctx
, src
[1]);
1419 result
= LLVMBuildFMul(ctx
->builder
, src
[0], src
[1], "");
1422 src
[0] = to_float(ctx
, src
[0]);
1423 src
[1] = to_float(ctx
, src
[1]);
1424 result
= ac_emit_fdiv(&ctx
->ac
, src
[0], src
[1]);
1427 src
[0] = to_float(ctx
, src
[0]);
1428 result
= ac_emit_fdiv(&ctx
->ac
, ctx
->f32one
, src
[0]);
1431 result
= LLVMBuildAnd(ctx
->builder
, src
[0], src
[1], "");
1434 result
= LLVMBuildOr(ctx
->builder
, src
[0], src
[1], "");
1437 result
= LLVMBuildXor(ctx
->builder
, src
[0], src
[1], "");
1440 result
= LLVMBuildShl(ctx
->builder
, src
[0], src
[1], "");
1443 result
= LLVMBuildAShr(ctx
->builder
, src
[0], src
[1], "");
1446 result
= LLVMBuildLShr(ctx
->builder
, src
[0], src
[1], "");
1449 result
= emit_int_cmp(ctx
, LLVMIntSLT
, src
[0], src
[1]);
1452 result
= emit_int_cmp(ctx
, LLVMIntNE
, src
[0], src
[1]);
1455 result
= emit_int_cmp(ctx
, LLVMIntEQ
, src
[0], src
[1]);
1458 result
= emit_int_cmp(ctx
, LLVMIntSGE
, src
[0], src
[1]);
1461 result
= emit_int_cmp(ctx
, LLVMIntULT
, src
[0], src
[1]);
1464 result
= emit_int_cmp(ctx
, LLVMIntUGE
, src
[0], src
[1]);
1467 result
= emit_float_cmp(ctx
, LLVMRealUEQ
, src
[0], src
[1]);
1470 result
= emit_float_cmp(ctx
, LLVMRealUNE
, src
[0], src
[1]);
1473 result
= emit_float_cmp(ctx
, LLVMRealULT
, src
[0], src
[1]);
1476 result
= emit_float_cmp(ctx
, LLVMRealUGE
, src
[0], src
[1]);
1479 result
= emit_intrin_1f_param(ctx
, "llvm.fabs.f32", src
[0]);
1482 result
= emit_iabs(ctx
, src
[0]);
1485 result
= emit_minmax_int(ctx
, LLVMIntSGT
, src
[0], src
[1]);
1488 result
= emit_minmax_int(ctx
, LLVMIntSLT
, src
[0], src
[1]);
1491 result
= emit_minmax_int(ctx
, LLVMIntUGT
, src
[0], src
[1]);
1494 result
= emit_minmax_int(ctx
, LLVMIntULT
, src
[0], src
[1]);
1497 result
= emit_isign(ctx
, src
[0]);
1500 src
[0] = to_float(ctx
, src
[0]);
1501 result
= emit_fsign(ctx
, src
[0]);
1504 result
= emit_intrin_1f_param(ctx
, "llvm.floor.f32", src
[0]);
1507 result
= emit_intrin_1f_param(ctx
, "llvm.trunc.f32", src
[0]);
1510 result
= emit_intrin_1f_param(ctx
, "llvm.ceil.f32", src
[0]);
1512 case nir_op_fround_even
:
1513 result
= emit_intrin_1f_param(ctx
, "llvm.rint.f32", src
[0]);
1516 result
= emit_ffract(ctx
, src
[0]);
1519 result
= emit_intrin_1f_param(ctx
, "llvm.sin.f32", src
[0]);
1522 result
= emit_intrin_1f_param(ctx
, "llvm.cos.f32", src
[0]);
1525 result
= emit_intrin_1f_param(ctx
, "llvm.sqrt.f32", src
[0]);
1528 result
= emit_intrin_1f_param(ctx
, "llvm.exp2.f32", src
[0]);
1531 result
= emit_intrin_1f_param(ctx
, "llvm.log2.f32", src
[0]);
1534 result
= emit_intrin_1f_param(ctx
, "llvm.sqrt.f32", src
[0]);
1535 result
= ac_emit_fdiv(&ctx
->ac
, ctx
->f32one
, result
);
1538 result
= emit_intrin_2f_param(ctx
, "llvm.pow.f32", src
[0], src
[1]);
1541 result
= emit_intrin_2f_param(ctx
, "llvm.maxnum.f32", src
[0], src
[1]);
1544 result
= emit_intrin_2f_param(ctx
, "llvm.minnum.f32", src
[0], src
[1]);
1547 result
= emit_intrin_3f_param(ctx
, "llvm.fma.f32", src
[0], src
[1], src
[2]);
1549 case nir_op_ibitfield_extract
:
1550 result
= emit_bitfield_extract(ctx
, "llvm.AMDGPU.bfe.i32", src
);
1552 case nir_op_ubitfield_extract
:
1553 result
= emit_bitfield_extract(ctx
, "llvm.AMDGPU.bfe.u32", src
);
1555 case nir_op_bitfield_insert
:
1556 result
= emit_bitfield_insert(ctx
, src
[0], src
[1], src
[2], src
[3]);
1558 case nir_op_bitfield_reverse
:
1559 result
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.bitreverse.i32", ctx
->i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1561 case nir_op_bit_count
:
1562 result
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.ctpop.i32", ctx
->i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1567 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1568 src
[i
] = to_integer(ctx
, src
[i
]);
1569 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
1572 src
[0] = to_float(ctx
, src
[0]);
1573 result
= LLVMBuildFPToSI(ctx
->builder
, src
[0], ctx
->i32
, "");
1576 src
[0] = to_float(ctx
, src
[0]);
1577 result
= LLVMBuildFPToUI(ctx
->builder
, src
[0], ctx
->i32
, "");
1580 result
= LLVMBuildSIToFP(ctx
->builder
, src
[0], ctx
->f32
, "");
1583 result
= LLVMBuildUIToFP(ctx
->builder
, src
[0], ctx
->f32
, "");
1586 result
= emit_bcsel(ctx
, src
[0], src
[1], src
[2]);
1588 case nir_op_find_lsb
:
1589 result
= emit_find_lsb(ctx
, src
[0]);
1591 case nir_op_ufind_msb
:
1592 result
= emit_ufind_msb(ctx
, src
[0]);
1594 case nir_op_ifind_msb
:
1595 result
= emit_ifind_msb(ctx
, src
[0]);
1597 case nir_op_uadd_carry
:
1598 result
= emit_uint_carry(ctx
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
1600 case nir_op_usub_borrow
:
1601 result
= emit_uint_carry(ctx
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
1604 result
= emit_b2f(ctx
, src
[0]);
1606 case nir_op_fquantize2f16
:
1607 src
[0] = to_float(ctx
, src
[0]);
1608 result
= LLVMBuildFPTrunc(ctx
->builder
, src
[0], ctx
->f16
, "");
1609 /* need to convert back up to f32 */
1610 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
1612 case nir_op_umul_high
:
1613 result
= emit_umul_high(ctx
, src
[0], src
[1]);
1615 case nir_op_imul_high
:
1616 result
= emit_imul_high(ctx
, src
[0], src
[1]);
1618 case nir_op_pack_half_2x16
:
1619 result
= emit_pack_half_2x16(ctx
, src
[0]);
1621 case nir_op_unpack_half_2x16
:
1622 result
= emit_unpack_half_2x16(ctx
, src
[0]);
1626 case nir_op_fddx_fine
:
1627 case nir_op_fddy_fine
:
1628 case nir_op_fddx_coarse
:
1629 case nir_op_fddy_coarse
:
1630 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
1633 fprintf(stderr
, "Unknown NIR alu instr: ");
1634 nir_print_instr(&instr
->instr
, stderr
);
1635 fprintf(stderr
, "\n");
1640 assert(instr
->dest
.dest
.is_ssa
);
1641 result
= to_integer(ctx
, result
);
1642 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.dest
.ssa
,
1647 static void visit_load_const(struct nir_to_llvm_context
*ctx
,
1648 nir_load_const_instr
*instr
)
1650 LLVMValueRef values
[4], value
= NULL
;
1651 LLVMTypeRef element_type
=
1652 LLVMIntTypeInContext(ctx
->context
, instr
->def
.bit_size
);
1654 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1655 switch (instr
->def
.bit_size
) {
1657 values
[i
] = LLVMConstInt(element_type
,
1658 instr
->value
.u32
[i
], false);
1661 values
[i
] = LLVMConstInt(element_type
,
1662 instr
->value
.u64
[i
], false);
1666 "unsupported nir load_const bit_size: %d\n",
1667 instr
->def
.bit_size
);
1671 if (instr
->def
.num_components
> 1) {
1672 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1676 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, value
);
1679 static LLVMValueRef
cast_ptr(struct nir_to_llvm_context
*ctx
, LLVMValueRef ptr
,
1682 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
1683 return LLVMBuildBitCast(ctx
->builder
, ptr
,
1684 LLVMPointerType(type
, addr_space
), "");
1688 get_buffer_size(struct nir_to_llvm_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1691 LLVMBuildExtractElement(ctx
->builder
, descriptor
,
1692 LLVMConstInt(ctx
->i32
, 2, false), "");
1695 if (ctx
->options
->chip_class
>= VI
&& in_elements
) {
1696 /* On VI, the descriptor contains the size in bytes,
1697 * but TXQ must return the size in elements.
1698 * The stride is always non-zero for resources using TXQ.
1700 LLVMValueRef stride
=
1701 LLVMBuildExtractElement(ctx
->builder
, descriptor
,
1702 LLVMConstInt(ctx
->i32
, 1, false), "");
1703 stride
= LLVMBuildLShr(ctx
->builder
, stride
,
1704 LLVMConstInt(ctx
->i32
, 16, false), "");
1705 stride
= LLVMBuildAnd(ctx
->builder
, stride
,
1706 LLVMConstInt(ctx
->i32
, 0x3fff, false), "");
1708 size
= LLVMBuildUDiv(ctx
->builder
, size
, stride
, "");
1714 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
1717 static void build_int_type_name(
1719 char *buf
, unsigned bufsize
)
1721 assert(bufsize
>= 6);
1723 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
1724 snprintf(buf
, bufsize
, "v%ui32",
1725 LLVMGetVectorSize(type
));
1730 static LLVMValueRef
radv_lower_gather4_integer(struct nir_to_llvm_context
*ctx
,
1731 struct ac_tex_info
*tinfo
,
1732 nir_tex_instr
*instr
,
1733 const char *intr_name
,
1734 unsigned coord_vgpr_index
)
1736 LLVMValueRef coord
= tinfo
->args
[0];
1737 LLVMValueRef half_texel
[2];
1742 LLVMValueRef txq_args
[10];
1743 int txq_arg_count
= 0;
1745 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
1746 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, false);
1747 txq_args
[txq_arg_count
++] = tinfo
->args
[1];
1748 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0xf, 0); /* dmask */
1749 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* unorm */
1750 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* r128 */
1751 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, da
? 1 : 0, 0);
1752 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* glc */
1753 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* slc */
1754 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* tfe */
1755 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* lwe */
1756 size
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.SI.getresinfo.i32", ctx
->v4i32
,
1757 txq_args
, txq_arg_count
,
1758 AC_FUNC_ATTR_READNONE
);
1760 for (c
= 0; c
< 2; c
++) {
1761 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
1762 LLVMConstInt(ctx
->i32
, c
, false), "");
1763 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1764 half_texel
[c
] = ac_emit_fdiv(&ctx
->ac
, ctx
->f32one
, half_texel
[c
]);
1765 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1766 LLVMConstReal(ctx
->f32
, -0.5), "");
1770 for (c
= 0; c
< 2; c
++) {
1772 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, coord_vgpr_index
+ c
, 0);
1773 tmp
= LLVMBuildExtractElement(ctx
->builder
, coord
, index
, "");
1774 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1775 tmp
= LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
1776 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1777 coord
= LLVMBuildInsertElement(ctx
->builder
, coord
, tmp
, index
, "");
1780 tinfo
->args
[0] = coord
;
1781 return ac_emit_llvm_intrinsic(&ctx
->ac
, intr_name
, tinfo
->dst_type
, tinfo
->args
, tinfo
->arg_count
,
1782 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_NOUNWIND
);
1786 static LLVMValueRef
build_tex_intrinsic(struct nir_to_llvm_context
*ctx
,
1787 nir_tex_instr
*instr
,
1788 struct ac_tex_info
*tinfo
)
1790 const char *name
= "llvm.SI.image.sample";
1791 const char *infix
= "";
1792 char intr_name
[127];
1794 bool is_shadow
= instr
->is_shadow
;
1795 bool has_offset
= tinfo
->has_offset
;
1796 switch (instr
->op
) {
1798 case nir_texop_txf_ms
:
1799 case nir_texop_samples_identical
:
1800 name
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
? "llvm.SI.image.load" :
1801 instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
? "llvm.SI.vs.load.input" :
1802 "llvm.SI.image.load.mip";
1813 name
= "llvm.SI.getresinfo";
1815 case nir_texop_query_levels
:
1816 name
= "llvm.SI.getresinfo";
1819 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
1826 name
= "llvm.SI.gather4";
1830 name
= "llvm.SI.getlod";
1838 build_int_type_name(LLVMTypeOf(tinfo
->args
[0]), type
, sizeof(type
));
1839 sprintf(intr_name
, "%s%s%s%s.%s", name
, is_shadow
? ".c" : "", infix
,
1840 has_offset
? ".o" : "", type
);
1842 if (instr
->op
== nir_texop_tg4
) {
1843 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
1844 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
1845 return radv_lower_gather4_integer(ctx
, tinfo
, instr
, intr_name
,
1846 (int)has_offset
+ (int)is_shadow
);
1849 return ac_emit_llvm_intrinsic(&ctx
->ac
, intr_name
, tinfo
->dst_type
, tinfo
->args
, tinfo
->arg_count
,
1850 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_NOUNWIND
);
1854 static LLVMValueRef
visit_vulkan_resource_index(struct nir_to_llvm_context
*ctx
,
1855 nir_intrinsic_instr
*instr
)
1857 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
1858 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
1859 unsigned binding
= nir_intrinsic_binding(instr
);
1860 LLVMValueRef desc_ptr
= ctx
->descriptor_sets
[desc_set
];
1861 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[desc_set
].layout
;
1862 unsigned base_offset
= layout
->binding
[binding
].offset
;
1863 LLVMValueRef offset
, stride
;
1865 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
1866 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
1867 desc_ptr
= ctx
->push_constants
;
1868 base_offset
= ctx
->options
->layout
->push_constant_size
;
1869 base_offset
+= 16 * layout
->binding
[binding
].dynamic_offset_offset
;
1870 stride
= LLVMConstInt(ctx
->i32
, 16, false);
1872 stride
= LLVMConstInt(ctx
->i32
, layout
->binding
[binding
].size
, false);
1874 offset
= LLVMConstInt(ctx
->i32
, base_offset
, false);
1875 index
= LLVMBuildMul(ctx
->builder
, index
, stride
, "");
1876 offset
= LLVMBuildAdd(ctx
->builder
, offset
, index
, "");
1878 desc_ptr
= build_gep0(ctx
, desc_ptr
, offset
);
1879 desc_ptr
= cast_ptr(ctx
, desc_ptr
, ctx
->v4i32
);
1880 LLVMSetMetadata(desc_ptr
, ctx
->uniform_md_kind
, ctx
->empty_md
);
1882 return LLVMBuildLoad(ctx
->builder
, desc_ptr
, "");
1885 static LLVMValueRef
visit_load_push_constant(struct nir_to_llvm_context
*ctx
,
1886 nir_intrinsic_instr
*instr
)
1888 LLVMValueRef ptr
, addr
;
1890 addr
= LLVMConstInt(ctx
->i32
, nir_intrinsic_base(instr
), 0);
1891 addr
= LLVMBuildAdd(ctx
->builder
, addr
, get_src(ctx
, instr
->src
[0]), "");
1893 ptr
= build_gep0(ctx
, ctx
->push_constants
, addr
);
1894 ptr
= cast_ptr(ctx
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
1896 return LLVMBuildLoad(ctx
->builder
, ptr
, "");
1899 static LLVMValueRef
visit_get_buffer_size(struct nir_to_llvm_context
*ctx
,
1900 nir_intrinsic_instr
*instr
)
1902 LLVMValueRef desc
= get_src(ctx
, instr
->src
[0]);
1904 return get_buffer_size(ctx
, desc
, false);
1906 static void visit_store_ssbo(struct nir_to_llvm_context
*ctx
,
1907 nir_intrinsic_instr
*instr
)
1909 const char *store_name
;
1910 LLVMTypeRef data_type
= ctx
->f32
;
1911 unsigned writemask
= nir_intrinsic_write_mask(instr
);
1912 LLVMValueRef base_data
, base_offset
;
1913 LLVMValueRef params
[6];
1915 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
1916 ctx
->shader_info
->fs
.writes_memory
= true;
1918 params
[1] = get_src(ctx
, instr
->src
[1]);
1919 params
[2] = LLVMConstInt(ctx
->i32
, 0, false); /* vindex */
1920 params
[4] = LLVMConstInt(ctx
->i1
, 0, false); /* glc */
1921 params
[5] = LLVMConstInt(ctx
->i1
, 0, false); /* slc */
1923 if (instr
->num_components
> 1)
1924 data_type
= LLVMVectorType(ctx
->f32
, instr
->num_components
);
1926 base_data
= to_float(ctx
, get_src(ctx
, instr
->src
[0]));
1927 base_data
= trim_vector(ctx
, base_data
, instr
->num_components
);
1928 base_data
= LLVMBuildBitCast(ctx
->builder
, base_data
,
1930 base_offset
= get_src(ctx
, instr
->src
[2]); /* voffset */
1934 LLVMValueRef offset
;
1936 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
1938 /* Due to an LLVM limitation, split 3-element writes
1939 * into a 2-element and a 1-element write. */
1941 writemask
|= 1 << (start
+ 2);
1946 store_name
= "llvm.amdgcn.buffer.store.v4f32";
1948 } else if (count
== 2) {
1949 tmp
= LLVMBuildExtractElement(ctx
->builder
,
1950 base_data
, LLVMConstInt(ctx
->i32
, start
, false), "");
1951 data
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), tmp
,
1954 tmp
= LLVMBuildExtractElement(ctx
->builder
,
1955 base_data
, LLVMConstInt(ctx
->i32
, start
+ 1, false), "");
1956 data
= LLVMBuildInsertElement(ctx
->builder
, data
, tmp
,
1958 store_name
= "llvm.amdgcn.buffer.store.v2f32";
1962 if (get_llvm_num_components(base_data
) > 1)
1963 data
= LLVMBuildExtractElement(ctx
->builder
, base_data
,
1964 LLVMConstInt(ctx
->i32
, start
, false), "");
1967 store_name
= "llvm.amdgcn.buffer.store.f32";
1970 offset
= base_offset
;
1972 offset
= LLVMBuildAdd(ctx
->builder
, offset
, LLVMConstInt(ctx
->i32
, start
* 4, false), "");
1976 ac_emit_llvm_intrinsic(&ctx
->ac
, store_name
,
1977 ctx
->voidt
, params
, 6, 0);
1981 static LLVMValueRef
visit_atomic_ssbo(struct nir_to_llvm_context
*ctx
,
1982 nir_intrinsic_instr
*instr
)
1985 LLVMValueRef params
[6];
1987 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
1988 ctx
->shader_info
->fs
.writes_memory
= true;
1990 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
1991 params
[arg_count
++] = llvm_extract_elem(ctx
, get_src(ctx
, instr
->src
[3]), 0);
1993 params
[arg_count
++] = llvm_extract_elem(ctx
, get_src(ctx
, instr
->src
[2]), 0);
1994 params
[arg_count
++] = get_src(ctx
, instr
->src
[0]);
1995 params
[arg_count
++] = LLVMConstInt(ctx
->i32
, 0, false); /* vindex */
1996 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1997 params
[arg_count
++] = LLVMConstInt(ctx
->i1
, 0, false); /* slc */
1999 switch (instr
->intrinsic
) {
2000 case nir_intrinsic_ssbo_atomic_add
:
2001 name
= "llvm.amdgcn.buffer.atomic.add";
2003 case nir_intrinsic_ssbo_atomic_imin
:
2004 name
= "llvm.amdgcn.buffer.atomic.smin";
2006 case nir_intrinsic_ssbo_atomic_umin
:
2007 name
= "llvm.amdgcn.buffer.atomic.umin";
2009 case nir_intrinsic_ssbo_atomic_imax
:
2010 name
= "llvm.amdgcn.buffer.atomic.smax";
2012 case nir_intrinsic_ssbo_atomic_umax
:
2013 name
= "llvm.amdgcn.buffer.atomic.umax";
2015 case nir_intrinsic_ssbo_atomic_and
:
2016 name
= "llvm.amdgcn.buffer.atomic.and";
2018 case nir_intrinsic_ssbo_atomic_or
:
2019 name
= "llvm.amdgcn.buffer.atomic.or";
2021 case nir_intrinsic_ssbo_atomic_xor
:
2022 name
= "llvm.amdgcn.buffer.atomic.xor";
2024 case nir_intrinsic_ssbo_atomic_exchange
:
2025 name
= "llvm.amdgcn.buffer.atomic.swap";
2027 case nir_intrinsic_ssbo_atomic_comp_swap
:
2028 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
2034 return ac_emit_llvm_intrinsic(&ctx
->ac
, name
, ctx
->i32
, params
, arg_count
, 0);
2037 static LLVMValueRef
visit_load_buffer(struct nir_to_llvm_context
*ctx
,
2038 nir_intrinsic_instr
*instr
)
2040 const char *load_name
;
2041 LLVMTypeRef data_type
= ctx
->f32
;
2042 if (instr
->num_components
== 3)
2043 data_type
= LLVMVectorType(ctx
->f32
, 4);
2044 else if (instr
->num_components
> 1)
2045 data_type
= LLVMVectorType(ctx
->f32
, instr
->num_components
);
2047 if (instr
->num_components
== 4 || instr
->num_components
== 3)
2048 load_name
= "llvm.amdgcn.buffer.load.v4f32";
2049 else if (instr
->num_components
== 2)
2050 load_name
= "llvm.amdgcn.buffer.load.v2f32";
2051 else if (instr
->num_components
== 1)
2052 load_name
= "llvm.amdgcn.buffer.load.f32";
2056 LLVMValueRef params
[] = {
2057 get_src(ctx
, instr
->src
[0]),
2058 LLVMConstInt(ctx
->i32
, 0, false),
2059 get_src(ctx
, instr
->src
[1]),
2060 LLVMConstInt(ctx
->i1
, 0, false),
2061 LLVMConstInt(ctx
->i1
, 0, false),
2065 ac_emit_llvm_intrinsic(&ctx
->ac
, load_name
, data_type
, params
, 5, 0);
2067 if (instr
->num_components
== 3)
2068 ret
= trim_vector(ctx
, ret
, 3);
2070 return LLVMBuildBitCast(ctx
->builder
, ret
,
2071 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2074 static LLVMValueRef
visit_load_ubo_buffer(struct nir_to_llvm_context
*ctx
,
2075 nir_intrinsic_instr
*instr
)
2077 LLVMValueRef results
[4], ret
;
2078 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
2079 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
2081 rsrc
= LLVMBuildBitCast(ctx
->builder
, rsrc
, LLVMVectorType(ctx
->i8
, 16), "");
2083 for (unsigned i
= 0; i
< instr
->num_components
; ++i
) {
2084 LLVMValueRef params
[] = {
2086 LLVMBuildAdd(ctx
->builder
, LLVMConstInt(ctx
->i32
, 4 * i
, 0),
2089 results
[i
] = ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.SI.load.const", ctx
->f32
,
2090 params
, 2, AC_FUNC_ATTR_READNONE
);
2094 ret
= ac_build_gather_values(&ctx
->ac
, results
, instr
->num_components
);
2095 return LLVMBuildBitCast(ctx
->builder
, ret
,
2096 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2100 radv_get_deref_offset(struct nir_to_llvm_context
*ctx
, nir_deref
*tail
,
2101 bool vs_in
, unsigned *const_out
, LLVMValueRef
*indir_out
)
2103 unsigned const_offset
= 0;
2104 LLVMValueRef offset
= NULL
;
2107 while (tail
->child
!= NULL
) {
2108 const struct glsl_type
*parent_type
= tail
->type
;
2111 if (tail
->deref_type
== nir_deref_type_array
) {
2112 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2113 LLVMValueRef index
, stride
, local_offset
;
2114 unsigned size
= glsl_count_attribute_slots(tail
->type
, vs_in
);
2116 const_offset
+= size
* deref_array
->base_offset
;
2117 if (deref_array
->deref_array_type
== nir_deref_array_type_direct
)
2120 assert(deref_array
->deref_array_type
== nir_deref_array_type_indirect
);
2121 index
= get_src(ctx
, deref_array
->indirect
);
2122 stride
= LLVMConstInt(ctx
->i32
, size
, 0);
2123 local_offset
= LLVMBuildMul(ctx
->builder
, stride
, index
, "");
2126 offset
= LLVMBuildAdd(ctx
->builder
, offset
, local_offset
, "");
2128 offset
= local_offset
;
2129 } else if (tail
->deref_type
== nir_deref_type_struct
) {
2130 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
2132 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
2133 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
2134 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
2137 unreachable("unsupported deref type");
2141 if (const_offset
&& offset
)
2142 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
2143 LLVMConstInt(ctx
->i32
, const_offset
, 0),
2146 *const_out
= const_offset
;
2147 *indir_out
= offset
;
2150 static LLVMValueRef
visit_load_var(struct nir_to_llvm_context
*ctx
,
2151 nir_intrinsic_instr
*instr
)
2153 LLVMValueRef values
[4];
2154 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2155 int ve
= instr
->dest
.ssa
.num_components
;
2156 LLVMValueRef indir_index
;
2157 unsigned const_index
;
2158 switch (instr
->variables
[0]->var
->data
.mode
) {
2159 case nir_var_shader_in
:
2160 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
,
2161 ctx
->stage
== MESA_SHADER_VERTEX
,
2162 &const_index
, &indir_index
);
2163 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2165 unsigned count
= glsl_count_attribute_slots(
2166 instr
->variables
[0]->var
->type
,
2167 ctx
->stage
== MESA_SHADER_VERTEX
);
2168 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2169 &ctx
->ac
, ctx
->inputs
+ idx
+ chan
, count
,
2172 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2176 values
[chan
] = ctx
->inputs
[idx
+ chan
+ const_index
* 4];
2178 return to_integer(ctx
, ac_build_gather_values(&ctx
->ac
, values
, ve
));
2181 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2182 &const_index
, &indir_index
);
2183 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2185 unsigned count
= glsl_count_attribute_slots(
2186 instr
->variables
[0]->var
->type
, false);
2187 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2188 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2191 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2195 values
[chan
] = LLVMBuildLoad(ctx
->builder
, ctx
->locals
[idx
+ chan
+ const_index
* 4], "");
2198 return to_integer(ctx
, ac_build_gather_values(&ctx
->ac
, values
, ve
));
2199 case nir_var_shader_out
:
2200 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2201 &const_index
, &indir_index
);
2202 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2204 unsigned count
= glsl_count_attribute_slots(
2205 instr
->variables
[0]->var
->type
, false);
2206 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2207 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
2210 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2214 values
[chan
] = LLVMBuildLoad(ctx
->builder
,
2215 ctx
->outputs
[idx
+ chan
+ const_index
* 4],
2219 return to_integer(ctx
, ac_build_gather_values(&ctx
->ac
, values
, ve
));
2220 case nir_var_shared
: {
2221 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2222 &const_index
, &indir_index
);
2223 LLVMValueRef ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
2224 LLVMValueRef derived_ptr
;
2226 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2227 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, chan
, false);
2229 index
= LLVMBuildAdd(ctx
->builder
, index
, indir_index
, "");
2230 derived_ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, "");
2231 values
[chan
] = LLVMBuildLoad(ctx
->builder
, derived_ptr
, "");
2233 return to_integer(ctx
, ac_build_gather_values(&ctx
->ac
, values
, ve
));
2242 visit_store_var(struct nir_to_llvm_context
*ctx
,
2243 nir_intrinsic_instr
*instr
)
2245 LLVMValueRef temp_ptr
, value
;
2246 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2247 LLVMValueRef src
= to_float(ctx
, get_src(ctx
, instr
->src
[0]));
2248 int writemask
= instr
->const_index
[0];
2249 LLVMValueRef indir_index
;
2250 unsigned const_index
;
2251 switch (instr
->variables
[0]->var
->data
.mode
) {
2252 case nir_var_shader_out
:
2253 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2254 &const_index
, &indir_index
);
2255 for (unsigned chan
= 0; chan
< 4; chan
++) {
2257 if (!(writemask
& (1 << chan
)))
2259 if (get_llvm_num_components(src
) == 1)
2262 value
= LLVMBuildExtractElement(ctx
->builder
, src
,
2263 LLVMConstInt(ctx
->i32
,
2267 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CLIP_DIST0
||
2268 instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CULL_DIST0
)
2271 unsigned count
= glsl_count_attribute_slots(
2272 instr
->variables
[0]->var
->type
, false);
2273 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2274 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
2277 if (get_llvm_num_components(tmp_vec
) > 1) {
2278 tmp_vec
= LLVMBuildInsertElement(ctx
->builder
, tmp_vec
,
2279 value
, indir_index
, "");
2282 build_store_values_extended(ctx
, ctx
->outputs
+ idx
+ chan
,
2283 count
, stride
, tmp_vec
);
2286 temp_ptr
= ctx
->outputs
[idx
+ chan
+ const_index
* stride
];
2288 LLVMBuildStore(ctx
->builder
, value
, temp_ptr
);
2293 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2294 &const_index
, &indir_index
);
2295 for (unsigned chan
= 0; chan
< 4; chan
++) {
2296 if (!(writemask
& (1 << chan
)))
2299 if (get_llvm_num_components(src
) == 1)
2302 value
= LLVMBuildExtractElement(ctx
->builder
, src
,
2303 LLVMConstInt(ctx
->i32
, chan
, false), "");
2305 unsigned count
= glsl_count_attribute_slots(
2306 instr
->variables
[0]->var
->type
, false);
2307 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2308 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2311 tmp_vec
= LLVMBuildInsertElement(ctx
->builder
, tmp_vec
,
2312 value
, indir_index
, "");
2313 build_store_values_extended(ctx
, ctx
->locals
+ idx
+ chan
,
2316 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
2318 LLVMBuildStore(ctx
->builder
, value
, temp_ptr
);
2322 case nir_var_shared
: {
2324 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2325 &const_index
, &indir_index
);
2327 ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
2328 LLVMValueRef derived_ptr
;
2330 for (unsigned chan
= 0; chan
< 4; chan
++) {
2331 if (!(writemask
& (1 << chan
)))
2334 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, chan
, false);
2336 if (get_llvm_num_components(src
) == 1)
2339 value
= LLVMBuildExtractElement(ctx
->builder
, src
,
2340 LLVMConstInt(ctx
->i32
,
2345 index
= LLVMBuildAdd(ctx
->builder
, index
, indir_index
, "");
2347 derived_ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, "");
2348 LLVMBuildStore(ctx
->builder
,
2349 to_integer(ctx
, value
), derived_ptr
);
2358 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
2361 case GLSL_SAMPLER_DIM_BUF
:
2363 case GLSL_SAMPLER_DIM_1D
:
2364 return array
? 2 : 1;
2365 case GLSL_SAMPLER_DIM_2D
:
2366 return array
? 3 : 2;
2367 case GLSL_SAMPLER_DIM_MS
:
2368 return array
? 4 : 3;
2369 case GLSL_SAMPLER_DIM_3D
:
2370 case GLSL_SAMPLER_DIM_CUBE
:
2372 case GLSL_SAMPLER_DIM_RECT
:
2373 case GLSL_SAMPLER_DIM_SUBPASS
:
2375 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
2383 static LLVMValueRef
get_image_coords(struct nir_to_llvm_context
*ctx
,
2384 nir_intrinsic_instr
*instr
)
2386 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
2387 if(instr
->variables
[0]->deref
.child
)
2388 type
= instr
->variables
[0]->deref
.child
->type
;
2390 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
2391 LLVMValueRef coords
[4];
2392 LLVMValueRef masks
[] = {
2393 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
2394 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false),
2398 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2399 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
2400 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2401 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
2402 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2404 count
= image_type_to_components_count(dim
,
2405 glsl_sampler_type_is_array(type
));
2408 if (instr
->src
[0].ssa
->num_components
)
2409 res
= LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[0], "");
2416 for (chan
= 0; chan
< count
; ++chan
) {
2417 coords
[chan
] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[chan
], "");
2421 for (chan
= 0; chan
< count
; ++chan
)
2422 coords
[chan
] = LLVMBuildAdd(ctx
->builder
, coords
[chan
], LLVMBuildFPToUI(ctx
->builder
, ctx
->frag_pos
[chan
], ctx
->i32
, ""), "");
2425 coords
[count
] = llvm_extract_elem(ctx
, get_src(ctx
, instr
->src
[1]), 0);
2430 coords
[3] = LLVMGetUndef(ctx
->i32
);
2433 res
= ac_build_gather_values(&ctx
->ac
, coords
, count
);
2438 static void build_type_name_for_intr(
2440 char *buf
, unsigned bufsize
)
2442 LLVMTypeRef elem_type
= type
;
2444 assert(bufsize
>= 8);
2446 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
2447 int ret
= snprintf(buf
, bufsize
, "v%u",
2448 LLVMGetVectorSize(type
));
2450 char *type_name
= LLVMPrintTypeToString(type
);
2451 fprintf(stderr
, "Error building type name for: %s\n",
2455 elem_type
= LLVMGetElementType(type
);
2459 switch (LLVMGetTypeKind(elem_type
)) {
2461 case LLVMIntegerTypeKind
:
2462 snprintf(buf
, bufsize
, "i%d", LLVMGetIntTypeWidth(elem_type
));
2464 case LLVMFloatTypeKind
:
2465 snprintf(buf
, bufsize
, "f32");
2467 case LLVMDoubleTypeKind
:
2468 snprintf(buf
, bufsize
, "f64");
2473 static void get_image_intr_name(const char *base_name
,
2474 LLVMTypeRef data_type
,
2475 LLVMTypeRef coords_type
,
2476 LLVMTypeRef rsrc_type
,
2477 char *out_name
, unsigned out_len
)
2479 char coords_type_name
[8];
2481 build_type_name_for_intr(coords_type
, coords_type_name
,
2482 sizeof(coords_type_name
));
2484 if (HAVE_LLVM
<= 0x0309) {
2485 snprintf(out_name
, out_len
, "%s.%s", base_name
, coords_type_name
);
2487 char data_type_name
[8];
2488 char rsrc_type_name
[8];
2490 build_type_name_for_intr(data_type
, data_type_name
,
2491 sizeof(data_type_name
));
2492 build_type_name_for_intr(rsrc_type
, rsrc_type_name
,
2493 sizeof(rsrc_type_name
));
2494 snprintf(out_name
, out_len
, "%s.%s.%s.%s", base_name
,
2495 data_type_name
, coords_type_name
, rsrc_type_name
);
2499 static LLVMValueRef
visit_image_load(struct nir_to_llvm_context
*ctx
,
2500 nir_intrinsic_instr
*instr
)
2502 LLVMValueRef params
[7];
2504 char intrinsic_name
[64];
2505 const nir_variable
*var
= instr
->variables
[0]->var
;
2506 const struct glsl_type
*type
= var
->type
;
2507 if(instr
->variables
[0]->deref
.child
)
2508 type
= instr
->variables
[0]->deref
.child
->type
;
2510 type
= glsl_without_array(type
);
2511 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2512 params
[0] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
2513 params
[1] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
2514 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
2515 params
[2] = LLVMConstInt(ctx
->i32
, 0, false); /* voffset */
2516 params
[3] = LLVMConstInt(ctx
->i1
, 0, false); /* glc */
2517 params
[4] = LLVMConstInt(ctx
->i1
, 0, false); /* slc */
2518 res
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.load.format.v4f32", ctx
->v4f32
,
2521 res
= trim_vector(ctx
, res
, instr
->dest
.ssa
.num_components
);
2522 res
= to_integer(ctx
, res
);
2524 bool is_da
= glsl_sampler_type_is_array(type
) ||
2525 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
2526 LLVMValueRef da
= is_da
? ctx
->i32one
: ctx
->i32zero
;
2527 LLVMValueRef glc
= LLVMConstInt(ctx
->i1
, 0, false);
2528 LLVMValueRef slc
= LLVMConstInt(ctx
->i1
, 0, false);
2530 params
[0] = get_image_coords(ctx
, instr
);
2531 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
2532 params
[2] = LLVMConstInt(ctx
->i32
, 15, false); /* dmask */
2533 if (HAVE_LLVM
<= 0x0309) {
2534 params
[3] = LLVMConstInt(ctx
->i1
, 0, false); /* r128 */
2539 LLVMValueRef lwe
= LLVMConstInt(ctx
->i1
, 0, false);
2546 get_image_intr_name("llvm.amdgcn.image.load",
2547 ctx
->v4f32
, /* vdata */
2548 LLVMTypeOf(params
[0]), /* coords */
2549 LLVMTypeOf(params
[1]), /* rsrc */
2550 intrinsic_name
, sizeof(intrinsic_name
));
2552 res
= ac_emit_llvm_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->v4f32
,
2553 params
, 7, AC_FUNC_ATTR_READONLY
);
2555 return to_integer(ctx
, res
);
2558 static void visit_image_store(struct nir_to_llvm_context
*ctx
,
2559 nir_intrinsic_instr
*instr
)
2561 LLVMValueRef params
[8];
2562 char intrinsic_name
[64];
2563 const nir_variable
*var
= instr
->variables
[0]->var
;
2564 LLVMValueRef i1false
= LLVMConstInt(ctx
->i1
, 0, 0);
2565 LLVMValueRef i1true
= LLVMConstInt(ctx
->i1
, 1, 0);
2566 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2568 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
2569 ctx
->shader_info
->fs
.writes_memory
= true;
2571 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2572 params
[0] = to_float(ctx
, get_src(ctx
, instr
->src
[2])); /* data */
2573 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
2574 params
[2] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
2575 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
2576 params
[3] = LLVMConstInt(ctx
->i32
, 0, false); /* voffset */
2577 params
[4] = i1false
; /* glc */
2578 params
[5] = i1false
; /* slc */
2579 ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->voidt
,
2582 bool is_da
= glsl_sampler_type_is_array(type
) ||
2583 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
2584 LLVMValueRef da
= is_da
? i1true
: i1false
;
2585 LLVMValueRef glc
= i1false
;
2586 LLVMValueRef slc
= i1false
;
2588 params
[0] = to_float(ctx
, get_src(ctx
, instr
->src
[2]));
2589 params
[1] = get_image_coords(ctx
, instr
); /* coords */
2590 params
[2] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
2591 params
[3] = LLVMConstInt(ctx
->i32
, 15, false); /* dmask */
2592 if (HAVE_LLVM
<= 0x0309) {
2593 params
[4] = i1false
; /* r128 */
2598 LLVMValueRef lwe
= i1false
;
2605 get_image_intr_name("llvm.amdgcn.image.store",
2606 LLVMTypeOf(params
[0]), /* vdata */
2607 LLVMTypeOf(params
[1]), /* coords */
2608 LLVMTypeOf(params
[2]), /* rsrc */
2609 intrinsic_name
, sizeof(intrinsic_name
));
2611 ac_emit_llvm_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->voidt
,
2617 static LLVMValueRef
visit_image_atomic(struct nir_to_llvm_context
*ctx
,
2618 nir_intrinsic_instr
*instr
)
2620 LLVMValueRef params
[6];
2621 int param_count
= 0;
2622 const nir_variable
*var
= instr
->variables
[0]->var
;
2623 LLVMValueRef i1false
= LLVMConstInt(ctx
->i1
, 0, 0);
2624 LLVMValueRef i1true
= LLVMConstInt(ctx
->i1
, 1, 0);
2625 const char *base_name
= "llvm.amdgcn.image.atomic";
2626 const char *atomic_name
;
2627 LLVMValueRef coords
;
2628 char intrinsic_name
[32], coords_type
[8];
2629 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2631 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
2632 ctx
->shader_info
->fs
.writes_memory
= true;
2634 params
[param_count
++] = get_src(ctx
, instr
->src
[2]);
2635 if (instr
->intrinsic
== nir_intrinsic_image_atomic_comp_swap
)
2636 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
2638 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2639 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
2640 coords
= params
[param_count
++] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
2641 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
2642 params
[param_count
++] = ctx
->i32zero
; /* voffset */
2643 params
[param_count
++] = i1false
; /* glc */
2644 params
[param_count
++] = i1false
; /* slc */
2646 bool da
= glsl_sampler_type_is_array(type
) ||
2647 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
2649 coords
= params
[param_count
++] = get_image_coords(ctx
, instr
);
2650 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
2651 params
[param_count
++] = i1false
; /* r128 */
2652 params
[param_count
++] = da
? i1true
: i1false
; /* da */
2653 params
[param_count
++] = i1false
; /* slc */
2656 switch (instr
->intrinsic
) {
2657 case nir_intrinsic_image_atomic_add
:
2658 atomic_name
= "add";
2660 case nir_intrinsic_image_atomic_min
:
2661 atomic_name
= "smin";
2663 case nir_intrinsic_image_atomic_max
:
2664 atomic_name
= "smax";
2666 case nir_intrinsic_image_atomic_and
:
2667 atomic_name
= "and";
2669 case nir_intrinsic_image_atomic_or
:
2672 case nir_intrinsic_image_atomic_xor
:
2673 atomic_name
= "xor";
2675 case nir_intrinsic_image_atomic_exchange
:
2676 atomic_name
= "swap";
2678 case nir_intrinsic_image_atomic_comp_swap
:
2679 atomic_name
= "cmpswap";
2684 build_int_type_name(LLVMTypeOf(coords
),
2685 coords_type
, sizeof(coords_type
));
2687 snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2688 "%s.%s.%s", base_name
, atomic_name
, coords_type
);
2689 return ac_emit_llvm_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->i32
, params
, param_count
, 0);
2692 static LLVMValueRef
visit_image_size(struct nir_to_llvm_context
*ctx
,
2693 nir_intrinsic_instr
*instr
)
2696 LLVMValueRef params
[10];
2697 const nir_variable
*var
= instr
->variables
[0]->var
;
2698 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
2699 bool da
= glsl_sampler_type_is_array(var
->type
) ||
2700 glsl_get_sampler_dim(var
->type
) == GLSL_SAMPLER_DIM_CUBE
;
2701 if(instr
->variables
[0]->deref
.child
)
2702 type
= instr
->variables
[0]->deref
.child
->type
;
2704 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
2705 return get_buffer_size(ctx
, get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
), true);
2706 params
[0] = ctx
->i32zero
;
2707 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
2708 params
[2] = LLVMConstInt(ctx
->i32
, 15, false);
2709 params
[3] = ctx
->i32zero
;
2710 params
[4] = ctx
->i32zero
;
2711 params
[5] = da
? ctx
->i32one
: ctx
->i32zero
;
2712 params
[6] = ctx
->i32zero
;
2713 params
[7] = ctx
->i32zero
;
2714 params
[8] = ctx
->i32zero
;
2715 params
[9] = ctx
->i32zero
;
2717 res
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.SI.getresinfo.i32", ctx
->v4i32
,
2718 params
, 10, AC_FUNC_ATTR_READNONE
);
2720 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
2721 glsl_sampler_type_is_array(type
)) {
2722 LLVMValueRef two
= LLVMConstInt(ctx
->i32
, 2, false);
2723 LLVMValueRef six
= LLVMConstInt(ctx
->i32
, 6, false);
2724 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->builder
, res
, two
, "");
2725 z
= LLVMBuildSDiv(ctx
->builder
, z
, six
, "");
2726 res
= LLVMBuildInsertElement(ctx
->builder
, res
, z
, two
, "");
2731 static void emit_waitcnt(struct nir_to_llvm_context
*ctx
)
2733 LLVMValueRef args
[1] = {
2734 LLVMConstInt(ctx
->i32
, 0xf70, false),
2736 ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.amdgcn.s.waitcnt",
2737 ctx
->voidt
, args
, 1, 0);
2740 static void emit_barrier(struct nir_to_llvm_context
*ctx
)
2743 ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.amdgcn.s.barrier",
2744 ctx
->voidt
, NULL
, 0, 0);
2747 static void emit_discard_if(struct nir_to_llvm_context
*ctx
,
2748 nir_intrinsic_instr
*instr
)
2751 ctx
->shader_info
->fs
.can_discard
= true;
2753 cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
2754 get_src(ctx
, instr
->src
[0]),
2757 cond
= LLVMBuildSelect(ctx
->builder
, cond
,
2758 LLVMConstReal(ctx
->f32
, -1.0f
),
2760 ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.AMDGPU.kill",
2766 visit_load_local_invocation_index(struct nir_to_llvm_context
*ctx
)
2768 LLVMValueRef result
;
2769 LLVMValueRef thread_id
= get_thread_id(ctx
);
2770 result
= LLVMBuildAnd(ctx
->builder
, ctx
->tg_size
,
2771 LLVMConstInt(ctx
->i32
, 0xfc0, false), "");
2773 return LLVMBuildAdd(ctx
->builder
, result
, thread_id
, "");
2776 static LLVMValueRef
visit_var_atomic(struct nir_to_llvm_context
*ctx
,
2777 nir_intrinsic_instr
*instr
)
2779 LLVMValueRef ptr
, result
;
2780 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2781 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
2782 ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
2784 if (instr
->intrinsic
== nir_intrinsic_var_atomic_comp_swap
) {
2785 LLVMValueRef src1
= get_src(ctx
, instr
->src
[1]);
2786 result
= LLVMBuildAtomicCmpXchg(ctx
->builder
,
2788 LLVMAtomicOrderingSequentiallyConsistent
,
2789 LLVMAtomicOrderingSequentiallyConsistent
,
2792 LLVMAtomicRMWBinOp op
;
2793 switch (instr
->intrinsic
) {
2794 case nir_intrinsic_var_atomic_add
:
2795 op
= LLVMAtomicRMWBinOpAdd
;
2797 case nir_intrinsic_var_atomic_umin
:
2798 op
= LLVMAtomicRMWBinOpUMin
;
2800 case nir_intrinsic_var_atomic_umax
:
2801 op
= LLVMAtomicRMWBinOpUMax
;
2803 case nir_intrinsic_var_atomic_imin
:
2804 op
= LLVMAtomicRMWBinOpMin
;
2806 case nir_intrinsic_var_atomic_imax
:
2807 op
= LLVMAtomicRMWBinOpMax
;
2809 case nir_intrinsic_var_atomic_and
:
2810 op
= LLVMAtomicRMWBinOpAnd
;
2812 case nir_intrinsic_var_atomic_or
:
2813 op
= LLVMAtomicRMWBinOpOr
;
2815 case nir_intrinsic_var_atomic_xor
:
2816 op
= LLVMAtomicRMWBinOpXor
;
2818 case nir_intrinsic_var_atomic_exchange
:
2819 op
= LLVMAtomicRMWBinOpXchg
;
2825 result
= LLVMBuildAtomicRMW(ctx
->builder
, op
, ptr
, to_integer(ctx
, src
),
2826 LLVMAtomicOrderingSequentiallyConsistent
,
2832 #define INTERP_CENTER 0
2833 #define INTERP_CENTROID 1
2834 #define INTERP_SAMPLE 2
2836 static LLVMValueRef
lookup_interp_param(struct nir_to_llvm_context
*ctx
,
2837 enum glsl_interp_mode interp
, unsigned location
)
2840 case INTERP_MODE_FLAT
:
2843 case INTERP_MODE_SMOOTH
:
2844 case INTERP_MODE_NONE
:
2845 if (location
== INTERP_CENTER
)
2846 return ctx
->persp_center
;
2847 else if (location
== INTERP_CENTROID
)
2848 return ctx
->persp_centroid
;
2849 else if (location
== INTERP_SAMPLE
)
2850 return ctx
->persp_sample
;
2852 case INTERP_MODE_NOPERSPECTIVE
:
2853 if (location
== INTERP_CENTER
)
2854 return ctx
->linear_center
;
2855 else if (location
== INTERP_CENTROID
)
2856 return ctx
->linear_centroid
;
2857 else if (location
== INTERP_SAMPLE
)
2858 return ctx
->linear_sample
;
2864 static LLVMValueRef
load_sample_position(struct nir_to_llvm_context
*ctx
,
2865 LLVMValueRef sample_id
)
2867 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
2868 LLVMValueRef offset0
= LLVMBuildMul(ctx
->builder
, sample_id
, LLVMConstInt(ctx
->i32
, 8, false), "");
2869 LLVMValueRef offset1
= LLVMBuildAdd(ctx
->builder
, offset0
, LLVMConstInt(ctx
->i32
, 4, false), "");
2870 LLVMValueRef result
[2];
2872 result
[0] = build_indexed_load_const(ctx
, ctx
->sample_positions
, offset0
);
2873 result
[1] = build_indexed_load_const(ctx
, ctx
->sample_positions
, offset1
);
2875 return ac_build_gather_values(&ctx
->ac
, result
, 2);
2878 static LLVMValueRef
load_sample_pos(struct nir_to_llvm_context
*ctx
)
2880 LLVMValueRef values
[2];
2882 values
[0] = emit_ffract(ctx
, ctx
->frag_pos
[0]);
2883 values
[1] = emit_ffract(ctx
, ctx
->frag_pos
[1]);
2884 return ac_build_gather_values(&ctx
->ac
, values
, 2);
2887 static LLVMValueRef
visit_interp(struct nir_to_llvm_context
*ctx
,
2888 nir_intrinsic_instr
*instr
)
2890 LLVMValueRef result
[2];
2891 LLVMValueRef interp_param
, attr_number
;
2894 LLVMValueRef src_c0
, src_c1
;
2895 const char *intr_name
;
2897 int input_index
= instr
->variables
[0]->var
->data
.location
- VARYING_SLOT_VAR0
;
2898 switch (instr
->intrinsic
) {
2899 case nir_intrinsic_interp_var_at_centroid
:
2900 location
= INTERP_CENTROID
;
2902 case nir_intrinsic_interp_var_at_sample
:
2903 case nir_intrinsic_interp_var_at_offset
:
2904 location
= INTERP_SAMPLE
;
2905 src0
= get_src(ctx
, instr
->src
[0]);
2911 if (instr
->intrinsic
== nir_intrinsic_interp_var_at_offset
) {
2912 src_c0
= to_float(ctx
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32zero
, ""));
2913 src_c1
= to_float(ctx
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32one
, ""));
2914 } else if (instr
->intrinsic
== nir_intrinsic_interp_var_at_sample
) {
2915 LLVMValueRef sample_position
;
2916 LLVMValueRef halfval
= LLVMConstReal(ctx
->f32
, 0.5f
);
2918 /* fetch sample ID */
2919 sample_position
= load_sample_position(ctx
, src0
);
2921 src_c0
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->i32zero
, "");
2922 src_c0
= LLVMBuildFSub(ctx
->builder
, src_c0
, halfval
, "");
2923 src_c1
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->i32one
, "");
2924 src_c1
= LLVMBuildFSub(ctx
->builder
, src_c1
, halfval
, "");
2926 interp_param
= lookup_interp_param(ctx
, instr
->variables
[0]->var
->data
.interpolation
, location
);
2927 attr_number
= LLVMConstInt(ctx
->i32
, input_index
, false);
2929 if (location
== INTERP_SAMPLE
) {
2930 LLVMValueRef ij_out
[2];
2931 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
, interp_param
);
2934 * take the I then J parameters, and the DDX/Y for it, and
2935 * calculate the IJ inputs for the interpolator.
2936 * temp1 = ddx * offset/sample.x + I;
2937 * interp_param.I = ddy * offset/sample.y + temp1;
2938 * temp1 = ddx * offset/sample.x + J;
2939 * interp_param.J = ddy * offset/sample.y + temp1;
2941 for (unsigned i
= 0; i
< 2; i
++) {
2942 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->i32
, i
, false);
2943 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->i32
, i
+ 2, false);
2944 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->builder
,
2945 ddxy_out
, ix_ll
, "");
2946 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->builder
,
2947 ddxy_out
, iy_ll
, "");
2948 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->builder
,
2949 interp_param
, ix_ll
, "");
2950 LLVMValueRef temp1
, temp2
;
2952 interp_el
= LLVMBuildBitCast(ctx
->builder
, interp_el
,
2955 temp1
= LLVMBuildFMul(ctx
->builder
, ddx_el
, src_c0
, "");
2956 temp1
= LLVMBuildFAdd(ctx
->builder
, temp1
, interp_el
, "");
2958 temp2
= LLVMBuildFMul(ctx
->builder
, ddy_el
, src_c1
, "");
2959 temp2
= LLVMBuildFAdd(ctx
->builder
, temp2
, temp1
, "");
2961 ij_out
[i
] = LLVMBuildBitCast(ctx
->builder
,
2962 temp2
, ctx
->i32
, "");
2964 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
2967 intr_name
= interp_param
? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
2968 for (chan
= 0; chan
< 2; chan
++) {
2969 LLVMValueRef args
[4];
2970 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
2972 args
[0] = llvm_chan
;
2973 args
[1] = attr_number
;
2974 args
[2] = ctx
->prim_mask
;
2975 args
[3] = interp_param
;
2976 result
[chan
] = ac_emit_llvm_intrinsic(&ctx
->ac
, intr_name
,
2977 ctx
->f32
, args
, args
[3] ? 4 : 3,
2978 AC_FUNC_ATTR_READNONE
);
2980 return ac_build_gather_values(&ctx
->ac
, result
, 2);
2984 visit_emit_vertex(struct nir_to_llvm_context
*ctx
,
2985 nir_intrinsic_instr
*instr
)
2987 LLVMValueRef gs_next_vertex
;
2988 LLVMValueRef can_emit
, kill
;
2989 LLVMValueRef args
[2];
2992 assert(instr
->const_index
[0] == 0);
2993 /* Write vertex attribute values to GSVS ring */
2994 gs_next_vertex
= LLVMBuildLoad(ctx
->builder
,
2995 ctx
->gs_next_vertex
,
2998 /* If this thread has already emitted the declared maximum number of
2999 * vertices, kill it: excessive vertex emissions are not supposed to
3000 * have any effect, and GS threads have no externally observable
3001 * effects other than emitting vertices.
3003 can_emit
= LLVMBuildICmp(ctx
->builder
, LLVMIntULT
, gs_next_vertex
,
3004 LLVMConstInt(ctx
->i32
, ctx
->gs_max_out_vertices
, false), "");
3006 kill
= LLVMBuildSelect(ctx
->builder
, can_emit
,
3007 LLVMConstReal(ctx
->f32
, 1.0f
),
3008 LLVMConstReal(ctx
->f32
, -1.0f
), "");
3009 ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.AMDGPU.kill",
3010 ctx
->voidt
, &kill
, 1, 0);
3012 /* loop num outputs */
3014 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
3015 LLVMValueRef
*out_ptr
= &ctx
->outputs
[i
* 4];
3016 if (!(ctx
->output_mask
& (1ull << i
)))
3019 for (unsigned j
= 0; j
< 4; j
++) {
3020 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
,
3022 LLVMValueRef voffset
= LLVMConstInt(ctx
->i32
, (idx
* 4 + j
) * ctx
->gs_max_out_vertices
, false);
3023 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, gs_next_vertex
, "");
3024 voffset
= LLVMBuildMul(ctx
->builder
, voffset
, LLVMConstInt(ctx
->i32
, 4, false), "");
3026 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->i32
, "");
3028 build_tbuffer_store(ctx
, ctx
->gsvs_ring
,
3030 voffset
, ctx
->gs2vs_offset
, 0,
3031 V_008F0C_BUF_DATA_FORMAT_32
,
3032 V_008F0C_BUF_NUM_FORMAT_UINT
,
3038 gs_next_vertex
= LLVMBuildAdd(ctx
->builder
, gs_next_vertex
,
3040 LLVMBuildStore(ctx
->builder
, gs_next_vertex
, ctx
->gs_next_vertex
);
3041 args
[0] = LLVMConstInt(ctx
->i32
, SENDMSG_GS_OP_EMIT
| SENDMSG_GS
| (0 << 8), false);
3042 args
[1] = ctx
->gs_wave_id
;
3043 ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.SI.sendmsg",
3044 ctx
->voidt
, args
, 2, 0);
3048 visit_end_primitive(struct nir_to_llvm_context
*ctx
,
3049 nir_intrinsic_instr
*instr
)
3051 LLVMValueRef args
[2];
3053 assert(instr
->const_index
[0] == 0);
3054 args
[0] = LLVMConstInt(ctx
->i32
, SENDMSG_GS_OP_CUT
| SENDMSG_GS
| (0 << 8), false);
3055 args
[1] = ctx
->gs_wave_id
;
3057 ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.SI.sendmsg", ctx
->voidt
,
3061 static void visit_intrinsic(struct nir_to_llvm_context
*ctx
,
3062 nir_intrinsic_instr
*instr
)
3064 LLVMValueRef result
= NULL
;
3066 switch (instr
->intrinsic
) {
3067 case nir_intrinsic_load_work_group_id
: {
3068 result
= ctx
->workgroup_ids
;
3071 case nir_intrinsic_load_base_vertex
: {
3072 result
= ctx
->base_vertex
;
3075 case nir_intrinsic_load_vertex_id_zero_base
: {
3076 result
= ctx
->vertex_id
;
3079 case nir_intrinsic_load_local_invocation_id
: {
3080 result
= ctx
->local_invocation_ids
;
3083 case nir_intrinsic_load_base_instance
:
3084 result
= ctx
->start_instance
;
3086 case nir_intrinsic_load_invocation_id
:
3087 result
= ctx
->gs_invocation_id
;
3089 case nir_intrinsic_load_primitive_id
:
3090 if (ctx
->stage
== MESA_SHADER_GEOMETRY
)
3091 result
= ctx
->gs_prim_id
;
3093 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
3095 case nir_intrinsic_load_sample_id
:
3096 ctx
->shader_info
->fs
.force_persample
= true;
3097 result
= unpack_param(ctx
, ctx
->ancillary
, 8, 4);
3099 case nir_intrinsic_load_sample_pos
:
3100 ctx
->shader_info
->fs
.force_persample
= true;
3101 result
= load_sample_pos(ctx
);
3103 case nir_intrinsic_load_front_face
:
3104 result
= ctx
->front_face
;
3106 case nir_intrinsic_load_instance_id
:
3107 result
= ctx
->instance_id
;
3108 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
3109 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
3111 case nir_intrinsic_load_num_work_groups
:
3112 result
= ctx
->num_work_groups
;
3114 case nir_intrinsic_load_local_invocation_index
:
3115 result
= visit_load_local_invocation_index(ctx
);
3117 case nir_intrinsic_load_push_constant
:
3118 result
= visit_load_push_constant(ctx
, instr
);
3120 case nir_intrinsic_vulkan_resource_index
:
3121 result
= visit_vulkan_resource_index(ctx
, instr
);
3123 case nir_intrinsic_store_ssbo
:
3124 visit_store_ssbo(ctx
, instr
);
3126 case nir_intrinsic_load_ssbo
:
3127 result
= visit_load_buffer(ctx
, instr
);
3129 case nir_intrinsic_ssbo_atomic_add
:
3130 case nir_intrinsic_ssbo_atomic_imin
:
3131 case nir_intrinsic_ssbo_atomic_umin
:
3132 case nir_intrinsic_ssbo_atomic_imax
:
3133 case nir_intrinsic_ssbo_atomic_umax
:
3134 case nir_intrinsic_ssbo_atomic_and
:
3135 case nir_intrinsic_ssbo_atomic_or
:
3136 case nir_intrinsic_ssbo_atomic_xor
:
3137 case nir_intrinsic_ssbo_atomic_exchange
:
3138 case nir_intrinsic_ssbo_atomic_comp_swap
:
3139 result
= visit_atomic_ssbo(ctx
, instr
);
3141 case nir_intrinsic_load_ubo
:
3142 result
= visit_load_ubo_buffer(ctx
, instr
);
3144 case nir_intrinsic_get_buffer_size
:
3145 result
= visit_get_buffer_size(ctx
, instr
);
3147 case nir_intrinsic_load_var
:
3148 result
= visit_load_var(ctx
, instr
);
3150 case nir_intrinsic_store_var
:
3151 visit_store_var(ctx
, instr
);
3153 case nir_intrinsic_image_load
:
3154 result
= visit_image_load(ctx
, instr
);
3156 case nir_intrinsic_image_store
:
3157 visit_image_store(ctx
, instr
);
3159 case nir_intrinsic_image_atomic_add
:
3160 case nir_intrinsic_image_atomic_min
:
3161 case nir_intrinsic_image_atomic_max
:
3162 case nir_intrinsic_image_atomic_and
:
3163 case nir_intrinsic_image_atomic_or
:
3164 case nir_intrinsic_image_atomic_xor
:
3165 case nir_intrinsic_image_atomic_exchange
:
3166 case nir_intrinsic_image_atomic_comp_swap
:
3167 result
= visit_image_atomic(ctx
, instr
);
3169 case nir_intrinsic_image_size
:
3170 result
= visit_image_size(ctx
, instr
);
3172 case nir_intrinsic_discard
:
3173 ctx
->shader_info
->fs
.can_discard
= true;
3174 ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.AMDGPU.kilp",
3178 case nir_intrinsic_discard_if
:
3179 emit_discard_if(ctx
, instr
);
3181 case nir_intrinsic_memory_barrier
:
3184 case nir_intrinsic_barrier
:
3187 case nir_intrinsic_var_atomic_add
:
3188 case nir_intrinsic_var_atomic_imin
:
3189 case nir_intrinsic_var_atomic_umin
:
3190 case nir_intrinsic_var_atomic_imax
:
3191 case nir_intrinsic_var_atomic_umax
:
3192 case nir_intrinsic_var_atomic_and
:
3193 case nir_intrinsic_var_atomic_or
:
3194 case nir_intrinsic_var_atomic_xor
:
3195 case nir_intrinsic_var_atomic_exchange
:
3196 case nir_intrinsic_var_atomic_comp_swap
:
3197 result
= visit_var_atomic(ctx
, instr
);
3199 case nir_intrinsic_interp_var_at_centroid
:
3200 case nir_intrinsic_interp_var_at_sample
:
3201 case nir_intrinsic_interp_var_at_offset
:
3202 result
= visit_interp(ctx
, instr
);
3204 case nir_intrinsic_emit_vertex
:
3205 visit_emit_vertex(ctx
, instr
);
3207 case nir_intrinsic_end_primitive
:
3208 visit_end_primitive(ctx
, instr
);
3211 fprintf(stderr
, "Unknown intrinsic: ");
3212 nir_print_instr(&instr
->instr
, stderr
);
3213 fprintf(stderr
, "\n");
3217 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
3221 static LLVMValueRef
get_sampler_desc(struct nir_to_llvm_context
*ctx
,
3222 nir_deref_var
*deref
,
3223 enum desc_type desc_type
)
3225 unsigned desc_set
= deref
->var
->data
.descriptor_set
;
3226 LLVMValueRef list
= ctx
->descriptor_sets
[desc_set
];
3227 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[desc_set
].layout
;
3228 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ deref
->var
->data
.binding
;
3229 unsigned offset
= binding
->offset
;
3230 unsigned stride
= binding
->size
;
3232 LLVMBuilderRef builder
= ctx
->builder
;
3234 LLVMValueRef index
= NULL
;
3236 assert(deref
->var
->data
.binding
< layout
->binding_count
);
3238 switch (desc_type
) {
3250 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
3260 unreachable("invalid desc_type\n");
3263 if (deref
->deref
.child
) {
3264 nir_deref_array
*child
= (nir_deref_array
*)deref
->deref
.child
;
3266 assert(child
->deref_array_type
!= nir_deref_array_type_wildcard
);
3267 offset
+= child
->base_offset
* stride
;
3268 if (child
->deref_array_type
== nir_deref_array_type_indirect
) {
3269 index
= get_src(ctx
, child
->indirect
);
3273 assert(stride
% type_size
== 0);
3276 index
= ctx
->i32zero
;
3278 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, stride
/ type_size
, 0), "");
3280 list
= build_gep0(ctx
, list
, LLVMConstInt(ctx
->i32
, offset
, 0));
3281 list
= LLVMBuildPointerCast(builder
, list
, const_array(type
, 0), "");
3283 return build_indexed_load_const(ctx
, list
, index
);
3286 static void set_tex_fetch_args(struct nir_to_llvm_context
*ctx
,
3287 struct ac_tex_info
*tinfo
,
3288 nir_tex_instr
*instr
,
3290 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
3291 LLVMValueRef
*param
, unsigned count
,
3295 unsigned is_rect
= 0;
3296 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
3298 if (op
== nir_texop_lod
)
3300 /* Pad to power of two vector */
3301 while (count
< util_next_power_of_two(count
))
3302 param
[count
++] = LLVMGetUndef(ctx
->i32
);
3305 tinfo
->args
[0] = ac_build_gather_values(&ctx
->ac
, param
, count
);
3307 tinfo
->args
[0] = param
[0];
3309 tinfo
->args
[1] = res_ptr
;
3312 if (op
== nir_texop_txf
||
3313 op
== nir_texop_txf_ms
||
3314 op
== nir_texop_query_levels
||
3315 op
== nir_texop_texture_samples
||
3316 op
== nir_texop_txs
)
3317 tinfo
->dst_type
= ctx
->v4i32
;
3319 tinfo
->dst_type
= ctx
->v4f32
;
3320 tinfo
->args
[num_args
++] = samp_ptr
;
3323 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
&& op
== nir_texop_txf
) {
3324 tinfo
->args
[0] = res_ptr
;
3325 tinfo
->args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
3326 tinfo
->args
[2] = param
[0];
3327 tinfo
->arg_count
= 3;
3331 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, dmask
, 0);
3332 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, is_rect
, 0); /* unorm */
3333 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* r128 */
3334 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, da
? 1 : 0, 0);
3335 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* glc */
3336 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* slc */
3337 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* tfe */
3338 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* lwe */
3340 tinfo
->arg_count
= num_args
;
3343 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
3346 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
3347 * filtering manually. The driver sets img7 to a mask clearing
3348 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
3349 * s_and_b32 samp0, samp0, img7
3352 * The ANISO_OVERRIDE sampler field enables this fix in TA.
3354 static LLVMValueRef
sici_fix_sampler_aniso(struct nir_to_llvm_context
*ctx
,
3355 LLVMValueRef res
, LLVMValueRef samp
)
3357 LLVMBuilderRef builder
= ctx
->builder
;
3358 LLVMValueRef img7
, samp0
;
3360 if (ctx
->options
->chip_class
>= VI
)
3363 img7
= LLVMBuildExtractElement(builder
, res
,
3364 LLVMConstInt(ctx
->i32
, 7, 0), "");
3365 samp0
= LLVMBuildExtractElement(builder
, samp
,
3366 LLVMConstInt(ctx
->i32
, 0, 0), "");
3367 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
3368 return LLVMBuildInsertElement(builder
, samp
, samp0
,
3369 LLVMConstInt(ctx
->i32
, 0, 0), "");
3372 static void tex_fetch_ptrs(struct nir_to_llvm_context
*ctx
,
3373 nir_tex_instr
*instr
,
3374 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
3375 LLVMValueRef
*fmask_ptr
)
3377 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
3378 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_BUFFER
);
3380 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_IMAGE
);
3383 *samp_ptr
= get_sampler_desc(ctx
, instr
->sampler
, DESC_SAMPLER
);
3385 *samp_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_SAMPLER
);
3386 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
3387 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
3389 if (fmask_ptr
&& !instr
->sampler
&& (instr
->op
== nir_texop_txf_ms
||
3390 instr
->op
== nir_texop_samples_identical
))
3391 *fmask_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_FMASK
);
3394 static void visit_tex(struct nir_to_llvm_context
*ctx
, nir_tex_instr
*instr
)
3396 LLVMValueRef result
= NULL
;
3397 struct ac_tex_info tinfo
= { 0 };
3398 unsigned dmask
= 0xf;
3399 LLVMValueRef address
[16];
3400 LLVMValueRef coords
[5];
3401 LLVMValueRef coord
= NULL
, lod
= NULL
, comparator
= NULL
;
3402 LLVMValueRef bias
= NULL
, offsets
= NULL
;
3403 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
, sample_index
= NULL
;
3404 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
3405 LLVMValueRef derivs
[6];
3406 unsigned chan
, count
= 0;
3407 unsigned const_src
= 0, num_deriv_comp
= 0;
3409 tex_fetch_ptrs(ctx
, instr
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
3411 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3412 switch (instr
->src
[i
].src_type
) {
3413 case nir_tex_src_coord
:
3414 coord
= get_src(ctx
, instr
->src
[i
].src
);
3416 case nir_tex_src_projector
:
3418 case nir_tex_src_comparator
:
3419 comparator
= get_src(ctx
, instr
->src
[i
].src
);
3421 case nir_tex_src_offset
:
3422 offsets
= get_src(ctx
, instr
->src
[i
].src
);
3425 case nir_tex_src_bias
:
3426 bias
= get_src(ctx
, instr
->src
[i
].src
);
3428 case nir_tex_src_lod
:
3429 lod
= get_src(ctx
, instr
->src
[i
].src
);
3431 case nir_tex_src_ms_index
:
3432 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
3434 case nir_tex_src_ms_mcs
:
3436 case nir_tex_src_ddx
:
3437 ddx
= get_src(ctx
, instr
->src
[i
].src
);
3438 num_deriv_comp
= instr
->src
[i
].src
.ssa
->num_components
;
3440 case nir_tex_src_ddy
:
3441 ddy
= get_src(ctx
, instr
->src
[i
].src
);
3443 case nir_tex_src_texture_offset
:
3444 case nir_tex_src_sampler_offset
:
3445 case nir_tex_src_plane
:
3451 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
3452 result
= get_buffer_size(ctx
, res_ptr
, false);
3456 if (instr
->op
== nir_texop_texture_samples
) {
3457 LLVMValueRef res
, samples
, is_msaa
;
3458 res
= LLVMBuildBitCast(ctx
->builder
, res_ptr
, ctx
->v8i32
, "");
3459 samples
= LLVMBuildExtractElement(ctx
->builder
, res
,
3460 LLVMConstInt(ctx
->i32
, 3, false), "");
3461 is_msaa
= LLVMBuildLShr(ctx
->builder
, samples
,
3462 LLVMConstInt(ctx
->i32
, 28, false), "");
3463 is_msaa
= LLVMBuildAnd(ctx
->builder
, is_msaa
,
3464 LLVMConstInt(ctx
->i32
, 0xe, false), "");
3465 is_msaa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, is_msaa
,
3466 LLVMConstInt(ctx
->i32
, 0xe, false), "");
3468 samples
= LLVMBuildLShr(ctx
->builder
, samples
,
3469 LLVMConstInt(ctx
->i32
, 16, false), "");
3470 samples
= LLVMBuildAnd(ctx
->builder
, samples
,
3471 LLVMConstInt(ctx
->i32
, 0xf, false), "");
3472 samples
= LLVMBuildShl(ctx
->builder
, ctx
->i32one
,
3474 samples
= LLVMBuildSelect(ctx
->builder
, is_msaa
, samples
,
3481 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
3482 coords
[chan
] = llvm_extract_elem(ctx
, coord
, chan
);
3484 if (offsets
&& instr
->op
!= nir_texop_txf
) {
3485 LLVMValueRef offset
[3], pack
;
3486 for (chan
= 0; chan
< 3; ++chan
)
3487 offset
[chan
] = ctx
->i32zero
;
3489 tinfo
.has_offset
= true;
3490 for (chan
= 0; chan
< get_llvm_num_components(offsets
); chan
++) {
3491 offset
[chan
] = llvm_extract_elem(ctx
, offsets
, chan
);
3492 offset
[chan
] = LLVMBuildAnd(ctx
->builder
, offset
[chan
],
3493 LLVMConstInt(ctx
->i32
, 0x3f, false), "");
3495 offset
[chan
] = LLVMBuildShl(ctx
->builder
, offset
[chan
],
3496 LLVMConstInt(ctx
->i32
, chan
* 8, false), "");
3498 pack
= LLVMBuildOr(ctx
->builder
, offset
[0], offset
[1], "");
3499 pack
= LLVMBuildOr(ctx
->builder
, pack
, offset
[2], "");
3500 address
[count
++] = pack
;
3503 /* pack LOD bias value */
3504 if (instr
->op
== nir_texop_txb
&& bias
) {
3505 address
[count
++] = bias
;
3508 /* Pack depth comparison value */
3509 if (instr
->is_shadow
&& comparator
) {
3510 address
[count
++] = llvm_extract_elem(ctx
, comparator
, 0);
3513 /* pack derivatives */
3515 switch (instr
->sampler_dim
) {
3516 case GLSL_SAMPLER_DIM_3D
:
3517 case GLSL_SAMPLER_DIM_CUBE
:
3520 case GLSL_SAMPLER_DIM_2D
:
3524 case GLSL_SAMPLER_DIM_1D
:
3529 for (unsigned i
= 0; i
< num_deriv_comp
; i
++) {
3530 derivs
[i
* 2] = to_float(ctx
, llvm_extract_elem(ctx
, ddx
, i
));
3531 derivs
[i
* 2 + 1] = to_float(ctx
, llvm_extract_elem(ctx
, ddy
, i
));
3535 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& coord
) {
3536 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
3537 coords
[chan
] = to_float(ctx
, coords
[chan
]);
3538 if (instr
->coord_components
== 3)
3539 coords
[3] = LLVMGetUndef(ctx
->f32
);
3540 ac_prepare_cube_coords(&ctx
->ac
,
3541 instr
->op
== nir_texop_txd
, instr
->is_array
,
3548 for (unsigned i
= 0; i
< num_deriv_comp
* 2; i
++)
3549 address
[count
++] = derivs
[i
];
3552 /* Pack texture coordinates */
3554 address
[count
++] = coords
[0];
3555 if (instr
->coord_components
> 1)
3556 address
[count
++] = coords
[1];
3557 if (instr
->coord_components
> 2) {
3558 /* This seems like a bit of a hack - but it passes Vulkan CTS with it */
3559 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_3D
&& instr
->op
!= nir_texop_txf
) {
3560 coords
[2] = to_float(ctx
, coords
[2]);
3561 coords
[2] = ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.rint.f32", ctx
->f32
, &coords
[2],
3563 coords
[2] = to_integer(ctx
, coords
[2]);
3565 address
[count
++] = coords
[2];
3570 if ((instr
->op
== nir_texop_txl
|| instr
->op
== nir_texop_txf
) && lod
) {
3571 address
[count
++] = lod
;
3572 } else if (instr
->op
== nir_texop_txf_ms
&& sample_index
) {
3573 address
[count
++] = sample_index
;
3574 } else if(instr
->op
== nir_texop_txs
) {
3577 address
[count
++] = lod
;
3579 address
[count
++] = ctx
->i32zero
;
3582 for (chan
= 0; chan
< count
; chan
++) {
3583 address
[chan
] = LLVMBuildBitCast(ctx
->builder
,
3584 address
[chan
], ctx
->i32
, "");
3587 if (instr
->op
== nir_texop_samples_identical
) {
3588 LLVMValueRef txf_address
[4];
3589 struct ac_tex_info txf_info
= { 0 };
3590 unsigned txf_count
= count
;
3591 memcpy(txf_address
, address
, sizeof(txf_address
));
3593 if (!instr
->is_array
)
3594 txf_address
[2] = ctx
->i32zero
;
3595 txf_address
[3] = ctx
->i32zero
;
3597 set_tex_fetch_args(ctx
, &txf_info
, instr
, nir_texop_txf
,
3599 txf_address
, txf_count
, 0xf);
3601 result
= build_tex_intrinsic(ctx
, instr
, &txf_info
);
3603 result
= LLVMBuildExtractElement(ctx
->builder
, result
, ctx
->i32zero
, "");
3604 result
= emit_int_cmp(ctx
, LLVMIntEQ
, result
, ctx
->i32zero
);
3608 /* Adjust the sample index according to FMASK.
3610 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3611 * which is the identity mapping. Each nibble says which physical sample
3612 * should be fetched to get that sample.
3614 * For example, 0x11111100 means there are only 2 samples stored and
3615 * the second sample covers 3/4 of the pixel. When reading samples 0
3616 * and 1, return physical sample 0 (determined by the first two 0s
3617 * in FMASK), otherwise return physical sample 1.
3619 * The sample index should be adjusted as follows:
3620 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
3622 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
) {
3623 LLVMValueRef txf_address
[4];
3624 struct ac_tex_info txf_info
= { 0 };
3625 unsigned txf_count
= count
;
3626 memcpy(txf_address
, address
, sizeof(txf_address
));
3628 if (!instr
->is_array
)
3629 txf_address
[2] = ctx
->i32zero
;
3630 txf_address
[3] = ctx
->i32zero
;
3632 set_tex_fetch_args(ctx
, &txf_info
, instr
, nir_texop_txf
,
3634 txf_address
, txf_count
, 0xf);
3636 result
= build_tex_intrinsic(ctx
, instr
, &txf_info
);
3637 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
3638 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
3640 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
3644 unsigned sample_chan
= instr
->is_array
? 3 : 2;
3646 LLVMValueRef sample_index4
=
3647 LLVMBuildMul(ctx
->builder
, address
[sample_chan
], four
, "");
3648 LLVMValueRef shifted_fmask
=
3649 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
3650 LLVMValueRef final_sample
=
3651 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
3653 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3654 * resource descriptor is 0 (invalid),
3656 LLVMValueRef fmask_desc
=
3657 LLVMBuildBitCast(ctx
->builder
, fmask_ptr
,
3660 LLVMValueRef fmask_word1
=
3661 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
3664 LLVMValueRef word1_is_nonzero
=
3665 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3666 fmask_word1
, ctx
->i32zero
, "");
3668 /* Replace the MSAA sample index. */
3669 address
[sample_chan
] =
3670 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
3671 final_sample
, address
[sample_chan
], "");
3674 if (offsets
&& instr
->op
== nir_texop_txf
) {
3675 nir_const_value
*const_offset
=
3676 nir_src_as_const_value(instr
->src
[const_src
].src
);
3677 int num_offsets
= instr
->src
[const_src
].src
.ssa
->num_components
;
3678 assert(const_offset
);
3679 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
3680 if (num_offsets
> 2)
3681 address
[2] = LLVMBuildAdd(ctx
->builder
,
3682 address
[2], LLVMConstInt(ctx
->i32
, const_offset
->i32
[2], false), "");
3683 if (num_offsets
> 1)
3684 address
[1] = LLVMBuildAdd(ctx
->builder
,
3685 address
[1], LLVMConstInt(ctx
->i32
, const_offset
->i32
[1], false), "");
3686 address
[0] = LLVMBuildAdd(ctx
->builder
,
3687 address
[0], LLVMConstInt(ctx
->i32
, const_offset
->i32
[0], false), "");
3691 /* TODO TG4 support */
3692 if (instr
->op
== nir_texop_tg4
) {
3693 if (instr
->is_shadow
)
3696 dmask
= 1 << instr
->component
;
3698 set_tex_fetch_args(ctx
, &tinfo
, instr
, instr
->op
,
3699 res_ptr
, samp_ptr
, address
, count
, dmask
);
3701 result
= build_tex_intrinsic(ctx
, instr
, &tinfo
);
3703 if (instr
->op
== nir_texop_query_levels
)
3704 result
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, 3, false), "");
3705 else if (instr
->is_shadow
&& instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&& instr
->op
!= nir_texop_tg4
)
3706 result
= LLVMBuildExtractElement(ctx
->builder
, result
, ctx
->i32zero
, "");
3707 else if (instr
->op
== nir_texop_txs
&&
3708 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
3710 LLVMValueRef two
= LLVMConstInt(ctx
->i32
, 2, false);
3711 LLVMValueRef six
= LLVMConstInt(ctx
->i32
, 6, false);
3712 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->builder
, result
, two
, "");
3713 z
= LLVMBuildSDiv(ctx
->builder
, z
, six
, "");
3714 result
= LLVMBuildInsertElement(ctx
->builder
, result
, z
, two
, "");
3715 } else if (instr
->dest
.ssa
.num_components
!= 4)
3716 result
= trim_vector(ctx
, result
, instr
->dest
.ssa
.num_components
);
3720 assert(instr
->dest
.is_ssa
);
3721 result
= to_integer(ctx
, result
);
3722 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
3727 static void visit_phi(struct nir_to_llvm_context
*ctx
, nir_phi_instr
*instr
)
3729 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
3730 LLVMValueRef result
= LLVMBuildPhi(ctx
->builder
, type
, "");
3732 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
3733 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
3736 static void visit_post_phi(struct nir_to_llvm_context
*ctx
,
3737 nir_phi_instr
*instr
,
3738 LLVMValueRef llvm_phi
)
3740 nir_foreach_phi_src(src
, instr
) {
3741 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
3742 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
3744 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
3748 static void phi_post_pass(struct nir_to_llvm_context
*ctx
)
3750 struct hash_entry
*entry
;
3751 hash_table_foreach(ctx
->phis
, entry
) {
3752 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
3753 (LLVMValueRef
)entry
->data
);
3758 static void visit_ssa_undef(struct nir_to_llvm_context
*ctx
,
3759 nir_ssa_undef_instr
*instr
)
3761 unsigned num_components
= instr
->def
.num_components
;
3764 if (num_components
== 1)
3765 undef
= LLVMGetUndef(ctx
->i32
);
3767 undef
= LLVMGetUndef(LLVMVectorType(ctx
->i32
, num_components
));
3769 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, undef
);
3772 static void visit_jump(struct nir_to_llvm_context
*ctx
,
3773 nir_jump_instr
*instr
)
3775 switch (instr
->type
) {
3776 case nir_jump_break
:
3777 LLVMBuildBr(ctx
->builder
, ctx
->break_block
);
3778 LLVMClearInsertionPosition(ctx
->builder
);
3780 case nir_jump_continue
:
3781 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
3782 LLVMClearInsertionPosition(ctx
->builder
);
3785 fprintf(stderr
, "Unknown NIR jump instr: ");
3786 nir_print_instr(&instr
->instr
, stderr
);
3787 fprintf(stderr
, "\n");
3792 static void visit_cf_list(struct nir_to_llvm_context
*ctx
,
3793 struct exec_list
*list
);
3795 static void visit_block(struct nir_to_llvm_context
*ctx
, nir_block
*block
)
3797 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->builder
);
3798 nir_foreach_instr(instr
, block
)
3800 switch (instr
->type
) {
3801 case nir_instr_type_alu
:
3802 visit_alu(ctx
, nir_instr_as_alu(instr
));
3804 case nir_instr_type_load_const
:
3805 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
3807 case nir_instr_type_intrinsic
:
3808 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
3810 case nir_instr_type_tex
:
3811 visit_tex(ctx
, nir_instr_as_tex(instr
));
3813 case nir_instr_type_phi
:
3814 visit_phi(ctx
, nir_instr_as_phi(instr
));
3816 case nir_instr_type_ssa_undef
:
3817 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
3819 case nir_instr_type_jump
:
3820 visit_jump(ctx
, nir_instr_as_jump(instr
));
3823 fprintf(stderr
, "Unknown NIR instr type: ");
3824 nir_print_instr(instr
, stderr
);
3825 fprintf(stderr
, "\n");
3830 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
3833 static void visit_if(struct nir_to_llvm_context
*ctx
, nir_if
*if_stmt
)
3835 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
3837 LLVMBasicBlockRef merge_block
=
3838 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
3839 LLVMBasicBlockRef if_block
=
3840 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
3841 LLVMBasicBlockRef else_block
= merge_block
;
3842 if (!exec_list_is_empty(&if_stmt
->else_list
))
3843 else_block
= LLVMAppendBasicBlockInContext(
3844 ctx
->context
, ctx
->main_function
, "");
3846 LLVMValueRef cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, value
,
3847 LLVMConstInt(ctx
->i32
, 0, false), "");
3848 LLVMBuildCondBr(ctx
->builder
, cond
, if_block
, else_block
);
3850 LLVMPositionBuilderAtEnd(ctx
->builder
, if_block
);
3851 visit_cf_list(ctx
, &if_stmt
->then_list
);
3852 if (LLVMGetInsertBlock(ctx
->builder
))
3853 LLVMBuildBr(ctx
->builder
, merge_block
);
3855 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
3856 LLVMPositionBuilderAtEnd(ctx
->builder
, else_block
);
3857 visit_cf_list(ctx
, &if_stmt
->else_list
);
3858 if (LLVMGetInsertBlock(ctx
->builder
))
3859 LLVMBuildBr(ctx
->builder
, merge_block
);
3862 LLVMPositionBuilderAtEnd(ctx
->builder
, merge_block
);
3865 static void visit_loop(struct nir_to_llvm_context
*ctx
, nir_loop
*loop
)
3867 LLVMBasicBlockRef continue_parent
= ctx
->continue_block
;
3868 LLVMBasicBlockRef break_parent
= ctx
->break_block
;
3870 ctx
->continue_block
=
3871 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
3873 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
3875 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
3876 LLVMPositionBuilderAtEnd(ctx
->builder
, ctx
->continue_block
);
3877 visit_cf_list(ctx
, &loop
->body
);
3879 if (LLVMGetInsertBlock(ctx
->builder
))
3880 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
3881 LLVMPositionBuilderAtEnd(ctx
->builder
, ctx
->break_block
);
3883 ctx
->continue_block
= continue_parent
;
3884 ctx
->break_block
= break_parent
;
3887 static void visit_cf_list(struct nir_to_llvm_context
*ctx
,
3888 struct exec_list
*list
)
3890 foreach_list_typed(nir_cf_node
, node
, node
, list
)
3892 switch (node
->type
) {
3893 case nir_cf_node_block
:
3894 visit_block(ctx
, nir_cf_node_as_block(node
));
3897 case nir_cf_node_if
:
3898 visit_if(ctx
, nir_cf_node_as_if(node
));
3901 case nir_cf_node_loop
:
3902 visit_loop(ctx
, nir_cf_node_as_loop(node
));
3912 handle_vs_input_decl(struct nir_to_llvm_context
*ctx
,
3913 struct nir_variable
*variable
)
3915 LLVMValueRef t_list_ptr
= ctx
->vertex_buffers
;
3916 LLVMValueRef t_offset
;
3917 LLVMValueRef t_list
;
3918 LLVMValueRef args
[3];
3920 LLVMValueRef buffer_index
;
3921 int index
= variable
->data
.location
- VERT_ATTRIB_GENERIC0
;
3922 int idx
= variable
->data
.location
;
3923 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, true);
3925 variable
->data
.driver_location
= idx
* 4;
3927 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << index
)) {
3928 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->instance_id
,
3929 ctx
->start_instance
, "");
3930 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
3931 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
3933 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->vertex_id
,
3934 ctx
->base_vertex
, "");
3936 for (unsigned i
= 0; i
< attrib_count
; ++i
, ++idx
) {
3937 t_offset
= LLVMConstInt(ctx
->i32
, index
+ i
, false);
3939 t_list
= build_indexed_load_const(ctx
, t_list_ptr
, t_offset
);
3941 args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
3942 args
[2] = buffer_index
;
3943 input
= ac_emit_llvm_intrinsic(&ctx
->ac
,
3944 "llvm.SI.vs.load.input", ctx
->v4f32
, args
, 3,
3945 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_NOUNWIND
);
3947 for (unsigned chan
= 0; chan
< 4; chan
++) {
3948 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
3949 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
, chan
)] =
3950 to_integer(ctx
, LLVMBuildExtractElement(ctx
->builder
,
3951 input
, llvm_chan
, ""));
3957 static void interp_fs_input(struct nir_to_llvm_context
*ctx
,
3959 LLVMValueRef interp_param
,
3960 LLVMValueRef prim_mask
,
3961 LLVMValueRef result
[4])
3963 const char *intr_name
;
3964 LLVMValueRef attr_number
;
3967 attr_number
= LLVMConstInt(ctx
->i32
, attr
, false);
3969 /* fs.constant returns the param from the middle vertex, so it's not
3970 * really useful for flat shading. It's meant to be used for custom
3971 * interpolation (but the intrinsic can't fetch from the other two
3974 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
3975 * to do the right thing. The only reason we use fs.constant is that
3976 * fs.interp cannot be used on integers, because they can be equal
3979 intr_name
= interp_param
? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
3981 for (chan
= 0; chan
< 4; chan
++) {
3982 LLVMValueRef args
[4];
3983 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
3985 args
[0] = llvm_chan
;
3986 args
[1] = attr_number
;
3987 args
[2] = prim_mask
;
3988 args
[3] = interp_param
;
3989 result
[chan
] = ac_emit_llvm_intrinsic(&ctx
->ac
, intr_name
,
3990 ctx
->f32
, args
, args
[3] ? 4 : 3,
3991 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_NOUNWIND
);
3996 handle_fs_input_decl(struct nir_to_llvm_context
*ctx
,
3997 struct nir_variable
*variable
)
3999 int idx
= variable
->data
.location
;
4000 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4001 LLVMValueRef interp
;
4003 variable
->data
.driver_location
= idx
* 4;
4004 ctx
->input_mask
|= ((1ull << attrib_count
) - 1) << variable
->data
.location
;
4006 if (glsl_get_base_type(glsl_without_array(variable
->type
)) == GLSL_TYPE_FLOAT
) {
4007 unsigned interp_type
;
4008 if (variable
->data
.sample
) {
4009 interp_type
= INTERP_SAMPLE
;
4010 ctx
->shader_info
->fs
.force_persample
= true;
4011 } else if (variable
->data
.centroid
)
4012 interp_type
= INTERP_CENTROID
;
4014 interp_type
= INTERP_CENTER
;
4016 interp
= lookup_interp_param(ctx
, variable
->data
.interpolation
, interp_type
);
4020 for (unsigned i
= 0; i
< attrib_count
; ++i
)
4021 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
+ i
, 0)] = interp
;
4026 handle_shader_input_decl(struct nir_to_llvm_context
*ctx
,
4027 struct nir_variable
*variable
)
4029 switch (ctx
->stage
) {
4030 case MESA_SHADER_VERTEX
:
4031 handle_vs_input_decl(ctx
, variable
);
4033 case MESA_SHADER_FRAGMENT
:
4034 handle_fs_input_decl(ctx
, variable
);
4043 handle_fs_inputs_pre(struct nir_to_llvm_context
*ctx
,
4044 struct nir_shader
*nir
)
4047 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_INPUTS
; ++i
) {
4048 LLVMValueRef interp_param
;
4049 LLVMValueRef
*inputs
= ctx
->inputs
+radeon_llvm_reg_index_soa(i
, 0);
4051 if (!(ctx
->input_mask
& (1ull << i
)))
4054 if (i
>= VARYING_SLOT_VAR0
|| i
== VARYING_SLOT_PNTC
) {
4055 interp_param
= *inputs
;
4056 interp_fs_input(ctx
, index
, interp_param
, ctx
->prim_mask
,
4060 ctx
->shader_info
->fs
.flat_shaded_mask
|= 1u << index
;
4062 } else if (i
== VARYING_SLOT_POS
) {
4063 for(int i
= 0; i
< 3; ++i
)
4064 inputs
[i
] = ctx
->frag_pos
[i
];
4066 inputs
[3] = ac_emit_fdiv(&ctx
->ac
, ctx
->f32one
, ctx
->frag_pos
[3]);
4069 ctx
->shader_info
->fs
.num_interp
= index
;
4070 if (ctx
->input_mask
& (1 << VARYING_SLOT_PNTC
))
4071 ctx
->shader_info
->fs
.has_pcoord
= true;
4072 ctx
->shader_info
->fs
.input_mask
= ctx
->input_mask
>> VARYING_SLOT_VAR0
;
4076 ac_build_alloca(struct nir_to_llvm_context
*ctx
,
4080 LLVMBuilderRef builder
= ctx
->builder
;
4081 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
4082 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
4083 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
4084 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
4085 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ctx
->context
);
4089 LLVMPositionBuilderBefore(first_builder
, first_instr
);
4091 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
4094 res
= LLVMBuildAlloca(first_builder
, type
, name
);
4095 LLVMBuildStore(builder
, LLVMConstNull(type
), res
);
4097 LLVMDisposeBuilder(first_builder
);
4102 static LLVMValueRef
si_build_alloca_undef(struct nir_to_llvm_context
*ctx
,
4106 LLVMValueRef ptr
= ac_build_alloca(ctx
, type
, name
);
4107 LLVMBuildStore(ctx
->builder
, LLVMGetUndef(type
), ptr
);
4112 handle_shader_output_decl(struct nir_to_llvm_context
*ctx
,
4113 struct nir_variable
*variable
)
4115 int idx
= variable
->data
.location
+ variable
->data
.index
;
4116 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4118 variable
->data
.driver_location
= idx
* 4;
4120 if (ctx
->stage
== MESA_SHADER_VERTEX
) {
4122 if (idx
== VARYING_SLOT_CLIP_DIST0
||
4123 idx
== VARYING_SLOT_CULL_DIST0
) {
4124 int length
= glsl_get_length(variable
->type
);
4125 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
4126 ctx
->shader_info
->vs
.clip_dist_mask
= (1 << length
) - 1;
4127 ctx
->num_clips
= length
;
4128 } else if (idx
== VARYING_SLOT_CULL_DIST0
) {
4129 ctx
->shader_info
->vs
.cull_dist_mask
= (1 << length
) - 1;
4130 ctx
->num_culls
= length
;
4139 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
4140 for (unsigned chan
= 0; chan
< 4; chan
++) {
4141 ctx
->outputs
[radeon_llvm_reg_index_soa(idx
+ i
, chan
)] =
4142 si_build_alloca_undef(ctx
, ctx
->f32
, "");
4145 ctx
->output_mask
|= ((1ull << attrib_count
) - 1) << idx
;
4149 setup_locals(struct nir_to_llvm_context
*ctx
,
4150 struct nir_function
*func
)
4153 ctx
->num_locals
= 0;
4154 nir_foreach_variable(variable
, &func
->impl
->locals
) {
4155 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4156 variable
->data
.driver_location
= ctx
->num_locals
* 4;
4157 ctx
->num_locals
+= attrib_count
;
4159 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
4163 for (i
= 0; i
< ctx
->num_locals
; i
++) {
4164 for (j
= 0; j
< 4; j
++) {
4165 ctx
->locals
[i
* 4 + j
] =
4166 si_build_alloca_undef(ctx
, ctx
->f32
, "temp");
4172 emit_float_saturate(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
, float lo
, float hi
)
4174 v
= to_float(ctx
, v
);
4175 v
= emit_intrin_2f_param(ctx
, "llvm.maxnum.f32", v
, LLVMConstReal(ctx
->f32
, lo
));
4176 return emit_intrin_2f_param(ctx
, "llvm.minnum.f32", v
, LLVMConstReal(ctx
->f32
, hi
));
4180 static LLVMValueRef
emit_pack_int16(struct nir_to_llvm_context
*ctx
,
4181 LLVMValueRef src0
, LLVMValueRef src1
)
4183 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
4184 LLVMValueRef comp
[2];
4186 comp
[0] = LLVMBuildAnd(ctx
->builder
, src0
, LLVMConstInt(ctx
-> i32
, 65535, 0), "");
4187 comp
[1] = LLVMBuildAnd(ctx
->builder
, src1
, LLVMConstInt(ctx
-> i32
, 65535, 0), "");
4188 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
4189 return LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
4192 /* Initialize arguments for the shader export intrinsic */
4194 si_llvm_init_export_args(struct nir_to_llvm_context
*ctx
,
4195 LLVMValueRef
*values
,
4199 /* Default is 0xf. Adjusted below depending on the format. */
4200 args
[0] = LLVMConstInt(ctx
->i32
, target
!= V_008DFC_SQ_EXP_NULL
? 0xf : 0, false);
4201 /* Specify whether the EXEC mask represents the valid mask */
4202 args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
4204 /* Specify whether this is the last export */
4205 args
[2] = LLVMConstInt(ctx
->i32
, 0, false);
4206 /* Specify the target we are exporting */
4207 args
[3] = LLVMConstInt(ctx
->i32
, target
, false);
4209 args
[4] = LLVMConstInt(ctx
->i32
, 0, false); /* COMPR flag */
4210 args
[5] = LLVMGetUndef(ctx
->f32
);
4211 args
[6] = LLVMGetUndef(ctx
->f32
);
4212 args
[7] = LLVMGetUndef(ctx
->f32
);
4213 args
[8] = LLVMGetUndef(ctx
->f32
);
4218 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&& target
>= V_008DFC_SQ_EXP_MRT
) {
4219 LLVMValueRef val
[4];
4220 unsigned index
= target
- V_008DFC_SQ_EXP_MRT
;
4221 unsigned col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * index
)) & 0xf;
4222 bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> index
) & 1;
4224 switch(col_format
) {
4225 case V_028714_SPI_SHADER_ZERO
:
4226 args
[0] = LLVMConstInt(ctx
->i32
, 0x0, 0);
4227 args
[3] = LLVMConstInt(ctx
->i32
, V_008DFC_SQ_EXP_NULL
, 0);
4230 case V_028714_SPI_SHADER_32_R
:
4231 args
[0] = LLVMConstInt(ctx
->i32
, 0x1, 0);
4232 args
[5] = values
[0];
4235 case V_028714_SPI_SHADER_32_GR
:
4236 args
[0] = LLVMConstInt(ctx
->i32
, 0x3, 0);
4237 args
[5] = values
[0];
4238 args
[6] = values
[1];
4241 case V_028714_SPI_SHADER_32_AR
:
4242 args
[0] = LLVMConstInt(ctx
->i32
, 0x9, 0);
4243 args
[5] = values
[0];
4244 args
[8] = values
[3];
4247 case V_028714_SPI_SHADER_FP16_ABGR
:
4248 args
[4] = ctx
->i32one
;
4250 for (unsigned chan
= 0; chan
< 2; chan
++) {
4251 LLVMValueRef pack_args
[2] = {
4253 values
[2 * chan
+ 1]
4255 LLVMValueRef packed
;
4257 packed
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.SI.packf16",
4258 ctx
->i32
, pack_args
, 2,
4259 AC_FUNC_ATTR_READNONE
);
4260 args
[chan
+ 5] = packed
;
4264 case V_028714_SPI_SHADER_UNORM16_ABGR
:
4265 for (unsigned chan
= 0; chan
< 4; chan
++) {
4266 val
[chan
] = emit_float_saturate(ctx
, values
[chan
], 0, 1);
4267 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
4268 LLVMConstReal(ctx
->f32
, 65535), "");
4269 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
4270 LLVMConstReal(ctx
->f32
, 0.5), "");
4271 val
[chan
] = LLVMBuildFPToUI(ctx
->builder
, val
[chan
],
4275 args
[4] = ctx
->i32one
;
4276 args
[5] = emit_pack_int16(ctx
, val
[0], val
[1]);
4277 args
[6] = emit_pack_int16(ctx
, val
[2], val
[3]);
4280 case V_028714_SPI_SHADER_SNORM16_ABGR
:
4281 for (unsigned chan
= 0; chan
< 4; chan
++) {
4282 val
[chan
] = emit_float_saturate(ctx
, values
[chan
], -1, 1);
4283 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
4284 LLVMConstReal(ctx
->f32
, 32767), "");
4286 /* If positive, add 0.5, else add -0.5. */
4287 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
4288 LLVMBuildSelect(ctx
->builder
,
4289 LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
,
4290 val
[chan
], ctx
->f32zero
, ""),
4291 LLVMConstReal(ctx
->f32
, 0.5),
4292 LLVMConstReal(ctx
->f32
, -0.5), ""), "");
4293 val
[chan
] = LLVMBuildFPToSI(ctx
->builder
, val
[chan
], ctx
->i32
, "");
4296 args
[4] = ctx
->i32one
;
4297 args
[5] = emit_pack_int16(ctx
, val
[0], val
[1]);
4298 args
[6] = emit_pack_int16(ctx
, val
[2], val
[3]);
4301 case V_028714_SPI_SHADER_UINT16_ABGR
: {
4302 LLVMValueRef max
= LLVMConstInt(ctx
->i32
, is_int8
? 255 : 65535, 0);
4304 for (unsigned chan
= 0; chan
< 4; chan
++) {
4305 val
[chan
] = to_integer(ctx
, values
[chan
]);
4306 val
[chan
] = emit_minmax_int(ctx
, LLVMIntULT
, val
[chan
], max
);
4309 args
[4] = ctx
->i32one
;
4310 args
[5] = emit_pack_int16(ctx
, val
[0], val
[1]);
4311 args
[6] = emit_pack_int16(ctx
, val
[2], val
[3]);
4315 case V_028714_SPI_SHADER_SINT16_ABGR
: {
4316 LLVMValueRef max
= LLVMConstInt(ctx
->i32
, is_int8
? 127 : 32767, 0);
4317 LLVMValueRef min
= LLVMConstInt(ctx
->i32
, is_int8
? -128 : -32768, 0);
4320 for (unsigned chan
= 0; chan
< 4; chan
++) {
4321 val
[chan
] = to_integer(ctx
, values
[chan
]);
4322 val
[chan
] = emit_minmax_int(ctx
, LLVMIntSLT
, val
[chan
], max
);
4323 val
[chan
] = emit_minmax_int(ctx
, LLVMIntSGT
, val
[chan
], min
);
4326 args
[4] = ctx
->i32one
;
4327 args
[5] = emit_pack_int16(ctx
, val
[0], val
[1]);
4328 args
[6] = emit_pack_int16(ctx
, val
[2], val
[3]);
4333 case V_028714_SPI_SHADER_32_ABGR
:
4334 memcpy(&args
[5], values
, sizeof(values
[0]) * 4);
4338 memcpy(&args
[5], values
, sizeof(values
[0]) * 4);
4340 for (unsigned i
= 5; i
< 9; ++i
)
4341 args
[i
] = to_float(ctx
, args
[i
]);
4345 handle_vs_outputs_post(struct nir_to_llvm_context
*ctx
)
4347 uint32_t param_count
= 0;
4349 unsigned pos_idx
, num_pos_exports
= 0;
4350 LLVMValueRef args
[9];
4351 LLVMValueRef pos_args
[4][9] = { { 0 } };
4352 LLVMValueRef psize_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
4354 const uint64_t clip_mask
= ctx
->output_mask
& ((1ull << VARYING_SLOT_CLIP_DIST0
) |
4355 (1ull << VARYING_SLOT_CLIP_DIST1
) |
4356 (1ull << VARYING_SLOT_CULL_DIST0
) |
4357 (1ull << VARYING_SLOT_CULL_DIST1
));
4360 LLVMValueRef slots
[8];
4363 if (ctx
->shader_info
->vs
.cull_dist_mask
)
4364 ctx
->shader_info
->vs
.cull_dist_mask
<<= ctx
->num_clips
;
4366 i
= VARYING_SLOT_CLIP_DIST0
;
4367 for (j
= 0; j
< ctx
->num_clips
; j
++)
4368 slots
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4369 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4370 i
= VARYING_SLOT_CULL_DIST0
;
4371 for (j
= 0; j
< ctx
->num_culls
; j
++)
4372 slots
[ctx
->num_clips
+ j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4373 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4375 for (i
= ctx
->num_clips
+ ctx
->num_culls
; i
< 8; i
++)
4376 slots
[i
] = LLVMGetUndef(ctx
->f32
);
4378 if (ctx
->num_clips
+ ctx
->num_culls
> 4) {
4379 target
= V_008DFC_SQ_EXP_POS
+ 3;
4380 si_llvm_init_export_args(ctx
, &slots
[4], target
, args
);
4381 memcpy(pos_args
[target
- V_008DFC_SQ_EXP_POS
],
4382 args
, sizeof(args
));
4385 target
= V_008DFC_SQ_EXP_POS
+ 2;
4386 si_llvm_init_export_args(ctx
, &slots
[0], target
, args
);
4387 memcpy(pos_args
[target
- V_008DFC_SQ_EXP_POS
],
4388 args
, sizeof(args
));
4392 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
4393 LLVMValueRef values
[4];
4394 if (!(ctx
->output_mask
& (1ull << i
)))
4397 for (unsigned j
= 0; j
< 4; j
++)
4398 values
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4399 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4401 if (i
== VARYING_SLOT_POS
) {
4402 target
= V_008DFC_SQ_EXP_POS
;
4403 } else if (i
== VARYING_SLOT_CLIP_DIST0
||
4404 i
== VARYING_SLOT_CLIP_DIST1
||
4405 i
== VARYING_SLOT_CULL_DIST0
||
4406 i
== VARYING_SLOT_CULL_DIST1
) {
4408 } else if (i
== VARYING_SLOT_PSIZ
) {
4409 ctx
->shader_info
->vs
.writes_pointsize
= true;
4410 psize_value
= values
[0];
4412 } else if (i
== VARYING_SLOT_LAYER
) {
4413 ctx
->shader_info
->vs
.writes_layer
= true;
4414 layer_value
= values
[0];
4416 } else if (i
== VARYING_SLOT_VIEWPORT
) {
4417 ctx
->shader_info
->vs
.writes_viewport_index
= true;
4418 viewport_index_value
= values
[0];
4420 } else if (i
>= VARYING_SLOT_VAR0
) {
4421 ctx
->shader_info
->vs
.export_mask
|= 1u << (i
- VARYING_SLOT_VAR0
);
4422 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
4426 si_llvm_init_export_args(ctx
, values
, target
, args
);
4428 if (target
>= V_008DFC_SQ_EXP_POS
&&
4429 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
4430 memcpy(pos_args
[target
- V_008DFC_SQ_EXP_POS
],
4431 args
, sizeof(args
));
4433 ac_emit_llvm_intrinsic(&ctx
->ac
,
4440 /* We need to add the position output manually if it's missing. */
4441 if (!pos_args
[0][0]) {
4442 pos_args
[0][0] = LLVMConstInt(ctx
->i32
, 0xf, false);
4443 pos_args
[0][1] = ctx
->i32zero
; /* EXEC mask */
4444 pos_args
[0][2] = ctx
->i32zero
; /* last export? */
4445 pos_args
[0][3] = LLVMConstInt(ctx
->i32
, V_008DFC_SQ_EXP_POS
, false);
4446 pos_args
[0][4] = ctx
->i32zero
; /* COMPR flag */
4447 pos_args
[0][5] = ctx
->f32zero
; /* X */
4448 pos_args
[0][6] = ctx
->f32zero
; /* Y */
4449 pos_args
[0][7] = ctx
->f32zero
; /* Z */
4450 pos_args
[0][8] = ctx
->f32one
; /* W */
4453 uint32_t mask
= ((ctx
->shader_info
->vs
.writes_pointsize
== true ? 1 : 0) |
4454 (ctx
->shader_info
->vs
.writes_layer
== true ? 4 : 0) |
4455 (ctx
->shader_info
->vs
.writes_viewport_index
== true ? 8 : 0));
4457 pos_args
[1][0] = LLVMConstInt(ctx
->i32
, mask
, false); /* writemask */
4458 pos_args
[1][1] = ctx
->i32zero
; /* EXEC mask */
4459 pos_args
[1][2] = ctx
->i32zero
; /* last export? */
4460 pos_args
[1][3] = LLVMConstInt(ctx
->i32
, V_008DFC_SQ_EXP_POS
+ 1, false);
4461 pos_args
[1][4] = ctx
->i32zero
; /* COMPR flag */
4462 pos_args
[1][5] = ctx
->f32zero
; /* X */
4463 pos_args
[1][6] = ctx
->f32zero
; /* Y */
4464 pos_args
[1][7] = ctx
->f32zero
; /* Z */
4465 pos_args
[1][8] = ctx
->f32zero
; /* W */
4467 if (ctx
->shader_info
->vs
.writes_pointsize
== true)
4468 pos_args
[1][5] = psize_value
;
4469 if (ctx
->shader_info
->vs
.writes_layer
== true)
4470 pos_args
[1][7] = layer_value
;
4471 if (ctx
->shader_info
->vs
.writes_viewport_index
== true)
4472 pos_args
[1][8] = viewport_index_value
;
4474 for (i
= 0; i
< 4; i
++) {
4480 for (i
= 0; i
< 4; i
++) {
4481 if (!pos_args
[i
][0])
4484 /* Specify the target we are exporting */
4485 pos_args
[i
][3] = LLVMConstInt(ctx
->i32
, V_008DFC_SQ_EXP_POS
+ pos_idx
++, false);
4486 if (pos_idx
== num_pos_exports
)
4487 pos_args
[i
][2] = ctx
->i32one
;
4488 ac_emit_llvm_intrinsic(&ctx
->ac
,
4494 ctx
->shader_info
->vs
.pos_exports
= num_pos_exports
;
4495 ctx
->shader_info
->vs
.param_exports
= param_count
;
4499 si_export_mrt_color(struct nir_to_llvm_context
*ctx
,
4500 LLVMValueRef
*color
, unsigned param
, bool is_last
)
4502 LLVMValueRef args
[9];
4504 si_llvm_init_export_args(ctx
, color
, param
,
4508 args
[1] = ctx
->i32one
; /* whether the EXEC mask is valid */
4509 args
[2] = ctx
->i32one
; /* DONE bit */
4510 } else if (args
[0] == ctx
->i32zero
)
4511 return; /* unnecessary NULL export */
4513 ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.SI.export",
4514 ctx
->voidt
, args
, 9, 0);
4518 si_export_mrt_z(struct nir_to_llvm_context
*ctx
,
4519 LLVMValueRef depth
, LLVMValueRef stencil
,
4520 LLVMValueRef samplemask
)
4522 LLVMValueRef args
[9];
4524 args
[1] = ctx
->i32one
; /* whether the EXEC mask is valid */
4525 args
[2] = ctx
->i32one
; /* DONE bit */
4526 /* Specify the target we are exporting */
4527 args
[3] = LLVMConstInt(ctx
->i32
, V_008DFC_SQ_EXP_MRTZ
, false);
4529 args
[4] = ctx
->i32zero
; /* COMP flag */
4530 args
[5] = LLVMGetUndef(ctx
->f32
); /* R, depth */
4531 args
[6] = LLVMGetUndef(ctx
->f32
); /* G, stencil test val[0:7], stencil op val[8:15] */
4532 args
[7] = LLVMGetUndef(ctx
->f32
); /* B, sample mask */
4533 args
[8] = LLVMGetUndef(ctx
->f32
); /* A, alpha to mask */
4546 args
[7] = samplemask
;
4550 /* SI (except OLAND) has a bug that it only looks
4551 * at the X writemask component. */
4552 if (ctx
->options
->chip_class
== SI
&&
4553 ctx
->options
->family
!= CHIP_OLAND
)
4556 args
[0] = LLVMConstInt(ctx
->i32
, mask
, false);
4557 ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.SI.export",
4558 ctx
->voidt
, args
, 9, 0);
4562 handle_fs_outputs_post(struct nir_to_llvm_context
*ctx
)
4565 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
4567 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
4568 LLVMValueRef values
[4];
4570 if (!(ctx
->output_mask
& (1ull << i
)))
4573 if (i
== FRAG_RESULT_DEPTH
) {
4574 ctx
->shader_info
->fs
.writes_z
= true;
4575 depth
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4576 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
4577 } else if (i
== FRAG_RESULT_STENCIL
) {
4578 ctx
->shader_info
->fs
.writes_stencil
= true;
4579 stencil
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4580 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
4583 for (unsigned j
= 0; j
< 4; j
++)
4584 values
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4585 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4587 if (!ctx
->shader_info
->fs
.writes_z
&& !ctx
->shader_info
->fs
.writes_stencil
)
4588 last
= ctx
->output_mask
<= ((1ull << (i
+ 1)) - 1);
4590 si_export_mrt_color(ctx
, values
, V_008DFC_SQ_EXP_MRT
+ index
, last
);
4595 if (depth
|| stencil
)
4596 si_export_mrt_z(ctx
, depth
, stencil
, samplemask
);
4598 si_export_mrt_color(ctx
, NULL
, V_008DFC_SQ_EXP_NULL
, true);
4600 ctx
->shader_info
->fs
.output_mask
= index
? ((1ull << index
) - 1) : 0;
4604 emit_gs_epilogue(struct nir_to_llvm_context
*ctx
)
4606 LLVMValueRef args
[2];
4608 args
[0] = LLVMConstInt(ctx
->i32
, SENDMSG_GS_OP_NOP
| SENDMSG_GS_DONE
, false);
4609 args
[1] = ctx
->gs_wave_id
;
4610 ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.SI.sendmsg",
4611 ctx
->voidt
, args
, 2, 0);
4615 handle_shader_outputs_post(struct nir_to_llvm_context
*ctx
)
4617 switch (ctx
->stage
) {
4618 case MESA_SHADER_VERTEX
:
4619 handle_vs_outputs_post(ctx
);
4621 case MESA_SHADER_FRAGMENT
:
4622 handle_fs_outputs_post(ctx
);
4624 case MESA_SHADER_GEOMETRY
:
4625 emit_gs_epilogue(ctx
);
4633 handle_shared_compute_var(struct nir_to_llvm_context
*ctx
,
4634 struct nir_variable
*variable
, uint32_t *offset
, int idx
)
4636 unsigned size
= glsl_count_attribute_slots(variable
->type
, false);
4637 variable
->data
.driver_location
= *offset
;
4641 static void ac_llvm_finalize_module(struct nir_to_llvm_context
* ctx
)
4643 LLVMPassManagerRef passmgr
;
4644 /* Create the pass manager */
4645 passmgr
= LLVMCreateFunctionPassManagerForModule(
4648 /* This pass should eliminate all the load and store instructions */
4649 LLVMAddPromoteMemoryToRegisterPass(passmgr
);
4651 /* Add some optimization passes */
4652 LLVMAddScalarReplAggregatesPass(passmgr
);
4653 LLVMAddLICMPass(passmgr
);
4654 LLVMAddAggressiveDCEPass(passmgr
);
4655 LLVMAddCFGSimplificationPass(passmgr
);
4656 LLVMAddInstructionCombiningPass(passmgr
);
4659 LLVMInitializeFunctionPassManager(passmgr
);
4660 LLVMRunFunctionPassManager(passmgr
, ctx
->main_function
);
4661 LLVMFinalizeFunctionPassManager(passmgr
);
4663 LLVMDisposeBuilder(ctx
->builder
);
4664 LLVMDisposePassManager(passmgr
);
4668 ac_setup_rings(struct nir_to_llvm_context
*ctx
)
4670 if (ctx
->stage
== MESA_SHADER_VERTEX
&& ctx
->options
->key
.vs
.as_es
) {
4671 ctx
->esgs_ring
= build_indexed_load_const(ctx
, ctx
->ring_offsets
, ctx
->i32one
);
4674 if (ctx
->is_gs_copy_shader
) {
4675 ctx
->gsvs_ring
= build_indexed_load_const(ctx
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, 3, false));
4677 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
4679 ctx
->esgs_ring
= build_indexed_load_const(ctx
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, 2, false));
4680 ctx
->gsvs_ring
= build_indexed_load_const(ctx
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, 4, false));
4682 ctx
->gsvs_ring
= LLVMBuildBitCast(ctx
->builder
, ctx
->gsvs_ring
, ctx
->v4i32
, "");
4684 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->gsvs_num_entries
, LLVMConstInt(ctx
->i32
, 2, false), "");
4685 tmp
= LLVMBuildExtractElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->i32one
, "");
4686 tmp
= LLVMBuildOr(ctx
->builder
, tmp
, ctx
->gsvs_ring_stride
, "");
4687 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, tmp
, ctx
->i32one
, "");
4689 ctx
->gsvs_ring
= LLVMBuildBitCast(ctx
->builder
, ctx
->gsvs_ring
, ctx
->v16i8
, "");
4694 LLVMModuleRef
ac_translate_nir_to_llvm(LLVMTargetMachineRef tm
,
4695 struct nir_shader
*nir
,
4696 struct ac_shader_variant_info
*shader_info
,
4697 const struct ac_nir_compiler_options
*options
)
4699 struct nir_to_llvm_context ctx
= {0};
4700 struct nir_function
*func
;
4702 ctx
.options
= options
;
4703 ctx
.shader_info
= shader_info
;
4704 ctx
.context
= LLVMContextCreate();
4705 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
4707 ac_llvm_context_init(&ctx
.ac
, ctx
.context
);
4708 ctx
.ac
.module
= ctx
.module
;
4710 ctx
.has_ds_bpermute
= ctx
.options
->chip_class
>= VI
;
4712 memset(shader_info
, 0, sizeof(*shader_info
));
4714 LLVMSetTarget(ctx
.module
, options
->supports_spill
? "amdgcn-mesa-mesa3d" : "amdgcn--");
4717 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
4718 ctx
.ac
.builder
= ctx
.builder
;
4719 ctx
.stage
= nir
->stage
;
4721 for (i
= 0; i
< AC_UD_MAX_SETS
; i
++)
4722 shader_info
->user_sgprs_locs
.descriptor_sets
[i
].sgpr_idx
= -1;
4723 for (i
= 0; i
< AC_UD_MAX_UD
; i
++)
4724 shader_info
->user_sgprs_locs
.shader_data
[i
].sgpr_idx
= -1;
4726 create_function(&ctx
);
4728 if (nir
->stage
== MESA_SHADER_COMPUTE
) {
4730 nir_foreach_variable(variable
, &nir
->shared
)
4734 uint32_t shared_size
= 0;
4736 LLVMTypeRef i8p
= LLVMPointerType(ctx
.i8
, LOCAL_ADDR_SPACE
);
4737 nir_foreach_variable(variable
, &nir
->shared
) {
4738 handle_shared_compute_var(&ctx
, variable
, &shared_size
, idx
);
4743 var
= LLVMAddGlobalInAddressSpace(ctx
.module
,
4744 LLVMArrayType(ctx
.i8
, shared_size
),
4747 LLVMSetAlignment(var
, 4);
4748 ctx
.shared_memory
= LLVMBuildBitCast(ctx
.builder
, var
, i8p
, "");
4750 } else if (nir
->stage
== MESA_SHADER_GEOMETRY
) {
4751 ctx
.gs_next_vertex
= ac_build_alloca(&ctx
, ctx
.i32
, "gs_next_vertex");
4753 ctx
.gs_max_out_vertices
= nir
->info
->gs
.vertices_out
;
4756 ac_setup_rings(&ctx
);
4758 nir_foreach_variable(variable
, &nir
->inputs
)
4759 handle_shader_input_decl(&ctx
, variable
);
4761 if (nir
->stage
== MESA_SHADER_FRAGMENT
)
4762 handle_fs_inputs_pre(&ctx
, nir
);
4764 nir_foreach_variable(variable
, &nir
->outputs
)
4765 handle_shader_output_decl(&ctx
, variable
);
4767 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4768 _mesa_key_pointer_equal
);
4769 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4770 _mesa_key_pointer_equal
);
4772 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
4774 setup_locals(&ctx
, func
);
4776 visit_cf_list(&ctx
, &func
->impl
->body
);
4777 phi_post_pass(&ctx
);
4779 handle_shader_outputs_post(&ctx
);
4780 LLVMBuildRetVoid(ctx
.builder
);
4782 ac_llvm_finalize_module(&ctx
);
4784 ralloc_free(ctx
.defs
);
4785 ralloc_free(ctx
.phis
);
4787 if (nir
->stage
== MESA_SHADER_GEOMETRY
) {
4788 shader_info
->gs
.gsvs_vertex_size
= util_bitcount64(ctx
.output_mask
) * 16;
4789 shader_info
->gs
.max_gsvs_emit_size
= shader_info
->gs
.gsvs_vertex_size
*
4790 nir
->info
->gs
.vertices_out
;
4795 static void ac_diagnostic_handler(LLVMDiagnosticInfoRef di
, void *context
)
4797 unsigned *retval
= (unsigned *)context
;
4798 LLVMDiagnosticSeverity severity
= LLVMGetDiagInfoSeverity(di
);
4799 char *description
= LLVMGetDiagInfoDescription(di
);
4801 if (severity
== LLVMDSError
) {
4803 fprintf(stderr
, "LLVM triggered Diagnostic Handler: %s\n",
4807 LLVMDisposeMessage(description
);
4810 static unsigned ac_llvm_compile(LLVMModuleRef M
,
4811 struct ac_shader_binary
*binary
,
4812 LLVMTargetMachineRef tm
)
4814 unsigned retval
= 0;
4816 LLVMContextRef llvm_ctx
;
4817 LLVMMemoryBufferRef out_buffer
;
4818 unsigned buffer_size
;
4819 const char *buffer_data
;
4822 /* Setup Diagnostic Handler*/
4823 llvm_ctx
= LLVMGetModuleContext(M
);
4825 LLVMContextSetDiagnosticHandler(llvm_ctx
, ac_diagnostic_handler
,
4829 mem_err
= LLVMTargetMachineEmitToMemoryBuffer(tm
, M
, LLVMObjectFile
,
4832 /* Process Errors/Warnings */
4834 fprintf(stderr
, "%s: %s", __FUNCTION__
, err
);
4840 /* Extract Shader Code*/
4841 buffer_size
= LLVMGetBufferSize(out_buffer
);
4842 buffer_data
= LLVMGetBufferStart(out_buffer
);
4844 ac_elf_read(buffer_data
, buffer_size
, binary
);
4847 LLVMDisposeMemoryBuffer(out_buffer
);
4853 static void ac_compile_llvm_module(LLVMTargetMachineRef tm
,
4854 LLVMModuleRef llvm_module
,
4855 struct ac_shader_binary
*binary
,
4856 struct ac_shader_config
*config
,
4857 struct ac_shader_variant_info
*shader_info
,
4858 gl_shader_stage stage
,
4859 bool dump_shader
, bool supports_spill
)
4862 ac_dump_module(llvm_module
);
4864 memset(binary
, 0, sizeof(*binary
));
4865 int v
= ac_llvm_compile(llvm_module
, binary
, tm
);
4867 fprintf(stderr
, "compile failed\n");
4871 fprintf(stderr
, "disasm:\n%s\n", binary
->disasm_string
);
4873 ac_shader_binary_read_config(binary
, config
, 0, supports_spill
);
4875 LLVMContextRef ctx
= LLVMGetModuleContext(llvm_module
);
4876 LLVMDisposeModule(llvm_module
);
4877 LLVMContextDispose(ctx
);
4879 if (stage
== MESA_SHADER_FRAGMENT
) {
4880 shader_info
->num_input_vgprs
= 0;
4881 if (G_0286CC_PERSP_SAMPLE_ENA(config
->spi_ps_input_addr
))
4882 shader_info
->num_input_vgprs
+= 2;
4883 if (G_0286CC_PERSP_CENTER_ENA(config
->spi_ps_input_addr
))
4884 shader_info
->num_input_vgprs
+= 2;
4885 if (G_0286CC_PERSP_CENTROID_ENA(config
->spi_ps_input_addr
))
4886 shader_info
->num_input_vgprs
+= 2;
4887 if (G_0286CC_PERSP_PULL_MODEL_ENA(config
->spi_ps_input_addr
))
4888 shader_info
->num_input_vgprs
+= 3;
4889 if (G_0286CC_LINEAR_SAMPLE_ENA(config
->spi_ps_input_addr
))
4890 shader_info
->num_input_vgprs
+= 2;
4891 if (G_0286CC_LINEAR_CENTER_ENA(config
->spi_ps_input_addr
))
4892 shader_info
->num_input_vgprs
+= 2;
4893 if (G_0286CC_LINEAR_CENTROID_ENA(config
->spi_ps_input_addr
))
4894 shader_info
->num_input_vgprs
+= 2;
4895 if (G_0286CC_LINE_STIPPLE_TEX_ENA(config
->spi_ps_input_addr
))
4896 shader_info
->num_input_vgprs
+= 1;
4897 if (G_0286CC_POS_X_FLOAT_ENA(config
->spi_ps_input_addr
))
4898 shader_info
->num_input_vgprs
+= 1;
4899 if (G_0286CC_POS_Y_FLOAT_ENA(config
->spi_ps_input_addr
))
4900 shader_info
->num_input_vgprs
+= 1;
4901 if (G_0286CC_POS_Z_FLOAT_ENA(config
->spi_ps_input_addr
))
4902 shader_info
->num_input_vgprs
+= 1;
4903 if (G_0286CC_POS_W_FLOAT_ENA(config
->spi_ps_input_addr
))
4904 shader_info
->num_input_vgprs
+= 1;
4905 if (G_0286CC_FRONT_FACE_ENA(config
->spi_ps_input_addr
))
4906 shader_info
->num_input_vgprs
+= 1;
4907 if (G_0286CC_ANCILLARY_ENA(config
->spi_ps_input_addr
))
4908 shader_info
->num_input_vgprs
+= 1;
4909 if (G_0286CC_SAMPLE_COVERAGE_ENA(config
->spi_ps_input_addr
))
4910 shader_info
->num_input_vgprs
+= 1;
4911 if (G_0286CC_POS_FIXED_PT_ENA(config
->spi_ps_input_addr
))
4912 shader_info
->num_input_vgprs
+= 1;
4914 config
->num_vgprs
= MAX2(config
->num_vgprs
, shader_info
->num_input_vgprs
);
4916 /* +3 for scratch wave offset and VCC */
4917 config
->num_sgprs
= MAX2(config
->num_sgprs
,
4918 shader_info
->num_input_sgprs
+ 3);
4921 void ac_compile_nir_shader(LLVMTargetMachineRef tm
,
4922 struct ac_shader_binary
*binary
,
4923 struct ac_shader_config
*config
,
4924 struct ac_shader_variant_info
*shader_info
,
4925 struct nir_shader
*nir
,
4926 const struct ac_nir_compiler_options
*options
,
4930 LLVMModuleRef llvm_module
= ac_translate_nir_to_llvm(tm
, nir
, shader_info
,
4933 ac_compile_llvm_module(tm
, llvm_module
, binary
, config
, shader_info
, nir
->stage
, dump_shader
, options
->supports_spill
);
4934 switch (nir
->stage
) {
4935 case MESA_SHADER_COMPUTE
:
4936 for (int i
= 0; i
< 3; ++i
)
4937 shader_info
->cs
.block_size
[i
] = nir
->info
->cs
.local_size
[i
];
4939 case MESA_SHADER_FRAGMENT
:
4940 shader_info
->fs
.early_fragment_test
= nir
->info
->fs
.early_fragment_tests
;
4942 case MESA_SHADER_GEOMETRY
:
4943 shader_info
->gs
.vertices_in
= nir
->info
->gs
.vertices_in
;
4944 shader_info
->gs
.vertices_out
= nir
->info
->gs
.vertices_out
;
4945 shader_info
->gs
.output_prim
= nir
->info
->gs
.output_primitive
;
4946 shader_info
->gs
.invocations
= nir
->info
->gs
.invocations
;
4948 case MESA_SHADER_VERTEX
:
4949 shader_info
->vs
.as_es
= options
->key
.vs
.as_es
;
4957 ac_gs_copy_shader_emit(struct nir_to_llvm_context
*ctx
)
4959 LLVMValueRef args
[9];
4960 args
[0] = ctx
->gsvs_ring
;
4961 args
[1] = LLVMBuildMul(ctx
->builder
, ctx
->vertex_id
, LLVMConstInt(ctx
->i32
, 4, false), "");
4962 args
[3] = ctx
->i32zero
;
4963 args
[4] = ctx
->i32one
; /* OFFEN */
4964 args
[5] = ctx
->i32zero
; /* IDXEN */
4965 args
[6] = ctx
->i32one
; /* GLC */
4966 args
[7] = ctx
->i32one
; /* SLC */
4967 args
[8] = ctx
->i32zero
; /* TFE */
4970 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
4971 if (!(ctx
->output_mask
& (1ull << i
)))
4974 for (unsigned j
= 0; j
< 4; j
++) {
4976 args
[2] = LLVMConstInt(ctx
->i32
,
4978 ctx
->gs_max_out_vertices
* 16 * 4, false);
4980 value
= ac_emit_llvm_intrinsic(&ctx
->ac
,
4981 "llvm.SI.buffer.load.dword.i32.i32",
4983 AC_FUNC_ATTR_READONLY
);
4985 LLVMBuildStore(ctx
->builder
,
4986 to_float(ctx
, value
), ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)]);
4990 handle_vs_outputs_post(ctx
);
4993 void ac_create_gs_copy_shader(LLVMTargetMachineRef tm
,
4994 struct nir_shader
*geom_shader
,
4995 struct ac_shader_binary
*binary
,
4996 struct ac_shader_config
*config
,
4997 struct ac_shader_variant_info
*shader_info
,
4998 const struct ac_nir_compiler_options
*options
,
5001 struct nir_to_llvm_context ctx
= {0};
5002 ctx
.context
= LLVMContextCreate();
5003 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
5004 ctx
.options
= options
;
5005 ctx
.shader_info
= shader_info
;
5007 ac_llvm_context_init(&ctx
.ac
, ctx
.context
);
5008 ctx
.ac
.module
= ctx
.module
;
5010 ctx
.is_gs_copy_shader
= true;
5011 LLVMSetTarget(ctx
.module
, "amdgcn--");
5014 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
5015 ctx
.ac
.builder
= ctx
.builder
;
5016 ctx
.stage
= MESA_SHADER_VERTEX
;
5018 create_function(&ctx
);
5020 ctx
.gs_max_out_vertices
= geom_shader
->info
->gs
.vertices_out
;
5021 ac_setup_rings(&ctx
);
5023 nir_foreach_variable(variable
, &geom_shader
->outputs
)
5024 handle_shader_output_decl(&ctx
, variable
);
5026 ac_gs_copy_shader_emit(&ctx
);
5028 LLVMBuildRetVoid(ctx
.builder
);
5030 ac_llvm_finalize_module(&ctx
);
5032 ac_compile_llvm_module(tm
, ctx
.module
, binary
, config
, shader_info
,
5034 dump_shader
, options
->supports_spill
);