2 * Copyright © 2016 Bas Nieuwenhuizen
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 #include "ac_nir_to_llvm.h"
25 #include "ac_llvm_build.h"
26 #include "ac_llvm_util.h"
27 #include "ac_binary.h"
30 #include "../vulkan/radv_descriptor_set.h"
31 #include "util/bitscan.h"
32 #include <llvm-c/Transforms/Scalar.h>
34 enum radeon_llvm_calling_convention
{
35 RADEON_LLVM_AMDGPU_VS
= 87,
36 RADEON_LLVM_AMDGPU_GS
= 88,
37 RADEON_LLVM_AMDGPU_PS
= 89,
38 RADEON_LLVM_AMDGPU_CS
= 90,
41 #define CONST_ADDR_SPACE 2
42 #define LOCAL_ADDR_SPACE 3
44 #define RADEON_LLVM_MAX_INPUTS (VARYING_SLOT_VAR31 + 1)
45 #define RADEON_LLVM_MAX_OUTPUTS (VARYING_SLOT_VAR31 + 1)
54 struct nir_to_llvm_context
{
55 struct ac_llvm_context ac
;
56 const struct ac_nir_compiler_options
*options
;
57 struct ac_shader_variant_info
*shader_info
;
59 LLVMContextRef context
;
61 LLVMBuilderRef builder
;
62 LLVMValueRef main_function
;
64 struct hash_table
*defs
;
65 struct hash_table
*phis
;
67 LLVMValueRef descriptor_sets
[AC_UD_MAX_SETS
];
68 LLVMValueRef ring_offsets
;
69 LLVMValueRef push_constants
;
70 LLVMValueRef num_work_groups
;
71 LLVMValueRef workgroup_ids
;
72 LLVMValueRef local_invocation_ids
;
75 LLVMValueRef vertex_buffers
;
76 LLVMValueRef base_vertex
;
77 LLVMValueRef start_instance
;
78 LLVMValueRef draw_index
;
79 LLVMValueRef vertex_id
;
80 LLVMValueRef rel_auto_id
;
81 LLVMValueRef vs_prim_id
;
82 LLVMValueRef instance_id
;
84 LLVMValueRef es2gs_offset
;
86 LLVMValueRef gsvs_ring_stride
;
87 LLVMValueRef gsvs_num_entries
;
88 LLVMValueRef gs2vs_offset
;
89 LLVMValueRef gs_wave_id
;
90 LLVMValueRef gs_vtx_offset
[6];
91 LLVMValueRef gs_prim_id
, gs_invocation_id
;
93 LLVMValueRef esgs_ring
;
94 LLVMValueRef gsvs_ring
;
96 LLVMValueRef prim_mask
;
97 LLVMValueRef sample_positions
;
98 LLVMValueRef persp_sample
, persp_center
, persp_centroid
;
99 LLVMValueRef linear_sample
, linear_center
, linear_centroid
;
100 LLVMValueRef front_face
;
101 LLVMValueRef ancillary
;
102 LLVMValueRef sample_coverage
;
103 LLVMValueRef frag_pos
[4];
105 LLVMBasicBlockRef continue_block
;
106 LLVMBasicBlockRef break_block
;
126 LLVMValueRef i1false
;
127 LLVMValueRef i32zero
;
129 LLVMValueRef f32zero
;
131 LLVMValueRef v4f32empty
;
133 unsigned uniform_md_kind
;
134 LLVMValueRef empty_md
;
135 gl_shader_stage stage
;
138 LLVMValueRef inputs
[RADEON_LLVM_MAX_INPUTS
* 4];
139 LLVMValueRef outputs
[RADEON_LLVM_MAX_OUTPUTS
* 4];
141 LLVMValueRef shared_memory
;
143 uint64_t output_mask
;
145 LLVMValueRef
*locals
;
147 uint8_t num_output_clips
;
148 uint8_t num_output_culls
;
150 bool has_ds_bpermute
;
152 bool is_gs_copy_shader
;
153 LLVMValueRef gs_next_vertex
;
154 unsigned gs_max_out_vertices
;
157 static LLVMValueRef
get_sampler_desc(struct nir_to_llvm_context
*ctx
,
158 nir_deref_var
*deref
,
159 enum desc_type desc_type
);
160 static unsigned radeon_llvm_reg_index_soa(unsigned index
, unsigned chan
)
162 return (index
* 4) + chan
;
165 static unsigned shader_io_get_unique_index(gl_varying_slot slot
)
167 if (slot
== VARYING_SLOT_POS
)
169 if (slot
== VARYING_SLOT_PSIZ
)
171 if (slot
== VARYING_SLOT_CLIP_DIST0
)
173 /* 3 is reserved for clip dist as well */
174 if (slot
>= VARYING_SLOT_VAR0
&& slot
<= VARYING_SLOT_VAR31
)
175 return 4 + (slot
- VARYING_SLOT_VAR0
);
176 unreachable("illegal slot in get unique index\n");
179 static unsigned llvm_get_type_size(LLVMTypeRef type
)
181 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
184 case LLVMIntegerTypeKind
:
185 return LLVMGetIntTypeWidth(type
) / 8;
186 case LLVMFloatTypeKind
:
188 case LLVMPointerTypeKind
:
190 case LLVMVectorTypeKind
:
191 return LLVMGetVectorSize(type
) *
192 llvm_get_type_size(LLVMGetElementType(type
));
199 static void set_llvm_calling_convention(LLVMValueRef func
,
200 gl_shader_stage stage
)
202 enum radeon_llvm_calling_convention calling_conv
;
205 case MESA_SHADER_VERTEX
:
206 case MESA_SHADER_TESS_CTRL
:
207 case MESA_SHADER_TESS_EVAL
:
208 calling_conv
= RADEON_LLVM_AMDGPU_VS
;
210 case MESA_SHADER_GEOMETRY
:
211 calling_conv
= RADEON_LLVM_AMDGPU_GS
;
213 case MESA_SHADER_FRAGMENT
:
214 calling_conv
= RADEON_LLVM_AMDGPU_PS
;
216 case MESA_SHADER_COMPUTE
:
217 calling_conv
= RADEON_LLVM_AMDGPU_CS
;
220 unreachable("Unhandle shader type");
223 LLVMSetFunctionCallConv(func
, calling_conv
);
227 create_llvm_function(LLVMContextRef ctx
, LLVMModuleRef module
,
228 LLVMBuilderRef builder
, LLVMTypeRef
*return_types
,
229 unsigned num_return_elems
, LLVMTypeRef
*param_types
,
230 unsigned param_count
, unsigned array_params_mask
,
231 unsigned sgpr_params
, bool unsafe_math
)
233 LLVMTypeRef main_function_type
, ret_type
;
234 LLVMBasicBlockRef main_function_body
;
236 if (num_return_elems
)
237 ret_type
= LLVMStructTypeInContext(ctx
, return_types
,
238 num_return_elems
, true);
240 ret_type
= LLVMVoidTypeInContext(ctx
);
242 /* Setup the function */
244 LLVMFunctionType(ret_type
, param_types
, param_count
, 0);
245 LLVMValueRef main_function
=
246 LLVMAddFunction(module
, "main", main_function_type
);
248 LLVMAppendBasicBlockInContext(ctx
, main_function
, "main_body");
249 LLVMPositionBuilderAtEnd(builder
, main_function_body
);
251 LLVMSetFunctionCallConv(main_function
, RADEON_LLVM_AMDGPU_CS
);
252 for (unsigned i
= 0; i
< sgpr_params
; ++i
) {
253 if (array_params_mask
& (1 << i
)) {
254 LLVMValueRef P
= LLVMGetParam(main_function
, i
);
255 ac_add_function_attr(ctx
, main_function
, i
+ 1, AC_FUNC_ATTR_BYVAL
);
256 ac_add_attr_dereferenceable(P
, UINT64_MAX
);
259 ac_add_function_attr(ctx
, main_function
, i
+ 1, AC_FUNC_ATTR_INREG
);
264 /* These were copied from some LLVM test. */
265 LLVMAddTargetDependentFunctionAttr(main_function
,
266 "less-precise-fpmad",
268 LLVMAddTargetDependentFunctionAttr(main_function
,
271 LLVMAddTargetDependentFunctionAttr(main_function
,
274 LLVMAddTargetDependentFunctionAttr(main_function
,
278 return main_function
;
281 static LLVMTypeRef
const_array(LLVMTypeRef elem_type
, int num_elements
)
283 return LLVMPointerType(LLVMArrayType(elem_type
, num_elements
),
287 static LLVMValueRef
get_shared_memory_ptr(struct nir_to_llvm_context
*ctx
,
295 offset
= LLVMConstInt(ctx
->i32
, idx
* 16, false);
297 ptr
= ctx
->shared_memory
;
298 ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &offset
, 1, "");
299 addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
300 ptr
= LLVMBuildBitCast(ctx
->builder
, ptr
, LLVMPointerType(type
, addr_space
), "");
304 static LLVMTypeRef
to_integer_type_scalar(struct nir_to_llvm_context
*ctx
, LLVMTypeRef t
)
306 if (t
== ctx
->f16
|| t
== ctx
->i16
)
308 else if (t
== ctx
->f32
|| t
== ctx
->i32
)
310 else if (t
== ctx
->f64
|| t
== ctx
->i64
)
313 unreachable("Unhandled integer size");
316 static LLVMTypeRef
to_integer_type(struct nir_to_llvm_context
*ctx
, LLVMTypeRef t
)
318 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
319 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
320 return LLVMVectorType(to_integer_type_scalar(ctx
, elem_type
),
321 LLVMGetVectorSize(t
));
323 return to_integer_type_scalar(ctx
, t
);
326 static LLVMValueRef
to_integer(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
)
328 LLVMTypeRef type
= LLVMTypeOf(v
);
329 return LLVMBuildBitCast(ctx
->builder
, v
, to_integer_type(ctx
, type
), "");
332 static LLVMTypeRef
to_float_type_scalar(struct nir_to_llvm_context
*ctx
, LLVMTypeRef t
)
334 if (t
== ctx
->i16
|| t
== ctx
->f16
)
336 else if (t
== ctx
->i32
|| t
== ctx
->f32
)
338 else if (t
== ctx
->i64
|| t
== ctx
->f64
)
341 unreachable("Unhandled float size");
344 static LLVMTypeRef
to_float_type(struct nir_to_llvm_context
*ctx
, LLVMTypeRef t
)
346 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
347 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
348 return LLVMVectorType(to_float_type_scalar(ctx
, elem_type
),
349 LLVMGetVectorSize(t
));
351 return to_float_type_scalar(ctx
, t
);
354 static LLVMValueRef
to_float(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
)
356 LLVMTypeRef type
= LLVMTypeOf(v
);
357 return LLVMBuildBitCast(ctx
->builder
, v
, to_float_type(ctx
, type
), "");
360 static int get_elem_bits(struct nir_to_llvm_context
*ctx
, LLVMTypeRef type
)
362 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
363 type
= LLVMGetElementType(type
);
365 if (LLVMGetTypeKind(type
) == LLVMIntegerTypeKind
)
366 return LLVMGetIntTypeWidth(type
);
368 if (type
== ctx
->f16
)
370 if (type
== ctx
->f32
)
372 if (type
== ctx
->f64
)
375 unreachable("Unhandled type kind in get_elem_bits");
378 static LLVMValueRef
unpack_param(struct nir_to_llvm_context
*ctx
,
379 LLVMValueRef param
, unsigned rshift
,
382 LLVMValueRef value
= param
;
384 value
= LLVMBuildLShr(ctx
->builder
, value
,
385 LLVMConstInt(ctx
->i32
, rshift
, false), "");
387 if (rshift
+ bitwidth
< 32) {
388 unsigned mask
= (1 << bitwidth
) - 1;
389 value
= LLVMBuildAnd(ctx
->builder
, value
,
390 LLVMConstInt(ctx
->i32
, mask
, false), "");
395 static void set_userdata_location(struct ac_userdata_info
*ud_info
, uint8_t sgpr_idx
, uint8_t num_sgprs
)
397 ud_info
->sgpr_idx
= sgpr_idx
;
398 ud_info
->num_sgprs
= num_sgprs
;
399 ud_info
->indirect
= false;
400 ud_info
->indirect_offset
= 0;
403 static void set_userdata_location_shader(struct nir_to_llvm_context
*ctx
,
404 int idx
, uint8_t sgpr_idx
, uint8_t num_sgprs
)
406 set_userdata_location(&ctx
->shader_info
->user_sgprs_locs
.shader_data
[idx
], sgpr_idx
, num_sgprs
);
410 static void set_userdata_location_indirect(struct ac_userdata_info
*ud_info
, uint8_t sgpr_idx
, uint8_t num_sgprs
,
411 uint32_t indirect_offset
)
413 ud_info
->sgpr_idx
= sgpr_idx
;
414 ud_info
->num_sgprs
= num_sgprs
;
415 ud_info
->indirect
= true;
416 ud_info
->indirect_offset
= indirect_offset
;
420 static void create_function(struct nir_to_llvm_context
*ctx
)
422 LLVMTypeRef arg_types
[23];
423 unsigned arg_idx
= 0;
424 unsigned array_params_mask
= 0;
425 unsigned sgpr_count
= 0, user_sgpr_count
;
427 unsigned num_sets
= ctx
->options
->layout
? ctx
->options
->layout
->num_sets
: 0;
428 unsigned user_sgpr_idx
;
429 bool need_push_constants
;
430 bool need_ring_offsets
= false;
432 /* until we sort out scratch/global buffers always assign ring offsets for gs/vs/es */
433 if (ctx
->stage
== MESA_SHADER_GEOMETRY
||
434 ctx
->stage
== MESA_SHADER_VERTEX
||
435 ctx
->is_gs_copy_shader
)
436 need_ring_offsets
= true;
438 need_push_constants
= true;
439 if (!ctx
->options
->layout
)
440 need_push_constants
= false;
441 else if (!ctx
->options
->layout
->push_constant_size
&&
442 !ctx
->options
->layout
->dynamic_offset_count
)
443 need_push_constants
= false;
445 if (need_ring_offsets
&& !ctx
->options
->supports_spill
) {
446 arg_types
[arg_idx
++] = const_array(ctx
->v16i8
, 8); /* address of rings */
449 /* 1 for each descriptor set */
450 for (unsigned i
= 0; i
< num_sets
; ++i
) {
451 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& (1 << ctx
->stage
)) {
452 array_params_mask
|= (1 << arg_idx
);
453 arg_types
[arg_idx
++] = const_array(ctx
->i8
, 1024 * 1024);
457 if (need_push_constants
) {
458 /* 1 for push constants and dynamic descriptors */
459 array_params_mask
|= (1 << arg_idx
);
460 arg_types
[arg_idx
++] = const_array(ctx
->i8
, 1024 * 1024);
463 switch (ctx
->stage
) {
464 case MESA_SHADER_COMPUTE
:
465 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3); /* grid size */
466 user_sgpr_count
= arg_idx
;
467 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3);
468 arg_types
[arg_idx
++] = ctx
->i32
;
469 sgpr_count
= arg_idx
;
471 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3);
473 case MESA_SHADER_VERTEX
:
474 if (!ctx
->is_gs_copy_shader
) {
475 arg_types
[arg_idx
++] = const_array(ctx
->v16i8
, 16); /* vertex buffers */
476 arg_types
[arg_idx
++] = ctx
->i32
; // base vertex
477 arg_types
[arg_idx
++] = ctx
->i32
; // start instance
478 arg_types
[arg_idx
++] = ctx
->i32
; // draw index
480 user_sgpr_count
= arg_idx
;
481 if (ctx
->options
->key
.vs
.as_es
)
482 arg_types
[arg_idx
++] = ctx
->i32
; //es2gs offset
483 sgpr_count
= arg_idx
;
484 arg_types
[arg_idx
++] = ctx
->i32
; // vertex id
485 if (!ctx
->is_gs_copy_shader
) {
486 arg_types
[arg_idx
++] = ctx
->i32
; // rel auto id
487 arg_types
[arg_idx
++] = ctx
->i32
; // vs prim id
488 arg_types
[arg_idx
++] = ctx
->i32
; // instance id
491 case MESA_SHADER_GEOMETRY
:
492 arg_types
[arg_idx
++] = ctx
->i32
; // gsvs stride
493 arg_types
[arg_idx
++] = ctx
->i32
; // gsvs num entires
494 user_sgpr_count
= arg_idx
;
495 arg_types
[arg_idx
++] = ctx
->i32
; // gs2vs offset
496 arg_types
[arg_idx
++] = ctx
->i32
; // wave id
497 sgpr_count
= arg_idx
;
498 arg_types
[arg_idx
++] = ctx
->i32
; // vtx0
499 arg_types
[arg_idx
++] = ctx
->i32
; // vtx1
500 arg_types
[arg_idx
++] = ctx
->i32
; // prim id
501 arg_types
[arg_idx
++] = ctx
->i32
; // vtx2
502 arg_types
[arg_idx
++] = ctx
->i32
; // vtx3
503 arg_types
[arg_idx
++] = ctx
->i32
; // vtx4
504 arg_types
[arg_idx
++] = ctx
->i32
; // vtx5
505 arg_types
[arg_idx
++] = ctx
->i32
; // GS instance id
507 case MESA_SHADER_FRAGMENT
:
508 arg_types
[arg_idx
++] = const_array(ctx
->f32
, 32); /* sample positions */
509 user_sgpr_count
= arg_idx
;
510 arg_types
[arg_idx
++] = ctx
->i32
; /* prim mask */
511 sgpr_count
= arg_idx
;
512 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp sample */
513 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp center */
514 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp centroid */
515 arg_types
[arg_idx
++] = ctx
->v3i32
; /* persp pull model */
516 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear sample */
517 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear center */
518 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear centroid */
519 arg_types
[arg_idx
++] = ctx
->f32
; /* line stipple tex */
520 arg_types
[arg_idx
++] = ctx
->f32
; /* pos x float */
521 arg_types
[arg_idx
++] = ctx
->f32
; /* pos y float */
522 arg_types
[arg_idx
++] = ctx
->f32
; /* pos z float */
523 arg_types
[arg_idx
++] = ctx
->f32
; /* pos w float */
524 arg_types
[arg_idx
++] = ctx
->i32
; /* front face */
525 arg_types
[arg_idx
++] = ctx
->i32
; /* ancillary */
526 arg_types
[arg_idx
++] = ctx
->i32
; /* sample coverage */
527 arg_types
[arg_idx
++] = ctx
->i32
; /* fixed pt */
530 unreachable("Shader stage not implemented");
533 ctx
->main_function
= create_llvm_function(
534 ctx
->context
, ctx
->module
, ctx
->builder
, NULL
, 0, arg_types
,
535 arg_idx
, array_params_mask
, sgpr_count
, ctx
->options
->unsafe_math
);
536 set_llvm_calling_convention(ctx
->main_function
, ctx
->stage
);
538 ctx
->shader_info
->num_input_sgprs
= 0;
539 ctx
->shader_info
->num_input_vgprs
= 0;
541 ctx
->shader_info
->num_user_sgprs
= ctx
->options
->supports_spill
? 2 : 0;
542 for (i
= 0; i
< user_sgpr_count
; i
++)
543 ctx
->shader_info
->num_user_sgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
545 ctx
->shader_info
->num_input_sgprs
= ctx
->shader_info
->num_user_sgprs
;
546 for (; i
< sgpr_count
; i
++)
547 ctx
->shader_info
->num_input_sgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
549 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
550 for (; i
< arg_idx
; ++i
)
551 ctx
->shader_info
->num_input_vgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
556 if (ctx
->options
->supports_spill
|| need_ring_offsets
) {
557 set_userdata_location_shader(ctx
, AC_UD_SCRATCH_RING_OFFSETS
, user_sgpr_idx
, 2);
559 if (ctx
->options
->supports_spill
) {
560 ctx
->ring_offsets
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.implicit.buffer.ptr",
561 LLVMPointerType(ctx
->i8
, CONST_ADDR_SPACE
),
562 NULL
, 0, AC_FUNC_ATTR_READNONE
);
563 ctx
->ring_offsets
= LLVMBuildBitCast(ctx
->builder
, ctx
->ring_offsets
,
564 const_array(ctx
->v16i8
, 8), "");
566 ctx
->ring_offsets
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
569 for (unsigned i
= 0; i
< num_sets
; ++i
) {
570 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& (1 << ctx
->stage
)) {
571 set_userdata_location(&ctx
->shader_info
->user_sgprs_locs
.descriptor_sets
[i
], user_sgpr_idx
, 2);
573 ctx
->descriptor_sets
[i
] =
574 LLVMGetParam(ctx
->main_function
, arg_idx
++);
576 ctx
->descriptor_sets
[i
] = NULL
;
579 if (need_push_constants
) {
580 ctx
->push_constants
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
581 set_userdata_location_shader(ctx
, AC_UD_PUSH_CONSTANTS
, user_sgpr_idx
, 2);
585 switch (ctx
->stage
) {
586 case MESA_SHADER_COMPUTE
:
587 set_userdata_location_shader(ctx
, AC_UD_CS_GRID_SIZE
, user_sgpr_idx
, 3);
589 ctx
->num_work_groups
=
590 LLVMGetParam(ctx
->main_function
, arg_idx
++);
592 LLVMGetParam(ctx
->main_function
, arg_idx
++);
594 LLVMGetParam(ctx
->main_function
, arg_idx
++);
595 ctx
->local_invocation_ids
=
596 LLVMGetParam(ctx
->main_function
, arg_idx
++);
598 case MESA_SHADER_VERTEX
:
599 if (!ctx
->is_gs_copy_shader
) {
600 set_userdata_location_shader(ctx
, AC_UD_VS_VERTEX_BUFFERS
, user_sgpr_idx
, 2);
602 ctx
->vertex_buffers
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
603 set_userdata_location_shader(ctx
, AC_UD_VS_BASE_VERTEX_START_INSTANCE
, user_sgpr_idx
, 3);
605 ctx
->base_vertex
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
606 ctx
->start_instance
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
607 ctx
->draw_index
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
609 if (ctx
->options
->key
.vs
.as_es
)
610 ctx
->es2gs_offset
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
611 ctx
->vertex_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
612 if (!ctx
->is_gs_copy_shader
) {
613 ctx
->rel_auto_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
614 ctx
->vs_prim_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
615 ctx
->instance_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
618 case MESA_SHADER_GEOMETRY
:
619 set_userdata_location_shader(ctx
, AC_UD_GS_VS_RING_STRIDE_ENTRIES
, user_sgpr_idx
, 2);
621 ctx
->gsvs_ring_stride
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
622 ctx
->gsvs_num_entries
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
623 ctx
->gs2vs_offset
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
624 ctx
->gs_wave_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
625 ctx
->gs_vtx_offset
[0] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
626 ctx
->gs_vtx_offset
[1] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
627 ctx
->gs_prim_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
628 ctx
->gs_vtx_offset
[2] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
629 ctx
->gs_vtx_offset
[3] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
630 ctx
->gs_vtx_offset
[4] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
631 ctx
->gs_vtx_offset
[5] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
632 ctx
->gs_invocation_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
634 case MESA_SHADER_FRAGMENT
:
635 set_userdata_location_shader(ctx
, AC_UD_PS_SAMPLE_POS
, user_sgpr_idx
, 2);
637 ctx
->sample_positions
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
638 ctx
->prim_mask
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
639 ctx
->persp_sample
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
640 ctx
->persp_center
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
641 ctx
->persp_centroid
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
643 ctx
->linear_sample
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
644 ctx
->linear_center
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
645 ctx
->linear_centroid
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
646 arg_idx
++; /* line stipple */
647 ctx
->frag_pos
[0] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
648 ctx
->frag_pos
[1] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
649 ctx
->frag_pos
[2] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
650 ctx
->frag_pos
[3] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
651 ctx
->front_face
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
652 ctx
->ancillary
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
653 ctx
->sample_coverage
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
656 unreachable("Shader stage not implemented");
660 static void setup_types(struct nir_to_llvm_context
*ctx
)
662 LLVMValueRef args
[4];
664 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->context
);
665 ctx
->i1
= LLVMIntTypeInContext(ctx
->context
, 1);
666 ctx
->i8
= LLVMIntTypeInContext(ctx
->context
, 8);
667 ctx
->i16
= LLVMIntTypeInContext(ctx
->context
, 16);
668 ctx
->i32
= LLVMIntTypeInContext(ctx
->context
, 32);
669 ctx
->i64
= LLVMIntTypeInContext(ctx
->context
, 64);
670 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
671 ctx
->v3i32
= LLVMVectorType(ctx
->i32
, 3);
672 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
673 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
674 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
675 ctx
->f16
= LLVMHalfTypeInContext(ctx
->context
);
676 ctx
->f64
= LLVMDoubleTypeInContext(ctx
->context
);
677 ctx
->v2f32
= LLVMVectorType(ctx
->f32
, 2);
678 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
679 ctx
->v16i8
= LLVMVectorType(ctx
->i8
, 16);
681 ctx
->i1false
= LLVMConstInt(ctx
->i1
, 0, false);
682 ctx
->i1true
= LLVMConstInt(ctx
->i1
, 1, false);
683 ctx
->i32zero
= LLVMConstInt(ctx
->i32
, 0, false);
684 ctx
->i32one
= LLVMConstInt(ctx
->i32
, 1, false);
685 ctx
->f32zero
= LLVMConstReal(ctx
->f32
, 0.0);
686 ctx
->f32one
= LLVMConstReal(ctx
->f32
, 1.0);
688 args
[0] = ctx
->f32zero
;
689 args
[1] = ctx
->f32zero
;
690 args
[2] = ctx
->f32zero
;
691 args
[3] = ctx
->f32one
;
692 ctx
->v4f32empty
= LLVMConstVector(args
, 4);
694 ctx
->uniform_md_kind
=
695 LLVMGetMDKindIDInContext(ctx
->context
, "amdgpu.uniform", 14);
696 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
698 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
701 static int get_llvm_num_components(LLVMValueRef value
)
703 LLVMTypeRef type
= LLVMTypeOf(value
);
704 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
705 ? LLVMGetVectorSize(type
)
707 return num_components
;
710 static LLVMValueRef
llvm_extract_elem(struct nir_to_llvm_context
*ctx
,
714 int count
= get_llvm_num_components(value
);
716 assert(index
< count
);
720 return LLVMBuildExtractElement(ctx
->builder
, value
,
721 LLVMConstInt(ctx
->i32
, index
, false), "");
724 static LLVMValueRef
trim_vector(struct nir_to_llvm_context
*ctx
,
725 LLVMValueRef value
, unsigned count
)
727 unsigned num_components
= get_llvm_num_components(value
);
728 if (count
== num_components
)
731 LLVMValueRef masks
[] = {
732 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
733 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false)};
736 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
739 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
740 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
744 build_store_values_extended(struct nir_to_llvm_context
*ctx
,
745 LLVMValueRef
*values
,
746 unsigned value_count
,
747 unsigned value_stride
,
750 LLVMBuilderRef builder
= ctx
->builder
;
753 if (value_count
== 1) {
754 LLVMBuildStore(builder
, vec
, values
[0]);
758 for (i
= 0; i
< value_count
; i
++) {
759 LLVMValueRef ptr
= values
[i
* value_stride
];
760 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
761 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
762 LLVMBuildStore(builder
, value
, ptr
);
766 static LLVMTypeRef
get_def_type(struct nir_to_llvm_context
*ctx
,
769 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->context
, def
->bit_size
);
770 if (def
->num_components
> 1) {
771 type
= LLVMVectorType(type
, def
->num_components
);
776 static LLVMValueRef
get_src(struct nir_to_llvm_context
*ctx
, nir_src src
)
779 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->defs
, src
.ssa
);
780 return (LLVMValueRef
)entry
->data
;
784 static LLVMBasicBlockRef
get_block(struct nir_to_llvm_context
*ctx
,
787 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->defs
, b
);
788 return (LLVMBasicBlockRef
)entry
->data
;
791 static LLVMValueRef
get_alu_src(struct nir_to_llvm_context
*ctx
,
793 unsigned num_components
)
795 LLVMValueRef value
= get_src(ctx
, src
.src
);
796 bool need_swizzle
= false;
799 LLVMTypeRef type
= LLVMTypeOf(value
);
800 unsigned src_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
801 ? LLVMGetVectorSize(type
)
804 for (unsigned i
= 0; i
< num_components
; ++i
) {
805 assert(src
.swizzle
[i
] < src_components
);
806 if (src
.swizzle
[i
] != i
)
810 if (need_swizzle
|| num_components
!= src_components
) {
811 LLVMValueRef masks
[] = {
812 LLVMConstInt(ctx
->i32
, src
.swizzle
[0], false),
813 LLVMConstInt(ctx
->i32
, src
.swizzle
[1], false),
814 LLVMConstInt(ctx
->i32
, src
.swizzle
[2], false),
815 LLVMConstInt(ctx
->i32
, src
.swizzle
[3], false)};
817 if (src_components
> 1 && num_components
== 1) {
818 value
= LLVMBuildExtractElement(ctx
->builder
, value
,
820 } else if (src_components
== 1 && num_components
> 1) {
821 LLVMValueRef values
[] = {value
, value
, value
, value
};
822 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
824 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
825 value
= LLVMBuildShuffleVector(ctx
->builder
, value
, value
,
834 static LLVMValueRef
emit_int_cmp(struct nir_to_llvm_context
*ctx
,
835 LLVMIntPredicate pred
, LLVMValueRef src0
,
838 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
839 return LLVMBuildSelect(ctx
->builder
, result
,
840 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
841 LLVMConstInt(ctx
->i32
, 0, false), "");
844 static LLVMValueRef
emit_float_cmp(struct nir_to_llvm_context
*ctx
,
845 LLVMRealPredicate pred
, LLVMValueRef src0
,
849 src0
= to_float(ctx
, src0
);
850 src1
= to_float(ctx
, src1
);
851 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
852 return LLVMBuildSelect(ctx
->builder
, result
,
853 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
854 LLVMConstInt(ctx
->i32
, 0, false), "");
857 static LLVMValueRef
emit_intrin_1f_param(struct nir_to_llvm_context
*ctx
,
859 LLVMTypeRef result_type
,
863 LLVMValueRef params
[] = {
867 sprintf(name
, "%s.f%d", intrin
, get_elem_bits(ctx
, result_type
));
868 return ac_build_intrinsic(&ctx
->ac
, name
, result_type
, params
, 1, AC_FUNC_ATTR_READNONE
);
871 static LLVMValueRef
emit_intrin_2f_param(struct nir_to_llvm_context
*ctx
,
873 LLVMTypeRef result_type
,
874 LLVMValueRef src0
, LLVMValueRef src1
)
877 LLVMValueRef params
[] = {
882 sprintf(name
, "%s.f%d", intrin
, get_elem_bits(ctx
, result_type
));
883 return ac_build_intrinsic(&ctx
->ac
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
886 static LLVMValueRef
emit_intrin_3f_param(struct nir_to_llvm_context
*ctx
,
888 LLVMTypeRef result_type
,
889 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
892 LLVMValueRef params
[] = {
898 sprintf(name
, "%s.f%d", intrin
, get_elem_bits(ctx
, result_type
));
899 return ac_build_intrinsic(&ctx
->ac
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
902 static LLVMValueRef
emit_bcsel(struct nir_to_llvm_context
*ctx
,
903 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
905 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
907 return LLVMBuildSelect(ctx
->builder
, v
, src1
, src2
, "");
910 static LLVMValueRef
emit_find_lsb(struct nir_to_llvm_context
*ctx
,
913 LLVMValueRef params
[2] = {
916 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
917 * add special code to check for x=0. The reason is that
918 * the LLVM behavior for x=0 is different from what we
921 * The hardware already implements the correct behavior.
923 LLVMConstInt(ctx
->i32
, 1, false),
925 return ac_build_intrinsic(&ctx
->ac
, "llvm.cttz.i32", ctx
->i32
, params
, 2, AC_FUNC_ATTR_READNONE
);
928 static LLVMValueRef
emit_ifind_msb(struct nir_to_llvm_context
*ctx
,
931 return ac_build_imsb(&ctx
->ac
, src0
, ctx
->i32
);
934 static LLVMValueRef
emit_ufind_msb(struct nir_to_llvm_context
*ctx
,
937 return ac_build_umsb(&ctx
->ac
, src0
, ctx
->i32
);
940 static LLVMValueRef
emit_minmax_int(struct nir_to_llvm_context
*ctx
,
941 LLVMIntPredicate pred
,
942 LLVMValueRef src0
, LLVMValueRef src1
)
944 return LLVMBuildSelect(ctx
->builder
,
945 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
950 static LLVMValueRef
emit_iabs(struct nir_to_llvm_context
*ctx
,
953 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
954 LLVMBuildNeg(ctx
->builder
, src0
, ""));
957 static LLVMValueRef
emit_fsign(struct nir_to_llvm_context
*ctx
,
960 LLVMValueRef cmp
, val
;
962 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, ctx
->f32zero
, "");
963 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->f32one
, src0
, "");
964 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, ctx
->f32zero
, "");
965 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(ctx
->f32
, -1.0), "");
969 static LLVMValueRef
emit_isign(struct nir_to_llvm_context
*ctx
,
972 LLVMValueRef cmp
, val
;
974 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, ctx
->i32zero
, "");
975 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->i32one
, src0
, "");
976 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, ctx
->i32zero
, "");
977 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(ctx
->i32
, -1, true), "");
981 static LLVMValueRef
emit_ffract(struct nir_to_llvm_context
*ctx
,
984 const char *intr
= "llvm.floor.f32";
985 LLVMValueRef fsrc0
= to_float(ctx
, src0
);
986 LLVMValueRef params
[] = {
989 LLVMValueRef floor
= ac_build_intrinsic(&ctx
->ac
, intr
,
991 AC_FUNC_ATTR_READNONE
);
992 return LLVMBuildFSub(ctx
->builder
, fsrc0
, floor
, "");
995 static LLVMValueRef
emit_uint_carry(struct nir_to_llvm_context
*ctx
,
997 LLVMValueRef src0
, LLVMValueRef src1
)
999 LLVMTypeRef ret_type
;
1000 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
1002 LLVMValueRef params
[] = { src0
, src1
};
1003 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
1006 res
= ac_build_intrinsic(&ctx
->ac
, intrin
, ret_type
,
1007 params
, 2, AC_FUNC_ATTR_READNONE
);
1009 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
1010 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
1014 static LLVMValueRef
emit_b2f(struct nir_to_llvm_context
*ctx
,
1017 return LLVMBuildAnd(ctx
->builder
, src0
, LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""), "");
1020 static LLVMValueRef
emit_umul_high(struct nir_to_llvm_context
*ctx
,
1021 LLVMValueRef src0
, LLVMValueRef src1
)
1023 LLVMValueRef dst64
, result
;
1024 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
1025 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
1027 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1028 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1029 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1033 static LLVMValueRef
emit_imul_high(struct nir_to_llvm_context
*ctx
,
1034 LLVMValueRef src0
, LLVMValueRef src1
)
1036 LLVMValueRef dst64
, result
;
1037 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
1038 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
1040 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1041 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1042 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1046 static LLVMValueRef
emit_bitfield_extract(struct nir_to_llvm_context
*ctx
,
1048 LLVMValueRef srcs
[3])
1050 LLVMValueRef result
;
1051 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
1053 result
= ac_build_bfe(&ctx
->ac
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
1054 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
1058 static LLVMValueRef
emit_bitfield_insert(struct nir_to_llvm_context
*ctx
,
1059 LLVMValueRef src0
, LLVMValueRef src1
,
1060 LLVMValueRef src2
, LLVMValueRef src3
)
1062 LLVMValueRef bfi_args
[3], result
;
1064 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
1065 LLVMBuildSub(ctx
->builder
,
1066 LLVMBuildShl(ctx
->builder
,
1071 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
1074 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
1077 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
1078 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
1080 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
1081 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
1082 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
1084 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
1088 static LLVMValueRef
emit_pack_half_2x16(struct nir_to_llvm_context
*ctx
,
1091 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1093 LLVMValueRef comp
[2];
1095 src0
= to_float(ctx
, src0
);
1096 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32zero
, "");
1097 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32one
, "");
1098 for (i
= 0; i
< 2; i
++) {
1099 comp
[i
] = LLVMBuildFPTrunc(ctx
->builder
, comp
[i
], ctx
->f16
, "");
1100 comp
[i
] = LLVMBuildBitCast(ctx
->builder
, comp
[i
], ctx
->i16
, "");
1101 comp
[i
] = LLVMBuildZExt(ctx
->builder
, comp
[i
], ctx
->i32
, "");
1104 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
1105 comp
[0] = LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
1110 static LLVMValueRef
emit_unpack_half_2x16(struct nir_to_llvm_context
*ctx
,
1113 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1114 LLVMValueRef temps
[2], result
, val
;
1117 for (i
= 0; i
< 2; i
++) {
1118 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
1119 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
1120 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
1121 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
1124 result
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), temps
[0],
1126 result
= LLVMBuildInsertElement(ctx
->builder
, result
, temps
[1],
1131 static LLVMValueRef
emit_ddxy(struct nir_to_llvm_context
*ctx
,
1137 LLVMValueRef result
;
1138 ctx
->has_ddxy
= true;
1140 if (!ctx
->lds
&& !ctx
->has_ds_bpermute
)
1141 ctx
->lds
= LLVMAddGlobalInAddressSpace(ctx
->module
,
1142 LLVMArrayType(ctx
->i32
, 64),
1143 "ddxy_lds", LOCAL_ADDR_SPACE
);
1145 if (op
== nir_op_fddx_fine
|| op
== nir_op_fddx
)
1146 mask
= AC_TID_MASK_LEFT
;
1147 else if (op
== nir_op_fddy_fine
|| op
== nir_op_fddy
)
1148 mask
= AC_TID_MASK_TOP
;
1150 mask
= AC_TID_MASK_TOP_LEFT
;
1152 /* for DDX we want to next X pixel, DDY next Y pixel. */
1153 if (op
== nir_op_fddx_fine
||
1154 op
== nir_op_fddx_coarse
||
1160 result
= ac_build_ddxy(&ctx
->ac
, ctx
->has_ds_bpermute
,
1161 mask
, idx
, ctx
->lds
,
1167 * this takes an I,J coordinate pair,
1168 * and works out the X and Y derivatives.
1169 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
1171 static LLVMValueRef
emit_ddxy_interp(
1172 struct nir_to_llvm_context
*ctx
,
1173 LLVMValueRef interp_ij
)
1175 LLVMValueRef result
[4], a
;
1178 for (i
= 0; i
< 2; i
++) {
1179 a
= LLVMBuildExtractElement(ctx
->builder
, interp_ij
,
1180 LLVMConstInt(ctx
->i32
, i
, false), "");
1181 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
1182 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
1184 return ac_build_gather_values(&ctx
->ac
, result
, 4);
1187 static void visit_alu(struct nir_to_llvm_context
*ctx
, nir_alu_instr
*instr
)
1189 LLVMValueRef src
[4], result
= NULL
;
1190 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
1191 unsigned src_components
;
1192 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
1194 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
1195 switch (instr
->op
) {
1201 case nir_op_pack_half_2x16
:
1204 case nir_op_unpack_half_2x16
:
1208 src_components
= num_components
;
1211 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1212 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
1214 switch (instr
->op
) {
1220 src
[0] = to_float(ctx
, src
[0]);
1221 result
= LLVMBuildFNeg(ctx
->builder
, src
[0], "");
1224 result
= LLVMBuildNeg(ctx
->builder
, src
[0], "");
1227 result
= LLVMBuildNot(ctx
->builder
, src
[0], "");
1230 result
= LLVMBuildAdd(ctx
->builder
, src
[0], src
[1], "");
1233 src
[0] = to_float(ctx
, src
[0]);
1234 src
[1] = to_float(ctx
, src
[1]);
1235 result
= LLVMBuildFAdd(ctx
->builder
, src
[0], src
[1], "");
1238 src
[0] = to_float(ctx
, src
[0]);
1239 src
[1] = to_float(ctx
, src
[1]);
1240 result
= LLVMBuildFSub(ctx
->builder
, src
[0], src
[1], "");
1243 result
= LLVMBuildSub(ctx
->builder
, src
[0], src
[1], "");
1246 result
= LLVMBuildMul(ctx
->builder
, src
[0], src
[1], "");
1249 result
= LLVMBuildSRem(ctx
->builder
, src
[0], src
[1], "");
1252 result
= LLVMBuildURem(ctx
->builder
, src
[0], src
[1], "");
1255 src
[0] = to_float(ctx
, src
[0]);
1256 src
[1] = to_float(ctx
, src
[1]);
1257 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1258 result
= emit_intrin_1f_param(ctx
, "llvm.floor",
1259 to_float_type(ctx
, def_type
), result
);
1260 result
= LLVMBuildFMul(ctx
->builder
, src
[1] , result
, "");
1261 result
= LLVMBuildFSub(ctx
->builder
, src
[0], result
, "");
1264 src
[0] = to_float(ctx
, src
[0]);
1265 src
[1] = to_float(ctx
, src
[1]);
1266 result
= LLVMBuildFRem(ctx
->builder
, src
[0], src
[1], "");
1269 result
= LLVMBuildSRem(ctx
->builder
, src
[0], src
[1], "");
1272 result
= LLVMBuildSDiv(ctx
->builder
, src
[0], src
[1], "");
1275 result
= LLVMBuildUDiv(ctx
->builder
, src
[0], src
[1], "");
1278 src
[0] = to_float(ctx
, src
[0]);
1279 src
[1] = to_float(ctx
, src
[1]);
1280 result
= LLVMBuildFMul(ctx
->builder
, src
[0], src
[1], "");
1283 src
[0] = to_float(ctx
, src
[0]);
1284 src
[1] = to_float(ctx
, src
[1]);
1285 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1288 src
[0] = to_float(ctx
, src
[0]);
1289 result
= ac_build_fdiv(&ctx
->ac
, ctx
->f32one
, src
[0]);
1292 result
= LLVMBuildAnd(ctx
->builder
, src
[0], src
[1], "");
1295 result
= LLVMBuildOr(ctx
->builder
, src
[0], src
[1], "");
1298 result
= LLVMBuildXor(ctx
->builder
, src
[0], src
[1], "");
1301 result
= LLVMBuildShl(ctx
->builder
, src
[0], src
[1], "");
1304 result
= LLVMBuildAShr(ctx
->builder
, src
[0], src
[1], "");
1307 result
= LLVMBuildLShr(ctx
->builder
, src
[0], src
[1], "");
1310 result
= emit_int_cmp(ctx
, LLVMIntSLT
, src
[0], src
[1]);
1313 result
= emit_int_cmp(ctx
, LLVMIntNE
, src
[0], src
[1]);
1316 result
= emit_int_cmp(ctx
, LLVMIntEQ
, src
[0], src
[1]);
1319 result
= emit_int_cmp(ctx
, LLVMIntSGE
, src
[0], src
[1]);
1322 result
= emit_int_cmp(ctx
, LLVMIntULT
, src
[0], src
[1]);
1325 result
= emit_int_cmp(ctx
, LLVMIntUGE
, src
[0], src
[1]);
1328 result
= emit_float_cmp(ctx
, LLVMRealUEQ
, src
[0], src
[1]);
1331 result
= emit_float_cmp(ctx
, LLVMRealUNE
, src
[0], src
[1]);
1334 result
= emit_float_cmp(ctx
, LLVMRealULT
, src
[0], src
[1]);
1337 result
= emit_float_cmp(ctx
, LLVMRealUGE
, src
[0], src
[1]);
1340 result
= emit_intrin_1f_param(ctx
, "llvm.fabs",
1341 to_float_type(ctx
, def_type
), src
[0]);
1344 result
= emit_iabs(ctx
, src
[0]);
1347 result
= emit_minmax_int(ctx
, LLVMIntSGT
, src
[0], src
[1]);
1350 result
= emit_minmax_int(ctx
, LLVMIntSLT
, src
[0], src
[1]);
1353 result
= emit_minmax_int(ctx
, LLVMIntUGT
, src
[0], src
[1]);
1356 result
= emit_minmax_int(ctx
, LLVMIntULT
, src
[0], src
[1]);
1359 result
= emit_isign(ctx
, src
[0]);
1362 src
[0] = to_float(ctx
, src
[0]);
1363 result
= emit_fsign(ctx
, src
[0]);
1366 result
= emit_intrin_1f_param(ctx
, "llvm.floor",
1367 to_float_type(ctx
, def_type
), src
[0]);
1370 result
= emit_intrin_1f_param(ctx
, "llvm.trunc",
1371 to_float_type(ctx
, def_type
), src
[0]);
1374 result
= emit_intrin_1f_param(ctx
, "llvm.ceil",
1375 to_float_type(ctx
, def_type
), src
[0]);
1377 case nir_op_fround_even
:
1378 result
= emit_intrin_1f_param(ctx
, "llvm.rint",
1379 to_float_type(ctx
, def_type
),src
[0]);
1382 result
= emit_ffract(ctx
, src
[0]);
1385 result
= emit_intrin_1f_param(ctx
, "llvm.sin",
1386 to_float_type(ctx
, def_type
), src
[0]);
1389 result
= emit_intrin_1f_param(ctx
, "llvm.cos",
1390 to_float_type(ctx
, def_type
), src
[0]);
1393 result
= emit_intrin_1f_param(ctx
, "llvm.sqrt",
1394 to_float_type(ctx
, def_type
), src
[0]);
1397 result
= emit_intrin_1f_param(ctx
, "llvm.exp2",
1398 to_float_type(ctx
, def_type
), src
[0]);
1401 result
= emit_intrin_1f_param(ctx
, "llvm.log2",
1402 to_float_type(ctx
, def_type
), src
[0]);
1405 result
= emit_intrin_1f_param(ctx
, "llvm.sqrt",
1406 to_float_type(ctx
, def_type
), src
[0]);
1407 result
= ac_build_fdiv(&ctx
->ac
, ctx
->f32one
, result
);
1410 result
= emit_intrin_2f_param(ctx
, "llvm.pow",
1411 to_float_type(ctx
, def_type
), src
[0], src
[1]);
1414 result
= emit_intrin_2f_param(ctx
, "llvm.maxnum",
1415 to_float_type(ctx
, def_type
), src
[0], src
[1]);
1418 result
= emit_intrin_2f_param(ctx
, "llvm.minnum",
1419 to_float_type(ctx
, def_type
), src
[0], src
[1]);
1422 result
= emit_intrin_3f_param(ctx
, "llvm.fma",
1423 to_float_type(ctx
, def_type
), src
[0], src
[1], src
[2]);
1425 case nir_op_ibitfield_extract
:
1426 result
= emit_bitfield_extract(ctx
, true, src
);
1428 case nir_op_ubitfield_extract
:
1429 result
= emit_bitfield_extract(ctx
, false, src
);
1431 case nir_op_bitfield_insert
:
1432 result
= emit_bitfield_insert(ctx
, src
[0], src
[1], src
[2], src
[3]);
1434 case nir_op_bitfield_reverse
:
1435 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.bitreverse.i32", ctx
->i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1437 case nir_op_bit_count
:
1438 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i32", ctx
->i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1443 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1444 src
[i
] = to_integer(ctx
, src
[i
]);
1445 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
1449 src
[0] = to_float(ctx
, src
[0]);
1450 result
= LLVMBuildFPToSI(ctx
->builder
, src
[0], def_type
, "");
1454 src
[0] = to_float(ctx
, src
[0]);
1455 result
= LLVMBuildFPToUI(ctx
->builder
, src
[0], def_type
, "");
1459 result
= LLVMBuildSIToFP(ctx
->builder
, src
[0], to_float_type(ctx
, def_type
), "");
1463 result
= LLVMBuildUIToFP(ctx
->builder
, src
[0], to_float_type(ctx
, def_type
), "");
1466 result
= LLVMBuildFPExt(ctx
->builder
, src
[0], to_float_type(ctx
, def_type
), "");
1469 result
= LLVMBuildFPTrunc(ctx
->builder
, src
[0], to_float_type(ctx
, def_type
), "");
1473 if (get_elem_bits(ctx
, LLVMTypeOf(src
[0])) < get_elem_bits(ctx
, def_type
))
1474 result
= LLVMBuildZExt(ctx
->builder
, src
[0], def_type
, "");
1476 result
= LLVMBuildTrunc(ctx
->builder
, src
[0], def_type
, "");
1480 if (get_elem_bits(ctx
, LLVMTypeOf(src
[0])) < get_elem_bits(ctx
, def_type
))
1481 result
= LLVMBuildSExt(ctx
->builder
, src
[0], def_type
, "");
1483 result
= LLVMBuildTrunc(ctx
->builder
, src
[0], def_type
, "");
1486 result
= emit_bcsel(ctx
, src
[0], src
[1], src
[2]);
1488 case nir_op_find_lsb
:
1489 result
= emit_find_lsb(ctx
, src
[0]);
1491 case nir_op_ufind_msb
:
1492 result
= emit_ufind_msb(ctx
, src
[0]);
1494 case nir_op_ifind_msb
:
1495 result
= emit_ifind_msb(ctx
, src
[0]);
1497 case nir_op_uadd_carry
:
1498 result
= emit_uint_carry(ctx
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
1500 case nir_op_usub_borrow
:
1501 result
= emit_uint_carry(ctx
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
1504 result
= emit_b2f(ctx
, src
[0]);
1506 case nir_op_fquantize2f16
:
1507 src
[0] = to_float(ctx
, src
[0]);
1508 result
= LLVMBuildFPTrunc(ctx
->builder
, src
[0], ctx
->f16
, "");
1509 /* need to convert back up to f32 */
1510 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
1512 case nir_op_umul_high
:
1513 result
= emit_umul_high(ctx
, src
[0], src
[1]);
1515 case nir_op_imul_high
:
1516 result
= emit_imul_high(ctx
, src
[0], src
[1]);
1518 case nir_op_pack_half_2x16
:
1519 result
= emit_pack_half_2x16(ctx
, src
[0]);
1521 case nir_op_unpack_half_2x16
:
1522 result
= emit_unpack_half_2x16(ctx
, src
[0]);
1526 case nir_op_fddx_fine
:
1527 case nir_op_fddy_fine
:
1528 case nir_op_fddx_coarse
:
1529 case nir_op_fddy_coarse
:
1530 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
1533 fprintf(stderr
, "Unknown NIR alu instr: ");
1534 nir_print_instr(&instr
->instr
, stderr
);
1535 fprintf(stderr
, "\n");
1540 assert(instr
->dest
.dest
.is_ssa
);
1541 result
= to_integer(ctx
, result
);
1542 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.dest
.ssa
,
1547 static void visit_load_const(struct nir_to_llvm_context
*ctx
,
1548 nir_load_const_instr
*instr
)
1550 LLVMValueRef values
[4], value
= NULL
;
1551 LLVMTypeRef element_type
=
1552 LLVMIntTypeInContext(ctx
->context
, instr
->def
.bit_size
);
1554 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1555 switch (instr
->def
.bit_size
) {
1557 values
[i
] = LLVMConstInt(element_type
,
1558 instr
->value
.u32
[i
], false);
1561 values
[i
] = LLVMConstInt(element_type
,
1562 instr
->value
.u64
[i
], false);
1566 "unsupported nir load_const bit_size: %d\n",
1567 instr
->def
.bit_size
);
1571 if (instr
->def
.num_components
> 1) {
1572 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1576 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, value
);
1579 static LLVMValueRef
cast_ptr(struct nir_to_llvm_context
*ctx
, LLVMValueRef ptr
,
1582 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
1583 return LLVMBuildBitCast(ctx
->builder
, ptr
,
1584 LLVMPointerType(type
, addr_space
), "");
1588 get_buffer_size(struct nir_to_llvm_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1591 LLVMBuildExtractElement(ctx
->builder
, descriptor
,
1592 LLVMConstInt(ctx
->i32
, 2, false), "");
1595 if (ctx
->options
->chip_class
>= VI
&& in_elements
) {
1596 /* On VI, the descriptor contains the size in bytes,
1597 * but TXQ must return the size in elements.
1598 * The stride is always non-zero for resources using TXQ.
1600 LLVMValueRef stride
=
1601 LLVMBuildExtractElement(ctx
->builder
, descriptor
,
1602 LLVMConstInt(ctx
->i32
, 1, false), "");
1603 stride
= LLVMBuildLShr(ctx
->builder
, stride
,
1604 LLVMConstInt(ctx
->i32
, 16, false), "");
1605 stride
= LLVMBuildAnd(ctx
->builder
, stride
,
1606 LLVMConstInt(ctx
->i32
, 0x3fff, false), "");
1608 size
= LLVMBuildUDiv(ctx
->builder
, size
, stride
, "");
1614 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
1617 static void build_int_type_name(
1619 char *buf
, unsigned bufsize
)
1621 assert(bufsize
>= 6);
1623 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
1624 snprintf(buf
, bufsize
, "v%ui32",
1625 LLVMGetVectorSize(type
));
1630 static LLVMValueRef
radv_lower_gather4_integer(struct nir_to_llvm_context
*ctx
,
1631 struct ac_image_args
*args
,
1632 nir_tex_instr
*instr
)
1634 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
1635 LLVMValueRef coord
= args
->addr
;
1636 LLVMValueRef half_texel
[2];
1637 LLVMValueRef compare_cube_wa
;
1638 LLVMValueRef result
;
1640 unsigned coord_vgpr_index
= (unsigned)args
->offset
+ (unsigned)args
->compare
;
1644 struct ac_image_args txq_args
= { 0 };
1646 txq_args
.da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
1647 txq_args
.opcode
= ac_image_get_resinfo
;
1648 txq_args
.dmask
= 0xf;
1649 txq_args
.addr
= ctx
->i32zero
;
1650 txq_args
.resource
= args
->resource
;
1651 LLVMValueRef size
= ac_build_image_opcode(&ctx
->ac
, &txq_args
);
1653 for (c
= 0; c
< 2; c
++) {
1654 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
1655 LLVMConstInt(ctx
->i32
, c
, false), "");
1656 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1657 half_texel
[c
] = ac_build_fdiv(&ctx
->ac
, ctx
->f32one
, half_texel
[c
]);
1658 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1659 LLVMConstReal(ctx
->f32
, -0.5), "");
1663 LLVMValueRef orig_coords
= args
->addr
;
1665 for (c
= 0; c
< 2; c
++) {
1667 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, coord_vgpr_index
+ c
, 0);
1668 tmp
= LLVMBuildExtractElement(ctx
->builder
, coord
, index
, "");
1669 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1670 tmp
= LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
1671 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1672 coord
= LLVMBuildInsertElement(ctx
->builder
, coord
, tmp
, index
, "");
1677 * Apparantly cube has issue with integer types that the workaround doesn't solve,
1678 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
1679 * workaround by sampling using a scaled type and converting.
1680 * This is taken from amdgpu-pro shaders.
1682 /* NOTE this produces some ugly code compared to amdgpu-pro,
1683 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
1684 * and then reads them back. -pro generates two selects,
1685 * one s_cmp for the descriptor rewriting
1686 * one v_cmp for the coordinate and result changes.
1688 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1689 LLVMValueRef tmp
, tmp2
;
1691 /* workaround 8/8/8/8 uint/sint cube gather bug */
1692 /* first detect it then change to a scaled read and f2i */
1693 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32one
, "");
1696 /* extract the DATA_FORMAT */
1697 tmp
= ac_build_bfe(&ctx
->ac
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
1698 LLVMConstInt(ctx
->i32
, 6, false), false);
1700 /* is the DATA_FORMAT == 8_8_8_8 */
1701 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
1703 if (stype
== GLSL_TYPE_UINT
)
1704 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
1705 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
1706 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
1708 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
1709 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
1710 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
1712 /* replace the NUM FORMAT in the descriptor */
1713 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT_GFX6
, false), "");
1714 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
1716 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32one
, "");
1718 /* don't modify the coordinates for this case */
1719 coord
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, orig_coords
, coord
, "");
1722 result
= ac_build_image_opcode(&ctx
->ac
, args
);
1724 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1725 LLVMValueRef tmp
, tmp2
;
1727 /* if the cube workaround is in place, f2i the result. */
1728 for (c
= 0; c
< 4; c
++) {
1729 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
1730 if (stype
== GLSL_TYPE_UINT
)
1731 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
1733 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
1734 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1735 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
1736 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
1737 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1738 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
1744 static LLVMValueRef
build_tex_intrinsic(struct nir_to_llvm_context
*ctx
,
1745 nir_tex_instr
*instr
,
1746 struct ac_image_args
*args
)
1748 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
1749 return ac_build_buffer_load_format(&ctx
->ac
,
1752 LLVMConstInt(ctx
->i32
, 0, false),
1756 args
->opcode
= ac_image_sample
;
1757 args
->compare
= instr
->is_shadow
;
1759 switch (instr
->op
) {
1761 case nir_texop_txf_ms
:
1762 case nir_texop_samples_identical
:
1763 args
->opcode
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
? ac_image_load
: ac_image_load_mip
;
1764 args
->compare
= false;
1765 args
->offset
= false;
1774 case nir_texop_query_levels
:
1775 args
->opcode
= ac_image_get_resinfo
;
1778 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
1779 args
->level_zero
= true;
1785 args
->opcode
= ac_image_gather4
;
1786 args
->level_zero
= true;
1789 args
->opcode
= ac_image_get_lod
;
1790 args
->compare
= false;
1791 args
->offset
= false;
1797 if (instr
->op
== nir_texop_tg4
) {
1798 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
1799 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
1800 return radv_lower_gather4_integer(ctx
, args
, instr
);
1803 return ac_build_image_opcode(&ctx
->ac
, args
);
1806 static LLVMValueRef
visit_vulkan_resource_index(struct nir_to_llvm_context
*ctx
,
1807 nir_intrinsic_instr
*instr
)
1809 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
1810 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
1811 unsigned binding
= nir_intrinsic_binding(instr
);
1812 LLVMValueRef desc_ptr
= ctx
->descriptor_sets
[desc_set
];
1813 struct radv_pipeline_layout
*pipeline_layout
= ctx
->options
->layout
;
1814 struct radv_descriptor_set_layout
*layout
= pipeline_layout
->set
[desc_set
].layout
;
1815 unsigned base_offset
= layout
->binding
[binding
].offset
;
1816 LLVMValueRef offset
, stride
;
1818 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
1819 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
1820 unsigned idx
= pipeline_layout
->set
[desc_set
].dynamic_offset_start
+
1821 layout
->binding
[binding
].dynamic_offset_offset
;
1822 desc_ptr
= ctx
->push_constants
;
1823 base_offset
= pipeline_layout
->push_constant_size
+ 16 * idx
;
1824 stride
= LLVMConstInt(ctx
->i32
, 16, false);
1826 stride
= LLVMConstInt(ctx
->i32
, layout
->binding
[binding
].size
, false);
1828 offset
= LLVMConstInt(ctx
->i32
, base_offset
, false);
1829 index
= LLVMBuildMul(ctx
->builder
, index
, stride
, "");
1830 offset
= LLVMBuildAdd(ctx
->builder
, offset
, index
, "");
1832 desc_ptr
= ac_build_gep0(&ctx
->ac
, desc_ptr
, offset
);
1833 desc_ptr
= cast_ptr(ctx
, desc_ptr
, ctx
->v4i32
);
1834 LLVMSetMetadata(desc_ptr
, ctx
->uniform_md_kind
, ctx
->empty_md
);
1836 return LLVMBuildLoad(ctx
->builder
, desc_ptr
, "");
1839 static LLVMValueRef
visit_load_push_constant(struct nir_to_llvm_context
*ctx
,
1840 nir_intrinsic_instr
*instr
)
1842 LLVMValueRef ptr
, addr
;
1844 addr
= LLVMConstInt(ctx
->i32
, nir_intrinsic_base(instr
), 0);
1845 addr
= LLVMBuildAdd(ctx
->builder
, addr
, get_src(ctx
, instr
->src
[0]), "");
1847 ptr
= ac_build_gep0(&ctx
->ac
, ctx
->push_constants
, addr
);
1848 ptr
= cast_ptr(ctx
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
1850 return LLVMBuildLoad(ctx
->builder
, ptr
, "");
1853 static LLVMValueRef
visit_get_buffer_size(struct nir_to_llvm_context
*ctx
,
1854 nir_intrinsic_instr
*instr
)
1856 LLVMValueRef desc
= get_src(ctx
, instr
->src
[0]);
1858 return get_buffer_size(ctx
, desc
, false);
1860 static void visit_store_ssbo(struct nir_to_llvm_context
*ctx
,
1861 nir_intrinsic_instr
*instr
)
1863 const char *store_name
;
1864 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
1865 LLVMTypeRef data_type
= ctx
->f32
;
1866 int elem_size_mult
= get_elem_bits(ctx
, LLVMTypeOf(src_data
)) / 32;
1867 int components_32bit
= elem_size_mult
* instr
->num_components
;
1868 unsigned writemask
= nir_intrinsic_write_mask(instr
);
1869 LLVMValueRef base_data
, base_offset
;
1870 LLVMValueRef params
[6];
1872 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
1873 ctx
->shader_info
->fs
.writes_memory
= true;
1875 params
[1] = get_src(ctx
, instr
->src
[1]);
1876 params
[2] = LLVMConstInt(ctx
->i32
, 0, false); /* vindex */
1877 params
[4] = ctx
->i1false
; /* glc */
1878 params
[5] = ctx
->i1false
; /* slc */
1880 if (components_32bit
> 1)
1881 data_type
= LLVMVectorType(ctx
->f32
, components_32bit
);
1883 base_data
= to_float(ctx
, src_data
);
1884 base_data
= trim_vector(ctx
, base_data
, instr
->num_components
);
1885 base_data
= LLVMBuildBitCast(ctx
->builder
, base_data
,
1887 base_offset
= get_src(ctx
, instr
->src
[2]); /* voffset */
1891 LLVMValueRef offset
;
1893 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
1895 /* Due to an LLVM limitation, split 3-element writes
1896 * into a 2-element and a 1-element write. */
1898 writemask
|= 1 << (start
+ 2);
1902 start
*= elem_size_mult
;
1903 count
*= elem_size_mult
;
1906 writemask
|= ((1u << (count
- 4)) - 1u) << (start
+ 4);
1911 store_name
= "llvm.amdgcn.buffer.store.v4f32";
1913 } else if (count
== 2) {
1914 tmp
= LLVMBuildExtractElement(ctx
->builder
,
1915 base_data
, LLVMConstInt(ctx
->i32
, start
, false), "");
1916 data
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), tmp
,
1919 tmp
= LLVMBuildExtractElement(ctx
->builder
,
1920 base_data
, LLVMConstInt(ctx
->i32
, start
+ 1, false), "");
1921 data
= LLVMBuildInsertElement(ctx
->builder
, data
, tmp
,
1923 store_name
= "llvm.amdgcn.buffer.store.v2f32";
1927 if (get_llvm_num_components(base_data
) > 1)
1928 data
= LLVMBuildExtractElement(ctx
->builder
, base_data
,
1929 LLVMConstInt(ctx
->i32
, start
, false), "");
1932 store_name
= "llvm.amdgcn.buffer.store.f32";
1935 offset
= base_offset
;
1937 offset
= LLVMBuildAdd(ctx
->builder
, offset
, LLVMConstInt(ctx
->i32
, start
* 4, false), "");
1941 ac_build_intrinsic(&ctx
->ac
, store_name
,
1942 ctx
->voidt
, params
, 6, 0);
1946 static LLVMValueRef
visit_atomic_ssbo(struct nir_to_llvm_context
*ctx
,
1947 nir_intrinsic_instr
*instr
)
1950 LLVMValueRef params
[6];
1952 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
1953 ctx
->shader_info
->fs
.writes_memory
= true;
1955 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
1956 params
[arg_count
++] = llvm_extract_elem(ctx
, get_src(ctx
, instr
->src
[3]), 0);
1958 params
[arg_count
++] = llvm_extract_elem(ctx
, get_src(ctx
, instr
->src
[2]), 0);
1959 params
[arg_count
++] = get_src(ctx
, instr
->src
[0]);
1960 params
[arg_count
++] = LLVMConstInt(ctx
->i32
, 0, false); /* vindex */
1961 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1962 params
[arg_count
++] = ctx
->i1false
; /* slc */
1964 switch (instr
->intrinsic
) {
1965 case nir_intrinsic_ssbo_atomic_add
:
1966 name
= "llvm.amdgcn.buffer.atomic.add";
1968 case nir_intrinsic_ssbo_atomic_imin
:
1969 name
= "llvm.amdgcn.buffer.atomic.smin";
1971 case nir_intrinsic_ssbo_atomic_umin
:
1972 name
= "llvm.amdgcn.buffer.atomic.umin";
1974 case nir_intrinsic_ssbo_atomic_imax
:
1975 name
= "llvm.amdgcn.buffer.atomic.smax";
1977 case nir_intrinsic_ssbo_atomic_umax
:
1978 name
= "llvm.amdgcn.buffer.atomic.umax";
1980 case nir_intrinsic_ssbo_atomic_and
:
1981 name
= "llvm.amdgcn.buffer.atomic.and";
1983 case nir_intrinsic_ssbo_atomic_or
:
1984 name
= "llvm.amdgcn.buffer.atomic.or";
1986 case nir_intrinsic_ssbo_atomic_xor
:
1987 name
= "llvm.amdgcn.buffer.atomic.xor";
1989 case nir_intrinsic_ssbo_atomic_exchange
:
1990 name
= "llvm.amdgcn.buffer.atomic.swap";
1992 case nir_intrinsic_ssbo_atomic_comp_swap
:
1993 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
1999 return ac_build_intrinsic(&ctx
->ac
, name
, ctx
->i32
, params
, arg_count
, 0);
2002 static LLVMValueRef
visit_load_buffer(struct nir_to_llvm_context
*ctx
,
2003 nir_intrinsic_instr
*instr
)
2005 LLVMValueRef results
[2];
2006 int load_components
;
2007 int num_components
= instr
->num_components
;
2008 if (instr
->dest
.ssa
.bit_size
== 64)
2009 num_components
*= 2;
2011 for (int i
= 0; i
< num_components
; i
+= load_components
) {
2012 load_components
= MIN2(num_components
- i
, 4);
2013 const char *load_name
;
2014 LLVMTypeRef data_type
= ctx
->f32
;
2015 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, i
* 4, false);
2016 offset
= LLVMBuildAdd(ctx
->builder
, get_src(ctx
, instr
->src
[1]), offset
, "");
2018 if (load_components
== 3)
2019 data_type
= LLVMVectorType(ctx
->f32
, 4);
2020 else if (load_components
> 1)
2021 data_type
= LLVMVectorType(ctx
->f32
, load_components
);
2023 if (load_components
>= 3)
2024 load_name
= "llvm.amdgcn.buffer.load.v4f32";
2025 else if (load_components
== 2)
2026 load_name
= "llvm.amdgcn.buffer.load.v2f32";
2027 else if (load_components
== 1)
2028 load_name
= "llvm.amdgcn.buffer.load.f32";
2030 unreachable("unhandled number of components");
2032 LLVMValueRef params
[] = {
2033 get_src(ctx
, instr
->src
[0]),
2034 LLVMConstInt(ctx
->i32
, 0, false),
2040 results
[i
] = ac_build_intrinsic(&ctx
->ac
, load_name
, data_type
, params
, 5, 0);
2044 LLVMValueRef ret
= results
[0];
2045 if (num_components
> 4 || num_components
== 3) {
2046 LLVMValueRef masks
[] = {
2047 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
2048 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false),
2049 LLVMConstInt(ctx
->i32
, 4, false), LLVMConstInt(ctx
->i32
, 5, false),
2050 LLVMConstInt(ctx
->i32
, 6, false), LLVMConstInt(ctx
->i32
, 7, false)
2053 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
2054 ret
= LLVMBuildShuffleVector(ctx
->builder
, results
[0],
2055 results
[num_components
> 4 ? 1 : 0], swizzle
, "");
2058 return LLVMBuildBitCast(ctx
->builder
, ret
,
2059 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2062 static LLVMValueRef
visit_load_ubo_buffer(struct nir_to_llvm_context
*ctx
,
2063 nir_intrinsic_instr
*instr
)
2065 LLVMValueRef results
[8], ret
;
2066 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
2067 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
2068 int num_components
= instr
->num_components
;
2070 rsrc
= LLVMBuildBitCast(ctx
->builder
, rsrc
, LLVMVectorType(ctx
->i8
, 16), "");
2072 if (instr
->dest
.ssa
.bit_size
== 64)
2073 num_components
*= 2;
2075 for (unsigned i
= 0; i
< num_components
; ++i
) {
2076 LLVMValueRef params
[] = {
2078 LLVMBuildAdd(ctx
->builder
, LLVMConstInt(ctx
->i32
, 4 * i
, 0),
2081 results
[i
] = ac_build_intrinsic(&ctx
->ac
, "llvm.SI.load.const", ctx
->f32
,
2083 AC_FUNC_ATTR_READNONE
|
2084 AC_FUNC_ATTR_LEGACY
);
2088 ret
= ac_build_gather_values(&ctx
->ac
, results
, instr
->num_components
);
2089 return LLVMBuildBitCast(ctx
->builder
, ret
,
2090 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2094 radv_get_deref_offset(struct nir_to_llvm_context
*ctx
, nir_deref
*tail
,
2095 bool vs_in
, unsigned *vertex_index_out
,
2096 unsigned *const_out
, LLVMValueRef
*indir_out
)
2098 unsigned const_offset
= 0;
2099 LLVMValueRef offset
= NULL
;
2101 if (vertex_index_out
!= NULL
) {
2103 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2104 *vertex_index_out
= deref_array
->base_offset
;
2107 while (tail
->child
!= NULL
) {
2108 const struct glsl_type
*parent_type
= tail
->type
;
2111 if (tail
->deref_type
== nir_deref_type_array
) {
2112 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2113 LLVMValueRef index
, stride
, local_offset
;
2114 unsigned size
= glsl_count_attribute_slots(tail
->type
, vs_in
);
2116 const_offset
+= size
* deref_array
->base_offset
;
2117 if (deref_array
->deref_array_type
== nir_deref_array_type_direct
)
2120 assert(deref_array
->deref_array_type
== nir_deref_array_type_indirect
);
2121 index
= get_src(ctx
, deref_array
->indirect
);
2122 stride
= LLVMConstInt(ctx
->i32
, size
, 0);
2123 local_offset
= LLVMBuildMul(ctx
->builder
, stride
, index
, "");
2126 offset
= LLVMBuildAdd(ctx
->builder
, offset
, local_offset
, "");
2128 offset
= local_offset
;
2129 } else if (tail
->deref_type
== nir_deref_type_struct
) {
2130 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
2132 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
2133 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
2134 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
2137 unreachable("unsupported deref type");
2141 if (const_offset
&& offset
)
2142 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
2143 LLVMConstInt(ctx
->i32
, const_offset
, 0),
2146 *const_out
= const_offset
;
2147 *indir_out
= offset
;
2151 load_gs_input(struct nir_to_llvm_context
*ctx
,
2152 nir_intrinsic_instr
*instr
)
2154 LLVMValueRef indir_index
, vtx_offset
;
2155 unsigned const_index
;
2156 LLVMValueRef args
[9];
2157 unsigned param
, vtx_offset_param
;
2158 LLVMValueRef value
[4], result
;
2159 unsigned vertex_index
;
2160 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
,
2161 false, &vertex_index
,
2162 &const_index
, &indir_index
);
2163 vtx_offset_param
= vertex_index
;
2164 assert(vtx_offset_param
< 6);
2165 vtx_offset
= LLVMBuildMul(ctx
->builder
, ctx
->gs_vtx_offset
[vtx_offset_param
],
2166 LLVMConstInt(ctx
->i32
, 4, false), "");
2168 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2169 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
2171 args
[0] = ctx
->esgs_ring
;
2172 args
[1] = vtx_offset
;
2173 args
[2] = LLVMConstInt(ctx
->i32
, (param
* 4 + i
+ const_index
) * 256, false);
2174 args
[3] = ctx
->i32zero
;
2175 args
[4] = ctx
->i32one
; /* OFFEN */
2176 args
[5] = ctx
->i32zero
; /* IDXEN */
2177 args
[6] = ctx
->i32one
; /* GLC */
2178 args
[7] = ctx
->i32zero
; /* SLC */
2179 args
[8] = ctx
->i32zero
; /* TFE */
2181 value
[i
] = ac_build_intrinsic(&ctx
->ac
, "llvm.SI.buffer.load.dword.i32.i32",
2183 AC_FUNC_ATTR_READONLY
|
2184 AC_FUNC_ATTR_LEGACY
);
2186 result
= ac_build_gather_values(&ctx
->ac
, value
, instr
->num_components
);
2191 static LLVMValueRef
visit_load_var(struct nir_to_llvm_context
*ctx
,
2192 nir_intrinsic_instr
*instr
)
2194 LLVMValueRef values
[8];
2195 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2196 int ve
= instr
->dest
.ssa
.num_components
;
2197 LLVMValueRef indir_index
;
2199 unsigned const_index
;
2200 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
2201 instr
->variables
[0]->var
->data
.mode
== nir_var_shader_in
;
2202 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, vs_in
, NULL
,
2203 &const_index
, &indir_index
);
2205 if (instr
->dest
.ssa
.bit_size
== 64)
2208 switch (instr
->variables
[0]->var
->data
.mode
) {
2209 case nir_var_shader_in
:
2210 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
2211 return load_gs_input(ctx
, instr
);
2213 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2215 unsigned count
= glsl_count_attribute_slots(
2216 instr
->variables
[0]->var
->type
,
2217 ctx
->stage
== MESA_SHADER_VERTEX
);
2219 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2220 &ctx
->ac
, ctx
->inputs
+ idx
+ chan
, count
,
2223 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2227 values
[chan
] = ctx
->inputs
[idx
+ chan
+ const_index
* 4];
2231 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2233 unsigned count
= glsl_count_attribute_slots(
2234 instr
->variables
[0]->var
->type
, false);
2236 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2237 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2240 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2244 values
[chan
] = LLVMBuildLoad(ctx
->builder
, ctx
->locals
[idx
+ chan
+ const_index
* 4], "");
2248 case nir_var_shader_out
:
2249 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2251 unsigned count
= glsl_count_attribute_slots(
2252 instr
->variables
[0]->var
->type
, false);
2254 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2255 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
2258 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2262 values
[chan
] = LLVMBuildLoad(ctx
->builder
,
2263 ctx
->outputs
[idx
+ chan
+ const_index
* 4],
2268 case nir_var_shared
: {
2269 LLVMValueRef ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
2270 LLVMValueRef derived_ptr
;
2273 indir_index
= LLVMBuildMul(ctx
->builder
, indir_index
, LLVMConstInt(ctx
->i32
, 4, false), "");
2275 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2276 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, chan
, false);
2278 index
= LLVMBuildAdd(ctx
->builder
, index
, indir_index
, "");
2279 derived_ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, "");
2281 values
[chan
] = LLVMBuildLoad(ctx
->builder
, derived_ptr
, "");
2286 unreachable("unhandle variable mode");
2288 ret
= ac_build_gather_values(&ctx
->ac
, values
, ve
);
2289 return LLVMBuildBitCast(ctx
->builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2293 visit_store_var(struct nir_to_llvm_context
*ctx
,
2294 nir_intrinsic_instr
*instr
)
2296 LLVMValueRef temp_ptr
, value
;
2297 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2298 LLVMValueRef src
= to_float(ctx
, get_src(ctx
, instr
->src
[0]));
2299 int writemask
= instr
->const_index
[0];
2300 LLVMValueRef indir_index
;
2301 unsigned const_index
;
2302 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2303 NULL
, &const_index
, &indir_index
);
2305 if (get_elem_bits(ctx
, LLVMTypeOf(src
)) == 64) {
2306 int old_writemask
= writemask
;
2308 src
= LLVMBuildBitCast(ctx
->builder
, src
,
2309 LLVMVectorType(ctx
->f32
, get_llvm_num_components(src
) * 2),
2313 for (unsigned chan
= 0; chan
< 4; chan
++) {
2314 if (old_writemask
& (1 << chan
))
2315 writemask
|= 3u << (2 * chan
);
2319 switch (instr
->variables
[0]->var
->data
.mode
) {
2320 case nir_var_shader_out
:
2321 for (unsigned chan
= 0; chan
< 8; chan
++) {
2323 if (!(writemask
& (1 << chan
)))
2326 value
= llvm_extract_elem(ctx
, src
, chan
);
2328 if (instr
->variables
[0]->var
->data
.compact
)
2331 unsigned count
= glsl_count_attribute_slots(
2332 instr
->variables
[0]->var
->type
, false);
2334 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2335 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
2338 if (get_llvm_num_components(tmp_vec
) > 1) {
2339 tmp_vec
= LLVMBuildInsertElement(ctx
->builder
, tmp_vec
,
2340 value
, indir_index
, "");
2343 build_store_values_extended(ctx
, ctx
->outputs
+ idx
+ chan
,
2344 count
, stride
, tmp_vec
);
2347 temp_ptr
= ctx
->outputs
[idx
+ chan
+ const_index
* stride
];
2349 LLVMBuildStore(ctx
->builder
, value
, temp_ptr
);
2354 for (unsigned chan
= 0; chan
< 8; chan
++) {
2355 if (!(writemask
& (1 << chan
)))
2358 value
= llvm_extract_elem(ctx
, src
, chan
);
2360 unsigned count
= glsl_count_attribute_slots(
2361 instr
->variables
[0]->var
->type
, false);
2363 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2364 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2367 tmp_vec
= LLVMBuildInsertElement(ctx
->builder
, tmp_vec
,
2368 value
, indir_index
, "");
2369 build_store_values_extended(ctx
, ctx
->locals
+ idx
+ chan
,
2372 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
2374 LLVMBuildStore(ctx
->builder
, value
, temp_ptr
);
2378 case nir_var_shared
: {
2379 LLVMValueRef ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
2382 indir_index
= LLVMBuildMul(ctx
->builder
, indir_index
, LLVMConstInt(ctx
->i32
, 4, false), "");
2384 for (unsigned chan
= 0; chan
< 8; chan
++) {
2385 if (!(writemask
& (1 << chan
)))
2387 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, chan
, false);
2388 LLVMValueRef derived_ptr
;
2391 index
= LLVMBuildAdd(ctx
->builder
, index
, indir_index
, "");
2393 value
= llvm_extract_elem(ctx
, src
, chan
);
2394 derived_ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, "");
2395 LLVMBuildStore(ctx
->builder
,
2396 to_integer(ctx
, value
), derived_ptr
);
2405 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
2408 case GLSL_SAMPLER_DIM_BUF
:
2410 case GLSL_SAMPLER_DIM_1D
:
2411 return array
? 2 : 1;
2412 case GLSL_SAMPLER_DIM_2D
:
2413 return array
? 3 : 2;
2414 case GLSL_SAMPLER_DIM_MS
:
2415 return array
? 4 : 3;
2416 case GLSL_SAMPLER_DIM_3D
:
2417 case GLSL_SAMPLER_DIM_CUBE
:
2419 case GLSL_SAMPLER_DIM_RECT
:
2420 case GLSL_SAMPLER_DIM_SUBPASS
:
2422 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
2432 /* Adjust the sample index according to FMASK.
2434 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
2435 * which is the identity mapping. Each nibble says which physical sample
2436 * should be fetched to get that sample.
2438 * For example, 0x11111100 means there are only 2 samples stored and
2439 * the second sample covers 3/4 of the pixel. When reading samples 0
2440 * and 1, return physical sample 0 (determined by the first two 0s
2441 * in FMASK), otherwise return physical sample 1.
2443 * The sample index should be adjusted as follows:
2444 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
2446 static LLVMValueRef
adjust_sample_index_using_fmask(struct nir_to_llvm_context
*ctx
,
2447 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
2448 LLVMValueRef coord_z
,
2449 LLVMValueRef sample_index
,
2450 LLVMValueRef fmask_desc_ptr
)
2452 LLVMValueRef fmask_load_address
[4];
2455 fmask_load_address
[0] = coord_x
;
2456 fmask_load_address
[1] = coord_y
;
2458 fmask_load_address
[2] = coord_z
;
2459 fmask_load_address
[3] = LLVMGetUndef(ctx
->i32
);
2462 struct ac_image_args args
= {0};
2464 args
.opcode
= ac_image_load
;
2465 args
.da
= coord_z
? true : false;
2466 args
.resource
= fmask_desc_ptr
;
2468 args
.addr
= ac_build_gather_values(&ctx
->ac
, fmask_load_address
, coord_z
? 4 : 2);
2470 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2472 res
= to_integer(ctx
, res
);
2473 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
2474 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
2476 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
2480 LLVMValueRef sample_index4
=
2481 LLVMBuildMul(ctx
->builder
, sample_index
, four
, "");
2482 LLVMValueRef shifted_fmask
=
2483 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
2484 LLVMValueRef final_sample
=
2485 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
2487 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
2488 * resource descriptor is 0 (invalid),
2490 LLVMValueRef fmask_desc
=
2491 LLVMBuildBitCast(ctx
->builder
, fmask_desc_ptr
,
2494 LLVMValueRef fmask_word1
=
2495 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
2498 LLVMValueRef word1_is_nonzero
=
2499 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
2500 fmask_word1
, ctx
->i32zero
, "");
2502 /* Replace the MSAA sample index. */
2504 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
2505 final_sample
, sample_index
, "");
2506 return sample_index
;
2509 static LLVMValueRef
get_image_coords(struct nir_to_llvm_context
*ctx
,
2510 nir_intrinsic_instr
*instr
)
2512 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
2513 if(instr
->variables
[0]->deref
.child
)
2514 type
= instr
->variables
[0]->deref
.child
->type
;
2516 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
2517 LLVMValueRef coords
[4];
2518 LLVMValueRef masks
[] = {
2519 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
2520 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false),
2523 LLVMValueRef sample_index
= llvm_extract_elem(ctx
, get_src(ctx
, instr
->src
[1]), 0);
2526 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2527 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
2528 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2529 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
2530 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2532 count
= image_type_to_components_count(dim
,
2533 glsl_sampler_type_is_array(type
));
2536 LLVMValueRef fmask_load_address
[3];
2539 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[0], "");
2540 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[1], "");
2541 if (glsl_sampler_type_is_array(type
))
2542 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[2], "");
2544 fmask_load_address
[2] = NULL
;
2546 for (chan
= 0; chan
< 2; ++chan
)
2547 fmask_load_address
[chan
] = LLVMBuildAdd(ctx
->builder
, fmask_load_address
[chan
], LLVMBuildFPToUI(ctx
->builder
, ctx
->frag_pos
[chan
], ctx
->i32
, ""), "");
2549 sample_index
= adjust_sample_index_using_fmask(ctx
,
2550 fmask_load_address
[0],
2551 fmask_load_address
[1],
2552 fmask_load_address
[2],
2554 get_sampler_desc(ctx
, instr
->variables
[0], DESC_FMASK
));
2557 if (instr
->src
[0].ssa
->num_components
)
2558 res
= LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[0], "");
2565 for (chan
= 0; chan
< count
; ++chan
) {
2566 coords
[chan
] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[chan
], "");
2570 for (chan
= 0; chan
< count
; ++chan
)
2571 coords
[chan
] = LLVMBuildAdd(ctx
->builder
, coords
[chan
], LLVMBuildFPToUI(ctx
->builder
, ctx
->frag_pos
[chan
], ctx
->i32
, ""), "");
2574 coords
[count
] = sample_index
;
2579 coords
[3] = LLVMGetUndef(ctx
->i32
);
2582 res
= ac_build_gather_values(&ctx
->ac
, coords
, count
);
2587 static LLVMValueRef
visit_image_load(struct nir_to_llvm_context
*ctx
,
2588 nir_intrinsic_instr
*instr
)
2590 LLVMValueRef params
[7];
2592 char intrinsic_name
[64];
2593 const nir_variable
*var
= instr
->variables
[0]->var
;
2594 const struct glsl_type
*type
= var
->type
;
2595 if(instr
->variables
[0]->deref
.child
)
2596 type
= instr
->variables
[0]->deref
.child
->type
;
2598 type
= glsl_without_array(type
);
2599 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2600 params
[0] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
2601 params
[1] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
2602 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
2603 params
[2] = LLVMConstInt(ctx
->i32
, 0, false); /* voffset */
2604 params
[3] = ctx
->i1false
; /* glc */
2605 params
[4] = ctx
->i1false
; /* slc */
2606 res
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.load.format.v4f32", ctx
->v4f32
,
2609 res
= trim_vector(ctx
, res
, instr
->dest
.ssa
.num_components
);
2610 res
= to_integer(ctx
, res
);
2612 bool is_da
= glsl_sampler_type_is_array(type
) ||
2613 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
2614 LLVMValueRef da
= is_da
? ctx
->i1true
: ctx
->i1false
;
2615 LLVMValueRef glc
= ctx
->i1false
;
2616 LLVMValueRef slc
= ctx
->i1false
;
2618 params
[0] = get_image_coords(ctx
, instr
);
2619 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
2620 params
[2] = LLVMConstInt(ctx
->i32
, 15, false); /* dmask */
2621 if (HAVE_LLVM
<= 0x0309) {
2622 params
[3] = ctx
->i1false
; /* r128 */
2627 LLVMValueRef lwe
= ctx
->i1false
;
2634 ac_get_image_intr_name("llvm.amdgcn.image.load",
2635 ctx
->v4f32
, /* vdata */
2636 LLVMTypeOf(params
[0]), /* coords */
2637 LLVMTypeOf(params
[1]), /* rsrc */
2638 intrinsic_name
, sizeof(intrinsic_name
));
2640 res
= ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->v4f32
,
2641 params
, 7, AC_FUNC_ATTR_READONLY
);
2643 return to_integer(ctx
, res
);
2646 static void visit_image_store(struct nir_to_llvm_context
*ctx
,
2647 nir_intrinsic_instr
*instr
)
2649 LLVMValueRef params
[8];
2650 char intrinsic_name
[64];
2651 const nir_variable
*var
= instr
->variables
[0]->var
;
2652 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2654 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
2655 ctx
->shader_info
->fs
.writes_memory
= true;
2657 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2658 params
[0] = to_float(ctx
, get_src(ctx
, instr
->src
[2])); /* data */
2659 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
2660 params
[2] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
2661 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
2662 params
[3] = LLVMConstInt(ctx
->i32
, 0, false); /* voffset */
2663 params
[4] = ctx
->i1false
; /* glc */
2664 params
[5] = ctx
->i1false
; /* slc */
2665 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->voidt
,
2668 bool is_da
= glsl_sampler_type_is_array(type
) ||
2669 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
2670 LLVMValueRef da
= is_da
? ctx
->i1true
: ctx
->i1false
;
2671 LLVMValueRef glc
= ctx
->i1false
;
2672 LLVMValueRef slc
= ctx
->i1false
;
2674 params
[0] = to_float(ctx
, get_src(ctx
, instr
->src
[2]));
2675 params
[1] = get_image_coords(ctx
, instr
); /* coords */
2676 params
[2] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
2677 params
[3] = LLVMConstInt(ctx
->i32
, 15, false); /* dmask */
2678 if (HAVE_LLVM
<= 0x0309) {
2679 params
[4] = ctx
->i1false
; /* r128 */
2684 LLVMValueRef lwe
= ctx
->i1false
;
2691 ac_get_image_intr_name("llvm.amdgcn.image.store",
2692 LLVMTypeOf(params
[0]), /* vdata */
2693 LLVMTypeOf(params
[1]), /* coords */
2694 LLVMTypeOf(params
[2]), /* rsrc */
2695 intrinsic_name
, sizeof(intrinsic_name
));
2697 ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->voidt
,
2703 static LLVMValueRef
visit_image_atomic(struct nir_to_llvm_context
*ctx
,
2704 nir_intrinsic_instr
*instr
)
2706 LLVMValueRef params
[6];
2707 int param_count
= 0;
2708 const nir_variable
*var
= instr
->variables
[0]->var
;
2710 const char *base_name
= "llvm.amdgcn.image.atomic";
2711 const char *atomic_name
;
2712 LLVMValueRef coords
;
2713 char intrinsic_name
[32], coords_type
[8];
2714 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2716 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
2717 ctx
->shader_info
->fs
.writes_memory
= true;
2719 params
[param_count
++] = get_src(ctx
, instr
->src
[2]);
2720 if (instr
->intrinsic
== nir_intrinsic_image_atomic_comp_swap
)
2721 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
2723 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2724 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
2725 coords
= params
[param_count
++] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
2726 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
2727 params
[param_count
++] = ctx
->i32zero
; /* voffset */
2728 params
[param_count
++] = ctx
->i1false
; /* glc */
2729 params
[param_count
++] = ctx
->i1false
; /* slc */
2731 bool da
= glsl_sampler_type_is_array(type
) ||
2732 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
2734 coords
= params
[param_count
++] = get_image_coords(ctx
, instr
);
2735 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
2736 params
[param_count
++] = ctx
->i1false
; /* r128 */
2737 params
[param_count
++] = da
? ctx
->i1true
: ctx
->i1false
; /* da */
2738 params
[param_count
++] = ctx
->i1false
; /* slc */
2741 switch (instr
->intrinsic
) {
2742 case nir_intrinsic_image_atomic_add
:
2743 atomic_name
= "add";
2745 case nir_intrinsic_image_atomic_min
:
2746 atomic_name
= "smin";
2748 case nir_intrinsic_image_atomic_max
:
2749 atomic_name
= "smax";
2751 case nir_intrinsic_image_atomic_and
:
2752 atomic_name
= "and";
2754 case nir_intrinsic_image_atomic_or
:
2757 case nir_intrinsic_image_atomic_xor
:
2758 atomic_name
= "xor";
2760 case nir_intrinsic_image_atomic_exchange
:
2761 atomic_name
= "swap";
2763 case nir_intrinsic_image_atomic_comp_swap
:
2764 atomic_name
= "cmpswap";
2769 build_int_type_name(LLVMTypeOf(coords
),
2770 coords_type
, sizeof(coords_type
));
2772 snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2773 "%s.%s.%s", base_name
, atomic_name
, coords_type
);
2774 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->i32
, params
, param_count
, 0);
2777 static LLVMValueRef
visit_image_size(struct nir_to_llvm_context
*ctx
,
2778 nir_intrinsic_instr
*instr
)
2781 const nir_variable
*var
= instr
->variables
[0]->var
;
2782 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
2783 bool da
= glsl_sampler_type_is_array(var
->type
) ||
2784 glsl_get_sampler_dim(var
->type
) == GLSL_SAMPLER_DIM_CUBE
;
2785 if(instr
->variables
[0]->deref
.child
)
2786 type
= instr
->variables
[0]->deref
.child
->type
;
2788 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
2789 return get_buffer_size(ctx
, get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
), true);
2791 struct ac_image_args args
= { 0 };
2795 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
2796 args
.opcode
= ac_image_get_resinfo
;
2797 args
.addr
= ctx
->i32zero
;
2799 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2801 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
2802 glsl_sampler_type_is_array(type
)) {
2803 LLVMValueRef two
= LLVMConstInt(ctx
->i32
, 2, false);
2804 LLVMValueRef six
= LLVMConstInt(ctx
->i32
, 6, false);
2805 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->builder
, res
, two
, "");
2806 z
= LLVMBuildSDiv(ctx
->builder
, z
, six
, "");
2807 res
= LLVMBuildInsertElement(ctx
->builder
, res
, z
, two
, "");
2812 #define NOOP_WAITCNT 0xf7f
2813 #define LGKM_CNT 0x07f
2814 #define VM_CNT 0xf70
2816 static void emit_waitcnt(struct nir_to_llvm_context
*ctx
,
2819 LLVMValueRef args
[1] = {
2820 LLVMConstInt(ctx
->i32
, simm16
, false),
2822 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.s.waitcnt",
2823 ctx
->voidt
, args
, 1, 0);
2826 static void emit_barrier(struct nir_to_llvm_context
*ctx
)
2829 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.s.barrier",
2830 ctx
->voidt
, NULL
, 0, 0);
2833 static void emit_discard_if(struct nir_to_llvm_context
*ctx
,
2834 nir_intrinsic_instr
*instr
)
2837 ctx
->shader_info
->fs
.can_discard
= true;
2839 cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
2840 get_src(ctx
, instr
->src
[0]),
2843 cond
= LLVMBuildSelect(ctx
->builder
, cond
,
2844 LLVMConstReal(ctx
->f32
, -1.0f
),
2846 ac_build_kill(&ctx
->ac
, cond
);
2850 visit_load_local_invocation_index(struct nir_to_llvm_context
*ctx
)
2852 LLVMValueRef result
;
2853 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
2854 result
= LLVMBuildAnd(ctx
->builder
, ctx
->tg_size
,
2855 LLVMConstInt(ctx
->i32
, 0xfc0, false), "");
2857 return LLVMBuildAdd(ctx
->builder
, result
, thread_id
, "");
2860 static LLVMValueRef
visit_var_atomic(struct nir_to_llvm_context
*ctx
,
2861 nir_intrinsic_instr
*instr
)
2863 LLVMValueRef ptr
, result
;
2864 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2865 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
2866 ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
2868 if (instr
->intrinsic
== nir_intrinsic_var_atomic_comp_swap
) {
2869 LLVMValueRef src1
= get_src(ctx
, instr
->src
[1]);
2870 result
= LLVMBuildAtomicCmpXchg(ctx
->builder
,
2872 LLVMAtomicOrderingSequentiallyConsistent
,
2873 LLVMAtomicOrderingSequentiallyConsistent
,
2876 LLVMAtomicRMWBinOp op
;
2877 switch (instr
->intrinsic
) {
2878 case nir_intrinsic_var_atomic_add
:
2879 op
= LLVMAtomicRMWBinOpAdd
;
2881 case nir_intrinsic_var_atomic_umin
:
2882 op
= LLVMAtomicRMWBinOpUMin
;
2884 case nir_intrinsic_var_atomic_umax
:
2885 op
= LLVMAtomicRMWBinOpUMax
;
2887 case nir_intrinsic_var_atomic_imin
:
2888 op
= LLVMAtomicRMWBinOpMin
;
2890 case nir_intrinsic_var_atomic_imax
:
2891 op
= LLVMAtomicRMWBinOpMax
;
2893 case nir_intrinsic_var_atomic_and
:
2894 op
= LLVMAtomicRMWBinOpAnd
;
2896 case nir_intrinsic_var_atomic_or
:
2897 op
= LLVMAtomicRMWBinOpOr
;
2899 case nir_intrinsic_var_atomic_xor
:
2900 op
= LLVMAtomicRMWBinOpXor
;
2902 case nir_intrinsic_var_atomic_exchange
:
2903 op
= LLVMAtomicRMWBinOpXchg
;
2909 result
= LLVMBuildAtomicRMW(ctx
->builder
, op
, ptr
, to_integer(ctx
, src
),
2910 LLVMAtomicOrderingSequentiallyConsistent
,
2916 #define INTERP_CENTER 0
2917 #define INTERP_CENTROID 1
2918 #define INTERP_SAMPLE 2
2920 static LLVMValueRef
lookup_interp_param(struct nir_to_llvm_context
*ctx
,
2921 enum glsl_interp_mode interp
, unsigned location
)
2924 case INTERP_MODE_FLAT
:
2927 case INTERP_MODE_SMOOTH
:
2928 case INTERP_MODE_NONE
:
2929 if (location
== INTERP_CENTER
)
2930 return ctx
->persp_center
;
2931 else if (location
== INTERP_CENTROID
)
2932 return ctx
->persp_centroid
;
2933 else if (location
== INTERP_SAMPLE
)
2934 return ctx
->persp_sample
;
2936 case INTERP_MODE_NOPERSPECTIVE
:
2937 if (location
== INTERP_CENTER
)
2938 return ctx
->linear_center
;
2939 else if (location
== INTERP_CENTROID
)
2940 return ctx
->linear_centroid
;
2941 else if (location
== INTERP_SAMPLE
)
2942 return ctx
->linear_sample
;
2948 static LLVMValueRef
load_sample_position(struct nir_to_llvm_context
*ctx
,
2949 LLVMValueRef sample_id
)
2951 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
2952 LLVMValueRef offset0
= LLVMBuildMul(ctx
->builder
, sample_id
, LLVMConstInt(ctx
->i32
, 8, false), "");
2953 LLVMValueRef offset1
= LLVMBuildAdd(ctx
->builder
, offset0
, LLVMConstInt(ctx
->i32
, 4, false), "");
2954 LLVMValueRef result
[2];
2956 result
[0] = ac_build_indexed_load_const(&ctx
->ac
, ctx
->sample_positions
, offset0
);
2957 result
[1] = ac_build_indexed_load_const(&ctx
->ac
, ctx
->sample_positions
, offset1
);
2959 return ac_build_gather_values(&ctx
->ac
, result
, 2);
2962 static LLVMValueRef
load_sample_pos(struct nir_to_llvm_context
*ctx
)
2964 LLVMValueRef values
[2];
2966 values
[0] = emit_ffract(ctx
, ctx
->frag_pos
[0]);
2967 values
[1] = emit_ffract(ctx
, ctx
->frag_pos
[1]);
2968 return ac_build_gather_values(&ctx
->ac
, values
, 2);
2971 static LLVMValueRef
visit_interp(struct nir_to_llvm_context
*ctx
,
2972 nir_intrinsic_instr
*instr
)
2974 LLVMValueRef result
[2];
2975 LLVMValueRef interp_param
, attr_number
;
2978 LLVMValueRef src_c0
, src_c1
;
2980 int input_index
= instr
->variables
[0]->var
->data
.location
- VARYING_SLOT_VAR0
;
2981 switch (instr
->intrinsic
) {
2982 case nir_intrinsic_interp_var_at_centroid
:
2983 location
= INTERP_CENTROID
;
2985 case nir_intrinsic_interp_var_at_sample
:
2986 location
= INTERP_SAMPLE
;
2987 src0
= get_src(ctx
, instr
->src
[0]);
2989 case nir_intrinsic_interp_var_at_offset
:
2990 location
= INTERP_CENTER
;
2991 src0
= get_src(ctx
, instr
->src
[0]);
2996 if (instr
->intrinsic
== nir_intrinsic_interp_var_at_offset
) {
2997 src_c0
= to_float(ctx
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32zero
, ""));
2998 src_c1
= to_float(ctx
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32one
, ""));
2999 } else if (instr
->intrinsic
== nir_intrinsic_interp_var_at_sample
) {
3000 LLVMValueRef sample_position
;
3001 LLVMValueRef halfval
= LLVMConstReal(ctx
->f32
, 0.5f
);
3003 /* fetch sample ID */
3004 sample_position
= load_sample_position(ctx
, src0
);
3006 src_c0
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->i32zero
, "");
3007 src_c0
= LLVMBuildFSub(ctx
->builder
, src_c0
, halfval
, "");
3008 src_c1
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->i32one
, "");
3009 src_c1
= LLVMBuildFSub(ctx
->builder
, src_c1
, halfval
, "");
3011 interp_param
= lookup_interp_param(ctx
, instr
->variables
[0]->var
->data
.interpolation
, location
);
3012 attr_number
= LLVMConstInt(ctx
->i32
, input_index
, false);
3014 if (location
== INTERP_SAMPLE
|| location
== INTERP_CENTER
) {
3015 LLVMValueRef ij_out
[2];
3016 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
, interp_param
);
3019 * take the I then J parameters, and the DDX/Y for it, and
3020 * calculate the IJ inputs for the interpolator.
3021 * temp1 = ddx * offset/sample.x + I;
3022 * interp_param.I = ddy * offset/sample.y + temp1;
3023 * temp1 = ddx * offset/sample.x + J;
3024 * interp_param.J = ddy * offset/sample.y + temp1;
3026 for (unsigned i
= 0; i
< 2; i
++) {
3027 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->i32
, i
, false);
3028 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->i32
, i
+ 2, false);
3029 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->builder
,
3030 ddxy_out
, ix_ll
, "");
3031 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->builder
,
3032 ddxy_out
, iy_ll
, "");
3033 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->builder
,
3034 interp_param
, ix_ll
, "");
3035 LLVMValueRef temp1
, temp2
;
3037 interp_el
= LLVMBuildBitCast(ctx
->builder
, interp_el
,
3040 temp1
= LLVMBuildFMul(ctx
->builder
, ddx_el
, src_c0
, "");
3041 temp1
= LLVMBuildFAdd(ctx
->builder
, temp1
, interp_el
, "");
3043 temp2
= LLVMBuildFMul(ctx
->builder
, ddy_el
, src_c1
, "");
3044 temp2
= LLVMBuildFAdd(ctx
->builder
, temp2
, temp1
, "");
3046 ij_out
[i
] = LLVMBuildBitCast(ctx
->builder
,
3047 temp2
, ctx
->i32
, "");
3049 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
3053 for (chan
= 0; chan
< 2; chan
++) {
3054 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
3057 interp_param
= LLVMBuildBitCast(ctx
->builder
,
3058 interp_param
, LLVMVectorType(ctx
->f32
, 2), "");
3059 LLVMValueRef i
= LLVMBuildExtractElement(
3060 ctx
->builder
, interp_param
, ctx
->i32zero
, "");
3061 LLVMValueRef j
= LLVMBuildExtractElement(
3062 ctx
->builder
, interp_param
, ctx
->i32one
, "");
3064 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
3065 llvm_chan
, attr_number
,
3066 ctx
->prim_mask
, i
, j
);
3068 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
3069 LLVMConstInt(ctx
->i32
, 2, false),
3070 llvm_chan
, attr_number
,
3074 return ac_build_gather_values(&ctx
->ac
, result
, 2);
3078 visit_emit_vertex(struct nir_to_llvm_context
*ctx
,
3079 nir_intrinsic_instr
*instr
)
3081 LLVMValueRef gs_next_vertex
;
3082 LLVMValueRef can_emit
, kill
;
3085 assert(instr
->const_index
[0] == 0);
3086 /* Write vertex attribute values to GSVS ring */
3087 gs_next_vertex
= LLVMBuildLoad(ctx
->builder
,
3088 ctx
->gs_next_vertex
,
3091 /* If this thread has already emitted the declared maximum number of
3092 * vertices, kill it: excessive vertex emissions are not supposed to
3093 * have any effect, and GS threads have no externally observable
3094 * effects other than emitting vertices.
3096 can_emit
= LLVMBuildICmp(ctx
->builder
, LLVMIntULT
, gs_next_vertex
,
3097 LLVMConstInt(ctx
->i32
, ctx
->gs_max_out_vertices
, false), "");
3099 kill
= LLVMBuildSelect(ctx
->builder
, can_emit
,
3100 LLVMConstReal(ctx
->f32
, 1.0f
),
3101 LLVMConstReal(ctx
->f32
, -1.0f
), "");
3102 ac_build_kill(&ctx
->ac
, kill
);
3104 /* loop num outputs */
3106 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
3107 LLVMValueRef
*out_ptr
= &ctx
->outputs
[i
* 4];
3112 if (!(ctx
->output_mask
& (1ull << i
)))
3115 if (i
== VARYING_SLOT_CLIP_DIST0
) {
3116 /* pack clip and cull into a single set of slots */
3117 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
3121 for (unsigned j
= 0; j
< length
; j
++) {
3122 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
,
3124 LLVMValueRef voffset
= LLVMConstInt(ctx
->i32
, (slot
* 4 + j
) * ctx
->gs_max_out_vertices
, false);
3125 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, gs_next_vertex
, "");
3126 voffset
= LLVMBuildMul(ctx
->builder
, voffset
, LLVMConstInt(ctx
->i32
, 4, false), "");
3128 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->i32
, "");
3130 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->gsvs_ring
,
3132 voffset
, ctx
->gs2vs_offset
, 0,
3138 gs_next_vertex
= LLVMBuildAdd(ctx
->builder
, gs_next_vertex
,
3140 LLVMBuildStore(ctx
->builder
, gs_next_vertex
, ctx
->gs_next_vertex
);
3142 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_EMIT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
3146 visit_end_primitive(struct nir_to_llvm_context
*ctx
,
3147 nir_intrinsic_instr
*instr
)
3149 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_CUT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
3152 static void visit_intrinsic(struct nir_to_llvm_context
*ctx
,
3153 nir_intrinsic_instr
*instr
)
3155 LLVMValueRef result
= NULL
;
3157 switch (instr
->intrinsic
) {
3158 case nir_intrinsic_load_work_group_id
: {
3159 result
= ctx
->workgroup_ids
;
3162 case nir_intrinsic_load_base_vertex
: {
3163 result
= ctx
->base_vertex
;
3166 case nir_intrinsic_load_vertex_id_zero_base
: {
3167 result
= ctx
->vertex_id
;
3170 case nir_intrinsic_load_local_invocation_id
: {
3171 result
= ctx
->local_invocation_ids
;
3174 case nir_intrinsic_load_base_instance
:
3175 result
= ctx
->start_instance
;
3177 case nir_intrinsic_load_draw_id
:
3178 result
= ctx
->draw_index
;
3180 case nir_intrinsic_load_invocation_id
:
3181 result
= ctx
->gs_invocation_id
;
3183 case nir_intrinsic_load_primitive_id
:
3184 if (ctx
->stage
== MESA_SHADER_GEOMETRY
)
3185 result
= ctx
->gs_prim_id
;
3187 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
3189 case nir_intrinsic_load_sample_id
:
3190 ctx
->shader_info
->fs
.force_persample
= true;
3191 result
= unpack_param(ctx
, ctx
->ancillary
, 8, 4);
3193 case nir_intrinsic_load_sample_pos
:
3194 ctx
->shader_info
->fs
.force_persample
= true;
3195 result
= load_sample_pos(ctx
);
3197 case nir_intrinsic_load_sample_mask_in
:
3198 result
= ctx
->sample_coverage
;
3200 case nir_intrinsic_load_front_face
:
3201 result
= ctx
->front_face
;
3203 case nir_intrinsic_load_instance_id
:
3204 result
= ctx
->instance_id
;
3205 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
3206 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
3208 case nir_intrinsic_load_num_work_groups
:
3209 result
= ctx
->num_work_groups
;
3211 case nir_intrinsic_load_local_invocation_index
:
3212 result
= visit_load_local_invocation_index(ctx
);
3214 case nir_intrinsic_load_push_constant
:
3215 result
= visit_load_push_constant(ctx
, instr
);
3217 case nir_intrinsic_vulkan_resource_index
:
3218 result
= visit_vulkan_resource_index(ctx
, instr
);
3220 case nir_intrinsic_store_ssbo
:
3221 visit_store_ssbo(ctx
, instr
);
3223 case nir_intrinsic_load_ssbo
:
3224 result
= visit_load_buffer(ctx
, instr
);
3226 case nir_intrinsic_ssbo_atomic_add
:
3227 case nir_intrinsic_ssbo_atomic_imin
:
3228 case nir_intrinsic_ssbo_atomic_umin
:
3229 case nir_intrinsic_ssbo_atomic_imax
:
3230 case nir_intrinsic_ssbo_atomic_umax
:
3231 case nir_intrinsic_ssbo_atomic_and
:
3232 case nir_intrinsic_ssbo_atomic_or
:
3233 case nir_intrinsic_ssbo_atomic_xor
:
3234 case nir_intrinsic_ssbo_atomic_exchange
:
3235 case nir_intrinsic_ssbo_atomic_comp_swap
:
3236 result
= visit_atomic_ssbo(ctx
, instr
);
3238 case nir_intrinsic_load_ubo
:
3239 result
= visit_load_ubo_buffer(ctx
, instr
);
3241 case nir_intrinsic_get_buffer_size
:
3242 result
= visit_get_buffer_size(ctx
, instr
);
3244 case nir_intrinsic_load_var
:
3245 result
= visit_load_var(ctx
, instr
);
3247 case nir_intrinsic_store_var
:
3248 visit_store_var(ctx
, instr
);
3250 case nir_intrinsic_image_load
:
3251 result
= visit_image_load(ctx
, instr
);
3253 case nir_intrinsic_image_store
:
3254 visit_image_store(ctx
, instr
);
3256 case nir_intrinsic_image_atomic_add
:
3257 case nir_intrinsic_image_atomic_min
:
3258 case nir_intrinsic_image_atomic_max
:
3259 case nir_intrinsic_image_atomic_and
:
3260 case nir_intrinsic_image_atomic_or
:
3261 case nir_intrinsic_image_atomic_xor
:
3262 case nir_intrinsic_image_atomic_exchange
:
3263 case nir_intrinsic_image_atomic_comp_swap
:
3264 result
= visit_image_atomic(ctx
, instr
);
3266 case nir_intrinsic_image_size
:
3267 result
= visit_image_size(ctx
, instr
);
3269 case nir_intrinsic_discard
:
3270 ctx
->shader_info
->fs
.can_discard
= true;
3271 ac_build_intrinsic(&ctx
->ac
, "llvm.AMDGPU.kilp",
3273 NULL
, 0, AC_FUNC_ATTR_LEGACY
);
3275 case nir_intrinsic_discard_if
:
3276 emit_discard_if(ctx
, instr
);
3278 case nir_intrinsic_memory_barrier
:
3279 emit_waitcnt(ctx
, VM_CNT
);
3281 case nir_intrinsic_barrier
:
3284 case nir_intrinsic_var_atomic_add
:
3285 case nir_intrinsic_var_atomic_imin
:
3286 case nir_intrinsic_var_atomic_umin
:
3287 case nir_intrinsic_var_atomic_imax
:
3288 case nir_intrinsic_var_atomic_umax
:
3289 case nir_intrinsic_var_atomic_and
:
3290 case nir_intrinsic_var_atomic_or
:
3291 case nir_intrinsic_var_atomic_xor
:
3292 case nir_intrinsic_var_atomic_exchange
:
3293 case nir_intrinsic_var_atomic_comp_swap
:
3294 result
= visit_var_atomic(ctx
, instr
);
3296 case nir_intrinsic_interp_var_at_centroid
:
3297 case nir_intrinsic_interp_var_at_sample
:
3298 case nir_intrinsic_interp_var_at_offset
:
3299 result
= visit_interp(ctx
, instr
);
3301 case nir_intrinsic_emit_vertex
:
3302 visit_emit_vertex(ctx
, instr
);
3304 case nir_intrinsic_end_primitive
:
3305 visit_end_primitive(ctx
, instr
);
3308 fprintf(stderr
, "Unknown intrinsic: ");
3309 nir_print_instr(&instr
->instr
, stderr
);
3310 fprintf(stderr
, "\n");
3314 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
3318 static LLVMValueRef
get_sampler_desc(struct nir_to_llvm_context
*ctx
,
3319 nir_deref_var
*deref
,
3320 enum desc_type desc_type
)
3322 unsigned desc_set
= deref
->var
->data
.descriptor_set
;
3323 LLVMValueRef list
= ctx
->descriptor_sets
[desc_set
];
3324 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[desc_set
].layout
;
3325 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ deref
->var
->data
.binding
;
3326 unsigned offset
= binding
->offset
;
3327 unsigned stride
= binding
->size
;
3329 LLVMBuilderRef builder
= ctx
->builder
;
3331 LLVMValueRef index
= NULL
;
3332 unsigned constant_index
= 0;
3334 assert(deref
->var
->data
.binding
< layout
->binding_count
);
3336 switch (desc_type
) {
3348 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
3358 unreachable("invalid desc_type\n");
3361 if (deref
->deref
.child
) {
3362 nir_deref_array
*child
= (nir_deref_array
*)deref
->deref
.child
;
3364 assert(child
->deref_array_type
!= nir_deref_array_type_wildcard
);
3365 offset
+= child
->base_offset
* stride
;
3366 if (child
->deref_array_type
== nir_deref_array_type_indirect
) {
3367 index
= get_src(ctx
, child
->indirect
);
3370 constant_index
= child
->base_offset
;
3372 if (desc_type
== DESC_SAMPLER
&& binding
->immutable_samplers
&&
3373 (!index
|| binding
->immutable_samplers_equal
)) {
3374 if (binding
->immutable_samplers_equal
)
3377 LLVMValueRef constants
[] = {
3378 LLVMConstInt(ctx
->i32
, binding
->immutable_samplers
[constant_index
* 4 + 0], 0),
3379 LLVMConstInt(ctx
->i32
, binding
->immutable_samplers
[constant_index
* 4 + 1], 0),
3380 LLVMConstInt(ctx
->i32
, binding
->immutable_samplers
[constant_index
* 4 + 2], 0),
3381 LLVMConstInt(ctx
->i32
, binding
->immutable_samplers
[constant_index
* 4 + 3], 0),
3383 return ac_build_gather_values(&ctx
->ac
, constants
, 4);
3386 assert(stride
% type_size
== 0);
3389 index
= ctx
->i32zero
;
3391 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, stride
/ type_size
, 0), "");
3393 list
= ac_build_gep0(&ctx
->ac
, list
, LLVMConstInt(ctx
->i32
, offset
, 0));
3394 list
= LLVMBuildPointerCast(builder
, list
, const_array(type
, 0), "");
3396 return ac_build_indexed_load_const(&ctx
->ac
, list
, index
);
3399 static void set_tex_fetch_args(struct nir_to_llvm_context
*ctx
,
3400 struct ac_image_args
*args
,
3401 nir_tex_instr
*instr
,
3403 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
3404 LLVMValueRef
*param
, unsigned count
,
3407 unsigned is_rect
= 0;
3408 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
3410 if (op
== nir_texop_lod
)
3412 /* Pad to power of two vector */
3413 while (count
< util_next_power_of_two(count
))
3414 param
[count
++] = LLVMGetUndef(ctx
->i32
);
3417 args
->addr
= ac_build_gather_values(&ctx
->ac
, param
, count
);
3419 args
->addr
= param
[0];
3421 args
->resource
= res_ptr
;
3422 args
->sampler
= samp_ptr
;
3424 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
&& op
== nir_texop_txf
) {
3425 args
->addr
= param
[0];
3429 args
->dmask
= dmask
;
3430 args
->unorm
= is_rect
;
3434 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
3437 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
3438 * filtering manually. The driver sets img7 to a mask clearing
3439 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
3440 * s_and_b32 samp0, samp0, img7
3443 * The ANISO_OVERRIDE sampler field enables this fix in TA.
3445 static LLVMValueRef
sici_fix_sampler_aniso(struct nir_to_llvm_context
*ctx
,
3446 LLVMValueRef res
, LLVMValueRef samp
)
3448 LLVMBuilderRef builder
= ctx
->builder
;
3449 LLVMValueRef img7
, samp0
;
3451 if (ctx
->options
->chip_class
>= VI
)
3454 img7
= LLVMBuildExtractElement(builder
, res
,
3455 LLVMConstInt(ctx
->i32
, 7, 0), "");
3456 samp0
= LLVMBuildExtractElement(builder
, samp
,
3457 LLVMConstInt(ctx
->i32
, 0, 0), "");
3458 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
3459 return LLVMBuildInsertElement(builder
, samp
, samp0
,
3460 LLVMConstInt(ctx
->i32
, 0, 0), "");
3463 static void tex_fetch_ptrs(struct nir_to_llvm_context
*ctx
,
3464 nir_tex_instr
*instr
,
3465 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
3466 LLVMValueRef
*fmask_ptr
)
3468 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
3469 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_BUFFER
);
3471 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_IMAGE
);
3474 *samp_ptr
= get_sampler_desc(ctx
, instr
->sampler
, DESC_SAMPLER
);
3476 *samp_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_SAMPLER
);
3477 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
3478 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
3480 if (fmask_ptr
&& !instr
->sampler
&& (instr
->op
== nir_texop_txf_ms
||
3481 instr
->op
== nir_texop_samples_identical
))
3482 *fmask_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_FMASK
);
3485 static LLVMValueRef
apply_round_slice(struct nir_to_llvm_context
*ctx
,
3488 coord
= to_float(ctx
, coord
);
3489 coord
= ac_build_intrinsic(&ctx
->ac
, "llvm.rint.f32", ctx
->f32
, &coord
, 1, 0);
3490 coord
= to_integer(ctx
, coord
);
3494 static void visit_tex(struct nir_to_llvm_context
*ctx
, nir_tex_instr
*instr
)
3496 LLVMValueRef result
= NULL
;
3497 struct ac_image_args args
= { 0 };
3498 unsigned dmask
= 0xf;
3499 LLVMValueRef address
[16];
3500 LLVMValueRef coords
[5];
3501 LLVMValueRef coord
= NULL
, lod
= NULL
, comparator
= NULL
;
3502 LLVMValueRef bias
= NULL
, offsets
= NULL
;
3503 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
, sample_index
= NULL
;
3504 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
3505 LLVMValueRef derivs
[6];
3506 unsigned chan
, count
= 0;
3507 unsigned const_src
= 0, num_deriv_comp
= 0;
3509 tex_fetch_ptrs(ctx
, instr
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
3511 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3512 switch (instr
->src
[i
].src_type
) {
3513 case nir_tex_src_coord
:
3514 coord
= get_src(ctx
, instr
->src
[i
].src
);
3516 case nir_tex_src_projector
:
3518 case nir_tex_src_comparator
:
3519 comparator
= get_src(ctx
, instr
->src
[i
].src
);
3521 case nir_tex_src_offset
:
3522 offsets
= get_src(ctx
, instr
->src
[i
].src
);
3525 case nir_tex_src_bias
:
3526 bias
= get_src(ctx
, instr
->src
[i
].src
);
3528 case nir_tex_src_lod
:
3529 lod
= get_src(ctx
, instr
->src
[i
].src
);
3531 case nir_tex_src_ms_index
:
3532 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
3534 case nir_tex_src_ms_mcs
:
3536 case nir_tex_src_ddx
:
3537 ddx
= get_src(ctx
, instr
->src
[i
].src
);
3538 num_deriv_comp
= instr
->src
[i
].src
.ssa
->num_components
;
3540 case nir_tex_src_ddy
:
3541 ddy
= get_src(ctx
, instr
->src
[i
].src
);
3543 case nir_tex_src_texture_offset
:
3544 case nir_tex_src_sampler_offset
:
3545 case nir_tex_src_plane
:
3551 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
3552 result
= get_buffer_size(ctx
, res_ptr
, true);
3556 if (instr
->op
== nir_texop_texture_samples
) {
3557 LLVMValueRef res
, samples
, is_msaa
;
3558 res
= LLVMBuildBitCast(ctx
->builder
, res_ptr
, ctx
->v8i32
, "");
3559 samples
= LLVMBuildExtractElement(ctx
->builder
, res
,
3560 LLVMConstInt(ctx
->i32
, 3, false), "");
3561 is_msaa
= LLVMBuildLShr(ctx
->builder
, samples
,
3562 LLVMConstInt(ctx
->i32
, 28, false), "");
3563 is_msaa
= LLVMBuildAnd(ctx
->builder
, is_msaa
,
3564 LLVMConstInt(ctx
->i32
, 0xe, false), "");
3565 is_msaa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, is_msaa
,
3566 LLVMConstInt(ctx
->i32
, 0xe, false), "");
3568 samples
= LLVMBuildLShr(ctx
->builder
, samples
,
3569 LLVMConstInt(ctx
->i32
, 16, false), "");
3570 samples
= LLVMBuildAnd(ctx
->builder
, samples
,
3571 LLVMConstInt(ctx
->i32
, 0xf, false), "");
3572 samples
= LLVMBuildShl(ctx
->builder
, ctx
->i32one
,
3574 samples
= LLVMBuildSelect(ctx
->builder
, is_msaa
, samples
,
3581 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
3582 coords
[chan
] = llvm_extract_elem(ctx
, coord
, chan
);
3584 if (offsets
&& instr
->op
!= nir_texop_txf
) {
3585 LLVMValueRef offset
[3], pack
;
3586 for (chan
= 0; chan
< 3; ++chan
)
3587 offset
[chan
] = ctx
->i32zero
;
3590 for (chan
= 0; chan
< get_llvm_num_components(offsets
); chan
++) {
3591 offset
[chan
] = llvm_extract_elem(ctx
, offsets
, chan
);
3592 offset
[chan
] = LLVMBuildAnd(ctx
->builder
, offset
[chan
],
3593 LLVMConstInt(ctx
->i32
, 0x3f, false), "");
3595 offset
[chan
] = LLVMBuildShl(ctx
->builder
, offset
[chan
],
3596 LLVMConstInt(ctx
->i32
, chan
* 8, false), "");
3598 pack
= LLVMBuildOr(ctx
->builder
, offset
[0], offset
[1], "");
3599 pack
= LLVMBuildOr(ctx
->builder
, pack
, offset
[2], "");
3600 address
[count
++] = pack
;
3603 /* pack LOD bias value */
3604 if (instr
->op
== nir_texop_txb
&& bias
) {
3605 address
[count
++] = bias
;
3608 /* Pack depth comparison value */
3609 if (instr
->is_shadow
&& comparator
) {
3610 address
[count
++] = llvm_extract_elem(ctx
, comparator
, 0);
3613 /* pack derivatives */
3615 switch (instr
->sampler_dim
) {
3616 case GLSL_SAMPLER_DIM_3D
:
3617 case GLSL_SAMPLER_DIM_CUBE
:
3620 case GLSL_SAMPLER_DIM_2D
:
3624 case GLSL_SAMPLER_DIM_1D
:
3629 for (unsigned i
= 0; i
< num_deriv_comp
; i
++) {
3630 derivs
[i
* 2] = to_float(ctx
, llvm_extract_elem(ctx
, ddx
, i
));
3631 derivs
[i
* 2 + 1] = to_float(ctx
, llvm_extract_elem(ctx
, ddy
, i
));
3635 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& coord
) {
3636 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
3637 coords
[chan
] = to_float(ctx
, coords
[chan
]);
3638 if (instr
->coord_components
== 3)
3639 coords
[3] = LLVMGetUndef(ctx
->f32
);
3640 ac_prepare_cube_coords(&ctx
->ac
,
3641 instr
->op
== nir_texop_txd
, instr
->is_array
,
3648 for (unsigned i
= 0; i
< num_deriv_comp
* 2; i
++)
3649 address
[count
++] = derivs
[i
];
3652 /* Pack texture coordinates */
3654 address
[count
++] = coords
[0];
3655 if (instr
->coord_components
> 1) {
3656 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&& instr
->is_array
&& instr
->op
!= nir_texop_txf
) {
3657 coords
[1] = apply_round_slice(ctx
, coords
[1]);
3659 address
[count
++] = coords
[1];
3661 if (instr
->coord_components
> 2) {
3662 /* This seems like a bit of a hack - but it passes Vulkan CTS with it */
3663 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_3D
&& instr
->op
!= nir_texop_txf
) {
3664 coords
[2] = apply_round_slice(ctx
, coords
[2]);
3666 address
[count
++] = coords
[2];
3671 if ((instr
->op
== nir_texop_txl
|| instr
->op
== nir_texop_txf
) && lod
) {
3672 address
[count
++] = lod
;
3673 } else if (instr
->op
== nir_texop_txf_ms
&& sample_index
) {
3674 address
[count
++] = sample_index
;
3675 } else if(instr
->op
== nir_texop_txs
) {
3678 address
[count
++] = lod
;
3680 address
[count
++] = ctx
->i32zero
;
3683 for (chan
= 0; chan
< count
; chan
++) {
3684 address
[chan
] = LLVMBuildBitCast(ctx
->builder
,
3685 address
[chan
], ctx
->i32
, "");
3688 if (instr
->op
== nir_texop_samples_identical
) {
3689 LLVMValueRef txf_address
[4];
3690 struct ac_image_args txf_args
= { 0 };
3691 unsigned txf_count
= count
;
3692 memcpy(txf_address
, address
, sizeof(txf_address
));
3694 if (!instr
->is_array
)
3695 txf_address
[2] = ctx
->i32zero
;
3696 txf_address
[3] = ctx
->i32zero
;
3698 set_tex_fetch_args(ctx
, &txf_args
, instr
, nir_texop_txf
,
3700 txf_address
, txf_count
, 0xf);
3702 result
= build_tex_intrinsic(ctx
, instr
, &txf_args
);
3704 result
= LLVMBuildExtractElement(ctx
->builder
, result
, ctx
->i32zero
, "");
3705 result
= emit_int_cmp(ctx
, LLVMIntEQ
, result
, ctx
->i32zero
);
3709 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
3710 instr
->op
!= nir_texop_txs
) {
3711 unsigned sample_chan
= instr
->is_array
? 3 : 2;
3712 address
[sample_chan
] = adjust_sample_index_using_fmask(ctx
,
3715 instr
->is_array
? address
[2] : NULL
,
3716 address
[sample_chan
],
3720 if (offsets
&& instr
->op
== nir_texop_txf
) {
3721 nir_const_value
*const_offset
=
3722 nir_src_as_const_value(instr
->src
[const_src
].src
);
3723 int num_offsets
= instr
->src
[const_src
].src
.ssa
->num_components
;
3724 assert(const_offset
);
3725 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
3726 if (num_offsets
> 2)
3727 address
[2] = LLVMBuildAdd(ctx
->builder
,
3728 address
[2], LLVMConstInt(ctx
->i32
, const_offset
->i32
[2], false), "");
3729 if (num_offsets
> 1)
3730 address
[1] = LLVMBuildAdd(ctx
->builder
,
3731 address
[1], LLVMConstInt(ctx
->i32
, const_offset
->i32
[1], false), "");
3732 address
[0] = LLVMBuildAdd(ctx
->builder
,
3733 address
[0], LLVMConstInt(ctx
->i32
, const_offset
->i32
[0], false), "");
3737 /* TODO TG4 support */
3738 if (instr
->op
== nir_texop_tg4
) {
3739 if (instr
->is_shadow
)
3742 dmask
= 1 << instr
->component
;
3744 set_tex_fetch_args(ctx
, &args
, instr
, instr
->op
,
3745 res_ptr
, samp_ptr
, address
, count
, dmask
);
3747 result
= build_tex_intrinsic(ctx
, instr
, &args
);
3749 if (instr
->op
== nir_texop_query_levels
)
3750 result
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, 3, false), "");
3751 else if (instr
->is_shadow
&& instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&& instr
->op
!= nir_texop_tg4
)
3752 result
= LLVMBuildExtractElement(ctx
->builder
, result
, ctx
->i32zero
, "");
3753 else if (instr
->op
== nir_texop_txs
&&
3754 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
3756 LLVMValueRef two
= LLVMConstInt(ctx
->i32
, 2, false);
3757 LLVMValueRef six
= LLVMConstInt(ctx
->i32
, 6, false);
3758 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->builder
, result
, two
, "");
3759 z
= LLVMBuildSDiv(ctx
->builder
, z
, six
, "");
3760 result
= LLVMBuildInsertElement(ctx
->builder
, result
, z
, two
, "");
3761 } else if (instr
->dest
.ssa
.num_components
!= 4)
3762 result
= trim_vector(ctx
, result
, instr
->dest
.ssa
.num_components
);
3766 assert(instr
->dest
.is_ssa
);
3767 result
= to_integer(ctx
, result
);
3768 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
3773 static void visit_phi(struct nir_to_llvm_context
*ctx
, nir_phi_instr
*instr
)
3775 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
3776 LLVMValueRef result
= LLVMBuildPhi(ctx
->builder
, type
, "");
3778 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
3779 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
3782 static void visit_post_phi(struct nir_to_llvm_context
*ctx
,
3783 nir_phi_instr
*instr
,
3784 LLVMValueRef llvm_phi
)
3786 nir_foreach_phi_src(src
, instr
) {
3787 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
3788 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
3790 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
3794 static void phi_post_pass(struct nir_to_llvm_context
*ctx
)
3796 struct hash_entry
*entry
;
3797 hash_table_foreach(ctx
->phis
, entry
) {
3798 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
3799 (LLVMValueRef
)entry
->data
);
3804 static void visit_ssa_undef(struct nir_to_llvm_context
*ctx
,
3805 nir_ssa_undef_instr
*instr
)
3807 unsigned num_components
= instr
->def
.num_components
;
3810 if (num_components
== 1)
3811 undef
= LLVMGetUndef(ctx
->i32
);
3813 undef
= LLVMGetUndef(LLVMVectorType(ctx
->i32
, num_components
));
3815 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, undef
);
3818 static void visit_jump(struct nir_to_llvm_context
*ctx
,
3819 nir_jump_instr
*instr
)
3821 switch (instr
->type
) {
3822 case nir_jump_break
:
3823 LLVMBuildBr(ctx
->builder
, ctx
->break_block
);
3824 LLVMClearInsertionPosition(ctx
->builder
);
3826 case nir_jump_continue
:
3827 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
3828 LLVMClearInsertionPosition(ctx
->builder
);
3831 fprintf(stderr
, "Unknown NIR jump instr: ");
3832 nir_print_instr(&instr
->instr
, stderr
);
3833 fprintf(stderr
, "\n");
3838 static void visit_cf_list(struct nir_to_llvm_context
*ctx
,
3839 struct exec_list
*list
);
3841 static void visit_block(struct nir_to_llvm_context
*ctx
, nir_block
*block
)
3843 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->builder
);
3844 nir_foreach_instr(instr
, block
)
3846 switch (instr
->type
) {
3847 case nir_instr_type_alu
:
3848 visit_alu(ctx
, nir_instr_as_alu(instr
));
3850 case nir_instr_type_load_const
:
3851 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
3853 case nir_instr_type_intrinsic
:
3854 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
3856 case nir_instr_type_tex
:
3857 visit_tex(ctx
, nir_instr_as_tex(instr
));
3859 case nir_instr_type_phi
:
3860 visit_phi(ctx
, nir_instr_as_phi(instr
));
3862 case nir_instr_type_ssa_undef
:
3863 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
3865 case nir_instr_type_jump
:
3866 visit_jump(ctx
, nir_instr_as_jump(instr
));
3869 fprintf(stderr
, "Unknown NIR instr type: ");
3870 nir_print_instr(instr
, stderr
);
3871 fprintf(stderr
, "\n");
3876 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
3879 static void visit_if(struct nir_to_llvm_context
*ctx
, nir_if
*if_stmt
)
3881 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
3883 LLVMBasicBlockRef merge_block
=
3884 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
3885 LLVMBasicBlockRef if_block
=
3886 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
3887 LLVMBasicBlockRef else_block
= merge_block
;
3888 if (!exec_list_is_empty(&if_stmt
->else_list
))
3889 else_block
= LLVMAppendBasicBlockInContext(
3890 ctx
->context
, ctx
->main_function
, "");
3892 LLVMValueRef cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, value
,
3893 LLVMConstInt(ctx
->i32
, 0, false), "");
3894 LLVMBuildCondBr(ctx
->builder
, cond
, if_block
, else_block
);
3896 LLVMPositionBuilderAtEnd(ctx
->builder
, if_block
);
3897 visit_cf_list(ctx
, &if_stmt
->then_list
);
3898 if (LLVMGetInsertBlock(ctx
->builder
))
3899 LLVMBuildBr(ctx
->builder
, merge_block
);
3901 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
3902 LLVMPositionBuilderAtEnd(ctx
->builder
, else_block
);
3903 visit_cf_list(ctx
, &if_stmt
->else_list
);
3904 if (LLVMGetInsertBlock(ctx
->builder
))
3905 LLVMBuildBr(ctx
->builder
, merge_block
);
3908 LLVMPositionBuilderAtEnd(ctx
->builder
, merge_block
);
3911 static void visit_loop(struct nir_to_llvm_context
*ctx
, nir_loop
*loop
)
3913 LLVMBasicBlockRef continue_parent
= ctx
->continue_block
;
3914 LLVMBasicBlockRef break_parent
= ctx
->break_block
;
3916 ctx
->continue_block
=
3917 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
3919 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
3921 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
3922 LLVMPositionBuilderAtEnd(ctx
->builder
, ctx
->continue_block
);
3923 visit_cf_list(ctx
, &loop
->body
);
3925 if (LLVMGetInsertBlock(ctx
->builder
))
3926 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
3927 LLVMPositionBuilderAtEnd(ctx
->builder
, ctx
->break_block
);
3929 ctx
->continue_block
= continue_parent
;
3930 ctx
->break_block
= break_parent
;
3933 static void visit_cf_list(struct nir_to_llvm_context
*ctx
,
3934 struct exec_list
*list
)
3936 foreach_list_typed(nir_cf_node
, node
, node
, list
)
3938 switch (node
->type
) {
3939 case nir_cf_node_block
:
3940 visit_block(ctx
, nir_cf_node_as_block(node
));
3943 case nir_cf_node_if
:
3944 visit_if(ctx
, nir_cf_node_as_if(node
));
3947 case nir_cf_node_loop
:
3948 visit_loop(ctx
, nir_cf_node_as_loop(node
));
3958 handle_vs_input_decl(struct nir_to_llvm_context
*ctx
,
3959 struct nir_variable
*variable
)
3961 LLVMValueRef t_list_ptr
= ctx
->vertex_buffers
;
3962 LLVMValueRef t_offset
;
3963 LLVMValueRef t_list
;
3964 LLVMValueRef args
[3];
3966 LLVMValueRef buffer_index
;
3967 int index
= variable
->data
.location
- VERT_ATTRIB_GENERIC0
;
3968 int idx
= variable
->data
.location
;
3969 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, true);
3971 variable
->data
.driver_location
= idx
* 4;
3973 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << index
)) {
3974 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->instance_id
,
3975 ctx
->start_instance
, "");
3976 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
3977 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
3979 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->vertex_id
,
3980 ctx
->base_vertex
, "");
3982 for (unsigned i
= 0; i
< attrib_count
; ++i
, ++idx
) {
3983 t_offset
= LLVMConstInt(ctx
->i32
, index
+ i
, false);
3985 t_list
= ac_build_indexed_load_const(&ctx
->ac
, t_list_ptr
, t_offset
);
3987 args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
3988 args
[2] = buffer_index
;
3989 input
= ac_build_intrinsic(&ctx
->ac
,
3990 "llvm.SI.vs.load.input", ctx
->v4f32
, args
, 3,
3991 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_NOUNWIND
|
3992 AC_FUNC_ATTR_LEGACY
);
3994 for (unsigned chan
= 0; chan
< 4; chan
++) {
3995 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
3996 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
, chan
)] =
3997 to_integer(ctx
, LLVMBuildExtractElement(ctx
->builder
,
3998 input
, llvm_chan
, ""));
4003 static void interp_fs_input(struct nir_to_llvm_context
*ctx
,
4005 LLVMValueRef interp_param
,
4006 LLVMValueRef prim_mask
,
4007 LLVMValueRef result
[4])
4009 LLVMValueRef attr_number
;
4012 bool interp
= interp_param
!= NULL
;
4014 attr_number
= LLVMConstInt(ctx
->i32
, attr
, false);
4016 /* fs.constant returns the param from the middle vertex, so it's not
4017 * really useful for flat shading. It's meant to be used for custom
4018 * interpolation (but the intrinsic can't fetch from the other two
4021 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
4022 * to do the right thing. The only reason we use fs.constant is that
4023 * fs.interp cannot be used on integers, because they can be equal
4027 interp_param
= LLVMBuildBitCast(ctx
->builder
, interp_param
,
4028 LLVMVectorType(ctx
->f32
, 2), "");
4030 i
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
4032 j
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
4036 for (chan
= 0; chan
< 4; chan
++) {
4037 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
4040 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
4045 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
4046 LLVMConstInt(ctx
->i32
, 2, false),
4055 handle_fs_input_decl(struct nir_to_llvm_context
*ctx
,
4056 struct nir_variable
*variable
)
4058 int idx
= variable
->data
.location
;
4059 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4060 LLVMValueRef interp
;
4062 variable
->data
.driver_location
= idx
* 4;
4063 ctx
->input_mask
|= ((1ull << attrib_count
) - 1) << variable
->data
.location
;
4065 if (glsl_get_base_type(glsl_without_array(variable
->type
)) == GLSL_TYPE_FLOAT
) {
4066 unsigned interp_type
;
4067 if (variable
->data
.sample
) {
4068 interp_type
= INTERP_SAMPLE
;
4069 ctx
->shader_info
->fs
.force_persample
= true;
4070 } else if (variable
->data
.centroid
)
4071 interp_type
= INTERP_CENTROID
;
4073 interp_type
= INTERP_CENTER
;
4075 interp
= lookup_interp_param(ctx
, variable
->data
.interpolation
, interp_type
);
4079 for (unsigned i
= 0; i
< attrib_count
; ++i
)
4080 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
+ i
, 0)] = interp
;
4085 handle_shader_input_decl(struct nir_to_llvm_context
*ctx
,
4086 struct nir_variable
*variable
)
4088 switch (ctx
->stage
) {
4089 case MESA_SHADER_VERTEX
:
4090 handle_vs_input_decl(ctx
, variable
);
4092 case MESA_SHADER_FRAGMENT
:
4093 handle_fs_input_decl(ctx
, variable
);
4102 handle_fs_inputs_pre(struct nir_to_llvm_context
*ctx
,
4103 struct nir_shader
*nir
)
4106 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_INPUTS
; ++i
) {
4107 LLVMValueRef interp_param
;
4108 LLVMValueRef
*inputs
= ctx
->inputs
+radeon_llvm_reg_index_soa(i
, 0);
4110 if (!(ctx
->input_mask
& (1ull << i
)))
4113 if (i
>= VARYING_SLOT_VAR0
|| i
== VARYING_SLOT_PNTC
||
4114 i
== VARYING_SLOT_PRIMITIVE_ID
|| i
== VARYING_SLOT_LAYER
) {
4115 interp_param
= *inputs
;
4116 interp_fs_input(ctx
, index
, interp_param
, ctx
->prim_mask
,
4120 ctx
->shader_info
->fs
.flat_shaded_mask
|= 1u << index
;
4122 } else if (i
== VARYING_SLOT_POS
) {
4123 for(int i
= 0; i
< 3; ++i
)
4124 inputs
[i
] = ctx
->frag_pos
[i
];
4126 inputs
[3] = ac_build_fdiv(&ctx
->ac
, ctx
->f32one
, ctx
->frag_pos
[3]);
4129 ctx
->shader_info
->fs
.num_interp
= index
;
4130 if (ctx
->input_mask
& (1 << VARYING_SLOT_PNTC
))
4131 ctx
->shader_info
->fs
.has_pcoord
= true;
4132 if (ctx
->input_mask
& (1 << VARYING_SLOT_PRIMITIVE_ID
))
4133 ctx
->shader_info
->fs
.prim_id_input
= true;
4134 if (ctx
->input_mask
& (1 << VARYING_SLOT_LAYER
))
4135 ctx
->shader_info
->fs
.layer_input
= true;
4136 ctx
->shader_info
->fs
.input_mask
= ctx
->input_mask
>> VARYING_SLOT_VAR0
;
4140 ac_build_alloca(struct nir_to_llvm_context
*ctx
,
4144 LLVMBuilderRef builder
= ctx
->builder
;
4145 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
4146 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
4147 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
4148 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
4149 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ctx
->context
);
4153 LLVMPositionBuilderBefore(first_builder
, first_instr
);
4155 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
4158 res
= LLVMBuildAlloca(first_builder
, type
, name
);
4159 LLVMBuildStore(builder
, LLVMConstNull(type
), res
);
4161 LLVMDisposeBuilder(first_builder
);
4166 static LLVMValueRef
si_build_alloca_undef(struct nir_to_llvm_context
*ctx
,
4170 LLVMValueRef ptr
= ac_build_alloca(ctx
, type
, name
);
4171 LLVMBuildStore(ctx
->builder
, LLVMGetUndef(type
), ptr
);
4176 handle_shader_output_decl(struct nir_to_llvm_context
*ctx
,
4177 struct nir_variable
*variable
)
4179 int idx
= variable
->data
.location
+ variable
->data
.index
;
4180 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4181 uint64_t mask_attribs
;
4182 variable
->data
.driver_location
= idx
* 4;
4184 mask_attribs
= ((1ull << attrib_count
) - 1) << idx
;
4185 if (ctx
->stage
== MESA_SHADER_VERTEX
||
4186 ctx
->stage
== MESA_SHADER_GEOMETRY
) {
4187 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
4188 int length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
4189 if (ctx
->stage
== MESA_SHADER_VERTEX
) {
4190 ctx
->shader_info
->vs
.outinfo
.clip_dist_mask
= (1 << ctx
->num_output_clips
) - 1;
4191 ctx
->shader_info
->vs
.outinfo
.cull_dist_mask
= (1 << ctx
->num_output_culls
) - 1;
4198 mask_attribs
= 1ull << idx
;
4202 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
4203 for (unsigned chan
= 0; chan
< 4; chan
++) {
4204 ctx
->outputs
[radeon_llvm_reg_index_soa(idx
+ i
, chan
)] =
4205 si_build_alloca_undef(ctx
, ctx
->f32
, "");
4208 ctx
->output_mask
|= mask_attribs
;
4212 setup_locals(struct nir_to_llvm_context
*ctx
,
4213 struct nir_function
*func
)
4216 ctx
->num_locals
= 0;
4217 nir_foreach_variable(variable
, &func
->impl
->locals
) {
4218 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4219 variable
->data
.driver_location
= ctx
->num_locals
* 4;
4220 ctx
->num_locals
+= attrib_count
;
4222 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
4226 for (i
= 0; i
< ctx
->num_locals
; i
++) {
4227 for (j
= 0; j
< 4; j
++) {
4228 ctx
->locals
[i
* 4 + j
] =
4229 si_build_alloca_undef(ctx
, ctx
->f32
, "temp");
4235 emit_float_saturate(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
, float lo
, float hi
)
4237 v
= to_float(ctx
, v
);
4238 v
= emit_intrin_2f_param(ctx
, "llvm.maxnum.f32", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, lo
));
4239 return emit_intrin_2f_param(ctx
, "llvm.minnum.f32", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, hi
));
4243 static LLVMValueRef
emit_pack_int16(struct nir_to_llvm_context
*ctx
,
4244 LLVMValueRef src0
, LLVMValueRef src1
)
4246 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
4247 LLVMValueRef comp
[2];
4249 comp
[0] = LLVMBuildAnd(ctx
->builder
, src0
, LLVMConstInt(ctx
-> i32
, 65535, 0), "");
4250 comp
[1] = LLVMBuildAnd(ctx
->builder
, src1
, LLVMConstInt(ctx
-> i32
, 65535, 0), "");
4251 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
4252 return LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
4255 /* Initialize arguments for the shader export intrinsic */
4257 si_llvm_init_export_args(struct nir_to_llvm_context
*ctx
,
4258 LLVMValueRef
*values
,
4260 struct ac_export_args
*args
)
4262 /* Default is 0xf. Adjusted below depending on the format. */
4263 args
->enabled_channels
= 0xf;
4265 /* Specify whether the EXEC mask represents the valid mask */
4266 args
->valid_mask
= 0;
4268 /* Specify whether this is the last export */
4271 /* Specify the target we are exporting */
4272 args
->target
= target
;
4274 args
->compr
= false;
4275 args
->out
[0] = LLVMGetUndef(ctx
->f32
);
4276 args
->out
[1] = LLVMGetUndef(ctx
->f32
);
4277 args
->out
[2] = LLVMGetUndef(ctx
->f32
);
4278 args
->out
[3] = LLVMGetUndef(ctx
->f32
);
4283 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&& target
>= V_008DFC_SQ_EXP_MRT
) {
4284 LLVMValueRef val
[4];
4285 unsigned index
= target
- V_008DFC_SQ_EXP_MRT
;
4286 unsigned col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * index
)) & 0xf;
4287 bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> index
) & 1;
4289 switch(col_format
) {
4290 case V_028714_SPI_SHADER_ZERO
:
4291 args
->enabled_channels
= 0; /* writemask */
4292 args
->target
= V_008DFC_SQ_EXP_NULL
;
4295 case V_028714_SPI_SHADER_32_R
:
4296 args
->enabled_channels
= 1;
4297 args
->out
[0] = values
[0];
4300 case V_028714_SPI_SHADER_32_GR
:
4301 args
->enabled_channels
= 0x3;
4302 args
->out
[0] = values
[0];
4303 args
->out
[1] = values
[1];
4306 case V_028714_SPI_SHADER_32_AR
:
4307 args
->enabled_channels
= 0x9;
4308 args
->out
[0] = values
[0];
4309 args
->out
[3] = values
[3];
4312 case V_028714_SPI_SHADER_FP16_ABGR
:
4315 for (unsigned chan
= 0; chan
< 2; chan
++) {
4316 LLVMValueRef pack_args
[2] = {
4318 values
[2 * chan
+ 1]
4320 LLVMValueRef packed
;
4322 packed
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, pack_args
);
4323 args
->out
[chan
] = packed
;
4327 case V_028714_SPI_SHADER_UNORM16_ABGR
:
4328 for (unsigned chan
= 0; chan
< 4; chan
++) {
4329 val
[chan
] = ac_build_clamp(&ctx
->ac
, values
[chan
]);
4330 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
4331 LLVMConstReal(ctx
->f32
, 65535), "");
4332 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
4333 LLVMConstReal(ctx
->f32
, 0.5), "");
4334 val
[chan
] = LLVMBuildFPToUI(ctx
->builder
, val
[chan
],
4339 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
4340 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
4343 case V_028714_SPI_SHADER_SNORM16_ABGR
:
4344 for (unsigned chan
= 0; chan
< 4; chan
++) {
4345 val
[chan
] = emit_float_saturate(ctx
, values
[chan
], -1, 1);
4346 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
4347 LLVMConstReal(ctx
->f32
, 32767), "");
4349 /* If positive, add 0.5, else add -0.5. */
4350 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
4351 LLVMBuildSelect(ctx
->builder
,
4352 LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
,
4353 val
[chan
], ctx
->f32zero
, ""),
4354 LLVMConstReal(ctx
->f32
, 0.5),
4355 LLVMConstReal(ctx
->f32
, -0.5), ""), "");
4356 val
[chan
] = LLVMBuildFPToSI(ctx
->builder
, val
[chan
], ctx
->i32
, "");
4360 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
4361 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
4364 case V_028714_SPI_SHADER_UINT16_ABGR
: {
4365 LLVMValueRef max
= LLVMConstInt(ctx
->i32
, is_int8
? 255 : 65535, 0);
4367 for (unsigned chan
= 0; chan
< 4; chan
++) {
4368 val
[chan
] = to_integer(ctx
, values
[chan
]);
4369 val
[chan
] = emit_minmax_int(ctx
, LLVMIntULT
, val
[chan
], max
);
4373 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
4374 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
4378 case V_028714_SPI_SHADER_SINT16_ABGR
: {
4379 LLVMValueRef max
= LLVMConstInt(ctx
->i32
, is_int8
? 127 : 32767, 0);
4380 LLVMValueRef min
= LLVMConstInt(ctx
->i32
, is_int8
? -128 : -32768, 0);
4383 for (unsigned chan
= 0; chan
< 4; chan
++) {
4384 val
[chan
] = to_integer(ctx
, values
[chan
]);
4385 val
[chan
] = emit_minmax_int(ctx
, LLVMIntSLT
, val
[chan
], max
);
4386 val
[chan
] = emit_minmax_int(ctx
, LLVMIntSGT
, val
[chan
], min
);
4390 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
4391 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
4396 case V_028714_SPI_SHADER_32_ABGR
:
4397 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
4401 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
4403 for (unsigned i
= 0; i
< 4; ++i
)
4404 args
->out
[i
] = to_float(ctx
, args
->out
[i
]);
4408 handle_vs_outputs_post(struct nir_to_llvm_context
*ctx
,
4409 struct ac_vs_output_info
*outinfo
)
4411 uint32_t param_count
= 0;
4413 unsigned pos_idx
, num_pos_exports
= 0;
4414 struct ac_export_args args
, pos_args
[4] = {};
4415 LLVMValueRef psize_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
4418 outinfo
->prim_id_output
= 0xffffffff;
4419 outinfo
->layer_output
= 0xffffffff;
4420 if (ctx
->output_mask
& (1ull << VARYING_SLOT_CLIP_DIST0
)) {
4421 LLVMValueRef slots
[8];
4424 if (outinfo
->cull_dist_mask
)
4425 outinfo
->cull_dist_mask
<<= ctx
->num_output_clips
;
4427 i
= VARYING_SLOT_CLIP_DIST0
;
4428 for (j
= 0; j
< ctx
->num_output_clips
+ ctx
->num_output_culls
; j
++)
4429 slots
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4430 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4432 for (i
= ctx
->num_output_clips
+ ctx
->num_output_culls
; i
< 8; i
++)
4433 slots
[i
] = LLVMGetUndef(ctx
->f32
);
4435 if (ctx
->num_output_clips
+ ctx
->num_output_culls
> 4) {
4436 target
= V_008DFC_SQ_EXP_POS
+ 3;
4437 si_llvm_init_export_args(ctx
, &slots
[4], target
, &args
);
4438 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
4439 &args
, sizeof(args
));
4442 target
= V_008DFC_SQ_EXP_POS
+ 2;
4443 si_llvm_init_export_args(ctx
, &slots
[0], target
, &args
);
4444 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
4445 &args
, sizeof(args
));
4449 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
4450 LLVMValueRef values
[4];
4451 if (!(ctx
->output_mask
& (1ull << i
)))
4454 for (unsigned j
= 0; j
< 4; j
++)
4455 values
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4456 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4458 if (i
== VARYING_SLOT_POS
) {
4459 target
= V_008DFC_SQ_EXP_POS
;
4460 } else if (i
== VARYING_SLOT_CLIP_DIST0
) {
4462 } else if (i
== VARYING_SLOT_PSIZ
) {
4463 outinfo
->writes_pointsize
= true;
4464 psize_value
= values
[0];
4466 } else if (i
== VARYING_SLOT_LAYER
) {
4467 outinfo
->writes_layer
= true;
4468 layer_value
= values
[0];
4469 outinfo
->layer_output
= param_count
;
4470 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
4472 } else if (i
== VARYING_SLOT_VIEWPORT
) {
4473 outinfo
->writes_viewport_index
= true;
4474 viewport_index_value
= values
[0];
4476 } else if (i
== VARYING_SLOT_PRIMITIVE_ID
) {
4477 outinfo
->prim_id_output
= param_count
;
4478 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
4480 } else if (i
>= VARYING_SLOT_VAR0
) {
4481 outinfo
->export_mask
|= 1u << (i
- VARYING_SLOT_VAR0
);
4482 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
4486 si_llvm_init_export_args(ctx
, values
, target
, &args
);
4488 if (target
>= V_008DFC_SQ_EXP_POS
&&
4489 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
4490 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
4491 &args
, sizeof(args
));
4493 ac_build_export(&ctx
->ac
, &args
);
4497 /* We need to add the position output manually if it's missing. */
4498 if (!pos_args
[0].out
[0]) {
4499 pos_args
[0].enabled_channels
= 0xf;
4500 pos_args
[0].valid_mask
= 0;
4501 pos_args
[0].done
= 0;
4502 pos_args
[0].target
= V_008DFC_SQ_EXP_POS
;
4503 pos_args
[0].compr
= 0;
4504 pos_args
[0].out
[0] = ctx
->f32zero
; /* X */
4505 pos_args
[0].out
[1] = ctx
->f32zero
; /* Y */
4506 pos_args
[0].out
[2] = ctx
->f32zero
; /* Z */
4507 pos_args
[0].out
[3] = ctx
->f32one
; /* W */
4510 uint32_t mask
= ((outinfo
->writes_pointsize
== true ? 1 : 0) |
4511 (outinfo
->writes_layer
== true ? 4 : 0) |
4512 (outinfo
->writes_viewport_index
== true ? 8 : 0));
4514 pos_args
[1].enabled_channels
= mask
;
4515 pos_args
[1].valid_mask
= 0;
4516 pos_args
[1].done
= 0;
4517 pos_args
[1].target
= V_008DFC_SQ_EXP_POS
+ 1;
4518 pos_args
[1].compr
= 0;
4519 pos_args
[1].out
[0] = ctx
->f32zero
; /* X */
4520 pos_args
[1].out
[1] = ctx
->f32zero
; /* Y */
4521 pos_args
[1].out
[2] = ctx
->f32zero
; /* Z */
4522 pos_args
[1].out
[3] = ctx
->f32zero
; /* W */
4524 if (outinfo
->writes_pointsize
== true)
4525 pos_args
[1].out
[0] = psize_value
;
4526 if (outinfo
->writes_layer
== true)
4527 pos_args
[1].out
[2] = layer_value
;
4528 if (outinfo
->writes_viewport_index
== true)
4529 pos_args
[1].out
[3] = viewport_index_value
;
4531 for (i
= 0; i
< 4; i
++) {
4532 if (pos_args
[i
].out
[0])
4537 for (i
= 0; i
< 4; i
++) {
4538 if (!pos_args
[i
].out
[0])
4541 /* Specify the target we are exporting */
4542 pos_args
[i
].target
= V_008DFC_SQ_EXP_POS
+ pos_idx
++;
4543 if (pos_idx
== num_pos_exports
)
4544 pos_args
[i
].done
= 1;
4545 ac_build_export(&ctx
->ac
, &pos_args
[i
]);
4548 outinfo
->pos_exports
= num_pos_exports
;
4549 outinfo
->param_exports
= param_count
;
4553 handle_es_outputs_post(struct nir_to_llvm_context
*ctx
,
4554 struct ac_es_output_info
*outinfo
)
4557 uint64_t max_output_written
= 0;
4558 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
4559 LLVMValueRef
*out_ptr
= &ctx
->outputs
[i
* 4];
4563 if (!(ctx
->output_mask
& (1ull << i
)))
4566 if (i
== VARYING_SLOT_CLIP_DIST0
)
4567 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
4569 param_index
= shader_io_get_unique_index(i
);
4571 if (param_index
> max_output_written
)
4572 max_output_written
= param_index
;
4574 for (j
= 0; j
< length
; j
++) {
4575 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], "");
4576 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->i32
, "");
4578 ac_build_buffer_store_dword(&ctx
->ac
,
4581 NULL
, ctx
->es2gs_offset
,
4582 (4 * param_index
+ j
+ start
) * 4,
4586 outinfo
->esgs_itemsize
= (max_output_written
+ 1) * 16;
4590 si_export_mrt_color(struct nir_to_llvm_context
*ctx
,
4591 LLVMValueRef
*color
, unsigned param
, bool is_last
)
4594 struct ac_export_args args
;
4597 si_llvm_init_export_args(ctx
, color
, param
,
4601 args
.valid_mask
= 1; /* whether the EXEC mask is valid */
4602 args
.done
= 1; /* DONE bit */
4603 } else if (!args
.enabled_channels
)
4604 return; /* unnecessary NULL export */
4606 ac_build_export(&ctx
->ac
, &args
);
4610 si_export_mrt_z(struct nir_to_llvm_context
*ctx
,
4611 LLVMValueRef depth
, LLVMValueRef stencil
,
4612 LLVMValueRef samplemask
)
4614 struct ac_export_args args
;
4616 args
.enabled_channels
= 0;
4617 args
.valid_mask
= 1;
4619 args
.target
= V_008DFC_SQ_EXP_MRTZ
;
4622 args
.out
[0] = LLVMGetUndef(ctx
->f32
); /* R, depth */
4623 args
.out
[1] = LLVMGetUndef(ctx
->f32
); /* G, stencil test val[0:7], stencil op val[8:15] */
4624 args
.out
[2] = LLVMGetUndef(ctx
->f32
); /* B, sample mask */
4625 args
.out
[3] = LLVMGetUndef(ctx
->f32
); /* A, alpha to mask */
4628 args
.out
[0] = depth
;
4629 args
.enabled_channels
|= 0x1;
4633 args
.out
[1] = stencil
;
4634 args
.enabled_channels
|= 0x2;
4638 args
.out
[2] = samplemask
;
4639 args
.enabled_channels
|= 0x4;
4642 /* SI (except OLAND) has a bug that it only looks
4643 * at the X writemask component. */
4644 if (ctx
->options
->chip_class
== SI
&&
4645 ctx
->options
->family
!= CHIP_OLAND
)
4646 args
.enabled_channels
|= 0x1;
4648 ac_build_export(&ctx
->ac
, &args
);
4652 handle_fs_outputs_post(struct nir_to_llvm_context
*ctx
)
4655 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
4657 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
4658 LLVMValueRef values
[4];
4660 if (!(ctx
->output_mask
& (1ull << i
)))
4663 if (i
== FRAG_RESULT_DEPTH
) {
4664 ctx
->shader_info
->fs
.writes_z
= true;
4665 depth
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4666 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
4667 } else if (i
== FRAG_RESULT_STENCIL
) {
4668 ctx
->shader_info
->fs
.writes_stencil
= true;
4669 stencil
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4670 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
4671 } else if (i
== FRAG_RESULT_SAMPLE_MASK
) {
4672 ctx
->shader_info
->fs
.writes_sample_mask
= true;
4673 samplemask
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4674 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
4677 for (unsigned j
= 0; j
< 4; j
++)
4678 values
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4679 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4681 if (!ctx
->shader_info
->fs
.writes_z
&& !ctx
->shader_info
->fs
.writes_stencil
&& !ctx
->shader_info
->fs
.writes_sample_mask
)
4682 last
= ctx
->output_mask
<= ((1ull << (i
+ 1)) - 1);
4684 si_export_mrt_color(ctx
, values
, V_008DFC_SQ_EXP_MRT
+ index
, last
);
4689 if (depth
|| stencil
|| samplemask
)
4690 si_export_mrt_z(ctx
, depth
, stencil
, samplemask
);
4692 si_export_mrt_color(ctx
, NULL
, V_008DFC_SQ_EXP_NULL
, true);
4694 ctx
->shader_info
->fs
.output_mask
= index
? ((1ull << index
) - 1) : 0;
4698 emit_gs_epilogue(struct nir_to_llvm_context
*ctx
)
4700 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_NOP
| AC_SENDMSG_GS_DONE
, ctx
->gs_wave_id
);
4704 handle_shader_outputs_post(struct nir_to_llvm_context
*ctx
)
4706 switch (ctx
->stage
) {
4707 case MESA_SHADER_VERTEX
:
4708 if (ctx
->options
->key
.vs
.as_es
)
4709 handle_es_outputs_post(ctx
, &ctx
->shader_info
->vs
.es_info
);
4711 handle_vs_outputs_post(ctx
, &ctx
->shader_info
->vs
.outinfo
);
4713 case MESA_SHADER_FRAGMENT
:
4714 handle_fs_outputs_post(ctx
);
4716 case MESA_SHADER_GEOMETRY
:
4717 emit_gs_epilogue(ctx
);
4725 handle_shared_compute_var(struct nir_to_llvm_context
*ctx
,
4726 struct nir_variable
*variable
, uint32_t *offset
, int idx
)
4728 unsigned size
= glsl_count_attribute_slots(variable
->type
, false);
4729 variable
->data
.driver_location
= *offset
;
4733 static void ac_llvm_finalize_module(struct nir_to_llvm_context
* ctx
)
4735 LLVMPassManagerRef passmgr
;
4736 /* Create the pass manager */
4737 passmgr
= LLVMCreateFunctionPassManagerForModule(
4740 /* This pass should eliminate all the load and store instructions */
4741 LLVMAddPromoteMemoryToRegisterPass(passmgr
);
4743 /* Add some optimization passes */
4744 LLVMAddScalarReplAggregatesPass(passmgr
);
4745 LLVMAddLICMPass(passmgr
);
4746 LLVMAddAggressiveDCEPass(passmgr
);
4747 LLVMAddCFGSimplificationPass(passmgr
);
4748 LLVMAddInstructionCombiningPass(passmgr
);
4751 LLVMInitializeFunctionPassManager(passmgr
);
4752 LLVMRunFunctionPassManager(passmgr
, ctx
->main_function
);
4753 LLVMFinalizeFunctionPassManager(passmgr
);
4755 LLVMDisposeBuilder(ctx
->builder
);
4756 LLVMDisposePassManager(passmgr
);
4760 ac_setup_rings(struct nir_to_llvm_context
*ctx
)
4762 if (ctx
->stage
== MESA_SHADER_VERTEX
&& ctx
->options
->key
.vs
.as_es
) {
4763 ctx
->esgs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, ctx
->i32one
);
4766 if (ctx
->is_gs_copy_shader
) {
4767 ctx
->gsvs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, 3, false));
4769 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
4771 ctx
->esgs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, 2, false));
4772 ctx
->gsvs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, 4, false));
4774 ctx
->gsvs_ring
= LLVMBuildBitCast(ctx
->builder
, ctx
->gsvs_ring
, ctx
->v4i32
, "");
4776 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->gsvs_num_entries
, LLVMConstInt(ctx
->i32
, 2, false), "");
4777 tmp
= LLVMBuildExtractElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->i32one
, "");
4778 tmp
= LLVMBuildOr(ctx
->builder
, tmp
, ctx
->gsvs_ring_stride
, "");
4779 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, tmp
, ctx
->i32one
, "");
4781 ctx
->gsvs_ring
= LLVMBuildBitCast(ctx
->builder
, ctx
->gsvs_ring
, ctx
->v16i8
, "");
4786 LLVMModuleRef
ac_translate_nir_to_llvm(LLVMTargetMachineRef tm
,
4787 struct nir_shader
*nir
,
4788 struct ac_shader_variant_info
*shader_info
,
4789 const struct ac_nir_compiler_options
*options
)
4791 struct nir_to_llvm_context ctx
= {0};
4792 struct nir_function
*func
;
4794 ctx
.options
= options
;
4795 ctx
.shader_info
= shader_info
;
4796 ctx
.context
= LLVMContextCreate();
4797 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
4799 ac_llvm_context_init(&ctx
.ac
, ctx
.context
);
4800 ctx
.ac
.module
= ctx
.module
;
4802 ctx
.has_ds_bpermute
= ctx
.options
->chip_class
>= VI
;
4804 memset(shader_info
, 0, sizeof(*shader_info
));
4806 LLVMSetTarget(ctx
.module
, options
->supports_spill
? "amdgcn-mesa-mesa3d" : "amdgcn--");
4808 LLVMTargetDataRef data_layout
= LLVMCreateTargetDataLayout(tm
);
4809 char *data_layout_str
= LLVMCopyStringRepOfTargetData(data_layout
);
4810 LLVMSetDataLayout(ctx
.module
, data_layout_str
);
4811 LLVMDisposeTargetData(data_layout
);
4812 LLVMDisposeMessage(data_layout_str
);
4816 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
4817 ctx
.ac
.builder
= ctx
.builder
;
4818 ctx
.stage
= nir
->stage
;
4820 for (i
= 0; i
< AC_UD_MAX_SETS
; i
++)
4821 shader_info
->user_sgprs_locs
.descriptor_sets
[i
].sgpr_idx
= -1;
4822 for (i
= 0; i
< AC_UD_MAX_UD
; i
++)
4823 shader_info
->user_sgprs_locs
.shader_data
[i
].sgpr_idx
= -1;
4825 create_function(&ctx
);
4827 if (nir
->stage
== MESA_SHADER_COMPUTE
) {
4829 nir_foreach_variable(variable
, &nir
->shared
)
4833 uint32_t shared_size
= 0;
4835 LLVMTypeRef i8p
= LLVMPointerType(ctx
.i8
, LOCAL_ADDR_SPACE
);
4836 nir_foreach_variable(variable
, &nir
->shared
) {
4837 handle_shared_compute_var(&ctx
, variable
, &shared_size
, idx
);
4842 var
= LLVMAddGlobalInAddressSpace(ctx
.module
,
4843 LLVMArrayType(ctx
.i8
, shared_size
),
4846 LLVMSetAlignment(var
, 4);
4847 ctx
.shared_memory
= LLVMBuildBitCast(ctx
.builder
, var
, i8p
, "");
4849 } else if (nir
->stage
== MESA_SHADER_GEOMETRY
) {
4850 ctx
.gs_next_vertex
= ac_build_alloca(&ctx
, ctx
.i32
, "gs_next_vertex");
4852 ctx
.gs_max_out_vertices
= nir
->info
->gs
.vertices_out
;
4855 ac_setup_rings(&ctx
);
4857 nir_foreach_variable(variable
, &nir
->inputs
)
4858 handle_shader_input_decl(&ctx
, variable
);
4860 if (nir
->stage
== MESA_SHADER_FRAGMENT
)
4861 handle_fs_inputs_pre(&ctx
, nir
);
4863 ctx
.num_output_clips
= nir
->info
->clip_distance_array_size
;
4864 ctx
.num_output_culls
= nir
->info
->cull_distance_array_size
;
4866 nir_foreach_variable(variable
, &nir
->outputs
)
4867 handle_shader_output_decl(&ctx
, variable
);
4869 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4870 _mesa_key_pointer_equal
);
4871 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4872 _mesa_key_pointer_equal
);
4874 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
4876 setup_locals(&ctx
, func
);
4878 visit_cf_list(&ctx
, &func
->impl
->body
);
4879 phi_post_pass(&ctx
);
4881 handle_shader_outputs_post(&ctx
);
4882 LLVMBuildRetVoid(ctx
.builder
);
4884 ac_llvm_finalize_module(&ctx
);
4886 ralloc_free(ctx
.defs
);
4887 ralloc_free(ctx
.phis
);
4889 if (nir
->stage
== MESA_SHADER_GEOMETRY
) {
4890 shader_info
->gs
.gsvs_vertex_size
= util_bitcount64(ctx
.output_mask
) * 16;
4891 shader_info
->gs
.max_gsvs_emit_size
= shader_info
->gs
.gsvs_vertex_size
*
4892 nir
->info
->gs
.vertices_out
;
4897 static void ac_diagnostic_handler(LLVMDiagnosticInfoRef di
, void *context
)
4899 unsigned *retval
= (unsigned *)context
;
4900 LLVMDiagnosticSeverity severity
= LLVMGetDiagInfoSeverity(di
);
4901 char *description
= LLVMGetDiagInfoDescription(di
);
4903 if (severity
== LLVMDSError
) {
4905 fprintf(stderr
, "LLVM triggered Diagnostic Handler: %s\n",
4909 LLVMDisposeMessage(description
);
4912 static unsigned ac_llvm_compile(LLVMModuleRef M
,
4913 struct ac_shader_binary
*binary
,
4914 LLVMTargetMachineRef tm
)
4916 unsigned retval
= 0;
4918 LLVMContextRef llvm_ctx
;
4919 LLVMMemoryBufferRef out_buffer
;
4920 unsigned buffer_size
;
4921 const char *buffer_data
;
4924 /* Setup Diagnostic Handler*/
4925 llvm_ctx
= LLVMGetModuleContext(M
);
4927 LLVMContextSetDiagnosticHandler(llvm_ctx
, ac_diagnostic_handler
,
4931 mem_err
= LLVMTargetMachineEmitToMemoryBuffer(tm
, M
, LLVMObjectFile
,
4934 /* Process Errors/Warnings */
4936 fprintf(stderr
, "%s: %s", __FUNCTION__
, err
);
4942 /* Extract Shader Code*/
4943 buffer_size
= LLVMGetBufferSize(out_buffer
);
4944 buffer_data
= LLVMGetBufferStart(out_buffer
);
4946 ac_elf_read(buffer_data
, buffer_size
, binary
);
4949 LLVMDisposeMemoryBuffer(out_buffer
);
4955 static void ac_compile_llvm_module(LLVMTargetMachineRef tm
,
4956 LLVMModuleRef llvm_module
,
4957 struct ac_shader_binary
*binary
,
4958 struct ac_shader_config
*config
,
4959 struct ac_shader_variant_info
*shader_info
,
4960 gl_shader_stage stage
,
4961 bool dump_shader
, bool supports_spill
)
4964 ac_dump_module(llvm_module
);
4966 memset(binary
, 0, sizeof(*binary
));
4967 int v
= ac_llvm_compile(llvm_module
, binary
, tm
);
4969 fprintf(stderr
, "compile failed\n");
4973 fprintf(stderr
, "disasm:\n%s\n", binary
->disasm_string
);
4975 ac_shader_binary_read_config(binary
, config
, 0, supports_spill
);
4977 LLVMContextRef ctx
= LLVMGetModuleContext(llvm_module
);
4978 LLVMDisposeModule(llvm_module
);
4979 LLVMContextDispose(ctx
);
4981 if (stage
== MESA_SHADER_FRAGMENT
) {
4982 shader_info
->num_input_vgprs
= 0;
4983 if (G_0286CC_PERSP_SAMPLE_ENA(config
->spi_ps_input_addr
))
4984 shader_info
->num_input_vgprs
+= 2;
4985 if (G_0286CC_PERSP_CENTER_ENA(config
->spi_ps_input_addr
))
4986 shader_info
->num_input_vgprs
+= 2;
4987 if (G_0286CC_PERSP_CENTROID_ENA(config
->spi_ps_input_addr
))
4988 shader_info
->num_input_vgprs
+= 2;
4989 if (G_0286CC_PERSP_PULL_MODEL_ENA(config
->spi_ps_input_addr
))
4990 shader_info
->num_input_vgprs
+= 3;
4991 if (G_0286CC_LINEAR_SAMPLE_ENA(config
->spi_ps_input_addr
))
4992 shader_info
->num_input_vgprs
+= 2;
4993 if (G_0286CC_LINEAR_CENTER_ENA(config
->spi_ps_input_addr
))
4994 shader_info
->num_input_vgprs
+= 2;
4995 if (G_0286CC_LINEAR_CENTROID_ENA(config
->spi_ps_input_addr
))
4996 shader_info
->num_input_vgprs
+= 2;
4997 if (G_0286CC_LINE_STIPPLE_TEX_ENA(config
->spi_ps_input_addr
))
4998 shader_info
->num_input_vgprs
+= 1;
4999 if (G_0286CC_POS_X_FLOAT_ENA(config
->spi_ps_input_addr
))
5000 shader_info
->num_input_vgprs
+= 1;
5001 if (G_0286CC_POS_Y_FLOAT_ENA(config
->spi_ps_input_addr
))
5002 shader_info
->num_input_vgprs
+= 1;
5003 if (G_0286CC_POS_Z_FLOAT_ENA(config
->spi_ps_input_addr
))
5004 shader_info
->num_input_vgprs
+= 1;
5005 if (G_0286CC_POS_W_FLOAT_ENA(config
->spi_ps_input_addr
))
5006 shader_info
->num_input_vgprs
+= 1;
5007 if (G_0286CC_FRONT_FACE_ENA(config
->spi_ps_input_addr
))
5008 shader_info
->num_input_vgprs
+= 1;
5009 if (G_0286CC_ANCILLARY_ENA(config
->spi_ps_input_addr
))
5010 shader_info
->num_input_vgprs
+= 1;
5011 if (G_0286CC_SAMPLE_COVERAGE_ENA(config
->spi_ps_input_addr
))
5012 shader_info
->num_input_vgprs
+= 1;
5013 if (G_0286CC_POS_FIXED_PT_ENA(config
->spi_ps_input_addr
))
5014 shader_info
->num_input_vgprs
+= 1;
5016 config
->num_vgprs
= MAX2(config
->num_vgprs
, shader_info
->num_input_vgprs
);
5018 /* +3 for scratch wave offset and VCC */
5019 config
->num_sgprs
= MAX2(config
->num_sgprs
,
5020 shader_info
->num_input_sgprs
+ 3);
5023 void ac_compile_nir_shader(LLVMTargetMachineRef tm
,
5024 struct ac_shader_binary
*binary
,
5025 struct ac_shader_config
*config
,
5026 struct ac_shader_variant_info
*shader_info
,
5027 struct nir_shader
*nir
,
5028 const struct ac_nir_compiler_options
*options
,
5032 LLVMModuleRef llvm_module
= ac_translate_nir_to_llvm(tm
, nir
, shader_info
,
5035 ac_compile_llvm_module(tm
, llvm_module
, binary
, config
, shader_info
, nir
->stage
, dump_shader
, options
->supports_spill
);
5036 switch (nir
->stage
) {
5037 case MESA_SHADER_COMPUTE
:
5038 for (int i
= 0; i
< 3; ++i
)
5039 shader_info
->cs
.block_size
[i
] = nir
->info
->cs
.local_size
[i
];
5041 case MESA_SHADER_FRAGMENT
:
5042 shader_info
->fs
.early_fragment_test
= nir
->info
->fs
.early_fragment_tests
;
5044 case MESA_SHADER_GEOMETRY
:
5045 shader_info
->gs
.vertices_in
= nir
->info
->gs
.vertices_in
;
5046 shader_info
->gs
.vertices_out
= nir
->info
->gs
.vertices_out
;
5047 shader_info
->gs
.output_prim
= nir
->info
->gs
.output_primitive
;
5048 shader_info
->gs
.invocations
= nir
->info
->gs
.invocations
;
5050 case MESA_SHADER_VERTEX
:
5051 shader_info
->vs
.as_es
= options
->key
.vs
.as_es
;
5059 ac_gs_copy_shader_emit(struct nir_to_llvm_context
*ctx
)
5061 LLVMValueRef args
[9];
5062 args
[0] = ctx
->gsvs_ring
;
5063 args
[1] = LLVMBuildMul(ctx
->builder
, ctx
->vertex_id
, LLVMConstInt(ctx
->i32
, 4, false), "");
5064 args
[3] = ctx
->i32zero
;
5065 args
[4] = ctx
->i32one
; /* OFFEN */
5066 args
[5] = ctx
->i32zero
; /* IDXEN */
5067 args
[6] = ctx
->i32one
; /* GLC */
5068 args
[7] = ctx
->i32one
; /* SLC */
5069 args
[8] = ctx
->i32zero
; /* TFE */
5073 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5077 if (!(ctx
->output_mask
& (1ull << i
)))
5080 if (i
== VARYING_SLOT_CLIP_DIST0
) {
5081 /* unpack clip and cull from a single set of slots */
5082 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
5087 for (unsigned j
= 0; j
< length
; j
++) {
5089 args
[2] = LLVMConstInt(ctx
->i32
,
5091 ctx
->gs_max_out_vertices
* 16 * 4, false);
5093 value
= ac_build_intrinsic(&ctx
->ac
,
5094 "llvm.SI.buffer.load.dword.i32.i32",
5096 AC_FUNC_ATTR_READONLY
|
5097 AC_FUNC_ATTR_LEGACY
);
5099 LLVMBuildStore(ctx
->builder
,
5100 to_float(ctx
, value
), ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)]);
5104 handle_vs_outputs_post(ctx
, &ctx
->shader_info
->vs
.outinfo
);
5107 void ac_create_gs_copy_shader(LLVMTargetMachineRef tm
,
5108 struct nir_shader
*geom_shader
,
5109 struct ac_shader_binary
*binary
,
5110 struct ac_shader_config
*config
,
5111 struct ac_shader_variant_info
*shader_info
,
5112 const struct ac_nir_compiler_options
*options
,
5115 struct nir_to_llvm_context ctx
= {0};
5116 ctx
.context
= LLVMContextCreate();
5117 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
5118 ctx
.options
= options
;
5119 ctx
.shader_info
= shader_info
;
5121 ac_llvm_context_init(&ctx
.ac
, ctx
.context
);
5122 ctx
.ac
.module
= ctx
.module
;
5124 ctx
.is_gs_copy_shader
= true;
5125 LLVMSetTarget(ctx
.module
, "amdgcn--");
5128 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
5129 ctx
.ac
.builder
= ctx
.builder
;
5130 ctx
.stage
= MESA_SHADER_VERTEX
;
5132 create_function(&ctx
);
5134 ctx
.gs_max_out_vertices
= geom_shader
->info
->gs
.vertices_out
;
5135 ac_setup_rings(&ctx
);
5137 ctx
.num_output_clips
= geom_shader
->info
->clip_distance_array_size
;
5138 ctx
.num_output_culls
= geom_shader
->info
->cull_distance_array_size
;
5140 nir_foreach_variable(variable
, &geom_shader
->outputs
)
5141 handle_shader_output_decl(&ctx
, variable
);
5143 ac_gs_copy_shader_emit(&ctx
);
5145 LLVMBuildRetVoid(ctx
.builder
);
5147 ac_llvm_finalize_module(&ctx
);
5149 ac_compile_llvm_module(tm
, ctx
.module
, binary
, config
, shader_info
,
5151 dump_shader
, options
->supports_spill
);