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 draw_index
;
86 LLVMValueRef vertex_id
;
87 LLVMValueRef rel_auto_id
;
88 LLVMValueRef vs_prim_id
;
89 LLVMValueRef instance_id
;
91 LLVMValueRef es2gs_offset
;
93 LLVMValueRef gsvs_ring_stride
;
94 LLVMValueRef gsvs_num_entries
;
95 LLVMValueRef gs2vs_offset
;
96 LLVMValueRef gs_wave_id
;
97 LLVMValueRef gs_vtx_offset
[6];
98 LLVMValueRef gs_prim_id
, gs_invocation_id
;
100 LLVMValueRef esgs_ring
;
101 LLVMValueRef gsvs_ring
;
103 LLVMValueRef prim_mask
;
104 LLVMValueRef sample_positions
;
105 LLVMValueRef persp_sample
, persp_center
, persp_centroid
;
106 LLVMValueRef linear_sample
, linear_center
, linear_centroid
;
107 LLVMValueRef front_face
;
108 LLVMValueRef ancillary
;
109 LLVMValueRef frag_pos
[4];
111 LLVMBasicBlockRef continue_block
;
112 LLVMBasicBlockRef break_block
;
131 LLVMValueRef i32zero
;
133 LLVMValueRef f32zero
;
135 LLVMValueRef v4f32empty
;
137 unsigned uniform_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 shader_io_get_unique_index(gl_varying_slot slot
)
178 if (slot
== VARYING_SLOT_POS
)
180 if (slot
== VARYING_SLOT_PSIZ
)
182 if (slot
== VARYING_SLOT_CLIP_DIST0
)
184 if (slot
== VARYING_SLOT_CLIP_DIST1
)
186 if (slot
>= VARYING_SLOT_VAR0
&& slot
<= VARYING_SLOT_VAR31
)
187 return 4 + (slot
- VARYING_SLOT_VAR0
);
188 unreachable("illegal slot in get unique index\n");
191 static unsigned llvm_get_type_size(LLVMTypeRef type
)
193 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
196 case LLVMIntegerTypeKind
:
197 return LLVMGetIntTypeWidth(type
) / 8;
198 case LLVMFloatTypeKind
:
200 case LLVMPointerTypeKind
:
202 case LLVMVectorTypeKind
:
203 return LLVMGetVectorSize(type
) *
204 llvm_get_type_size(LLVMGetElementType(type
));
211 static void set_llvm_calling_convention(LLVMValueRef func
,
212 gl_shader_stage stage
)
214 enum radeon_llvm_calling_convention calling_conv
;
217 case MESA_SHADER_VERTEX
:
218 case MESA_SHADER_TESS_CTRL
:
219 case MESA_SHADER_TESS_EVAL
:
220 calling_conv
= RADEON_LLVM_AMDGPU_VS
;
222 case MESA_SHADER_GEOMETRY
:
223 calling_conv
= RADEON_LLVM_AMDGPU_GS
;
225 case MESA_SHADER_FRAGMENT
:
226 calling_conv
= RADEON_LLVM_AMDGPU_PS
;
228 case MESA_SHADER_COMPUTE
:
229 calling_conv
= RADEON_LLVM_AMDGPU_CS
;
232 unreachable("Unhandle shader type");
235 LLVMSetFunctionCallConv(func
, calling_conv
);
239 create_llvm_function(LLVMContextRef ctx
, LLVMModuleRef module
,
240 LLVMBuilderRef builder
, LLVMTypeRef
*return_types
,
241 unsigned num_return_elems
, LLVMTypeRef
*param_types
,
242 unsigned param_count
, unsigned array_params_mask
,
243 unsigned sgpr_params
, bool unsafe_math
)
245 LLVMTypeRef main_function_type
, ret_type
;
246 LLVMBasicBlockRef main_function_body
;
248 if (num_return_elems
)
249 ret_type
= LLVMStructTypeInContext(ctx
, return_types
,
250 num_return_elems
, true);
252 ret_type
= LLVMVoidTypeInContext(ctx
);
254 /* Setup the function */
256 LLVMFunctionType(ret_type
, param_types
, param_count
, 0);
257 LLVMValueRef main_function
=
258 LLVMAddFunction(module
, "main", main_function_type
);
260 LLVMAppendBasicBlockInContext(ctx
, main_function
, "main_body");
261 LLVMPositionBuilderAtEnd(builder
, main_function_body
);
263 LLVMSetFunctionCallConv(main_function
, RADEON_LLVM_AMDGPU_CS
);
264 for (unsigned i
= 0; i
< sgpr_params
; ++i
) {
265 if (array_params_mask
& (1 << i
)) {
266 LLVMValueRef P
= LLVMGetParam(main_function
, i
);
267 ac_add_function_attr(main_function
, i
+ 1, AC_FUNC_ATTR_BYVAL
);
268 ac_add_attr_dereferenceable(P
, UINT64_MAX
);
271 ac_add_function_attr(main_function
, i
+ 1, AC_FUNC_ATTR_INREG
);
276 /* These were copied from some LLVM test. */
277 LLVMAddTargetDependentFunctionAttr(main_function
,
278 "less-precise-fpmad",
280 LLVMAddTargetDependentFunctionAttr(main_function
,
283 LLVMAddTargetDependentFunctionAttr(main_function
,
286 LLVMAddTargetDependentFunctionAttr(main_function
,
290 return main_function
;
293 static LLVMTypeRef
const_array(LLVMTypeRef elem_type
, int num_elements
)
295 return LLVMPointerType(LLVMArrayType(elem_type
, num_elements
),
299 static LLVMValueRef
get_shared_memory_ptr(struct nir_to_llvm_context
*ctx
,
307 offset
= LLVMConstInt(ctx
->i32
, idx
, false);
309 ptr
= ctx
->shared_memory
;
310 ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &offset
, 1, "");
311 addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
312 ptr
= LLVMBuildBitCast(ctx
->builder
, ptr
, LLVMPointerType(type
, addr_space
), "");
316 static LLVMTypeRef
to_integer_type_scalar(struct nir_to_llvm_context
*ctx
, LLVMTypeRef t
)
318 if (t
== ctx
->f16
|| t
== ctx
->i16
)
320 else if (t
== ctx
->f32
|| t
== ctx
->i32
)
322 else if (t
== ctx
->f64
|| t
== ctx
->i64
)
325 unreachable("Unhandled integer size");
328 static LLVMTypeRef
to_integer_type(struct nir_to_llvm_context
*ctx
, LLVMTypeRef t
)
330 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
331 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
332 return LLVMVectorType(to_integer_type_scalar(ctx
, elem_type
),
333 LLVMGetVectorSize(t
));
335 return to_integer_type_scalar(ctx
, t
);
338 static LLVMValueRef
to_integer(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
)
340 LLVMTypeRef type
= LLVMTypeOf(v
);
341 return LLVMBuildBitCast(ctx
->builder
, v
, to_integer_type(ctx
, type
), "");
344 static LLVMTypeRef
to_float_type_scalar(struct nir_to_llvm_context
*ctx
, LLVMTypeRef t
)
346 if (t
== ctx
->i16
|| t
== ctx
->f16
)
348 else if (t
== ctx
->i32
|| t
== ctx
->f32
)
350 else if (t
== ctx
->i64
|| t
== ctx
->f64
)
353 unreachable("Unhandled float size");
356 static LLVMTypeRef
to_float_type(struct nir_to_llvm_context
*ctx
, LLVMTypeRef t
)
358 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
359 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
360 return LLVMVectorType(to_float_type_scalar(ctx
, elem_type
),
361 LLVMGetVectorSize(t
));
363 return to_float_type_scalar(ctx
, t
);
366 static LLVMValueRef
to_float(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
)
368 LLVMTypeRef type
= LLVMTypeOf(v
);
369 return LLVMBuildBitCast(ctx
->builder
, v
, to_float_type(ctx
, type
), "");
372 static int get_elem_bits(struct nir_to_llvm_context
*ctx
, LLVMTypeRef type
)
374 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
375 type
= LLVMGetElementType(type
);
377 if (LLVMGetTypeKind(type
) == LLVMIntegerTypeKind
)
378 return LLVMGetIntTypeWidth(type
);
380 if (type
== ctx
->f16
)
382 if (type
== ctx
->f32
)
384 if (type
== ctx
->f64
)
387 unreachable("Unhandled type kind in get_elem_bits");
390 static LLVMValueRef
unpack_param(struct nir_to_llvm_context
*ctx
,
391 LLVMValueRef param
, unsigned rshift
,
394 LLVMValueRef value
= param
;
396 value
= LLVMBuildLShr(ctx
->builder
, value
,
397 LLVMConstInt(ctx
->i32
, rshift
, false), "");
399 if (rshift
+ bitwidth
< 32) {
400 unsigned mask
= (1 << bitwidth
) - 1;
401 value
= LLVMBuildAnd(ctx
->builder
, value
,
402 LLVMConstInt(ctx
->i32
, mask
, false), "");
407 static void set_userdata_location(struct ac_userdata_info
*ud_info
, uint8_t sgpr_idx
, uint8_t num_sgprs
)
409 ud_info
->sgpr_idx
= sgpr_idx
;
410 ud_info
->num_sgprs
= num_sgprs
;
411 ud_info
->indirect
= false;
412 ud_info
->indirect_offset
= 0;
415 static void set_userdata_location_shader(struct nir_to_llvm_context
*ctx
,
416 int idx
, uint8_t sgpr_idx
, uint8_t num_sgprs
)
418 set_userdata_location(&ctx
->shader_info
->user_sgprs_locs
.shader_data
[idx
], sgpr_idx
, num_sgprs
);
422 static void set_userdata_location_indirect(struct ac_userdata_info
*ud_info
, uint8_t sgpr_idx
, uint8_t num_sgprs
,
423 uint32_t indirect_offset
)
425 ud_info
->sgpr_idx
= sgpr_idx
;
426 ud_info
->num_sgprs
= num_sgprs
;
427 ud_info
->indirect
= true;
428 ud_info
->indirect_offset
= indirect_offset
;
432 static void create_function(struct nir_to_llvm_context
*ctx
)
434 LLVMTypeRef arg_types
[23];
435 unsigned arg_idx
= 0;
436 unsigned array_params_mask
= 0;
437 unsigned sgpr_count
= 0, user_sgpr_count
;
439 unsigned num_sets
= ctx
->options
->layout
? ctx
->options
->layout
->num_sets
: 0;
440 unsigned user_sgpr_idx
;
441 bool need_push_constants
;
442 bool need_ring_offsets
= false;
444 /* until we sort out scratch/global buffers always assign ring offsets for gs/vs/es */
445 if (ctx
->stage
== MESA_SHADER_GEOMETRY
||
446 ctx
->stage
== MESA_SHADER_VERTEX
||
447 ctx
->is_gs_copy_shader
)
448 need_ring_offsets
= true;
450 need_push_constants
= true;
451 if (!ctx
->options
->layout
)
452 need_push_constants
= false;
453 else if (!ctx
->options
->layout
->push_constant_size
&&
454 !ctx
->options
->layout
->dynamic_offset_count
)
455 need_push_constants
= false;
457 if (need_ring_offsets
&& !ctx
->options
->supports_spill
) {
458 arg_types
[arg_idx
++] = const_array(ctx
->v16i8
, 8); /* address of rings */
461 /* 1 for each descriptor set */
462 for (unsigned i
= 0; i
< num_sets
; ++i
) {
463 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& (1 << ctx
->stage
)) {
464 array_params_mask
|= (1 << arg_idx
);
465 arg_types
[arg_idx
++] = const_array(ctx
->i8
, 1024 * 1024);
469 if (need_push_constants
) {
470 /* 1 for push constants and dynamic descriptors */
471 array_params_mask
|= (1 << arg_idx
);
472 arg_types
[arg_idx
++] = const_array(ctx
->i8
, 1024 * 1024);
475 switch (ctx
->stage
) {
476 case MESA_SHADER_COMPUTE
:
477 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3); /* grid size */
478 user_sgpr_count
= arg_idx
;
479 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3);
480 arg_types
[arg_idx
++] = ctx
->i32
;
481 sgpr_count
= arg_idx
;
483 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3);
485 case MESA_SHADER_VERTEX
:
486 if (!ctx
->is_gs_copy_shader
) {
487 arg_types
[arg_idx
++] = const_array(ctx
->v16i8
, 16); /* vertex buffers */
488 arg_types
[arg_idx
++] = ctx
->i32
; // base vertex
489 arg_types
[arg_idx
++] = ctx
->i32
; // start instance
490 arg_types
[arg_idx
++] = ctx
->i32
; // draw index
492 user_sgpr_count
= arg_idx
;
493 if (ctx
->options
->key
.vs
.as_es
)
494 arg_types
[arg_idx
++] = ctx
->i32
; //es2gs offset
495 sgpr_count
= arg_idx
;
496 arg_types
[arg_idx
++] = ctx
->i32
; // vertex id
497 if (!ctx
->is_gs_copy_shader
) {
498 arg_types
[arg_idx
++] = ctx
->i32
; // rel auto id
499 arg_types
[arg_idx
++] = ctx
->i32
; // vs prim id
500 arg_types
[arg_idx
++] = ctx
->i32
; // instance id
503 case MESA_SHADER_GEOMETRY
:
504 arg_types
[arg_idx
++] = ctx
->i32
; // gsvs stride
505 arg_types
[arg_idx
++] = ctx
->i32
; // gsvs num entires
506 user_sgpr_count
= arg_idx
;
507 arg_types
[arg_idx
++] = ctx
->i32
; // gs2vs offset
508 arg_types
[arg_idx
++] = ctx
->i32
; // wave id
509 sgpr_count
= arg_idx
;
510 arg_types
[arg_idx
++] = ctx
->i32
; // vtx0
511 arg_types
[arg_idx
++] = ctx
->i32
; // vtx1
512 arg_types
[arg_idx
++] = ctx
->i32
; // prim id
513 arg_types
[arg_idx
++] = ctx
->i32
; // vtx2
514 arg_types
[arg_idx
++] = ctx
->i32
; // vtx3
515 arg_types
[arg_idx
++] = ctx
->i32
; // vtx4
516 arg_types
[arg_idx
++] = ctx
->i32
; // vtx5
517 arg_types
[arg_idx
++] = ctx
->i32
; // GS instance id
519 case MESA_SHADER_FRAGMENT
:
520 arg_types
[arg_idx
++] = const_array(ctx
->f32
, 32); /* sample positions */
521 user_sgpr_count
= arg_idx
;
522 arg_types
[arg_idx
++] = ctx
->i32
; /* prim mask */
523 sgpr_count
= arg_idx
;
524 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp sample */
525 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp center */
526 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp centroid */
527 arg_types
[arg_idx
++] = ctx
->v3i32
; /* persp pull model */
528 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear sample */
529 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear center */
530 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear centroid */
531 arg_types
[arg_idx
++] = ctx
->f32
; /* line stipple tex */
532 arg_types
[arg_idx
++] = ctx
->f32
; /* pos x float */
533 arg_types
[arg_idx
++] = ctx
->f32
; /* pos y float */
534 arg_types
[arg_idx
++] = ctx
->f32
; /* pos z float */
535 arg_types
[arg_idx
++] = ctx
->f32
; /* pos w float */
536 arg_types
[arg_idx
++] = ctx
->i32
; /* front face */
537 arg_types
[arg_idx
++] = ctx
->i32
; /* ancillary */
538 arg_types
[arg_idx
++] = ctx
->f32
; /* sample coverage */
539 arg_types
[arg_idx
++] = ctx
->i32
; /* fixed pt */
542 unreachable("Shader stage not implemented");
545 ctx
->main_function
= create_llvm_function(
546 ctx
->context
, ctx
->module
, ctx
->builder
, NULL
, 0, arg_types
,
547 arg_idx
, array_params_mask
, sgpr_count
, ctx
->options
->unsafe_math
);
548 set_llvm_calling_convention(ctx
->main_function
, ctx
->stage
);
550 ctx
->shader_info
->num_input_sgprs
= 0;
551 ctx
->shader_info
->num_input_vgprs
= 0;
553 ctx
->shader_info
->num_user_sgprs
= ctx
->options
->supports_spill
? 2 : 0;
554 for (i
= 0; i
< user_sgpr_count
; i
++)
555 ctx
->shader_info
->num_user_sgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
557 ctx
->shader_info
->num_input_sgprs
= ctx
->shader_info
->num_user_sgprs
;
558 for (; i
< sgpr_count
; i
++)
559 ctx
->shader_info
->num_input_sgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
561 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
562 for (; i
< arg_idx
; ++i
)
563 ctx
->shader_info
->num_input_vgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
568 if (ctx
->options
->supports_spill
|| need_ring_offsets
) {
569 set_userdata_location_shader(ctx
, AC_UD_SCRATCH_RING_OFFSETS
, user_sgpr_idx
, 2);
571 if (ctx
->options
->supports_spill
) {
572 ctx
->ring_offsets
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.amdgcn.implicit.buffer.ptr",
573 LLVMPointerType(ctx
->i8
, CONST_ADDR_SPACE
),
574 NULL
, 0, AC_FUNC_ATTR_READNONE
);
575 ctx
->ring_offsets
= LLVMBuildBitCast(ctx
->builder
, ctx
->ring_offsets
,
576 const_array(ctx
->v16i8
, 8), "");
578 ctx
->ring_offsets
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
581 for (unsigned i
= 0; i
< num_sets
; ++i
) {
582 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& (1 << ctx
->stage
)) {
583 set_userdata_location(&ctx
->shader_info
->user_sgprs_locs
.descriptor_sets
[i
], user_sgpr_idx
, 2);
585 ctx
->descriptor_sets
[i
] =
586 LLVMGetParam(ctx
->main_function
, arg_idx
++);
588 ctx
->descriptor_sets
[i
] = NULL
;
591 if (need_push_constants
) {
592 ctx
->push_constants
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
593 set_userdata_location_shader(ctx
, AC_UD_PUSH_CONSTANTS
, user_sgpr_idx
, 2);
597 switch (ctx
->stage
) {
598 case MESA_SHADER_COMPUTE
:
599 set_userdata_location_shader(ctx
, AC_UD_CS_GRID_SIZE
, user_sgpr_idx
, 3);
601 ctx
->num_work_groups
=
602 LLVMGetParam(ctx
->main_function
, arg_idx
++);
604 LLVMGetParam(ctx
->main_function
, arg_idx
++);
606 LLVMGetParam(ctx
->main_function
, arg_idx
++);
607 ctx
->local_invocation_ids
=
608 LLVMGetParam(ctx
->main_function
, arg_idx
++);
610 case MESA_SHADER_VERTEX
:
611 if (!ctx
->is_gs_copy_shader
) {
612 set_userdata_location_shader(ctx
, AC_UD_VS_VERTEX_BUFFERS
, user_sgpr_idx
, 2);
614 ctx
->vertex_buffers
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
615 set_userdata_location_shader(ctx
, AC_UD_VS_BASE_VERTEX_START_INSTANCE
, user_sgpr_idx
, 3);
617 ctx
->base_vertex
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
618 ctx
->start_instance
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
619 ctx
->draw_index
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
621 if (ctx
->options
->key
.vs
.as_es
)
622 ctx
->es2gs_offset
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
623 ctx
->vertex_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
624 if (!ctx
->is_gs_copy_shader
) {
625 ctx
->rel_auto_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
626 ctx
->vs_prim_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
627 ctx
->instance_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
630 case MESA_SHADER_GEOMETRY
:
631 set_userdata_location_shader(ctx
, AC_UD_GS_VS_RING_STRIDE_ENTRIES
, user_sgpr_idx
, 2);
633 ctx
->gsvs_ring_stride
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
634 ctx
->gsvs_num_entries
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
635 ctx
->gs2vs_offset
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
636 ctx
->gs_wave_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
637 ctx
->gs_vtx_offset
[0] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
638 ctx
->gs_vtx_offset
[1] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
639 ctx
->gs_prim_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
640 ctx
->gs_vtx_offset
[2] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
641 ctx
->gs_vtx_offset
[3] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
642 ctx
->gs_vtx_offset
[4] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
643 ctx
->gs_vtx_offset
[5] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
644 ctx
->gs_invocation_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
646 case MESA_SHADER_FRAGMENT
:
647 set_userdata_location_shader(ctx
, AC_UD_PS_SAMPLE_POS
, user_sgpr_idx
, 2);
649 ctx
->sample_positions
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
650 ctx
->prim_mask
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
651 ctx
->persp_sample
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
652 ctx
->persp_center
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
653 ctx
->persp_centroid
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
655 ctx
->linear_sample
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
656 ctx
->linear_center
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
657 ctx
->linear_centroid
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
658 arg_idx
++; /* line stipple */
659 ctx
->frag_pos
[0] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
660 ctx
->frag_pos
[1] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
661 ctx
->frag_pos
[2] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
662 ctx
->frag_pos
[3] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
663 ctx
->front_face
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
664 ctx
->ancillary
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
667 unreachable("Shader stage not implemented");
671 static void setup_types(struct nir_to_llvm_context
*ctx
)
673 LLVMValueRef args
[4];
675 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->context
);
676 ctx
->i1
= LLVMIntTypeInContext(ctx
->context
, 1);
677 ctx
->i8
= LLVMIntTypeInContext(ctx
->context
, 8);
678 ctx
->i16
= LLVMIntTypeInContext(ctx
->context
, 16);
679 ctx
->i32
= LLVMIntTypeInContext(ctx
->context
, 32);
680 ctx
->i64
= LLVMIntTypeInContext(ctx
->context
, 64);
681 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
682 ctx
->v3i32
= LLVMVectorType(ctx
->i32
, 3);
683 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
684 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
685 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
686 ctx
->f16
= LLVMHalfTypeInContext(ctx
->context
);
687 ctx
->f64
= LLVMDoubleTypeInContext(ctx
->context
);
688 ctx
->v2f32
= LLVMVectorType(ctx
->f32
, 2);
689 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
690 ctx
->v16i8
= LLVMVectorType(ctx
->i8
, 16);
692 ctx
->i32zero
= LLVMConstInt(ctx
->i32
, 0, false);
693 ctx
->i32one
= LLVMConstInt(ctx
->i32
, 1, false);
694 ctx
->f32zero
= LLVMConstReal(ctx
->f32
, 0.0);
695 ctx
->f32one
= LLVMConstReal(ctx
->f32
, 1.0);
697 args
[0] = ctx
->f32zero
;
698 args
[1] = ctx
->f32zero
;
699 args
[2] = ctx
->f32zero
;
700 args
[3] = ctx
->f32one
;
701 ctx
->v4f32empty
= LLVMConstVector(args
, 4);
703 ctx
->uniform_md_kind
=
704 LLVMGetMDKindIDInContext(ctx
->context
, "amdgpu.uniform", 14);
705 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
707 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
710 static int get_llvm_num_components(LLVMValueRef value
)
712 LLVMTypeRef type
= LLVMTypeOf(value
);
713 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
714 ? LLVMGetVectorSize(type
)
716 return num_components
;
719 static LLVMValueRef
llvm_extract_elem(struct nir_to_llvm_context
*ctx
,
723 int count
= get_llvm_num_components(value
);
725 assert(index
< count
);
729 return LLVMBuildExtractElement(ctx
->builder
, value
,
730 LLVMConstInt(ctx
->i32
, index
, false), "");
733 static LLVMValueRef
trim_vector(struct nir_to_llvm_context
*ctx
,
734 LLVMValueRef value
, unsigned count
)
736 unsigned num_components
= get_llvm_num_components(value
);
737 if (count
== num_components
)
740 LLVMValueRef masks
[] = {
741 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
742 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false)};
745 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
748 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
749 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
753 build_store_values_extended(struct nir_to_llvm_context
*ctx
,
754 LLVMValueRef
*values
,
755 unsigned value_count
,
756 unsigned value_stride
,
759 LLVMBuilderRef builder
= ctx
->builder
;
762 if (value_count
== 1) {
763 LLVMBuildStore(builder
, vec
, values
[0]);
767 for (i
= 0; i
< value_count
; i
++) {
768 LLVMValueRef ptr
= values
[i
* value_stride
];
769 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
770 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
771 LLVMBuildStore(builder
, value
, ptr
);
775 static LLVMTypeRef
get_def_type(struct nir_to_llvm_context
*ctx
,
778 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->context
, def
->bit_size
);
779 if (def
->num_components
> 1) {
780 type
= LLVMVectorType(type
, def
->num_components
);
785 static LLVMValueRef
get_src(struct nir_to_llvm_context
*ctx
, nir_src src
)
788 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->defs
, src
.ssa
);
789 return (LLVMValueRef
)entry
->data
;
793 static LLVMBasicBlockRef
get_block(struct nir_to_llvm_context
*ctx
,
796 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->defs
, b
);
797 return (LLVMBasicBlockRef
)entry
->data
;
800 static LLVMValueRef
get_alu_src(struct nir_to_llvm_context
*ctx
,
802 unsigned num_components
)
804 LLVMValueRef value
= get_src(ctx
, src
.src
);
805 bool need_swizzle
= false;
808 LLVMTypeRef type
= LLVMTypeOf(value
);
809 unsigned src_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
810 ? LLVMGetVectorSize(type
)
813 for (unsigned i
= 0; i
< num_components
; ++i
) {
814 assert(src
.swizzle
[i
] < src_components
);
815 if (src
.swizzle
[i
] != i
)
819 if (need_swizzle
|| num_components
!= src_components
) {
820 LLVMValueRef masks
[] = {
821 LLVMConstInt(ctx
->i32
, src
.swizzle
[0], false),
822 LLVMConstInt(ctx
->i32
, src
.swizzle
[1], false),
823 LLVMConstInt(ctx
->i32
, src
.swizzle
[2], false),
824 LLVMConstInt(ctx
->i32
, src
.swizzle
[3], false)};
826 if (src_components
> 1 && num_components
== 1) {
827 value
= LLVMBuildExtractElement(ctx
->builder
, value
,
829 } else if (src_components
== 1 && num_components
> 1) {
830 LLVMValueRef values
[] = {value
, value
, value
, value
};
831 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
833 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
834 value
= LLVMBuildShuffleVector(ctx
->builder
, value
, value
,
843 static LLVMValueRef
emit_int_cmp(struct nir_to_llvm_context
*ctx
,
844 LLVMIntPredicate pred
, LLVMValueRef src0
,
847 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
848 return LLVMBuildSelect(ctx
->builder
, result
,
849 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
850 LLVMConstInt(ctx
->i32
, 0, false), "");
853 static LLVMValueRef
emit_float_cmp(struct nir_to_llvm_context
*ctx
,
854 LLVMRealPredicate pred
, LLVMValueRef src0
,
858 src0
= to_float(ctx
, src0
);
859 src1
= to_float(ctx
, src1
);
860 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
861 return LLVMBuildSelect(ctx
->builder
, result
,
862 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
863 LLVMConstInt(ctx
->i32
, 0, false), "");
866 static LLVMValueRef
emit_intrin_1f_param(struct nir_to_llvm_context
*ctx
,
868 LLVMTypeRef result_type
,
872 LLVMValueRef params
[] = {
876 sprintf(name
, "%s.f%d", intrin
, get_elem_bits(ctx
, result_type
));
877 return ac_emit_llvm_intrinsic(&ctx
->ac
, name
, result_type
, params
, 1, AC_FUNC_ATTR_READNONE
);
880 static LLVMValueRef
emit_intrin_2f_param(struct nir_to_llvm_context
*ctx
,
882 LLVMTypeRef result_type
,
883 LLVMValueRef src0
, LLVMValueRef src1
)
886 LLVMValueRef params
[] = {
891 sprintf(name
, "%s.f%d", intrin
, get_elem_bits(ctx
, result_type
));
892 return ac_emit_llvm_intrinsic(&ctx
->ac
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
895 static LLVMValueRef
emit_intrin_3f_param(struct nir_to_llvm_context
*ctx
,
897 LLVMTypeRef result_type
,
898 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
901 LLVMValueRef params
[] = {
907 sprintf(name
, "%s.f%d", intrin
, get_elem_bits(ctx
, result_type
));
908 return ac_emit_llvm_intrinsic(&ctx
->ac
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
911 static LLVMValueRef
emit_bcsel(struct nir_to_llvm_context
*ctx
,
912 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
914 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
916 return LLVMBuildSelect(ctx
->builder
, v
, src1
, src2
, "");
919 static LLVMValueRef
emit_find_lsb(struct nir_to_llvm_context
*ctx
,
922 LLVMValueRef params
[2] = {
925 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
926 * add special code to check for x=0. The reason is that
927 * the LLVM behavior for x=0 is different from what we
930 * The hardware already implements the correct behavior.
932 LLVMConstInt(ctx
->i32
, 1, false),
934 return ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.cttz.i32", ctx
->i32
, params
, 2, AC_FUNC_ATTR_READNONE
);
937 static LLVMValueRef
emit_ifind_msb(struct nir_to_llvm_context
*ctx
,
940 LLVMValueRef msb
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.AMDGPU.flbit.i32",
942 AC_FUNC_ATTR_READNONE
);
944 /* The HW returns the last bit index from MSB, but NIR wants
945 * the index from LSB. Invert it by doing "31 - msb". */
946 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
949 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
950 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
951 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
952 src0
, ctx
->i32zero
, ""),
953 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
954 src0
, all_ones
, ""), "");
956 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
959 static LLVMValueRef
emit_ufind_msb(struct nir_to_llvm_context
*ctx
,
962 LLVMValueRef args
[2] = {
966 LLVMValueRef msb
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.ctlz.i32",
967 ctx
->i32
, args
, ARRAY_SIZE(args
),
968 AC_FUNC_ATTR_READNONE
);
970 /* The HW returns the last bit index from MSB, but NIR wants
971 * the index from LSB. Invert it by doing "31 - msb". */
972 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
975 return LLVMBuildSelect(ctx
->builder
,
976 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src0
,
978 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
981 static LLVMValueRef
emit_minmax_int(struct nir_to_llvm_context
*ctx
,
982 LLVMIntPredicate pred
,
983 LLVMValueRef src0
, LLVMValueRef src1
)
985 return LLVMBuildSelect(ctx
->builder
,
986 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
991 static LLVMValueRef
emit_iabs(struct nir_to_llvm_context
*ctx
,
994 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
995 LLVMBuildNeg(ctx
->builder
, src0
, ""));
998 static LLVMValueRef
emit_fsign(struct nir_to_llvm_context
*ctx
,
1001 LLVMValueRef cmp
, val
;
1003 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, ctx
->f32zero
, "");
1004 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->f32one
, src0
, "");
1005 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, ctx
->f32zero
, "");
1006 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(ctx
->f32
, -1.0), "");
1010 static LLVMValueRef
emit_isign(struct nir_to_llvm_context
*ctx
,
1013 LLVMValueRef cmp
, val
;
1015 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, ctx
->i32zero
, "");
1016 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->i32one
, src0
, "");
1017 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, ctx
->i32zero
, "");
1018 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(ctx
->i32
, -1, true), "");
1022 static LLVMValueRef
emit_ffract(struct nir_to_llvm_context
*ctx
,
1025 const char *intr
= "llvm.floor.f32";
1026 LLVMValueRef fsrc0
= to_float(ctx
, src0
);
1027 LLVMValueRef params
[] = {
1030 LLVMValueRef floor
= ac_emit_llvm_intrinsic(&ctx
->ac
, intr
,
1031 ctx
->f32
, params
, 1,
1032 AC_FUNC_ATTR_READNONE
);
1033 return LLVMBuildFSub(ctx
->builder
, fsrc0
, floor
, "");
1036 static LLVMValueRef
emit_uint_carry(struct nir_to_llvm_context
*ctx
,
1038 LLVMValueRef src0
, LLVMValueRef src1
)
1040 LLVMTypeRef ret_type
;
1041 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
1043 LLVMValueRef params
[] = { src0
, src1
};
1044 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
1047 res
= ac_emit_llvm_intrinsic(&ctx
->ac
, intrin
, ret_type
,
1048 params
, 2, AC_FUNC_ATTR_READNONE
);
1050 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
1051 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
1055 static LLVMValueRef
emit_b2f(struct nir_to_llvm_context
*ctx
,
1058 return LLVMBuildAnd(ctx
->builder
, src0
, LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""), "");
1061 static LLVMValueRef
emit_umul_high(struct nir_to_llvm_context
*ctx
,
1062 LLVMValueRef src0
, LLVMValueRef src1
)
1064 LLVMValueRef dst64
, result
;
1065 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
1066 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
1068 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1069 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1070 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1074 static LLVMValueRef
emit_imul_high(struct nir_to_llvm_context
*ctx
,
1075 LLVMValueRef src0
, LLVMValueRef src1
)
1077 LLVMValueRef dst64
, result
;
1078 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
1079 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
1081 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1082 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1083 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1087 static LLVMValueRef
emit_bitfield_extract(struct nir_to_llvm_context
*ctx
,
1089 LLVMValueRef srcs
[3])
1091 LLVMValueRef result
;
1092 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
1093 result
= ac_emit_llvm_intrinsic(&ctx
->ac
, intrin
, ctx
->i32
, srcs
, 3, AC_FUNC_ATTR_READNONE
);
1095 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
1099 static LLVMValueRef
emit_bitfield_insert(struct nir_to_llvm_context
*ctx
,
1100 LLVMValueRef src0
, LLVMValueRef src1
,
1101 LLVMValueRef src2
, LLVMValueRef src3
)
1103 LLVMValueRef bfi_args
[3], result
;
1105 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
1106 LLVMBuildSub(ctx
->builder
,
1107 LLVMBuildShl(ctx
->builder
,
1112 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
1115 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
1118 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
1119 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
1121 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
1122 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
1123 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
1125 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
1129 static LLVMValueRef
emit_pack_half_2x16(struct nir_to_llvm_context
*ctx
,
1132 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1134 LLVMValueRef comp
[2];
1136 src0
= to_float(ctx
, src0
);
1137 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32zero
, "");
1138 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32one
, "");
1139 for (i
= 0; i
< 2; i
++) {
1140 comp
[i
] = LLVMBuildFPTrunc(ctx
->builder
, comp
[i
], ctx
->f16
, "");
1141 comp
[i
] = LLVMBuildBitCast(ctx
->builder
, comp
[i
], ctx
->i16
, "");
1142 comp
[i
] = LLVMBuildZExt(ctx
->builder
, comp
[i
], ctx
->i32
, "");
1145 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
1146 comp
[0] = LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
1151 static LLVMValueRef
emit_unpack_half_2x16(struct nir_to_llvm_context
*ctx
,
1154 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1155 LLVMValueRef temps
[2], result
, val
;
1158 for (i
= 0; i
< 2; i
++) {
1159 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
1160 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
1161 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
1162 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
1165 result
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), temps
[0],
1167 result
= LLVMBuildInsertElement(ctx
->builder
, result
, temps
[1],
1172 static LLVMValueRef
emit_ddxy(struct nir_to_llvm_context
*ctx
,
1178 LLVMValueRef result
;
1179 ctx
->has_ddxy
= true;
1181 if (!ctx
->lds
&& !ctx
->has_ds_bpermute
)
1182 ctx
->lds
= LLVMAddGlobalInAddressSpace(ctx
->module
,
1183 LLVMArrayType(ctx
->i32
, 64),
1184 "ddxy_lds", LOCAL_ADDR_SPACE
);
1186 if (op
== nir_op_fddx_fine
|| op
== nir_op_fddx
)
1187 mask
= AC_TID_MASK_LEFT
;
1188 else if (op
== nir_op_fddy_fine
|| op
== nir_op_fddy
)
1189 mask
= AC_TID_MASK_TOP
;
1191 mask
= AC_TID_MASK_TOP_LEFT
;
1193 /* for DDX we want to next X pixel, DDY next Y pixel. */
1194 if (op
== nir_op_fddx_fine
||
1195 op
== nir_op_fddx_coarse
||
1201 result
= ac_emit_ddxy(&ctx
->ac
, ctx
->has_ds_bpermute
,
1202 mask
, idx
, ctx
->lds
,
1208 * this takes an I,J coordinate pair,
1209 * and works out the X and Y derivatives.
1210 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
1212 static LLVMValueRef
emit_ddxy_interp(
1213 struct nir_to_llvm_context
*ctx
,
1214 LLVMValueRef interp_ij
)
1216 LLVMValueRef result
[4], a
;
1219 for (i
= 0; i
< 2; i
++) {
1220 a
= LLVMBuildExtractElement(ctx
->builder
, interp_ij
,
1221 LLVMConstInt(ctx
->i32
, i
, false), "");
1222 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
1223 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
1225 return ac_build_gather_values(&ctx
->ac
, result
, 4);
1228 static void visit_alu(struct nir_to_llvm_context
*ctx
, nir_alu_instr
*instr
)
1230 LLVMValueRef src
[4], result
= NULL
;
1231 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
1232 unsigned src_components
;
1233 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
1235 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
1236 switch (instr
->op
) {
1242 case nir_op_pack_half_2x16
:
1245 case nir_op_unpack_half_2x16
:
1249 src_components
= num_components
;
1252 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1253 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
1255 switch (instr
->op
) {
1261 src
[0] = to_float(ctx
, src
[0]);
1262 result
= LLVMBuildFNeg(ctx
->builder
, src
[0], "");
1265 result
= LLVMBuildNeg(ctx
->builder
, src
[0], "");
1268 result
= LLVMBuildNot(ctx
->builder
, src
[0], "");
1271 result
= LLVMBuildAdd(ctx
->builder
, src
[0], src
[1], "");
1274 src
[0] = to_float(ctx
, src
[0]);
1275 src
[1] = to_float(ctx
, src
[1]);
1276 result
= LLVMBuildFAdd(ctx
->builder
, src
[0], src
[1], "");
1279 src
[0] = to_float(ctx
, src
[0]);
1280 src
[1] = to_float(ctx
, src
[1]);
1281 result
= LLVMBuildFSub(ctx
->builder
, src
[0], src
[1], "");
1284 result
= LLVMBuildSub(ctx
->builder
, src
[0], src
[1], "");
1287 result
= LLVMBuildMul(ctx
->builder
, src
[0], src
[1], "");
1290 result
= LLVMBuildSRem(ctx
->builder
, src
[0], src
[1], "");
1293 result
= LLVMBuildURem(ctx
->builder
, src
[0], src
[1], "");
1296 src
[0] = to_float(ctx
, src
[0]);
1297 src
[1] = to_float(ctx
, src
[1]);
1298 result
= ac_emit_fdiv(&ctx
->ac
, src
[0], src
[1]);
1299 result
= emit_intrin_1f_param(ctx
, "llvm.floor",
1300 to_float_type(ctx
, def_type
), result
);
1301 result
= LLVMBuildFMul(ctx
->builder
, src
[1] , result
, "");
1302 result
= LLVMBuildFSub(ctx
->builder
, src
[0], result
, "");
1305 src
[0] = to_float(ctx
, src
[0]);
1306 src
[1] = to_float(ctx
, src
[1]);
1307 result
= LLVMBuildFRem(ctx
->builder
, src
[0], src
[1], "");
1310 result
= LLVMBuildSRem(ctx
->builder
, src
[0], src
[1], "");
1313 result
= LLVMBuildSDiv(ctx
->builder
, src
[0], src
[1], "");
1316 result
= LLVMBuildUDiv(ctx
->builder
, src
[0], src
[1], "");
1319 src
[0] = to_float(ctx
, src
[0]);
1320 src
[1] = to_float(ctx
, src
[1]);
1321 result
= LLVMBuildFMul(ctx
->builder
, src
[0], src
[1], "");
1324 src
[0] = to_float(ctx
, src
[0]);
1325 src
[1] = to_float(ctx
, src
[1]);
1326 result
= ac_emit_fdiv(&ctx
->ac
, src
[0], src
[1]);
1329 src
[0] = to_float(ctx
, src
[0]);
1330 result
= ac_emit_fdiv(&ctx
->ac
, ctx
->f32one
, src
[0]);
1333 result
= LLVMBuildAnd(ctx
->builder
, src
[0], src
[1], "");
1336 result
= LLVMBuildOr(ctx
->builder
, src
[0], src
[1], "");
1339 result
= LLVMBuildXor(ctx
->builder
, src
[0], src
[1], "");
1342 result
= LLVMBuildShl(ctx
->builder
, src
[0], src
[1], "");
1345 result
= LLVMBuildAShr(ctx
->builder
, src
[0], src
[1], "");
1348 result
= LLVMBuildLShr(ctx
->builder
, src
[0], src
[1], "");
1351 result
= emit_int_cmp(ctx
, LLVMIntSLT
, src
[0], src
[1]);
1354 result
= emit_int_cmp(ctx
, LLVMIntNE
, src
[0], src
[1]);
1357 result
= emit_int_cmp(ctx
, LLVMIntEQ
, src
[0], src
[1]);
1360 result
= emit_int_cmp(ctx
, LLVMIntSGE
, src
[0], src
[1]);
1363 result
= emit_int_cmp(ctx
, LLVMIntULT
, src
[0], src
[1]);
1366 result
= emit_int_cmp(ctx
, LLVMIntUGE
, src
[0], src
[1]);
1369 result
= emit_float_cmp(ctx
, LLVMRealUEQ
, src
[0], src
[1]);
1372 result
= emit_float_cmp(ctx
, LLVMRealUNE
, src
[0], src
[1]);
1375 result
= emit_float_cmp(ctx
, LLVMRealULT
, src
[0], src
[1]);
1378 result
= emit_float_cmp(ctx
, LLVMRealUGE
, src
[0], src
[1]);
1381 result
= emit_intrin_1f_param(ctx
, "llvm.fabs",
1382 to_float_type(ctx
, def_type
), src
[0]);
1385 result
= emit_iabs(ctx
, src
[0]);
1388 result
= emit_minmax_int(ctx
, LLVMIntSGT
, src
[0], src
[1]);
1391 result
= emit_minmax_int(ctx
, LLVMIntSLT
, src
[0], src
[1]);
1394 result
= emit_minmax_int(ctx
, LLVMIntUGT
, src
[0], src
[1]);
1397 result
= emit_minmax_int(ctx
, LLVMIntULT
, src
[0], src
[1]);
1400 result
= emit_isign(ctx
, src
[0]);
1403 src
[0] = to_float(ctx
, src
[0]);
1404 result
= emit_fsign(ctx
, src
[0]);
1407 result
= emit_intrin_1f_param(ctx
, "llvm.floor",
1408 to_float_type(ctx
, def_type
), src
[0]);
1411 result
= emit_intrin_1f_param(ctx
, "llvm.trunc",
1412 to_float_type(ctx
, def_type
), src
[0]);
1415 result
= emit_intrin_1f_param(ctx
, "llvm.ceil",
1416 to_float_type(ctx
, def_type
), src
[0]);
1418 case nir_op_fround_even
:
1419 result
= emit_intrin_1f_param(ctx
, "llvm.rint",
1420 to_float_type(ctx
, def_type
),src
[0]);
1423 result
= emit_ffract(ctx
, src
[0]);
1426 result
= emit_intrin_1f_param(ctx
, "llvm.sin",
1427 to_float_type(ctx
, def_type
), src
[0]);
1430 result
= emit_intrin_1f_param(ctx
, "llvm.cos",
1431 to_float_type(ctx
, def_type
), src
[0]);
1434 result
= emit_intrin_1f_param(ctx
, "llvm.sqrt",
1435 to_float_type(ctx
, def_type
), src
[0]);
1438 result
= emit_intrin_1f_param(ctx
, "llvm.exp2",
1439 to_float_type(ctx
, def_type
), src
[0]);
1442 result
= emit_intrin_1f_param(ctx
, "llvm.log2",
1443 to_float_type(ctx
, def_type
), src
[0]);
1446 result
= emit_intrin_1f_param(ctx
, "llvm.sqrt",
1447 to_float_type(ctx
, def_type
), src
[0]);
1448 result
= ac_emit_fdiv(&ctx
->ac
, ctx
->f32one
, result
);
1451 result
= emit_intrin_2f_param(ctx
, "llvm.pow",
1452 to_float_type(ctx
, def_type
), src
[0], src
[1]);
1455 result
= emit_intrin_2f_param(ctx
, "llvm.maxnum",
1456 to_float_type(ctx
, def_type
), src
[0], src
[1]);
1459 result
= emit_intrin_2f_param(ctx
, "llvm.minnum",
1460 to_float_type(ctx
, def_type
), src
[0], src
[1]);
1463 result
= emit_intrin_3f_param(ctx
, "llvm.fma",
1464 to_float_type(ctx
, def_type
), src
[0], src
[1], src
[2]);
1466 case nir_op_ibitfield_extract
:
1467 result
= emit_bitfield_extract(ctx
, "llvm.AMDGPU.bfe.i32", src
);
1469 case nir_op_ubitfield_extract
:
1470 result
= emit_bitfield_extract(ctx
, "llvm.AMDGPU.bfe.u32", src
);
1472 case nir_op_bitfield_insert
:
1473 result
= emit_bitfield_insert(ctx
, src
[0], src
[1], src
[2], src
[3]);
1475 case nir_op_bitfield_reverse
:
1476 result
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.bitreverse.i32", ctx
->i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1478 case nir_op_bit_count
:
1479 result
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.ctpop.i32", ctx
->i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1484 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1485 src
[i
] = to_integer(ctx
, src
[i
]);
1486 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
1490 src
[0] = to_float(ctx
, src
[0]);
1491 result
= LLVMBuildFPToSI(ctx
->builder
, src
[0], def_type
, "");
1495 src
[0] = to_float(ctx
, src
[0]);
1496 result
= LLVMBuildFPToUI(ctx
->builder
, src
[0], def_type
, "");
1500 result
= LLVMBuildSIToFP(ctx
->builder
, src
[0], to_float_type(ctx
, def_type
), "");
1504 result
= LLVMBuildUIToFP(ctx
->builder
, src
[0], to_float_type(ctx
, def_type
), "");
1507 result
= LLVMBuildFPExt(ctx
->builder
, src
[0], to_float_type(ctx
, def_type
), "");
1510 result
= LLVMBuildFPTrunc(ctx
->builder
, src
[0], to_float_type(ctx
, def_type
), "");
1513 result
= emit_bcsel(ctx
, src
[0], src
[1], src
[2]);
1515 case nir_op_find_lsb
:
1516 result
= emit_find_lsb(ctx
, src
[0]);
1518 case nir_op_ufind_msb
:
1519 result
= emit_ufind_msb(ctx
, src
[0]);
1521 case nir_op_ifind_msb
:
1522 result
= emit_ifind_msb(ctx
, src
[0]);
1524 case nir_op_uadd_carry
:
1525 result
= emit_uint_carry(ctx
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
1527 case nir_op_usub_borrow
:
1528 result
= emit_uint_carry(ctx
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
1531 result
= emit_b2f(ctx
, src
[0]);
1533 case nir_op_fquantize2f16
:
1534 src
[0] = to_float(ctx
, src
[0]);
1535 result
= LLVMBuildFPTrunc(ctx
->builder
, src
[0], ctx
->f16
, "");
1536 /* need to convert back up to f32 */
1537 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
1539 case nir_op_umul_high
:
1540 result
= emit_umul_high(ctx
, src
[0], src
[1]);
1542 case nir_op_imul_high
:
1543 result
= emit_imul_high(ctx
, src
[0], src
[1]);
1545 case nir_op_pack_half_2x16
:
1546 result
= emit_pack_half_2x16(ctx
, src
[0]);
1548 case nir_op_unpack_half_2x16
:
1549 result
= emit_unpack_half_2x16(ctx
, src
[0]);
1553 case nir_op_fddx_fine
:
1554 case nir_op_fddy_fine
:
1555 case nir_op_fddx_coarse
:
1556 case nir_op_fddy_coarse
:
1557 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
1560 fprintf(stderr
, "Unknown NIR alu instr: ");
1561 nir_print_instr(&instr
->instr
, stderr
);
1562 fprintf(stderr
, "\n");
1567 assert(instr
->dest
.dest
.is_ssa
);
1568 result
= to_integer(ctx
, result
);
1569 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.dest
.ssa
,
1574 static void visit_load_const(struct nir_to_llvm_context
*ctx
,
1575 nir_load_const_instr
*instr
)
1577 LLVMValueRef values
[4], value
= NULL
;
1578 LLVMTypeRef element_type
=
1579 LLVMIntTypeInContext(ctx
->context
, instr
->def
.bit_size
);
1581 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1582 switch (instr
->def
.bit_size
) {
1584 values
[i
] = LLVMConstInt(element_type
,
1585 instr
->value
.u32
[i
], false);
1588 values
[i
] = LLVMConstInt(element_type
,
1589 instr
->value
.u64
[i
], false);
1593 "unsupported nir load_const bit_size: %d\n",
1594 instr
->def
.bit_size
);
1598 if (instr
->def
.num_components
> 1) {
1599 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1603 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, value
);
1606 static LLVMValueRef
cast_ptr(struct nir_to_llvm_context
*ctx
, LLVMValueRef ptr
,
1609 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
1610 return LLVMBuildBitCast(ctx
->builder
, ptr
,
1611 LLVMPointerType(type
, addr_space
), "");
1615 get_buffer_size(struct nir_to_llvm_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1618 LLVMBuildExtractElement(ctx
->builder
, descriptor
,
1619 LLVMConstInt(ctx
->i32
, 2, false), "");
1622 if (ctx
->options
->chip_class
>= VI
&& in_elements
) {
1623 /* On VI, the descriptor contains the size in bytes,
1624 * but TXQ must return the size in elements.
1625 * The stride is always non-zero for resources using TXQ.
1627 LLVMValueRef stride
=
1628 LLVMBuildExtractElement(ctx
->builder
, descriptor
,
1629 LLVMConstInt(ctx
->i32
, 1, false), "");
1630 stride
= LLVMBuildLShr(ctx
->builder
, stride
,
1631 LLVMConstInt(ctx
->i32
, 16, false), "");
1632 stride
= LLVMBuildAnd(ctx
->builder
, stride
,
1633 LLVMConstInt(ctx
->i32
, 0x3fff, false), "");
1635 size
= LLVMBuildUDiv(ctx
->builder
, size
, stride
, "");
1641 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
1644 static void build_int_type_name(
1646 char *buf
, unsigned bufsize
)
1648 assert(bufsize
>= 6);
1650 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
1651 snprintf(buf
, bufsize
, "v%ui32",
1652 LLVMGetVectorSize(type
));
1657 static LLVMValueRef
radv_lower_gather4_integer(struct nir_to_llvm_context
*ctx
,
1658 struct ac_tex_info
*tinfo
,
1659 nir_tex_instr
*instr
,
1660 const char *intr_name
,
1661 unsigned coord_vgpr_index
)
1663 LLVMValueRef coord
= tinfo
->args
[0];
1664 LLVMValueRef half_texel
[2];
1669 LLVMValueRef txq_args
[10];
1670 int txq_arg_count
= 0;
1672 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
1673 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, false);
1674 txq_args
[txq_arg_count
++] = tinfo
->args
[1];
1675 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0xf, 0); /* dmask */
1676 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* unorm */
1677 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* r128 */
1678 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, da
? 1 : 0, 0);
1679 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* glc */
1680 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* slc */
1681 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* tfe */
1682 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* lwe */
1683 size
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.SI.getresinfo.i32", ctx
->v4i32
,
1684 txq_args
, txq_arg_count
,
1685 AC_FUNC_ATTR_READNONE
);
1687 for (c
= 0; c
< 2; c
++) {
1688 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
1689 LLVMConstInt(ctx
->i32
, c
, false), "");
1690 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1691 half_texel
[c
] = ac_emit_fdiv(&ctx
->ac
, ctx
->f32one
, half_texel
[c
]);
1692 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1693 LLVMConstReal(ctx
->f32
, -0.5), "");
1697 for (c
= 0; c
< 2; c
++) {
1699 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, coord_vgpr_index
+ c
, 0);
1700 tmp
= LLVMBuildExtractElement(ctx
->builder
, coord
, index
, "");
1701 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1702 tmp
= LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
1703 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1704 coord
= LLVMBuildInsertElement(ctx
->builder
, coord
, tmp
, index
, "");
1707 tinfo
->args
[0] = coord
;
1708 return ac_emit_llvm_intrinsic(&ctx
->ac
, intr_name
, tinfo
->dst_type
, tinfo
->args
, tinfo
->arg_count
,
1709 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_NOUNWIND
);
1713 static LLVMValueRef
build_tex_intrinsic(struct nir_to_llvm_context
*ctx
,
1714 nir_tex_instr
*instr
,
1715 struct ac_tex_info
*tinfo
)
1717 const char *name
= "llvm.SI.image.sample";
1718 const char *infix
= "";
1719 char intr_name
[127];
1721 bool is_shadow
= instr
->is_shadow
;
1722 bool has_offset
= tinfo
->has_offset
;
1723 switch (instr
->op
) {
1725 case nir_texop_txf_ms
:
1726 case nir_texop_samples_identical
:
1727 name
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
? "llvm.SI.image.load" :
1728 instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
? "llvm.SI.vs.load.input" :
1729 "llvm.SI.image.load.mip";
1740 name
= "llvm.SI.getresinfo";
1742 case nir_texop_query_levels
:
1743 name
= "llvm.SI.getresinfo";
1746 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
1753 name
= "llvm.SI.gather4";
1757 name
= "llvm.SI.getlod";
1765 build_int_type_name(LLVMTypeOf(tinfo
->args
[0]), type
, sizeof(type
));
1766 sprintf(intr_name
, "%s%s%s%s.%s", name
, is_shadow
? ".c" : "", infix
,
1767 has_offset
? ".o" : "", type
);
1769 if (instr
->op
== nir_texop_tg4
) {
1770 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
1771 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
1772 return radv_lower_gather4_integer(ctx
, tinfo
, instr
, intr_name
,
1773 (int)has_offset
+ (int)is_shadow
);
1776 return ac_emit_llvm_intrinsic(&ctx
->ac
, intr_name
, tinfo
->dst_type
, tinfo
->args
, tinfo
->arg_count
,
1777 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_NOUNWIND
);
1781 static LLVMValueRef
visit_vulkan_resource_index(struct nir_to_llvm_context
*ctx
,
1782 nir_intrinsic_instr
*instr
)
1784 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
1785 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
1786 unsigned binding
= nir_intrinsic_binding(instr
);
1787 LLVMValueRef desc_ptr
= ctx
->descriptor_sets
[desc_set
];
1788 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[desc_set
].layout
;
1789 unsigned base_offset
= layout
->binding
[binding
].offset
;
1790 LLVMValueRef offset
, stride
;
1792 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
1793 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
1794 desc_ptr
= ctx
->push_constants
;
1795 base_offset
= ctx
->options
->layout
->push_constant_size
;
1796 base_offset
+= 16 * layout
->binding
[binding
].dynamic_offset_offset
;
1797 stride
= LLVMConstInt(ctx
->i32
, 16, false);
1799 stride
= LLVMConstInt(ctx
->i32
, layout
->binding
[binding
].size
, false);
1801 offset
= LLVMConstInt(ctx
->i32
, base_offset
, false);
1802 index
= LLVMBuildMul(ctx
->builder
, index
, stride
, "");
1803 offset
= LLVMBuildAdd(ctx
->builder
, offset
, index
, "");
1805 desc_ptr
= ac_build_gep0(&ctx
->ac
, desc_ptr
, offset
);
1806 desc_ptr
= cast_ptr(ctx
, desc_ptr
, ctx
->v4i32
);
1807 LLVMSetMetadata(desc_ptr
, ctx
->uniform_md_kind
, ctx
->empty_md
);
1809 return LLVMBuildLoad(ctx
->builder
, desc_ptr
, "");
1812 static LLVMValueRef
visit_load_push_constant(struct nir_to_llvm_context
*ctx
,
1813 nir_intrinsic_instr
*instr
)
1815 LLVMValueRef ptr
, addr
;
1817 addr
= LLVMConstInt(ctx
->i32
, nir_intrinsic_base(instr
), 0);
1818 addr
= LLVMBuildAdd(ctx
->builder
, addr
, get_src(ctx
, instr
->src
[0]), "");
1820 ptr
= ac_build_gep0(&ctx
->ac
, ctx
->push_constants
, addr
);
1821 ptr
= cast_ptr(ctx
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
1823 return LLVMBuildLoad(ctx
->builder
, ptr
, "");
1826 static LLVMValueRef
visit_get_buffer_size(struct nir_to_llvm_context
*ctx
,
1827 nir_intrinsic_instr
*instr
)
1829 LLVMValueRef desc
= get_src(ctx
, instr
->src
[0]);
1831 return get_buffer_size(ctx
, desc
, false);
1833 static void visit_store_ssbo(struct nir_to_llvm_context
*ctx
,
1834 nir_intrinsic_instr
*instr
)
1836 const char *store_name
;
1837 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
1838 LLVMTypeRef data_type
= ctx
->f32
;
1839 int elem_size_mult
= get_elem_bits(ctx
, LLVMTypeOf(src_data
)) / 32;
1840 int components_32bit
= elem_size_mult
* instr
->num_components
;
1841 unsigned writemask
= nir_intrinsic_write_mask(instr
);
1842 LLVMValueRef base_data
, base_offset
;
1843 LLVMValueRef params
[6];
1845 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
1846 ctx
->shader_info
->fs
.writes_memory
= true;
1848 params
[1] = get_src(ctx
, instr
->src
[1]);
1849 params
[2] = LLVMConstInt(ctx
->i32
, 0, false); /* vindex */
1850 params
[4] = LLVMConstInt(ctx
->i1
, 0, false); /* glc */
1851 params
[5] = LLVMConstInt(ctx
->i1
, 0, false); /* slc */
1853 if (components_32bit
> 1)
1854 data_type
= LLVMVectorType(ctx
->f32
, components_32bit
);
1856 base_data
= to_float(ctx
, src_data
);
1857 base_data
= trim_vector(ctx
, base_data
, instr
->num_components
);
1858 base_data
= LLVMBuildBitCast(ctx
->builder
, base_data
,
1860 base_offset
= get_src(ctx
, instr
->src
[2]); /* voffset */
1864 LLVMValueRef offset
;
1866 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
1868 /* Due to an LLVM limitation, split 3-element writes
1869 * into a 2-element and a 1-element write. */
1871 writemask
|= 1 << (start
+ 2);
1875 start
*= elem_size_mult
;
1876 count
*= elem_size_mult
;
1879 writemask
|= ((1u << (count
- 4)) - 1u) << (start
+ 4);
1884 store_name
= "llvm.amdgcn.buffer.store.v4f32";
1886 } else if (count
== 2) {
1887 tmp
= LLVMBuildExtractElement(ctx
->builder
,
1888 base_data
, LLVMConstInt(ctx
->i32
, start
, false), "");
1889 data
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), tmp
,
1892 tmp
= LLVMBuildExtractElement(ctx
->builder
,
1893 base_data
, LLVMConstInt(ctx
->i32
, start
+ 1, false), "");
1894 data
= LLVMBuildInsertElement(ctx
->builder
, data
, tmp
,
1896 store_name
= "llvm.amdgcn.buffer.store.v2f32";
1900 if (get_llvm_num_components(base_data
) > 1)
1901 data
= LLVMBuildExtractElement(ctx
->builder
, base_data
,
1902 LLVMConstInt(ctx
->i32
, start
, false), "");
1905 store_name
= "llvm.amdgcn.buffer.store.f32";
1908 offset
= base_offset
;
1910 offset
= LLVMBuildAdd(ctx
->builder
, offset
, LLVMConstInt(ctx
->i32
, start
* 4, false), "");
1914 ac_emit_llvm_intrinsic(&ctx
->ac
, store_name
,
1915 ctx
->voidt
, params
, 6, 0);
1919 static LLVMValueRef
visit_atomic_ssbo(struct nir_to_llvm_context
*ctx
,
1920 nir_intrinsic_instr
*instr
)
1923 LLVMValueRef params
[6];
1925 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
1926 ctx
->shader_info
->fs
.writes_memory
= true;
1928 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
1929 params
[arg_count
++] = llvm_extract_elem(ctx
, get_src(ctx
, instr
->src
[3]), 0);
1931 params
[arg_count
++] = llvm_extract_elem(ctx
, get_src(ctx
, instr
->src
[2]), 0);
1932 params
[arg_count
++] = get_src(ctx
, instr
->src
[0]);
1933 params
[arg_count
++] = LLVMConstInt(ctx
->i32
, 0, false); /* vindex */
1934 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1935 params
[arg_count
++] = LLVMConstInt(ctx
->i1
, 0, false); /* slc */
1937 switch (instr
->intrinsic
) {
1938 case nir_intrinsic_ssbo_atomic_add
:
1939 name
= "llvm.amdgcn.buffer.atomic.add";
1941 case nir_intrinsic_ssbo_atomic_imin
:
1942 name
= "llvm.amdgcn.buffer.atomic.smin";
1944 case nir_intrinsic_ssbo_atomic_umin
:
1945 name
= "llvm.amdgcn.buffer.atomic.umin";
1947 case nir_intrinsic_ssbo_atomic_imax
:
1948 name
= "llvm.amdgcn.buffer.atomic.smax";
1950 case nir_intrinsic_ssbo_atomic_umax
:
1951 name
= "llvm.amdgcn.buffer.atomic.umax";
1953 case nir_intrinsic_ssbo_atomic_and
:
1954 name
= "llvm.amdgcn.buffer.atomic.and";
1956 case nir_intrinsic_ssbo_atomic_or
:
1957 name
= "llvm.amdgcn.buffer.atomic.or";
1959 case nir_intrinsic_ssbo_atomic_xor
:
1960 name
= "llvm.amdgcn.buffer.atomic.xor";
1962 case nir_intrinsic_ssbo_atomic_exchange
:
1963 name
= "llvm.amdgcn.buffer.atomic.swap";
1965 case nir_intrinsic_ssbo_atomic_comp_swap
:
1966 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
1972 return ac_emit_llvm_intrinsic(&ctx
->ac
, name
, ctx
->i32
, params
, arg_count
, 0);
1975 static LLVMValueRef
visit_load_buffer(struct nir_to_llvm_context
*ctx
,
1976 nir_intrinsic_instr
*instr
)
1978 LLVMValueRef results
[2];
1979 int load_components
;
1980 int num_components
= instr
->num_components
;
1981 if (instr
->dest
.ssa
.bit_size
== 64)
1982 num_components
*= 2;
1984 for (int i
= 0; i
< num_components
; i
+= load_components
) {
1985 load_components
= MIN2(num_components
- i
, 4);
1986 const char *load_name
;
1987 LLVMTypeRef data_type
= ctx
->f32
;
1988 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, i
* 4, false);
1989 offset
= LLVMBuildAdd(ctx
->builder
, get_src(ctx
, instr
->src
[1]), offset
, "");
1991 if (load_components
== 3)
1992 data_type
= LLVMVectorType(ctx
->f32
, 4);
1993 else if (load_components
> 1)
1994 data_type
= LLVMVectorType(ctx
->f32
, load_components
);
1996 if (load_components
>= 3)
1997 load_name
= "llvm.amdgcn.buffer.load.v4f32";
1998 else if (load_components
== 2)
1999 load_name
= "llvm.amdgcn.buffer.load.v2f32";
2000 else if (load_components
== 1)
2001 load_name
= "llvm.amdgcn.buffer.load.f32";
2003 unreachable("unhandled number of components");
2005 LLVMValueRef params
[] = {
2006 get_src(ctx
, instr
->src
[0]),
2007 LLVMConstInt(ctx
->i32
, 0, false),
2009 LLVMConstInt(ctx
->i1
, 0, false),
2010 LLVMConstInt(ctx
->i1
, 0, false),
2013 results
[i
] = ac_emit_llvm_intrinsic(&ctx
->ac
, load_name
, data_type
, params
, 5, 0);
2017 LLVMValueRef ret
= results
[0];
2018 if (num_components
> 4 || num_components
== 3) {
2019 LLVMValueRef masks
[] = {
2020 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
2021 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false),
2022 LLVMConstInt(ctx
->i32
, 4, false), LLVMConstInt(ctx
->i32
, 5, false),
2023 LLVMConstInt(ctx
->i32
, 6, false), LLVMConstInt(ctx
->i32
, 7, false)
2026 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
2027 ret
= LLVMBuildShuffleVector(ctx
->builder
, results
[0],
2028 results
[num_components
> 4 ? 1 : 0], swizzle
, "");
2031 return LLVMBuildBitCast(ctx
->builder
, ret
,
2032 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2035 static LLVMValueRef
visit_load_ubo_buffer(struct nir_to_llvm_context
*ctx
,
2036 nir_intrinsic_instr
*instr
)
2038 LLVMValueRef results
[8], ret
;
2039 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
2040 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
2041 int num_components
= instr
->num_components
;
2043 rsrc
= LLVMBuildBitCast(ctx
->builder
, rsrc
, LLVMVectorType(ctx
->i8
, 16), "");
2045 if (instr
->dest
.ssa
.bit_size
== 64)
2046 num_components
*= 2;
2048 for (unsigned i
= 0; i
< num_components
; ++i
) {
2049 LLVMValueRef params
[] = {
2051 LLVMBuildAdd(ctx
->builder
, LLVMConstInt(ctx
->i32
, 4 * i
, 0),
2054 results
[i
] = ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.SI.load.const", ctx
->f32
,
2055 params
, 2, AC_FUNC_ATTR_READNONE
);
2059 ret
= ac_build_gather_values(&ctx
->ac
, results
, instr
->num_components
);
2060 return LLVMBuildBitCast(ctx
->builder
, ret
,
2061 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2065 radv_get_deref_offset(struct nir_to_llvm_context
*ctx
, nir_deref
*tail
,
2066 bool vs_in
, unsigned *vertex_index_out
,
2067 unsigned *const_out
, LLVMValueRef
*indir_out
)
2069 unsigned const_offset
= 0;
2070 LLVMValueRef offset
= NULL
;
2072 if (vertex_index_out
!= NULL
) {
2074 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2075 *vertex_index_out
= deref_array
->base_offset
;
2078 while (tail
->child
!= NULL
) {
2079 const struct glsl_type
*parent_type
= tail
->type
;
2082 if (tail
->deref_type
== nir_deref_type_array
) {
2083 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2084 LLVMValueRef index
, stride
, local_offset
;
2085 unsigned size
= glsl_count_attribute_slots(tail
->type
, vs_in
);
2087 const_offset
+= size
* deref_array
->base_offset
;
2088 if (deref_array
->deref_array_type
== nir_deref_array_type_direct
)
2091 assert(deref_array
->deref_array_type
== nir_deref_array_type_indirect
);
2092 index
= get_src(ctx
, deref_array
->indirect
);
2093 stride
= LLVMConstInt(ctx
->i32
, size
, 0);
2094 local_offset
= LLVMBuildMul(ctx
->builder
, stride
, index
, "");
2097 offset
= LLVMBuildAdd(ctx
->builder
, offset
, local_offset
, "");
2099 offset
= local_offset
;
2100 } else if (tail
->deref_type
== nir_deref_type_struct
) {
2101 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
2103 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
2104 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
2105 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
2108 unreachable("unsupported deref type");
2112 if (const_offset
&& offset
)
2113 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
2114 LLVMConstInt(ctx
->i32
, const_offset
, 0),
2117 *const_out
= const_offset
;
2118 *indir_out
= offset
;
2122 load_gs_input(struct nir_to_llvm_context
*ctx
,
2123 nir_intrinsic_instr
*instr
)
2125 LLVMValueRef indir_index
, vtx_offset
;
2126 unsigned const_index
;
2127 LLVMValueRef args
[9];
2128 unsigned param
, vtx_offset_param
;
2129 LLVMValueRef value
[4], result
;
2130 unsigned vertex_index
;
2131 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
,
2132 false, &vertex_index
,
2133 &const_index
, &indir_index
);
2134 vtx_offset_param
= vertex_index
;
2135 assert(vtx_offset_param
< 6);
2136 vtx_offset
= LLVMBuildMul(ctx
->builder
, ctx
->gs_vtx_offset
[vtx_offset_param
],
2137 LLVMConstInt(ctx
->i32
, 4, false), "");
2139 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
2140 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2141 args
[0] = ctx
->esgs_ring
;
2142 args
[1] = vtx_offset
;
2143 args
[2] = LLVMConstInt(ctx
->i32
, (param
* 4 + i
+ const_index
) * 256, false);
2144 args
[3] = ctx
->i32zero
;
2145 args
[4] = ctx
->i32one
; /* OFFEN */
2146 args
[5] = ctx
->i32zero
; /* IDXEN */
2147 args
[6] = ctx
->i32one
; /* GLC */
2148 args
[7] = ctx
->i32zero
; /* SLC */
2149 args
[8] = ctx
->i32zero
; /* TFE */
2151 value
[i
] = ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.SI.buffer.load.dword.i32.i32",
2152 ctx
->i32
, args
, 9, AC_FUNC_ATTR_READONLY
);
2154 result
= ac_build_gather_values(&ctx
->ac
, value
, instr
->num_components
);
2159 static LLVMValueRef
visit_load_var(struct nir_to_llvm_context
*ctx
,
2160 nir_intrinsic_instr
*instr
)
2162 LLVMValueRef values
[8];
2163 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2164 int ve
= instr
->dest
.ssa
.num_components
;
2165 LLVMValueRef indir_index
;
2167 unsigned const_index
;
2168 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
2169 instr
->variables
[0]->var
->data
.mode
== nir_var_shader_in
;
2170 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, vs_in
, NULL
,
2171 &const_index
, &indir_index
);
2173 if (instr
->dest
.ssa
.bit_size
== 64)
2176 switch (instr
->variables
[0]->var
->data
.mode
) {
2177 case nir_var_shader_in
:
2178 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
2179 return load_gs_input(ctx
, instr
);
2181 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2183 unsigned count
= glsl_count_attribute_slots(
2184 instr
->variables
[0]->var
->type
,
2185 ctx
->stage
== MESA_SHADER_VERTEX
);
2187 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2188 &ctx
->ac
, ctx
->inputs
+ idx
+ chan
, count
,
2191 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2195 values
[chan
] = ctx
->inputs
[idx
+ chan
+ const_index
* 4];
2199 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2201 unsigned count
= glsl_count_attribute_slots(
2202 instr
->variables
[0]->var
->type
, false);
2204 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2205 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2208 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2212 values
[chan
] = LLVMBuildLoad(ctx
->builder
, ctx
->locals
[idx
+ chan
+ const_index
* 4], "");
2216 case nir_var_shader_out
:
2217 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2219 unsigned count
= glsl_count_attribute_slots(
2220 instr
->variables
[0]->var
->type
, false);
2222 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2223 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
2226 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2230 values
[chan
] = LLVMBuildLoad(ctx
->builder
,
2231 ctx
->outputs
[idx
+ chan
+ const_index
* 4],
2236 case nir_var_shared
: {
2237 LLVMValueRef ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
2238 LLVMValueRef derived_ptr
;
2240 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2241 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, chan
, false);
2243 index
= LLVMBuildAdd(ctx
->builder
, index
, indir_index
, "");
2244 derived_ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, "");
2246 values
[chan
] = LLVMBuildLoad(ctx
->builder
, derived_ptr
, "");
2251 unreachable("unhandle variable mode");
2253 ret
= ac_build_gather_values(&ctx
->ac
, values
, ve
);
2254 return LLVMBuildBitCast(ctx
->builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2258 visit_store_var(struct nir_to_llvm_context
*ctx
,
2259 nir_intrinsic_instr
*instr
)
2261 LLVMValueRef temp_ptr
, value
;
2262 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2263 LLVMValueRef src
= to_float(ctx
, get_src(ctx
, instr
->src
[0]));
2264 int writemask
= instr
->const_index
[0];
2265 LLVMValueRef indir_index
;
2266 unsigned const_index
;
2267 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2268 NULL
, &const_index
, &indir_index
);
2270 if (get_elem_bits(ctx
, LLVMTypeOf(src
)) == 64) {
2271 int old_writemask
= writemask
;
2273 src
= LLVMBuildBitCast(ctx
->builder
, src
,
2274 LLVMVectorType(ctx
->f32
, get_llvm_num_components(src
) * 2),
2278 for (unsigned chan
= 0; chan
< 4; chan
++) {
2279 if (old_writemask
& (1 << chan
))
2280 writemask
|= 3u << (2 * chan
);
2284 switch (instr
->variables
[0]->var
->data
.mode
) {
2285 case nir_var_shader_out
:
2286 for (unsigned chan
= 0; chan
< 8; chan
++) {
2288 if (!(writemask
& (1 << chan
)))
2291 value
= llvm_extract_elem(ctx
, src
, chan
);
2293 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CLIP_DIST0
||
2294 instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CULL_DIST0
)
2297 unsigned count
= glsl_count_attribute_slots(
2298 instr
->variables
[0]->var
->type
, false);
2300 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2301 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
2304 if (get_llvm_num_components(tmp_vec
) > 1) {
2305 tmp_vec
= LLVMBuildInsertElement(ctx
->builder
, tmp_vec
,
2306 value
, indir_index
, "");
2309 build_store_values_extended(ctx
, ctx
->outputs
+ idx
+ chan
,
2310 count
, stride
, tmp_vec
);
2313 temp_ptr
= ctx
->outputs
[idx
+ chan
+ const_index
* stride
];
2315 LLVMBuildStore(ctx
->builder
, value
, temp_ptr
);
2320 for (unsigned chan
= 0; chan
< 8; chan
++) {
2321 if (!(writemask
& (1 << chan
)))
2324 value
= llvm_extract_elem(ctx
, src
, chan
);
2326 unsigned count
= glsl_count_attribute_slots(
2327 instr
->variables
[0]->var
->type
, false);
2329 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2330 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2333 tmp_vec
= LLVMBuildInsertElement(ctx
->builder
, tmp_vec
,
2334 value
, indir_index
, "");
2335 build_store_values_extended(ctx
, ctx
->locals
+ idx
+ chan
,
2338 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
2340 LLVMBuildStore(ctx
->builder
, value
, temp_ptr
);
2344 case nir_var_shared
: {
2345 LLVMValueRef ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
2346 for (unsigned chan
= 0; chan
< 8; chan
++) {
2347 if (!(writemask
& (1 << chan
)))
2349 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, chan
, false);
2350 LLVMValueRef derived_ptr
;
2353 index
= LLVMBuildAdd(ctx
->builder
, index
, indir_index
, "");
2355 value
= llvm_extract_elem(ctx
, src
, chan
);
2356 derived_ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, "");
2357 LLVMBuildStore(ctx
->builder
,
2358 to_integer(ctx
, value
), derived_ptr
);
2367 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
2370 case GLSL_SAMPLER_DIM_BUF
:
2372 case GLSL_SAMPLER_DIM_1D
:
2373 return array
? 2 : 1;
2374 case GLSL_SAMPLER_DIM_2D
:
2375 return array
? 3 : 2;
2376 case GLSL_SAMPLER_DIM_MS
:
2377 return array
? 4 : 3;
2378 case GLSL_SAMPLER_DIM_3D
:
2379 case GLSL_SAMPLER_DIM_CUBE
:
2381 case GLSL_SAMPLER_DIM_RECT
:
2382 case GLSL_SAMPLER_DIM_SUBPASS
:
2384 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
2392 static LLVMValueRef
get_image_coords(struct nir_to_llvm_context
*ctx
,
2393 nir_intrinsic_instr
*instr
)
2395 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
2396 if(instr
->variables
[0]->deref
.child
)
2397 type
= instr
->variables
[0]->deref
.child
->type
;
2399 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
2400 LLVMValueRef coords
[4];
2401 LLVMValueRef masks
[] = {
2402 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
2403 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false),
2407 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2408 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
2409 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2410 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
2411 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2413 count
= image_type_to_components_count(dim
,
2414 glsl_sampler_type_is_array(type
));
2417 if (instr
->src
[0].ssa
->num_components
)
2418 res
= LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[0], "");
2425 for (chan
= 0; chan
< count
; ++chan
) {
2426 coords
[chan
] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[chan
], "");
2430 for (chan
= 0; chan
< count
; ++chan
)
2431 coords
[chan
] = LLVMBuildAdd(ctx
->builder
, coords
[chan
], LLVMBuildFPToUI(ctx
->builder
, ctx
->frag_pos
[chan
], ctx
->i32
, ""), "");
2434 coords
[count
] = llvm_extract_elem(ctx
, get_src(ctx
, instr
->src
[1]), 0);
2439 coords
[3] = LLVMGetUndef(ctx
->i32
);
2442 res
= ac_build_gather_values(&ctx
->ac
, coords
, count
);
2447 static void build_type_name_for_intr(
2449 char *buf
, unsigned bufsize
)
2451 LLVMTypeRef elem_type
= type
;
2453 assert(bufsize
>= 8);
2455 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
2456 int ret
= snprintf(buf
, bufsize
, "v%u",
2457 LLVMGetVectorSize(type
));
2459 char *type_name
= LLVMPrintTypeToString(type
);
2460 fprintf(stderr
, "Error building type name for: %s\n",
2464 elem_type
= LLVMGetElementType(type
);
2468 switch (LLVMGetTypeKind(elem_type
)) {
2470 case LLVMIntegerTypeKind
:
2471 snprintf(buf
, bufsize
, "i%d", LLVMGetIntTypeWidth(elem_type
));
2473 case LLVMFloatTypeKind
:
2474 snprintf(buf
, bufsize
, "f32");
2476 case LLVMDoubleTypeKind
:
2477 snprintf(buf
, bufsize
, "f64");
2482 static void get_image_intr_name(const char *base_name
,
2483 LLVMTypeRef data_type
,
2484 LLVMTypeRef coords_type
,
2485 LLVMTypeRef rsrc_type
,
2486 char *out_name
, unsigned out_len
)
2488 char coords_type_name
[8];
2490 build_type_name_for_intr(coords_type
, coords_type_name
,
2491 sizeof(coords_type_name
));
2493 if (HAVE_LLVM
<= 0x0309) {
2494 snprintf(out_name
, out_len
, "%s.%s", base_name
, coords_type_name
);
2496 char data_type_name
[8];
2497 char rsrc_type_name
[8];
2499 build_type_name_for_intr(data_type
, data_type_name
,
2500 sizeof(data_type_name
));
2501 build_type_name_for_intr(rsrc_type
, rsrc_type_name
,
2502 sizeof(rsrc_type_name
));
2503 snprintf(out_name
, out_len
, "%s.%s.%s.%s", base_name
,
2504 data_type_name
, coords_type_name
, rsrc_type_name
);
2508 static LLVMValueRef
visit_image_load(struct nir_to_llvm_context
*ctx
,
2509 nir_intrinsic_instr
*instr
)
2511 LLVMValueRef params
[7];
2513 char intrinsic_name
[64];
2514 const nir_variable
*var
= instr
->variables
[0]->var
;
2515 const struct glsl_type
*type
= var
->type
;
2516 if(instr
->variables
[0]->deref
.child
)
2517 type
= instr
->variables
[0]->deref
.child
->type
;
2519 type
= glsl_without_array(type
);
2520 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2521 params
[0] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
2522 params
[1] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
2523 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
2524 params
[2] = LLVMConstInt(ctx
->i32
, 0, false); /* voffset */
2525 params
[3] = LLVMConstInt(ctx
->i1
, 0, false); /* glc */
2526 params
[4] = LLVMConstInt(ctx
->i1
, 0, false); /* slc */
2527 res
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.load.format.v4f32", ctx
->v4f32
,
2530 res
= trim_vector(ctx
, res
, instr
->dest
.ssa
.num_components
);
2531 res
= to_integer(ctx
, res
);
2533 bool is_da
= glsl_sampler_type_is_array(type
) ||
2534 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
2535 LLVMValueRef da
= is_da
? ctx
->i32one
: ctx
->i32zero
;
2536 LLVMValueRef glc
= LLVMConstInt(ctx
->i1
, 0, false);
2537 LLVMValueRef slc
= LLVMConstInt(ctx
->i1
, 0, false);
2539 params
[0] = get_image_coords(ctx
, instr
);
2540 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
2541 params
[2] = LLVMConstInt(ctx
->i32
, 15, false); /* dmask */
2542 if (HAVE_LLVM
<= 0x0309) {
2543 params
[3] = LLVMConstInt(ctx
->i1
, 0, false); /* r128 */
2548 LLVMValueRef lwe
= LLVMConstInt(ctx
->i1
, 0, false);
2555 get_image_intr_name("llvm.amdgcn.image.load",
2556 ctx
->v4f32
, /* vdata */
2557 LLVMTypeOf(params
[0]), /* coords */
2558 LLVMTypeOf(params
[1]), /* rsrc */
2559 intrinsic_name
, sizeof(intrinsic_name
));
2561 res
= ac_emit_llvm_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->v4f32
,
2562 params
, 7, AC_FUNC_ATTR_READONLY
);
2564 return to_integer(ctx
, res
);
2567 static void visit_image_store(struct nir_to_llvm_context
*ctx
,
2568 nir_intrinsic_instr
*instr
)
2570 LLVMValueRef params
[8];
2571 char intrinsic_name
[64];
2572 const nir_variable
*var
= instr
->variables
[0]->var
;
2573 LLVMValueRef i1false
= LLVMConstInt(ctx
->i1
, 0, 0);
2574 LLVMValueRef i1true
= LLVMConstInt(ctx
->i1
, 1, 0);
2575 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2577 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
2578 ctx
->shader_info
->fs
.writes_memory
= true;
2580 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2581 params
[0] = to_float(ctx
, get_src(ctx
, instr
->src
[2])); /* data */
2582 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
2583 params
[2] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
2584 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
2585 params
[3] = LLVMConstInt(ctx
->i32
, 0, false); /* voffset */
2586 params
[4] = i1false
; /* glc */
2587 params
[5] = i1false
; /* slc */
2588 ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->voidt
,
2591 bool is_da
= glsl_sampler_type_is_array(type
) ||
2592 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
2593 LLVMValueRef da
= is_da
? i1true
: i1false
;
2594 LLVMValueRef glc
= i1false
;
2595 LLVMValueRef slc
= i1false
;
2597 params
[0] = to_float(ctx
, get_src(ctx
, instr
->src
[2]));
2598 params
[1] = get_image_coords(ctx
, instr
); /* coords */
2599 params
[2] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
2600 params
[3] = LLVMConstInt(ctx
->i32
, 15, false); /* dmask */
2601 if (HAVE_LLVM
<= 0x0309) {
2602 params
[4] = i1false
; /* r128 */
2607 LLVMValueRef lwe
= i1false
;
2614 get_image_intr_name("llvm.amdgcn.image.store",
2615 LLVMTypeOf(params
[0]), /* vdata */
2616 LLVMTypeOf(params
[1]), /* coords */
2617 LLVMTypeOf(params
[2]), /* rsrc */
2618 intrinsic_name
, sizeof(intrinsic_name
));
2620 ac_emit_llvm_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->voidt
,
2626 static LLVMValueRef
visit_image_atomic(struct nir_to_llvm_context
*ctx
,
2627 nir_intrinsic_instr
*instr
)
2629 LLVMValueRef params
[6];
2630 int param_count
= 0;
2631 const nir_variable
*var
= instr
->variables
[0]->var
;
2632 LLVMValueRef i1false
= LLVMConstInt(ctx
->i1
, 0, 0);
2633 LLVMValueRef i1true
= LLVMConstInt(ctx
->i1
, 1, 0);
2634 const char *base_name
= "llvm.amdgcn.image.atomic";
2635 const char *atomic_name
;
2636 LLVMValueRef coords
;
2637 char intrinsic_name
[32], coords_type
[8];
2638 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2640 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
2641 ctx
->shader_info
->fs
.writes_memory
= true;
2643 params
[param_count
++] = get_src(ctx
, instr
->src
[2]);
2644 if (instr
->intrinsic
== nir_intrinsic_image_atomic_comp_swap
)
2645 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
2647 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2648 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
2649 coords
= params
[param_count
++] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
2650 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
2651 params
[param_count
++] = ctx
->i32zero
; /* voffset */
2652 params
[param_count
++] = i1false
; /* glc */
2653 params
[param_count
++] = i1false
; /* slc */
2655 bool da
= glsl_sampler_type_is_array(type
) ||
2656 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
2658 coords
= params
[param_count
++] = get_image_coords(ctx
, instr
);
2659 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
2660 params
[param_count
++] = i1false
; /* r128 */
2661 params
[param_count
++] = da
? i1true
: i1false
; /* da */
2662 params
[param_count
++] = i1false
; /* slc */
2665 switch (instr
->intrinsic
) {
2666 case nir_intrinsic_image_atomic_add
:
2667 atomic_name
= "add";
2669 case nir_intrinsic_image_atomic_min
:
2670 atomic_name
= "smin";
2672 case nir_intrinsic_image_atomic_max
:
2673 atomic_name
= "smax";
2675 case nir_intrinsic_image_atomic_and
:
2676 atomic_name
= "and";
2678 case nir_intrinsic_image_atomic_or
:
2681 case nir_intrinsic_image_atomic_xor
:
2682 atomic_name
= "xor";
2684 case nir_intrinsic_image_atomic_exchange
:
2685 atomic_name
= "swap";
2687 case nir_intrinsic_image_atomic_comp_swap
:
2688 atomic_name
= "cmpswap";
2693 build_int_type_name(LLVMTypeOf(coords
),
2694 coords_type
, sizeof(coords_type
));
2696 snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2697 "%s.%s.%s", base_name
, atomic_name
, coords_type
);
2698 return ac_emit_llvm_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->i32
, params
, param_count
, 0);
2701 static LLVMValueRef
visit_image_size(struct nir_to_llvm_context
*ctx
,
2702 nir_intrinsic_instr
*instr
)
2705 LLVMValueRef params
[10];
2706 const nir_variable
*var
= instr
->variables
[0]->var
;
2707 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
2708 bool da
= glsl_sampler_type_is_array(var
->type
) ||
2709 glsl_get_sampler_dim(var
->type
) == GLSL_SAMPLER_DIM_CUBE
;
2710 if(instr
->variables
[0]->deref
.child
)
2711 type
= instr
->variables
[0]->deref
.child
->type
;
2713 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
2714 return get_buffer_size(ctx
, get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
), true);
2715 params
[0] = ctx
->i32zero
;
2716 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
2717 params
[2] = LLVMConstInt(ctx
->i32
, 15, false);
2718 params
[3] = ctx
->i32zero
;
2719 params
[4] = ctx
->i32zero
;
2720 params
[5] = da
? ctx
->i32one
: ctx
->i32zero
;
2721 params
[6] = ctx
->i32zero
;
2722 params
[7] = ctx
->i32zero
;
2723 params
[8] = ctx
->i32zero
;
2724 params
[9] = ctx
->i32zero
;
2726 res
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.SI.getresinfo.i32", ctx
->v4i32
,
2727 params
, 10, AC_FUNC_ATTR_READNONE
);
2729 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
2730 glsl_sampler_type_is_array(type
)) {
2731 LLVMValueRef two
= LLVMConstInt(ctx
->i32
, 2, false);
2732 LLVMValueRef six
= LLVMConstInt(ctx
->i32
, 6, false);
2733 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->builder
, res
, two
, "");
2734 z
= LLVMBuildSDiv(ctx
->builder
, z
, six
, "");
2735 res
= LLVMBuildInsertElement(ctx
->builder
, res
, z
, two
, "");
2740 static void emit_waitcnt(struct nir_to_llvm_context
*ctx
)
2742 LLVMValueRef args
[1] = {
2743 LLVMConstInt(ctx
->i32
, 0xf70, false),
2745 ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.amdgcn.s.waitcnt",
2746 ctx
->voidt
, args
, 1, 0);
2749 static void emit_barrier(struct nir_to_llvm_context
*ctx
)
2752 ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.amdgcn.s.barrier",
2753 ctx
->voidt
, NULL
, 0, 0);
2756 static void emit_discard_if(struct nir_to_llvm_context
*ctx
,
2757 nir_intrinsic_instr
*instr
)
2760 ctx
->shader_info
->fs
.can_discard
= true;
2762 cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
2763 get_src(ctx
, instr
->src
[0]),
2766 cond
= LLVMBuildSelect(ctx
->builder
, cond
,
2767 LLVMConstReal(ctx
->f32
, -1.0f
),
2769 ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.AMDGPU.kill",
2775 visit_load_local_invocation_index(struct nir_to_llvm_context
*ctx
)
2777 LLVMValueRef result
;
2778 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
2779 result
= LLVMBuildAnd(ctx
->builder
, ctx
->tg_size
,
2780 LLVMConstInt(ctx
->i32
, 0xfc0, false), "");
2782 return LLVMBuildAdd(ctx
->builder
, result
, thread_id
, "");
2785 static LLVMValueRef
visit_var_atomic(struct nir_to_llvm_context
*ctx
,
2786 nir_intrinsic_instr
*instr
)
2788 LLVMValueRef ptr
, result
;
2789 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2790 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
2791 ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
2793 if (instr
->intrinsic
== nir_intrinsic_var_atomic_comp_swap
) {
2794 LLVMValueRef src1
= get_src(ctx
, instr
->src
[1]);
2795 result
= LLVMBuildAtomicCmpXchg(ctx
->builder
,
2797 LLVMAtomicOrderingSequentiallyConsistent
,
2798 LLVMAtomicOrderingSequentiallyConsistent
,
2801 LLVMAtomicRMWBinOp op
;
2802 switch (instr
->intrinsic
) {
2803 case nir_intrinsic_var_atomic_add
:
2804 op
= LLVMAtomicRMWBinOpAdd
;
2806 case nir_intrinsic_var_atomic_umin
:
2807 op
= LLVMAtomicRMWBinOpUMin
;
2809 case nir_intrinsic_var_atomic_umax
:
2810 op
= LLVMAtomicRMWBinOpUMax
;
2812 case nir_intrinsic_var_atomic_imin
:
2813 op
= LLVMAtomicRMWBinOpMin
;
2815 case nir_intrinsic_var_atomic_imax
:
2816 op
= LLVMAtomicRMWBinOpMax
;
2818 case nir_intrinsic_var_atomic_and
:
2819 op
= LLVMAtomicRMWBinOpAnd
;
2821 case nir_intrinsic_var_atomic_or
:
2822 op
= LLVMAtomicRMWBinOpOr
;
2824 case nir_intrinsic_var_atomic_xor
:
2825 op
= LLVMAtomicRMWBinOpXor
;
2827 case nir_intrinsic_var_atomic_exchange
:
2828 op
= LLVMAtomicRMWBinOpXchg
;
2834 result
= LLVMBuildAtomicRMW(ctx
->builder
, op
, ptr
, to_integer(ctx
, src
),
2835 LLVMAtomicOrderingSequentiallyConsistent
,
2841 #define INTERP_CENTER 0
2842 #define INTERP_CENTROID 1
2843 #define INTERP_SAMPLE 2
2845 static LLVMValueRef
lookup_interp_param(struct nir_to_llvm_context
*ctx
,
2846 enum glsl_interp_mode interp
, unsigned location
)
2849 case INTERP_MODE_FLAT
:
2852 case INTERP_MODE_SMOOTH
:
2853 case INTERP_MODE_NONE
:
2854 if (location
== INTERP_CENTER
)
2855 return ctx
->persp_center
;
2856 else if (location
== INTERP_CENTROID
)
2857 return ctx
->persp_centroid
;
2858 else if (location
== INTERP_SAMPLE
)
2859 return ctx
->persp_sample
;
2861 case INTERP_MODE_NOPERSPECTIVE
:
2862 if (location
== INTERP_CENTER
)
2863 return ctx
->linear_center
;
2864 else if (location
== INTERP_CENTROID
)
2865 return ctx
->linear_centroid
;
2866 else if (location
== INTERP_SAMPLE
)
2867 return ctx
->linear_sample
;
2873 static LLVMValueRef
load_sample_position(struct nir_to_llvm_context
*ctx
,
2874 LLVMValueRef sample_id
)
2876 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
2877 LLVMValueRef offset0
= LLVMBuildMul(ctx
->builder
, sample_id
, LLVMConstInt(ctx
->i32
, 8, false), "");
2878 LLVMValueRef offset1
= LLVMBuildAdd(ctx
->builder
, offset0
, LLVMConstInt(ctx
->i32
, 4, false), "");
2879 LLVMValueRef result
[2];
2881 result
[0] = ac_build_indexed_load_const(&ctx
->ac
, ctx
->sample_positions
, offset0
);
2882 result
[1] = ac_build_indexed_load_const(&ctx
->ac
, ctx
->sample_positions
, offset1
);
2884 return ac_build_gather_values(&ctx
->ac
, result
, 2);
2887 static LLVMValueRef
load_sample_pos(struct nir_to_llvm_context
*ctx
)
2889 LLVMValueRef values
[2];
2891 values
[0] = emit_ffract(ctx
, ctx
->frag_pos
[0]);
2892 values
[1] = emit_ffract(ctx
, ctx
->frag_pos
[1]);
2893 return ac_build_gather_values(&ctx
->ac
, values
, 2);
2896 static LLVMValueRef
visit_interp(struct nir_to_llvm_context
*ctx
,
2897 nir_intrinsic_instr
*instr
)
2899 LLVMValueRef result
[2];
2900 LLVMValueRef interp_param
, attr_number
;
2903 LLVMValueRef src_c0
, src_c1
;
2904 const char *intr_name
;
2906 int input_index
= instr
->variables
[0]->var
->data
.location
- VARYING_SLOT_VAR0
;
2907 switch (instr
->intrinsic
) {
2908 case nir_intrinsic_interp_var_at_centroid
:
2909 location
= INTERP_CENTROID
;
2911 case nir_intrinsic_interp_var_at_sample
:
2912 case nir_intrinsic_interp_var_at_offset
:
2913 location
= INTERP_SAMPLE
;
2914 src0
= get_src(ctx
, instr
->src
[0]);
2920 if (instr
->intrinsic
== nir_intrinsic_interp_var_at_offset
) {
2921 src_c0
= to_float(ctx
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32zero
, ""));
2922 src_c1
= to_float(ctx
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32one
, ""));
2923 } else if (instr
->intrinsic
== nir_intrinsic_interp_var_at_sample
) {
2924 LLVMValueRef sample_position
;
2925 LLVMValueRef halfval
= LLVMConstReal(ctx
->f32
, 0.5f
);
2927 /* fetch sample ID */
2928 sample_position
= load_sample_position(ctx
, src0
);
2930 src_c0
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->i32zero
, "");
2931 src_c0
= LLVMBuildFSub(ctx
->builder
, src_c0
, halfval
, "");
2932 src_c1
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->i32one
, "");
2933 src_c1
= LLVMBuildFSub(ctx
->builder
, src_c1
, halfval
, "");
2935 interp_param
= lookup_interp_param(ctx
, instr
->variables
[0]->var
->data
.interpolation
, location
);
2936 attr_number
= LLVMConstInt(ctx
->i32
, input_index
, false);
2938 if (location
== INTERP_SAMPLE
) {
2939 LLVMValueRef ij_out
[2];
2940 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
, interp_param
);
2943 * take the I then J parameters, and the DDX/Y for it, and
2944 * calculate the IJ inputs for the interpolator.
2945 * temp1 = ddx * offset/sample.x + I;
2946 * interp_param.I = ddy * offset/sample.y + temp1;
2947 * temp1 = ddx * offset/sample.x + J;
2948 * interp_param.J = ddy * offset/sample.y + temp1;
2950 for (unsigned i
= 0; i
< 2; i
++) {
2951 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->i32
, i
, false);
2952 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->i32
, i
+ 2, false);
2953 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->builder
,
2954 ddxy_out
, ix_ll
, "");
2955 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->builder
,
2956 ddxy_out
, iy_ll
, "");
2957 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->builder
,
2958 interp_param
, ix_ll
, "");
2959 LLVMValueRef temp1
, temp2
;
2961 interp_el
= LLVMBuildBitCast(ctx
->builder
, interp_el
,
2964 temp1
= LLVMBuildFMul(ctx
->builder
, ddx_el
, src_c0
, "");
2965 temp1
= LLVMBuildFAdd(ctx
->builder
, temp1
, interp_el
, "");
2967 temp2
= LLVMBuildFMul(ctx
->builder
, ddy_el
, src_c1
, "");
2968 temp2
= LLVMBuildFAdd(ctx
->builder
, temp2
, temp1
, "");
2970 ij_out
[i
] = LLVMBuildBitCast(ctx
->builder
,
2971 temp2
, ctx
->i32
, "");
2973 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
2976 intr_name
= interp_param
? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
2977 for (chan
= 0; chan
< 2; chan
++) {
2978 LLVMValueRef args
[4];
2979 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
2981 args
[0] = llvm_chan
;
2982 args
[1] = attr_number
;
2983 args
[2] = ctx
->prim_mask
;
2984 args
[3] = interp_param
;
2985 result
[chan
] = ac_emit_llvm_intrinsic(&ctx
->ac
, intr_name
,
2986 ctx
->f32
, args
, args
[3] ? 4 : 3,
2987 AC_FUNC_ATTR_READNONE
);
2989 return ac_build_gather_values(&ctx
->ac
, result
, 2);
2993 visit_emit_vertex(struct nir_to_llvm_context
*ctx
,
2994 nir_intrinsic_instr
*instr
)
2996 LLVMValueRef gs_next_vertex
;
2997 LLVMValueRef can_emit
, kill
;
2998 LLVMValueRef args
[2];
3001 assert(instr
->const_index
[0] == 0);
3002 /* Write vertex attribute values to GSVS ring */
3003 gs_next_vertex
= LLVMBuildLoad(ctx
->builder
,
3004 ctx
->gs_next_vertex
,
3007 /* If this thread has already emitted the declared maximum number of
3008 * vertices, kill it: excessive vertex emissions are not supposed to
3009 * have any effect, and GS threads have no externally observable
3010 * effects other than emitting vertices.
3012 can_emit
= LLVMBuildICmp(ctx
->builder
, LLVMIntULT
, gs_next_vertex
,
3013 LLVMConstInt(ctx
->i32
, ctx
->gs_max_out_vertices
, false), "");
3015 kill
= LLVMBuildSelect(ctx
->builder
, can_emit
,
3016 LLVMConstReal(ctx
->f32
, 1.0f
),
3017 LLVMConstReal(ctx
->f32
, -1.0f
), "");
3018 ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.AMDGPU.kill",
3019 ctx
->voidt
, &kill
, 1, 0);
3021 /* loop num outputs */
3023 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
3024 LLVMValueRef
*out_ptr
= &ctx
->outputs
[i
* 4];
3025 if (!(ctx
->output_mask
& (1ull << i
)))
3028 for (unsigned j
= 0; j
< 4; j
++) {
3029 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
,
3031 LLVMValueRef voffset
= LLVMConstInt(ctx
->i32
, (idx
* 4 + j
) * ctx
->gs_max_out_vertices
, false);
3032 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, gs_next_vertex
, "");
3033 voffset
= LLVMBuildMul(ctx
->builder
, voffset
, LLVMConstInt(ctx
->i32
, 4, false), "");
3035 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->i32
, "");
3037 ac_build_tbuffer_store(&ctx
->ac
, ctx
->gsvs_ring
,
3039 voffset
, ctx
->gs2vs_offset
, 0,
3040 V_008F0C_BUF_DATA_FORMAT_32
,
3041 V_008F0C_BUF_NUM_FORMAT_UINT
,
3047 gs_next_vertex
= LLVMBuildAdd(ctx
->builder
, gs_next_vertex
,
3049 LLVMBuildStore(ctx
->builder
, gs_next_vertex
, ctx
->gs_next_vertex
);
3050 args
[0] = LLVMConstInt(ctx
->i32
, SENDMSG_GS_OP_EMIT
| SENDMSG_GS
| (0 << 8), false);
3051 args
[1] = ctx
->gs_wave_id
;
3052 ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.SI.sendmsg",
3053 ctx
->voidt
, args
, 2, 0);
3057 visit_end_primitive(struct nir_to_llvm_context
*ctx
,
3058 nir_intrinsic_instr
*instr
)
3060 LLVMValueRef args
[2];
3062 assert(instr
->const_index
[0] == 0);
3063 args
[0] = LLVMConstInt(ctx
->i32
, SENDMSG_GS_OP_CUT
| SENDMSG_GS
| (0 << 8), false);
3064 args
[1] = ctx
->gs_wave_id
;
3066 ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.SI.sendmsg", ctx
->voidt
,
3070 static void visit_intrinsic(struct nir_to_llvm_context
*ctx
,
3071 nir_intrinsic_instr
*instr
)
3073 LLVMValueRef result
= NULL
;
3075 switch (instr
->intrinsic
) {
3076 case nir_intrinsic_load_work_group_id
: {
3077 result
= ctx
->workgroup_ids
;
3080 case nir_intrinsic_load_base_vertex
: {
3081 result
= ctx
->base_vertex
;
3084 case nir_intrinsic_load_vertex_id_zero_base
: {
3085 result
= ctx
->vertex_id
;
3088 case nir_intrinsic_load_local_invocation_id
: {
3089 result
= ctx
->local_invocation_ids
;
3092 case nir_intrinsic_load_base_instance
:
3093 result
= ctx
->start_instance
;
3095 case nir_intrinsic_load_draw_id
:
3096 result
= ctx
->draw_index
;
3098 case nir_intrinsic_load_invocation_id
:
3099 result
= ctx
->gs_invocation_id
;
3101 case nir_intrinsic_load_primitive_id
:
3102 if (ctx
->stage
== MESA_SHADER_GEOMETRY
)
3103 result
= ctx
->gs_prim_id
;
3105 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
3107 case nir_intrinsic_load_sample_id
:
3108 ctx
->shader_info
->fs
.force_persample
= true;
3109 result
= unpack_param(ctx
, ctx
->ancillary
, 8, 4);
3111 case nir_intrinsic_load_sample_pos
:
3112 ctx
->shader_info
->fs
.force_persample
= true;
3113 result
= load_sample_pos(ctx
);
3115 case nir_intrinsic_load_front_face
:
3116 result
= ctx
->front_face
;
3118 case nir_intrinsic_load_instance_id
:
3119 result
= ctx
->instance_id
;
3120 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
3121 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
3123 case nir_intrinsic_load_num_work_groups
:
3124 result
= ctx
->num_work_groups
;
3126 case nir_intrinsic_load_local_invocation_index
:
3127 result
= visit_load_local_invocation_index(ctx
);
3129 case nir_intrinsic_load_push_constant
:
3130 result
= visit_load_push_constant(ctx
, instr
);
3132 case nir_intrinsic_vulkan_resource_index
:
3133 result
= visit_vulkan_resource_index(ctx
, instr
);
3135 case nir_intrinsic_store_ssbo
:
3136 visit_store_ssbo(ctx
, instr
);
3138 case nir_intrinsic_load_ssbo
:
3139 result
= visit_load_buffer(ctx
, instr
);
3141 case nir_intrinsic_ssbo_atomic_add
:
3142 case nir_intrinsic_ssbo_atomic_imin
:
3143 case nir_intrinsic_ssbo_atomic_umin
:
3144 case nir_intrinsic_ssbo_atomic_imax
:
3145 case nir_intrinsic_ssbo_atomic_umax
:
3146 case nir_intrinsic_ssbo_atomic_and
:
3147 case nir_intrinsic_ssbo_atomic_or
:
3148 case nir_intrinsic_ssbo_atomic_xor
:
3149 case nir_intrinsic_ssbo_atomic_exchange
:
3150 case nir_intrinsic_ssbo_atomic_comp_swap
:
3151 result
= visit_atomic_ssbo(ctx
, instr
);
3153 case nir_intrinsic_load_ubo
:
3154 result
= visit_load_ubo_buffer(ctx
, instr
);
3156 case nir_intrinsic_get_buffer_size
:
3157 result
= visit_get_buffer_size(ctx
, instr
);
3159 case nir_intrinsic_load_var
:
3160 result
= visit_load_var(ctx
, instr
);
3162 case nir_intrinsic_store_var
:
3163 visit_store_var(ctx
, instr
);
3165 case nir_intrinsic_image_load
:
3166 result
= visit_image_load(ctx
, instr
);
3168 case nir_intrinsic_image_store
:
3169 visit_image_store(ctx
, instr
);
3171 case nir_intrinsic_image_atomic_add
:
3172 case nir_intrinsic_image_atomic_min
:
3173 case nir_intrinsic_image_atomic_max
:
3174 case nir_intrinsic_image_atomic_and
:
3175 case nir_intrinsic_image_atomic_or
:
3176 case nir_intrinsic_image_atomic_xor
:
3177 case nir_intrinsic_image_atomic_exchange
:
3178 case nir_intrinsic_image_atomic_comp_swap
:
3179 result
= visit_image_atomic(ctx
, instr
);
3181 case nir_intrinsic_image_size
:
3182 result
= visit_image_size(ctx
, instr
);
3184 case nir_intrinsic_discard
:
3185 ctx
->shader_info
->fs
.can_discard
= true;
3186 ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.AMDGPU.kilp",
3190 case nir_intrinsic_discard_if
:
3191 emit_discard_if(ctx
, instr
);
3193 case nir_intrinsic_memory_barrier
:
3196 case nir_intrinsic_barrier
:
3199 case nir_intrinsic_var_atomic_add
:
3200 case nir_intrinsic_var_atomic_imin
:
3201 case nir_intrinsic_var_atomic_umin
:
3202 case nir_intrinsic_var_atomic_imax
:
3203 case nir_intrinsic_var_atomic_umax
:
3204 case nir_intrinsic_var_atomic_and
:
3205 case nir_intrinsic_var_atomic_or
:
3206 case nir_intrinsic_var_atomic_xor
:
3207 case nir_intrinsic_var_atomic_exchange
:
3208 case nir_intrinsic_var_atomic_comp_swap
:
3209 result
= visit_var_atomic(ctx
, instr
);
3211 case nir_intrinsic_interp_var_at_centroid
:
3212 case nir_intrinsic_interp_var_at_sample
:
3213 case nir_intrinsic_interp_var_at_offset
:
3214 result
= visit_interp(ctx
, instr
);
3216 case nir_intrinsic_emit_vertex
:
3217 visit_emit_vertex(ctx
, instr
);
3219 case nir_intrinsic_end_primitive
:
3220 visit_end_primitive(ctx
, instr
);
3223 fprintf(stderr
, "Unknown intrinsic: ");
3224 nir_print_instr(&instr
->instr
, stderr
);
3225 fprintf(stderr
, "\n");
3229 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
3233 static LLVMValueRef
get_sampler_desc(struct nir_to_llvm_context
*ctx
,
3234 nir_deref_var
*deref
,
3235 enum desc_type desc_type
)
3237 unsigned desc_set
= deref
->var
->data
.descriptor_set
;
3238 LLVMValueRef list
= ctx
->descriptor_sets
[desc_set
];
3239 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[desc_set
].layout
;
3240 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ deref
->var
->data
.binding
;
3241 unsigned offset
= binding
->offset
;
3242 unsigned stride
= binding
->size
;
3244 LLVMBuilderRef builder
= ctx
->builder
;
3246 LLVMValueRef index
= NULL
;
3248 assert(deref
->var
->data
.binding
< layout
->binding_count
);
3250 switch (desc_type
) {
3262 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
3272 unreachable("invalid desc_type\n");
3275 if (deref
->deref
.child
) {
3276 nir_deref_array
*child
= (nir_deref_array
*)deref
->deref
.child
;
3278 assert(child
->deref_array_type
!= nir_deref_array_type_wildcard
);
3279 offset
+= child
->base_offset
* stride
;
3280 if (child
->deref_array_type
== nir_deref_array_type_indirect
) {
3281 index
= get_src(ctx
, child
->indirect
);
3285 assert(stride
% type_size
== 0);
3288 index
= ctx
->i32zero
;
3290 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, stride
/ type_size
, 0), "");
3292 list
= ac_build_gep0(&ctx
->ac
, list
, LLVMConstInt(ctx
->i32
, offset
, 0));
3293 list
= LLVMBuildPointerCast(builder
, list
, const_array(type
, 0), "");
3295 return ac_build_indexed_load_const(&ctx
->ac
, list
, index
);
3298 static void set_tex_fetch_args(struct nir_to_llvm_context
*ctx
,
3299 struct ac_tex_info
*tinfo
,
3300 nir_tex_instr
*instr
,
3302 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
3303 LLVMValueRef
*param
, unsigned count
,
3307 unsigned is_rect
= 0;
3308 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
3310 if (op
== nir_texop_lod
)
3312 /* Pad to power of two vector */
3313 while (count
< util_next_power_of_two(count
))
3314 param
[count
++] = LLVMGetUndef(ctx
->i32
);
3317 tinfo
->args
[0] = ac_build_gather_values(&ctx
->ac
, param
, count
);
3319 tinfo
->args
[0] = param
[0];
3321 tinfo
->args
[1] = res_ptr
;
3324 if (op
== nir_texop_txf
||
3325 op
== nir_texop_txf_ms
||
3326 op
== nir_texop_query_levels
||
3327 op
== nir_texop_texture_samples
||
3328 op
== nir_texop_txs
)
3329 tinfo
->dst_type
= ctx
->v4i32
;
3331 tinfo
->dst_type
= ctx
->v4f32
;
3332 tinfo
->args
[num_args
++] = samp_ptr
;
3335 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
&& op
== nir_texop_txf
) {
3336 tinfo
->args
[0] = res_ptr
;
3337 tinfo
->args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
3338 tinfo
->args
[2] = param
[0];
3339 tinfo
->arg_count
= 3;
3343 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, dmask
, 0);
3344 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, is_rect
, 0); /* unorm */
3345 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* r128 */
3346 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, da
? 1 : 0, 0);
3347 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* glc */
3348 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* slc */
3349 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* tfe */
3350 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* lwe */
3352 tinfo
->arg_count
= num_args
;
3355 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
3358 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
3359 * filtering manually. The driver sets img7 to a mask clearing
3360 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
3361 * s_and_b32 samp0, samp0, img7
3364 * The ANISO_OVERRIDE sampler field enables this fix in TA.
3366 static LLVMValueRef
sici_fix_sampler_aniso(struct nir_to_llvm_context
*ctx
,
3367 LLVMValueRef res
, LLVMValueRef samp
)
3369 LLVMBuilderRef builder
= ctx
->builder
;
3370 LLVMValueRef img7
, samp0
;
3372 if (ctx
->options
->chip_class
>= VI
)
3375 img7
= LLVMBuildExtractElement(builder
, res
,
3376 LLVMConstInt(ctx
->i32
, 7, 0), "");
3377 samp0
= LLVMBuildExtractElement(builder
, samp
,
3378 LLVMConstInt(ctx
->i32
, 0, 0), "");
3379 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
3380 return LLVMBuildInsertElement(builder
, samp
, samp0
,
3381 LLVMConstInt(ctx
->i32
, 0, 0), "");
3384 static void tex_fetch_ptrs(struct nir_to_llvm_context
*ctx
,
3385 nir_tex_instr
*instr
,
3386 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
3387 LLVMValueRef
*fmask_ptr
)
3389 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
3390 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_BUFFER
);
3392 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_IMAGE
);
3395 *samp_ptr
= get_sampler_desc(ctx
, instr
->sampler
, DESC_SAMPLER
);
3397 *samp_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_SAMPLER
);
3398 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
3399 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
3401 if (fmask_ptr
&& !instr
->sampler
&& (instr
->op
== nir_texop_txf_ms
||
3402 instr
->op
== nir_texop_samples_identical
))
3403 *fmask_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_FMASK
);
3406 static LLVMValueRef
apply_round_slice(struct nir_to_llvm_context
*ctx
,
3409 coord
= to_float(ctx
, coord
);
3410 coord
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.rint.f32", ctx
->f32
, &coord
, 1, 0);
3411 coord
= to_integer(ctx
, coord
);
3415 static void visit_tex(struct nir_to_llvm_context
*ctx
, nir_tex_instr
*instr
)
3417 LLVMValueRef result
= NULL
;
3418 struct ac_tex_info tinfo
= { 0 };
3419 unsigned dmask
= 0xf;
3420 LLVMValueRef address
[16];
3421 LLVMValueRef coords
[5];
3422 LLVMValueRef coord
= NULL
, lod
= NULL
, comparator
= NULL
;
3423 LLVMValueRef bias
= NULL
, offsets
= NULL
;
3424 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
, sample_index
= NULL
;
3425 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
3426 LLVMValueRef derivs
[6];
3427 unsigned chan
, count
= 0;
3428 unsigned const_src
= 0, num_deriv_comp
= 0;
3430 tex_fetch_ptrs(ctx
, instr
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
3432 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3433 switch (instr
->src
[i
].src_type
) {
3434 case nir_tex_src_coord
:
3435 coord
= get_src(ctx
, instr
->src
[i
].src
);
3437 case nir_tex_src_projector
:
3439 case nir_tex_src_comparator
:
3440 comparator
= get_src(ctx
, instr
->src
[i
].src
);
3442 case nir_tex_src_offset
:
3443 offsets
= get_src(ctx
, instr
->src
[i
].src
);
3446 case nir_tex_src_bias
:
3447 bias
= get_src(ctx
, instr
->src
[i
].src
);
3449 case nir_tex_src_lod
:
3450 lod
= get_src(ctx
, instr
->src
[i
].src
);
3452 case nir_tex_src_ms_index
:
3453 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
3455 case nir_tex_src_ms_mcs
:
3457 case nir_tex_src_ddx
:
3458 ddx
= get_src(ctx
, instr
->src
[i
].src
);
3459 num_deriv_comp
= instr
->src
[i
].src
.ssa
->num_components
;
3461 case nir_tex_src_ddy
:
3462 ddy
= get_src(ctx
, instr
->src
[i
].src
);
3464 case nir_tex_src_texture_offset
:
3465 case nir_tex_src_sampler_offset
:
3466 case nir_tex_src_plane
:
3472 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
3473 result
= get_buffer_size(ctx
, res_ptr
, false);
3477 if (instr
->op
== nir_texop_texture_samples
) {
3478 LLVMValueRef res
, samples
, is_msaa
;
3479 res
= LLVMBuildBitCast(ctx
->builder
, res_ptr
, ctx
->v8i32
, "");
3480 samples
= LLVMBuildExtractElement(ctx
->builder
, res
,
3481 LLVMConstInt(ctx
->i32
, 3, false), "");
3482 is_msaa
= LLVMBuildLShr(ctx
->builder
, samples
,
3483 LLVMConstInt(ctx
->i32
, 28, false), "");
3484 is_msaa
= LLVMBuildAnd(ctx
->builder
, is_msaa
,
3485 LLVMConstInt(ctx
->i32
, 0xe, false), "");
3486 is_msaa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, is_msaa
,
3487 LLVMConstInt(ctx
->i32
, 0xe, false), "");
3489 samples
= LLVMBuildLShr(ctx
->builder
, samples
,
3490 LLVMConstInt(ctx
->i32
, 16, false), "");
3491 samples
= LLVMBuildAnd(ctx
->builder
, samples
,
3492 LLVMConstInt(ctx
->i32
, 0xf, false), "");
3493 samples
= LLVMBuildShl(ctx
->builder
, ctx
->i32one
,
3495 samples
= LLVMBuildSelect(ctx
->builder
, is_msaa
, samples
,
3502 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
3503 coords
[chan
] = llvm_extract_elem(ctx
, coord
, chan
);
3505 if (offsets
&& instr
->op
!= nir_texop_txf
) {
3506 LLVMValueRef offset
[3], pack
;
3507 for (chan
= 0; chan
< 3; ++chan
)
3508 offset
[chan
] = ctx
->i32zero
;
3510 tinfo
.has_offset
= true;
3511 for (chan
= 0; chan
< get_llvm_num_components(offsets
); chan
++) {
3512 offset
[chan
] = llvm_extract_elem(ctx
, offsets
, chan
);
3513 offset
[chan
] = LLVMBuildAnd(ctx
->builder
, offset
[chan
],
3514 LLVMConstInt(ctx
->i32
, 0x3f, false), "");
3516 offset
[chan
] = LLVMBuildShl(ctx
->builder
, offset
[chan
],
3517 LLVMConstInt(ctx
->i32
, chan
* 8, false), "");
3519 pack
= LLVMBuildOr(ctx
->builder
, offset
[0], offset
[1], "");
3520 pack
= LLVMBuildOr(ctx
->builder
, pack
, offset
[2], "");
3521 address
[count
++] = pack
;
3524 /* pack LOD bias value */
3525 if (instr
->op
== nir_texop_txb
&& bias
) {
3526 address
[count
++] = bias
;
3529 /* Pack depth comparison value */
3530 if (instr
->is_shadow
&& comparator
) {
3531 address
[count
++] = llvm_extract_elem(ctx
, comparator
, 0);
3534 /* pack derivatives */
3536 switch (instr
->sampler_dim
) {
3537 case GLSL_SAMPLER_DIM_3D
:
3538 case GLSL_SAMPLER_DIM_CUBE
:
3541 case GLSL_SAMPLER_DIM_2D
:
3545 case GLSL_SAMPLER_DIM_1D
:
3550 for (unsigned i
= 0; i
< num_deriv_comp
; i
++) {
3551 derivs
[i
* 2] = to_float(ctx
, llvm_extract_elem(ctx
, ddx
, i
));
3552 derivs
[i
* 2 + 1] = to_float(ctx
, llvm_extract_elem(ctx
, ddy
, i
));
3556 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& coord
) {
3557 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
3558 coords
[chan
] = to_float(ctx
, coords
[chan
]);
3559 if (instr
->coord_components
== 3)
3560 coords
[3] = LLVMGetUndef(ctx
->f32
);
3561 ac_prepare_cube_coords(&ctx
->ac
,
3562 instr
->op
== nir_texop_txd
, instr
->is_array
,
3569 for (unsigned i
= 0; i
< num_deriv_comp
* 2; i
++)
3570 address
[count
++] = derivs
[i
];
3573 /* Pack texture coordinates */
3575 address
[count
++] = coords
[0];
3576 if (instr
->coord_components
> 1) {
3577 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&& instr
->is_array
&& instr
->op
!= nir_texop_txf
) {
3578 coords
[1] = apply_round_slice(ctx
, coords
[1]);
3580 address
[count
++] = coords
[1];
3582 if (instr
->coord_components
> 2) {
3583 /* This seems like a bit of a hack - but it passes Vulkan CTS with it */
3584 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_3D
&& instr
->op
!= nir_texop_txf
) {
3585 coords
[2] = apply_round_slice(ctx
, coords
[2]);
3587 address
[count
++] = coords
[2];
3592 if ((instr
->op
== nir_texop_txl
|| instr
->op
== nir_texop_txf
) && lod
) {
3593 address
[count
++] = lod
;
3594 } else if (instr
->op
== nir_texop_txf_ms
&& sample_index
) {
3595 address
[count
++] = sample_index
;
3596 } else if(instr
->op
== nir_texop_txs
) {
3599 address
[count
++] = lod
;
3601 address
[count
++] = ctx
->i32zero
;
3604 for (chan
= 0; chan
< count
; chan
++) {
3605 address
[chan
] = LLVMBuildBitCast(ctx
->builder
,
3606 address
[chan
], ctx
->i32
, "");
3609 if (instr
->op
== nir_texop_samples_identical
) {
3610 LLVMValueRef txf_address
[4];
3611 struct ac_tex_info txf_info
= { 0 };
3612 unsigned txf_count
= count
;
3613 memcpy(txf_address
, address
, sizeof(txf_address
));
3615 if (!instr
->is_array
)
3616 txf_address
[2] = ctx
->i32zero
;
3617 txf_address
[3] = ctx
->i32zero
;
3619 set_tex_fetch_args(ctx
, &txf_info
, instr
, nir_texop_txf
,
3621 txf_address
, txf_count
, 0xf);
3623 result
= build_tex_intrinsic(ctx
, instr
, &txf_info
);
3625 result
= LLVMBuildExtractElement(ctx
->builder
, result
, ctx
->i32zero
, "");
3626 result
= emit_int_cmp(ctx
, LLVMIntEQ
, result
, ctx
->i32zero
);
3630 /* Adjust the sample index according to FMASK.
3632 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3633 * which is the identity mapping. Each nibble says which physical sample
3634 * should be fetched to get that sample.
3636 * For example, 0x11111100 means there are only 2 samples stored and
3637 * the second sample covers 3/4 of the pixel. When reading samples 0
3638 * and 1, return physical sample 0 (determined by the first two 0s
3639 * in FMASK), otherwise return physical sample 1.
3641 * The sample index should be adjusted as follows:
3642 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
3644 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
) {
3645 LLVMValueRef txf_address
[4];
3646 struct ac_tex_info txf_info
= { 0 };
3647 unsigned txf_count
= count
;
3648 memcpy(txf_address
, address
, sizeof(txf_address
));
3650 if (!instr
->is_array
)
3651 txf_address
[2] = ctx
->i32zero
;
3652 txf_address
[3] = ctx
->i32zero
;
3654 set_tex_fetch_args(ctx
, &txf_info
, instr
, nir_texop_txf
,
3656 txf_address
, txf_count
, 0xf);
3658 result
= build_tex_intrinsic(ctx
, instr
, &txf_info
);
3659 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
3660 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
3662 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
3666 unsigned sample_chan
= instr
->is_array
? 3 : 2;
3668 LLVMValueRef sample_index4
=
3669 LLVMBuildMul(ctx
->builder
, address
[sample_chan
], four
, "");
3670 LLVMValueRef shifted_fmask
=
3671 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
3672 LLVMValueRef final_sample
=
3673 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
3675 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3676 * resource descriptor is 0 (invalid),
3678 LLVMValueRef fmask_desc
=
3679 LLVMBuildBitCast(ctx
->builder
, fmask_ptr
,
3682 LLVMValueRef fmask_word1
=
3683 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
3686 LLVMValueRef word1_is_nonzero
=
3687 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3688 fmask_word1
, ctx
->i32zero
, "");
3690 /* Replace the MSAA sample index. */
3691 address
[sample_chan
] =
3692 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
3693 final_sample
, address
[sample_chan
], "");
3696 if (offsets
&& instr
->op
== nir_texop_txf
) {
3697 nir_const_value
*const_offset
=
3698 nir_src_as_const_value(instr
->src
[const_src
].src
);
3699 int num_offsets
= instr
->src
[const_src
].src
.ssa
->num_components
;
3700 assert(const_offset
);
3701 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
3702 if (num_offsets
> 2)
3703 address
[2] = LLVMBuildAdd(ctx
->builder
,
3704 address
[2], LLVMConstInt(ctx
->i32
, const_offset
->i32
[2], false), "");
3705 if (num_offsets
> 1)
3706 address
[1] = LLVMBuildAdd(ctx
->builder
,
3707 address
[1], LLVMConstInt(ctx
->i32
, const_offset
->i32
[1], false), "");
3708 address
[0] = LLVMBuildAdd(ctx
->builder
,
3709 address
[0], LLVMConstInt(ctx
->i32
, const_offset
->i32
[0], false), "");
3713 /* TODO TG4 support */
3714 if (instr
->op
== nir_texop_tg4
) {
3715 if (instr
->is_shadow
)
3718 dmask
= 1 << instr
->component
;
3720 set_tex_fetch_args(ctx
, &tinfo
, instr
, instr
->op
,
3721 res_ptr
, samp_ptr
, address
, count
, dmask
);
3723 result
= build_tex_intrinsic(ctx
, instr
, &tinfo
);
3725 if (instr
->op
== nir_texop_query_levels
)
3726 result
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, 3, false), "");
3727 else if (instr
->is_shadow
&& instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&& instr
->op
!= nir_texop_tg4
)
3728 result
= LLVMBuildExtractElement(ctx
->builder
, result
, ctx
->i32zero
, "");
3729 else if (instr
->op
== nir_texop_txs
&&
3730 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
3732 LLVMValueRef two
= LLVMConstInt(ctx
->i32
, 2, false);
3733 LLVMValueRef six
= LLVMConstInt(ctx
->i32
, 6, false);
3734 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->builder
, result
, two
, "");
3735 z
= LLVMBuildSDiv(ctx
->builder
, z
, six
, "");
3736 result
= LLVMBuildInsertElement(ctx
->builder
, result
, z
, two
, "");
3737 } else if (instr
->dest
.ssa
.num_components
!= 4)
3738 result
= trim_vector(ctx
, result
, instr
->dest
.ssa
.num_components
);
3742 assert(instr
->dest
.is_ssa
);
3743 result
= to_integer(ctx
, result
);
3744 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
3749 static void visit_phi(struct nir_to_llvm_context
*ctx
, nir_phi_instr
*instr
)
3751 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
3752 LLVMValueRef result
= LLVMBuildPhi(ctx
->builder
, type
, "");
3754 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
3755 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
3758 static void visit_post_phi(struct nir_to_llvm_context
*ctx
,
3759 nir_phi_instr
*instr
,
3760 LLVMValueRef llvm_phi
)
3762 nir_foreach_phi_src(src
, instr
) {
3763 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
3764 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
3766 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
3770 static void phi_post_pass(struct nir_to_llvm_context
*ctx
)
3772 struct hash_entry
*entry
;
3773 hash_table_foreach(ctx
->phis
, entry
) {
3774 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
3775 (LLVMValueRef
)entry
->data
);
3780 static void visit_ssa_undef(struct nir_to_llvm_context
*ctx
,
3781 nir_ssa_undef_instr
*instr
)
3783 unsigned num_components
= instr
->def
.num_components
;
3786 if (num_components
== 1)
3787 undef
= LLVMGetUndef(ctx
->i32
);
3789 undef
= LLVMGetUndef(LLVMVectorType(ctx
->i32
, num_components
));
3791 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, undef
);
3794 static void visit_jump(struct nir_to_llvm_context
*ctx
,
3795 nir_jump_instr
*instr
)
3797 switch (instr
->type
) {
3798 case nir_jump_break
:
3799 LLVMBuildBr(ctx
->builder
, ctx
->break_block
);
3800 LLVMClearInsertionPosition(ctx
->builder
);
3802 case nir_jump_continue
:
3803 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
3804 LLVMClearInsertionPosition(ctx
->builder
);
3807 fprintf(stderr
, "Unknown NIR jump instr: ");
3808 nir_print_instr(&instr
->instr
, stderr
);
3809 fprintf(stderr
, "\n");
3814 static void visit_cf_list(struct nir_to_llvm_context
*ctx
,
3815 struct exec_list
*list
);
3817 static void visit_block(struct nir_to_llvm_context
*ctx
, nir_block
*block
)
3819 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->builder
);
3820 nir_foreach_instr(instr
, block
)
3822 switch (instr
->type
) {
3823 case nir_instr_type_alu
:
3824 visit_alu(ctx
, nir_instr_as_alu(instr
));
3826 case nir_instr_type_load_const
:
3827 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
3829 case nir_instr_type_intrinsic
:
3830 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
3832 case nir_instr_type_tex
:
3833 visit_tex(ctx
, nir_instr_as_tex(instr
));
3835 case nir_instr_type_phi
:
3836 visit_phi(ctx
, nir_instr_as_phi(instr
));
3838 case nir_instr_type_ssa_undef
:
3839 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
3841 case nir_instr_type_jump
:
3842 visit_jump(ctx
, nir_instr_as_jump(instr
));
3845 fprintf(stderr
, "Unknown NIR instr type: ");
3846 nir_print_instr(instr
, stderr
);
3847 fprintf(stderr
, "\n");
3852 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
3855 static void visit_if(struct nir_to_llvm_context
*ctx
, nir_if
*if_stmt
)
3857 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
3859 LLVMBasicBlockRef merge_block
=
3860 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
3861 LLVMBasicBlockRef if_block
=
3862 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
3863 LLVMBasicBlockRef else_block
= merge_block
;
3864 if (!exec_list_is_empty(&if_stmt
->else_list
))
3865 else_block
= LLVMAppendBasicBlockInContext(
3866 ctx
->context
, ctx
->main_function
, "");
3868 LLVMValueRef cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, value
,
3869 LLVMConstInt(ctx
->i32
, 0, false), "");
3870 LLVMBuildCondBr(ctx
->builder
, cond
, if_block
, else_block
);
3872 LLVMPositionBuilderAtEnd(ctx
->builder
, if_block
);
3873 visit_cf_list(ctx
, &if_stmt
->then_list
);
3874 if (LLVMGetInsertBlock(ctx
->builder
))
3875 LLVMBuildBr(ctx
->builder
, merge_block
);
3877 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
3878 LLVMPositionBuilderAtEnd(ctx
->builder
, else_block
);
3879 visit_cf_list(ctx
, &if_stmt
->else_list
);
3880 if (LLVMGetInsertBlock(ctx
->builder
))
3881 LLVMBuildBr(ctx
->builder
, merge_block
);
3884 LLVMPositionBuilderAtEnd(ctx
->builder
, merge_block
);
3887 static void visit_loop(struct nir_to_llvm_context
*ctx
, nir_loop
*loop
)
3889 LLVMBasicBlockRef continue_parent
= ctx
->continue_block
;
3890 LLVMBasicBlockRef break_parent
= ctx
->break_block
;
3892 ctx
->continue_block
=
3893 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
3895 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
3897 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
3898 LLVMPositionBuilderAtEnd(ctx
->builder
, ctx
->continue_block
);
3899 visit_cf_list(ctx
, &loop
->body
);
3901 if (LLVMGetInsertBlock(ctx
->builder
))
3902 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
3903 LLVMPositionBuilderAtEnd(ctx
->builder
, ctx
->break_block
);
3905 ctx
->continue_block
= continue_parent
;
3906 ctx
->break_block
= break_parent
;
3909 static void visit_cf_list(struct nir_to_llvm_context
*ctx
,
3910 struct exec_list
*list
)
3912 foreach_list_typed(nir_cf_node
, node
, node
, list
)
3914 switch (node
->type
) {
3915 case nir_cf_node_block
:
3916 visit_block(ctx
, nir_cf_node_as_block(node
));
3919 case nir_cf_node_if
:
3920 visit_if(ctx
, nir_cf_node_as_if(node
));
3923 case nir_cf_node_loop
:
3924 visit_loop(ctx
, nir_cf_node_as_loop(node
));
3934 handle_vs_input_decl(struct nir_to_llvm_context
*ctx
,
3935 struct nir_variable
*variable
)
3937 LLVMValueRef t_list_ptr
= ctx
->vertex_buffers
;
3938 LLVMValueRef t_offset
;
3939 LLVMValueRef t_list
;
3940 LLVMValueRef args
[3];
3942 LLVMValueRef buffer_index
;
3943 int index
= variable
->data
.location
- VERT_ATTRIB_GENERIC0
;
3944 int idx
= variable
->data
.location
;
3945 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, true);
3947 variable
->data
.driver_location
= idx
* 4;
3949 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << index
)) {
3950 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->instance_id
,
3951 ctx
->start_instance
, "");
3952 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
3953 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
3955 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->vertex_id
,
3956 ctx
->base_vertex
, "");
3958 for (unsigned i
= 0; i
< attrib_count
; ++i
, ++idx
) {
3959 t_offset
= LLVMConstInt(ctx
->i32
, index
+ i
, false);
3961 t_list
= ac_build_indexed_load_const(&ctx
->ac
, t_list_ptr
, t_offset
);
3963 args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
3964 args
[2] = buffer_index
;
3965 input
= ac_emit_llvm_intrinsic(&ctx
->ac
,
3966 "llvm.SI.vs.load.input", ctx
->v4f32
, args
, 3,
3967 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_NOUNWIND
);
3969 for (unsigned chan
= 0; chan
< 4; chan
++) {
3970 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
3971 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
, chan
)] =
3972 to_integer(ctx
, LLVMBuildExtractElement(ctx
->builder
,
3973 input
, llvm_chan
, ""));
3979 static void interp_fs_input(struct nir_to_llvm_context
*ctx
,
3981 LLVMValueRef interp_param
,
3982 LLVMValueRef prim_mask
,
3983 LLVMValueRef result
[4])
3985 const char *intr_name
;
3986 LLVMValueRef attr_number
;
3989 attr_number
= LLVMConstInt(ctx
->i32
, attr
, false);
3991 /* fs.constant returns the param from the middle vertex, so it's not
3992 * really useful for flat shading. It's meant to be used for custom
3993 * interpolation (but the intrinsic can't fetch from the other two
3996 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
3997 * to do the right thing. The only reason we use fs.constant is that
3998 * fs.interp cannot be used on integers, because they can be equal
4001 intr_name
= interp_param
? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
4003 for (chan
= 0; chan
< 4; chan
++) {
4004 LLVMValueRef args
[4];
4005 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
4007 args
[0] = llvm_chan
;
4008 args
[1] = attr_number
;
4009 args
[2] = prim_mask
;
4010 args
[3] = interp_param
;
4011 result
[chan
] = ac_emit_llvm_intrinsic(&ctx
->ac
, intr_name
,
4012 ctx
->f32
, args
, args
[3] ? 4 : 3,
4013 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_NOUNWIND
);
4018 handle_fs_input_decl(struct nir_to_llvm_context
*ctx
,
4019 struct nir_variable
*variable
)
4021 int idx
= variable
->data
.location
;
4022 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4023 LLVMValueRef interp
;
4025 variable
->data
.driver_location
= idx
* 4;
4026 ctx
->input_mask
|= ((1ull << attrib_count
) - 1) << variable
->data
.location
;
4028 if (glsl_get_base_type(glsl_without_array(variable
->type
)) == GLSL_TYPE_FLOAT
) {
4029 unsigned interp_type
;
4030 if (variable
->data
.sample
) {
4031 interp_type
= INTERP_SAMPLE
;
4032 ctx
->shader_info
->fs
.force_persample
= true;
4033 } else if (variable
->data
.centroid
)
4034 interp_type
= INTERP_CENTROID
;
4036 interp_type
= INTERP_CENTER
;
4038 interp
= lookup_interp_param(ctx
, variable
->data
.interpolation
, interp_type
);
4042 for (unsigned i
= 0; i
< attrib_count
; ++i
)
4043 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
+ i
, 0)] = interp
;
4048 handle_shader_input_decl(struct nir_to_llvm_context
*ctx
,
4049 struct nir_variable
*variable
)
4051 switch (ctx
->stage
) {
4052 case MESA_SHADER_VERTEX
:
4053 handle_vs_input_decl(ctx
, variable
);
4055 case MESA_SHADER_FRAGMENT
:
4056 handle_fs_input_decl(ctx
, variable
);
4065 handle_fs_inputs_pre(struct nir_to_llvm_context
*ctx
,
4066 struct nir_shader
*nir
)
4069 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_INPUTS
; ++i
) {
4070 LLVMValueRef interp_param
;
4071 LLVMValueRef
*inputs
= ctx
->inputs
+radeon_llvm_reg_index_soa(i
, 0);
4073 if (!(ctx
->input_mask
& (1ull << i
)))
4076 if (i
>= VARYING_SLOT_VAR0
|| i
== VARYING_SLOT_PNTC
||
4077 i
== VARYING_SLOT_PRIMITIVE_ID
|| i
== VARYING_SLOT_LAYER
) {
4078 interp_param
= *inputs
;
4079 interp_fs_input(ctx
, index
, interp_param
, ctx
->prim_mask
,
4083 ctx
->shader_info
->fs
.flat_shaded_mask
|= 1u << index
;
4085 } else if (i
== VARYING_SLOT_POS
) {
4086 for(int i
= 0; i
< 3; ++i
)
4087 inputs
[i
] = ctx
->frag_pos
[i
];
4089 inputs
[3] = ac_emit_fdiv(&ctx
->ac
, ctx
->f32one
, ctx
->frag_pos
[3]);
4092 ctx
->shader_info
->fs
.num_interp
= index
;
4093 if (ctx
->input_mask
& (1 << VARYING_SLOT_PNTC
))
4094 ctx
->shader_info
->fs
.has_pcoord
= true;
4095 if (ctx
->input_mask
& (1 << VARYING_SLOT_PRIMITIVE_ID
))
4096 ctx
->shader_info
->fs
.prim_id_input
= true;
4097 if (ctx
->input_mask
& (1 << VARYING_SLOT_LAYER
))
4098 ctx
->shader_info
->fs
.layer_input
= true;
4099 ctx
->shader_info
->fs
.input_mask
= ctx
->input_mask
>> VARYING_SLOT_VAR0
;
4103 ac_build_alloca(struct nir_to_llvm_context
*ctx
,
4107 LLVMBuilderRef builder
= ctx
->builder
;
4108 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
4109 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
4110 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
4111 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
4112 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ctx
->context
);
4116 LLVMPositionBuilderBefore(first_builder
, first_instr
);
4118 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
4121 res
= LLVMBuildAlloca(first_builder
, type
, name
);
4122 LLVMBuildStore(builder
, LLVMConstNull(type
), res
);
4124 LLVMDisposeBuilder(first_builder
);
4129 static LLVMValueRef
si_build_alloca_undef(struct nir_to_llvm_context
*ctx
,
4133 LLVMValueRef ptr
= ac_build_alloca(ctx
, type
, name
);
4134 LLVMBuildStore(ctx
->builder
, LLVMGetUndef(type
), ptr
);
4139 handle_shader_output_decl(struct nir_to_llvm_context
*ctx
,
4140 struct nir_variable
*variable
)
4142 int idx
= variable
->data
.location
+ variable
->data
.index
;
4143 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4145 variable
->data
.driver_location
= idx
* 4;
4147 if (ctx
->stage
== MESA_SHADER_VERTEX
||
4148 ctx
->stage
== MESA_SHADER_GEOMETRY
) {
4149 if (idx
== VARYING_SLOT_CLIP_DIST0
||
4150 idx
== VARYING_SLOT_CULL_DIST0
) {
4151 int length
= glsl_get_length(variable
->type
);
4152 if (ctx
->stage
== MESA_SHADER_VERTEX
) {
4153 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
4154 ctx
->shader_info
->vs
.clip_dist_mask
= (1 << length
) - 1;
4155 ctx
->num_clips
= length
;
4156 } else if (idx
== VARYING_SLOT_CULL_DIST0
) {
4157 ctx
->shader_info
->vs
.cull_dist_mask
= (1 << length
) - 1;
4158 ctx
->num_culls
= length
;
4168 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
4169 for (unsigned chan
= 0; chan
< 4; chan
++) {
4170 ctx
->outputs
[radeon_llvm_reg_index_soa(idx
+ i
, chan
)] =
4171 si_build_alloca_undef(ctx
, ctx
->f32
, "");
4174 ctx
->output_mask
|= ((1ull << attrib_count
) - 1) << idx
;
4178 setup_locals(struct nir_to_llvm_context
*ctx
,
4179 struct nir_function
*func
)
4182 ctx
->num_locals
= 0;
4183 nir_foreach_variable(variable
, &func
->impl
->locals
) {
4184 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4185 variable
->data
.driver_location
= ctx
->num_locals
* 4;
4186 ctx
->num_locals
+= attrib_count
;
4188 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
4192 for (i
= 0; i
< ctx
->num_locals
; i
++) {
4193 for (j
= 0; j
< 4; j
++) {
4194 ctx
->locals
[i
* 4 + j
] =
4195 si_build_alloca_undef(ctx
, ctx
->f32
, "temp");
4201 emit_float_saturate(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
, float lo
, float hi
)
4203 v
= to_float(ctx
, v
);
4204 v
= emit_intrin_2f_param(ctx
, "llvm.maxnum.f32", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, lo
));
4205 return emit_intrin_2f_param(ctx
, "llvm.minnum.f32", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, hi
));
4209 static LLVMValueRef
emit_pack_int16(struct nir_to_llvm_context
*ctx
,
4210 LLVMValueRef src0
, LLVMValueRef src1
)
4212 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
4213 LLVMValueRef comp
[2];
4215 comp
[0] = LLVMBuildAnd(ctx
->builder
, src0
, LLVMConstInt(ctx
-> i32
, 65535, 0), "");
4216 comp
[1] = LLVMBuildAnd(ctx
->builder
, src1
, LLVMConstInt(ctx
-> i32
, 65535, 0), "");
4217 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
4218 return LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
4221 /* Initialize arguments for the shader export intrinsic */
4223 si_llvm_init_export_args(struct nir_to_llvm_context
*ctx
,
4224 LLVMValueRef
*values
,
4228 /* Default is 0xf. Adjusted below depending on the format. */
4229 args
[0] = LLVMConstInt(ctx
->i32
, target
!= V_008DFC_SQ_EXP_NULL
? 0xf : 0, false);
4230 /* Specify whether the EXEC mask represents the valid mask */
4231 args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
4233 /* Specify whether this is the last export */
4234 args
[2] = LLVMConstInt(ctx
->i32
, 0, false);
4235 /* Specify the target we are exporting */
4236 args
[3] = LLVMConstInt(ctx
->i32
, target
, false);
4238 args
[4] = LLVMConstInt(ctx
->i32
, 0, false); /* COMPR flag */
4239 args
[5] = LLVMGetUndef(ctx
->f32
);
4240 args
[6] = LLVMGetUndef(ctx
->f32
);
4241 args
[7] = LLVMGetUndef(ctx
->f32
);
4242 args
[8] = LLVMGetUndef(ctx
->f32
);
4247 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&& target
>= V_008DFC_SQ_EXP_MRT
) {
4248 LLVMValueRef val
[4];
4249 unsigned index
= target
- V_008DFC_SQ_EXP_MRT
;
4250 unsigned col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * index
)) & 0xf;
4251 bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> index
) & 1;
4253 switch(col_format
) {
4254 case V_028714_SPI_SHADER_ZERO
:
4255 args
[0] = LLVMConstInt(ctx
->i32
, 0x0, 0);
4256 args
[3] = LLVMConstInt(ctx
->i32
, V_008DFC_SQ_EXP_NULL
, 0);
4259 case V_028714_SPI_SHADER_32_R
:
4260 args
[0] = LLVMConstInt(ctx
->i32
, 0x1, 0);
4261 args
[5] = values
[0];
4264 case V_028714_SPI_SHADER_32_GR
:
4265 args
[0] = LLVMConstInt(ctx
->i32
, 0x3, 0);
4266 args
[5] = values
[0];
4267 args
[6] = values
[1];
4270 case V_028714_SPI_SHADER_32_AR
:
4271 args
[0] = LLVMConstInt(ctx
->i32
, 0x9, 0);
4272 args
[5] = values
[0];
4273 args
[8] = values
[3];
4276 case V_028714_SPI_SHADER_FP16_ABGR
:
4277 args
[4] = ctx
->i32one
;
4279 for (unsigned chan
= 0; chan
< 2; chan
++) {
4280 LLVMValueRef pack_args
[2] = {
4282 values
[2 * chan
+ 1]
4284 LLVMValueRef packed
;
4286 packed
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.SI.packf16",
4287 ctx
->i32
, pack_args
, 2,
4288 AC_FUNC_ATTR_READNONE
);
4289 args
[chan
+ 5] = packed
;
4293 case V_028714_SPI_SHADER_UNORM16_ABGR
:
4294 for (unsigned chan
= 0; chan
< 4; chan
++) {
4295 val
[chan
] = emit_float_saturate(ctx
, values
[chan
], 0, 1);
4296 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
4297 LLVMConstReal(ctx
->f32
, 65535), "");
4298 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
4299 LLVMConstReal(ctx
->f32
, 0.5), "");
4300 val
[chan
] = LLVMBuildFPToUI(ctx
->builder
, val
[chan
],
4304 args
[4] = ctx
->i32one
;
4305 args
[5] = emit_pack_int16(ctx
, val
[0], val
[1]);
4306 args
[6] = emit_pack_int16(ctx
, val
[2], val
[3]);
4309 case V_028714_SPI_SHADER_SNORM16_ABGR
:
4310 for (unsigned chan
= 0; chan
< 4; chan
++) {
4311 val
[chan
] = emit_float_saturate(ctx
, values
[chan
], -1, 1);
4312 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
4313 LLVMConstReal(ctx
->f32
, 32767), "");
4315 /* If positive, add 0.5, else add -0.5. */
4316 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
4317 LLVMBuildSelect(ctx
->builder
,
4318 LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
,
4319 val
[chan
], ctx
->f32zero
, ""),
4320 LLVMConstReal(ctx
->f32
, 0.5),
4321 LLVMConstReal(ctx
->f32
, -0.5), ""), "");
4322 val
[chan
] = LLVMBuildFPToSI(ctx
->builder
, val
[chan
], ctx
->i32
, "");
4325 args
[4] = ctx
->i32one
;
4326 args
[5] = emit_pack_int16(ctx
, val
[0], val
[1]);
4327 args
[6] = emit_pack_int16(ctx
, val
[2], val
[3]);
4330 case V_028714_SPI_SHADER_UINT16_ABGR
: {
4331 LLVMValueRef max
= LLVMConstInt(ctx
->i32
, is_int8
? 255 : 65535, 0);
4333 for (unsigned chan
= 0; chan
< 4; chan
++) {
4334 val
[chan
] = to_integer(ctx
, values
[chan
]);
4335 val
[chan
] = emit_minmax_int(ctx
, LLVMIntULT
, val
[chan
], max
);
4338 args
[4] = ctx
->i32one
;
4339 args
[5] = emit_pack_int16(ctx
, val
[0], val
[1]);
4340 args
[6] = emit_pack_int16(ctx
, val
[2], val
[3]);
4344 case V_028714_SPI_SHADER_SINT16_ABGR
: {
4345 LLVMValueRef max
= LLVMConstInt(ctx
->i32
, is_int8
? 127 : 32767, 0);
4346 LLVMValueRef min
= LLVMConstInt(ctx
->i32
, is_int8
? -128 : -32768, 0);
4349 for (unsigned chan
= 0; chan
< 4; chan
++) {
4350 val
[chan
] = to_integer(ctx
, values
[chan
]);
4351 val
[chan
] = emit_minmax_int(ctx
, LLVMIntSLT
, val
[chan
], max
);
4352 val
[chan
] = emit_minmax_int(ctx
, LLVMIntSGT
, val
[chan
], min
);
4355 args
[4] = ctx
->i32one
;
4356 args
[5] = emit_pack_int16(ctx
, val
[0], val
[1]);
4357 args
[6] = emit_pack_int16(ctx
, val
[2], val
[3]);
4362 case V_028714_SPI_SHADER_32_ABGR
:
4363 memcpy(&args
[5], values
, sizeof(values
[0]) * 4);
4367 memcpy(&args
[5], values
, sizeof(values
[0]) * 4);
4369 for (unsigned i
= 5; i
< 9; ++i
)
4370 args
[i
] = to_float(ctx
, args
[i
]);
4374 handle_vs_outputs_post(struct nir_to_llvm_context
*ctx
)
4376 uint32_t param_count
= 0;
4378 unsigned pos_idx
, num_pos_exports
= 0;
4379 LLVMValueRef args
[9];
4380 LLVMValueRef pos_args
[4][9] = { { 0 } };
4381 LLVMValueRef psize_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
4383 const uint64_t clip_mask
= ctx
->output_mask
& ((1ull << VARYING_SLOT_CLIP_DIST0
) |
4384 (1ull << VARYING_SLOT_CLIP_DIST1
) |
4385 (1ull << VARYING_SLOT_CULL_DIST0
) |
4386 (1ull << VARYING_SLOT_CULL_DIST1
));
4388 ctx
->shader_info
->vs
.prim_id_output
= 0xffffffff;
4389 ctx
->shader_info
->vs
.layer_output
= 0xffffffff;
4391 LLVMValueRef slots
[8];
4394 if (ctx
->shader_info
->vs
.cull_dist_mask
)
4395 ctx
->shader_info
->vs
.cull_dist_mask
<<= ctx
->num_clips
;
4397 i
= VARYING_SLOT_CLIP_DIST0
;
4398 for (j
= 0; j
< ctx
->num_clips
; j
++)
4399 slots
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4400 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4401 i
= VARYING_SLOT_CULL_DIST0
;
4402 for (j
= 0; j
< ctx
->num_culls
; j
++)
4403 slots
[ctx
->num_clips
+ j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4404 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4406 for (i
= ctx
->num_clips
+ ctx
->num_culls
; i
< 8; i
++)
4407 slots
[i
] = LLVMGetUndef(ctx
->f32
);
4409 if (ctx
->num_clips
+ ctx
->num_culls
> 4) {
4410 target
= V_008DFC_SQ_EXP_POS
+ 3;
4411 si_llvm_init_export_args(ctx
, &slots
[4], target
, args
);
4412 memcpy(pos_args
[target
- V_008DFC_SQ_EXP_POS
],
4413 args
, sizeof(args
));
4416 target
= V_008DFC_SQ_EXP_POS
+ 2;
4417 si_llvm_init_export_args(ctx
, &slots
[0], target
, args
);
4418 memcpy(pos_args
[target
- V_008DFC_SQ_EXP_POS
],
4419 args
, sizeof(args
));
4423 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
4424 LLVMValueRef values
[4];
4425 if (!(ctx
->output_mask
& (1ull << i
)))
4428 for (unsigned j
= 0; j
< 4; j
++)
4429 values
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4430 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4432 if (i
== VARYING_SLOT_POS
) {
4433 target
= V_008DFC_SQ_EXP_POS
;
4434 } else if (i
== VARYING_SLOT_CLIP_DIST0
||
4435 i
== VARYING_SLOT_CLIP_DIST1
||
4436 i
== VARYING_SLOT_CULL_DIST0
||
4437 i
== VARYING_SLOT_CULL_DIST1
) {
4439 } else if (i
== VARYING_SLOT_PSIZ
) {
4440 ctx
->shader_info
->vs
.writes_pointsize
= true;
4441 psize_value
= values
[0];
4443 } else if (i
== VARYING_SLOT_LAYER
) {
4444 ctx
->shader_info
->vs
.writes_layer
= true;
4445 layer_value
= values
[0];
4446 ctx
->shader_info
->vs
.layer_output
= param_count
;
4447 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
4449 } else if (i
== VARYING_SLOT_VIEWPORT
) {
4450 ctx
->shader_info
->vs
.writes_viewport_index
= true;
4451 viewport_index_value
= values
[0];
4453 } else if (i
== VARYING_SLOT_PRIMITIVE_ID
) {
4454 ctx
->shader_info
->vs
.prim_id_output
= param_count
;
4455 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
4457 } else if (i
>= VARYING_SLOT_VAR0
) {
4458 ctx
->shader_info
->vs
.export_mask
|= 1u << (i
- VARYING_SLOT_VAR0
);
4459 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
4463 si_llvm_init_export_args(ctx
, values
, target
, args
);
4465 if (target
>= V_008DFC_SQ_EXP_POS
&&
4466 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
4467 memcpy(pos_args
[target
- V_008DFC_SQ_EXP_POS
],
4468 args
, sizeof(args
));
4470 ac_emit_llvm_intrinsic(&ctx
->ac
,
4477 /* We need to add the position output manually if it's missing. */
4478 if (!pos_args
[0][0]) {
4479 pos_args
[0][0] = LLVMConstInt(ctx
->i32
, 0xf, false);
4480 pos_args
[0][1] = ctx
->i32zero
; /* EXEC mask */
4481 pos_args
[0][2] = ctx
->i32zero
; /* last export? */
4482 pos_args
[0][3] = LLVMConstInt(ctx
->i32
, V_008DFC_SQ_EXP_POS
, false);
4483 pos_args
[0][4] = ctx
->i32zero
; /* COMPR flag */
4484 pos_args
[0][5] = ctx
->f32zero
; /* X */
4485 pos_args
[0][6] = ctx
->f32zero
; /* Y */
4486 pos_args
[0][7] = ctx
->f32zero
; /* Z */
4487 pos_args
[0][8] = ctx
->f32one
; /* W */
4490 uint32_t mask
= ((ctx
->shader_info
->vs
.writes_pointsize
== true ? 1 : 0) |
4491 (ctx
->shader_info
->vs
.writes_layer
== true ? 4 : 0) |
4492 (ctx
->shader_info
->vs
.writes_viewport_index
== true ? 8 : 0));
4494 pos_args
[1][0] = LLVMConstInt(ctx
->i32
, mask
, false); /* writemask */
4495 pos_args
[1][1] = ctx
->i32zero
; /* EXEC mask */
4496 pos_args
[1][2] = ctx
->i32zero
; /* last export? */
4497 pos_args
[1][3] = LLVMConstInt(ctx
->i32
, V_008DFC_SQ_EXP_POS
+ 1, false);
4498 pos_args
[1][4] = ctx
->i32zero
; /* COMPR flag */
4499 pos_args
[1][5] = ctx
->f32zero
; /* X */
4500 pos_args
[1][6] = ctx
->f32zero
; /* Y */
4501 pos_args
[1][7] = ctx
->f32zero
; /* Z */
4502 pos_args
[1][8] = ctx
->f32zero
; /* W */
4504 if (ctx
->shader_info
->vs
.writes_pointsize
== true)
4505 pos_args
[1][5] = psize_value
;
4506 if (ctx
->shader_info
->vs
.writes_layer
== true)
4507 pos_args
[1][7] = layer_value
;
4508 if (ctx
->shader_info
->vs
.writes_viewport_index
== true)
4509 pos_args
[1][8] = viewport_index_value
;
4511 for (i
= 0; i
< 4; i
++) {
4517 for (i
= 0; i
< 4; i
++) {
4518 if (!pos_args
[i
][0])
4521 /* Specify the target we are exporting */
4522 pos_args
[i
][3] = LLVMConstInt(ctx
->i32
, V_008DFC_SQ_EXP_POS
+ pos_idx
++, false);
4523 if (pos_idx
== num_pos_exports
)
4524 pos_args
[i
][2] = ctx
->i32one
;
4525 ac_emit_llvm_intrinsic(&ctx
->ac
,
4531 ctx
->shader_info
->vs
.pos_exports
= num_pos_exports
;
4532 ctx
->shader_info
->vs
.param_exports
= param_count
;
4536 handle_es_outputs_post(struct nir_to_llvm_context
*ctx
)
4539 uint64_t max_output_written
= 0;
4540 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
4541 LLVMValueRef
*out_ptr
= &ctx
->outputs
[i
* 4];
4543 if (!(ctx
->output_mask
& (1ull << i
)))
4546 param_index
= shader_io_get_unique_index(i
);
4548 if (param_index
> max_output_written
)
4549 max_output_written
= param_index
;
4551 for (j
= 0; j
< 4; j
++) {
4552 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], "");
4553 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->i32
, "");
4555 ac_build_tbuffer_store(&ctx
->ac
,
4558 LLVMGetUndef(ctx
->i32
), ctx
->es2gs_offset
,
4559 (4 * param_index
+ j
) * 4,
4560 V_008F0C_BUF_DATA_FORMAT_32
,
4561 V_008F0C_BUF_NUM_FORMAT_UINT
,
4565 ctx
->shader_info
->vs
.esgs_itemsize
= (max_output_written
+ 1) * 16;
4569 si_export_mrt_color(struct nir_to_llvm_context
*ctx
,
4570 LLVMValueRef
*color
, unsigned param
, bool is_last
)
4572 LLVMValueRef args
[9];
4574 si_llvm_init_export_args(ctx
, color
, param
,
4578 args
[1] = ctx
->i32one
; /* whether the EXEC mask is valid */
4579 args
[2] = ctx
->i32one
; /* DONE bit */
4580 } else if (args
[0] == ctx
->i32zero
)
4581 return; /* unnecessary NULL export */
4583 ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.SI.export",
4584 ctx
->voidt
, args
, 9, 0);
4588 si_export_mrt_z(struct nir_to_llvm_context
*ctx
,
4589 LLVMValueRef depth
, LLVMValueRef stencil
,
4590 LLVMValueRef samplemask
)
4592 LLVMValueRef args
[9];
4594 args
[1] = ctx
->i32one
; /* whether the EXEC mask is valid */
4595 args
[2] = ctx
->i32one
; /* DONE bit */
4596 /* Specify the target we are exporting */
4597 args
[3] = LLVMConstInt(ctx
->i32
, V_008DFC_SQ_EXP_MRTZ
, false);
4599 args
[4] = ctx
->i32zero
; /* COMP flag */
4600 args
[5] = LLVMGetUndef(ctx
->f32
); /* R, depth */
4601 args
[6] = LLVMGetUndef(ctx
->f32
); /* G, stencil test val[0:7], stencil op val[8:15] */
4602 args
[7] = LLVMGetUndef(ctx
->f32
); /* B, sample mask */
4603 args
[8] = LLVMGetUndef(ctx
->f32
); /* A, alpha to mask */
4616 args
[7] = samplemask
;
4620 /* SI (except OLAND) has a bug that it only looks
4621 * at the X writemask component. */
4622 if (ctx
->options
->chip_class
== SI
&&
4623 ctx
->options
->family
!= CHIP_OLAND
)
4626 args
[0] = LLVMConstInt(ctx
->i32
, mask
, false);
4627 ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.SI.export",
4628 ctx
->voidt
, args
, 9, 0);
4632 handle_fs_outputs_post(struct nir_to_llvm_context
*ctx
)
4635 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
4637 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
4638 LLVMValueRef values
[4];
4640 if (!(ctx
->output_mask
& (1ull << i
)))
4643 if (i
== FRAG_RESULT_DEPTH
) {
4644 ctx
->shader_info
->fs
.writes_z
= true;
4645 depth
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4646 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
4647 } else if (i
== FRAG_RESULT_STENCIL
) {
4648 ctx
->shader_info
->fs
.writes_stencil
= true;
4649 stencil
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4650 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
4653 for (unsigned j
= 0; j
< 4; j
++)
4654 values
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4655 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4657 if (!ctx
->shader_info
->fs
.writes_z
&& !ctx
->shader_info
->fs
.writes_stencil
)
4658 last
= ctx
->output_mask
<= ((1ull << (i
+ 1)) - 1);
4660 si_export_mrt_color(ctx
, values
, V_008DFC_SQ_EXP_MRT
+ index
, last
);
4665 if (depth
|| stencil
)
4666 si_export_mrt_z(ctx
, depth
, stencil
, samplemask
);
4668 si_export_mrt_color(ctx
, NULL
, V_008DFC_SQ_EXP_NULL
, true);
4670 ctx
->shader_info
->fs
.output_mask
= index
? ((1ull << index
) - 1) : 0;
4674 emit_gs_epilogue(struct nir_to_llvm_context
*ctx
)
4676 LLVMValueRef args
[2];
4678 args
[0] = LLVMConstInt(ctx
->i32
, SENDMSG_GS_OP_NOP
| SENDMSG_GS_DONE
, false);
4679 args
[1] = ctx
->gs_wave_id
;
4680 ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.SI.sendmsg",
4681 ctx
->voidt
, args
, 2, 0);
4685 handle_shader_outputs_post(struct nir_to_llvm_context
*ctx
)
4687 switch (ctx
->stage
) {
4688 case MESA_SHADER_VERTEX
:
4689 if (ctx
->options
->key
.vs
.as_es
)
4690 handle_es_outputs_post(ctx
);
4692 handle_vs_outputs_post(ctx
);
4694 case MESA_SHADER_FRAGMENT
:
4695 handle_fs_outputs_post(ctx
);
4697 case MESA_SHADER_GEOMETRY
:
4698 emit_gs_epilogue(ctx
);
4706 handle_shared_compute_var(struct nir_to_llvm_context
*ctx
,
4707 struct nir_variable
*variable
, uint32_t *offset
, int idx
)
4709 unsigned size
= glsl_count_attribute_slots(variable
->type
, false);
4710 variable
->data
.driver_location
= *offset
;
4714 static void ac_llvm_finalize_module(struct nir_to_llvm_context
* ctx
)
4716 LLVMPassManagerRef passmgr
;
4717 /* Create the pass manager */
4718 passmgr
= LLVMCreateFunctionPassManagerForModule(
4721 /* This pass should eliminate all the load and store instructions */
4722 LLVMAddPromoteMemoryToRegisterPass(passmgr
);
4724 /* Add some optimization passes */
4725 LLVMAddScalarReplAggregatesPass(passmgr
);
4726 LLVMAddLICMPass(passmgr
);
4727 LLVMAddAggressiveDCEPass(passmgr
);
4728 LLVMAddCFGSimplificationPass(passmgr
);
4729 LLVMAddInstructionCombiningPass(passmgr
);
4732 LLVMInitializeFunctionPassManager(passmgr
);
4733 LLVMRunFunctionPassManager(passmgr
, ctx
->main_function
);
4734 LLVMFinalizeFunctionPassManager(passmgr
);
4736 LLVMDisposeBuilder(ctx
->builder
);
4737 LLVMDisposePassManager(passmgr
);
4741 ac_setup_rings(struct nir_to_llvm_context
*ctx
)
4743 if (ctx
->stage
== MESA_SHADER_VERTEX
&& ctx
->options
->key
.vs
.as_es
) {
4744 ctx
->esgs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, ctx
->i32one
);
4747 if (ctx
->is_gs_copy_shader
) {
4748 ctx
->gsvs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, 3, false));
4750 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
4752 ctx
->esgs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, 2, false));
4753 ctx
->gsvs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, 4, false));
4755 ctx
->gsvs_ring
= LLVMBuildBitCast(ctx
->builder
, ctx
->gsvs_ring
, ctx
->v4i32
, "");
4757 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->gsvs_num_entries
, LLVMConstInt(ctx
->i32
, 2, false), "");
4758 tmp
= LLVMBuildExtractElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->i32one
, "");
4759 tmp
= LLVMBuildOr(ctx
->builder
, tmp
, ctx
->gsvs_ring_stride
, "");
4760 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, tmp
, ctx
->i32one
, "");
4762 ctx
->gsvs_ring
= LLVMBuildBitCast(ctx
->builder
, ctx
->gsvs_ring
, ctx
->v16i8
, "");
4767 LLVMModuleRef
ac_translate_nir_to_llvm(LLVMTargetMachineRef tm
,
4768 struct nir_shader
*nir
,
4769 struct ac_shader_variant_info
*shader_info
,
4770 const struct ac_nir_compiler_options
*options
)
4772 struct nir_to_llvm_context ctx
= {0};
4773 struct nir_function
*func
;
4775 ctx
.options
= options
;
4776 ctx
.shader_info
= shader_info
;
4777 ctx
.context
= LLVMContextCreate();
4778 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
4780 ac_llvm_context_init(&ctx
.ac
, ctx
.context
);
4781 ctx
.ac
.module
= ctx
.module
;
4783 ctx
.has_ds_bpermute
= ctx
.options
->chip_class
>= VI
;
4785 memset(shader_info
, 0, sizeof(*shader_info
));
4787 LLVMSetTarget(ctx
.module
, options
->supports_spill
? "amdgcn-mesa-mesa3d" : "amdgcn--");
4790 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
4791 ctx
.ac
.builder
= ctx
.builder
;
4792 ctx
.stage
= nir
->stage
;
4794 for (i
= 0; i
< AC_UD_MAX_SETS
; i
++)
4795 shader_info
->user_sgprs_locs
.descriptor_sets
[i
].sgpr_idx
= -1;
4796 for (i
= 0; i
< AC_UD_MAX_UD
; i
++)
4797 shader_info
->user_sgprs_locs
.shader_data
[i
].sgpr_idx
= -1;
4799 create_function(&ctx
);
4801 if (nir
->stage
== MESA_SHADER_COMPUTE
) {
4803 nir_foreach_variable(variable
, &nir
->shared
)
4807 uint32_t shared_size
= 0;
4809 LLVMTypeRef i8p
= LLVMPointerType(ctx
.i8
, LOCAL_ADDR_SPACE
);
4810 nir_foreach_variable(variable
, &nir
->shared
) {
4811 handle_shared_compute_var(&ctx
, variable
, &shared_size
, idx
);
4816 var
= LLVMAddGlobalInAddressSpace(ctx
.module
,
4817 LLVMArrayType(ctx
.i8
, shared_size
),
4820 LLVMSetAlignment(var
, 4);
4821 ctx
.shared_memory
= LLVMBuildBitCast(ctx
.builder
, var
, i8p
, "");
4823 } else if (nir
->stage
== MESA_SHADER_GEOMETRY
) {
4824 ctx
.gs_next_vertex
= ac_build_alloca(&ctx
, ctx
.i32
, "gs_next_vertex");
4826 ctx
.gs_max_out_vertices
= nir
->info
->gs
.vertices_out
;
4829 ac_setup_rings(&ctx
);
4831 nir_foreach_variable(variable
, &nir
->inputs
)
4832 handle_shader_input_decl(&ctx
, variable
);
4834 if (nir
->stage
== MESA_SHADER_FRAGMENT
)
4835 handle_fs_inputs_pre(&ctx
, nir
);
4837 nir_foreach_variable(variable
, &nir
->outputs
)
4838 handle_shader_output_decl(&ctx
, variable
);
4840 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4841 _mesa_key_pointer_equal
);
4842 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4843 _mesa_key_pointer_equal
);
4845 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
4847 setup_locals(&ctx
, func
);
4849 visit_cf_list(&ctx
, &func
->impl
->body
);
4850 phi_post_pass(&ctx
);
4852 handle_shader_outputs_post(&ctx
);
4853 LLVMBuildRetVoid(ctx
.builder
);
4855 ac_llvm_finalize_module(&ctx
);
4857 ralloc_free(ctx
.defs
);
4858 ralloc_free(ctx
.phis
);
4860 if (nir
->stage
== MESA_SHADER_GEOMETRY
) {
4861 shader_info
->gs
.gsvs_vertex_size
= util_bitcount64(ctx
.output_mask
) * 16;
4862 shader_info
->gs
.max_gsvs_emit_size
= shader_info
->gs
.gsvs_vertex_size
*
4863 nir
->info
->gs
.vertices_out
;
4868 static void ac_diagnostic_handler(LLVMDiagnosticInfoRef di
, void *context
)
4870 unsigned *retval
= (unsigned *)context
;
4871 LLVMDiagnosticSeverity severity
= LLVMGetDiagInfoSeverity(di
);
4872 char *description
= LLVMGetDiagInfoDescription(di
);
4874 if (severity
== LLVMDSError
) {
4876 fprintf(stderr
, "LLVM triggered Diagnostic Handler: %s\n",
4880 LLVMDisposeMessage(description
);
4883 static unsigned ac_llvm_compile(LLVMModuleRef M
,
4884 struct ac_shader_binary
*binary
,
4885 LLVMTargetMachineRef tm
)
4887 unsigned retval
= 0;
4889 LLVMContextRef llvm_ctx
;
4890 LLVMMemoryBufferRef out_buffer
;
4891 unsigned buffer_size
;
4892 const char *buffer_data
;
4895 /* Setup Diagnostic Handler*/
4896 llvm_ctx
= LLVMGetModuleContext(M
);
4898 LLVMContextSetDiagnosticHandler(llvm_ctx
, ac_diagnostic_handler
,
4902 mem_err
= LLVMTargetMachineEmitToMemoryBuffer(tm
, M
, LLVMObjectFile
,
4905 /* Process Errors/Warnings */
4907 fprintf(stderr
, "%s: %s", __FUNCTION__
, err
);
4913 /* Extract Shader Code*/
4914 buffer_size
= LLVMGetBufferSize(out_buffer
);
4915 buffer_data
= LLVMGetBufferStart(out_buffer
);
4917 ac_elf_read(buffer_data
, buffer_size
, binary
);
4920 LLVMDisposeMemoryBuffer(out_buffer
);
4926 static void ac_compile_llvm_module(LLVMTargetMachineRef tm
,
4927 LLVMModuleRef llvm_module
,
4928 struct ac_shader_binary
*binary
,
4929 struct ac_shader_config
*config
,
4930 struct ac_shader_variant_info
*shader_info
,
4931 gl_shader_stage stage
,
4932 bool dump_shader
, bool supports_spill
)
4935 ac_dump_module(llvm_module
);
4937 memset(binary
, 0, sizeof(*binary
));
4938 int v
= ac_llvm_compile(llvm_module
, binary
, tm
);
4940 fprintf(stderr
, "compile failed\n");
4944 fprintf(stderr
, "disasm:\n%s\n", binary
->disasm_string
);
4946 ac_shader_binary_read_config(binary
, config
, 0, supports_spill
);
4948 LLVMContextRef ctx
= LLVMGetModuleContext(llvm_module
);
4949 LLVMDisposeModule(llvm_module
);
4950 LLVMContextDispose(ctx
);
4952 if (stage
== MESA_SHADER_FRAGMENT
) {
4953 shader_info
->num_input_vgprs
= 0;
4954 if (G_0286CC_PERSP_SAMPLE_ENA(config
->spi_ps_input_addr
))
4955 shader_info
->num_input_vgprs
+= 2;
4956 if (G_0286CC_PERSP_CENTER_ENA(config
->spi_ps_input_addr
))
4957 shader_info
->num_input_vgprs
+= 2;
4958 if (G_0286CC_PERSP_CENTROID_ENA(config
->spi_ps_input_addr
))
4959 shader_info
->num_input_vgprs
+= 2;
4960 if (G_0286CC_PERSP_PULL_MODEL_ENA(config
->spi_ps_input_addr
))
4961 shader_info
->num_input_vgprs
+= 3;
4962 if (G_0286CC_LINEAR_SAMPLE_ENA(config
->spi_ps_input_addr
))
4963 shader_info
->num_input_vgprs
+= 2;
4964 if (G_0286CC_LINEAR_CENTER_ENA(config
->spi_ps_input_addr
))
4965 shader_info
->num_input_vgprs
+= 2;
4966 if (G_0286CC_LINEAR_CENTROID_ENA(config
->spi_ps_input_addr
))
4967 shader_info
->num_input_vgprs
+= 2;
4968 if (G_0286CC_LINE_STIPPLE_TEX_ENA(config
->spi_ps_input_addr
))
4969 shader_info
->num_input_vgprs
+= 1;
4970 if (G_0286CC_POS_X_FLOAT_ENA(config
->spi_ps_input_addr
))
4971 shader_info
->num_input_vgprs
+= 1;
4972 if (G_0286CC_POS_Y_FLOAT_ENA(config
->spi_ps_input_addr
))
4973 shader_info
->num_input_vgprs
+= 1;
4974 if (G_0286CC_POS_Z_FLOAT_ENA(config
->spi_ps_input_addr
))
4975 shader_info
->num_input_vgprs
+= 1;
4976 if (G_0286CC_POS_W_FLOAT_ENA(config
->spi_ps_input_addr
))
4977 shader_info
->num_input_vgprs
+= 1;
4978 if (G_0286CC_FRONT_FACE_ENA(config
->spi_ps_input_addr
))
4979 shader_info
->num_input_vgprs
+= 1;
4980 if (G_0286CC_ANCILLARY_ENA(config
->spi_ps_input_addr
))
4981 shader_info
->num_input_vgprs
+= 1;
4982 if (G_0286CC_SAMPLE_COVERAGE_ENA(config
->spi_ps_input_addr
))
4983 shader_info
->num_input_vgprs
+= 1;
4984 if (G_0286CC_POS_FIXED_PT_ENA(config
->spi_ps_input_addr
))
4985 shader_info
->num_input_vgprs
+= 1;
4987 config
->num_vgprs
= MAX2(config
->num_vgprs
, shader_info
->num_input_vgprs
);
4989 /* +3 for scratch wave offset and VCC */
4990 config
->num_sgprs
= MAX2(config
->num_sgprs
,
4991 shader_info
->num_input_sgprs
+ 3);
4994 void ac_compile_nir_shader(LLVMTargetMachineRef tm
,
4995 struct ac_shader_binary
*binary
,
4996 struct ac_shader_config
*config
,
4997 struct ac_shader_variant_info
*shader_info
,
4998 struct nir_shader
*nir
,
4999 const struct ac_nir_compiler_options
*options
,
5003 LLVMModuleRef llvm_module
= ac_translate_nir_to_llvm(tm
, nir
, shader_info
,
5006 ac_compile_llvm_module(tm
, llvm_module
, binary
, config
, shader_info
, nir
->stage
, dump_shader
, options
->supports_spill
);
5007 switch (nir
->stage
) {
5008 case MESA_SHADER_COMPUTE
:
5009 for (int i
= 0; i
< 3; ++i
)
5010 shader_info
->cs
.block_size
[i
] = nir
->info
->cs
.local_size
[i
];
5012 case MESA_SHADER_FRAGMENT
:
5013 shader_info
->fs
.early_fragment_test
= nir
->info
->fs
.early_fragment_tests
;
5015 case MESA_SHADER_GEOMETRY
:
5016 shader_info
->gs
.vertices_in
= nir
->info
->gs
.vertices_in
;
5017 shader_info
->gs
.vertices_out
= nir
->info
->gs
.vertices_out
;
5018 shader_info
->gs
.output_prim
= nir
->info
->gs
.output_primitive
;
5019 shader_info
->gs
.invocations
= nir
->info
->gs
.invocations
;
5021 case MESA_SHADER_VERTEX
:
5022 shader_info
->vs
.as_es
= options
->key
.vs
.as_es
;
5030 ac_gs_copy_shader_emit(struct nir_to_llvm_context
*ctx
)
5032 LLVMValueRef args
[9];
5033 args
[0] = ctx
->gsvs_ring
;
5034 args
[1] = LLVMBuildMul(ctx
->builder
, ctx
->vertex_id
, LLVMConstInt(ctx
->i32
, 4, false), "");
5035 args
[3] = ctx
->i32zero
;
5036 args
[4] = ctx
->i32one
; /* OFFEN */
5037 args
[5] = ctx
->i32zero
; /* IDXEN */
5038 args
[6] = ctx
->i32one
; /* GLC */
5039 args
[7] = ctx
->i32one
; /* SLC */
5040 args
[8] = ctx
->i32zero
; /* TFE */
5043 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5044 if (!(ctx
->output_mask
& (1ull << i
)))
5047 for (unsigned j
= 0; j
< 4; j
++) {
5049 args
[2] = LLVMConstInt(ctx
->i32
,
5051 ctx
->gs_max_out_vertices
* 16 * 4, false);
5053 value
= ac_emit_llvm_intrinsic(&ctx
->ac
,
5054 "llvm.SI.buffer.load.dword.i32.i32",
5056 AC_FUNC_ATTR_READONLY
);
5058 LLVMBuildStore(ctx
->builder
,
5059 to_float(ctx
, value
), ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)]);
5063 handle_vs_outputs_post(ctx
);
5066 void ac_create_gs_copy_shader(LLVMTargetMachineRef tm
,
5067 struct nir_shader
*geom_shader
,
5068 struct ac_shader_binary
*binary
,
5069 struct ac_shader_config
*config
,
5070 struct ac_shader_variant_info
*shader_info
,
5071 const struct ac_nir_compiler_options
*options
,
5074 struct nir_to_llvm_context ctx
= {0};
5075 ctx
.context
= LLVMContextCreate();
5076 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
5077 ctx
.options
= options
;
5078 ctx
.shader_info
= shader_info
;
5080 ac_llvm_context_init(&ctx
.ac
, ctx
.context
);
5081 ctx
.ac
.module
= ctx
.module
;
5083 ctx
.is_gs_copy_shader
= true;
5084 LLVMSetTarget(ctx
.module
, "amdgcn--");
5087 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
5088 ctx
.ac
.builder
= ctx
.builder
;
5089 ctx
.stage
= MESA_SHADER_VERTEX
;
5091 create_function(&ctx
);
5093 ctx
.gs_max_out_vertices
= geom_shader
->info
->gs
.vertices_out
;
5094 ac_setup_rings(&ctx
);
5096 nir_foreach_variable(variable
, &geom_shader
->outputs
)
5097 handle_shader_output_decl(&ctx
, variable
);
5099 ac_gs_copy_shader_emit(&ctx
);
5101 LLVMBuildRetVoid(ctx
.builder
);
5103 ac_llvm_finalize_module(&ctx
);
5105 ac_compile_llvm_module(tm
, ctx
.module
, binary
, config
, shader_info
,
5107 dump_shader
, options
->supports_spill
);