2 * Copyright © 2016 Bas Nieuwenhuizen
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 #include "ac_nir_to_llvm.h"
25 #include "ac_llvm_util.h"
26 #include "ac_binary.h"
29 #include "../vulkan/radv_descriptor_set.h"
30 #include "util/bitscan.h"
31 #include <llvm-c/Transforms/Scalar.h>
33 enum radeon_llvm_calling_convention
{
34 RADEON_LLVM_AMDGPU_VS
= 87,
35 RADEON_LLVM_AMDGPU_GS
= 88,
36 RADEON_LLVM_AMDGPU_PS
= 89,
37 RADEON_LLVM_AMDGPU_CS
= 90,
40 #define CONST_ADDR_SPACE 2
41 #define LOCAL_ADDR_SPACE 3
43 #define RADEON_LLVM_MAX_INPUTS (VARYING_SLOT_VAR31 + 1)
44 #define RADEON_LLVM_MAX_OUTPUTS (VARYING_SLOT_VAR31 + 1)
47 #define SENDMSG_GS_DONE 3
49 #define SENDMSG_GS_OP_NOP (0 << 4)
50 #define SENDMSG_GS_OP_CUT (1 << 4)
51 #define SENDMSG_GS_OP_EMIT (2 << 4)
52 #define SENDMSG_GS_OP_EMIT_CUT (3 << 4)
61 struct nir_to_llvm_context
{
62 struct ac_llvm_context ac
;
63 const struct ac_nir_compiler_options
*options
;
64 struct ac_shader_variant_info
*shader_info
;
66 LLVMContextRef context
;
68 LLVMBuilderRef builder
;
69 LLVMValueRef main_function
;
71 struct hash_table
*defs
;
72 struct hash_table
*phis
;
74 LLVMValueRef descriptor_sets
[AC_UD_MAX_SETS
];
75 LLVMValueRef ring_offsets
;
76 LLVMValueRef push_constants
;
77 LLVMValueRef num_work_groups
;
78 LLVMValueRef workgroup_ids
;
79 LLVMValueRef local_invocation_ids
;
82 LLVMValueRef vertex_buffers
;
83 LLVMValueRef base_vertex
;
84 LLVMValueRef start_instance
;
85 LLVMValueRef vertex_id
;
86 LLVMValueRef rel_auto_id
;
87 LLVMValueRef vs_prim_id
;
88 LLVMValueRef instance_id
;
90 LLVMValueRef gsvs_ring_stride
;
91 LLVMValueRef gsvs_num_entries
;
92 LLVMValueRef gs2vs_offset
;
93 LLVMValueRef gs_wave_id
;
94 LLVMValueRef gs_vtx_offset
[6];
95 LLVMValueRef gs_prim_id
, gs_invocation_id
;
97 LLVMValueRef esgs_ring
;
98 LLVMValueRef gsvs_ring
;
100 LLVMValueRef prim_mask
;
101 LLVMValueRef sample_positions
;
102 LLVMValueRef persp_sample
, persp_center
, persp_centroid
;
103 LLVMValueRef linear_sample
, linear_center
, linear_centroid
;
104 LLVMValueRef front_face
;
105 LLVMValueRef ancillary
;
106 LLVMValueRef frag_pos
[4];
108 LLVMBasicBlockRef continue_block
;
109 LLVMBasicBlockRef break_block
;
127 LLVMValueRef i32zero
;
129 LLVMValueRef f32zero
;
131 LLVMValueRef v4f32empty
;
133 unsigned range_md_kind
;
134 unsigned uniform_md_kind
;
135 unsigned invariant_load_md_kind
;
136 LLVMValueRef empty_md
;
137 gl_shader_stage stage
;
140 LLVMValueRef inputs
[RADEON_LLVM_MAX_INPUTS
* 4];
141 LLVMValueRef outputs
[RADEON_LLVM_MAX_OUTPUTS
* 4];
143 LLVMValueRef shared_memory
;
145 uint64_t output_mask
;
147 LLVMValueRef
*locals
;
152 bool has_ds_bpermute
;
154 bool is_gs_copy_shader
;
155 LLVMValueRef gs_next_vertex
;
156 unsigned gs_max_out_vertices
;
160 LLVMValueRef args
[12];
162 LLVMTypeRef dst_type
;
166 static LLVMValueRef
get_sampler_desc(struct nir_to_llvm_context
*ctx
,
167 nir_deref_var
*deref
,
168 enum desc_type desc_type
);
169 static unsigned radeon_llvm_reg_index_soa(unsigned index
, unsigned chan
)
171 return (index
* 4) + chan
;
174 static unsigned llvm_get_type_size(LLVMTypeRef type
)
176 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
179 case LLVMIntegerTypeKind
:
180 return LLVMGetIntTypeWidth(type
) / 8;
181 case LLVMFloatTypeKind
:
183 case LLVMPointerTypeKind
:
185 case LLVMVectorTypeKind
:
186 return LLVMGetVectorSize(type
) *
187 llvm_get_type_size(LLVMGetElementType(type
));
194 static void set_llvm_calling_convention(LLVMValueRef func
,
195 gl_shader_stage stage
)
197 enum radeon_llvm_calling_convention calling_conv
;
200 case MESA_SHADER_VERTEX
:
201 case MESA_SHADER_TESS_CTRL
:
202 case MESA_SHADER_TESS_EVAL
:
203 calling_conv
= RADEON_LLVM_AMDGPU_VS
;
205 case MESA_SHADER_GEOMETRY
:
206 calling_conv
= RADEON_LLVM_AMDGPU_GS
;
208 case MESA_SHADER_FRAGMENT
:
209 calling_conv
= RADEON_LLVM_AMDGPU_PS
;
211 case MESA_SHADER_COMPUTE
:
212 calling_conv
= RADEON_LLVM_AMDGPU_CS
;
215 unreachable("Unhandle shader type");
218 LLVMSetFunctionCallConv(func
, calling_conv
);
222 create_llvm_function(LLVMContextRef ctx
, LLVMModuleRef module
,
223 LLVMBuilderRef builder
, LLVMTypeRef
*return_types
,
224 unsigned num_return_elems
, LLVMTypeRef
*param_types
,
225 unsigned param_count
, unsigned array_params_mask
,
226 unsigned sgpr_params
, bool unsafe_math
)
228 LLVMTypeRef main_function_type
, ret_type
;
229 LLVMBasicBlockRef main_function_body
;
231 if (num_return_elems
)
232 ret_type
= LLVMStructTypeInContext(ctx
, return_types
,
233 num_return_elems
, true);
235 ret_type
= LLVMVoidTypeInContext(ctx
);
237 /* Setup the function */
239 LLVMFunctionType(ret_type
, param_types
, param_count
, 0);
240 LLVMValueRef main_function
=
241 LLVMAddFunction(module
, "main", main_function_type
);
243 LLVMAppendBasicBlockInContext(ctx
, main_function
, "main_body");
244 LLVMPositionBuilderAtEnd(builder
, main_function_body
);
246 LLVMSetFunctionCallConv(main_function
, RADEON_LLVM_AMDGPU_CS
);
247 for (unsigned i
= 0; i
< sgpr_params
; ++i
) {
248 if (array_params_mask
& (1 << i
)) {
249 LLVMValueRef P
= LLVMGetParam(main_function
, i
);
250 ac_add_function_attr(main_function
, i
+ 1, AC_FUNC_ATTR_BYVAL
);
251 ac_add_attr_dereferenceable(P
, UINT64_MAX
);
254 ac_add_function_attr(main_function
, i
+ 1, AC_FUNC_ATTR_INREG
);
259 /* These were copied from some LLVM test. */
260 LLVMAddTargetDependentFunctionAttr(main_function
,
261 "less-precise-fpmad",
263 LLVMAddTargetDependentFunctionAttr(main_function
,
266 LLVMAddTargetDependentFunctionAttr(main_function
,
269 LLVMAddTargetDependentFunctionAttr(main_function
,
273 return main_function
;
276 static LLVMTypeRef
const_array(LLVMTypeRef elem_type
, int num_elements
)
278 return LLVMPointerType(LLVMArrayType(elem_type
, num_elements
),
282 static LLVMValueRef
get_shared_memory_ptr(struct nir_to_llvm_context
*ctx
,
290 offset
= LLVMConstInt(ctx
->i32
, idx
, false);
292 ptr
= ctx
->shared_memory
;
293 ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &offset
, 1, "");
294 addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
295 ptr
= LLVMBuildBitCast(ctx
->builder
, ptr
, LLVMPointerType(type
, addr_space
), "");
299 static LLVMValueRef
to_integer(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
)
301 LLVMTypeRef type
= LLVMTypeOf(v
);
302 if (type
== ctx
->f32
) {
303 return LLVMBuildBitCast(ctx
->builder
, v
, ctx
->i32
, "");
304 } else if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
305 LLVMTypeRef elem_type
= LLVMGetElementType(type
);
306 if (elem_type
== ctx
->f32
) {
307 LLVMTypeRef nt
= LLVMVectorType(ctx
->i32
, LLVMGetVectorSize(type
));
308 return LLVMBuildBitCast(ctx
->builder
, v
, nt
, "");
314 static LLVMValueRef
to_float(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
)
316 LLVMTypeRef type
= LLVMTypeOf(v
);
317 if (type
== ctx
->i32
) {
318 return LLVMBuildBitCast(ctx
->builder
, v
, ctx
->f32
, "");
319 } else if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
320 LLVMTypeRef elem_type
= LLVMGetElementType(type
);
321 if (elem_type
== ctx
->i32
) {
322 LLVMTypeRef nt
= LLVMVectorType(ctx
->f32
, LLVMGetVectorSize(type
));
323 return LLVMBuildBitCast(ctx
->builder
, v
, nt
, "");
329 static LLVMValueRef
unpack_param(struct nir_to_llvm_context
*ctx
,
330 LLVMValueRef param
, unsigned rshift
,
333 LLVMValueRef value
= param
;
335 value
= LLVMBuildLShr(ctx
->builder
, value
,
336 LLVMConstInt(ctx
->i32
, rshift
, false), "");
338 if (rshift
+ bitwidth
< 32) {
339 unsigned mask
= (1 << bitwidth
) - 1;
340 value
= LLVMBuildAnd(ctx
->builder
, value
,
341 LLVMConstInt(ctx
->i32
, mask
, false), "");
346 static LLVMValueRef
build_gep0(struct nir_to_llvm_context
*ctx
,
347 LLVMValueRef base_ptr
, LLVMValueRef index
)
349 LLVMValueRef indices
[2] = {
353 return LLVMBuildGEP(ctx
->builder
, base_ptr
,
357 static LLVMValueRef
build_indexed_load(struct nir_to_llvm_context
*ctx
,
358 LLVMValueRef base_ptr
, LLVMValueRef index
,
361 LLVMValueRef pointer
;
362 pointer
= build_gep0(ctx
, base_ptr
, index
);
364 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
365 return LLVMBuildLoad(ctx
->builder
, pointer
, "");
368 static LLVMValueRef
build_indexed_load_const(struct nir_to_llvm_context
*ctx
,
369 LLVMValueRef base_ptr
, LLVMValueRef index
)
371 LLVMValueRef result
= build_indexed_load(ctx
, base_ptr
, index
, true);
372 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
376 static void set_userdata_location(struct ac_userdata_info
*ud_info
, uint8_t sgpr_idx
, uint8_t num_sgprs
)
378 ud_info
->sgpr_idx
= sgpr_idx
;
379 ud_info
->num_sgprs
= num_sgprs
;
380 ud_info
->indirect
= false;
381 ud_info
->indirect_offset
= 0;
384 static void set_userdata_location_shader(struct nir_to_llvm_context
*ctx
,
385 int idx
, uint8_t sgpr_idx
, uint8_t num_sgprs
)
387 set_userdata_location(&ctx
->shader_info
->user_sgprs_locs
.shader_data
[idx
], sgpr_idx
, num_sgprs
);
391 static void set_userdata_location_indirect(struct ac_userdata_info
*ud_info
, uint8_t sgpr_idx
, uint8_t num_sgprs
,
392 uint32_t indirect_offset
)
394 ud_info
->sgpr_idx
= sgpr_idx
;
395 ud_info
->num_sgprs
= num_sgprs
;
396 ud_info
->indirect
= true;
397 ud_info
->indirect_offset
= indirect_offset
;
401 static void create_function(struct nir_to_llvm_context
*ctx
)
403 LLVMTypeRef arg_types
[23];
404 unsigned arg_idx
= 0;
405 unsigned array_params_mask
= 0;
406 unsigned sgpr_count
= 0, user_sgpr_count
;
408 unsigned num_sets
= ctx
->options
->layout
? ctx
->options
->layout
->num_sets
: 0;
409 unsigned user_sgpr_idx
;
410 bool need_push_constants
;
411 bool need_ring_offsets
= false;
413 /* until we sort out scratch/global buffers always assign ring offsets for gs/vs/es */
414 if (ctx
->stage
== MESA_SHADER_GEOMETRY
||
415 ctx
->stage
== MESA_SHADER_VERTEX
||
416 ctx
->is_gs_copy_shader
)
417 need_ring_offsets
= true;
419 need_push_constants
= true;
420 if (!ctx
->options
->layout
)
421 need_push_constants
= false;
422 else if (!ctx
->options
->layout
->push_constant_size
&&
423 !ctx
->options
->layout
->dynamic_offset_count
)
424 need_push_constants
= false;
426 if (need_ring_offsets
&& !ctx
->options
->supports_spill
) {
427 arg_types
[arg_idx
++] = const_array(ctx
->v16i8
, 8); /* address of rings */
430 /* 1 for each descriptor set */
431 for (unsigned i
= 0; i
< num_sets
; ++i
) {
432 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& (1 << ctx
->stage
)) {
433 array_params_mask
|= (1 << arg_idx
);
434 arg_types
[arg_idx
++] = const_array(ctx
->i8
, 1024 * 1024);
438 if (need_push_constants
) {
439 /* 1 for push constants and dynamic descriptors */
440 array_params_mask
|= (1 << arg_idx
);
441 arg_types
[arg_idx
++] = const_array(ctx
->i8
, 1024 * 1024);
444 switch (ctx
->stage
) {
445 case MESA_SHADER_COMPUTE
:
446 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3); /* grid size */
447 user_sgpr_count
= arg_idx
;
448 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3);
449 arg_types
[arg_idx
++] = ctx
->i32
;
450 sgpr_count
= arg_idx
;
452 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3);
454 case MESA_SHADER_VERTEX
:
455 arg_types
[arg_idx
++] = const_array(ctx
->v16i8
, 16); /* vertex buffers */
456 arg_types
[arg_idx
++] = ctx
->i32
; // base vertex
457 arg_types
[arg_idx
++] = ctx
->i32
; // start instance
458 user_sgpr_count
= sgpr_count
= arg_idx
;
459 arg_types
[arg_idx
++] = ctx
->i32
; // vertex id
460 arg_types
[arg_idx
++] = ctx
->i32
; // rel auto id
461 arg_types
[arg_idx
++] = ctx
->i32
; // vs prim id
462 arg_types
[arg_idx
++] = ctx
->i32
; // instance id
464 case MESA_SHADER_GEOMETRY
:
465 arg_types
[arg_idx
++] = ctx
->i32
; // gsvs stride
466 arg_types
[arg_idx
++] = ctx
->i32
; // gsvs num entires
467 user_sgpr_count
= arg_idx
;
468 arg_types
[arg_idx
++] = ctx
->i32
; // gs2vs offset
469 arg_types
[arg_idx
++] = ctx
->i32
; // wave id
470 sgpr_count
= arg_idx
;
471 arg_types
[arg_idx
++] = ctx
->i32
; // vtx0
472 arg_types
[arg_idx
++] = ctx
->i32
; // vtx1
473 arg_types
[arg_idx
++] = ctx
->i32
; // prim id
474 arg_types
[arg_idx
++] = ctx
->i32
; // vtx2
475 arg_types
[arg_idx
++] = ctx
->i32
; // vtx3
476 arg_types
[arg_idx
++] = ctx
->i32
; // vtx4
477 arg_types
[arg_idx
++] = ctx
->i32
; // vtx5
478 arg_types
[arg_idx
++] = ctx
->i32
; // GS instance id
480 case MESA_SHADER_FRAGMENT
:
481 arg_types
[arg_idx
++] = const_array(ctx
->f32
, 32); /* sample positions */
482 user_sgpr_count
= arg_idx
;
483 arg_types
[arg_idx
++] = ctx
->i32
; /* prim mask */
484 sgpr_count
= arg_idx
;
485 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp sample */
486 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp center */
487 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp centroid */
488 arg_types
[arg_idx
++] = ctx
->v3i32
; /* persp pull model */
489 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear sample */
490 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear center */
491 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear centroid */
492 arg_types
[arg_idx
++] = ctx
->f32
; /* line stipple tex */
493 arg_types
[arg_idx
++] = ctx
->f32
; /* pos x float */
494 arg_types
[arg_idx
++] = ctx
->f32
; /* pos y float */
495 arg_types
[arg_idx
++] = ctx
->f32
; /* pos z float */
496 arg_types
[arg_idx
++] = ctx
->f32
; /* pos w float */
497 arg_types
[arg_idx
++] = ctx
->i32
; /* front face */
498 arg_types
[arg_idx
++] = ctx
->i32
; /* ancillary */
499 arg_types
[arg_idx
++] = ctx
->f32
; /* sample coverage */
500 arg_types
[arg_idx
++] = ctx
->i32
; /* fixed pt */
503 unreachable("Shader stage not implemented");
506 ctx
->main_function
= create_llvm_function(
507 ctx
->context
, ctx
->module
, ctx
->builder
, NULL
, 0, arg_types
,
508 arg_idx
, array_params_mask
, sgpr_count
, ctx
->options
->unsafe_math
);
509 set_llvm_calling_convention(ctx
->main_function
, ctx
->stage
);
511 ctx
->shader_info
->num_input_sgprs
= 0;
512 ctx
->shader_info
->num_input_vgprs
= 0;
514 ctx
->shader_info
->num_user_sgprs
= ctx
->options
->supports_spill
? 2 : 0;
515 for (i
= 0; i
< user_sgpr_count
; i
++)
516 ctx
->shader_info
->num_user_sgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
518 ctx
->shader_info
->num_input_sgprs
= ctx
->shader_info
->num_user_sgprs
;
519 for (; i
< sgpr_count
; i
++)
520 ctx
->shader_info
->num_input_sgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
522 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
523 for (; i
< arg_idx
; ++i
)
524 ctx
->shader_info
->num_input_vgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
529 if (ctx
->options
->supports_spill
|| need_ring_offsets
) {
530 set_userdata_location_shader(ctx
, AC_UD_SCRATCH_RING_OFFSETS
, user_sgpr_idx
, 2);
532 if (ctx
->options
->supports_spill
) {
533 ctx
->ring_offsets
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.amdgcn.implicit.buffer.ptr",
534 LLVMPointerType(ctx
->i8
, CONST_ADDR_SPACE
),
535 NULL
, 0, AC_FUNC_ATTR_READNONE
);
536 ctx
->ring_offsets
= LLVMBuildBitCast(ctx
->builder
, ctx
->ring_offsets
,
537 const_array(ctx
->v16i8
, 8), "");
539 ctx
->ring_offsets
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
542 for (unsigned i
= 0; i
< num_sets
; ++i
) {
543 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& (1 << ctx
->stage
)) {
544 set_userdata_location(&ctx
->shader_info
->user_sgprs_locs
.descriptor_sets
[i
], user_sgpr_idx
, 2);
546 ctx
->descriptor_sets
[i
] =
547 LLVMGetParam(ctx
->main_function
, arg_idx
++);
549 ctx
->descriptor_sets
[i
] = NULL
;
552 if (need_push_constants
) {
553 ctx
->push_constants
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
554 set_userdata_location_shader(ctx
, AC_UD_PUSH_CONSTANTS
, user_sgpr_idx
, 2);
558 switch (ctx
->stage
) {
559 case MESA_SHADER_COMPUTE
:
560 set_userdata_location_shader(ctx
, AC_UD_CS_GRID_SIZE
, user_sgpr_idx
, 3);
562 ctx
->num_work_groups
=
563 LLVMGetParam(ctx
->main_function
, arg_idx
++);
565 LLVMGetParam(ctx
->main_function
, arg_idx
++);
567 LLVMGetParam(ctx
->main_function
, arg_idx
++);
568 ctx
->local_invocation_ids
=
569 LLVMGetParam(ctx
->main_function
, arg_idx
++);
571 case MESA_SHADER_VERTEX
:
572 set_userdata_location_shader(ctx
, AC_UD_VS_VERTEX_BUFFERS
, user_sgpr_idx
, 2);
574 ctx
->vertex_buffers
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
575 set_userdata_location_shader(ctx
, AC_UD_VS_BASE_VERTEX_START_INSTANCE
, user_sgpr_idx
, 2);
577 ctx
->base_vertex
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
578 ctx
->start_instance
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
579 ctx
->vertex_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
580 ctx
->rel_auto_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
581 ctx
->vs_prim_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
582 ctx
->instance_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
584 case MESA_SHADER_GEOMETRY
:
585 set_userdata_location_shader(ctx
, AC_UD_GS_VS_RING_STRIDE_ENTRIES
, user_sgpr_idx
, 2);
587 ctx
->gsvs_ring_stride
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
588 ctx
->gsvs_num_entries
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
589 ctx
->gs2vs_offset
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
590 ctx
->gs_wave_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
591 ctx
->gs_vtx_offset
[0] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
592 ctx
->gs_vtx_offset
[1] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
593 ctx
->gs_prim_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
594 ctx
->gs_vtx_offset
[2] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
595 ctx
->gs_vtx_offset
[3] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
596 ctx
->gs_vtx_offset
[4] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
597 ctx
->gs_vtx_offset
[5] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
598 ctx
->gs_invocation_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
600 case MESA_SHADER_FRAGMENT
:
601 set_userdata_location_shader(ctx
, AC_UD_PS_SAMPLE_POS
, user_sgpr_idx
, 2);
603 ctx
->sample_positions
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
604 ctx
->prim_mask
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
605 ctx
->persp_sample
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
606 ctx
->persp_center
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
607 ctx
->persp_centroid
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
609 ctx
->linear_sample
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
610 ctx
->linear_center
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
611 ctx
->linear_centroid
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
612 arg_idx
++; /* line stipple */
613 ctx
->frag_pos
[0] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
614 ctx
->frag_pos
[1] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
615 ctx
->frag_pos
[2] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
616 ctx
->frag_pos
[3] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
617 ctx
->front_face
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
618 ctx
->ancillary
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
621 unreachable("Shader stage not implemented");
625 static void setup_types(struct nir_to_llvm_context
*ctx
)
627 LLVMValueRef args
[4];
629 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->context
);
630 ctx
->i1
= LLVMIntTypeInContext(ctx
->context
, 1);
631 ctx
->i8
= LLVMIntTypeInContext(ctx
->context
, 8);
632 ctx
->i16
= LLVMIntTypeInContext(ctx
->context
, 16);
633 ctx
->i32
= LLVMIntTypeInContext(ctx
->context
, 32);
634 ctx
->i64
= LLVMIntTypeInContext(ctx
->context
, 64);
635 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
636 ctx
->v3i32
= LLVMVectorType(ctx
->i32
, 3);
637 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
638 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
639 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
640 ctx
->f16
= LLVMHalfTypeInContext(ctx
->context
);
641 ctx
->v2f32
= LLVMVectorType(ctx
->f32
, 2);
642 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
643 ctx
->v16i8
= LLVMVectorType(ctx
->i8
, 16);
645 ctx
->i32zero
= LLVMConstInt(ctx
->i32
, 0, false);
646 ctx
->i32one
= LLVMConstInt(ctx
->i32
, 1, false);
647 ctx
->f32zero
= LLVMConstReal(ctx
->f32
, 0.0);
648 ctx
->f32one
= LLVMConstReal(ctx
->f32
, 1.0);
650 args
[0] = ctx
->f32zero
;
651 args
[1] = ctx
->f32zero
;
652 args
[2] = ctx
->f32zero
;
653 args
[3] = ctx
->f32one
;
654 ctx
->v4f32empty
= LLVMConstVector(args
, 4);
656 ctx
->range_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
658 ctx
->invariant_load_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
659 "invariant.load", 14);
660 ctx
->uniform_md_kind
=
661 LLVMGetMDKindIDInContext(ctx
->context
, "amdgpu.uniform", 14);
662 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
664 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
667 static int get_llvm_num_components(LLVMValueRef value
)
669 LLVMTypeRef type
= LLVMTypeOf(value
);
670 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
671 ? LLVMGetVectorSize(type
)
673 return num_components
;
676 static LLVMValueRef
llvm_extract_elem(struct nir_to_llvm_context
*ctx
,
680 int count
= get_llvm_num_components(value
);
682 assert(index
< count
);
686 return LLVMBuildExtractElement(ctx
->builder
, value
,
687 LLVMConstInt(ctx
->i32
, index
, false), "");
690 static LLVMValueRef
trim_vector(struct nir_to_llvm_context
*ctx
,
691 LLVMValueRef value
, unsigned count
)
693 unsigned num_components
= get_llvm_num_components(value
);
694 if (count
== num_components
)
697 LLVMValueRef masks
[] = {
698 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
699 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false)};
702 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
705 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
706 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
710 build_store_values_extended(struct nir_to_llvm_context
*ctx
,
711 LLVMValueRef
*values
,
712 unsigned value_count
,
713 unsigned value_stride
,
716 LLVMBuilderRef builder
= ctx
->builder
;
719 if (value_count
== 1) {
720 LLVMBuildStore(builder
, vec
, values
[0]);
724 for (i
= 0; i
< value_count
; i
++) {
725 LLVMValueRef ptr
= values
[i
* value_stride
];
726 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
727 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
728 LLVMBuildStore(builder
, value
, ptr
);
732 static LLVMTypeRef
get_def_type(struct nir_to_llvm_context
*ctx
,
735 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->context
, def
->bit_size
);
736 if (def
->num_components
> 1) {
737 type
= LLVMVectorType(type
, def
->num_components
);
742 static LLVMValueRef
get_src(struct nir_to_llvm_context
*ctx
, nir_src src
)
745 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->defs
, src
.ssa
);
746 return (LLVMValueRef
)entry
->data
;
750 static LLVMBasicBlockRef
get_block(struct nir_to_llvm_context
*ctx
,
753 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->defs
, b
);
754 return (LLVMBasicBlockRef
)entry
->data
;
757 static LLVMValueRef
get_alu_src(struct nir_to_llvm_context
*ctx
,
759 unsigned num_components
)
761 LLVMValueRef value
= get_src(ctx
, src
.src
);
762 bool need_swizzle
= false;
765 LLVMTypeRef type
= LLVMTypeOf(value
);
766 unsigned src_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
767 ? LLVMGetVectorSize(type
)
770 for (unsigned i
= 0; i
< num_components
; ++i
) {
771 assert(src
.swizzle
[i
] < src_components
);
772 if (src
.swizzle
[i
] != i
)
776 if (need_swizzle
|| num_components
!= src_components
) {
777 LLVMValueRef masks
[] = {
778 LLVMConstInt(ctx
->i32
, src
.swizzle
[0], false),
779 LLVMConstInt(ctx
->i32
, src
.swizzle
[1], false),
780 LLVMConstInt(ctx
->i32
, src
.swizzle
[2], false),
781 LLVMConstInt(ctx
->i32
, src
.swizzle
[3], false)};
783 if (src_components
> 1 && num_components
== 1) {
784 value
= LLVMBuildExtractElement(ctx
->builder
, value
,
786 } else if (src_components
== 1 && num_components
> 1) {
787 LLVMValueRef values
[] = {value
, value
, value
, value
};
788 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
790 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
791 value
= LLVMBuildShuffleVector(ctx
->builder
, value
, value
,
800 static LLVMValueRef
emit_int_cmp(struct nir_to_llvm_context
*ctx
,
801 LLVMIntPredicate pred
, LLVMValueRef src0
,
804 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
805 return LLVMBuildSelect(ctx
->builder
, result
,
806 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
807 LLVMConstInt(ctx
->i32
, 0, false), "");
810 static LLVMValueRef
emit_float_cmp(struct nir_to_llvm_context
*ctx
,
811 LLVMRealPredicate pred
, LLVMValueRef src0
,
815 src0
= to_float(ctx
, src0
);
816 src1
= to_float(ctx
, src1
);
817 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
818 return LLVMBuildSelect(ctx
->builder
, result
,
819 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
820 LLVMConstInt(ctx
->i32
, 0, false), "");
823 static LLVMValueRef
emit_intrin_1f_param(struct nir_to_llvm_context
*ctx
,
827 LLVMValueRef params
[] = {
830 return ac_emit_llvm_intrinsic(&ctx
->ac
, intrin
, ctx
->f32
, params
, 1, AC_FUNC_ATTR_READNONE
);
833 static LLVMValueRef
emit_intrin_2f_param(struct nir_to_llvm_context
*ctx
,
835 LLVMValueRef src0
, LLVMValueRef src1
)
837 LLVMValueRef params
[] = {
841 return ac_emit_llvm_intrinsic(&ctx
->ac
, intrin
, ctx
->f32
, params
, 2, AC_FUNC_ATTR_READNONE
);
844 static LLVMValueRef
emit_intrin_3f_param(struct nir_to_llvm_context
*ctx
,
846 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
848 LLVMValueRef params
[] = {
853 return ac_emit_llvm_intrinsic(&ctx
->ac
, intrin
, ctx
->f32
, params
, 3, AC_FUNC_ATTR_READNONE
);
856 static LLVMValueRef
emit_bcsel(struct nir_to_llvm_context
*ctx
,
857 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
859 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
861 return LLVMBuildSelect(ctx
->builder
, v
, src1
, src2
, "");
864 static LLVMValueRef
emit_find_lsb(struct nir_to_llvm_context
*ctx
,
867 LLVMValueRef params
[2] = {
870 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
871 * add special code to check for x=0. The reason is that
872 * the LLVM behavior for x=0 is different from what we
875 * The hardware already implements the correct behavior.
877 LLVMConstInt(ctx
->i32
, 1, false),
879 return ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.cttz.i32", ctx
->i32
, params
, 2, AC_FUNC_ATTR_READNONE
);
882 static LLVMValueRef
emit_ifind_msb(struct nir_to_llvm_context
*ctx
,
885 LLVMValueRef msb
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.AMDGPU.flbit.i32",
887 AC_FUNC_ATTR_READNONE
);
889 /* The HW returns the last bit index from MSB, but NIR wants
890 * the index from LSB. Invert it by doing "31 - msb". */
891 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
894 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
895 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
896 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
897 src0
, ctx
->i32zero
, ""),
898 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
899 src0
, all_ones
, ""), "");
901 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
904 static LLVMValueRef
emit_ufind_msb(struct nir_to_llvm_context
*ctx
,
907 LLVMValueRef args
[2] = {
911 LLVMValueRef msb
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.ctlz.i32",
912 ctx
->i32
, args
, ARRAY_SIZE(args
),
913 AC_FUNC_ATTR_READNONE
);
915 /* The HW returns the last bit index from MSB, but NIR wants
916 * the index from LSB. Invert it by doing "31 - msb". */
917 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
920 return LLVMBuildSelect(ctx
->builder
,
921 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src0
,
923 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
926 static LLVMValueRef
emit_minmax_int(struct nir_to_llvm_context
*ctx
,
927 LLVMIntPredicate pred
,
928 LLVMValueRef src0
, LLVMValueRef src1
)
930 return LLVMBuildSelect(ctx
->builder
,
931 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
936 static LLVMValueRef
emit_iabs(struct nir_to_llvm_context
*ctx
,
939 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
940 LLVMBuildNeg(ctx
->builder
, src0
, ""));
943 static LLVMValueRef
emit_fsign(struct nir_to_llvm_context
*ctx
,
946 LLVMValueRef cmp
, val
;
948 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, ctx
->f32zero
, "");
949 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->f32one
, src0
, "");
950 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, ctx
->f32zero
, "");
951 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(ctx
->f32
, -1.0), "");
955 static LLVMValueRef
emit_isign(struct nir_to_llvm_context
*ctx
,
958 LLVMValueRef cmp
, val
;
960 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, ctx
->i32zero
, "");
961 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->i32one
, src0
, "");
962 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, ctx
->i32zero
, "");
963 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(ctx
->i32
, -1, true), "");
967 static LLVMValueRef
emit_ffract(struct nir_to_llvm_context
*ctx
,
970 const char *intr
= "llvm.floor.f32";
971 LLVMValueRef fsrc0
= to_float(ctx
, src0
);
972 LLVMValueRef params
[] = {
975 LLVMValueRef floor
= ac_emit_llvm_intrinsic(&ctx
->ac
, intr
,
977 AC_FUNC_ATTR_READNONE
);
978 return LLVMBuildFSub(ctx
->builder
, fsrc0
, floor
, "");
981 static LLVMValueRef
emit_uint_carry(struct nir_to_llvm_context
*ctx
,
983 LLVMValueRef src0
, LLVMValueRef src1
)
985 LLVMTypeRef ret_type
;
986 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
988 LLVMValueRef params
[] = { src0
, src1
};
989 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
992 res
= ac_emit_llvm_intrinsic(&ctx
->ac
, intrin
, ret_type
,
993 params
, 2, AC_FUNC_ATTR_READNONE
);
995 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
996 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
1000 static LLVMValueRef
emit_b2f(struct nir_to_llvm_context
*ctx
,
1003 return LLVMBuildAnd(ctx
->builder
, src0
, LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""), "");
1006 static LLVMValueRef
emit_umul_high(struct nir_to_llvm_context
*ctx
,
1007 LLVMValueRef src0
, LLVMValueRef src1
)
1009 LLVMValueRef dst64
, result
;
1010 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
1011 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
1013 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1014 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1015 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1019 static LLVMValueRef
emit_imul_high(struct nir_to_llvm_context
*ctx
,
1020 LLVMValueRef src0
, LLVMValueRef src1
)
1022 LLVMValueRef dst64
, result
;
1023 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
1024 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
1026 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1027 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1028 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1032 static LLVMValueRef
emit_bitfield_extract(struct nir_to_llvm_context
*ctx
,
1034 LLVMValueRef srcs
[3])
1036 LLVMValueRef result
;
1037 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
1038 result
= ac_emit_llvm_intrinsic(&ctx
->ac
, intrin
, ctx
->i32
, srcs
, 3, AC_FUNC_ATTR_READNONE
);
1040 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
1044 static LLVMValueRef
emit_bitfield_insert(struct nir_to_llvm_context
*ctx
,
1045 LLVMValueRef src0
, LLVMValueRef src1
,
1046 LLVMValueRef src2
, LLVMValueRef src3
)
1048 LLVMValueRef bfi_args
[3], result
;
1050 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
1051 LLVMBuildSub(ctx
->builder
,
1052 LLVMBuildShl(ctx
->builder
,
1057 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
1060 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
1063 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
1064 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
1066 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
1067 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
1068 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
1070 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
1074 static LLVMValueRef
emit_pack_half_2x16(struct nir_to_llvm_context
*ctx
,
1077 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1079 LLVMValueRef comp
[2];
1081 src0
= to_float(ctx
, src0
);
1082 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32zero
, "");
1083 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32one
, "");
1084 for (i
= 0; i
< 2; i
++) {
1085 comp
[i
] = LLVMBuildFPTrunc(ctx
->builder
, comp
[i
], ctx
->f16
, "");
1086 comp
[i
] = LLVMBuildBitCast(ctx
->builder
, comp
[i
], ctx
->i16
, "");
1087 comp
[i
] = LLVMBuildZExt(ctx
->builder
, comp
[i
], ctx
->i32
, "");
1090 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
1091 comp
[0] = LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
1096 static LLVMValueRef
emit_unpack_half_2x16(struct nir_to_llvm_context
*ctx
,
1099 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1100 LLVMValueRef temps
[2], result
, val
;
1103 for (i
= 0; i
< 2; i
++) {
1104 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
1105 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
1106 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
1107 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
1110 result
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), temps
[0],
1112 result
= LLVMBuildInsertElement(ctx
->builder
, result
, temps
[1],
1118 * Set range metadata on an instruction. This can only be used on load and
1119 * call instructions. If you know an instruction can only produce the values
1120 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
1121 * \p lo is the minimum value inclusive.
1122 * \p hi is the maximum value exclusive.
1124 static void set_range_metadata(struct nir_to_llvm_context
*ctx
,
1125 LLVMValueRef value
, unsigned lo
, unsigned hi
)
1127 LLVMValueRef range_md
, md_args
[2];
1128 LLVMTypeRef type
= LLVMTypeOf(value
);
1129 LLVMContextRef context
= LLVMGetTypeContext(type
);
1131 md_args
[0] = LLVMConstInt(type
, lo
, false);
1132 md_args
[1] = LLVMConstInt(type
, hi
, false);
1133 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
1134 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
1137 static LLVMValueRef
get_thread_id(struct nir_to_llvm_context
*ctx
)
1140 LLVMValueRef tid_args
[2];
1141 tid_args
[0] = LLVMConstInt(ctx
->i32
, 0xffffffff, false);
1142 tid_args
[1] = ctx
->i32zero
;
1143 tid_args
[1] = ac_emit_llvm_intrinsic(&ctx
->ac
,
1144 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
1145 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
1147 tid
= ac_emit_llvm_intrinsic(&ctx
->ac
,
1148 "llvm.amdgcn.mbcnt.hi", ctx
->i32
,
1149 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
1150 set_range_metadata(ctx
, tid
, 0, 64);
1155 * SI implements derivatives using the local data store (LDS)
1156 * All writes to the LDS happen in all executing threads at
1157 * the same time. TID is the Thread ID for the current
1158 * thread and is a value between 0 and 63, representing
1159 * the thread's position in the wavefront.
1161 * For the pixel shader threads are grouped into quads of four pixels.
1162 * The TIDs of the pixels of a quad are:
1170 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
1171 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
1172 * the current pixel's column, and masking with 0xfffffffe yields the TID
1173 * of the left pixel of the current pixel's row.
1175 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
1176 * adding 2 yields the TID of the pixel below the top pixel.
1178 /* masks for thread ID. */
1179 #define TID_MASK_TOP_LEFT 0xfffffffc
1180 #define TID_MASK_TOP 0xfffffffd
1181 #define TID_MASK_LEFT 0xfffffffe
1182 static LLVMValueRef
emit_ddxy(struct nir_to_llvm_context
*ctx
,
1186 LLVMValueRef tl
, trbl
, result
;
1187 LLVMValueRef tl_tid
, trbl_tid
;
1188 LLVMValueRef args
[2];
1189 LLVMValueRef thread_id
;
1192 ctx
->has_ddxy
= true;
1194 if (!ctx
->lds
&& !ctx
->has_ds_bpermute
)
1195 ctx
->lds
= LLVMAddGlobalInAddressSpace(ctx
->module
,
1196 LLVMArrayType(ctx
->i32
, 64),
1197 "ddxy_lds", LOCAL_ADDR_SPACE
);
1199 thread_id
= get_thread_id(ctx
);
1200 if (op
== nir_op_fddx_fine
|| op
== nir_op_fddx
)
1201 mask
= TID_MASK_LEFT
;
1202 else if (op
== nir_op_fddy_fine
|| op
== nir_op_fddy
)
1203 mask
= TID_MASK_TOP
;
1205 mask
= TID_MASK_TOP_LEFT
;
1207 tl_tid
= LLVMBuildAnd(ctx
->builder
, thread_id
,
1208 LLVMConstInt(ctx
->i32
, mask
, false), "");
1209 /* for DDX we want to next X pixel, DDY next Y pixel. */
1210 if (op
== nir_op_fddx_fine
||
1211 op
== nir_op_fddx_coarse
||
1217 trbl_tid
= LLVMBuildAdd(ctx
->builder
, tl_tid
,
1218 LLVMConstInt(ctx
->i32
, idx
, false), "");
1220 if (ctx
->has_ds_bpermute
) {
1221 args
[0] = LLVMBuildMul(ctx
->builder
, tl_tid
,
1222 LLVMConstInt(ctx
->i32
, 4, false), "");
1224 tl
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.amdgcn.ds.bpermute",
1226 AC_FUNC_ATTR_READNONE
);
1228 args
[0] = LLVMBuildMul(ctx
->builder
, trbl_tid
,
1229 LLVMConstInt(ctx
->i32
, 4, false), "");
1230 trbl
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.amdgcn.ds.bpermute",
1232 AC_FUNC_ATTR_READNONE
);
1234 LLVMValueRef store_ptr
, load_ptr0
, load_ptr1
;
1236 store_ptr
= build_gep0(ctx
, ctx
->lds
, thread_id
);
1237 load_ptr0
= build_gep0(ctx
, ctx
->lds
, tl_tid
);
1238 load_ptr1
= build_gep0(ctx
, ctx
->lds
, trbl_tid
);
1240 LLVMBuildStore(ctx
->builder
, src0
, store_ptr
);
1241 tl
= LLVMBuildLoad(ctx
->builder
, load_ptr0
, "");
1242 trbl
= LLVMBuildLoad(ctx
->builder
, load_ptr1
, "");
1244 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, ctx
->f32
, "");
1245 trbl
= LLVMBuildBitCast(ctx
->builder
, trbl
, ctx
->f32
, "");
1246 result
= LLVMBuildFSub(ctx
->builder
, trbl
, tl
, "");
1251 * this takes an I,J coordinate pair,
1252 * and works out the X and Y derivatives.
1253 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
1255 static LLVMValueRef
emit_ddxy_interp(
1256 struct nir_to_llvm_context
*ctx
,
1257 LLVMValueRef interp_ij
)
1259 LLVMValueRef result
[4], a
;
1262 for (i
= 0; i
< 2; i
++) {
1263 a
= LLVMBuildExtractElement(ctx
->builder
, interp_ij
,
1264 LLVMConstInt(ctx
->i32
, i
, false), "");
1265 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
1266 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
1268 return ac_build_gather_values(&ctx
->ac
, result
, 4);
1271 static void visit_alu(struct nir_to_llvm_context
*ctx
, nir_alu_instr
*instr
)
1273 LLVMValueRef src
[4], result
= NULL
;
1274 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
1275 unsigned src_components
;
1277 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
1278 switch (instr
->op
) {
1284 case nir_op_pack_half_2x16
:
1287 case nir_op_unpack_half_2x16
:
1291 src_components
= num_components
;
1294 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1295 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
1297 switch (instr
->op
) {
1303 src
[0] = to_float(ctx
, src
[0]);
1304 result
= LLVMBuildFNeg(ctx
->builder
, src
[0], "");
1307 result
= LLVMBuildNeg(ctx
->builder
, src
[0], "");
1310 result
= LLVMBuildNot(ctx
->builder
, src
[0], "");
1313 result
= LLVMBuildAdd(ctx
->builder
, src
[0], src
[1], "");
1316 src
[0] = to_float(ctx
, src
[0]);
1317 src
[1] = to_float(ctx
, src
[1]);
1318 result
= LLVMBuildFAdd(ctx
->builder
, src
[0], src
[1], "");
1321 src
[0] = to_float(ctx
, src
[0]);
1322 src
[1] = to_float(ctx
, src
[1]);
1323 result
= LLVMBuildFSub(ctx
->builder
, src
[0], src
[1], "");
1326 result
= LLVMBuildSub(ctx
->builder
, src
[0], src
[1], "");
1329 result
= LLVMBuildMul(ctx
->builder
, src
[0], src
[1], "");
1332 result
= LLVMBuildSRem(ctx
->builder
, src
[0], src
[1], "");
1335 result
= LLVMBuildURem(ctx
->builder
, src
[0], src
[1], "");
1338 src
[0] = to_float(ctx
, src
[0]);
1339 src
[1] = to_float(ctx
, src
[1]);
1340 result
= ac_emit_fdiv(&ctx
->ac
, src
[0], src
[1]);
1341 result
= emit_intrin_1f_param(ctx
, "llvm.floor.f32", result
);
1342 result
= LLVMBuildFMul(ctx
->builder
, src
[1] , result
, "");
1343 result
= LLVMBuildFSub(ctx
->builder
, src
[0], result
, "");
1346 src
[0] = to_float(ctx
, src
[0]);
1347 src
[1] = to_float(ctx
, src
[1]);
1348 result
= LLVMBuildFRem(ctx
->builder
, src
[0], src
[1], "");
1351 result
= LLVMBuildSRem(ctx
->builder
, src
[0], src
[1], "");
1354 result
= LLVMBuildSDiv(ctx
->builder
, src
[0], src
[1], "");
1357 result
= LLVMBuildUDiv(ctx
->builder
, src
[0], src
[1], "");
1360 src
[0] = to_float(ctx
, src
[0]);
1361 src
[1] = to_float(ctx
, src
[1]);
1362 result
= LLVMBuildFMul(ctx
->builder
, src
[0], src
[1], "");
1365 src
[0] = to_float(ctx
, src
[0]);
1366 src
[1] = to_float(ctx
, src
[1]);
1367 result
= ac_emit_fdiv(&ctx
->ac
, src
[0], src
[1]);
1370 src
[0] = to_float(ctx
, src
[0]);
1371 result
= ac_emit_fdiv(&ctx
->ac
, ctx
->f32one
, src
[0]);
1374 result
= LLVMBuildAnd(ctx
->builder
, src
[0], src
[1], "");
1377 result
= LLVMBuildOr(ctx
->builder
, src
[0], src
[1], "");
1380 result
= LLVMBuildXor(ctx
->builder
, src
[0], src
[1], "");
1383 result
= LLVMBuildShl(ctx
->builder
, src
[0], src
[1], "");
1386 result
= LLVMBuildAShr(ctx
->builder
, src
[0], src
[1], "");
1389 result
= LLVMBuildLShr(ctx
->builder
, src
[0], src
[1], "");
1392 result
= emit_int_cmp(ctx
, LLVMIntSLT
, src
[0], src
[1]);
1395 result
= emit_int_cmp(ctx
, LLVMIntNE
, src
[0], src
[1]);
1398 result
= emit_int_cmp(ctx
, LLVMIntEQ
, src
[0], src
[1]);
1401 result
= emit_int_cmp(ctx
, LLVMIntSGE
, src
[0], src
[1]);
1404 result
= emit_int_cmp(ctx
, LLVMIntULT
, src
[0], src
[1]);
1407 result
= emit_int_cmp(ctx
, LLVMIntUGE
, src
[0], src
[1]);
1410 result
= emit_float_cmp(ctx
, LLVMRealUEQ
, src
[0], src
[1]);
1413 result
= emit_float_cmp(ctx
, LLVMRealUNE
, src
[0], src
[1]);
1416 result
= emit_float_cmp(ctx
, LLVMRealULT
, src
[0], src
[1]);
1419 result
= emit_float_cmp(ctx
, LLVMRealUGE
, src
[0], src
[1]);
1422 result
= emit_intrin_1f_param(ctx
, "llvm.fabs.f32", src
[0]);
1425 result
= emit_iabs(ctx
, src
[0]);
1428 result
= emit_minmax_int(ctx
, LLVMIntSGT
, src
[0], src
[1]);
1431 result
= emit_minmax_int(ctx
, LLVMIntSLT
, src
[0], src
[1]);
1434 result
= emit_minmax_int(ctx
, LLVMIntUGT
, src
[0], src
[1]);
1437 result
= emit_minmax_int(ctx
, LLVMIntULT
, src
[0], src
[1]);
1440 result
= emit_isign(ctx
, src
[0]);
1443 src
[0] = to_float(ctx
, src
[0]);
1444 result
= emit_fsign(ctx
, src
[0]);
1447 result
= emit_intrin_1f_param(ctx
, "llvm.floor.f32", src
[0]);
1450 result
= emit_intrin_1f_param(ctx
, "llvm.trunc.f32", src
[0]);
1453 result
= emit_intrin_1f_param(ctx
, "llvm.ceil.f32", src
[0]);
1455 case nir_op_fround_even
:
1456 result
= emit_intrin_1f_param(ctx
, "llvm.rint.f32", src
[0]);
1459 result
= emit_ffract(ctx
, src
[0]);
1462 result
= emit_intrin_1f_param(ctx
, "llvm.sin.f32", src
[0]);
1465 result
= emit_intrin_1f_param(ctx
, "llvm.cos.f32", src
[0]);
1468 result
= emit_intrin_1f_param(ctx
, "llvm.sqrt.f32", src
[0]);
1471 result
= emit_intrin_1f_param(ctx
, "llvm.exp2.f32", src
[0]);
1474 result
= emit_intrin_1f_param(ctx
, "llvm.log2.f32", src
[0]);
1477 result
= emit_intrin_1f_param(ctx
, "llvm.sqrt.f32", src
[0]);
1478 result
= ac_emit_fdiv(&ctx
->ac
, ctx
->f32one
, result
);
1481 result
= emit_intrin_2f_param(ctx
, "llvm.pow.f32", src
[0], src
[1]);
1484 result
= emit_intrin_2f_param(ctx
, "llvm.maxnum.f32", src
[0], src
[1]);
1487 result
= emit_intrin_2f_param(ctx
, "llvm.minnum.f32", src
[0], src
[1]);
1490 result
= emit_intrin_3f_param(ctx
, "llvm.fma.f32", src
[0], src
[1], src
[2]);
1492 case nir_op_ibitfield_extract
:
1493 result
= emit_bitfield_extract(ctx
, "llvm.AMDGPU.bfe.i32", src
);
1495 case nir_op_ubitfield_extract
:
1496 result
= emit_bitfield_extract(ctx
, "llvm.AMDGPU.bfe.u32", src
);
1498 case nir_op_bitfield_insert
:
1499 result
= emit_bitfield_insert(ctx
, src
[0], src
[1], src
[2], src
[3]);
1501 case nir_op_bitfield_reverse
:
1502 result
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.bitreverse.i32", ctx
->i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1504 case nir_op_bit_count
:
1505 result
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.ctpop.i32", ctx
->i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1510 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1511 src
[i
] = to_integer(ctx
, src
[i
]);
1512 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
1515 src
[0] = to_float(ctx
, src
[0]);
1516 result
= LLVMBuildFPToSI(ctx
->builder
, src
[0], ctx
->i32
, "");
1519 src
[0] = to_float(ctx
, src
[0]);
1520 result
= LLVMBuildFPToUI(ctx
->builder
, src
[0], ctx
->i32
, "");
1523 result
= LLVMBuildSIToFP(ctx
->builder
, src
[0], ctx
->f32
, "");
1526 result
= LLVMBuildUIToFP(ctx
->builder
, src
[0], ctx
->f32
, "");
1529 result
= emit_bcsel(ctx
, src
[0], src
[1], src
[2]);
1531 case nir_op_find_lsb
:
1532 result
= emit_find_lsb(ctx
, src
[0]);
1534 case nir_op_ufind_msb
:
1535 result
= emit_ufind_msb(ctx
, src
[0]);
1537 case nir_op_ifind_msb
:
1538 result
= emit_ifind_msb(ctx
, src
[0]);
1540 case nir_op_uadd_carry
:
1541 result
= emit_uint_carry(ctx
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
1543 case nir_op_usub_borrow
:
1544 result
= emit_uint_carry(ctx
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
1547 result
= emit_b2f(ctx
, src
[0]);
1549 case nir_op_fquantize2f16
:
1550 src
[0] = to_float(ctx
, src
[0]);
1551 result
= LLVMBuildFPTrunc(ctx
->builder
, src
[0], ctx
->f16
, "");
1552 /* need to convert back up to f32 */
1553 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
1555 case nir_op_umul_high
:
1556 result
= emit_umul_high(ctx
, src
[0], src
[1]);
1558 case nir_op_imul_high
:
1559 result
= emit_imul_high(ctx
, src
[0], src
[1]);
1561 case nir_op_pack_half_2x16
:
1562 result
= emit_pack_half_2x16(ctx
, src
[0]);
1564 case nir_op_unpack_half_2x16
:
1565 result
= emit_unpack_half_2x16(ctx
, src
[0]);
1569 case nir_op_fddx_fine
:
1570 case nir_op_fddy_fine
:
1571 case nir_op_fddx_coarse
:
1572 case nir_op_fddy_coarse
:
1573 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
1576 fprintf(stderr
, "Unknown NIR alu instr: ");
1577 nir_print_instr(&instr
->instr
, stderr
);
1578 fprintf(stderr
, "\n");
1583 assert(instr
->dest
.dest
.is_ssa
);
1584 result
= to_integer(ctx
, result
);
1585 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.dest
.ssa
,
1590 static void visit_load_const(struct nir_to_llvm_context
*ctx
,
1591 nir_load_const_instr
*instr
)
1593 LLVMValueRef values
[4], value
= NULL
;
1594 LLVMTypeRef element_type
=
1595 LLVMIntTypeInContext(ctx
->context
, instr
->def
.bit_size
);
1597 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1598 switch (instr
->def
.bit_size
) {
1600 values
[i
] = LLVMConstInt(element_type
,
1601 instr
->value
.u32
[i
], false);
1604 values
[i
] = LLVMConstInt(element_type
,
1605 instr
->value
.u64
[i
], false);
1609 "unsupported nir load_const bit_size: %d\n",
1610 instr
->def
.bit_size
);
1614 if (instr
->def
.num_components
> 1) {
1615 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1619 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, value
);
1622 static LLVMValueRef
cast_ptr(struct nir_to_llvm_context
*ctx
, LLVMValueRef ptr
,
1625 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
1626 return LLVMBuildBitCast(ctx
->builder
, ptr
,
1627 LLVMPointerType(type
, addr_space
), "");
1631 get_buffer_size(struct nir_to_llvm_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1634 LLVMBuildExtractElement(ctx
->builder
, descriptor
,
1635 LLVMConstInt(ctx
->i32
, 2, false), "");
1638 if (ctx
->options
->chip_class
>= VI
&& in_elements
) {
1639 /* On VI, the descriptor contains the size in bytes,
1640 * but TXQ must return the size in elements.
1641 * The stride is always non-zero for resources using TXQ.
1643 LLVMValueRef stride
=
1644 LLVMBuildExtractElement(ctx
->builder
, descriptor
,
1645 LLVMConstInt(ctx
->i32
, 1, false), "");
1646 stride
= LLVMBuildLShr(ctx
->builder
, stride
,
1647 LLVMConstInt(ctx
->i32
, 16, false), "");
1648 stride
= LLVMBuildAnd(ctx
->builder
, stride
,
1649 LLVMConstInt(ctx
->i32
, 0x3fff, false), "");
1651 size
= LLVMBuildUDiv(ctx
->builder
, size
, stride
, "");
1657 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
1660 static void build_int_type_name(
1662 char *buf
, unsigned bufsize
)
1664 assert(bufsize
>= 6);
1666 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
1667 snprintf(buf
, bufsize
, "v%ui32",
1668 LLVMGetVectorSize(type
));
1673 static LLVMValueRef
radv_lower_gather4_integer(struct nir_to_llvm_context
*ctx
,
1674 struct ac_tex_info
*tinfo
,
1675 nir_tex_instr
*instr
,
1676 const char *intr_name
,
1677 unsigned coord_vgpr_index
)
1679 LLVMValueRef coord
= tinfo
->args
[0];
1680 LLVMValueRef half_texel
[2];
1685 LLVMValueRef txq_args
[10];
1686 int txq_arg_count
= 0;
1688 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
1689 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, false);
1690 txq_args
[txq_arg_count
++] = tinfo
->args
[1];
1691 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0xf, 0); /* dmask */
1692 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* unorm */
1693 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* r128 */
1694 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, da
? 1 : 0, 0);
1695 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* glc */
1696 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* slc */
1697 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* tfe */
1698 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* lwe */
1699 size
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.SI.getresinfo.i32", ctx
->v4i32
,
1700 txq_args
, txq_arg_count
,
1701 AC_FUNC_ATTR_READNONE
);
1703 for (c
= 0; c
< 2; c
++) {
1704 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
1705 LLVMConstInt(ctx
->i32
, c
, false), "");
1706 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1707 half_texel
[c
] = ac_emit_fdiv(&ctx
->ac
, ctx
->f32one
, half_texel
[c
]);
1708 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1709 LLVMConstReal(ctx
->f32
, -0.5), "");
1713 for (c
= 0; c
< 2; c
++) {
1715 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, coord_vgpr_index
+ c
, 0);
1716 tmp
= LLVMBuildExtractElement(ctx
->builder
, coord
, index
, "");
1717 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1718 tmp
= LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
1719 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1720 coord
= LLVMBuildInsertElement(ctx
->builder
, coord
, tmp
, index
, "");
1723 tinfo
->args
[0] = coord
;
1724 return ac_emit_llvm_intrinsic(&ctx
->ac
, intr_name
, tinfo
->dst_type
, tinfo
->args
, tinfo
->arg_count
,
1725 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_NOUNWIND
);
1729 static LLVMValueRef
build_tex_intrinsic(struct nir_to_llvm_context
*ctx
,
1730 nir_tex_instr
*instr
,
1731 struct ac_tex_info
*tinfo
)
1733 const char *name
= "llvm.SI.image.sample";
1734 const char *infix
= "";
1735 char intr_name
[127];
1737 bool is_shadow
= instr
->is_shadow
;
1738 bool has_offset
= tinfo
->has_offset
;
1739 switch (instr
->op
) {
1741 case nir_texop_txf_ms
:
1742 case nir_texop_samples_identical
:
1743 name
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
? "llvm.SI.image.load" :
1744 instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
? "llvm.SI.vs.load.input" :
1745 "llvm.SI.image.load.mip";
1756 name
= "llvm.SI.getresinfo";
1758 case nir_texop_query_levels
:
1759 name
= "llvm.SI.getresinfo";
1762 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
1769 name
= "llvm.SI.gather4";
1773 name
= "llvm.SI.getlod";
1781 build_int_type_name(LLVMTypeOf(tinfo
->args
[0]), type
, sizeof(type
));
1782 sprintf(intr_name
, "%s%s%s%s.%s", name
, is_shadow
? ".c" : "", infix
,
1783 has_offset
? ".o" : "", type
);
1785 if (instr
->op
== nir_texop_tg4
) {
1786 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
1787 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
1788 return radv_lower_gather4_integer(ctx
, tinfo
, instr
, intr_name
,
1789 (int)has_offset
+ (int)is_shadow
);
1792 return ac_emit_llvm_intrinsic(&ctx
->ac
, intr_name
, tinfo
->dst_type
, tinfo
->args
, tinfo
->arg_count
,
1793 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_NOUNWIND
);
1797 static LLVMValueRef
visit_vulkan_resource_index(struct nir_to_llvm_context
*ctx
,
1798 nir_intrinsic_instr
*instr
)
1800 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
1801 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
1802 unsigned binding
= nir_intrinsic_binding(instr
);
1803 LLVMValueRef desc_ptr
= ctx
->descriptor_sets
[desc_set
];
1804 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[desc_set
].layout
;
1805 unsigned base_offset
= layout
->binding
[binding
].offset
;
1806 LLVMValueRef offset
, stride
;
1808 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
1809 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
1810 desc_ptr
= ctx
->push_constants
;
1811 base_offset
= ctx
->options
->layout
->push_constant_size
;
1812 base_offset
+= 16 * layout
->binding
[binding
].dynamic_offset_offset
;
1813 stride
= LLVMConstInt(ctx
->i32
, 16, false);
1815 stride
= LLVMConstInt(ctx
->i32
, layout
->binding
[binding
].size
, false);
1817 offset
= LLVMConstInt(ctx
->i32
, base_offset
, false);
1818 index
= LLVMBuildMul(ctx
->builder
, index
, stride
, "");
1819 offset
= LLVMBuildAdd(ctx
->builder
, offset
, index
, "");
1821 desc_ptr
= build_gep0(ctx
, desc_ptr
, offset
);
1822 desc_ptr
= cast_ptr(ctx
, desc_ptr
, ctx
->v4i32
);
1823 LLVMSetMetadata(desc_ptr
, ctx
->uniform_md_kind
, ctx
->empty_md
);
1825 return LLVMBuildLoad(ctx
->builder
, desc_ptr
, "");
1828 static LLVMValueRef
visit_load_push_constant(struct nir_to_llvm_context
*ctx
,
1829 nir_intrinsic_instr
*instr
)
1831 LLVMValueRef ptr
, addr
;
1833 addr
= LLVMConstInt(ctx
->i32
, nir_intrinsic_base(instr
), 0);
1834 addr
= LLVMBuildAdd(ctx
->builder
, addr
, get_src(ctx
, instr
->src
[0]), "");
1836 ptr
= build_gep0(ctx
, ctx
->push_constants
, addr
);
1837 ptr
= cast_ptr(ctx
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
1839 return LLVMBuildLoad(ctx
->builder
, ptr
, "");
1842 static LLVMValueRef
visit_get_buffer_size(struct nir_to_llvm_context
*ctx
,
1843 nir_intrinsic_instr
*instr
)
1845 LLVMValueRef desc
= get_src(ctx
, instr
->src
[0]);
1847 return get_buffer_size(ctx
, desc
, false);
1849 static void visit_store_ssbo(struct nir_to_llvm_context
*ctx
,
1850 nir_intrinsic_instr
*instr
)
1852 const char *store_name
;
1853 LLVMTypeRef data_type
= ctx
->f32
;
1854 unsigned writemask
= nir_intrinsic_write_mask(instr
);
1855 LLVMValueRef base_data
, base_offset
;
1856 LLVMValueRef params
[6];
1858 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
1859 ctx
->shader_info
->fs
.writes_memory
= true;
1861 params
[1] = get_src(ctx
, instr
->src
[1]);
1862 params
[2] = LLVMConstInt(ctx
->i32
, 0, false); /* vindex */
1863 params
[4] = LLVMConstInt(ctx
->i1
, 0, false); /* glc */
1864 params
[5] = LLVMConstInt(ctx
->i1
, 0, false); /* slc */
1866 if (instr
->num_components
> 1)
1867 data_type
= LLVMVectorType(ctx
->f32
, instr
->num_components
);
1869 base_data
= to_float(ctx
, get_src(ctx
, instr
->src
[0]));
1870 base_data
= trim_vector(ctx
, base_data
, instr
->num_components
);
1871 base_data
= LLVMBuildBitCast(ctx
->builder
, base_data
,
1873 base_offset
= get_src(ctx
, instr
->src
[2]); /* voffset */
1877 LLVMValueRef offset
;
1879 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
1881 /* Due to an LLVM limitation, split 3-element writes
1882 * into a 2-element and a 1-element write. */
1884 writemask
|= 1 << (start
+ 2);
1889 store_name
= "llvm.amdgcn.buffer.store.v4f32";
1891 } else if (count
== 2) {
1892 tmp
= LLVMBuildExtractElement(ctx
->builder
,
1893 base_data
, LLVMConstInt(ctx
->i32
, start
, false), "");
1894 data
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), tmp
,
1897 tmp
= LLVMBuildExtractElement(ctx
->builder
,
1898 base_data
, LLVMConstInt(ctx
->i32
, start
+ 1, false), "");
1899 data
= LLVMBuildInsertElement(ctx
->builder
, data
, tmp
,
1901 store_name
= "llvm.amdgcn.buffer.store.v2f32";
1905 if (get_llvm_num_components(base_data
) > 1)
1906 data
= LLVMBuildExtractElement(ctx
->builder
, base_data
,
1907 LLVMConstInt(ctx
->i32
, start
, false), "");
1910 store_name
= "llvm.amdgcn.buffer.store.f32";
1913 offset
= base_offset
;
1915 offset
= LLVMBuildAdd(ctx
->builder
, offset
, LLVMConstInt(ctx
->i32
, start
* 4, false), "");
1919 ac_emit_llvm_intrinsic(&ctx
->ac
, store_name
,
1920 ctx
->voidt
, params
, 6, 0);
1924 static LLVMValueRef
visit_atomic_ssbo(struct nir_to_llvm_context
*ctx
,
1925 nir_intrinsic_instr
*instr
)
1928 LLVMValueRef params
[6];
1930 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
1931 ctx
->shader_info
->fs
.writes_memory
= true;
1933 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
1934 params
[arg_count
++] = llvm_extract_elem(ctx
, get_src(ctx
, instr
->src
[3]), 0);
1936 params
[arg_count
++] = llvm_extract_elem(ctx
, get_src(ctx
, instr
->src
[2]), 0);
1937 params
[arg_count
++] = get_src(ctx
, instr
->src
[0]);
1938 params
[arg_count
++] = LLVMConstInt(ctx
->i32
, 0, false); /* vindex */
1939 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1940 params
[arg_count
++] = LLVMConstInt(ctx
->i1
, 0, false); /* slc */
1942 switch (instr
->intrinsic
) {
1943 case nir_intrinsic_ssbo_atomic_add
:
1944 name
= "llvm.amdgcn.buffer.atomic.add";
1946 case nir_intrinsic_ssbo_atomic_imin
:
1947 name
= "llvm.amdgcn.buffer.atomic.smin";
1949 case nir_intrinsic_ssbo_atomic_umin
:
1950 name
= "llvm.amdgcn.buffer.atomic.umin";
1952 case nir_intrinsic_ssbo_atomic_imax
:
1953 name
= "llvm.amdgcn.buffer.atomic.smax";
1955 case nir_intrinsic_ssbo_atomic_umax
:
1956 name
= "llvm.amdgcn.buffer.atomic.umax";
1958 case nir_intrinsic_ssbo_atomic_and
:
1959 name
= "llvm.amdgcn.buffer.atomic.and";
1961 case nir_intrinsic_ssbo_atomic_or
:
1962 name
= "llvm.amdgcn.buffer.atomic.or";
1964 case nir_intrinsic_ssbo_atomic_xor
:
1965 name
= "llvm.amdgcn.buffer.atomic.xor";
1967 case nir_intrinsic_ssbo_atomic_exchange
:
1968 name
= "llvm.amdgcn.buffer.atomic.swap";
1970 case nir_intrinsic_ssbo_atomic_comp_swap
:
1971 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
1977 return ac_emit_llvm_intrinsic(&ctx
->ac
, name
, ctx
->i32
, params
, arg_count
, 0);
1980 static LLVMValueRef
visit_load_buffer(struct nir_to_llvm_context
*ctx
,
1981 nir_intrinsic_instr
*instr
)
1983 const char *load_name
;
1984 LLVMTypeRef data_type
= ctx
->f32
;
1985 if (instr
->num_components
== 3)
1986 data_type
= LLVMVectorType(ctx
->f32
, 4);
1987 else if (instr
->num_components
> 1)
1988 data_type
= LLVMVectorType(ctx
->f32
, instr
->num_components
);
1990 if (instr
->num_components
== 4 || instr
->num_components
== 3)
1991 load_name
= "llvm.amdgcn.buffer.load.v4f32";
1992 else if (instr
->num_components
== 2)
1993 load_name
= "llvm.amdgcn.buffer.load.v2f32";
1994 else if (instr
->num_components
== 1)
1995 load_name
= "llvm.amdgcn.buffer.load.f32";
1999 LLVMValueRef params
[] = {
2000 get_src(ctx
, instr
->src
[0]),
2001 LLVMConstInt(ctx
->i32
, 0, false),
2002 get_src(ctx
, instr
->src
[1]),
2003 LLVMConstInt(ctx
->i1
, 0, false),
2004 LLVMConstInt(ctx
->i1
, 0, false),
2008 ac_emit_llvm_intrinsic(&ctx
->ac
, load_name
, data_type
, params
, 5, 0);
2010 if (instr
->num_components
== 3)
2011 ret
= trim_vector(ctx
, ret
, 3);
2013 return LLVMBuildBitCast(ctx
->builder
, ret
,
2014 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2017 static LLVMValueRef
visit_load_ubo_buffer(struct nir_to_llvm_context
*ctx
,
2018 nir_intrinsic_instr
*instr
)
2020 LLVMValueRef results
[4], ret
;
2021 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
2022 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
2024 rsrc
= LLVMBuildBitCast(ctx
->builder
, rsrc
, LLVMVectorType(ctx
->i8
, 16), "");
2026 for (unsigned i
= 0; i
< instr
->num_components
; ++i
) {
2027 LLVMValueRef params
[] = {
2029 LLVMBuildAdd(ctx
->builder
, LLVMConstInt(ctx
->i32
, 4 * i
, 0),
2032 results
[i
] = ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.SI.load.const", ctx
->f32
,
2033 params
, 2, AC_FUNC_ATTR_READNONE
);
2037 ret
= ac_build_gather_values(&ctx
->ac
, results
, instr
->num_components
);
2038 return LLVMBuildBitCast(ctx
->builder
, ret
,
2039 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2043 radv_get_deref_offset(struct nir_to_llvm_context
*ctx
, nir_deref
*tail
,
2044 bool vs_in
, unsigned *const_out
, LLVMValueRef
*indir_out
)
2046 unsigned const_offset
= 0;
2047 LLVMValueRef offset
= NULL
;
2050 while (tail
->child
!= NULL
) {
2051 const struct glsl_type
*parent_type
= tail
->type
;
2054 if (tail
->deref_type
== nir_deref_type_array
) {
2055 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2056 LLVMValueRef index
, stride
, local_offset
;
2057 unsigned size
= glsl_count_attribute_slots(tail
->type
, vs_in
);
2059 const_offset
+= size
* deref_array
->base_offset
;
2060 if (deref_array
->deref_array_type
== nir_deref_array_type_direct
)
2063 assert(deref_array
->deref_array_type
== nir_deref_array_type_indirect
);
2064 index
= get_src(ctx
, deref_array
->indirect
);
2065 stride
= LLVMConstInt(ctx
->i32
, size
, 0);
2066 local_offset
= LLVMBuildMul(ctx
->builder
, stride
, index
, "");
2069 offset
= LLVMBuildAdd(ctx
->builder
, offset
, local_offset
, "");
2071 offset
= local_offset
;
2072 } else if (tail
->deref_type
== nir_deref_type_struct
) {
2073 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
2075 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
2076 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
2077 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
2080 unreachable("unsupported deref type");
2084 if (const_offset
&& offset
)
2085 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
2086 LLVMConstInt(ctx
->i32
, const_offset
, 0),
2089 *const_out
= const_offset
;
2090 *indir_out
= offset
;
2093 static LLVMValueRef
visit_load_var(struct nir_to_llvm_context
*ctx
,
2094 nir_intrinsic_instr
*instr
)
2096 LLVMValueRef values
[4];
2097 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2098 int ve
= instr
->dest
.ssa
.num_components
;
2099 LLVMValueRef indir_index
;
2100 unsigned const_index
;
2101 switch (instr
->variables
[0]->var
->data
.mode
) {
2102 case nir_var_shader_in
:
2103 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
,
2104 ctx
->stage
== MESA_SHADER_VERTEX
,
2105 &const_index
, &indir_index
);
2106 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2108 unsigned count
= glsl_count_attribute_slots(
2109 instr
->variables
[0]->var
->type
,
2110 ctx
->stage
== MESA_SHADER_VERTEX
);
2111 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2112 &ctx
->ac
, ctx
->inputs
+ idx
+ chan
, count
,
2115 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2119 values
[chan
] = ctx
->inputs
[idx
+ chan
+ const_index
* 4];
2121 return to_integer(ctx
, ac_build_gather_values(&ctx
->ac
, values
, ve
));
2124 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2125 &const_index
, &indir_index
);
2126 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2128 unsigned count
= glsl_count_attribute_slots(
2129 instr
->variables
[0]->var
->type
, false);
2130 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2131 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2134 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2138 values
[chan
] = LLVMBuildLoad(ctx
->builder
, ctx
->locals
[idx
+ chan
+ const_index
* 4], "");
2141 return to_integer(ctx
, ac_build_gather_values(&ctx
->ac
, values
, ve
));
2142 case nir_var_shader_out
:
2143 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2144 &const_index
, &indir_index
);
2145 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2147 unsigned count
= glsl_count_attribute_slots(
2148 instr
->variables
[0]->var
->type
, false);
2149 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2150 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
2153 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2157 values
[chan
] = LLVMBuildLoad(ctx
->builder
,
2158 ctx
->outputs
[idx
+ chan
+ const_index
* 4],
2162 return to_integer(ctx
, ac_build_gather_values(&ctx
->ac
, values
, ve
));
2163 case nir_var_shared
: {
2164 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2165 &const_index
, &indir_index
);
2166 LLVMValueRef ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
2167 LLVMValueRef derived_ptr
;
2169 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2170 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, chan
, false);
2172 index
= LLVMBuildAdd(ctx
->builder
, index
, indir_index
, "");
2173 derived_ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, "");
2174 values
[chan
] = LLVMBuildLoad(ctx
->builder
, derived_ptr
, "");
2176 return to_integer(ctx
, ac_build_gather_values(&ctx
->ac
, values
, ve
));
2185 visit_store_var(struct nir_to_llvm_context
*ctx
,
2186 nir_intrinsic_instr
*instr
)
2188 LLVMValueRef temp_ptr
, value
;
2189 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2190 LLVMValueRef src
= to_float(ctx
, get_src(ctx
, instr
->src
[0]));
2191 int writemask
= instr
->const_index
[0];
2192 LLVMValueRef indir_index
;
2193 unsigned const_index
;
2194 switch (instr
->variables
[0]->var
->data
.mode
) {
2195 case nir_var_shader_out
:
2196 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2197 &const_index
, &indir_index
);
2198 for (unsigned chan
= 0; chan
< 4; chan
++) {
2200 if (!(writemask
& (1 << chan
)))
2202 if (get_llvm_num_components(src
) == 1)
2205 value
= LLVMBuildExtractElement(ctx
->builder
, src
,
2206 LLVMConstInt(ctx
->i32
,
2210 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CLIP_DIST0
||
2211 instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CULL_DIST0
)
2214 unsigned count
= glsl_count_attribute_slots(
2215 instr
->variables
[0]->var
->type
, false);
2216 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2217 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
2220 if (get_llvm_num_components(tmp_vec
) > 1) {
2221 tmp_vec
= LLVMBuildInsertElement(ctx
->builder
, tmp_vec
,
2222 value
, indir_index
, "");
2225 build_store_values_extended(ctx
, ctx
->outputs
+ idx
+ chan
,
2226 count
, stride
, tmp_vec
);
2229 temp_ptr
= ctx
->outputs
[idx
+ chan
+ const_index
* stride
];
2231 LLVMBuildStore(ctx
->builder
, value
, temp_ptr
);
2236 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2237 &const_index
, &indir_index
);
2238 for (unsigned chan
= 0; chan
< 4; chan
++) {
2239 if (!(writemask
& (1 << chan
)))
2242 if (get_llvm_num_components(src
) == 1)
2245 value
= LLVMBuildExtractElement(ctx
->builder
, src
,
2246 LLVMConstInt(ctx
->i32
, chan
, false), "");
2248 unsigned count
= glsl_count_attribute_slots(
2249 instr
->variables
[0]->var
->type
, false);
2250 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2251 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2254 tmp_vec
= LLVMBuildInsertElement(ctx
->builder
, tmp_vec
,
2255 value
, indir_index
, "");
2256 build_store_values_extended(ctx
, ctx
->locals
+ idx
+ chan
,
2259 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
2261 LLVMBuildStore(ctx
->builder
, value
, temp_ptr
);
2265 case nir_var_shared
: {
2267 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2268 &const_index
, &indir_index
);
2270 ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
2271 LLVMValueRef derived_ptr
;
2273 for (unsigned chan
= 0; chan
< 4; chan
++) {
2274 if (!(writemask
& (1 << chan
)))
2277 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, chan
, false);
2279 if (get_llvm_num_components(src
) == 1)
2282 value
= LLVMBuildExtractElement(ctx
->builder
, src
,
2283 LLVMConstInt(ctx
->i32
,
2288 index
= LLVMBuildAdd(ctx
->builder
, index
, indir_index
, "");
2290 derived_ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, "");
2291 LLVMBuildStore(ctx
->builder
,
2292 to_integer(ctx
, value
), derived_ptr
);
2301 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
2304 case GLSL_SAMPLER_DIM_BUF
:
2306 case GLSL_SAMPLER_DIM_1D
:
2307 return array
? 2 : 1;
2308 case GLSL_SAMPLER_DIM_2D
:
2309 return array
? 3 : 2;
2310 case GLSL_SAMPLER_DIM_MS
:
2311 return array
? 4 : 3;
2312 case GLSL_SAMPLER_DIM_3D
:
2313 case GLSL_SAMPLER_DIM_CUBE
:
2315 case GLSL_SAMPLER_DIM_RECT
:
2316 case GLSL_SAMPLER_DIM_SUBPASS
:
2318 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
2326 static LLVMValueRef
get_image_coords(struct nir_to_llvm_context
*ctx
,
2327 nir_intrinsic_instr
*instr
)
2329 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
2330 if(instr
->variables
[0]->deref
.child
)
2331 type
= instr
->variables
[0]->deref
.child
->type
;
2333 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
2334 LLVMValueRef coords
[4];
2335 LLVMValueRef masks
[] = {
2336 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
2337 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false),
2341 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2342 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
2343 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2344 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
2345 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2347 count
= image_type_to_components_count(dim
,
2348 glsl_sampler_type_is_array(type
));
2351 if (instr
->src
[0].ssa
->num_components
)
2352 res
= LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[0], "");
2359 for (chan
= 0; chan
< count
; ++chan
) {
2360 coords
[chan
] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[chan
], "");
2364 for (chan
= 0; chan
< count
; ++chan
)
2365 coords
[chan
] = LLVMBuildAdd(ctx
->builder
, coords
[chan
], LLVMBuildFPToUI(ctx
->builder
, ctx
->frag_pos
[chan
], ctx
->i32
, ""), "");
2368 coords
[count
] = llvm_extract_elem(ctx
, get_src(ctx
, instr
->src
[1]), 0);
2373 coords
[3] = LLVMGetUndef(ctx
->i32
);
2376 res
= ac_build_gather_values(&ctx
->ac
, coords
, count
);
2381 static void build_type_name_for_intr(
2383 char *buf
, unsigned bufsize
)
2385 LLVMTypeRef elem_type
= type
;
2387 assert(bufsize
>= 8);
2389 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
2390 int ret
= snprintf(buf
, bufsize
, "v%u",
2391 LLVMGetVectorSize(type
));
2393 char *type_name
= LLVMPrintTypeToString(type
);
2394 fprintf(stderr
, "Error building type name for: %s\n",
2398 elem_type
= LLVMGetElementType(type
);
2402 switch (LLVMGetTypeKind(elem_type
)) {
2404 case LLVMIntegerTypeKind
:
2405 snprintf(buf
, bufsize
, "i%d", LLVMGetIntTypeWidth(elem_type
));
2407 case LLVMFloatTypeKind
:
2408 snprintf(buf
, bufsize
, "f32");
2410 case LLVMDoubleTypeKind
:
2411 snprintf(buf
, bufsize
, "f64");
2416 static void get_image_intr_name(const char *base_name
,
2417 LLVMTypeRef data_type
,
2418 LLVMTypeRef coords_type
,
2419 LLVMTypeRef rsrc_type
,
2420 char *out_name
, unsigned out_len
)
2422 char coords_type_name
[8];
2424 build_type_name_for_intr(coords_type
, coords_type_name
,
2425 sizeof(coords_type_name
));
2427 if (HAVE_LLVM
<= 0x0309) {
2428 snprintf(out_name
, out_len
, "%s.%s", base_name
, coords_type_name
);
2430 char data_type_name
[8];
2431 char rsrc_type_name
[8];
2433 build_type_name_for_intr(data_type
, data_type_name
,
2434 sizeof(data_type_name
));
2435 build_type_name_for_intr(rsrc_type
, rsrc_type_name
,
2436 sizeof(rsrc_type_name
));
2437 snprintf(out_name
, out_len
, "%s.%s.%s.%s", base_name
,
2438 data_type_name
, coords_type_name
, rsrc_type_name
);
2442 static LLVMValueRef
visit_image_load(struct nir_to_llvm_context
*ctx
,
2443 nir_intrinsic_instr
*instr
)
2445 LLVMValueRef params
[7];
2447 char intrinsic_name
[64];
2448 const nir_variable
*var
= instr
->variables
[0]->var
;
2449 const struct glsl_type
*type
= var
->type
;
2450 if(instr
->variables
[0]->deref
.child
)
2451 type
= instr
->variables
[0]->deref
.child
->type
;
2453 type
= glsl_without_array(type
);
2454 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2455 params
[0] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
2456 params
[1] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
2457 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
2458 params
[2] = LLVMConstInt(ctx
->i32
, 0, false); /* voffset */
2459 params
[3] = LLVMConstInt(ctx
->i1
, 0, false); /* glc */
2460 params
[4] = LLVMConstInt(ctx
->i1
, 0, false); /* slc */
2461 res
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.load.format.v4f32", ctx
->v4f32
,
2464 res
= trim_vector(ctx
, res
, instr
->dest
.ssa
.num_components
);
2465 res
= to_integer(ctx
, res
);
2467 bool is_da
= glsl_sampler_type_is_array(type
) ||
2468 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
2469 LLVMValueRef da
= is_da
? ctx
->i32one
: ctx
->i32zero
;
2470 LLVMValueRef glc
= LLVMConstInt(ctx
->i1
, 0, false);
2471 LLVMValueRef slc
= LLVMConstInt(ctx
->i1
, 0, false);
2473 params
[0] = get_image_coords(ctx
, instr
);
2474 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
2475 params
[2] = LLVMConstInt(ctx
->i32
, 15, false); /* dmask */
2476 if (HAVE_LLVM
<= 0x0309) {
2477 params
[3] = LLVMConstInt(ctx
->i1
, 0, false); /* r128 */
2482 LLVMValueRef lwe
= LLVMConstInt(ctx
->i1
, 0, false);
2489 get_image_intr_name("llvm.amdgcn.image.load",
2490 ctx
->v4f32
, /* vdata */
2491 LLVMTypeOf(params
[0]), /* coords */
2492 LLVMTypeOf(params
[1]), /* rsrc */
2493 intrinsic_name
, sizeof(intrinsic_name
));
2495 res
= ac_emit_llvm_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->v4f32
,
2496 params
, 7, AC_FUNC_ATTR_READONLY
);
2498 return to_integer(ctx
, res
);
2501 static void visit_image_store(struct nir_to_llvm_context
*ctx
,
2502 nir_intrinsic_instr
*instr
)
2504 LLVMValueRef params
[8];
2505 char intrinsic_name
[64];
2506 const nir_variable
*var
= instr
->variables
[0]->var
;
2507 LLVMValueRef i1false
= LLVMConstInt(ctx
->i1
, 0, 0);
2508 LLVMValueRef i1true
= LLVMConstInt(ctx
->i1
, 1, 0);
2509 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2511 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
2512 ctx
->shader_info
->fs
.writes_memory
= true;
2514 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2515 params
[0] = to_float(ctx
, get_src(ctx
, instr
->src
[2])); /* data */
2516 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
2517 params
[2] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
2518 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
2519 params
[3] = LLVMConstInt(ctx
->i32
, 0, false); /* voffset */
2520 params
[4] = i1false
; /* glc */
2521 params
[5] = i1false
; /* slc */
2522 ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->voidt
,
2525 bool is_da
= glsl_sampler_type_is_array(type
) ||
2526 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
2527 LLVMValueRef da
= is_da
? i1true
: i1false
;
2528 LLVMValueRef glc
= i1false
;
2529 LLVMValueRef slc
= i1false
;
2531 params
[0] = to_float(ctx
, get_src(ctx
, instr
->src
[2]));
2532 params
[1] = get_image_coords(ctx
, instr
); /* coords */
2533 params
[2] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
2534 params
[3] = LLVMConstInt(ctx
->i32
, 15, false); /* dmask */
2535 if (HAVE_LLVM
<= 0x0309) {
2536 params
[4] = i1false
; /* r128 */
2541 LLVMValueRef lwe
= i1false
;
2548 get_image_intr_name("llvm.amdgcn.image.store",
2549 LLVMTypeOf(params
[0]), /* vdata */
2550 LLVMTypeOf(params
[1]), /* coords */
2551 LLVMTypeOf(params
[2]), /* rsrc */
2552 intrinsic_name
, sizeof(intrinsic_name
));
2554 ac_emit_llvm_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->voidt
,
2560 static LLVMValueRef
visit_image_atomic(struct nir_to_llvm_context
*ctx
,
2561 nir_intrinsic_instr
*instr
)
2563 LLVMValueRef params
[6];
2564 int param_count
= 0;
2565 const nir_variable
*var
= instr
->variables
[0]->var
;
2566 LLVMValueRef i1false
= LLVMConstInt(ctx
->i1
, 0, 0);
2567 LLVMValueRef i1true
= LLVMConstInt(ctx
->i1
, 1, 0);
2568 const char *base_name
= "llvm.amdgcn.image.atomic";
2569 const char *atomic_name
;
2570 LLVMValueRef coords
;
2571 char intrinsic_name
[32], coords_type
[8];
2572 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2574 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
2575 ctx
->shader_info
->fs
.writes_memory
= true;
2577 params
[param_count
++] = get_src(ctx
, instr
->src
[2]);
2578 if (instr
->intrinsic
== nir_intrinsic_image_atomic_comp_swap
)
2579 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
2581 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2582 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
2583 coords
= params
[param_count
++] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
2584 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
2585 params
[param_count
++] = ctx
->i32zero
; /* voffset */
2586 params
[param_count
++] = i1false
; /* glc */
2587 params
[param_count
++] = i1false
; /* slc */
2589 bool da
= glsl_sampler_type_is_array(type
) ||
2590 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
2592 coords
= params
[param_count
++] = get_image_coords(ctx
, instr
);
2593 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
2594 params
[param_count
++] = i1false
; /* r128 */
2595 params
[param_count
++] = da
? i1true
: i1false
; /* da */
2596 params
[param_count
++] = i1false
; /* slc */
2599 switch (instr
->intrinsic
) {
2600 case nir_intrinsic_image_atomic_add
:
2601 atomic_name
= "add";
2603 case nir_intrinsic_image_atomic_min
:
2604 atomic_name
= "smin";
2606 case nir_intrinsic_image_atomic_max
:
2607 atomic_name
= "smax";
2609 case nir_intrinsic_image_atomic_and
:
2610 atomic_name
= "and";
2612 case nir_intrinsic_image_atomic_or
:
2615 case nir_intrinsic_image_atomic_xor
:
2616 atomic_name
= "xor";
2618 case nir_intrinsic_image_atomic_exchange
:
2619 atomic_name
= "swap";
2621 case nir_intrinsic_image_atomic_comp_swap
:
2622 atomic_name
= "cmpswap";
2627 build_int_type_name(LLVMTypeOf(coords
),
2628 coords_type
, sizeof(coords_type
));
2630 snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2631 "%s.%s.%s", base_name
, atomic_name
, coords_type
);
2632 return ac_emit_llvm_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->i32
, params
, param_count
, 0);
2635 static LLVMValueRef
visit_image_size(struct nir_to_llvm_context
*ctx
,
2636 nir_intrinsic_instr
*instr
)
2639 LLVMValueRef params
[10];
2640 const nir_variable
*var
= instr
->variables
[0]->var
;
2641 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
2642 bool da
= glsl_sampler_type_is_array(var
->type
) ||
2643 glsl_get_sampler_dim(var
->type
) == GLSL_SAMPLER_DIM_CUBE
;
2644 if(instr
->variables
[0]->deref
.child
)
2645 type
= instr
->variables
[0]->deref
.child
->type
;
2647 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
2648 return get_buffer_size(ctx
, get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
), true);
2649 params
[0] = ctx
->i32zero
;
2650 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
2651 params
[2] = LLVMConstInt(ctx
->i32
, 15, false);
2652 params
[3] = ctx
->i32zero
;
2653 params
[4] = ctx
->i32zero
;
2654 params
[5] = da
? ctx
->i32one
: ctx
->i32zero
;
2655 params
[6] = ctx
->i32zero
;
2656 params
[7] = ctx
->i32zero
;
2657 params
[8] = ctx
->i32zero
;
2658 params
[9] = ctx
->i32zero
;
2660 res
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.SI.getresinfo.i32", ctx
->v4i32
,
2661 params
, 10, AC_FUNC_ATTR_READNONE
);
2663 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
2664 glsl_sampler_type_is_array(type
)) {
2665 LLVMValueRef two
= LLVMConstInt(ctx
->i32
, 2, false);
2666 LLVMValueRef six
= LLVMConstInt(ctx
->i32
, 6, false);
2667 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->builder
, res
, two
, "");
2668 z
= LLVMBuildSDiv(ctx
->builder
, z
, six
, "");
2669 res
= LLVMBuildInsertElement(ctx
->builder
, res
, z
, two
, "");
2674 static void emit_waitcnt(struct nir_to_llvm_context
*ctx
)
2676 LLVMValueRef args
[1] = {
2677 LLVMConstInt(ctx
->i32
, 0xf70, false),
2679 ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.amdgcn.s.waitcnt",
2680 ctx
->voidt
, args
, 1, 0);
2683 static void emit_barrier(struct nir_to_llvm_context
*ctx
)
2686 ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.amdgcn.s.barrier",
2687 ctx
->voidt
, NULL
, 0, 0);
2690 static void emit_discard_if(struct nir_to_llvm_context
*ctx
,
2691 nir_intrinsic_instr
*instr
)
2694 ctx
->shader_info
->fs
.can_discard
= true;
2696 cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
2697 get_src(ctx
, instr
->src
[0]),
2700 cond
= LLVMBuildSelect(ctx
->builder
, cond
,
2701 LLVMConstReal(ctx
->f32
, -1.0f
),
2703 ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.AMDGPU.kill",
2709 visit_load_local_invocation_index(struct nir_to_llvm_context
*ctx
)
2711 LLVMValueRef result
;
2712 LLVMValueRef thread_id
= get_thread_id(ctx
);
2713 result
= LLVMBuildAnd(ctx
->builder
, ctx
->tg_size
,
2714 LLVMConstInt(ctx
->i32
, 0xfc0, false), "");
2716 return LLVMBuildAdd(ctx
->builder
, result
, thread_id
, "");
2719 static LLVMValueRef
visit_var_atomic(struct nir_to_llvm_context
*ctx
,
2720 nir_intrinsic_instr
*instr
)
2722 LLVMValueRef ptr
, result
;
2723 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2724 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
2725 ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
2727 if (instr
->intrinsic
== nir_intrinsic_var_atomic_comp_swap
) {
2728 LLVMValueRef src1
= get_src(ctx
, instr
->src
[1]);
2729 result
= LLVMBuildAtomicCmpXchg(ctx
->builder
,
2731 LLVMAtomicOrderingSequentiallyConsistent
,
2732 LLVMAtomicOrderingSequentiallyConsistent
,
2735 LLVMAtomicRMWBinOp op
;
2736 switch (instr
->intrinsic
) {
2737 case nir_intrinsic_var_atomic_add
:
2738 op
= LLVMAtomicRMWBinOpAdd
;
2740 case nir_intrinsic_var_atomic_umin
:
2741 op
= LLVMAtomicRMWBinOpUMin
;
2743 case nir_intrinsic_var_atomic_umax
:
2744 op
= LLVMAtomicRMWBinOpUMax
;
2746 case nir_intrinsic_var_atomic_imin
:
2747 op
= LLVMAtomicRMWBinOpMin
;
2749 case nir_intrinsic_var_atomic_imax
:
2750 op
= LLVMAtomicRMWBinOpMax
;
2752 case nir_intrinsic_var_atomic_and
:
2753 op
= LLVMAtomicRMWBinOpAnd
;
2755 case nir_intrinsic_var_atomic_or
:
2756 op
= LLVMAtomicRMWBinOpOr
;
2758 case nir_intrinsic_var_atomic_xor
:
2759 op
= LLVMAtomicRMWBinOpXor
;
2761 case nir_intrinsic_var_atomic_exchange
:
2762 op
= LLVMAtomicRMWBinOpXchg
;
2768 result
= LLVMBuildAtomicRMW(ctx
->builder
, op
, ptr
, to_integer(ctx
, src
),
2769 LLVMAtomicOrderingSequentiallyConsistent
,
2775 #define INTERP_CENTER 0
2776 #define INTERP_CENTROID 1
2777 #define INTERP_SAMPLE 2
2779 static LLVMValueRef
lookup_interp_param(struct nir_to_llvm_context
*ctx
,
2780 enum glsl_interp_mode interp
, unsigned location
)
2783 case INTERP_MODE_FLAT
:
2786 case INTERP_MODE_SMOOTH
:
2787 case INTERP_MODE_NONE
:
2788 if (location
== INTERP_CENTER
)
2789 return ctx
->persp_center
;
2790 else if (location
== INTERP_CENTROID
)
2791 return ctx
->persp_centroid
;
2792 else if (location
== INTERP_SAMPLE
)
2793 return ctx
->persp_sample
;
2795 case INTERP_MODE_NOPERSPECTIVE
:
2796 if (location
== INTERP_CENTER
)
2797 return ctx
->linear_center
;
2798 else if (location
== INTERP_CENTROID
)
2799 return ctx
->linear_centroid
;
2800 else if (location
== INTERP_SAMPLE
)
2801 return ctx
->linear_sample
;
2807 static LLVMValueRef
load_sample_position(struct nir_to_llvm_context
*ctx
,
2808 LLVMValueRef sample_id
)
2810 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
2811 LLVMValueRef offset0
= LLVMBuildMul(ctx
->builder
, sample_id
, LLVMConstInt(ctx
->i32
, 8, false), "");
2812 LLVMValueRef offset1
= LLVMBuildAdd(ctx
->builder
, offset0
, LLVMConstInt(ctx
->i32
, 4, false), "");
2813 LLVMValueRef result
[2];
2815 result
[0] = build_indexed_load_const(ctx
, ctx
->sample_positions
, offset0
);
2816 result
[1] = build_indexed_load_const(ctx
, ctx
->sample_positions
, offset1
);
2818 return ac_build_gather_values(&ctx
->ac
, result
, 2);
2821 static LLVMValueRef
load_sample_pos(struct nir_to_llvm_context
*ctx
)
2823 LLVMValueRef values
[2];
2825 values
[0] = emit_ffract(ctx
, ctx
->frag_pos
[0]);
2826 values
[1] = emit_ffract(ctx
, ctx
->frag_pos
[1]);
2827 return ac_build_gather_values(&ctx
->ac
, values
, 2);
2830 static LLVMValueRef
visit_interp(struct nir_to_llvm_context
*ctx
,
2831 nir_intrinsic_instr
*instr
)
2833 LLVMValueRef result
[2];
2834 LLVMValueRef interp_param
, attr_number
;
2837 LLVMValueRef src_c0
, src_c1
;
2838 const char *intr_name
;
2840 int input_index
= instr
->variables
[0]->var
->data
.location
- VARYING_SLOT_VAR0
;
2841 switch (instr
->intrinsic
) {
2842 case nir_intrinsic_interp_var_at_centroid
:
2843 location
= INTERP_CENTROID
;
2845 case nir_intrinsic_interp_var_at_sample
:
2846 case nir_intrinsic_interp_var_at_offset
:
2847 location
= INTERP_SAMPLE
;
2848 src0
= get_src(ctx
, instr
->src
[0]);
2854 if (instr
->intrinsic
== nir_intrinsic_interp_var_at_offset
) {
2855 src_c0
= to_float(ctx
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32zero
, ""));
2856 src_c1
= to_float(ctx
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32one
, ""));
2857 } else if (instr
->intrinsic
== nir_intrinsic_interp_var_at_sample
) {
2858 LLVMValueRef sample_position
;
2859 LLVMValueRef halfval
= LLVMConstReal(ctx
->f32
, 0.5f
);
2861 /* fetch sample ID */
2862 sample_position
= load_sample_position(ctx
, src0
);
2864 src_c0
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->i32zero
, "");
2865 src_c0
= LLVMBuildFSub(ctx
->builder
, src_c0
, halfval
, "");
2866 src_c1
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->i32one
, "");
2867 src_c1
= LLVMBuildFSub(ctx
->builder
, src_c1
, halfval
, "");
2869 interp_param
= lookup_interp_param(ctx
, instr
->variables
[0]->var
->data
.interpolation
, location
);
2870 attr_number
= LLVMConstInt(ctx
->i32
, input_index
, false);
2872 if (location
== INTERP_SAMPLE
) {
2873 LLVMValueRef ij_out
[2];
2874 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
, interp_param
);
2877 * take the I then J parameters, and the DDX/Y for it, and
2878 * calculate the IJ inputs for the interpolator.
2879 * temp1 = ddx * offset/sample.x + I;
2880 * interp_param.I = ddy * offset/sample.y + temp1;
2881 * temp1 = ddx * offset/sample.x + J;
2882 * interp_param.J = ddy * offset/sample.y + temp1;
2884 for (unsigned i
= 0; i
< 2; i
++) {
2885 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->i32
, i
, false);
2886 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->i32
, i
+ 2, false);
2887 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->builder
,
2888 ddxy_out
, ix_ll
, "");
2889 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->builder
,
2890 ddxy_out
, iy_ll
, "");
2891 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->builder
,
2892 interp_param
, ix_ll
, "");
2893 LLVMValueRef temp1
, temp2
;
2895 interp_el
= LLVMBuildBitCast(ctx
->builder
, interp_el
,
2898 temp1
= LLVMBuildFMul(ctx
->builder
, ddx_el
, src_c0
, "");
2899 temp1
= LLVMBuildFAdd(ctx
->builder
, temp1
, interp_el
, "");
2901 temp2
= LLVMBuildFMul(ctx
->builder
, ddy_el
, src_c1
, "");
2902 temp2
= LLVMBuildFAdd(ctx
->builder
, temp2
, temp1
, "");
2904 ij_out
[i
] = LLVMBuildBitCast(ctx
->builder
,
2905 temp2
, ctx
->i32
, "");
2907 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
2910 intr_name
= interp_param
? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
2911 for (chan
= 0; chan
< 2; chan
++) {
2912 LLVMValueRef args
[4];
2913 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
2915 args
[0] = llvm_chan
;
2916 args
[1] = attr_number
;
2917 args
[2] = ctx
->prim_mask
;
2918 args
[3] = interp_param
;
2919 result
[chan
] = ac_emit_llvm_intrinsic(&ctx
->ac
, intr_name
,
2920 ctx
->f32
, args
, args
[3] ? 4 : 3,
2921 AC_FUNC_ATTR_READNONE
);
2923 return ac_build_gather_values(&ctx
->ac
, result
, 2);
2926 static void visit_intrinsic(struct nir_to_llvm_context
*ctx
,
2927 nir_intrinsic_instr
*instr
)
2929 LLVMValueRef result
= NULL
;
2931 switch (instr
->intrinsic
) {
2932 case nir_intrinsic_load_work_group_id
: {
2933 result
= ctx
->workgroup_ids
;
2936 case nir_intrinsic_load_base_vertex
: {
2937 result
= ctx
->base_vertex
;
2940 case nir_intrinsic_load_vertex_id_zero_base
: {
2941 result
= ctx
->vertex_id
;
2944 case nir_intrinsic_load_local_invocation_id
: {
2945 result
= ctx
->local_invocation_ids
;
2948 case nir_intrinsic_load_base_instance
:
2949 result
= ctx
->start_instance
;
2951 case nir_intrinsic_load_sample_id
:
2952 ctx
->shader_info
->fs
.force_persample
= true;
2953 result
= unpack_param(ctx
, ctx
->ancillary
, 8, 4);
2955 case nir_intrinsic_load_sample_pos
:
2956 ctx
->shader_info
->fs
.force_persample
= true;
2957 result
= load_sample_pos(ctx
);
2959 case nir_intrinsic_load_front_face
:
2960 result
= ctx
->front_face
;
2962 case nir_intrinsic_load_instance_id
:
2963 result
= ctx
->instance_id
;
2964 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
2965 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
2967 case nir_intrinsic_load_num_work_groups
:
2968 result
= ctx
->num_work_groups
;
2970 case nir_intrinsic_load_local_invocation_index
:
2971 result
= visit_load_local_invocation_index(ctx
);
2973 case nir_intrinsic_load_push_constant
:
2974 result
= visit_load_push_constant(ctx
, instr
);
2976 case nir_intrinsic_vulkan_resource_index
:
2977 result
= visit_vulkan_resource_index(ctx
, instr
);
2979 case nir_intrinsic_store_ssbo
:
2980 visit_store_ssbo(ctx
, instr
);
2982 case nir_intrinsic_load_ssbo
:
2983 result
= visit_load_buffer(ctx
, instr
);
2985 case nir_intrinsic_ssbo_atomic_add
:
2986 case nir_intrinsic_ssbo_atomic_imin
:
2987 case nir_intrinsic_ssbo_atomic_umin
:
2988 case nir_intrinsic_ssbo_atomic_imax
:
2989 case nir_intrinsic_ssbo_atomic_umax
:
2990 case nir_intrinsic_ssbo_atomic_and
:
2991 case nir_intrinsic_ssbo_atomic_or
:
2992 case nir_intrinsic_ssbo_atomic_xor
:
2993 case nir_intrinsic_ssbo_atomic_exchange
:
2994 case nir_intrinsic_ssbo_atomic_comp_swap
:
2995 result
= visit_atomic_ssbo(ctx
, instr
);
2997 case nir_intrinsic_load_ubo
:
2998 result
= visit_load_ubo_buffer(ctx
, instr
);
3000 case nir_intrinsic_get_buffer_size
:
3001 result
= visit_get_buffer_size(ctx
, instr
);
3003 case nir_intrinsic_load_var
:
3004 result
= visit_load_var(ctx
, instr
);
3006 case nir_intrinsic_store_var
:
3007 visit_store_var(ctx
, instr
);
3009 case nir_intrinsic_image_load
:
3010 result
= visit_image_load(ctx
, instr
);
3012 case nir_intrinsic_image_store
:
3013 visit_image_store(ctx
, instr
);
3015 case nir_intrinsic_image_atomic_add
:
3016 case nir_intrinsic_image_atomic_min
:
3017 case nir_intrinsic_image_atomic_max
:
3018 case nir_intrinsic_image_atomic_and
:
3019 case nir_intrinsic_image_atomic_or
:
3020 case nir_intrinsic_image_atomic_xor
:
3021 case nir_intrinsic_image_atomic_exchange
:
3022 case nir_intrinsic_image_atomic_comp_swap
:
3023 result
= visit_image_atomic(ctx
, instr
);
3025 case nir_intrinsic_image_size
:
3026 result
= visit_image_size(ctx
, instr
);
3028 case nir_intrinsic_discard
:
3029 ctx
->shader_info
->fs
.can_discard
= true;
3030 ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.AMDGPU.kilp",
3034 case nir_intrinsic_discard_if
:
3035 emit_discard_if(ctx
, instr
);
3037 case nir_intrinsic_memory_barrier
:
3040 case nir_intrinsic_barrier
:
3043 case nir_intrinsic_var_atomic_add
:
3044 case nir_intrinsic_var_atomic_imin
:
3045 case nir_intrinsic_var_atomic_umin
:
3046 case nir_intrinsic_var_atomic_imax
:
3047 case nir_intrinsic_var_atomic_umax
:
3048 case nir_intrinsic_var_atomic_and
:
3049 case nir_intrinsic_var_atomic_or
:
3050 case nir_intrinsic_var_atomic_xor
:
3051 case nir_intrinsic_var_atomic_exchange
:
3052 case nir_intrinsic_var_atomic_comp_swap
:
3053 result
= visit_var_atomic(ctx
, instr
);
3055 case nir_intrinsic_interp_var_at_centroid
:
3056 case nir_intrinsic_interp_var_at_sample
:
3057 case nir_intrinsic_interp_var_at_offset
:
3058 result
= visit_interp(ctx
, instr
);
3061 fprintf(stderr
, "Unknown intrinsic: ");
3062 nir_print_instr(&instr
->instr
, stderr
);
3063 fprintf(stderr
, "\n");
3067 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
3071 static LLVMValueRef
get_sampler_desc(struct nir_to_llvm_context
*ctx
,
3072 nir_deref_var
*deref
,
3073 enum desc_type desc_type
)
3075 unsigned desc_set
= deref
->var
->data
.descriptor_set
;
3076 LLVMValueRef list
= ctx
->descriptor_sets
[desc_set
];
3077 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[desc_set
].layout
;
3078 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ deref
->var
->data
.binding
;
3079 unsigned offset
= binding
->offset
;
3080 unsigned stride
= binding
->size
;
3082 LLVMBuilderRef builder
= ctx
->builder
;
3084 LLVMValueRef index
= NULL
;
3086 assert(deref
->var
->data
.binding
< layout
->binding_count
);
3088 switch (desc_type
) {
3100 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
3110 unreachable("invalid desc_type\n");
3113 if (deref
->deref
.child
) {
3114 nir_deref_array
*child
= (nir_deref_array
*)deref
->deref
.child
;
3116 assert(child
->deref_array_type
!= nir_deref_array_type_wildcard
);
3117 offset
+= child
->base_offset
* stride
;
3118 if (child
->deref_array_type
== nir_deref_array_type_indirect
) {
3119 index
= get_src(ctx
, child
->indirect
);
3123 assert(stride
% type_size
== 0);
3126 index
= ctx
->i32zero
;
3128 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, stride
/ type_size
, 0), "");
3130 list
= build_gep0(ctx
, list
, LLVMConstInt(ctx
->i32
, offset
, 0));
3131 list
= LLVMBuildPointerCast(builder
, list
, const_array(type
, 0), "");
3133 return build_indexed_load_const(ctx
, list
, index
);
3136 static void set_tex_fetch_args(struct nir_to_llvm_context
*ctx
,
3137 struct ac_tex_info
*tinfo
,
3138 nir_tex_instr
*instr
,
3140 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
3141 LLVMValueRef
*param
, unsigned count
,
3145 unsigned is_rect
= 0;
3146 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
3148 if (op
== nir_texop_lod
)
3150 /* Pad to power of two vector */
3151 while (count
< util_next_power_of_two(count
))
3152 param
[count
++] = LLVMGetUndef(ctx
->i32
);
3155 tinfo
->args
[0] = ac_build_gather_values(&ctx
->ac
, param
, count
);
3157 tinfo
->args
[0] = param
[0];
3159 tinfo
->args
[1] = res_ptr
;
3162 if (op
== nir_texop_txf
||
3163 op
== nir_texop_txf_ms
||
3164 op
== nir_texop_query_levels
||
3165 op
== nir_texop_texture_samples
||
3166 op
== nir_texop_txs
)
3167 tinfo
->dst_type
= ctx
->v4i32
;
3169 tinfo
->dst_type
= ctx
->v4f32
;
3170 tinfo
->args
[num_args
++] = samp_ptr
;
3173 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
&& op
== nir_texop_txf
) {
3174 tinfo
->args
[0] = res_ptr
;
3175 tinfo
->args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
3176 tinfo
->args
[2] = param
[0];
3177 tinfo
->arg_count
= 3;
3181 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, dmask
, 0);
3182 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, is_rect
, 0); /* unorm */
3183 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* r128 */
3184 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, da
? 1 : 0, 0);
3185 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* glc */
3186 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* slc */
3187 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* tfe */
3188 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* lwe */
3190 tinfo
->arg_count
= num_args
;
3193 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
3196 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
3197 * filtering manually. The driver sets img7 to a mask clearing
3198 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
3199 * s_and_b32 samp0, samp0, img7
3202 * The ANISO_OVERRIDE sampler field enables this fix in TA.
3204 static LLVMValueRef
sici_fix_sampler_aniso(struct nir_to_llvm_context
*ctx
,
3205 LLVMValueRef res
, LLVMValueRef samp
)
3207 LLVMBuilderRef builder
= ctx
->builder
;
3208 LLVMValueRef img7
, samp0
;
3210 if (ctx
->options
->chip_class
>= VI
)
3213 img7
= LLVMBuildExtractElement(builder
, res
,
3214 LLVMConstInt(ctx
->i32
, 7, 0), "");
3215 samp0
= LLVMBuildExtractElement(builder
, samp
,
3216 LLVMConstInt(ctx
->i32
, 0, 0), "");
3217 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
3218 return LLVMBuildInsertElement(builder
, samp
, samp0
,
3219 LLVMConstInt(ctx
->i32
, 0, 0), "");
3222 static void tex_fetch_ptrs(struct nir_to_llvm_context
*ctx
,
3223 nir_tex_instr
*instr
,
3224 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
3225 LLVMValueRef
*fmask_ptr
)
3227 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
3228 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_BUFFER
);
3230 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_IMAGE
);
3233 *samp_ptr
= get_sampler_desc(ctx
, instr
->sampler
, DESC_SAMPLER
);
3235 *samp_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_SAMPLER
);
3236 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
3237 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
3239 if (fmask_ptr
&& !instr
->sampler
&& (instr
->op
== nir_texop_txf_ms
||
3240 instr
->op
== nir_texop_samples_identical
))
3241 *fmask_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_FMASK
);
3244 static void visit_tex(struct nir_to_llvm_context
*ctx
, nir_tex_instr
*instr
)
3246 LLVMValueRef result
= NULL
;
3247 struct ac_tex_info tinfo
= { 0 };
3248 unsigned dmask
= 0xf;
3249 LLVMValueRef address
[16];
3250 LLVMValueRef coords
[5];
3251 LLVMValueRef coord
= NULL
, lod
= NULL
, comparator
= NULL
;
3252 LLVMValueRef bias
= NULL
, offsets
= NULL
;
3253 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
, sample_index
= NULL
;
3254 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
3255 LLVMValueRef derivs
[6];
3256 unsigned chan
, count
= 0;
3257 unsigned const_src
= 0, num_deriv_comp
= 0;
3259 tex_fetch_ptrs(ctx
, instr
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
3261 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3262 switch (instr
->src
[i
].src_type
) {
3263 case nir_tex_src_coord
:
3264 coord
= get_src(ctx
, instr
->src
[i
].src
);
3266 case nir_tex_src_projector
:
3268 case nir_tex_src_comparator
:
3269 comparator
= get_src(ctx
, instr
->src
[i
].src
);
3271 case nir_tex_src_offset
:
3272 offsets
= get_src(ctx
, instr
->src
[i
].src
);
3275 case nir_tex_src_bias
:
3276 bias
= get_src(ctx
, instr
->src
[i
].src
);
3278 case nir_tex_src_lod
:
3279 lod
= get_src(ctx
, instr
->src
[i
].src
);
3281 case nir_tex_src_ms_index
:
3282 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
3284 case nir_tex_src_ms_mcs
:
3286 case nir_tex_src_ddx
:
3287 ddx
= get_src(ctx
, instr
->src
[i
].src
);
3288 num_deriv_comp
= instr
->src
[i
].src
.ssa
->num_components
;
3290 case nir_tex_src_ddy
:
3291 ddy
= get_src(ctx
, instr
->src
[i
].src
);
3293 case nir_tex_src_texture_offset
:
3294 case nir_tex_src_sampler_offset
:
3295 case nir_tex_src_plane
:
3301 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
3302 result
= get_buffer_size(ctx
, res_ptr
, false);
3306 if (instr
->op
== nir_texop_texture_samples
) {
3307 LLVMValueRef res
, samples
, is_msaa
;
3308 res
= LLVMBuildBitCast(ctx
->builder
, res_ptr
, ctx
->v8i32
, "");
3309 samples
= LLVMBuildExtractElement(ctx
->builder
, res
,
3310 LLVMConstInt(ctx
->i32
, 3, false), "");
3311 is_msaa
= LLVMBuildLShr(ctx
->builder
, samples
,
3312 LLVMConstInt(ctx
->i32
, 28, false), "");
3313 is_msaa
= LLVMBuildAnd(ctx
->builder
, is_msaa
,
3314 LLVMConstInt(ctx
->i32
, 0xe, false), "");
3315 is_msaa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, is_msaa
,
3316 LLVMConstInt(ctx
->i32
, 0xe, false), "");
3318 samples
= LLVMBuildLShr(ctx
->builder
, samples
,
3319 LLVMConstInt(ctx
->i32
, 16, false), "");
3320 samples
= LLVMBuildAnd(ctx
->builder
, samples
,
3321 LLVMConstInt(ctx
->i32
, 0xf, false), "");
3322 samples
= LLVMBuildShl(ctx
->builder
, ctx
->i32one
,
3324 samples
= LLVMBuildSelect(ctx
->builder
, is_msaa
, samples
,
3331 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
3332 coords
[chan
] = llvm_extract_elem(ctx
, coord
, chan
);
3334 if (offsets
&& instr
->op
!= nir_texop_txf
) {
3335 LLVMValueRef offset
[3], pack
;
3336 for (chan
= 0; chan
< 3; ++chan
)
3337 offset
[chan
] = ctx
->i32zero
;
3339 tinfo
.has_offset
= true;
3340 for (chan
= 0; chan
< get_llvm_num_components(offsets
); chan
++) {
3341 offset
[chan
] = llvm_extract_elem(ctx
, offsets
, chan
);
3342 offset
[chan
] = LLVMBuildAnd(ctx
->builder
, offset
[chan
],
3343 LLVMConstInt(ctx
->i32
, 0x3f, false), "");
3345 offset
[chan
] = LLVMBuildShl(ctx
->builder
, offset
[chan
],
3346 LLVMConstInt(ctx
->i32
, chan
* 8, false), "");
3348 pack
= LLVMBuildOr(ctx
->builder
, offset
[0], offset
[1], "");
3349 pack
= LLVMBuildOr(ctx
->builder
, pack
, offset
[2], "");
3350 address
[count
++] = pack
;
3353 /* pack LOD bias value */
3354 if (instr
->op
== nir_texop_txb
&& bias
) {
3355 address
[count
++] = bias
;
3358 /* Pack depth comparison value */
3359 if (instr
->is_shadow
&& comparator
) {
3360 address
[count
++] = llvm_extract_elem(ctx
, comparator
, 0);
3363 /* pack derivatives */
3365 switch (instr
->sampler_dim
) {
3366 case GLSL_SAMPLER_DIM_3D
:
3367 case GLSL_SAMPLER_DIM_CUBE
:
3370 case GLSL_SAMPLER_DIM_2D
:
3374 case GLSL_SAMPLER_DIM_1D
:
3379 for (unsigned i
= 0; i
< num_deriv_comp
; i
++) {
3380 derivs
[i
* 2] = to_float(ctx
, llvm_extract_elem(ctx
, ddx
, i
));
3381 derivs
[i
* 2 + 1] = to_float(ctx
, llvm_extract_elem(ctx
, ddy
, i
));
3385 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& coord
) {
3386 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
3387 coords
[chan
] = to_float(ctx
, coords
[chan
]);
3388 if (instr
->coord_components
== 3)
3389 coords
[3] = LLVMGetUndef(ctx
->f32
);
3390 ac_prepare_cube_coords(&ctx
->ac
,
3391 instr
->op
== nir_texop_txd
, instr
->is_array
,
3398 for (unsigned i
= 0; i
< num_deriv_comp
* 2; i
++)
3399 address
[count
++] = derivs
[i
];
3402 /* Pack texture coordinates */
3404 address
[count
++] = coords
[0];
3405 if (instr
->coord_components
> 1)
3406 address
[count
++] = coords
[1];
3407 if (instr
->coord_components
> 2) {
3408 /* This seems like a bit of a hack - but it passes Vulkan CTS with it */
3409 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_3D
&& instr
->op
!= nir_texop_txf
) {
3410 coords
[2] = to_float(ctx
, coords
[2]);
3411 coords
[2] = ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.rint.f32", ctx
->f32
, &coords
[2],
3413 coords
[2] = to_integer(ctx
, coords
[2]);
3415 address
[count
++] = coords
[2];
3420 if ((instr
->op
== nir_texop_txl
|| instr
->op
== nir_texop_txf
) && lod
) {
3421 address
[count
++] = lod
;
3422 } else if (instr
->op
== nir_texop_txf_ms
&& sample_index
) {
3423 address
[count
++] = sample_index
;
3424 } else if(instr
->op
== nir_texop_txs
) {
3427 address
[count
++] = lod
;
3429 address
[count
++] = ctx
->i32zero
;
3432 for (chan
= 0; chan
< count
; chan
++) {
3433 address
[chan
] = LLVMBuildBitCast(ctx
->builder
,
3434 address
[chan
], ctx
->i32
, "");
3437 if (instr
->op
== nir_texop_samples_identical
) {
3438 LLVMValueRef txf_address
[4];
3439 struct ac_tex_info txf_info
= { 0 };
3440 unsigned txf_count
= count
;
3441 memcpy(txf_address
, address
, sizeof(txf_address
));
3443 if (!instr
->is_array
)
3444 txf_address
[2] = ctx
->i32zero
;
3445 txf_address
[3] = ctx
->i32zero
;
3447 set_tex_fetch_args(ctx
, &txf_info
, instr
, nir_texop_txf
,
3449 txf_address
, txf_count
, 0xf);
3451 result
= build_tex_intrinsic(ctx
, instr
, &txf_info
);
3453 result
= LLVMBuildExtractElement(ctx
->builder
, result
, ctx
->i32zero
, "");
3454 result
= emit_int_cmp(ctx
, LLVMIntEQ
, result
, ctx
->i32zero
);
3458 /* Adjust the sample index according to FMASK.
3460 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3461 * which is the identity mapping. Each nibble says which physical sample
3462 * should be fetched to get that sample.
3464 * For example, 0x11111100 means there are only 2 samples stored and
3465 * the second sample covers 3/4 of the pixel. When reading samples 0
3466 * and 1, return physical sample 0 (determined by the first two 0s
3467 * in FMASK), otherwise return physical sample 1.
3469 * The sample index should be adjusted as follows:
3470 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
3472 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
) {
3473 LLVMValueRef txf_address
[4];
3474 struct ac_tex_info txf_info
= { 0 };
3475 unsigned txf_count
= count
;
3476 memcpy(txf_address
, address
, sizeof(txf_address
));
3478 if (!instr
->is_array
)
3479 txf_address
[2] = ctx
->i32zero
;
3480 txf_address
[3] = ctx
->i32zero
;
3482 set_tex_fetch_args(ctx
, &txf_info
, instr
, nir_texop_txf
,
3484 txf_address
, txf_count
, 0xf);
3486 result
= build_tex_intrinsic(ctx
, instr
, &txf_info
);
3487 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
3488 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
3490 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
3494 unsigned sample_chan
= instr
->is_array
? 3 : 2;
3496 LLVMValueRef sample_index4
=
3497 LLVMBuildMul(ctx
->builder
, address
[sample_chan
], four
, "");
3498 LLVMValueRef shifted_fmask
=
3499 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
3500 LLVMValueRef final_sample
=
3501 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
3503 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3504 * resource descriptor is 0 (invalid),
3506 LLVMValueRef fmask_desc
=
3507 LLVMBuildBitCast(ctx
->builder
, fmask_ptr
,
3510 LLVMValueRef fmask_word1
=
3511 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
3514 LLVMValueRef word1_is_nonzero
=
3515 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3516 fmask_word1
, ctx
->i32zero
, "");
3518 /* Replace the MSAA sample index. */
3519 address
[sample_chan
] =
3520 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
3521 final_sample
, address
[sample_chan
], "");
3524 if (offsets
&& instr
->op
== nir_texop_txf
) {
3525 nir_const_value
*const_offset
=
3526 nir_src_as_const_value(instr
->src
[const_src
].src
);
3527 int num_offsets
= instr
->src
[const_src
].src
.ssa
->num_components
;
3528 assert(const_offset
);
3529 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
3530 if (num_offsets
> 2)
3531 address
[2] = LLVMBuildAdd(ctx
->builder
,
3532 address
[2], LLVMConstInt(ctx
->i32
, const_offset
->i32
[2], false), "");
3533 if (num_offsets
> 1)
3534 address
[1] = LLVMBuildAdd(ctx
->builder
,
3535 address
[1], LLVMConstInt(ctx
->i32
, const_offset
->i32
[1], false), "");
3536 address
[0] = LLVMBuildAdd(ctx
->builder
,
3537 address
[0], LLVMConstInt(ctx
->i32
, const_offset
->i32
[0], false), "");
3541 /* TODO TG4 support */
3542 if (instr
->op
== nir_texop_tg4
) {
3543 if (instr
->is_shadow
)
3546 dmask
= 1 << instr
->component
;
3548 set_tex_fetch_args(ctx
, &tinfo
, instr
, instr
->op
,
3549 res_ptr
, samp_ptr
, address
, count
, dmask
);
3551 result
= build_tex_intrinsic(ctx
, instr
, &tinfo
);
3553 if (instr
->op
== nir_texop_query_levels
)
3554 result
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, 3, false), "");
3555 else if (instr
->is_shadow
&& instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&& instr
->op
!= nir_texop_tg4
)
3556 result
= LLVMBuildExtractElement(ctx
->builder
, result
, ctx
->i32zero
, "");
3557 else if (instr
->op
== nir_texop_txs
&&
3558 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
3560 LLVMValueRef two
= LLVMConstInt(ctx
->i32
, 2, false);
3561 LLVMValueRef six
= LLVMConstInt(ctx
->i32
, 6, false);
3562 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->builder
, result
, two
, "");
3563 z
= LLVMBuildSDiv(ctx
->builder
, z
, six
, "");
3564 result
= LLVMBuildInsertElement(ctx
->builder
, result
, z
, two
, "");
3565 } else if (instr
->dest
.ssa
.num_components
!= 4)
3566 result
= trim_vector(ctx
, result
, instr
->dest
.ssa
.num_components
);
3570 assert(instr
->dest
.is_ssa
);
3571 result
= to_integer(ctx
, result
);
3572 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
3577 static void visit_phi(struct nir_to_llvm_context
*ctx
, nir_phi_instr
*instr
)
3579 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
3580 LLVMValueRef result
= LLVMBuildPhi(ctx
->builder
, type
, "");
3582 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
3583 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
3586 static void visit_post_phi(struct nir_to_llvm_context
*ctx
,
3587 nir_phi_instr
*instr
,
3588 LLVMValueRef llvm_phi
)
3590 nir_foreach_phi_src(src
, instr
) {
3591 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
3592 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
3594 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
3598 static void phi_post_pass(struct nir_to_llvm_context
*ctx
)
3600 struct hash_entry
*entry
;
3601 hash_table_foreach(ctx
->phis
, entry
) {
3602 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
3603 (LLVMValueRef
)entry
->data
);
3608 static void visit_ssa_undef(struct nir_to_llvm_context
*ctx
,
3609 nir_ssa_undef_instr
*instr
)
3611 unsigned num_components
= instr
->def
.num_components
;
3614 if (num_components
== 1)
3615 undef
= LLVMGetUndef(ctx
->i32
);
3617 undef
= LLVMGetUndef(LLVMVectorType(ctx
->i32
, num_components
));
3619 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, undef
);
3622 static void visit_jump(struct nir_to_llvm_context
*ctx
,
3623 nir_jump_instr
*instr
)
3625 switch (instr
->type
) {
3626 case nir_jump_break
:
3627 LLVMBuildBr(ctx
->builder
, ctx
->break_block
);
3628 LLVMClearInsertionPosition(ctx
->builder
);
3630 case nir_jump_continue
:
3631 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
3632 LLVMClearInsertionPosition(ctx
->builder
);
3635 fprintf(stderr
, "Unknown NIR jump instr: ");
3636 nir_print_instr(&instr
->instr
, stderr
);
3637 fprintf(stderr
, "\n");
3642 static void visit_cf_list(struct nir_to_llvm_context
*ctx
,
3643 struct exec_list
*list
);
3645 static void visit_block(struct nir_to_llvm_context
*ctx
, nir_block
*block
)
3647 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->builder
);
3648 nir_foreach_instr(instr
, block
)
3650 switch (instr
->type
) {
3651 case nir_instr_type_alu
:
3652 visit_alu(ctx
, nir_instr_as_alu(instr
));
3654 case nir_instr_type_load_const
:
3655 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
3657 case nir_instr_type_intrinsic
:
3658 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
3660 case nir_instr_type_tex
:
3661 visit_tex(ctx
, nir_instr_as_tex(instr
));
3663 case nir_instr_type_phi
:
3664 visit_phi(ctx
, nir_instr_as_phi(instr
));
3666 case nir_instr_type_ssa_undef
:
3667 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
3669 case nir_instr_type_jump
:
3670 visit_jump(ctx
, nir_instr_as_jump(instr
));
3673 fprintf(stderr
, "Unknown NIR instr type: ");
3674 nir_print_instr(instr
, stderr
);
3675 fprintf(stderr
, "\n");
3680 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
3683 static void visit_if(struct nir_to_llvm_context
*ctx
, nir_if
*if_stmt
)
3685 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
3687 LLVMBasicBlockRef merge_block
=
3688 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
3689 LLVMBasicBlockRef if_block
=
3690 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
3691 LLVMBasicBlockRef else_block
= merge_block
;
3692 if (!exec_list_is_empty(&if_stmt
->else_list
))
3693 else_block
= LLVMAppendBasicBlockInContext(
3694 ctx
->context
, ctx
->main_function
, "");
3696 LLVMValueRef cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, value
,
3697 LLVMConstInt(ctx
->i32
, 0, false), "");
3698 LLVMBuildCondBr(ctx
->builder
, cond
, if_block
, else_block
);
3700 LLVMPositionBuilderAtEnd(ctx
->builder
, if_block
);
3701 visit_cf_list(ctx
, &if_stmt
->then_list
);
3702 if (LLVMGetInsertBlock(ctx
->builder
))
3703 LLVMBuildBr(ctx
->builder
, merge_block
);
3705 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
3706 LLVMPositionBuilderAtEnd(ctx
->builder
, else_block
);
3707 visit_cf_list(ctx
, &if_stmt
->else_list
);
3708 if (LLVMGetInsertBlock(ctx
->builder
))
3709 LLVMBuildBr(ctx
->builder
, merge_block
);
3712 LLVMPositionBuilderAtEnd(ctx
->builder
, merge_block
);
3715 static void visit_loop(struct nir_to_llvm_context
*ctx
, nir_loop
*loop
)
3717 LLVMBasicBlockRef continue_parent
= ctx
->continue_block
;
3718 LLVMBasicBlockRef break_parent
= ctx
->break_block
;
3720 ctx
->continue_block
=
3721 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
3723 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
3725 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
3726 LLVMPositionBuilderAtEnd(ctx
->builder
, ctx
->continue_block
);
3727 visit_cf_list(ctx
, &loop
->body
);
3729 if (LLVMGetInsertBlock(ctx
->builder
))
3730 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
3731 LLVMPositionBuilderAtEnd(ctx
->builder
, ctx
->break_block
);
3733 ctx
->continue_block
= continue_parent
;
3734 ctx
->break_block
= break_parent
;
3737 static void visit_cf_list(struct nir_to_llvm_context
*ctx
,
3738 struct exec_list
*list
)
3740 foreach_list_typed(nir_cf_node
, node
, node
, list
)
3742 switch (node
->type
) {
3743 case nir_cf_node_block
:
3744 visit_block(ctx
, nir_cf_node_as_block(node
));
3747 case nir_cf_node_if
:
3748 visit_if(ctx
, nir_cf_node_as_if(node
));
3751 case nir_cf_node_loop
:
3752 visit_loop(ctx
, nir_cf_node_as_loop(node
));
3762 handle_vs_input_decl(struct nir_to_llvm_context
*ctx
,
3763 struct nir_variable
*variable
)
3765 LLVMValueRef t_list_ptr
= ctx
->vertex_buffers
;
3766 LLVMValueRef t_offset
;
3767 LLVMValueRef t_list
;
3768 LLVMValueRef args
[3];
3770 LLVMValueRef buffer_index
;
3771 int index
= variable
->data
.location
- VERT_ATTRIB_GENERIC0
;
3772 int idx
= variable
->data
.location
;
3773 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, true);
3775 variable
->data
.driver_location
= idx
* 4;
3777 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << index
)) {
3778 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->instance_id
,
3779 ctx
->start_instance
, "");
3780 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
3781 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
3783 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->vertex_id
,
3784 ctx
->base_vertex
, "");
3786 for (unsigned i
= 0; i
< attrib_count
; ++i
, ++idx
) {
3787 t_offset
= LLVMConstInt(ctx
->i32
, index
+ i
, false);
3789 t_list
= build_indexed_load_const(ctx
, t_list_ptr
, t_offset
);
3791 args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
3792 args
[2] = buffer_index
;
3793 input
= ac_emit_llvm_intrinsic(&ctx
->ac
,
3794 "llvm.SI.vs.load.input", ctx
->v4f32
, args
, 3,
3795 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_NOUNWIND
);
3797 for (unsigned chan
= 0; chan
< 4; chan
++) {
3798 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
3799 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
, chan
)] =
3800 to_integer(ctx
, LLVMBuildExtractElement(ctx
->builder
,
3801 input
, llvm_chan
, ""));
3807 static void interp_fs_input(struct nir_to_llvm_context
*ctx
,
3809 LLVMValueRef interp_param
,
3810 LLVMValueRef prim_mask
,
3811 LLVMValueRef result
[4])
3813 const char *intr_name
;
3814 LLVMValueRef attr_number
;
3817 attr_number
= LLVMConstInt(ctx
->i32
, attr
, false);
3819 /* fs.constant returns the param from the middle vertex, so it's not
3820 * really useful for flat shading. It's meant to be used for custom
3821 * interpolation (but the intrinsic can't fetch from the other two
3824 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
3825 * to do the right thing. The only reason we use fs.constant is that
3826 * fs.interp cannot be used on integers, because they can be equal
3829 intr_name
= interp_param
? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
3831 for (chan
= 0; chan
< 4; chan
++) {
3832 LLVMValueRef args
[4];
3833 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
3835 args
[0] = llvm_chan
;
3836 args
[1] = attr_number
;
3837 args
[2] = prim_mask
;
3838 args
[3] = interp_param
;
3839 result
[chan
] = ac_emit_llvm_intrinsic(&ctx
->ac
, intr_name
,
3840 ctx
->f32
, args
, args
[3] ? 4 : 3,
3841 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_NOUNWIND
);
3846 handle_fs_input_decl(struct nir_to_llvm_context
*ctx
,
3847 struct nir_variable
*variable
)
3849 int idx
= variable
->data
.location
;
3850 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
3851 LLVMValueRef interp
;
3853 variable
->data
.driver_location
= idx
* 4;
3854 ctx
->input_mask
|= ((1ull << attrib_count
) - 1) << variable
->data
.location
;
3856 if (glsl_get_base_type(glsl_without_array(variable
->type
)) == GLSL_TYPE_FLOAT
) {
3857 unsigned interp_type
;
3858 if (variable
->data
.sample
) {
3859 interp_type
= INTERP_SAMPLE
;
3860 ctx
->shader_info
->fs
.force_persample
= true;
3861 } else if (variable
->data
.centroid
)
3862 interp_type
= INTERP_CENTROID
;
3864 interp_type
= INTERP_CENTER
;
3866 interp
= lookup_interp_param(ctx
, variable
->data
.interpolation
, interp_type
);
3870 for (unsigned i
= 0; i
< attrib_count
; ++i
)
3871 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
+ i
, 0)] = interp
;
3876 handle_shader_input_decl(struct nir_to_llvm_context
*ctx
,
3877 struct nir_variable
*variable
)
3879 switch (ctx
->stage
) {
3880 case MESA_SHADER_VERTEX
:
3881 handle_vs_input_decl(ctx
, variable
);
3883 case MESA_SHADER_FRAGMENT
:
3884 handle_fs_input_decl(ctx
, variable
);
3893 handle_fs_inputs_pre(struct nir_to_llvm_context
*ctx
,
3894 struct nir_shader
*nir
)
3897 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_INPUTS
; ++i
) {
3898 LLVMValueRef interp_param
;
3899 LLVMValueRef
*inputs
= ctx
->inputs
+radeon_llvm_reg_index_soa(i
, 0);
3901 if (!(ctx
->input_mask
& (1ull << i
)))
3904 if (i
>= VARYING_SLOT_VAR0
|| i
== VARYING_SLOT_PNTC
) {
3905 interp_param
= *inputs
;
3906 interp_fs_input(ctx
, index
, interp_param
, ctx
->prim_mask
,
3910 ctx
->shader_info
->fs
.flat_shaded_mask
|= 1u << index
;
3912 } else if (i
== VARYING_SLOT_POS
) {
3913 for(int i
= 0; i
< 3; ++i
)
3914 inputs
[i
] = ctx
->frag_pos
[i
];
3916 inputs
[3] = ac_emit_fdiv(&ctx
->ac
, ctx
->f32one
, ctx
->frag_pos
[3]);
3919 ctx
->shader_info
->fs
.num_interp
= index
;
3920 if (ctx
->input_mask
& (1 << VARYING_SLOT_PNTC
))
3921 ctx
->shader_info
->fs
.has_pcoord
= true;
3922 ctx
->shader_info
->fs
.input_mask
= ctx
->input_mask
>> VARYING_SLOT_VAR0
;
3926 ac_build_alloca(struct nir_to_llvm_context
*ctx
,
3930 LLVMBuilderRef builder
= ctx
->builder
;
3931 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
3932 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
3933 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
3934 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
3935 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ctx
->context
);
3939 LLVMPositionBuilderBefore(first_builder
, first_instr
);
3941 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
3944 res
= LLVMBuildAlloca(first_builder
, type
, name
);
3945 LLVMBuildStore(builder
, LLVMConstNull(type
), res
);
3947 LLVMDisposeBuilder(first_builder
);
3952 static LLVMValueRef
si_build_alloca_undef(struct nir_to_llvm_context
*ctx
,
3956 LLVMValueRef ptr
= ac_build_alloca(ctx
, type
, name
);
3957 LLVMBuildStore(ctx
->builder
, LLVMGetUndef(type
), ptr
);
3962 handle_shader_output_decl(struct nir_to_llvm_context
*ctx
,
3963 struct nir_variable
*variable
)
3965 int idx
= variable
->data
.location
+ variable
->data
.index
;
3966 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
3968 variable
->data
.driver_location
= idx
* 4;
3970 if (ctx
->stage
== MESA_SHADER_VERTEX
) {
3972 if (idx
== VARYING_SLOT_CLIP_DIST0
||
3973 idx
== VARYING_SLOT_CULL_DIST0
) {
3974 int length
= glsl_get_length(variable
->type
);
3975 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
3976 ctx
->shader_info
->vs
.clip_dist_mask
= (1 << length
) - 1;
3977 ctx
->num_clips
= length
;
3978 } else if (idx
== VARYING_SLOT_CULL_DIST0
) {
3979 ctx
->shader_info
->vs
.cull_dist_mask
= (1 << length
) - 1;
3980 ctx
->num_culls
= length
;
3989 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
3990 for (unsigned chan
= 0; chan
< 4; chan
++) {
3991 ctx
->outputs
[radeon_llvm_reg_index_soa(idx
+ i
, chan
)] =
3992 si_build_alloca_undef(ctx
, ctx
->f32
, "");
3995 ctx
->output_mask
|= ((1ull << attrib_count
) - 1) << idx
;
3999 setup_locals(struct nir_to_llvm_context
*ctx
,
4000 struct nir_function
*func
)
4003 ctx
->num_locals
= 0;
4004 nir_foreach_variable(variable
, &func
->impl
->locals
) {
4005 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4006 variable
->data
.driver_location
= ctx
->num_locals
* 4;
4007 ctx
->num_locals
+= attrib_count
;
4009 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
4013 for (i
= 0; i
< ctx
->num_locals
; i
++) {
4014 for (j
= 0; j
< 4; j
++) {
4015 ctx
->locals
[i
* 4 + j
] =
4016 si_build_alloca_undef(ctx
, ctx
->f32
, "temp");
4022 emit_float_saturate(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
, float lo
, float hi
)
4024 v
= to_float(ctx
, v
);
4025 v
= emit_intrin_2f_param(ctx
, "llvm.maxnum.f32", v
, LLVMConstReal(ctx
->f32
, lo
));
4026 return emit_intrin_2f_param(ctx
, "llvm.minnum.f32", v
, LLVMConstReal(ctx
->f32
, hi
));
4030 static LLVMValueRef
emit_pack_int16(struct nir_to_llvm_context
*ctx
,
4031 LLVMValueRef src0
, LLVMValueRef src1
)
4033 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
4034 LLVMValueRef comp
[2];
4036 comp
[0] = LLVMBuildAnd(ctx
->builder
, src0
, LLVMConstInt(ctx
-> i32
, 65535, 0), "");
4037 comp
[1] = LLVMBuildAnd(ctx
->builder
, src1
, LLVMConstInt(ctx
-> i32
, 65535, 0), "");
4038 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
4039 return LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
4042 /* Initialize arguments for the shader export intrinsic */
4044 si_llvm_init_export_args(struct nir_to_llvm_context
*ctx
,
4045 LLVMValueRef
*values
,
4049 /* Default is 0xf. Adjusted below depending on the format. */
4050 args
[0] = LLVMConstInt(ctx
->i32
, target
!= V_008DFC_SQ_EXP_NULL
? 0xf : 0, false);
4051 /* Specify whether the EXEC mask represents the valid mask */
4052 args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
4054 /* Specify whether this is the last export */
4055 args
[2] = LLVMConstInt(ctx
->i32
, 0, false);
4056 /* Specify the target we are exporting */
4057 args
[3] = LLVMConstInt(ctx
->i32
, target
, false);
4059 args
[4] = LLVMConstInt(ctx
->i32
, 0, false); /* COMPR flag */
4060 args
[5] = LLVMGetUndef(ctx
->f32
);
4061 args
[6] = LLVMGetUndef(ctx
->f32
);
4062 args
[7] = LLVMGetUndef(ctx
->f32
);
4063 args
[8] = LLVMGetUndef(ctx
->f32
);
4068 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&& target
>= V_008DFC_SQ_EXP_MRT
) {
4069 LLVMValueRef val
[4];
4070 unsigned index
= target
- V_008DFC_SQ_EXP_MRT
;
4071 unsigned col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * index
)) & 0xf;
4072 bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> index
) & 1;
4074 switch(col_format
) {
4075 case V_028714_SPI_SHADER_ZERO
:
4076 args
[0] = LLVMConstInt(ctx
->i32
, 0x0, 0);
4077 args
[3] = LLVMConstInt(ctx
->i32
, V_008DFC_SQ_EXP_NULL
, 0);
4080 case V_028714_SPI_SHADER_32_R
:
4081 args
[0] = LLVMConstInt(ctx
->i32
, 0x1, 0);
4082 args
[5] = values
[0];
4085 case V_028714_SPI_SHADER_32_GR
:
4086 args
[0] = LLVMConstInt(ctx
->i32
, 0x3, 0);
4087 args
[5] = values
[0];
4088 args
[6] = values
[1];
4091 case V_028714_SPI_SHADER_32_AR
:
4092 args
[0] = LLVMConstInt(ctx
->i32
, 0x9, 0);
4093 args
[5] = values
[0];
4094 args
[8] = values
[3];
4097 case V_028714_SPI_SHADER_FP16_ABGR
:
4098 args
[4] = ctx
->i32one
;
4100 for (unsigned chan
= 0; chan
< 2; chan
++) {
4101 LLVMValueRef pack_args
[2] = {
4103 values
[2 * chan
+ 1]
4105 LLVMValueRef packed
;
4107 packed
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.SI.packf16",
4108 ctx
->i32
, pack_args
, 2,
4109 AC_FUNC_ATTR_READNONE
);
4110 args
[chan
+ 5] = packed
;
4114 case V_028714_SPI_SHADER_UNORM16_ABGR
:
4115 for (unsigned chan
= 0; chan
< 4; chan
++) {
4116 val
[chan
] = emit_float_saturate(ctx
, values
[chan
], 0, 1);
4117 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
4118 LLVMConstReal(ctx
->f32
, 65535), "");
4119 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
4120 LLVMConstReal(ctx
->f32
, 0.5), "");
4121 val
[chan
] = LLVMBuildFPToUI(ctx
->builder
, val
[chan
],
4125 args
[4] = ctx
->i32one
;
4126 args
[5] = emit_pack_int16(ctx
, val
[0], val
[1]);
4127 args
[6] = emit_pack_int16(ctx
, val
[2], val
[3]);
4130 case V_028714_SPI_SHADER_SNORM16_ABGR
:
4131 for (unsigned chan
= 0; chan
< 4; chan
++) {
4132 val
[chan
] = emit_float_saturate(ctx
, values
[chan
], -1, 1);
4133 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
4134 LLVMConstReal(ctx
->f32
, 32767), "");
4136 /* If positive, add 0.5, else add -0.5. */
4137 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
4138 LLVMBuildSelect(ctx
->builder
,
4139 LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
,
4140 val
[chan
], ctx
->f32zero
, ""),
4141 LLVMConstReal(ctx
->f32
, 0.5),
4142 LLVMConstReal(ctx
->f32
, -0.5), ""), "");
4143 val
[chan
] = LLVMBuildFPToSI(ctx
->builder
, val
[chan
], ctx
->i32
, "");
4146 args
[4] = ctx
->i32one
;
4147 args
[5] = emit_pack_int16(ctx
, val
[0], val
[1]);
4148 args
[6] = emit_pack_int16(ctx
, val
[2], val
[3]);
4151 case V_028714_SPI_SHADER_UINT16_ABGR
: {
4152 LLVMValueRef max
= LLVMConstInt(ctx
->i32
, is_int8
? 255 : 65535, 0);
4154 for (unsigned chan
= 0; chan
< 4; chan
++) {
4155 val
[chan
] = to_integer(ctx
, values
[chan
]);
4156 val
[chan
] = emit_minmax_int(ctx
, LLVMIntULT
, val
[chan
], max
);
4159 args
[4] = ctx
->i32one
;
4160 args
[5] = emit_pack_int16(ctx
, val
[0], val
[1]);
4161 args
[6] = emit_pack_int16(ctx
, val
[2], val
[3]);
4165 case V_028714_SPI_SHADER_SINT16_ABGR
: {
4166 LLVMValueRef max
= LLVMConstInt(ctx
->i32
, is_int8
? 127 : 32767, 0);
4167 LLVMValueRef min
= LLVMConstInt(ctx
->i32
, is_int8
? -128 : -32768, 0);
4170 for (unsigned chan
= 0; chan
< 4; chan
++) {
4171 val
[chan
] = to_integer(ctx
, values
[chan
]);
4172 val
[chan
] = emit_minmax_int(ctx
, LLVMIntSLT
, val
[chan
], max
);
4173 val
[chan
] = emit_minmax_int(ctx
, LLVMIntSGT
, val
[chan
], min
);
4176 args
[4] = ctx
->i32one
;
4177 args
[5] = emit_pack_int16(ctx
, val
[0], val
[1]);
4178 args
[6] = emit_pack_int16(ctx
, val
[2], val
[3]);
4183 case V_028714_SPI_SHADER_32_ABGR
:
4184 memcpy(&args
[5], values
, sizeof(values
[0]) * 4);
4188 memcpy(&args
[5], values
, sizeof(values
[0]) * 4);
4190 for (unsigned i
= 5; i
< 9; ++i
)
4191 args
[i
] = to_float(ctx
, args
[i
]);
4195 handle_vs_outputs_post(struct nir_to_llvm_context
*ctx
)
4197 uint32_t param_count
= 0;
4199 unsigned pos_idx
, num_pos_exports
= 0;
4200 LLVMValueRef args
[9];
4201 LLVMValueRef pos_args
[4][9] = { { 0 } };
4202 LLVMValueRef psize_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
4204 const uint64_t clip_mask
= ctx
->output_mask
& ((1ull << VARYING_SLOT_CLIP_DIST0
) |
4205 (1ull << VARYING_SLOT_CLIP_DIST1
) |
4206 (1ull << VARYING_SLOT_CULL_DIST0
) |
4207 (1ull << VARYING_SLOT_CULL_DIST1
));
4210 LLVMValueRef slots
[8];
4213 if (ctx
->shader_info
->vs
.cull_dist_mask
)
4214 ctx
->shader_info
->vs
.cull_dist_mask
<<= ctx
->num_clips
;
4216 i
= VARYING_SLOT_CLIP_DIST0
;
4217 for (j
= 0; j
< ctx
->num_clips
; j
++)
4218 slots
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4219 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4220 i
= VARYING_SLOT_CULL_DIST0
;
4221 for (j
= 0; j
< ctx
->num_culls
; j
++)
4222 slots
[ctx
->num_clips
+ j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4223 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4225 for (i
= ctx
->num_clips
+ ctx
->num_culls
; i
< 8; i
++)
4226 slots
[i
] = LLVMGetUndef(ctx
->f32
);
4228 if (ctx
->num_clips
+ ctx
->num_culls
> 4) {
4229 target
= V_008DFC_SQ_EXP_POS
+ 3;
4230 si_llvm_init_export_args(ctx
, &slots
[4], target
, args
);
4231 memcpy(pos_args
[target
- V_008DFC_SQ_EXP_POS
],
4232 args
, sizeof(args
));
4235 target
= V_008DFC_SQ_EXP_POS
+ 2;
4236 si_llvm_init_export_args(ctx
, &slots
[0], target
, args
);
4237 memcpy(pos_args
[target
- V_008DFC_SQ_EXP_POS
],
4238 args
, sizeof(args
));
4242 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
4243 LLVMValueRef values
[4];
4244 if (!(ctx
->output_mask
& (1ull << i
)))
4247 for (unsigned j
= 0; j
< 4; j
++)
4248 values
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4249 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4251 if (i
== VARYING_SLOT_POS
) {
4252 target
= V_008DFC_SQ_EXP_POS
;
4253 } else if (i
== VARYING_SLOT_CLIP_DIST0
||
4254 i
== VARYING_SLOT_CLIP_DIST1
||
4255 i
== VARYING_SLOT_CULL_DIST0
||
4256 i
== VARYING_SLOT_CULL_DIST1
) {
4258 } else if (i
== VARYING_SLOT_PSIZ
) {
4259 ctx
->shader_info
->vs
.writes_pointsize
= true;
4260 psize_value
= values
[0];
4262 } else if (i
== VARYING_SLOT_LAYER
) {
4263 ctx
->shader_info
->vs
.writes_layer
= true;
4264 layer_value
= values
[0];
4266 } else if (i
== VARYING_SLOT_VIEWPORT
) {
4267 ctx
->shader_info
->vs
.writes_viewport_index
= true;
4268 viewport_index_value
= values
[0];
4270 } else if (i
>= VARYING_SLOT_VAR0
) {
4271 ctx
->shader_info
->vs
.export_mask
|= 1u << (i
- VARYING_SLOT_VAR0
);
4272 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
4276 si_llvm_init_export_args(ctx
, values
, target
, args
);
4278 if (target
>= V_008DFC_SQ_EXP_POS
&&
4279 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
4280 memcpy(pos_args
[target
- V_008DFC_SQ_EXP_POS
],
4281 args
, sizeof(args
));
4283 ac_emit_llvm_intrinsic(&ctx
->ac
,
4290 /* We need to add the position output manually if it's missing. */
4291 if (!pos_args
[0][0]) {
4292 pos_args
[0][0] = LLVMConstInt(ctx
->i32
, 0xf, false);
4293 pos_args
[0][1] = ctx
->i32zero
; /* EXEC mask */
4294 pos_args
[0][2] = ctx
->i32zero
; /* last export? */
4295 pos_args
[0][3] = LLVMConstInt(ctx
->i32
, V_008DFC_SQ_EXP_POS
, false);
4296 pos_args
[0][4] = ctx
->i32zero
; /* COMPR flag */
4297 pos_args
[0][5] = ctx
->f32zero
; /* X */
4298 pos_args
[0][6] = ctx
->f32zero
; /* Y */
4299 pos_args
[0][7] = ctx
->f32zero
; /* Z */
4300 pos_args
[0][8] = ctx
->f32one
; /* W */
4303 uint32_t mask
= ((ctx
->shader_info
->vs
.writes_pointsize
== true ? 1 : 0) |
4304 (ctx
->shader_info
->vs
.writes_layer
== true ? 4 : 0) |
4305 (ctx
->shader_info
->vs
.writes_viewport_index
== true ? 8 : 0));
4307 pos_args
[1][0] = LLVMConstInt(ctx
->i32
, mask
, false); /* writemask */
4308 pos_args
[1][1] = ctx
->i32zero
; /* EXEC mask */
4309 pos_args
[1][2] = ctx
->i32zero
; /* last export? */
4310 pos_args
[1][3] = LLVMConstInt(ctx
->i32
, V_008DFC_SQ_EXP_POS
+ 1, false);
4311 pos_args
[1][4] = ctx
->i32zero
; /* COMPR flag */
4312 pos_args
[1][5] = ctx
->f32zero
; /* X */
4313 pos_args
[1][6] = ctx
->f32zero
; /* Y */
4314 pos_args
[1][7] = ctx
->f32zero
; /* Z */
4315 pos_args
[1][8] = ctx
->f32zero
; /* W */
4317 if (ctx
->shader_info
->vs
.writes_pointsize
== true)
4318 pos_args
[1][5] = psize_value
;
4319 if (ctx
->shader_info
->vs
.writes_layer
== true)
4320 pos_args
[1][7] = layer_value
;
4321 if (ctx
->shader_info
->vs
.writes_viewport_index
== true)
4322 pos_args
[1][8] = viewport_index_value
;
4324 for (i
= 0; i
< 4; i
++) {
4330 for (i
= 0; i
< 4; i
++) {
4331 if (!pos_args
[i
][0])
4334 /* Specify the target we are exporting */
4335 pos_args
[i
][3] = LLVMConstInt(ctx
->i32
, V_008DFC_SQ_EXP_POS
+ pos_idx
++, false);
4336 if (pos_idx
== num_pos_exports
)
4337 pos_args
[i
][2] = ctx
->i32one
;
4338 ac_emit_llvm_intrinsic(&ctx
->ac
,
4344 ctx
->shader_info
->vs
.pos_exports
= num_pos_exports
;
4345 ctx
->shader_info
->vs
.param_exports
= param_count
;
4349 si_export_mrt_color(struct nir_to_llvm_context
*ctx
,
4350 LLVMValueRef
*color
, unsigned param
, bool is_last
)
4352 LLVMValueRef args
[9];
4354 si_llvm_init_export_args(ctx
, color
, param
,
4358 args
[1] = ctx
->i32one
; /* whether the EXEC mask is valid */
4359 args
[2] = ctx
->i32one
; /* DONE bit */
4360 } else if (args
[0] == ctx
->i32zero
)
4361 return; /* unnecessary NULL export */
4363 ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.SI.export",
4364 ctx
->voidt
, args
, 9, 0);
4368 si_export_mrt_z(struct nir_to_llvm_context
*ctx
,
4369 LLVMValueRef depth
, LLVMValueRef stencil
,
4370 LLVMValueRef samplemask
)
4372 LLVMValueRef args
[9];
4374 args
[1] = ctx
->i32one
; /* whether the EXEC mask is valid */
4375 args
[2] = ctx
->i32one
; /* DONE bit */
4376 /* Specify the target we are exporting */
4377 args
[3] = LLVMConstInt(ctx
->i32
, V_008DFC_SQ_EXP_MRTZ
, false);
4379 args
[4] = ctx
->i32zero
; /* COMP flag */
4380 args
[5] = LLVMGetUndef(ctx
->f32
); /* R, depth */
4381 args
[6] = LLVMGetUndef(ctx
->f32
); /* G, stencil test val[0:7], stencil op val[8:15] */
4382 args
[7] = LLVMGetUndef(ctx
->f32
); /* B, sample mask */
4383 args
[8] = LLVMGetUndef(ctx
->f32
); /* A, alpha to mask */
4396 args
[7] = samplemask
;
4400 /* SI (except OLAND) has a bug that it only looks
4401 * at the X writemask component. */
4402 if (ctx
->options
->chip_class
== SI
&&
4403 ctx
->options
->family
!= CHIP_OLAND
)
4406 args
[0] = LLVMConstInt(ctx
->i32
, mask
, false);
4407 ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.SI.export",
4408 ctx
->voidt
, args
, 9, 0);
4412 handle_fs_outputs_post(struct nir_to_llvm_context
*ctx
)
4415 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
4417 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
4418 LLVMValueRef values
[4];
4420 if (!(ctx
->output_mask
& (1ull << i
)))
4423 if (i
== FRAG_RESULT_DEPTH
) {
4424 ctx
->shader_info
->fs
.writes_z
= true;
4425 depth
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4426 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
4427 } else if (i
== FRAG_RESULT_STENCIL
) {
4428 ctx
->shader_info
->fs
.writes_stencil
= true;
4429 stencil
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4430 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
4433 for (unsigned j
= 0; j
< 4; j
++)
4434 values
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4435 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4437 if (!ctx
->shader_info
->fs
.writes_z
&& !ctx
->shader_info
->fs
.writes_stencil
)
4438 last
= ctx
->output_mask
<= ((1ull << (i
+ 1)) - 1);
4440 si_export_mrt_color(ctx
, values
, V_008DFC_SQ_EXP_MRT
+ index
, last
);
4445 if (depth
|| stencil
)
4446 si_export_mrt_z(ctx
, depth
, stencil
, samplemask
);
4448 si_export_mrt_color(ctx
, NULL
, V_008DFC_SQ_EXP_NULL
, true);
4450 ctx
->shader_info
->fs
.output_mask
= index
? ((1ull << index
) - 1) : 0;
4454 handle_shader_outputs_post(struct nir_to_llvm_context
*ctx
)
4456 switch (ctx
->stage
) {
4457 case MESA_SHADER_VERTEX
:
4458 handle_vs_outputs_post(ctx
);
4460 case MESA_SHADER_FRAGMENT
:
4461 handle_fs_outputs_post(ctx
);
4469 handle_shared_compute_var(struct nir_to_llvm_context
*ctx
,
4470 struct nir_variable
*variable
, uint32_t *offset
, int idx
)
4472 unsigned size
= glsl_count_attribute_slots(variable
->type
, false);
4473 variable
->data
.driver_location
= *offset
;
4477 static void ac_llvm_finalize_module(struct nir_to_llvm_context
* ctx
)
4479 LLVMPassManagerRef passmgr
;
4480 /* Create the pass manager */
4481 passmgr
= LLVMCreateFunctionPassManagerForModule(
4484 /* This pass should eliminate all the load and store instructions */
4485 LLVMAddPromoteMemoryToRegisterPass(passmgr
);
4487 /* Add some optimization passes */
4488 LLVMAddScalarReplAggregatesPass(passmgr
);
4489 LLVMAddLICMPass(passmgr
);
4490 LLVMAddAggressiveDCEPass(passmgr
);
4491 LLVMAddCFGSimplificationPass(passmgr
);
4492 LLVMAddInstructionCombiningPass(passmgr
);
4495 LLVMInitializeFunctionPassManager(passmgr
);
4496 LLVMRunFunctionPassManager(passmgr
, ctx
->main_function
);
4497 LLVMFinalizeFunctionPassManager(passmgr
);
4499 LLVMDisposeBuilder(ctx
->builder
);
4500 LLVMDisposePassManager(passmgr
);
4504 ac_setup_rings(struct nir_to_llvm_context
*ctx
)
4506 if (ctx
->stage
== MESA_SHADER_VERTEX
&& ctx
->options
->key
.vs
.as_es
) {
4507 ctx
->esgs_ring
= build_indexed_load_const(ctx
, ctx
->ring_offsets
, ctx
->i32one
);
4510 if (ctx
->is_gs_copy_shader
) {
4511 ctx
->gsvs_ring
= build_indexed_load_const(ctx
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, 3, false));
4513 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
4515 ctx
->esgs_ring
= build_indexed_load_const(ctx
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, 2, false));
4516 ctx
->gsvs_ring
= build_indexed_load_const(ctx
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, 4, false));
4518 ctx
->gsvs_ring
= LLVMBuildBitCast(ctx
->builder
, ctx
->gsvs_ring
, ctx
->v4i32
, "");
4520 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->gsvs_num_entries
, LLVMConstInt(ctx
->i32
, 2, false), "");
4521 tmp
= LLVMBuildExtractElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->i32one
, "");
4522 tmp
= LLVMBuildOr(ctx
->builder
, tmp
, ctx
->gsvs_ring_stride
, "");
4523 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, tmp
, ctx
->i32one
, "");
4525 ctx
->gsvs_ring
= LLVMBuildBitCast(ctx
->builder
, ctx
->gsvs_ring
, ctx
->v16i8
, "");
4530 LLVMModuleRef
ac_translate_nir_to_llvm(LLVMTargetMachineRef tm
,
4531 struct nir_shader
*nir
,
4532 struct ac_shader_variant_info
*shader_info
,
4533 const struct ac_nir_compiler_options
*options
)
4535 struct nir_to_llvm_context ctx
= {0};
4536 struct nir_function
*func
;
4538 ctx
.options
= options
;
4539 ctx
.shader_info
= shader_info
;
4540 ctx
.context
= LLVMContextCreate();
4541 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
4543 ac_llvm_context_init(&ctx
.ac
, ctx
.context
);
4544 ctx
.ac
.module
= ctx
.module
;
4546 ctx
.has_ds_bpermute
= ctx
.options
->chip_class
>= VI
;
4548 memset(shader_info
, 0, sizeof(*shader_info
));
4550 LLVMSetTarget(ctx
.module
, options
->supports_spill
? "amdgcn-mesa-mesa3d" : "amdgcn--");
4553 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
4554 ctx
.ac
.builder
= ctx
.builder
;
4555 ctx
.stage
= nir
->stage
;
4557 for (i
= 0; i
< AC_UD_MAX_SETS
; i
++)
4558 shader_info
->user_sgprs_locs
.descriptor_sets
[i
].sgpr_idx
= -1;
4559 for (i
= 0; i
< AC_UD_MAX_UD
; i
++)
4560 shader_info
->user_sgprs_locs
.shader_data
[i
].sgpr_idx
= -1;
4562 create_function(&ctx
);
4564 if (nir
->stage
== MESA_SHADER_COMPUTE
) {
4566 nir_foreach_variable(variable
, &nir
->shared
)
4570 uint32_t shared_size
= 0;
4572 LLVMTypeRef i8p
= LLVMPointerType(ctx
.i8
, LOCAL_ADDR_SPACE
);
4573 nir_foreach_variable(variable
, &nir
->shared
) {
4574 handle_shared_compute_var(&ctx
, variable
, &shared_size
, idx
);
4579 var
= LLVMAddGlobalInAddressSpace(ctx
.module
,
4580 LLVMArrayType(ctx
.i8
, shared_size
),
4583 LLVMSetAlignment(var
, 4);
4584 ctx
.shared_memory
= LLVMBuildBitCast(ctx
.builder
, var
, i8p
, "");
4586 } else if (nir
->stage
== MESA_SHADER_GEOMETRY
) {
4587 ctx
.gs_next_vertex
= ac_build_alloca(&ctx
, ctx
.i32
, "gs_next_vertex");
4589 ctx
.gs_max_out_vertices
= nir
->info
->gs
.vertices_out
;
4592 ac_setup_rings(&ctx
);
4594 nir_foreach_variable(variable
, &nir
->inputs
)
4595 handle_shader_input_decl(&ctx
, variable
);
4597 if (nir
->stage
== MESA_SHADER_FRAGMENT
)
4598 handle_fs_inputs_pre(&ctx
, nir
);
4600 nir_foreach_variable(variable
, &nir
->outputs
)
4601 handle_shader_output_decl(&ctx
, variable
);
4603 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4604 _mesa_key_pointer_equal
);
4605 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4606 _mesa_key_pointer_equal
);
4608 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
4610 setup_locals(&ctx
, func
);
4612 visit_cf_list(&ctx
, &func
->impl
->body
);
4613 phi_post_pass(&ctx
);
4615 handle_shader_outputs_post(&ctx
);
4616 LLVMBuildRetVoid(ctx
.builder
);
4618 ac_llvm_finalize_module(&ctx
);
4620 ralloc_free(ctx
.defs
);
4621 ralloc_free(ctx
.phis
);
4626 static void ac_diagnostic_handler(LLVMDiagnosticInfoRef di
, void *context
)
4628 unsigned *retval
= (unsigned *)context
;
4629 LLVMDiagnosticSeverity severity
= LLVMGetDiagInfoSeverity(di
);
4630 char *description
= LLVMGetDiagInfoDescription(di
);
4632 if (severity
== LLVMDSError
) {
4634 fprintf(stderr
, "LLVM triggered Diagnostic Handler: %s\n",
4638 LLVMDisposeMessage(description
);
4641 static unsigned ac_llvm_compile(LLVMModuleRef M
,
4642 struct ac_shader_binary
*binary
,
4643 LLVMTargetMachineRef tm
)
4645 unsigned retval
= 0;
4647 LLVMContextRef llvm_ctx
;
4648 LLVMMemoryBufferRef out_buffer
;
4649 unsigned buffer_size
;
4650 const char *buffer_data
;
4653 /* Setup Diagnostic Handler*/
4654 llvm_ctx
= LLVMGetModuleContext(M
);
4656 LLVMContextSetDiagnosticHandler(llvm_ctx
, ac_diagnostic_handler
,
4660 mem_err
= LLVMTargetMachineEmitToMemoryBuffer(tm
, M
, LLVMObjectFile
,
4663 /* Process Errors/Warnings */
4665 fprintf(stderr
, "%s: %s", __FUNCTION__
, err
);
4671 /* Extract Shader Code*/
4672 buffer_size
= LLVMGetBufferSize(out_buffer
);
4673 buffer_data
= LLVMGetBufferStart(out_buffer
);
4675 ac_elf_read(buffer_data
, buffer_size
, binary
);
4678 LLVMDisposeMemoryBuffer(out_buffer
);
4684 static void ac_compile_llvm_module(LLVMTargetMachineRef tm
,
4685 LLVMModuleRef llvm_module
,
4686 struct ac_shader_binary
*binary
,
4687 struct ac_shader_config
*config
,
4688 struct ac_shader_variant_info
*shader_info
,
4689 gl_shader_stage stage
,
4690 bool dump_shader
, bool supports_spill
)
4693 ac_dump_module(llvm_module
);
4695 memset(binary
, 0, sizeof(*binary
));
4696 int v
= ac_llvm_compile(llvm_module
, binary
, tm
);
4698 fprintf(stderr
, "compile failed\n");
4702 fprintf(stderr
, "disasm:\n%s\n", binary
->disasm_string
);
4704 ac_shader_binary_read_config(binary
, config
, 0, supports_spill
);
4706 LLVMContextRef ctx
= LLVMGetModuleContext(llvm_module
);
4707 LLVMDisposeModule(llvm_module
);
4708 LLVMContextDispose(ctx
);
4710 if (stage
== MESA_SHADER_FRAGMENT
) {
4711 shader_info
->num_input_vgprs
= 0;
4712 if (G_0286CC_PERSP_SAMPLE_ENA(config
->spi_ps_input_addr
))
4713 shader_info
->num_input_vgprs
+= 2;
4714 if (G_0286CC_PERSP_CENTER_ENA(config
->spi_ps_input_addr
))
4715 shader_info
->num_input_vgprs
+= 2;
4716 if (G_0286CC_PERSP_CENTROID_ENA(config
->spi_ps_input_addr
))
4717 shader_info
->num_input_vgprs
+= 2;
4718 if (G_0286CC_PERSP_PULL_MODEL_ENA(config
->spi_ps_input_addr
))
4719 shader_info
->num_input_vgprs
+= 3;
4720 if (G_0286CC_LINEAR_SAMPLE_ENA(config
->spi_ps_input_addr
))
4721 shader_info
->num_input_vgprs
+= 2;
4722 if (G_0286CC_LINEAR_CENTER_ENA(config
->spi_ps_input_addr
))
4723 shader_info
->num_input_vgprs
+= 2;
4724 if (G_0286CC_LINEAR_CENTROID_ENA(config
->spi_ps_input_addr
))
4725 shader_info
->num_input_vgprs
+= 2;
4726 if (G_0286CC_LINE_STIPPLE_TEX_ENA(config
->spi_ps_input_addr
))
4727 shader_info
->num_input_vgprs
+= 1;
4728 if (G_0286CC_POS_X_FLOAT_ENA(config
->spi_ps_input_addr
))
4729 shader_info
->num_input_vgprs
+= 1;
4730 if (G_0286CC_POS_Y_FLOAT_ENA(config
->spi_ps_input_addr
))
4731 shader_info
->num_input_vgprs
+= 1;
4732 if (G_0286CC_POS_Z_FLOAT_ENA(config
->spi_ps_input_addr
))
4733 shader_info
->num_input_vgprs
+= 1;
4734 if (G_0286CC_POS_W_FLOAT_ENA(config
->spi_ps_input_addr
))
4735 shader_info
->num_input_vgprs
+= 1;
4736 if (G_0286CC_FRONT_FACE_ENA(config
->spi_ps_input_addr
))
4737 shader_info
->num_input_vgprs
+= 1;
4738 if (G_0286CC_ANCILLARY_ENA(config
->spi_ps_input_addr
))
4739 shader_info
->num_input_vgprs
+= 1;
4740 if (G_0286CC_SAMPLE_COVERAGE_ENA(config
->spi_ps_input_addr
))
4741 shader_info
->num_input_vgprs
+= 1;
4742 if (G_0286CC_POS_FIXED_PT_ENA(config
->spi_ps_input_addr
))
4743 shader_info
->num_input_vgprs
+= 1;
4745 config
->num_vgprs
= MAX2(config
->num_vgprs
, shader_info
->num_input_vgprs
);
4747 /* +3 for scratch wave offset and VCC */
4748 config
->num_sgprs
= MAX2(config
->num_sgprs
,
4749 shader_info
->num_input_sgprs
+ 3);
4752 void ac_compile_nir_shader(LLVMTargetMachineRef tm
,
4753 struct ac_shader_binary
*binary
,
4754 struct ac_shader_config
*config
,
4755 struct ac_shader_variant_info
*shader_info
,
4756 struct nir_shader
*nir
,
4757 const struct ac_nir_compiler_options
*options
,
4761 LLVMModuleRef llvm_module
= ac_translate_nir_to_llvm(tm
, nir
, shader_info
,
4764 ac_compile_llvm_module(tm
, llvm_module
, binary
, config
, shader_info
, nir
->stage
, dump_shader
, options
->supports_spill
);
4765 switch (nir
->stage
) {
4766 case MESA_SHADER_COMPUTE
:
4767 for (int i
= 0; i
< 3; ++i
)
4768 shader_info
->cs
.block_size
[i
] = nir
->info
->cs
.local_size
[i
];
4770 case MESA_SHADER_FRAGMENT
:
4771 shader_info
->fs
.early_fragment_test
= nir
->info
->fs
.early_fragment_tests
;
4773 case MESA_SHADER_GEOMETRY
:
4774 shader_info
->gs
.vertices_in
= nir
->info
->gs
.vertices_in
;
4775 shader_info
->gs
.vertices_out
= nir
->info
->gs
.vertices_out
;
4776 shader_info
->gs
.output_prim
= nir
->info
->gs
.output_primitive
;
4777 shader_info
->gs
.invocations
= nir
->info
->gs
.invocations
;