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
;
83 LLVMValueRef ls_out_layout
;
84 LLVMValueRef es2gs_offset
;
86 LLVMValueRef tcs_offchip_layout
;
87 LLVMValueRef tcs_out_offsets
;
88 LLVMValueRef tcs_out_layout
;
89 LLVMValueRef tcs_in_layout
;
91 LLVMValueRef tess_factor_offset
;
92 LLVMValueRef tcs_patch_id
;
93 LLVMValueRef tcs_rel_ids
;
94 LLVMValueRef tes_rel_patch_id
;
95 LLVMValueRef tes_patch_id
;
99 LLVMValueRef gsvs_ring_stride
;
100 LLVMValueRef gsvs_num_entries
;
101 LLVMValueRef gs2vs_offset
;
102 LLVMValueRef gs_wave_id
;
103 LLVMValueRef gs_vtx_offset
[6];
104 LLVMValueRef gs_prim_id
, gs_invocation_id
;
106 LLVMValueRef esgs_ring
;
107 LLVMValueRef gsvs_ring
;
108 LLVMValueRef hs_ring_tess_offchip
;
109 LLVMValueRef hs_ring_tess_factor
;
111 LLVMValueRef prim_mask
;
112 LLVMValueRef sample_positions
;
113 LLVMValueRef persp_sample
, persp_center
, persp_centroid
;
114 LLVMValueRef linear_sample
, linear_center
, linear_centroid
;
115 LLVMValueRef front_face
;
116 LLVMValueRef ancillary
;
117 LLVMValueRef sample_coverage
;
118 LLVMValueRef frag_pos
[4];
120 LLVMBasicBlockRef continue_block
;
121 LLVMBasicBlockRef break_block
;
141 LLVMValueRef i1false
;
142 LLVMValueRef i32zero
;
144 LLVMValueRef f32zero
;
146 LLVMValueRef v4f32empty
;
148 unsigned uniform_md_kind
;
149 LLVMValueRef empty_md
;
150 gl_shader_stage stage
;
153 LLVMValueRef inputs
[RADEON_LLVM_MAX_INPUTS
* 4];
154 LLVMValueRef outputs
[RADEON_LLVM_MAX_OUTPUTS
* 4];
156 LLVMValueRef shared_memory
;
158 uint64_t output_mask
;
160 LLVMValueRef
*locals
;
162 uint8_t num_output_clips
;
163 uint8_t num_output_culls
;
165 bool has_ds_bpermute
;
167 bool is_gs_copy_shader
;
168 LLVMValueRef gs_next_vertex
;
169 unsigned gs_max_out_vertices
;
172 static LLVMValueRef
get_sampler_desc(struct nir_to_llvm_context
*ctx
,
173 nir_deref_var
*deref
,
174 enum desc_type desc_type
);
175 static unsigned radeon_llvm_reg_index_soa(unsigned index
, unsigned chan
)
177 return (index
* 4) + chan
;
180 static unsigned shader_io_get_unique_index(gl_varying_slot slot
)
182 /* handle patch indices separate */
183 if (slot
== VARYING_SLOT_TESS_LEVEL_OUTER
)
185 if (slot
== VARYING_SLOT_TESS_LEVEL_INNER
)
187 if (slot
>= VARYING_SLOT_PATCH0
&& slot
<= VARYING_SLOT_TESS_MAX
)
188 return 2 + (slot
- VARYING_SLOT_PATCH0
);
190 if (slot
== VARYING_SLOT_POS
)
192 if (slot
== VARYING_SLOT_PSIZ
)
194 if (slot
== VARYING_SLOT_CLIP_DIST0
)
196 /* 3 is reserved for clip dist as well */
197 if (slot
>= VARYING_SLOT_VAR0
&& slot
<= VARYING_SLOT_VAR31
)
198 return 4 + (slot
- VARYING_SLOT_VAR0
);
199 unreachable("illegal slot in get unique index\n");
202 static unsigned llvm_get_type_size(LLVMTypeRef type
)
204 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
207 case LLVMIntegerTypeKind
:
208 return LLVMGetIntTypeWidth(type
) / 8;
209 case LLVMFloatTypeKind
:
211 case LLVMPointerTypeKind
:
213 case LLVMVectorTypeKind
:
214 return LLVMGetVectorSize(type
) *
215 llvm_get_type_size(LLVMGetElementType(type
));
222 static void set_llvm_calling_convention(LLVMValueRef func
,
223 gl_shader_stage stage
)
225 enum radeon_llvm_calling_convention calling_conv
;
228 case MESA_SHADER_VERTEX
:
229 case MESA_SHADER_TESS_CTRL
:
230 case MESA_SHADER_TESS_EVAL
:
231 calling_conv
= RADEON_LLVM_AMDGPU_VS
;
233 case MESA_SHADER_GEOMETRY
:
234 calling_conv
= RADEON_LLVM_AMDGPU_GS
;
236 case MESA_SHADER_FRAGMENT
:
237 calling_conv
= RADEON_LLVM_AMDGPU_PS
;
239 case MESA_SHADER_COMPUTE
:
240 calling_conv
= RADEON_LLVM_AMDGPU_CS
;
243 unreachable("Unhandle shader type");
246 LLVMSetFunctionCallConv(func
, calling_conv
);
250 create_llvm_function(LLVMContextRef ctx
, LLVMModuleRef module
,
251 LLVMBuilderRef builder
, LLVMTypeRef
*return_types
,
252 unsigned num_return_elems
, LLVMTypeRef
*param_types
,
253 unsigned param_count
, unsigned array_params_mask
,
254 unsigned sgpr_params
, bool unsafe_math
)
256 LLVMTypeRef main_function_type
, ret_type
;
257 LLVMBasicBlockRef main_function_body
;
259 if (num_return_elems
)
260 ret_type
= LLVMStructTypeInContext(ctx
, return_types
,
261 num_return_elems
, true);
263 ret_type
= LLVMVoidTypeInContext(ctx
);
265 /* Setup the function */
267 LLVMFunctionType(ret_type
, param_types
, param_count
, 0);
268 LLVMValueRef main_function
=
269 LLVMAddFunction(module
, "main", main_function_type
);
271 LLVMAppendBasicBlockInContext(ctx
, main_function
, "main_body");
272 LLVMPositionBuilderAtEnd(builder
, main_function_body
);
274 LLVMSetFunctionCallConv(main_function
, RADEON_LLVM_AMDGPU_CS
);
275 for (unsigned i
= 0; i
< sgpr_params
; ++i
) {
276 if (array_params_mask
& (1 << i
)) {
277 LLVMValueRef P
= LLVMGetParam(main_function
, i
);
278 ac_add_function_attr(ctx
, main_function
, i
+ 1, AC_FUNC_ATTR_BYVAL
);
279 ac_add_attr_dereferenceable(P
, UINT64_MAX
);
282 ac_add_function_attr(ctx
, main_function
, i
+ 1, AC_FUNC_ATTR_INREG
);
287 /* These were copied from some LLVM test. */
288 LLVMAddTargetDependentFunctionAttr(main_function
,
289 "less-precise-fpmad",
291 LLVMAddTargetDependentFunctionAttr(main_function
,
294 LLVMAddTargetDependentFunctionAttr(main_function
,
297 LLVMAddTargetDependentFunctionAttr(main_function
,
301 return main_function
;
304 static LLVMTypeRef
const_array(LLVMTypeRef elem_type
, int num_elements
)
306 return LLVMPointerType(LLVMArrayType(elem_type
, num_elements
),
310 static LLVMValueRef
get_shared_memory_ptr(struct nir_to_llvm_context
*ctx
,
318 offset
= LLVMConstInt(ctx
->i32
, idx
* 16, false);
320 ptr
= ctx
->shared_memory
;
321 ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &offset
, 1, "");
322 addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
323 ptr
= LLVMBuildBitCast(ctx
->builder
, ptr
, LLVMPointerType(type
, addr_space
), "");
327 static LLVMTypeRef
to_integer_type_scalar(struct nir_to_llvm_context
*ctx
, LLVMTypeRef t
)
329 if (t
== ctx
->f16
|| t
== ctx
->i16
)
331 else if (t
== ctx
->f32
|| t
== ctx
->i32
)
333 else if (t
== ctx
->f64
|| t
== ctx
->i64
)
336 unreachable("Unhandled integer size");
339 static LLVMTypeRef
to_integer_type(struct nir_to_llvm_context
*ctx
, LLVMTypeRef t
)
341 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
342 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
343 return LLVMVectorType(to_integer_type_scalar(ctx
, elem_type
),
344 LLVMGetVectorSize(t
));
346 return to_integer_type_scalar(ctx
, t
);
349 static LLVMValueRef
to_integer(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
)
351 LLVMTypeRef type
= LLVMTypeOf(v
);
352 return LLVMBuildBitCast(ctx
->builder
, v
, to_integer_type(ctx
, type
), "");
355 static LLVMTypeRef
to_float_type_scalar(struct nir_to_llvm_context
*ctx
, LLVMTypeRef t
)
357 if (t
== ctx
->i16
|| t
== ctx
->f16
)
359 else if (t
== ctx
->i32
|| t
== ctx
->f32
)
361 else if (t
== ctx
->i64
|| t
== ctx
->f64
)
364 unreachable("Unhandled float size");
367 static LLVMTypeRef
to_float_type(struct nir_to_llvm_context
*ctx
, LLVMTypeRef t
)
369 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
370 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
371 return LLVMVectorType(to_float_type_scalar(ctx
, elem_type
),
372 LLVMGetVectorSize(t
));
374 return to_float_type_scalar(ctx
, t
);
377 static LLVMValueRef
to_float(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
)
379 LLVMTypeRef type
= LLVMTypeOf(v
);
380 return LLVMBuildBitCast(ctx
->builder
, v
, to_float_type(ctx
, type
), "");
383 static int get_elem_bits(struct nir_to_llvm_context
*ctx
, LLVMTypeRef type
)
385 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
386 type
= LLVMGetElementType(type
);
388 if (LLVMGetTypeKind(type
) == LLVMIntegerTypeKind
)
389 return LLVMGetIntTypeWidth(type
);
391 if (type
== ctx
->f16
)
393 if (type
== ctx
->f32
)
395 if (type
== ctx
->f64
)
398 unreachable("Unhandled type kind in get_elem_bits");
401 static LLVMValueRef
unpack_param(struct nir_to_llvm_context
*ctx
,
402 LLVMValueRef param
, unsigned rshift
,
405 LLVMValueRef value
= param
;
407 value
= LLVMBuildLShr(ctx
->builder
, value
,
408 LLVMConstInt(ctx
->i32
, rshift
, false), "");
410 if (rshift
+ bitwidth
< 32) {
411 unsigned mask
= (1 << bitwidth
) - 1;
412 value
= LLVMBuildAnd(ctx
->builder
, value
,
413 LLVMConstInt(ctx
->i32
, mask
, false), "");
418 static void set_userdata_location(struct ac_userdata_info
*ud_info
, uint8_t sgpr_idx
, uint8_t num_sgprs
)
420 ud_info
->sgpr_idx
= sgpr_idx
;
421 ud_info
->num_sgprs
= num_sgprs
;
422 ud_info
->indirect
= false;
423 ud_info
->indirect_offset
= 0;
426 static void set_userdata_location_shader(struct nir_to_llvm_context
*ctx
,
427 int idx
, uint8_t sgpr_idx
, uint8_t num_sgprs
)
429 set_userdata_location(&ctx
->shader_info
->user_sgprs_locs
.shader_data
[idx
], sgpr_idx
, num_sgprs
);
433 static void set_userdata_location_indirect(struct ac_userdata_info
*ud_info
, uint8_t sgpr_idx
, uint8_t num_sgprs
,
434 uint32_t indirect_offset
)
436 ud_info
->sgpr_idx
= sgpr_idx
;
437 ud_info
->num_sgprs
= num_sgprs
;
438 ud_info
->indirect
= true;
439 ud_info
->indirect_offset
= indirect_offset
;
443 static void create_function(struct nir_to_llvm_context
*ctx
)
445 LLVMTypeRef arg_types
[23];
446 unsigned arg_idx
= 0;
447 unsigned array_params_mask
= 0;
448 unsigned sgpr_count
= 0, user_sgpr_count
;
450 unsigned num_sets
= ctx
->options
->layout
? ctx
->options
->layout
->num_sets
: 0;
451 unsigned user_sgpr_idx
;
452 bool need_push_constants
;
453 bool need_ring_offsets
= false;
455 /* until we sort out scratch/global buffers always assign ring offsets for gs/vs/es */
456 if (ctx
->stage
== MESA_SHADER_GEOMETRY
||
457 ctx
->stage
== MESA_SHADER_VERTEX
||
458 ctx
->stage
== MESA_SHADER_TESS_CTRL
||
459 ctx
->stage
== MESA_SHADER_TESS_EVAL
||
460 ctx
->is_gs_copy_shader
)
461 need_ring_offsets
= true;
463 need_push_constants
= true;
464 if (!ctx
->options
->layout
)
465 need_push_constants
= false;
466 else if (!ctx
->options
->layout
->push_constant_size
&&
467 !ctx
->options
->layout
->dynamic_offset_count
)
468 need_push_constants
= false;
470 if (need_ring_offsets
&& !ctx
->options
->supports_spill
) {
471 arg_types
[arg_idx
++] = const_array(ctx
->v16i8
, 8); /* address of rings */
474 /* 1 for each descriptor set */
475 for (unsigned i
= 0; i
< num_sets
; ++i
) {
476 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& (1 << ctx
->stage
)) {
477 array_params_mask
|= (1 << arg_idx
);
478 arg_types
[arg_idx
++] = const_array(ctx
->i8
, 1024 * 1024);
482 if (need_push_constants
) {
483 /* 1 for push constants and dynamic descriptors */
484 array_params_mask
|= (1 << arg_idx
);
485 arg_types
[arg_idx
++] = const_array(ctx
->i8
, 1024 * 1024);
488 switch (ctx
->stage
) {
489 case MESA_SHADER_COMPUTE
:
490 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3); /* grid size */
491 user_sgpr_count
= arg_idx
;
492 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3);
493 arg_types
[arg_idx
++] = ctx
->i32
;
494 sgpr_count
= arg_idx
;
496 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3);
498 case MESA_SHADER_VERTEX
:
499 if (!ctx
->is_gs_copy_shader
) {
500 arg_types
[arg_idx
++] = const_array(ctx
->v16i8
, 16); /* vertex buffers */
501 arg_types
[arg_idx
++] = ctx
->i32
; // base vertex
502 arg_types
[arg_idx
++] = ctx
->i32
; // start instance
503 arg_types
[arg_idx
++] = ctx
->i32
; // draw index
505 user_sgpr_count
= arg_idx
;
506 if (ctx
->options
->key
.vs
.as_es
)
507 arg_types
[arg_idx
++] = ctx
->i32
; //es2gs offset
508 else if (ctx
->options
->key
.vs
.as_ls
) {
509 arg_types
[arg_idx
++] = ctx
->i32
; //ls out layout
512 sgpr_count
= arg_idx
;
513 arg_types
[arg_idx
++] = ctx
->i32
; // vertex id
514 if (!ctx
->is_gs_copy_shader
) {
515 arg_types
[arg_idx
++] = ctx
->i32
; // rel auto id
516 arg_types
[arg_idx
++] = ctx
->i32
; // vs prim id
517 arg_types
[arg_idx
++] = ctx
->i32
; // instance id
520 case MESA_SHADER_TESS_CTRL
:
521 arg_types
[arg_idx
++] = ctx
->i32
; // tcs offchip layout
522 arg_types
[arg_idx
++] = ctx
->i32
; // tcs out offsets
523 arg_types
[arg_idx
++] = ctx
->i32
; // tcs out layout
524 arg_types
[arg_idx
++] = ctx
->i32
; // tcs in layout
525 user_sgpr_count
= arg_idx
;
526 arg_types
[arg_idx
++] = ctx
->i32
; // param oc lds
527 arg_types
[arg_idx
++] = ctx
->i32
; // tess factor offset
528 sgpr_count
= arg_idx
;
529 arg_types
[arg_idx
++] = ctx
->i32
; // patch id
530 arg_types
[arg_idx
++] = ctx
->i32
; // rel ids;
532 case MESA_SHADER_TESS_EVAL
:
533 arg_types
[arg_idx
++] = ctx
->i32
; // tcs offchip layout
534 user_sgpr_count
= arg_idx
;
535 if (ctx
->options
->key
.tes
.as_es
) {
536 arg_types
[arg_idx
++] = ctx
->i32
; // OC LDS
537 arg_types
[arg_idx
++] = ctx
->i32
; //
538 arg_types
[arg_idx
++] = ctx
->i32
; // es2gs offset
540 arg_types
[arg_idx
++] = ctx
->i32
; //
541 arg_types
[arg_idx
++] = ctx
->i32
; // OC LDS
543 sgpr_count
= arg_idx
;
544 arg_types
[arg_idx
++] = ctx
->f32
; // tes_u
545 arg_types
[arg_idx
++] = ctx
->f32
; // tes_v
546 arg_types
[arg_idx
++] = ctx
->i32
; // tes rel patch id
547 arg_types
[arg_idx
++] = ctx
->i32
; // tes patch id
549 case MESA_SHADER_GEOMETRY
:
550 arg_types
[arg_idx
++] = ctx
->i32
; // gsvs stride
551 arg_types
[arg_idx
++] = ctx
->i32
; // gsvs num entires
552 user_sgpr_count
= arg_idx
;
553 arg_types
[arg_idx
++] = ctx
->i32
; // gs2vs offset
554 arg_types
[arg_idx
++] = ctx
->i32
; // wave id
555 sgpr_count
= arg_idx
;
556 arg_types
[arg_idx
++] = ctx
->i32
; // vtx0
557 arg_types
[arg_idx
++] = ctx
->i32
; // vtx1
558 arg_types
[arg_idx
++] = ctx
->i32
; // prim id
559 arg_types
[arg_idx
++] = ctx
->i32
; // vtx2
560 arg_types
[arg_idx
++] = ctx
->i32
; // vtx3
561 arg_types
[arg_idx
++] = ctx
->i32
; // vtx4
562 arg_types
[arg_idx
++] = ctx
->i32
; // vtx5
563 arg_types
[arg_idx
++] = ctx
->i32
; // GS instance id
565 case MESA_SHADER_FRAGMENT
:
566 arg_types
[arg_idx
++] = const_array(ctx
->f32
, 32); /* sample positions */
567 user_sgpr_count
= arg_idx
;
568 arg_types
[arg_idx
++] = ctx
->i32
; /* prim mask */
569 sgpr_count
= arg_idx
;
570 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp sample */
571 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp center */
572 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp centroid */
573 arg_types
[arg_idx
++] = ctx
->v3i32
; /* persp pull model */
574 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear sample */
575 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear center */
576 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear centroid */
577 arg_types
[arg_idx
++] = ctx
->f32
; /* line stipple tex */
578 arg_types
[arg_idx
++] = ctx
->f32
; /* pos x float */
579 arg_types
[arg_idx
++] = ctx
->f32
; /* pos y float */
580 arg_types
[arg_idx
++] = ctx
->f32
; /* pos z float */
581 arg_types
[arg_idx
++] = ctx
->f32
; /* pos w float */
582 arg_types
[arg_idx
++] = ctx
->i32
; /* front face */
583 arg_types
[arg_idx
++] = ctx
->i32
; /* ancillary */
584 arg_types
[arg_idx
++] = ctx
->i32
; /* sample coverage */
585 arg_types
[arg_idx
++] = ctx
->i32
; /* fixed pt */
588 unreachable("Shader stage not implemented");
591 ctx
->main_function
= create_llvm_function(
592 ctx
->context
, ctx
->module
, ctx
->builder
, NULL
, 0, arg_types
,
593 arg_idx
, array_params_mask
, sgpr_count
, ctx
->options
->unsafe_math
);
594 set_llvm_calling_convention(ctx
->main_function
, ctx
->stage
);
596 ctx
->shader_info
->num_input_sgprs
= 0;
597 ctx
->shader_info
->num_input_vgprs
= 0;
599 ctx
->shader_info
->num_user_sgprs
= ctx
->options
->supports_spill
? 2 : 0;
600 for (i
= 0; i
< user_sgpr_count
; i
++)
601 ctx
->shader_info
->num_user_sgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
603 ctx
->shader_info
->num_input_sgprs
= ctx
->shader_info
->num_user_sgprs
;
604 for (; i
< sgpr_count
; i
++)
605 ctx
->shader_info
->num_input_sgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
607 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
608 for (; i
< arg_idx
; ++i
)
609 ctx
->shader_info
->num_input_vgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
614 if (ctx
->options
->supports_spill
|| need_ring_offsets
) {
615 set_userdata_location_shader(ctx
, AC_UD_SCRATCH_RING_OFFSETS
, user_sgpr_idx
, 2);
617 if (ctx
->options
->supports_spill
) {
618 ctx
->ring_offsets
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.implicit.buffer.ptr",
619 LLVMPointerType(ctx
->i8
, CONST_ADDR_SPACE
),
620 NULL
, 0, AC_FUNC_ATTR_READNONE
);
621 ctx
->ring_offsets
= LLVMBuildBitCast(ctx
->builder
, ctx
->ring_offsets
,
622 const_array(ctx
->v16i8
, 8), "");
624 ctx
->ring_offsets
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
627 for (unsigned i
= 0; i
< num_sets
; ++i
) {
628 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& (1 << ctx
->stage
)) {
629 set_userdata_location(&ctx
->shader_info
->user_sgprs_locs
.descriptor_sets
[i
], user_sgpr_idx
, 2);
631 ctx
->descriptor_sets
[i
] =
632 LLVMGetParam(ctx
->main_function
, arg_idx
++);
634 ctx
->descriptor_sets
[i
] = NULL
;
637 if (need_push_constants
) {
638 ctx
->push_constants
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
639 set_userdata_location_shader(ctx
, AC_UD_PUSH_CONSTANTS
, user_sgpr_idx
, 2);
643 switch (ctx
->stage
) {
644 case MESA_SHADER_COMPUTE
:
645 set_userdata_location_shader(ctx
, AC_UD_CS_GRID_SIZE
, user_sgpr_idx
, 3);
647 ctx
->num_work_groups
=
648 LLVMGetParam(ctx
->main_function
, arg_idx
++);
650 LLVMGetParam(ctx
->main_function
, arg_idx
++);
652 LLVMGetParam(ctx
->main_function
, arg_idx
++);
653 ctx
->local_invocation_ids
=
654 LLVMGetParam(ctx
->main_function
, arg_idx
++);
656 case MESA_SHADER_VERTEX
:
657 if (!ctx
->is_gs_copy_shader
) {
658 set_userdata_location_shader(ctx
, AC_UD_VS_VERTEX_BUFFERS
, user_sgpr_idx
, 2);
660 ctx
->vertex_buffers
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
661 set_userdata_location_shader(ctx
, AC_UD_VS_BASE_VERTEX_START_INSTANCE
, user_sgpr_idx
, 3);
663 ctx
->base_vertex
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
664 ctx
->start_instance
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
665 ctx
->draw_index
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
667 if (ctx
->options
->key
.vs
.as_es
)
668 ctx
->es2gs_offset
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
669 else if (ctx
->options
->key
.vs
.as_ls
) {
670 set_userdata_location_shader(ctx
, AC_UD_VS_LS_TCS_IN_LAYOUT
, user_sgpr_idx
, 1);
672 ctx
->ls_out_layout
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
674 ctx
->vertex_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
675 if (!ctx
->is_gs_copy_shader
) {
676 ctx
->rel_auto_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
677 ctx
->vs_prim_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
678 ctx
->instance_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
681 case MESA_SHADER_TESS_CTRL
:
682 set_userdata_location_shader(ctx
, AC_UD_TCS_OFFCHIP_LAYOUT
, user_sgpr_idx
, 4);
684 ctx
->tcs_offchip_layout
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
685 ctx
->tcs_out_offsets
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
686 ctx
->tcs_out_layout
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
687 ctx
->tcs_in_layout
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
688 ctx
->oc_lds
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
689 ctx
->tess_factor_offset
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
690 ctx
->tcs_patch_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
691 ctx
->tcs_rel_ids
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
693 case MESA_SHADER_TESS_EVAL
:
694 set_userdata_location_shader(ctx
, AC_UD_TES_OFFCHIP_LAYOUT
, user_sgpr_idx
, 1);
696 ctx
->tcs_offchip_layout
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
697 if (ctx
->options
->key
.tes
.as_es
) {
698 ctx
->oc_lds
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
700 ctx
->es2gs_offset
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
703 ctx
->oc_lds
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
705 ctx
->tes_u
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
706 ctx
->tes_v
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
707 ctx
->tes_rel_patch_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
708 ctx
->tes_patch_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
710 case MESA_SHADER_GEOMETRY
:
711 set_userdata_location_shader(ctx
, AC_UD_GS_VS_RING_STRIDE_ENTRIES
, user_sgpr_idx
, 2);
713 ctx
->gsvs_ring_stride
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
714 ctx
->gsvs_num_entries
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
715 ctx
->gs2vs_offset
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
716 ctx
->gs_wave_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
717 ctx
->gs_vtx_offset
[0] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
718 ctx
->gs_vtx_offset
[1] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
719 ctx
->gs_prim_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
720 ctx
->gs_vtx_offset
[2] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
721 ctx
->gs_vtx_offset
[3] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
722 ctx
->gs_vtx_offset
[4] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
723 ctx
->gs_vtx_offset
[5] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
724 ctx
->gs_invocation_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
726 case MESA_SHADER_FRAGMENT
:
727 set_userdata_location_shader(ctx
, AC_UD_PS_SAMPLE_POS
, user_sgpr_idx
, 2);
729 ctx
->sample_positions
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
730 ctx
->prim_mask
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
731 ctx
->persp_sample
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
732 ctx
->persp_center
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
733 ctx
->persp_centroid
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
735 ctx
->linear_sample
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
736 ctx
->linear_center
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
737 ctx
->linear_centroid
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
738 arg_idx
++; /* line stipple */
739 ctx
->frag_pos
[0] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
740 ctx
->frag_pos
[1] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
741 ctx
->frag_pos
[2] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
742 ctx
->frag_pos
[3] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
743 ctx
->front_face
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
744 ctx
->ancillary
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
745 ctx
->sample_coverage
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
748 unreachable("Shader stage not implemented");
752 static void setup_types(struct nir_to_llvm_context
*ctx
)
754 LLVMValueRef args
[4];
756 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->context
);
757 ctx
->i1
= LLVMIntTypeInContext(ctx
->context
, 1);
758 ctx
->i8
= LLVMIntTypeInContext(ctx
->context
, 8);
759 ctx
->i16
= LLVMIntTypeInContext(ctx
->context
, 16);
760 ctx
->i32
= LLVMIntTypeInContext(ctx
->context
, 32);
761 ctx
->i64
= LLVMIntTypeInContext(ctx
->context
, 64);
762 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
763 ctx
->v3i32
= LLVMVectorType(ctx
->i32
, 3);
764 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
765 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
766 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
767 ctx
->f16
= LLVMHalfTypeInContext(ctx
->context
);
768 ctx
->f64
= LLVMDoubleTypeInContext(ctx
->context
);
769 ctx
->v2f32
= LLVMVectorType(ctx
->f32
, 2);
770 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
771 ctx
->v16i8
= LLVMVectorType(ctx
->i8
, 16);
773 ctx
->i1false
= LLVMConstInt(ctx
->i1
, 0, false);
774 ctx
->i1true
= LLVMConstInt(ctx
->i1
, 1, false);
775 ctx
->i32zero
= LLVMConstInt(ctx
->i32
, 0, false);
776 ctx
->i32one
= LLVMConstInt(ctx
->i32
, 1, false);
777 ctx
->f32zero
= LLVMConstReal(ctx
->f32
, 0.0);
778 ctx
->f32one
= LLVMConstReal(ctx
->f32
, 1.0);
780 args
[0] = ctx
->f32zero
;
781 args
[1] = ctx
->f32zero
;
782 args
[2] = ctx
->f32zero
;
783 args
[3] = ctx
->f32one
;
784 ctx
->v4f32empty
= LLVMConstVector(args
, 4);
786 ctx
->uniform_md_kind
=
787 LLVMGetMDKindIDInContext(ctx
->context
, "amdgpu.uniform", 14);
788 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
790 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
793 static int get_llvm_num_components(LLVMValueRef value
)
795 LLVMTypeRef type
= LLVMTypeOf(value
);
796 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
797 ? LLVMGetVectorSize(type
)
799 return num_components
;
802 static LLVMValueRef
llvm_extract_elem(struct nir_to_llvm_context
*ctx
,
806 int count
= get_llvm_num_components(value
);
808 assert(index
< count
);
812 return LLVMBuildExtractElement(ctx
->builder
, value
,
813 LLVMConstInt(ctx
->i32
, index
, false), "");
816 static LLVMValueRef
trim_vector(struct nir_to_llvm_context
*ctx
,
817 LLVMValueRef value
, unsigned count
)
819 unsigned num_components
= get_llvm_num_components(value
);
820 if (count
== num_components
)
823 LLVMValueRef masks
[] = {
824 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
825 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false)};
828 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
831 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
832 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
836 build_store_values_extended(struct nir_to_llvm_context
*ctx
,
837 LLVMValueRef
*values
,
838 unsigned value_count
,
839 unsigned value_stride
,
842 LLVMBuilderRef builder
= ctx
->builder
;
845 if (value_count
== 1) {
846 LLVMBuildStore(builder
, vec
, values
[0]);
850 for (i
= 0; i
< value_count
; i
++) {
851 LLVMValueRef ptr
= values
[i
* value_stride
];
852 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
853 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
854 LLVMBuildStore(builder
, value
, ptr
);
858 static LLVMTypeRef
get_def_type(struct nir_to_llvm_context
*ctx
,
861 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->context
, def
->bit_size
);
862 if (def
->num_components
> 1) {
863 type
= LLVMVectorType(type
, def
->num_components
);
868 static LLVMValueRef
get_src(struct nir_to_llvm_context
*ctx
, nir_src src
)
871 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->defs
, src
.ssa
);
872 return (LLVMValueRef
)entry
->data
;
876 static LLVMBasicBlockRef
get_block(struct nir_to_llvm_context
*ctx
,
879 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->defs
, b
);
880 return (LLVMBasicBlockRef
)entry
->data
;
883 static LLVMValueRef
get_alu_src(struct nir_to_llvm_context
*ctx
,
885 unsigned num_components
)
887 LLVMValueRef value
= get_src(ctx
, src
.src
);
888 bool need_swizzle
= false;
891 LLVMTypeRef type
= LLVMTypeOf(value
);
892 unsigned src_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
893 ? LLVMGetVectorSize(type
)
896 for (unsigned i
= 0; i
< num_components
; ++i
) {
897 assert(src
.swizzle
[i
] < src_components
);
898 if (src
.swizzle
[i
] != i
)
902 if (need_swizzle
|| num_components
!= src_components
) {
903 LLVMValueRef masks
[] = {
904 LLVMConstInt(ctx
->i32
, src
.swizzle
[0], false),
905 LLVMConstInt(ctx
->i32
, src
.swizzle
[1], false),
906 LLVMConstInt(ctx
->i32
, src
.swizzle
[2], false),
907 LLVMConstInt(ctx
->i32
, src
.swizzle
[3], false)};
909 if (src_components
> 1 && num_components
== 1) {
910 value
= LLVMBuildExtractElement(ctx
->builder
, value
,
912 } else if (src_components
== 1 && num_components
> 1) {
913 LLVMValueRef values
[] = {value
, value
, value
, value
};
914 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
916 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
917 value
= LLVMBuildShuffleVector(ctx
->builder
, value
, value
,
926 static LLVMValueRef
emit_int_cmp(struct nir_to_llvm_context
*ctx
,
927 LLVMIntPredicate pred
, LLVMValueRef src0
,
930 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
931 return LLVMBuildSelect(ctx
->builder
, result
,
932 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
933 LLVMConstInt(ctx
->i32
, 0, false), "");
936 static LLVMValueRef
emit_float_cmp(struct nir_to_llvm_context
*ctx
,
937 LLVMRealPredicate pred
, LLVMValueRef src0
,
941 src0
= to_float(ctx
, src0
);
942 src1
= to_float(ctx
, src1
);
943 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
944 return LLVMBuildSelect(ctx
->builder
, result
,
945 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
946 LLVMConstInt(ctx
->i32
, 0, false), "");
949 static LLVMValueRef
emit_intrin_1f_param(struct nir_to_llvm_context
*ctx
,
951 LLVMTypeRef result_type
,
955 LLVMValueRef params
[] = {
959 sprintf(name
, "%s.f%d", intrin
, get_elem_bits(ctx
, result_type
));
960 return ac_build_intrinsic(&ctx
->ac
, name
, result_type
, params
, 1, AC_FUNC_ATTR_READNONE
);
963 static LLVMValueRef
emit_intrin_2f_param(struct nir_to_llvm_context
*ctx
,
965 LLVMTypeRef result_type
,
966 LLVMValueRef src0
, LLVMValueRef src1
)
969 LLVMValueRef params
[] = {
974 sprintf(name
, "%s.f%d", intrin
, get_elem_bits(ctx
, result_type
));
975 return ac_build_intrinsic(&ctx
->ac
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
978 static LLVMValueRef
emit_intrin_3f_param(struct nir_to_llvm_context
*ctx
,
980 LLVMTypeRef result_type
,
981 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
984 LLVMValueRef params
[] = {
990 sprintf(name
, "%s.f%d", intrin
, get_elem_bits(ctx
, result_type
));
991 return ac_build_intrinsic(&ctx
->ac
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
994 static LLVMValueRef
emit_bcsel(struct nir_to_llvm_context
*ctx
,
995 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
997 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
999 return LLVMBuildSelect(ctx
->builder
, v
, src1
, src2
, "");
1002 static LLVMValueRef
emit_find_lsb(struct nir_to_llvm_context
*ctx
,
1005 LLVMValueRef params
[2] = {
1008 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
1009 * add special code to check for x=0. The reason is that
1010 * the LLVM behavior for x=0 is different from what we
1013 * The hardware already implements the correct behavior.
1015 LLVMConstInt(ctx
->i32
, 1, false),
1017 return ac_build_intrinsic(&ctx
->ac
, "llvm.cttz.i32", ctx
->i32
, params
, 2, AC_FUNC_ATTR_READNONE
);
1020 static LLVMValueRef
emit_ifind_msb(struct nir_to_llvm_context
*ctx
,
1023 return ac_build_imsb(&ctx
->ac
, src0
, ctx
->i32
);
1026 static LLVMValueRef
emit_ufind_msb(struct nir_to_llvm_context
*ctx
,
1029 return ac_build_umsb(&ctx
->ac
, src0
, ctx
->i32
);
1032 static LLVMValueRef
emit_minmax_int(struct nir_to_llvm_context
*ctx
,
1033 LLVMIntPredicate pred
,
1034 LLVMValueRef src0
, LLVMValueRef src1
)
1036 return LLVMBuildSelect(ctx
->builder
,
1037 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
1042 static LLVMValueRef
emit_iabs(struct nir_to_llvm_context
*ctx
,
1045 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
1046 LLVMBuildNeg(ctx
->builder
, src0
, ""));
1049 static LLVMValueRef
emit_fsign(struct nir_to_llvm_context
*ctx
,
1052 LLVMValueRef cmp
, val
;
1054 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, ctx
->f32zero
, "");
1055 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->f32one
, src0
, "");
1056 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, ctx
->f32zero
, "");
1057 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(ctx
->f32
, -1.0), "");
1061 static LLVMValueRef
emit_isign(struct nir_to_llvm_context
*ctx
,
1064 LLVMValueRef cmp
, val
;
1066 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, ctx
->i32zero
, "");
1067 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->i32one
, src0
, "");
1068 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, ctx
->i32zero
, "");
1069 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(ctx
->i32
, -1, true), "");
1073 static LLVMValueRef
emit_ffract(struct nir_to_llvm_context
*ctx
,
1076 const char *intr
= "llvm.floor.f32";
1077 LLVMValueRef fsrc0
= to_float(ctx
, src0
);
1078 LLVMValueRef params
[] = {
1081 LLVMValueRef floor
= ac_build_intrinsic(&ctx
->ac
, intr
,
1082 ctx
->f32
, params
, 1,
1083 AC_FUNC_ATTR_READNONE
);
1084 return LLVMBuildFSub(ctx
->builder
, fsrc0
, floor
, "");
1087 static LLVMValueRef
emit_uint_carry(struct nir_to_llvm_context
*ctx
,
1089 LLVMValueRef src0
, LLVMValueRef src1
)
1091 LLVMTypeRef ret_type
;
1092 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
1094 LLVMValueRef params
[] = { src0
, src1
};
1095 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
1098 res
= ac_build_intrinsic(&ctx
->ac
, intrin
, ret_type
,
1099 params
, 2, AC_FUNC_ATTR_READNONE
);
1101 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
1102 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
1106 static LLVMValueRef
emit_b2f(struct nir_to_llvm_context
*ctx
,
1109 return LLVMBuildAnd(ctx
->builder
, src0
, LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""), "");
1112 static LLVMValueRef
emit_umul_high(struct nir_to_llvm_context
*ctx
,
1113 LLVMValueRef src0
, LLVMValueRef src1
)
1115 LLVMValueRef dst64
, result
;
1116 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
1117 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
1119 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1120 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1121 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1125 static LLVMValueRef
emit_imul_high(struct nir_to_llvm_context
*ctx
,
1126 LLVMValueRef src0
, LLVMValueRef src1
)
1128 LLVMValueRef dst64
, result
;
1129 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
1130 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
1132 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1133 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1134 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1138 static LLVMValueRef
emit_bitfield_extract(struct nir_to_llvm_context
*ctx
,
1140 LLVMValueRef srcs
[3])
1142 LLVMValueRef result
;
1143 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
1145 result
= ac_build_bfe(&ctx
->ac
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
1146 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
1150 static LLVMValueRef
emit_bitfield_insert(struct nir_to_llvm_context
*ctx
,
1151 LLVMValueRef src0
, LLVMValueRef src1
,
1152 LLVMValueRef src2
, LLVMValueRef src3
)
1154 LLVMValueRef bfi_args
[3], result
;
1156 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
1157 LLVMBuildSub(ctx
->builder
,
1158 LLVMBuildShl(ctx
->builder
,
1163 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
1166 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
1169 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
1170 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
1172 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
1173 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
1174 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
1176 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
1180 static LLVMValueRef
emit_pack_half_2x16(struct nir_to_llvm_context
*ctx
,
1183 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1185 LLVMValueRef comp
[2];
1187 src0
= to_float(ctx
, src0
);
1188 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32zero
, "");
1189 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32one
, "");
1190 for (i
= 0; i
< 2; i
++) {
1191 comp
[i
] = LLVMBuildFPTrunc(ctx
->builder
, comp
[i
], ctx
->f16
, "");
1192 comp
[i
] = LLVMBuildBitCast(ctx
->builder
, comp
[i
], ctx
->i16
, "");
1193 comp
[i
] = LLVMBuildZExt(ctx
->builder
, comp
[i
], ctx
->i32
, "");
1196 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
1197 comp
[0] = LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
1202 static LLVMValueRef
emit_unpack_half_2x16(struct nir_to_llvm_context
*ctx
,
1205 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1206 LLVMValueRef temps
[2], result
, val
;
1209 for (i
= 0; i
< 2; i
++) {
1210 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
1211 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
1212 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
1213 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
1216 result
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), temps
[0],
1218 result
= LLVMBuildInsertElement(ctx
->builder
, result
, temps
[1],
1223 static LLVMValueRef
emit_ddxy(struct nir_to_llvm_context
*ctx
,
1229 LLVMValueRef result
;
1230 ctx
->has_ddxy
= true;
1232 if (!ctx
->lds
&& !ctx
->has_ds_bpermute
)
1233 ctx
->lds
= LLVMAddGlobalInAddressSpace(ctx
->module
,
1234 LLVMArrayType(ctx
->i32
, 64),
1235 "ddxy_lds", LOCAL_ADDR_SPACE
);
1237 if (op
== nir_op_fddx_fine
|| op
== nir_op_fddx
)
1238 mask
= AC_TID_MASK_LEFT
;
1239 else if (op
== nir_op_fddy_fine
|| op
== nir_op_fddy
)
1240 mask
= AC_TID_MASK_TOP
;
1242 mask
= AC_TID_MASK_TOP_LEFT
;
1244 /* for DDX we want to next X pixel, DDY next Y pixel. */
1245 if (op
== nir_op_fddx_fine
||
1246 op
== nir_op_fddx_coarse
||
1252 result
= ac_build_ddxy(&ctx
->ac
, ctx
->has_ds_bpermute
,
1253 mask
, idx
, ctx
->lds
,
1259 * this takes an I,J coordinate pair,
1260 * and works out the X and Y derivatives.
1261 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
1263 static LLVMValueRef
emit_ddxy_interp(
1264 struct nir_to_llvm_context
*ctx
,
1265 LLVMValueRef interp_ij
)
1267 LLVMValueRef result
[4], a
;
1270 for (i
= 0; i
< 2; i
++) {
1271 a
= LLVMBuildExtractElement(ctx
->builder
, interp_ij
,
1272 LLVMConstInt(ctx
->i32
, i
, false), "");
1273 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
1274 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
1276 return ac_build_gather_values(&ctx
->ac
, result
, 4);
1279 static void visit_alu(struct nir_to_llvm_context
*ctx
, nir_alu_instr
*instr
)
1281 LLVMValueRef src
[4], result
= NULL
;
1282 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
1283 unsigned src_components
;
1284 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
1286 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
1287 switch (instr
->op
) {
1293 case nir_op_pack_half_2x16
:
1296 case nir_op_unpack_half_2x16
:
1300 src_components
= num_components
;
1303 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1304 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
1306 switch (instr
->op
) {
1312 src
[0] = to_float(ctx
, src
[0]);
1313 result
= LLVMBuildFNeg(ctx
->builder
, src
[0], "");
1316 result
= LLVMBuildNeg(ctx
->builder
, src
[0], "");
1319 result
= LLVMBuildNot(ctx
->builder
, src
[0], "");
1322 result
= LLVMBuildAdd(ctx
->builder
, src
[0], src
[1], "");
1325 src
[0] = to_float(ctx
, src
[0]);
1326 src
[1] = to_float(ctx
, src
[1]);
1327 result
= LLVMBuildFAdd(ctx
->builder
, src
[0], src
[1], "");
1330 src
[0] = to_float(ctx
, src
[0]);
1331 src
[1] = to_float(ctx
, src
[1]);
1332 result
= LLVMBuildFSub(ctx
->builder
, src
[0], src
[1], "");
1335 result
= LLVMBuildSub(ctx
->builder
, src
[0], src
[1], "");
1338 result
= LLVMBuildMul(ctx
->builder
, src
[0], src
[1], "");
1341 result
= LLVMBuildSRem(ctx
->builder
, src
[0], src
[1], "");
1344 result
= LLVMBuildURem(ctx
->builder
, src
[0], src
[1], "");
1347 src
[0] = to_float(ctx
, src
[0]);
1348 src
[1] = to_float(ctx
, src
[1]);
1349 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1350 result
= emit_intrin_1f_param(ctx
, "llvm.floor",
1351 to_float_type(ctx
, def_type
), result
);
1352 result
= LLVMBuildFMul(ctx
->builder
, src
[1] , result
, "");
1353 result
= LLVMBuildFSub(ctx
->builder
, src
[0], result
, "");
1356 src
[0] = to_float(ctx
, src
[0]);
1357 src
[1] = to_float(ctx
, src
[1]);
1358 result
= LLVMBuildFRem(ctx
->builder
, src
[0], src
[1], "");
1361 result
= LLVMBuildSRem(ctx
->builder
, src
[0], src
[1], "");
1364 result
= LLVMBuildSDiv(ctx
->builder
, src
[0], src
[1], "");
1367 result
= LLVMBuildUDiv(ctx
->builder
, src
[0], src
[1], "");
1370 src
[0] = to_float(ctx
, src
[0]);
1371 src
[1] = to_float(ctx
, src
[1]);
1372 result
= LLVMBuildFMul(ctx
->builder
, src
[0], src
[1], "");
1375 src
[0] = to_float(ctx
, src
[0]);
1376 src
[1] = to_float(ctx
, src
[1]);
1377 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1380 src
[0] = to_float(ctx
, src
[0]);
1381 result
= ac_build_fdiv(&ctx
->ac
, ctx
->f32one
, src
[0]);
1384 result
= LLVMBuildAnd(ctx
->builder
, src
[0], src
[1], "");
1387 result
= LLVMBuildOr(ctx
->builder
, src
[0], src
[1], "");
1390 result
= LLVMBuildXor(ctx
->builder
, src
[0], src
[1], "");
1393 result
= LLVMBuildShl(ctx
->builder
, src
[0], src
[1], "");
1396 result
= LLVMBuildAShr(ctx
->builder
, src
[0], src
[1], "");
1399 result
= LLVMBuildLShr(ctx
->builder
, src
[0], src
[1], "");
1402 result
= emit_int_cmp(ctx
, LLVMIntSLT
, src
[0], src
[1]);
1405 result
= emit_int_cmp(ctx
, LLVMIntNE
, src
[0], src
[1]);
1408 result
= emit_int_cmp(ctx
, LLVMIntEQ
, src
[0], src
[1]);
1411 result
= emit_int_cmp(ctx
, LLVMIntSGE
, src
[0], src
[1]);
1414 result
= emit_int_cmp(ctx
, LLVMIntULT
, src
[0], src
[1]);
1417 result
= emit_int_cmp(ctx
, LLVMIntUGE
, src
[0], src
[1]);
1420 result
= emit_float_cmp(ctx
, LLVMRealUEQ
, src
[0], src
[1]);
1423 result
= emit_float_cmp(ctx
, LLVMRealUNE
, src
[0], src
[1]);
1426 result
= emit_float_cmp(ctx
, LLVMRealULT
, src
[0], src
[1]);
1429 result
= emit_float_cmp(ctx
, LLVMRealUGE
, src
[0], src
[1]);
1432 result
= emit_intrin_1f_param(ctx
, "llvm.fabs",
1433 to_float_type(ctx
, def_type
), src
[0]);
1436 result
= emit_iabs(ctx
, src
[0]);
1439 result
= emit_minmax_int(ctx
, LLVMIntSGT
, src
[0], src
[1]);
1442 result
= emit_minmax_int(ctx
, LLVMIntSLT
, src
[0], src
[1]);
1445 result
= emit_minmax_int(ctx
, LLVMIntUGT
, src
[0], src
[1]);
1448 result
= emit_minmax_int(ctx
, LLVMIntULT
, src
[0], src
[1]);
1451 result
= emit_isign(ctx
, src
[0]);
1454 src
[0] = to_float(ctx
, src
[0]);
1455 result
= emit_fsign(ctx
, src
[0]);
1458 result
= emit_intrin_1f_param(ctx
, "llvm.floor",
1459 to_float_type(ctx
, def_type
), src
[0]);
1462 result
= emit_intrin_1f_param(ctx
, "llvm.trunc",
1463 to_float_type(ctx
, def_type
), src
[0]);
1466 result
= emit_intrin_1f_param(ctx
, "llvm.ceil",
1467 to_float_type(ctx
, def_type
), src
[0]);
1469 case nir_op_fround_even
:
1470 result
= emit_intrin_1f_param(ctx
, "llvm.rint",
1471 to_float_type(ctx
, def_type
),src
[0]);
1474 result
= emit_ffract(ctx
, src
[0]);
1477 result
= emit_intrin_1f_param(ctx
, "llvm.sin",
1478 to_float_type(ctx
, def_type
), src
[0]);
1481 result
= emit_intrin_1f_param(ctx
, "llvm.cos",
1482 to_float_type(ctx
, def_type
), src
[0]);
1485 result
= emit_intrin_1f_param(ctx
, "llvm.sqrt",
1486 to_float_type(ctx
, def_type
), src
[0]);
1489 result
= emit_intrin_1f_param(ctx
, "llvm.exp2",
1490 to_float_type(ctx
, def_type
), src
[0]);
1493 result
= emit_intrin_1f_param(ctx
, "llvm.log2",
1494 to_float_type(ctx
, def_type
), src
[0]);
1497 result
= emit_intrin_1f_param(ctx
, "llvm.sqrt",
1498 to_float_type(ctx
, def_type
), src
[0]);
1499 result
= ac_build_fdiv(&ctx
->ac
, ctx
->f32one
, result
);
1502 result
= emit_intrin_2f_param(ctx
, "llvm.pow",
1503 to_float_type(ctx
, def_type
), src
[0], src
[1]);
1506 result
= emit_intrin_2f_param(ctx
, "llvm.maxnum",
1507 to_float_type(ctx
, def_type
), src
[0], src
[1]);
1510 result
= emit_intrin_2f_param(ctx
, "llvm.minnum",
1511 to_float_type(ctx
, def_type
), src
[0], src
[1]);
1514 result
= emit_intrin_3f_param(ctx
, "llvm.fma",
1515 to_float_type(ctx
, def_type
), src
[0], src
[1], src
[2]);
1517 case nir_op_ibitfield_extract
:
1518 result
= emit_bitfield_extract(ctx
, true, src
);
1520 case nir_op_ubitfield_extract
:
1521 result
= emit_bitfield_extract(ctx
, false, src
);
1523 case nir_op_bitfield_insert
:
1524 result
= emit_bitfield_insert(ctx
, src
[0], src
[1], src
[2], src
[3]);
1526 case nir_op_bitfield_reverse
:
1527 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.bitreverse.i32", ctx
->i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1529 case nir_op_bit_count
:
1530 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i32", ctx
->i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1535 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1536 src
[i
] = to_integer(ctx
, src
[i
]);
1537 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
1541 src
[0] = to_float(ctx
, src
[0]);
1542 result
= LLVMBuildFPToSI(ctx
->builder
, src
[0], def_type
, "");
1546 src
[0] = to_float(ctx
, src
[0]);
1547 result
= LLVMBuildFPToUI(ctx
->builder
, src
[0], def_type
, "");
1551 result
= LLVMBuildSIToFP(ctx
->builder
, src
[0], to_float_type(ctx
, def_type
), "");
1555 result
= LLVMBuildUIToFP(ctx
->builder
, src
[0], to_float_type(ctx
, def_type
), "");
1558 result
= LLVMBuildFPExt(ctx
->builder
, src
[0], to_float_type(ctx
, def_type
), "");
1561 result
= LLVMBuildFPTrunc(ctx
->builder
, src
[0], to_float_type(ctx
, def_type
), "");
1565 if (get_elem_bits(ctx
, LLVMTypeOf(src
[0])) < get_elem_bits(ctx
, def_type
))
1566 result
= LLVMBuildZExt(ctx
->builder
, src
[0], def_type
, "");
1568 result
= LLVMBuildTrunc(ctx
->builder
, src
[0], def_type
, "");
1572 if (get_elem_bits(ctx
, LLVMTypeOf(src
[0])) < get_elem_bits(ctx
, def_type
))
1573 result
= LLVMBuildSExt(ctx
->builder
, src
[0], def_type
, "");
1575 result
= LLVMBuildTrunc(ctx
->builder
, src
[0], def_type
, "");
1578 result
= emit_bcsel(ctx
, src
[0], src
[1], src
[2]);
1580 case nir_op_find_lsb
:
1581 result
= emit_find_lsb(ctx
, src
[0]);
1583 case nir_op_ufind_msb
:
1584 result
= emit_ufind_msb(ctx
, src
[0]);
1586 case nir_op_ifind_msb
:
1587 result
= emit_ifind_msb(ctx
, src
[0]);
1589 case nir_op_uadd_carry
:
1590 result
= emit_uint_carry(ctx
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
1592 case nir_op_usub_borrow
:
1593 result
= emit_uint_carry(ctx
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
1596 result
= emit_b2f(ctx
, src
[0]);
1598 case nir_op_fquantize2f16
:
1599 src
[0] = to_float(ctx
, src
[0]);
1600 result
= LLVMBuildFPTrunc(ctx
->builder
, src
[0], ctx
->f16
, "");
1601 /* need to convert back up to f32 */
1602 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
1604 case nir_op_umul_high
:
1605 result
= emit_umul_high(ctx
, src
[0], src
[1]);
1607 case nir_op_imul_high
:
1608 result
= emit_imul_high(ctx
, src
[0], src
[1]);
1610 case nir_op_pack_half_2x16
:
1611 result
= emit_pack_half_2x16(ctx
, src
[0]);
1613 case nir_op_unpack_half_2x16
:
1614 result
= emit_unpack_half_2x16(ctx
, src
[0]);
1618 case nir_op_fddx_fine
:
1619 case nir_op_fddy_fine
:
1620 case nir_op_fddx_coarse
:
1621 case nir_op_fddy_coarse
:
1622 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
1625 fprintf(stderr
, "Unknown NIR alu instr: ");
1626 nir_print_instr(&instr
->instr
, stderr
);
1627 fprintf(stderr
, "\n");
1632 assert(instr
->dest
.dest
.is_ssa
);
1633 result
= to_integer(ctx
, result
);
1634 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.dest
.ssa
,
1639 static void visit_load_const(struct nir_to_llvm_context
*ctx
,
1640 nir_load_const_instr
*instr
)
1642 LLVMValueRef values
[4], value
= NULL
;
1643 LLVMTypeRef element_type
=
1644 LLVMIntTypeInContext(ctx
->context
, instr
->def
.bit_size
);
1646 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1647 switch (instr
->def
.bit_size
) {
1649 values
[i
] = LLVMConstInt(element_type
,
1650 instr
->value
.u32
[i
], false);
1653 values
[i
] = LLVMConstInt(element_type
,
1654 instr
->value
.u64
[i
], false);
1658 "unsupported nir load_const bit_size: %d\n",
1659 instr
->def
.bit_size
);
1663 if (instr
->def
.num_components
> 1) {
1664 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1668 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, value
);
1671 static LLVMValueRef
cast_ptr(struct nir_to_llvm_context
*ctx
, LLVMValueRef ptr
,
1674 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
1675 return LLVMBuildBitCast(ctx
->builder
, ptr
,
1676 LLVMPointerType(type
, addr_space
), "");
1680 get_buffer_size(struct nir_to_llvm_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1683 LLVMBuildExtractElement(ctx
->builder
, descriptor
,
1684 LLVMConstInt(ctx
->i32
, 2, false), "");
1687 if (ctx
->options
->chip_class
>= VI
&& in_elements
) {
1688 /* On VI, the descriptor contains the size in bytes,
1689 * but TXQ must return the size in elements.
1690 * The stride is always non-zero for resources using TXQ.
1692 LLVMValueRef stride
=
1693 LLVMBuildExtractElement(ctx
->builder
, descriptor
,
1694 LLVMConstInt(ctx
->i32
, 1, false), "");
1695 stride
= LLVMBuildLShr(ctx
->builder
, stride
,
1696 LLVMConstInt(ctx
->i32
, 16, false), "");
1697 stride
= LLVMBuildAnd(ctx
->builder
, stride
,
1698 LLVMConstInt(ctx
->i32
, 0x3fff, false), "");
1700 size
= LLVMBuildUDiv(ctx
->builder
, size
, stride
, "");
1706 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
1709 static void build_int_type_name(
1711 char *buf
, unsigned bufsize
)
1713 assert(bufsize
>= 6);
1715 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
1716 snprintf(buf
, bufsize
, "v%ui32",
1717 LLVMGetVectorSize(type
));
1722 static LLVMValueRef
radv_lower_gather4_integer(struct nir_to_llvm_context
*ctx
,
1723 struct ac_image_args
*args
,
1724 nir_tex_instr
*instr
)
1726 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
1727 LLVMValueRef coord
= args
->addr
;
1728 LLVMValueRef half_texel
[2];
1729 LLVMValueRef compare_cube_wa
;
1730 LLVMValueRef result
;
1732 unsigned coord_vgpr_index
= (unsigned)args
->offset
+ (unsigned)args
->compare
;
1736 struct ac_image_args txq_args
= { 0 };
1738 txq_args
.da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
1739 txq_args
.opcode
= ac_image_get_resinfo
;
1740 txq_args
.dmask
= 0xf;
1741 txq_args
.addr
= ctx
->i32zero
;
1742 txq_args
.resource
= args
->resource
;
1743 LLVMValueRef size
= ac_build_image_opcode(&ctx
->ac
, &txq_args
);
1745 for (c
= 0; c
< 2; c
++) {
1746 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
1747 LLVMConstInt(ctx
->i32
, c
, false), "");
1748 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1749 half_texel
[c
] = ac_build_fdiv(&ctx
->ac
, ctx
->f32one
, half_texel
[c
]);
1750 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1751 LLVMConstReal(ctx
->f32
, -0.5), "");
1755 LLVMValueRef orig_coords
= args
->addr
;
1757 for (c
= 0; c
< 2; c
++) {
1759 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, coord_vgpr_index
+ c
, 0);
1760 tmp
= LLVMBuildExtractElement(ctx
->builder
, coord
, index
, "");
1761 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1762 tmp
= LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
1763 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1764 coord
= LLVMBuildInsertElement(ctx
->builder
, coord
, tmp
, index
, "");
1769 * Apparantly cube has issue with integer types that the workaround doesn't solve,
1770 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
1771 * workaround by sampling using a scaled type and converting.
1772 * This is taken from amdgpu-pro shaders.
1774 /* NOTE this produces some ugly code compared to amdgpu-pro,
1775 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
1776 * and then reads them back. -pro generates two selects,
1777 * one s_cmp for the descriptor rewriting
1778 * one v_cmp for the coordinate and result changes.
1780 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1781 LLVMValueRef tmp
, tmp2
;
1783 /* workaround 8/8/8/8 uint/sint cube gather bug */
1784 /* first detect it then change to a scaled read and f2i */
1785 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32one
, "");
1788 /* extract the DATA_FORMAT */
1789 tmp
= ac_build_bfe(&ctx
->ac
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
1790 LLVMConstInt(ctx
->i32
, 6, false), false);
1792 /* is the DATA_FORMAT == 8_8_8_8 */
1793 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
1795 if (stype
== GLSL_TYPE_UINT
)
1796 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
1797 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
1798 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
1800 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
1801 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
1802 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
1804 /* replace the NUM FORMAT in the descriptor */
1805 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT_GFX6
, false), "");
1806 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
1808 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32one
, "");
1810 /* don't modify the coordinates for this case */
1811 coord
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, orig_coords
, coord
, "");
1814 result
= ac_build_image_opcode(&ctx
->ac
, args
);
1816 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1817 LLVMValueRef tmp
, tmp2
;
1819 /* if the cube workaround is in place, f2i the result. */
1820 for (c
= 0; c
< 4; c
++) {
1821 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
1822 if (stype
== GLSL_TYPE_UINT
)
1823 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
1825 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
1826 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1827 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
1828 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
1829 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1830 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
1836 static LLVMValueRef
build_tex_intrinsic(struct nir_to_llvm_context
*ctx
,
1837 nir_tex_instr
*instr
,
1838 struct ac_image_args
*args
)
1840 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
1841 return ac_build_buffer_load_format(&ctx
->ac
,
1844 LLVMConstInt(ctx
->i32
, 0, false),
1848 args
->opcode
= ac_image_sample
;
1849 args
->compare
= instr
->is_shadow
;
1851 switch (instr
->op
) {
1853 case nir_texop_txf_ms
:
1854 case nir_texop_samples_identical
:
1855 args
->opcode
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
? ac_image_load
: ac_image_load_mip
;
1856 args
->compare
= false;
1857 args
->offset
= false;
1866 case nir_texop_query_levels
:
1867 args
->opcode
= ac_image_get_resinfo
;
1870 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
1871 args
->level_zero
= true;
1877 args
->opcode
= ac_image_gather4
;
1878 args
->level_zero
= true;
1881 args
->opcode
= ac_image_get_lod
;
1882 args
->compare
= false;
1883 args
->offset
= false;
1889 if (instr
->op
== nir_texop_tg4
) {
1890 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
1891 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
1892 return radv_lower_gather4_integer(ctx
, args
, instr
);
1895 return ac_build_image_opcode(&ctx
->ac
, args
);
1898 static LLVMValueRef
visit_vulkan_resource_index(struct nir_to_llvm_context
*ctx
,
1899 nir_intrinsic_instr
*instr
)
1901 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
1902 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
1903 unsigned binding
= nir_intrinsic_binding(instr
);
1904 LLVMValueRef desc_ptr
= ctx
->descriptor_sets
[desc_set
];
1905 struct radv_pipeline_layout
*pipeline_layout
= ctx
->options
->layout
;
1906 struct radv_descriptor_set_layout
*layout
= pipeline_layout
->set
[desc_set
].layout
;
1907 unsigned base_offset
= layout
->binding
[binding
].offset
;
1908 LLVMValueRef offset
, stride
;
1910 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
1911 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
1912 unsigned idx
= pipeline_layout
->set
[desc_set
].dynamic_offset_start
+
1913 layout
->binding
[binding
].dynamic_offset_offset
;
1914 desc_ptr
= ctx
->push_constants
;
1915 base_offset
= pipeline_layout
->push_constant_size
+ 16 * idx
;
1916 stride
= LLVMConstInt(ctx
->i32
, 16, false);
1918 stride
= LLVMConstInt(ctx
->i32
, layout
->binding
[binding
].size
, false);
1920 offset
= LLVMConstInt(ctx
->i32
, base_offset
, false);
1921 index
= LLVMBuildMul(ctx
->builder
, index
, stride
, "");
1922 offset
= LLVMBuildAdd(ctx
->builder
, offset
, index
, "");
1924 desc_ptr
= ac_build_gep0(&ctx
->ac
, desc_ptr
, offset
);
1925 desc_ptr
= cast_ptr(ctx
, desc_ptr
, ctx
->v4i32
);
1926 LLVMSetMetadata(desc_ptr
, ctx
->uniform_md_kind
, ctx
->empty_md
);
1928 return LLVMBuildLoad(ctx
->builder
, desc_ptr
, "");
1931 static LLVMValueRef
visit_load_push_constant(struct nir_to_llvm_context
*ctx
,
1932 nir_intrinsic_instr
*instr
)
1934 LLVMValueRef ptr
, addr
;
1936 addr
= LLVMConstInt(ctx
->i32
, nir_intrinsic_base(instr
), 0);
1937 addr
= LLVMBuildAdd(ctx
->builder
, addr
, get_src(ctx
, instr
->src
[0]), "");
1939 ptr
= ac_build_gep0(&ctx
->ac
, ctx
->push_constants
, addr
);
1940 ptr
= cast_ptr(ctx
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
1942 return LLVMBuildLoad(ctx
->builder
, ptr
, "");
1945 static LLVMValueRef
visit_get_buffer_size(struct nir_to_llvm_context
*ctx
,
1946 nir_intrinsic_instr
*instr
)
1948 LLVMValueRef desc
= get_src(ctx
, instr
->src
[0]);
1950 return get_buffer_size(ctx
, desc
, false);
1952 static void visit_store_ssbo(struct nir_to_llvm_context
*ctx
,
1953 nir_intrinsic_instr
*instr
)
1955 const char *store_name
;
1956 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
1957 LLVMTypeRef data_type
= ctx
->f32
;
1958 int elem_size_mult
= get_elem_bits(ctx
, LLVMTypeOf(src_data
)) / 32;
1959 int components_32bit
= elem_size_mult
* instr
->num_components
;
1960 unsigned writemask
= nir_intrinsic_write_mask(instr
);
1961 LLVMValueRef base_data
, base_offset
;
1962 LLVMValueRef params
[6];
1964 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
1965 ctx
->shader_info
->fs
.writes_memory
= true;
1967 params
[1] = get_src(ctx
, instr
->src
[1]);
1968 params
[2] = LLVMConstInt(ctx
->i32
, 0, false); /* vindex */
1969 params
[4] = ctx
->i1false
; /* glc */
1970 params
[5] = ctx
->i1false
; /* slc */
1972 if (components_32bit
> 1)
1973 data_type
= LLVMVectorType(ctx
->f32
, components_32bit
);
1975 base_data
= to_float(ctx
, src_data
);
1976 base_data
= trim_vector(ctx
, base_data
, instr
->num_components
);
1977 base_data
= LLVMBuildBitCast(ctx
->builder
, base_data
,
1979 base_offset
= get_src(ctx
, instr
->src
[2]); /* voffset */
1983 LLVMValueRef offset
;
1985 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
1987 /* Due to an LLVM limitation, split 3-element writes
1988 * into a 2-element and a 1-element write. */
1990 writemask
|= 1 << (start
+ 2);
1994 start
*= elem_size_mult
;
1995 count
*= elem_size_mult
;
1998 writemask
|= ((1u << (count
- 4)) - 1u) << (start
+ 4);
2003 store_name
= "llvm.amdgcn.buffer.store.v4f32";
2005 } else if (count
== 2) {
2006 tmp
= LLVMBuildExtractElement(ctx
->builder
,
2007 base_data
, LLVMConstInt(ctx
->i32
, start
, false), "");
2008 data
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), tmp
,
2011 tmp
= LLVMBuildExtractElement(ctx
->builder
,
2012 base_data
, LLVMConstInt(ctx
->i32
, start
+ 1, false), "");
2013 data
= LLVMBuildInsertElement(ctx
->builder
, data
, tmp
,
2015 store_name
= "llvm.amdgcn.buffer.store.v2f32";
2019 if (get_llvm_num_components(base_data
) > 1)
2020 data
= LLVMBuildExtractElement(ctx
->builder
, base_data
,
2021 LLVMConstInt(ctx
->i32
, start
, false), "");
2024 store_name
= "llvm.amdgcn.buffer.store.f32";
2027 offset
= base_offset
;
2029 offset
= LLVMBuildAdd(ctx
->builder
, offset
, LLVMConstInt(ctx
->i32
, start
* 4, false), "");
2033 ac_build_intrinsic(&ctx
->ac
, store_name
,
2034 ctx
->voidt
, params
, 6, 0);
2038 static LLVMValueRef
visit_atomic_ssbo(struct nir_to_llvm_context
*ctx
,
2039 nir_intrinsic_instr
*instr
)
2042 LLVMValueRef params
[6];
2044 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
2045 ctx
->shader_info
->fs
.writes_memory
= true;
2047 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
2048 params
[arg_count
++] = llvm_extract_elem(ctx
, get_src(ctx
, instr
->src
[3]), 0);
2050 params
[arg_count
++] = llvm_extract_elem(ctx
, get_src(ctx
, instr
->src
[2]), 0);
2051 params
[arg_count
++] = get_src(ctx
, instr
->src
[0]);
2052 params
[arg_count
++] = LLVMConstInt(ctx
->i32
, 0, false); /* vindex */
2053 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
2054 params
[arg_count
++] = ctx
->i1false
; /* slc */
2056 switch (instr
->intrinsic
) {
2057 case nir_intrinsic_ssbo_atomic_add
:
2058 name
= "llvm.amdgcn.buffer.atomic.add";
2060 case nir_intrinsic_ssbo_atomic_imin
:
2061 name
= "llvm.amdgcn.buffer.atomic.smin";
2063 case nir_intrinsic_ssbo_atomic_umin
:
2064 name
= "llvm.amdgcn.buffer.atomic.umin";
2066 case nir_intrinsic_ssbo_atomic_imax
:
2067 name
= "llvm.amdgcn.buffer.atomic.smax";
2069 case nir_intrinsic_ssbo_atomic_umax
:
2070 name
= "llvm.amdgcn.buffer.atomic.umax";
2072 case nir_intrinsic_ssbo_atomic_and
:
2073 name
= "llvm.amdgcn.buffer.atomic.and";
2075 case nir_intrinsic_ssbo_atomic_or
:
2076 name
= "llvm.amdgcn.buffer.atomic.or";
2078 case nir_intrinsic_ssbo_atomic_xor
:
2079 name
= "llvm.amdgcn.buffer.atomic.xor";
2081 case nir_intrinsic_ssbo_atomic_exchange
:
2082 name
= "llvm.amdgcn.buffer.atomic.swap";
2084 case nir_intrinsic_ssbo_atomic_comp_swap
:
2085 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
2091 return ac_build_intrinsic(&ctx
->ac
, name
, ctx
->i32
, params
, arg_count
, 0);
2094 static LLVMValueRef
visit_load_buffer(struct nir_to_llvm_context
*ctx
,
2095 nir_intrinsic_instr
*instr
)
2097 LLVMValueRef results
[2];
2098 int load_components
;
2099 int num_components
= instr
->num_components
;
2100 if (instr
->dest
.ssa
.bit_size
== 64)
2101 num_components
*= 2;
2103 for (int i
= 0; i
< num_components
; i
+= load_components
) {
2104 load_components
= MIN2(num_components
- i
, 4);
2105 const char *load_name
;
2106 LLVMTypeRef data_type
= ctx
->f32
;
2107 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, i
* 4, false);
2108 offset
= LLVMBuildAdd(ctx
->builder
, get_src(ctx
, instr
->src
[1]), offset
, "");
2110 if (load_components
== 3)
2111 data_type
= LLVMVectorType(ctx
->f32
, 4);
2112 else if (load_components
> 1)
2113 data_type
= LLVMVectorType(ctx
->f32
, load_components
);
2115 if (load_components
>= 3)
2116 load_name
= "llvm.amdgcn.buffer.load.v4f32";
2117 else if (load_components
== 2)
2118 load_name
= "llvm.amdgcn.buffer.load.v2f32";
2119 else if (load_components
== 1)
2120 load_name
= "llvm.amdgcn.buffer.load.f32";
2122 unreachable("unhandled number of components");
2124 LLVMValueRef params
[] = {
2125 get_src(ctx
, instr
->src
[0]),
2126 LLVMConstInt(ctx
->i32
, 0, false),
2132 results
[i
] = ac_build_intrinsic(&ctx
->ac
, load_name
, data_type
, params
, 5, 0);
2136 LLVMValueRef ret
= results
[0];
2137 if (num_components
> 4 || num_components
== 3) {
2138 LLVMValueRef masks
[] = {
2139 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
2140 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false),
2141 LLVMConstInt(ctx
->i32
, 4, false), LLVMConstInt(ctx
->i32
, 5, false),
2142 LLVMConstInt(ctx
->i32
, 6, false), LLVMConstInt(ctx
->i32
, 7, false)
2145 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
2146 ret
= LLVMBuildShuffleVector(ctx
->builder
, results
[0],
2147 results
[num_components
> 4 ? 1 : 0], swizzle
, "");
2150 return LLVMBuildBitCast(ctx
->builder
, ret
,
2151 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2154 static LLVMValueRef
visit_load_ubo_buffer(struct nir_to_llvm_context
*ctx
,
2155 nir_intrinsic_instr
*instr
)
2157 LLVMValueRef results
[8], ret
;
2158 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
2159 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
2160 int num_components
= instr
->num_components
;
2162 rsrc
= LLVMBuildBitCast(ctx
->builder
, rsrc
, LLVMVectorType(ctx
->i8
, 16), "");
2164 if (instr
->dest
.ssa
.bit_size
== 64)
2165 num_components
*= 2;
2167 for (unsigned i
= 0; i
< num_components
; ++i
) {
2168 LLVMValueRef params
[] = {
2170 LLVMBuildAdd(ctx
->builder
, LLVMConstInt(ctx
->i32
, 4 * i
, 0),
2173 results
[i
] = ac_build_intrinsic(&ctx
->ac
, "llvm.SI.load.const", ctx
->f32
,
2175 AC_FUNC_ATTR_READNONE
|
2176 AC_FUNC_ATTR_LEGACY
);
2180 ret
= ac_build_gather_values(&ctx
->ac
, results
, instr
->num_components
);
2181 return LLVMBuildBitCast(ctx
->builder
, ret
,
2182 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2186 radv_get_deref_offset(struct nir_to_llvm_context
*ctx
, nir_deref
*tail
,
2187 bool vs_in
, unsigned *vertex_index_out
,
2188 unsigned *const_out
, LLVMValueRef
*indir_out
)
2190 unsigned const_offset
= 0;
2191 LLVMValueRef offset
= NULL
;
2193 if (vertex_index_out
!= NULL
) {
2195 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2196 *vertex_index_out
= deref_array
->base_offset
;
2199 while (tail
->child
!= NULL
) {
2200 const struct glsl_type
*parent_type
= tail
->type
;
2203 if (tail
->deref_type
== nir_deref_type_array
) {
2204 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2205 LLVMValueRef index
, stride
, local_offset
;
2206 unsigned size
= glsl_count_attribute_slots(tail
->type
, vs_in
);
2208 const_offset
+= size
* deref_array
->base_offset
;
2209 if (deref_array
->deref_array_type
== nir_deref_array_type_direct
)
2212 assert(deref_array
->deref_array_type
== nir_deref_array_type_indirect
);
2213 index
= get_src(ctx
, deref_array
->indirect
);
2214 stride
= LLVMConstInt(ctx
->i32
, size
, 0);
2215 local_offset
= LLVMBuildMul(ctx
->builder
, stride
, index
, "");
2218 offset
= LLVMBuildAdd(ctx
->builder
, offset
, local_offset
, "");
2220 offset
= local_offset
;
2221 } else if (tail
->deref_type
== nir_deref_type_struct
) {
2222 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
2224 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
2225 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
2226 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
2229 unreachable("unsupported deref type");
2233 if (const_offset
&& offset
)
2234 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
2235 LLVMConstInt(ctx
->i32
, const_offset
, 0),
2238 *const_out
= const_offset
;
2239 *indir_out
= offset
;
2243 load_gs_input(struct nir_to_llvm_context
*ctx
,
2244 nir_intrinsic_instr
*instr
)
2246 LLVMValueRef indir_index
, vtx_offset
;
2247 unsigned const_index
;
2248 LLVMValueRef args
[9];
2249 unsigned param
, vtx_offset_param
;
2250 LLVMValueRef value
[4], result
;
2251 unsigned vertex_index
;
2252 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
,
2253 false, &vertex_index
,
2254 &const_index
, &indir_index
);
2255 vtx_offset_param
= vertex_index
;
2256 assert(vtx_offset_param
< 6);
2257 vtx_offset
= LLVMBuildMul(ctx
->builder
, ctx
->gs_vtx_offset
[vtx_offset_param
],
2258 LLVMConstInt(ctx
->i32
, 4, false), "");
2260 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2261 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
2263 args
[0] = ctx
->esgs_ring
;
2264 args
[1] = vtx_offset
;
2265 args
[2] = LLVMConstInt(ctx
->i32
, (param
* 4 + i
+ const_index
) * 256, false);
2266 args
[3] = ctx
->i32zero
;
2267 args
[4] = ctx
->i32one
; /* OFFEN */
2268 args
[5] = ctx
->i32zero
; /* IDXEN */
2269 args
[6] = ctx
->i32one
; /* GLC */
2270 args
[7] = ctx
->i32zero
; /* SLC */
2271 args
[8] = ctx
->i32zero
; /* TFE */
2273 value
[i
] = ac_build_intrinsic(&ctx
->ac
, "llvm.SI.buffer.load.dword.i32.i32",
2275 AC_FUNC_ATTR_READONLY
|
2276 AC_FUNC_ATTR_LEGACY
);
2278 result
= ac_build_gather_values(&ctx
->ac
, value
, instr
->num_components
);
2283 static LLVMValueRef
visit_load_var(struct nir_to_llvm_context
*ctx
,
2284 nir_intrinsic_instr
*instr
)
2286 LLVMValueRef values
[8];
2287 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2288 int ve
= instr
->dest
.ssa
.num_components
;
2289 LLVMValueRef indir_index
;
2291 unsigned const_index
;
2292 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
2293 instr
->variables
[0]->var
->data
.mode
== nir_var_shader_in
;
2294 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, vs_in
, NULL
,
2295 &const_index
, &indir_index
);
2297 if (instr
->dest
.ssa
.bit_size
== 64)
2300 switch (instr
->variables
[0]->var
->data
.mode
) {
2301 case nir_var_shader_in
:
2302 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
2303 return load_gs_input(ctx
, instr
);
2305 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2307 unsigned count
= glsl_count_attribute_slots(
2308 instr
->variables
[0]->var
->type
,
2309 ctx
->stage
== MESA_SHADER_VERTEX
);
2311 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2312 &ctx
->ac
, ctx
->inputs
+ idx
+ chan
, count
,
2315 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2319 values
[chan
] = ctx
->inputs
[idx
+ chan
+ const_index
* 4];
2323 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2325 unsigned count
= glsl_count_attribute_slots(
2326 instr
->variables
[0]->var
->type
, false);
2328 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2329 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2332 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2336 values
[chan
] = LLVMBuildLoad(ctx
->builder
, ctx
->locals
[idx
+ chan
+ const_index
* 4], "");
2340 case nir_var_shader_out
:
2341 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2343 unsigned count
= glsl_count_attribute_slots(
2344 instr
->variables
[0]->var
->type
, false);
2346 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2347 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
2350 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2354 values
[chan
] = LLVMBuildLoad(ctx
->builder
,
2355 ctx
->outputs
[idx
+ chan
+ const_index
* 4],
2360 case nir_var_shared
: {
2361 LLVMValueRef ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
2362 LLVMValueRef derived_ptr
;
2365 indir_index
= LLVMBuildMul(ctx
->builder
, indir_index
, LLVMConstInt(ctx
->i32
, 4, false), "");
2367 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2368 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, chan
, false);
2370 index
= LLVMBuildAdd(ctx
->builder
, index
, indir_index
, "");
2371 derived_ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, "");
2373 values
[chan
] = LLVMBuildLoad(ctx
->builder
, derived_ptr
, "");
2378 unreachable("unhandle variable mode");
2380 ret
= ac_build_gather_values(&ctx
->ac
, values
, ve
);
2381 return LLVMBuildBitCast(ctx
->builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2385 visit_store_var(struct nir_to_llvm_context
*ctx
,
2386 nir_intrinsic_instr
*instr
)
2388 LLVMValueRef temp_ptr
, value
;
2389 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2390 LLVMValueRef src
= to_float(ctx
, get_src(ctx
, instr
->src
[0]));
2391 int writemask
= instr
->const_index
[0];
2392 LLVMValueRef indir_index
;
2393 unsigned const_index
;
2394 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2395 NULL
, &const_index
, &indir_index
);
2397 if (get_elem_bits(ctx
, LLVMTypeOf(src
)) == 64) {
2398 int old_writemask
= writemask
;
2400 src
= LLVMBuildBitCast(ctx
->builder
, src
,
2401 LLVMVectorType(ctx
->f32
, get_llvm_num_components(src
) * 2),
2405 for (unsigned chan
= 0; chan
< 4; chan
++) {
2406 if (old_writemask
& (1 << chan
))
2407 writemask
|= 3u << (2 * chan
);
2411 switch (instr
->variables
[0]->var
->data
.mode
) {
2412 case nir_var_shader_out
:
2413 for (unsigned chan
= 0; chan
< 8; chan
++) {
2415 if (!(writemask
& (1 << chan
)))
2418 value
= llvm_extract_elem(ctx
, src
, chan
);
2420 if (instr
->variables
[0]->var
->data
.compact
)
2423 unsigned count
= glsl_count_attribute_slots(
2424 instr
->variables
[0]->var
->type
, false);
2426 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2427 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
2430 if (get_llvm_num_components(tmp_vec
) > 1) {
2431 tmp_vec
= LLVMBuildInsertElement(ctx
->builder
, tmp_vec
,
2432 value
, indir_index
, "");
2435 build_store_values_extended(ctx
, ctx
->outputs
+ idx
+ chan
,
2436 count
, stride
, tmp_vec
);
2439 temp_ptr
= ctx
->outputs
[idx
+ chan
+ const_index
* stride
];
2441 LLVMBuildStore(ctx
->builder
, value
, temp_ptr
);
2446 for (unsigned chan
= 0; chan
< 8; chan
++) {
2447 if (!(writemask
& (1 << chan
)))
2450 value
= llvm_extract_elem(ctx
, src
, chan
);
2452 unsigned count
= glsl_count_attribute_slots(
2453 instr
->variables
[0]->var
->type
, false);
2455 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2456 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2459 tmp_vec
= LLVMBuildInsertElement(ctx
->builder
, tmp_vec
,
2460 value
, indir_index
, "");
2461 build_store_values_extended(ctx
, ctx
->locals
+ idx
+ chan
,
2464 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
2466 LLVMBuildStore(ctx
->builder
, value
, temp_ptr
);
2470 case nir_var_shared
: {
2471 LLVMValueRef ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
2474 indir_index
= LLVMBuildMul(ctx
->builder
, indir_index
, LLVMConstInt(ctx
->i32
, 4, false), "");
2476 for (unsigned chan
= 0; chan
< 8; chan
++) {
2477 if (!(writemask
& (1 << chan
)))
2479 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, chan
, false);
2480 LLVMValueRef derived_ptr
;
2483 index
= LLVMBuildAdd(ctx
->builder
, index
, indir_index
, "");
2485 value
= llvm_extract_elem(ctx
, src
, chan
);
2486 derived_ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, "");
2487 LLVMBuildStore(ctx
->builder
,
2488 to_integer(ctx
, value
), derived_ptr
);
2497 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
2500 case GLSL_SAMPLER_DIM_BUF
:
2502 case GLSL_SAMPLER_DIM_1D
:
2503 return array
? 2 : 1;
2504 case GLSL_SAMPLER_DIM_2D
:
2505 return array
? 3 : 2;
2506 case GLSL_SAMPLER_DIM_MS
:
2507 return array
? 4 : 3;
2508 case GLSL_SAMPLER_DIM_3D
:
2509 case GLSL_SAMPLER_DIM_CUBE
:
2511 case GLSL_SAMPLER_DIM_RECT
:
2512 case GLSL_SAMPLER_DIM_SUBPASS
:
2514 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
2524 /* Adjust the sample index according to FMASK.
2526 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
2527 * which is the identity mapping. Each nibble says which physical sample
2528 * should be fetched to get that sample.
2530 * For example, 0x11111100 means there are only 2 samples stored and
2531 * the second sample covers 3/4 of the pixel. When reading samples 0
2532 * and 1, return physical sample 0 (determined by the first two 0s
2533 * in FMASK), otherwise return physical sample 1.
2535 * The sample index should be adjusted as follows:
2536 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
2538 static LLVMValueRef
adjust_sample_index_using_fmask(struct nir_to_llvm_context
*ctx
,
2539 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
2540 LLVMValueRef coord_z
,
2541 LLVMValueRef sample_index
,
2542 LLVMValueRef fmask_desc_ptr
)
2544 LLVMValueRef fmask_load_address
[4];
2547 fmask_load_address
[0] = coord_x
;
2548 fmask_load_address
[1] = coord_y
;
2550 fmask_load_address
[2] = coord_z
;
2551 fmask_load_address
[3] = LLVMGetUndef(ctx
->i32
);
2554 struct ac_image_args args
= {0};
2556 args
.opcode
= ac_image_load
;
2557 args
.da
= coord_z
? true : false;
2558 args
.resource
= fmask_desc_ptr
;
2560 args
.addr
= ac_build_gather_values(&ctx
->ac
, fmask_load_address
, coord_z
? 4 : 2);
2562 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2564 res
= to_integer(ctx
, res
);
2565 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
2566 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
2568 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
2572 LLVMValueRef sample_index4
=
2573 LLVMBuildMul(ctx
->builder
, sample_index
, four
, "");
2574 LLVMValueRef shifted_fmask
=
2575 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
2576 LLVMValueRef final_sample
=
2577 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
2579 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
2580 * resource descriptor is 0 (invalid),
2582 LLVMValueRef fmask_desc
=
2583 LLVMBuildBitCast(ctx
->builder
, fmask_desc_ptr
,
2586 LLVMValueRef fmask_word1
=
2587 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
2590 LLVMValueRef word1_is_nonzero
=
2591 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
2592 fmask_word1
, ctx
->i32zero
, "");
2594 /* Replace the MSAA sample index. */
2596 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
2597 final_sample
, sample_index
, "");
2598 return sample_index
;
2601 static LLVMValueRef
get_image_coords(struct nir_to_llvm_context
*ctx
,
2602 nir_intrinsic_instr
*instr
)
2604 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
2605 if(instr
->variables
[0]->deref
.child
)
2606 type
= instr
->variables
[0]->deref
.child
->type
;
2608 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
2609 LLVMValueRef coords
[4];
2610 LLVMValueRef masks
[] = {
2611 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
2612 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false),
2615 LLVMValueRef sample_index
= llvm_extract_elem(ctx
, get_src(ctx
, instr
->src
[1]), 0);
2618 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2619 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
2620 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2621 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
2622 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2624 count
= image_type_to_components_count(dim
,
2625 glsl_sampler_type_is_array(type
));
2628 LLVMValueRef fmask_load_address
[3];
2631 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[0], "");
2632 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[1], "");
2633 if (glsl_sampler_type_is_array(type
))
2634 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[2], "");
2636 fmask_load_address
[2] = NULL
;
2638 for (chan
= 0; chan
< 2; ++chan
)
2639 fmask_load_address
[chan
] = LLVMBuildAdd(ctx
->builder
, fmask_load_address
[chan
], LLVMBuildFPToUI(ctx
->builder
, ctx
->frag_pos
[chan
], ctx
->i32
, ""), "");
2641 sample_index
= adjust_sample_index_using_fmask(ctx
,
2642 fmask_load_address
[0],
2643 fmask_load_address
[1],
2644 fmask_load_address
[2],
2646 get_sampler_desc(ctx
, instr
->variables
[0], DESC_FMASK
));
2649 if (instr
->src
[0].ssa
->num_components
)
2650 res
= LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[0], "");
2657 for (chan
= 0; chan
< count
; ++chan
) {
2658 coords
[chan
] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[chan
], "");
2662 for (chan
= 0; chan
< count
; ++chan
)
2663 coords
[chan
] = LLVMBuildAdd(ctx
->builder
, coords
[chan
], LLVMBuildFPToUI(ctx
->builder
, ctx
->frag_pos
[chan
], ctx
->i32
, ""), "");
2666 coords
[count
] = sample_index
;
2671 coords
[3] = LLVMGetUndef(ctx
->i32
);
2674 res
= ac_build_gather_values(&ctx
->ac
, coords
, count
);
2679 static LLVMValueRef
visit_image_load(struct nir_to_llvm_context
*ctx
,
2680 nir_intrinsic_instr
*instr
)
2682 LLVMValueRef params
[7];
2684 char intrinsic_name
[64];
2685 const nir_variable
*var
= instr
->variables
[0]->var
;
2686 const struct glsl_type
*type
= var
->type
;
2687 if(instr
->variables
[0]->deref
.child
)
2688 type
= instr
->variables
[0]->deref
.child
->type
;
2690 type
= glsl_without_array(type
);
2691 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2692 params
[0] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
2693 params
[1] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
2694 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
2695 params
[2] = LLVMConstInt(ctx
->i32
, 0, false); /* voffset */
2696 params
[3] = ctx
->i1false
; /* glc */
2697 params
[4] = ctx
->i1false
; /* slc */
2698 res
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.load.format.v4f32", ctx
->v4f32
,
2701 res
= trim_vector(ctx
, res
, instr
->dest
.ssa
.num_components
);
2702 res
= to_integer(ctx
, res
);
2704 bool is_da
= glsl_sampler_type_is_array(type
) ||
2705 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
2706 LLVMValueRef da
= is_da
? ctx
->i1true
: ctx
->i1false
;
2707 LLVMValueRef glc
= ctx
->i1false
;
2708 LLVMValueRef slc
= ctx
->i1false
;
2710 params
[0] = get_image_coords(ctx
, instr
);
2711 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
2712 params
[2] = LLVMConstInt(ctx
->i32
, 15, false); /* dmask */
2713 if (HAVE_LLVM
<= 0x0309) {
2714 params
[3] = ctx
->i1false
; /* r128 */
2719 LLVMValueRef lwe
= ctx
->i1false
;
2726 ac_get_image_intr_name("llvm.amdgcn.image.load",
2727 ctx
->v4f32
, /* vdata */
2728 LLVMTypeOf(params
[0]), /* coords */
2729 LLVMTypeOf(params
[1]), /* rsrc */
2730 intrinsic_name
, sizeof(intrinsic_name
));
2732 res
= ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->v4f32
,
2733 params
, 7, AC_FUNC_ATTR_READONLY
);
2735 return to_integer(ctx
, res
);
2738 static void visit_image_store(struct nir_to_llvm_context
*ctx
,
2739 nir_intrinsic_instr
*instr
)
2741 LLVMValueRef params
[8];
2742 char intrinsic_name
[64];
2743 const nir_variable
*var
= instr
->variables
[0]->var
;
2744 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2746 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
2747 ctx
->shader_info
->fs
.writes_memory
= true;
2749 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2750 params
[0] = to_float(ctx
, get_src(ctx
, instr
->src
[2])); /* data */
2751 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
2752 params
[2] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
2753 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
2754 params
[3] = LLVMConstInt(ctx
->i32
, 0, false); /* voffset */
2755 params
[4] = ctx
->i1false
; /* glc */
2756 params
[5] = ctx
->i1false
; /* slc */
2757 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->voidt
,
2760 bool is_da
= glsl_sampler_type_is_array(type
) ||
2761 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
2762 LLVMValueRef da
= is_da
? ctx
->i1true
: ctx
->i1false
;
2763 LLVMValueRef glc
= ctx
->i1false
;
2764 LLVMValueRef slc
= ctx
->i1false
;
2766 params
[0] = to_float(ctx
, get_src(ctx
, instr
->src
[2]));
2767 params
[1] = get_image_coords(ctx
, instr
); /* coords */
2768 params
[2] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
2769 params
[3] = LLVMConstInt(ctx
->i32
, 15, false); /* dmask */
2770 if (HAVE_LLVM
<= 0x0309) {
2771 params
[4] = ctx
->i1false
; /* r128 */
2776 LLVMValueRef lwe
= ctx
->i1false
;
2783 ac_get_image_intr_name("llvm.amdgcn.image.store",
2784 LLVMTypeOf(params
[0]), /* vdata */
2785 LLVMTypeOf(params
[1]), /* coords */
2786 LLVMTypeOf(params
[2]), /* rsrc */
2787 intrinsic_name
, sizeof(intrinsic_name
));
2789 ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->voidt
,
2795 static LLVMValueRef
visit_image_atomic(struct nir_to_llvm_context
*ctx
,
2796 nir_intrinsic_instr
*instr
)
2798 LLVMValueRef params
[6];
2799 int param_count
= 0;
2800 const nir_variable
*var
= instr
->variables
[0]->var
;
2802 const char *base_name
= "llvm.amdgcn.image.atomic";
2803 const char *atomic_name
;
2804 LLVMValueRef coords
;
2805 char intrinsic_name
[32], coords_type
[8];
2806 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2808 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
2809 ctx
->shader_info
->fs
.writes_memory
= true;
2811 params
[param_count
++] = get_src(ctx
, instr
->src
[2]);
2812 if (instr
->intrinsic
== nir_intrinsic_image_atomic_comp_swap
)
2813 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
2815 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2816 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
2817 coords
= params
[param_count
++] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
2818 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
2819 params
[param_count
++] = ctx
->i32zero
; /* voffset */
2820 params
[param_count
++] = ctx
->i1false
; /* glc */
2821 params
[param_count
++] = ctx
->i1false
; /* slc */
2823 bool da
= glsl_sampler_type_is_array(type
) ||
2824 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
2826 coords
= params
[param_count
++] = get_image_coords(ctx
, instr
);
2827 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
2828 params
[param_count
++] = ctx
->i1false
; /* r128 */
2829 params
[param_count
++] = da
? ctx
->i1true
: ctx
->i1false
; /* da */
2830 params
[param_count
++] = ctx
->i1false
; /* slc */
2833 switch (instr
->intrinsic
) {
2834 case nir_intrinsic_image_atomic_add
:
2835 atomic_name
= "add";
2837 case nir_intrinsic_image_atomic_min
:
2838 atomic_name
= "smin";
2840 case nir_intrinsic_image_atomic_max
:
2841 atomic_name
= "smax";
2843 case nir_intrinsic_image_atomic_and
:
2844 atomic_name
= "and";
2846 case nir_intrinsic_image_atomic_or
:
2849 case nir_intrinsic_image_atomic_xor
:
2850 atomic_name
= "xor";
2852 case nir_intrinsic_image_atomic_exchange
:
2853 atomic_name
= "swap";
2855 case nir_intrinsic_image_atomic_comp_swap
:
2856 atomic_name
= "cmpswap";
2861 build_int_type_name(LLVMTypeOf(coords
),
2862 coords_type
, sizeof(coords_type
));
2864 snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2865 "%s.%s.%s", base_name
, atomic_name
, coords_type
);
2866 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->i32
, params
, param_count
, 0);
2869 static LLVMValueRef
visit_image_size(struct nir_to_llvm_context
*ctx
,
2870 nir_intrinsic_instr
*instr
)
2873 const nir_variable
*var
= instr
->variables
[0]->var
;
2874 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
2875 bool da
= glsl_sampler_type_is_array(var
->type
) ||
2876 glsl_get_sampler_dim(var
->type
) == GLSL_SAMPLER_DIM_CUBE
;
2877 if(instr
->variables
[0]->deref
.child
)
2878 type
= instr
->variables
[0]->deref
.child
->type
;
2880 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
2881 return get_buffer_size(ctx
, get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
), true);
2883 struct ac_image_args args
= { 0 };
2887 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
2888 args
.opcode
= ac_image_get_resinfo
;
2889 args
.addr
= ctx
->i32zero
;
2891 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2893 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
2894 glsl_sampler_type_is_array(type
)) {
2895 LLVMValueRef two
= LLVMConstInt(ctx
->i32
, 2, false);
2896 LLVMValueRef six
= LLVMConstInt(ctx
->i32
, 6, false);
2897 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->builder
, res
, two
, "");
2898 z
= LLVMBuildSDiv(ctx
->builder
, z
, six
, "");
2899 res
= LLVMBuildInsertElement(ctx
->builder
, res
, z
, two
, "");
2904 #define NOOP_WAITCNT 0xf7f
2905 #define LGKM_CNT 0x07f
2906 #define VM_CNT 0xf70
2908 static void emit_waitcnt(struct nir_to_llvm_context
*ctx
,
2911 LLVMValueRef args
[1] = {
2912 LLVMConstInt(ctx
->i32
, simm16
, false),
2914 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.s.waitcnt",
2915 ctx
->voidt
, args
, 1, 0);
2918 static void emit_barrier(struct nir_to_llvm_context
*ctx
)
2920 /* SI only (thanks to a hw bug workaround):
2921 * The real barrier instruction isn’t needed, because an entire patch
2922 * always fits into a single wave.
2924 if (ctx
->options
->chip_class
== SI
&&
2925 ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2926 emit_waitcnt(ctx
, LGKM_CNT
& VM_CNT
);
2929 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.s.barrier",
2930 ctx
->voidt
, NULL
, 0, AC_FUNC_ATTR_CONVERGENT
);
2933 static void emit_discard_if(struct nir_to_llvm_context
*ctx
,
2934 nir_intrinsic_instr
*instr
)
2937 ctx
->shader_info
->fs
.can_discard
= true;
2939 cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
2940 get_src(ctx
, instr
->src
[0]),
2943 cond
= LLVMBuildSelect(ctx
->builder
, cond
,
2944 LLVMConstReal(ctx
->f32
, -1.0f
),
2946 ac_build_kill(&ctx
->ac
, cond
);
2950 visit_load_local_invocation_index(struct nir_to_llvm_context
*ctx
)
2952 LLVMValueRef result
;
2953 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
2954 result
= LLVMBuildAnd(ctx
->builder
, ctx
->tg_size
,
2955 LLVMConstInt(ctx
->i32
, 0xfc0, false), "");
2957 return LLVMBuildAdd(ctx
->builder
, result
, thread_id
, "");
2960 static LLVMValueRef
visit_var_atomic(struct nir_to_llvm_context
*ctx
,
2961 nir_intrinsic_instr
*instr
)
2963 LLVMValueRef ptr
, result
;
2964 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2965 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
2966 ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
2968 if (instr
->intrinsic
== nir_intrinsic_var_atomic_comp_swap
) {
2969 LLVMValueRef src1
= get_src(ctx
, instr
->src
[1]);
2970 result
= LLVMBuildAtomicCmpXchg(ctx
->builder
,
2972 LLVMAtomicOrderingSequentiallyConsistent
,
2973 LLVMAtomicOrderingSequentiallyConsistent
,
2976 LLVMAtomicRMWBinOp op
;
2977 switch (instr
->intrinsic
) {
2978 case nir_intrinsic_var_atomic_add
:
2979 op
= LLVMAtomicRMWBinOpAdd
;
2981 case nir_intrinsic_var_atomic_umin
:
2982 op
= LLVMAtomicRMWBinOpUMin
;
2984 case nir_intrinsic_var_atomic_umax
:
2985 op
= LLVMAtomicRMWBinOpUMax
;
2987 case nir_intrinsic_var_atomic_imin
:
2988 op
= LLVMAtomicRMWBinOpMin
;
2990 case nir_intrinsic_var_atomic_imax
:
2991 op
= LLVMAtomicRMWBinOpMax
;
2993 case nir_intrinsic_var_atomic_and
:
2994 op
= LLVMAtomicRMWBinOpAnd
;
2996 case nir_intrinsic_var_atomic_or
:
2997 op
= LLVMAtomicRMWBinOpOr
;
2999 case nir_intrinsic_var_atomic_xor
:
3000 op
= LLVMAtomicRMWBinOpXor
;
3002 case nir_intrinsic_var_atomic_exchange
:
3003 op
= LLVMAtomicRMWBinOpXchg
;
3009 result
= LLVMBuildAtomicRMW(ctx
->builder
, op
, ptr
, to_integer(ctx
, src
),
3010 LLVMAtomicOrderingSequentiallyConsistent
,
3016 #define INTERP_CENTER 0
3017 #define INTERP_CENTROID 1
3018 #define INTERP_SAMPLE 2
3020 static LLVMValueRef
lookup_interp_param(struct nir_to_llvm_context
*ctx
,
3021 enum glsl_interp_mode interp
, unsigned location
)
3024 case INTERP_MODE_FLAT
:
3027 case INTERP_MODE_SMOOTH
:
3028 case INTERP_MODE_NONE
:
3029 if (location
== INTERP_CENTER
)
3030 return ctx
->persp_center
;
3031 else if (location
== INTERP_CENTROID
)
3032 return ctx
->persp_centroid
;
3033 else if (location
== INTERP_SAMPLE
)
3034 return ctx
->persp_sample
;
3036 case INTERP_MODE_NOPERSPECTIVE
:
3037 if (location
== INTERP_CENTER
)
3038 return ctx
->linear_center
;
3039 else if (location
== INTERP_CENTROID
)
3040 return ctx
->linear_centroid
;
3041 else if (location
== INTERP_SAMPLE
)
3042 return ctx
->linear_sample
;
3048 static LLVMValueRef
load_sample_position(struct nir_to_llvm_context
*ctx
,
3049 LLVMValueRef sample_id
)
3051 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
3052 LLVMValueRef offset0
= LLVMBuildMul(ctx
->builder
, sample_id
, LLVMConstInt(ctx
->i32
, 8, false), "");
3053 LLVMValueRef offset1
= LLVMBuildAdd(ctx
->builder
, offset0
, LLVMConstInt(ctx
->i32
, 4, false), "");
3054 LLVMValueRef result
[2];
3056 result
[0] = ac_build_indexed_load_const(&ctx
->ac
, ctx
->sample_positions
, offset0
);
3057 result
[1] = ac_build_indexed_load_const(&ctx
->ac
, ctx
->sample_positions
, offset1
);
3059 return ac_build_gather_values(&ctx
->ac
, result
, 2);
3062 static LLVMValueRef
load_sample_pos(struct nir_to_llvm_context
*ctx
)
3064 LLVMValueRef values
[2];
3066 values
[0] = emit_ffract(ctx
, ctx
->frag_pos
[0]);
3067 values
[1] = emit_ffract(ctx
, ctx
->frag_pos
[1]);
3068 return ac_build_gather_values(&ctx
->ac
, values
, 2);
3071 static LLVMValueRef
visit_interp(struct nir_to_llvm_context
*ctx
,
3072 nir_intrinsic_instr
*instr
)
3074 LLVMValueRef result
[2];
3075 LLVMValueRef interp_param
, attr_number
;
3078 LLVMValueRef src_c0
, src_c1
;
3080 int input_index
= instr
->variables
[0]->var
->data
.location
- VARYING_SLOT_VAR0
;
3081 switch (instr
->intrinsic
) {
3082 case nir_intrinsic_interp_var_at_centroid
:
3083 location
= INTERP_CENTROID
;
3085 case nir_intrinsic_interp_var_at_sample
:
3086 location
= INTERP_SAMPLE
;
3087 src0
= get_src(ctx
, instr
->src
[0]);
3089 case nir_intrinsic_interp_var_at_offset
:
3090 location
= INTERP_CENTER
;
3091 src0
= get_src(ctx
, instr
->src
[0]);
3096 if (instr
->intrinsic
== nir_intrinsic_interp_var_at_offset
) {
3097 src_c0
= to_float(ctx
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32zero
, ""));
3098 src_c1
= to_float(ctx
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32one
, ""));
3099 } else if (instr
->intrinsic
== nir_intrinsic_interp_var_at_sample
) {
3100 LLVMValueRef sample_position
;
3101 LLVMValueRef halfval
= LLVMConstReal(ctx
->f32
, 0.5f
);
3103 /* fetch sample ID */
3104 sample_position
= load_sample_position(ctx
, src0
);
3106 src_c0
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->i32zero
, "");
3107 src_c0
= LLVMBuildFSub(ctx
->builder
, src_c0
, halfval
, "");
3108 src_c1
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->i32one
, "");
3109 src_c1
= LLVMBuildFSub(ctx
->builder
, src_c1
, halfval
, "");
3111 interp_param
= lookup_interp_param(ctx
, instr
->variables
[0]->var
->data
.interpolation
, location
);
3112 attr_number
= LLVMConstInt(ctx
->i32
, input_index
, false);
3114 if (location
== INTERP_SAMPLE
|| location
== INTERP_CENTER
) {
3115 LLVMValueRef ij_out
[2];
3116 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
, interp_param
);
3119 * take the I then J parameters, and the DDX/Y for it, and
3120 * calculate the IJ inputs for the interpolator.
3121 * temp1 = ddx * offset/sample.x + I;
3122 * interp_param.I = ddy * offset/sample.y + temp1;
3123 * temp1 = ddx * offset/sample.x + J;
3124 * interp_param.J = ddy * offset/sample.y + temp1;
3126 for (unsigned i
= 0; i
< 2; i
++) {
3127 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->i32
, i
, false);
3128 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->i32
, i
+ 2, false);
3129 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->builder
,
3130 ddxy_out
, ix_ll
, "");
3131 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->builder
,
3132 ddxy_out
, iy_ll
, "");
3133 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->builder
,
3134 interp_param
, ix_ll
, "");
3135 LLVMValueRef temp1
, temp2
;
3137 interp_el
= LLVMBuildBitCast(ctx
->builder
, interp_el
,
3140 temp1
= LLVMBuildFMul(ctx
->builder
, ddx_el
, src_c0
, "");
3141 temp1
= LLVMBuildFAdd(ctx
->builder
, temp1
, interp_el
, "");
3143 temp2
= LLVMBuildFMul(ctx
->builder
, ddy_el
, src_c1
, "");
3144 temp2
= LLVMBuildFAdd(ctx
->builder
, temp2
, temp1
, "");
3146 ij_out
[i
] = LLVMBuildBitCast(ctx
->builder
,
3147 temp2
, ctx
->i32
, "");
3149 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
3153 for (chan
= 0; chan
< 2; chan
++) {
3154 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
3157 interp_param
= LLVMBuildBitCast(ctx
->builder
,
3158 interp_param
, LLVMVectorType(ctx
->f32
, 2), "");
3159 LLVMValueRef i
= LLVMBuildExtractElement(
3160 ctx
->builder
, interp_param
, ctx
->i32zero
, "");
3161 LLVMValueRef j
= LLVMBuildExtractElement(
3162 ctx
->builder
, interp_param
, ctx
->i32one
, "");
3164 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
3165 llvm_chan
, attr_number
,
3166 ctx
->prim_mask
, i
, j
);
3168 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
3169 LLVMConstInt(ctx
->i32
, 2, false),
3170 llvm_chan
, attr_number
,
3174 return ac_build_gather_values(&ctx
->ac
, result
, 2);
3178 visit_emit_vertex(struct nir_to_llvm_context
*ctx
,
3179 nir_intrinsic_instr
*instr
)
3181 LLVMValueRef gs_next_vertex
;
3182 LLVMValueRef can_emit
, kill
;
3185 assert(instr
->const_index
[0] == 0);
3186 /* Write vertex attribute values to GSVS ring */
3187 gs_next_vertex
= LLVMBuildLoad(ctx
->builder
,
3188 ctx
->gs_next_vertex
,
3191 /* If this thread has already emitted the declared maximum number of
3192 * vertices, kill it: excessive vertex emissions are not supposed to
3193 * have any effect, and GS threads have no externally observable
3194 * effects other than emitting vertices.
3196 can_emit
= LLVMBuildICmp(ctx
->builder
, LLVMIntULT
, gs_next_vertex
,
3197 LLVMConstInt(ctx
->i32
, ctx
->gs_max_out_vertices
, false), "");
3199 kill
= LLVMBuildSelect(ctx
->builder
, can_emit
,
3200 LLVMConstReal(ctx
->f32
, 1.0f
),
3201 LLVMConstReal(ctx
->f32
, -1.0f
), "");
3202 ac_build_kill(&ctx
->ac
, kill
);
3204 /* loop num outputs */
3206 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
3207 LLVMValueRef
*out_ptr
= &ctx
->outputs
[i
* 4];
3212 if (!(ctx
->output_mask
& (1ull << i
)))
3215 if (i
== VARYING_SLOT_CLIP_DIST0
) {
3216 /* pack clip and cull into a single set of slots */
3217 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
3221 for (unsigned j
= 0; j
< length
; j
++) {
3222 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
,
3224 LLVMValueRef voffset
= LLVMConstInt(ctx
->i32
, (slot
* 4 + j
) * ctx
->gs_max_out_vertices
, false);
3225 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, gs_next_vertex
, "");
3226 voffset
= LLVMBuildMul(ctx
->builder
, voffset
, LLVMConstInt(ctx
->i32
, 4, false), "");
3228 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->i32
, "");
3230 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->gsvs_ring
,
3232 voffset
, ctx
->gs2vs_offset
, 0,
3238 gs_next_vertex
= LLVMBuildAdd(ctx
->builder
, gs_next_vertex
,
3240 LLVMBuildStore(ctx
->builder
, gs_next_vertex
, ctx
->gs_next_vertex
);
3242 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_EMIT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
3246 visit_end_primitive(struct nir_to_llvm_context
*ctx
,
3247 nir_intrinsic_instr
*instr
)
3249 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_CUT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
3252 static void visit_intrinsic(struct nir_to_llvm_context
*ctx
,
3253 nir_intrinsic_instr
*instr
)
3255 LLVMValueRef result
= NULL
;
3257 switch (instr
->intrinsic
) {
3258 case nir_intrinsic_load_work_group_id
: {
3259 result
= ctx
->workgroup_ids
;
3262 case nir_intrinsic_load_base_vertex
: {
3263 result
= ctx
->base_vertex
;
3266 case nir_intrinsic_load_vertex_id_zero_base
: {
3267 result
= ctx
->vertex_id
;
3270 case nir_intrinsic_load_local_invocation_id
: {
3271 result
= ctx
->local_invocation_ids
;
3274 case nir_intrinsic_load_base_instance
:
3275 result
= ctx
->start_instance
;
3277 case nir_intrinsic_load_draw_id
:
3278 result
= ctx
->draw_index
;
3280 case nir_intrinsic_load_invocation_id
:
3281 result
= ctx
->gs_invocation_id
;
3283 case nir_intrinsic_load_primitive_id
:
3284 if (ctx
->stage
== MESA_SHADER_GEOMETRY
)
3285 result
= ctx
->gs_prim_id
;
3287 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
3289 case nir_intrinsic_load_sample_id
:
3290 ctx
->shader_info
->fs
.force_persample
= true;
3291 result
= unpack_param(ctx
, ctx
->ancillary
, 8, 4);
3293 case nir_intrinsic_load_sample_pos
:
3294 ctx
->shader_info
->fs
.force_persample
= true;
3295 result
= load_sample_pos(ctx
);
3297 case nir_intrinsic_load_sample_mask_in
:
3298 result
= ctx
->sample_coverage
;
3300 case nir_intrinsic_load_front_face
:
3301 result
= ctx
->front_face
;
3303 case nir_intrinsic_load_instance_id
:
3304 result
= ctx
->instance_id
;
3305 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
3306 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
3308 case nir_intrinsic_load_num_work_groups
:
3309 result
= ctx
->num_work_groups
;
3311 case nir_intrinsic_load_local_invocation_index
:
3312 result
= visit_load_local_invocation_index(ctx
);
3314 case nir_intrinsic_load_push_constant
:
3315 result
= visit_load_push_constant(ctx
, instr
);
3317 case nir_intrinsic_vulkan_resource_index
:
3318 result
= visit_vulkan_resource_index(ctx
, instr
);
3320 case nir_intrinsic_store_ssbo
:
3321 visit_store_ssbo(ctx
, instr
);
3323 case nir_intrinsic_load_ssbo
:
3324 result
= visit_load_buffer(ctx
, instr
);
3326 case nir_intrinsic_ssbo_atomic_add
:
3327 case nir_intrinsic_ssbo_atomic_imin
:
3328 case nir_intrinsic_ssbo_atomic_umin
:
3329 case nir_intrinsic_ssbo_atomic_imax
:
3330 case nir_intrinsic_ssbo_atomic_umax
:
3331 case nir_intrinsic_ssbo_atomic_and
:
3332 case nir_intrinsic_ssbo_atomic_or
:
3333 case nir_intrinsic_ssbo_atomic_xor
:
3334 case nir_intrinsic_ssbo_atomic_exchange
:
3335 case nir_intrinsic_ssbo_atomic_comp_swap
:
3336 result
= visit_atomic_ssbo(ctx
, instr
);
3338 case nir_intrinsic_load_ubo
:
3339 result
= visit_load_ubo_buffer(ctx
, instr
);
3341 case nir_intrinsic_get_buffer_size
:
3342 result
= visit_get_buffer_size(ctx
, instr
);
3344 case nir_intrinsic_load_var
:
3345 result
= visit_load_var(ctx
, instr
);
3347 case nir_intrinsic_store_var
:
3348 visit_store_var(ctx
, instr
);
3350 case nir_intrinsic_image_load
:
3351 result
= visit_image_load(ctx
, instr
);
3353 case nir_intrinsic_image_store
:
3354 visit_image_store(ctx
, instr
);
3356 case nir_intrinsic_image_atomic_add
:
3357 case nir_intrinsic_image_atomic_min
:
3358 case nir_intrinsic_image_atomic_max
:
3359 case nir_intrinsic_image_atomic_and
:
3360 case nir_intrinsic_image_atomic_or
:
3361 case nir_intrinsic_image_atomic_xor
:
3362 case nir_intrinsic_image_atomic_exchange
:
3363 case nir_intrinsic_image_atomic_comp_swap
:
3364 result
= visit_image_atomic(ctx
, instr
);
3366 case nir_intrinsic_image_size
:
3367 result
= visit_image_size(ctx
, instr
);
3369 case nir_intrinsic_discard
:
3370 ctx
->shader_info
->fs
.can_discard
= true;
3371 ac_build_intrinsic(&ctx
->ac
, "llvm.AMDGPU.kilp",
3373 NULL
, 0, AC_FUNC_ATTR_LEGACY
);
3375 case nir_intrinsic_discard_if
:
3376 emit_discard_if(ctx
, instr
);
3378 case nir_intrinsic_memory_barrier
:
3379 emit_waitcnt(ctx
, VM_CNT
);
3381 case nir_intrinsic_barrier
:
3384 case nir_intrinsic_var_atomic_add
:
3385 case nir_intrinsic_var_atomic_imin
:
3386 case nir_intrinsic_var_atomic_umin
:
3387 case nir_intrinsic_var_atomic_imax
:
3388 case nir_intrinsic_var_atomic_umax
:
3389 case nir_intrinsic_var_atomic_and
:
3390 case nir_intrinsic_var_atomic_or
:
3391 case nir_intrinsic_var_atomic_xor
:
3392 case nir_intrinsic_var_atomic_exchange
:
3393 case nir_intrinsic_var_atomic_comp_swap
:
3394 result
= visit_var_atomic(ctx
, instr
);
3396 case nir_intrinsic_interp_var_at_centroid
:
3397 case nir_intrinsic_interp_var_at_sample
:
3398 case nir_intrinsic_interp_var_at_offset
:
3399 result
= visit_interp(ctx
, instr
);
3401 case nir_intrinsic_emit_vertex
:
3402 visit_emit_vertex(ctx
, instr
);
3404 case nir_intrinsic_end_primitive
:
3405 visit_end_primitive(ctx
, instr
);
3408 fprintf(stderr
, "Unknown intrinsic: ");
3409 nir_print_instr(&instr
->instr
, stderr
);
3410 fprintf(stderr
, "\n");
3414 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
3418 static LLVMValueRef
get_sampler_desc(struct nir_to_llvm_context
*ctx
,
3419 nir_deref_var
*deref
,
3420 enum desc_type desc_type
)
3422 unsigned desc_set
= deref
->var
->data
.descriptor_set
;
3423 LLVMValueRef list
= ctx
->descriptor_sets
[desc_set
];
3424 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[desc_set
].layout
;
3425 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ deref
->var
->data
.binding
;
3426 unsigned offset
= binding
->offset
;
3427 unsigned stride
= binding
->size
;
3429 LLVMBuilderRef builder
= ctx
->builder
;
3431 LLVMValueRef index
= NULL
;
3432 unsigned constant_index
= 0;
3434 assert(deref
->var
->data
.binding
< layout
->binding_count
);
3436 switch (desc_type
) {
3448 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
3458 unreachable("invalid desc_type\n");
3461 if (deref
->deref
.child
) {
3462 nir_deref_array
*child
= (nir_deref_array
*)deref
->deref
.child
;
3464 assert(child
->deref_array_type
!= nir_deref_array_type_wildcard
);
3465 offset
+= child
->base_offset
* stride
;
3466 if (child
->deref_array_type
== nir_deref_array_type_indirect
) {
3467 index
= get_src(ctx
, child
->indirect
);
3470 constant_index
= child
->base_offset
;
3472 if (desc_type
== DESC_SAMPLER
&& binding
->immutable_samplers
&&
3473 (!index
|| binding
->immutable_samplers_equal
)) {
3474 if (binding
->immutable_samplers_equal
)
3477 LLVMValueRef constants
[] = {
3478 LLVMConstInt(ctx
->i32
, binding
->immutable_samplers
[constant_index
* 4 + 0], 0),
3479 LLVMConstInt(ctx
->i32
, binding
->immutable_samplers
[constant_index
* 4 + 1], 0),
3480 LLVMConstInt(ctx
->i32
, binding
->immutable_samplers
[constant_index
* 4 + 2], 0),
3481 LLVMConstInt(ctx
->i32
, binding
->immutable_samplers
[constant_index
* 4 + 3], 0),
3483 return ac_build_gather_values(&ctx
->ac
, constants
, 4);
3486 assert(stride
% type_size
== 0);
3489 index
= ctx
->i32zero
;
3491 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, stride
/ type_size
, 0), "");
3493 list
= ac_build_gep0(&ctx
->ac
, list
, LLVMConstInt(ctx
->i32
, offset
, 0));
3494 list
= LLVMBuildPointerCast(builder
, list
, const_array(type
, 0), "");
3496 return ac_build_indexed_load_const(&ctx
->ac
, list
, index
);
3499 static void set_tex_fetch_args(struct nir_to_llvm_context
*ctx
,
3500 struct ac_image_args
*args
,
3501 nir_tex_instr
*instr
,
3503 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
3504 LLVMValueRef
*param
, unsigned count
,
3507 unsigned is_rect
= 0;
3508 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
3510 if (op
== nir_texop_lod
)
3512 /* Pad to power of two vector */
3513 while (count
< util_next_power_of_two(count
))
3514 param
[count
++] = LLVMGetUndef(ctx
->i32
);
3517 args
->addr
= ac_build_gather_values(&ctx
->ac
, param
, count
);
3519 args
->addr
= param
[0];
3521 args
->resource
= res_ptr
;
3522 args
->sampler
= samp_ptr
;
3524 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
&& op
== nir_texop_txf
) {
3525 args
->addr
= param
[0];
3529 args
->dmask
= dmask
;
3530 args
->unorm
= is_rect
;
3534 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
3537 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
3538 * filtering manually. The driver sets img7 to a mask clearing
3539 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
3540 * s_and_b32 samp0, samp0, img7
3543 * The ANISO_OVERRIDE sampler field enables this fix in TA.
3545 static LLVMValueRef
sici_fix_sampler_aniso(struct nir_to_llvm_context
*ctx
,
3546 LLVMValueRef res
, LLVMValueRef samp
)
3548 LLVMBuilderRef builder
= ctx
->builder
;
3549 LLVMValueRef img7
, samp0
;
3551 if (ctx
->options
->chip_class
>= VI
)
3554 img7
= LLVMBuildExtractElement(builder
, res
,
3555 LLVMConstInt(ctx
->i32
, 7, 0), "");
3556 samp0
= LLVMBuildExtractElement(builder
, samp
,
3557 LLVMConstInt(ctx
->i32
, 0, 0), "");
3558 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
3559 return LLVMBuildInsertElement(builder
, samp
, samp0
,
3560 LLVMConstInt(ctx
->i32
, 0, 0), "");
3563 static void tex_fetch_ptrs(struct nir_to_llvm_context
*ctx
,
3564 nir_tex_instr
*instr
,
3565 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
3566 LLVMValueRef
*fmask_ptr
)
3568 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
3569 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_BUFFER
);
3571 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_IMAGE
);
3574 *samp_ptr
= get_sampler_desc(ctx
, instr
->sampler
, DESC_SAMPLER
);
3576 *samp_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_SAMPLER
);
3577 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
3578 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
3580 if (fmask_ptr
&& !instr
->sampler
&& (instr
->op
== nir_texop_txf_ms
||
3581 instr
->op
== nir_texop_samples_identical
))
3582 *fmask_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_FMASK
);
3585 static LLVMValueRef
apply_round_slice(struct nir_to_llvm_context
*ctx
,
3588 coord
= to_float(ctx
, coord
);
3589 coord
= ac_build_intrinsic(&ctx
->ac
, "llvm.rint.f32", ctx
->f32
, &coord
, 1, 0);
3590 coord
= to_integer(ctx
, coord
);
3594 static void visit_tex(struct nir_to_llvm_context
*ctx
, nir_tex_instr
*instr
)
3596 LLVMValueRef result
= NULL
;
3597 struct ac_image_args args
= { 0 };
3598 unsigned dmask
= 0xf;
3599 LLVMValueRef address
[16];
3600 LLVMValueRef coords
[5];
3601 LLVMValueRef coord
= NULL
, lod
= NULL
, comparator
= NULL
;
3602 LLVMValueRef bias
= NULL
, offsets
= NULL
;
3603 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
, sample_index
= NULL
;
3604 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
3605 LLVMValueRef derivs
[6];
3606 unsigned chan
, count
= 0;
3607 unsigned const_src
= 0, num_deriv_comp
= 0;
3609 tex_fetch_ptrs(ctx
, instr
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
3611 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3612 switch (instr
->src
[i
].src_type
) {
3613 case nir_tex_src_coord
:
3614 coord
= get_src(ctx
, instr
->src
[i
].src
);
3616 case nir_tex_src_projector
:
3618 case nir_tex_src_comparator
:
3619 comparator
= get_src(ctx
, instr
->src
[i
].src
);
3621 case nir_tex_src_offset
:
3622 offsets
= get_src(ctx
, instr
->src
[i
].src
);
3625 case nir_tex_src_bias
:
3626 bias
= get_src(ctx
, instr
->src
[i
].src
);
3628 case nir_tex_src_lod
:
3629 lod
= get_src(ctx
, instr
->src
[i
].src
);
3631 case nir_tex_src_ms_index
:
3632 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
3634 case nir_tex_src_ms_mcs
:
3636 case nir_tex_src_ddx
:
3637 ddx
= get_src(ctx
, instr
->src
[i
].src
);
3638 num_deriv_comp
= instr
->src
[i
].src
.ssa
->num_components
;
3640 case nir_tex_src_ddy
:
3641 ddy
= get_src(ctx
, instr
->src
[i
].src
);
3643 case nir_tex_src_texture_offset
:
3644 case nir_tex_src_sampler_offset
:
3645 case nir_tex_src_plane
:
3651 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
3652 result
= get_buffer_size(ctx
, res_ptr
, true);
3656 if (instr
->op
== nir_texop_texture_samples
) {
3657 LLVMValueRef res
, samples
, is_msaa
;
3658 res
= LLVMBuildBitCast(ctx
->builder
, res_ptr
, ctx
->v8i32
, "");
3659 samples
= LLVMBuildExtractElement(ctx
->builder
, res
,
3660 LLVMConstInt(ctx
->i32
, 3, false), "");
3661 is_msaa
= LLVMBuildLShr(ctx
->builder
, samples
,
3662 LLVMConstInt(ctx
->i32
, 28, false), "");
3663 is_msaa
= LLVMBuildAnd(ctx
->builder
, is_msaa
,
3664 LLVMConstInt(ctx
->i32
, 0xe, false), "");
3665 is_msaa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, is_msaa
,
3666 LLVMConstInt(ctx
->i32
, 0xe, false), "");
3668 samples
= LLVMBuildLShr(ctx
->builder
, samples
,
3669 LLVMConstInt(ctx
->i32
, 16, false), "");
3670 samples
= LLVMBuildAnd(ctx
->builder
, samples
,
3671 LLVMConstInt(ctx
->i32
, 0xf, false), "");
3672 samples
= LLVMBuildShl(ctx
->builder
, ctx
->i32one
,
3674 samples
= LLVMBuildSelect(ctx
->builder
, is_msaa
, samples
,
3681 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
3682 coords
[chan
] = llvm_extract_elem(ctx
, coord
, chan
);
3684 if (offsets
&& instr
->op
!= nir_texop_txf
) {
3685 LLVMValueRef offset
[3], pack
;
3686 for (chan
= 0; chan
< 3; ++chan
)
3687 offset
[chan
] = ctx
->i32zero
;
3690 for (chan
= 0; chan
< get_llvm_num_components(offsets
); chan
++) {
3691 offset
[chan
] = llvm_extract_elem(ctx
, offsets
, chan
);
3692 offset
[chan
] = LLVMBuildAnd(ctx
->builder
, offset
[chan
],
3693 LLVMConstInt(ctx
->i32
, 0x3f, false), "");
3695 offset
[chan
] = LLVMBuildShl(ctx
->builder
, offset
[chan
],
3696 LLVMConstInt(ctx
->i32
, chan
* 8, false), "");
3698 pack
= LLVMBuildOr(ctx
->builder
, offset
[0], offset
[1], "");
3699 pack
= LLVMBuildOr(ctx
->builder
, pack
, offset
[2], "");
3700 address
[count
++] = pack
;
3703 /* pack LOD bias value */
3704 if (instr
->op
== nir_texop_txb
&& bias
) {
3705 address
[count
++] = bias
;
3708 /* Pack depth comparison value */
3709 if (instr
->is_shadow
&& comparator
) {
3710 address
[count
++] = llvm_extract_elem(ctx
, comparator
, 0);
3713 /* pack derivatives */
3715 switch (instr
->sampler_dim
) {
3716 case GLSL_SAMPLER_DIM_3D
:
3717 case GLSL_SAMPLER_DIM_CUBE
:
3720 case GLSL_SAMPLER_DIM_2D
:
3724 case GLSL_SAMPLER_DIM_1D
:
3729 for (unsigned i
= 0; i
< num_deriv_comp
; i
++) {
3730 derivs
[i
* 2] = to_float(ctx
, llvm_extract_elem(ctx
, ddx
, i
));
3731 derivs
[i
* 2 + 1] = to_float(ctx
, llvm_extract_elem(ctx
, ddy
, i
));
3735 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& coord
) {
3736 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
3737 coords
[chan
] = to_float(ctx
, coords
[chan
]);
3738 if (instr
->coord_components
== 3)
3739 coords
[3] = LLVMGetUndef(ctx
->f32
);
3740 ac_prepare_cube_coords(&ctx
->ac
,
3741 instr
->op
== nir_texop_txd
, instr
->is_array
,
3748 for (unsigned i
= 0; i
< num_deriv_comp
* 2; i
++)
3749 address
[count
++] = derivs
[i
];
3752 /* Pack texture coordinates */
3754 address
[count
++] = coords
[0];
3755 if (instr
->coord_components
> 1) {
3756 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&& instr
->is_array
&& instr
->op
!= nir_texop_txf
) {
3757 coords
[1] = apply_round_slice(ctx
, coords
[1]);
3759 address
[count
++] = coords
[1];
3761 if (instr
->coord_components
> 2) {
3762 /* This seems like a bit of a hack - but it passes Vulkan CTS with it */
3763 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_3D
&& instr
->op
!= nir_texop_txf
) {
3764 coords
[2] = apply_round_slice(ctx
, coords
[2]);
3766 address
[count
++] = coords
[2];
3771 if ((instr
->op
== nir_texop_txl
|| instr
->op
== nir_texop_txf
) && lod
) {
3772 address
[count
++] = lod
;
3773 } else if (instr
->op
== nir_texop_txf_ms
&& sample_index
) {
3774 address
[count
++] = sample_index
;
3775 } else if(instr
->op
== nir_texop_txs
) {
3778 address
[count
++] = lod
;
3780 address
[count
++] = ctx
->i32zero
;
3783 for (chan
= 0; chan
< count
; chan
++) {
3784 address
[chan
] = LLVMBuildBitCast(ctx
->builder
,
3785 address
[chan
], ctx
->i32
, "");
3788 if (instr
->op
== nir_texop_samples_identical
) {
3789 LLVMValueRef txf_address
[4];
3790 struct ac_image_args txf_args
= { 0 };
3791 unsigned txf_count
= count
;
3792 memcpy(txf_address
, address
, sizeof(txf_address
));
3794 if (!instr
->is_array
)
3795 txf_address
[2] = ctx
->i32zero
;
3796 txf_address
[3] = ctx
->i32zero
;
3798 set_tex_fetch_args(ctx
, &txf_args
, instr
, nir_texop_txf
,
3800 txf_address
, txf_count
, 0xf);
3802 result
= build_tex_intrinsic(ctx
, instr
, &txf_args
);
3804 result
= LLVMBuildExtractElement(ctx
->builder
, result
, ctx
->i32zero
, "");
3805 result
= emit_int_cmp(ctx
, LLVMIntEQ
, result
, ctx
->i32zero
);
3809 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
3810 instr
->op
!= nir_texop_txs
) {
3811 unsigned sample_chan
= instr
->is_array
? 3 : 2;
3812 address
[sample_chan
] = adjust_sample_index_using_fmask(ctx
,
3815 instr
->is_array
? address
[2] : NULL
,
3816 address
[sample_chan
],
3820 if (offsets
&& instr
->op
== nir_texop_txf
) {
3821 nir_const_value
*const_offset
=
3822 nir_src_as_const_value(instr
->src
[const_src
].src
);
3823 int num_offsets
= instr
->src
[const_src
].src
.ssa
->num_components
;
3824 assert(const_offset
);
3825 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
3826 if (num_offsets
> 2)
3827 address
[2] = LLVMBuildAdd(ctx
->builder
,
3828 address
[2], LLVMConstInt(ctx
->i32
, const_offset
->i32
[2], false), "");
3829 if (num_offsets
> 1)
3830 address
[1] = LLVMBuildAdd(ctx
->builder
,
3831 address
[1], LLVMConstInt(ctx
->i32
, const_offset
->i32
[1], false), "");
3832 address
[0] = LLVMBuildAdd(ctx
->builder
,
3833 address
[0], LLVMConstInt(ctx
->i32
, const_offset
->i32
[0], false), "");
3837 /* TODO TG4 support */
3838 if (instr
->op
== nir_texop_tg4
) {
3839 if (instr
->is_shadow
)
3842 dmask
= 1 << instr
->component
;
3844 set_tex_fetch_args(ctx
, &args
, instr
, instr
->op
,
3845 res_ptr
, samp_ptr
, address
, count
, dmask
);
3847 result
= build_tex_intrinsic(ctx
, instr
, &args
);
3849 if (instr
->op
== nir_texop_query_levels
)
3850 result
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, 3, false), "");
3851 else if (instr
->is_shadow
&& instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&& instr
->op
!= nir_texop_tg4
)
3852 result
= LLVMBuildExtractElement(ctx
->builder
, result
, ctx
->i32zero
, "");
3853 else if (instr
->op
== nir_texop_txs
&&
3854 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
3856 LLVMValueRef two
= LLVMConstInt(ctx
->i32
, 2, false);
3857 LLVMValueRef six
= LLVMConstInt(ctx
->i32
, 6, false);
3858 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->builder
, result
, two
, "");
3859 z
= LLVMBuildSDiv(ctx
->builder
, z
, six
, "");
3860 result
= LLVMBuildInsertElement(ctx
->builder
, result
, z
, two
, "");
3861 } else if (instr
->dest
.ssa
.num_components
!= 4)
3862 result
= trim_vector(ctx
, result
, instr
->dest
.ssa
.num_components
);
3866 assert(instr
->dest
.is_ssa
);
3867 result
= to_integer(ctx
, result
);
3868 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
3873 static void visit_phi(struct nir_to_llvm_context
*ctx
, nir_phi_instr
*instr
)
3875 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
3876 LLVMValueRef result
= LLVMBuildPhi(ctx
->builder
, type
, "");
3878 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
3879 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
3882 static void visit_post_phi(struct nir_to_llvm_context
*ctx
,
3883 nir_phi_instr
*instr
,
3884 LLVMValueRef llvm_phi
)
3886 nir_foreach_phi_src(src
, instr
) {
3887 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
3888 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
3890 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
3894 static void phi_post_pass(struct nir_to_llvm_context
*ctx
)
3896 struct hash_entry
*entry
;
3897 hash_table_foreach(ctx
->phis
, entry
) {
3898 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
3899 (LLVMValueRef
)entry
->data
);
3904 static void visit_ssa_undef(struct nir_to_llvm_context
*ctx
,
3905 nir_ssa_undef_instr
*instr
)
3907 unsigned num_components
= instr
->def
.num_components
;
3910 if (num_components
== 1)
3911 undef
= LLVMGetUndef(ctx
->i32
);
3913 undef
= LLVMGetUndef(LLVMVectorType(ctx
->i32
, num_components
));
3915 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, undef
);
3918 static void visit_jump(struct nir_to_llvm_context
*ctx
,
3919 nir_jump_instr
*instr
)
3921 switch (instr
->type
) {
3922 case nir_jump_break
:
3923 LLVMBuildBr(ctx
->builder
, ctx
->break_block
);
3924 LLVMClearInsertionPosition(ctx
->builder
);
3926 case nir_jump_continue
:
3927 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
3928 LLVMClearInsertionPosition(ctx
->builder
);
3931 fprintf(stderr
, "Unknown NIR jump instr: ");
3932 nir_print_instr(&instr
->instr
, stderr
);
3933 fprintf(stderr
, "\n");
3938 static void visit_cf_list(struct nir_to_llvm_context
*ctx
,
3939 struct exec_list
*list
);
3941 static void visit_block(struct nir_to_llvm_context
*ctx
, nir_block
*block
)
3943 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->builder
);
3944 nir_foreach_instr(instr
, block
)
3946 switch (instr
->type
) {
3947 case nir_instr_type_alu
:
3948 visit_alu(ctx
, nir_instr_as_alu(instr
));
3950 case nir_instr_type_load_const
:
3951 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
3953 case nir_instr_type_intrinsic
:
3954 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
3956 case nir_instr_type_tex
:
3957 visit_tex(ctx
, nir_instr_as_tex(instr
));
3959 case nir_instr_type_phi
:
3960 visit_phi(ctx
, nir_instr_as_phi(instr
));
3962 case nir_instr_type_ssa_undef
:
3963 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
3965 case nir_instr_type_jump
:
3966 visit_jump(ctx
, nir_instr_as_jump(instr
));
3969 fprintf(stderr
, "Unknown NIR instr type: ");
3970 nir_print_instr(instr
, stderr
);
3971 fprintf(stderr
, "\n");
3976 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
3979 static void visit_if(struct nir_to_llvm_context
*ctx
, nir_if
*if_stmt
)
3981 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
3983 LLVMBasicBlockRef merge_block
=
3984 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
3985 LLVMBasicBlockRef if_block
=
3986 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
3987 LLVMBasicBlockRef else_block
= merge_block
;
3988 if (!exec_list_is_empty(&if_stmt
->else_list
))
3989 else_block
= LLVMAppendBasicBlockInContext(
3990 ctx
->context
, ctx
->main_function
, "");
3992 LLVMValueRef cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, value
,
3993 LLVMConstInt(ctx
->i32
, 0, false), "");
3994 LLVMBuildCondBr(ctx
->builder
, cond
, if_block
, else_block
);
3996 LLVMPositionBuilderAtEnd(ctx
->builder
, if_block
);
3997 visit_cf_list(ctx
, &if_stmt
->then_list
);
3998 if (LLVMGetInsertBlock(ctx
->builder
))
3999 LLVMBuildBr(ctx
->builder
, merge_block
);
4001 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
4002 LLVMPositionBuilderAtEnd(ctx
->builder
, else_block
);
4003 visit_cf_list(ctx
, &if_stmt
->else_list
);
4004 if (LLVMGetInsertBlock(ctx
->builder
))
4005 LLVMBuildBr(ctx
->builder
, merge_block
);
4008 LLVMPositionBuilderAtEnd(ctx
->builder
, merge_block
);
4011 static void visit_loop(struct nir_to_llvm_context
*ctx
, nir_loop
*loop
)
4013 LLVMBasicBlockRef continue_parent
= ctx
->continue_block
;
4014 LLVMBasicBlockRef break_parent
= ctx
->break_block
;
4016 ctx
->continue_block
=
4017 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
4019 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
4021 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
4022 LLVMPositionBuilderAtEnd(ctx
->builder
, ctx
->continue_block
);
4023 visit_cf_list(ctx
, &loop
->body
);
4025 if (LLVMGetInsertBlock(ctx
->builder
))
4026 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
4027 LLVMPositionBuilderAtEnd(ctx
->builder
, ctx
->break_block
);
4029 ctx
->continue_block
= continue_parent
;
4030 ctx
->break_block
= break_parent
;
4033 static void visit_cf_list(struct nir_to_llvm_context
*ctx
,
4034 struct exec_list
*list
)
4036 foreach_list_typed(nir_cf_node
, node
, node
, list
)
4038 switch (node
->type
) {
4039 case nir_cf_node_block
:
4040 visit_block(ctx
, nir_cf_node_as_block(node
));
4043 case nir_cf_node_if
:
4044 visit_if(ctx
, nir_cf_node_as_if(node
));
4047 case nir_cf_node_loop
:
4048 visit_loop(ctx
, nir_cf_node_as_loop(node
));
4058 handle_vs_input_decl(struct nir_to_llvm_context
*ctx
,
4059 struct nir_variable
*variable
)
4061 LLVMValueRef t_list_ptr
= ctx
->vertex_buffers
;
4062 LLVMValueRef t_offset
;
4063 LLVMValueRef t_list
;
4064 LLVMValueRef args
[3];
4066 LLVMValueRef buffer_index
;
4067 int index
= variable
->data
.location
- VERT_ATTRIB_GENERIC0
;
4068 int idx
= variable
->data
.location
;
4069 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, true);
4071 variable
->data
.driver_location
= idx
* 4;
4073 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << index
)) {
4074 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->instance_id
,
4075 ctx
->start_instance
, "");
4076 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
4077 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
4079 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->vertex_id
,
4080 ctx
->base_vertex
, "");
4082 for (unsigned i
= 0; i
< attrib_count
; ++i
, ++idx
) {
4083 t_offset
= LLVMConstInt(ctx
->i32
, index
+ i
, false);
4085 t_list
= ac_build_indexed_load_const(&ctx
->ac
, t_list_ptr
, t_offset
);
4087 args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
4088 args
[2] = buffer_index
;
4089 input
= ac_build_intrinsic(&ctx
->ac
,
4090 "llvm.SI.vs.load.input", ctx
->v4f32
, args
, 3,
4091 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_NOUNWIND
|
4092 AC_FUNC_ATTR_LEGACY
);
4094 for (unsigned chan
= 0; chan
< 4; chan
++) {
4095 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
4096 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
, chan
)] =
4097 to_integer(ctx
, LLVMBuildExtractElement(ctx
->builder
,
4098 input
, llvm_chan
, ""));
4103 static void interp_fs_input(struct nir_to_llvm_context
*ctx
,
4105 LLVMValueRef interp_param
,
4106 LLVMValueRef prim_mask
,
4107 LLVMValueRef result
[4])
4109 LLVMValueRef attr_number
;
4112 bool interp
= interp_param
!= NULL
;
4114 attr_number
= LLVMConstInt(ctx
->i32
, attr
, false);
4116 /* fs.constant returns the param from the middle vertex, so it's not
4117 * really useful for flat shading. It's meant to be used for custom
4118 * interpolation (but the intrinsic can't fetch from the other two
4121 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
4122 * to do the right thing. The only reason we use fs.constant is that
4123 * fs.interp cannot be used on integers, because they can be equal
4127 interp_param
= LLVMBuildBitCast(ctx
->builder
, interp_param
,
4128 LLVMVectorType(ctx
->f32
, 2), "");
4130 i
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
4132 j
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
4136 for (chan
= 0; chan
< 4; chan
++) {
4137 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
4140 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
4145 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
4146 LLVMConstInt(ctx
->i32
, 2, false),
4155 handle_fs_input_decl(struct nir_to_llvm_context
*ctx
,
4156 struct nir_variable
*variable
)
4158 int idx
= variable
->data
.location
;
4159 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4160 LLVMValueRef interp
;
4162 variable
->data
.driver_location
= idx
* 4;
4163 ctx
->input_mask
|= ((1ull << attrib_count
) - 1) << variable
->data
.location
;
4165 if (glsl_get_base_type(glsl_without_array(variable
->type
)) == GLSL_TYPE_FLOAT
) {
4166 unsigned interp_type
;
4167 if (variable
->data
.sample
) {
4168 interp_type
= INTERP_SAMPLE
;
4169 ctx
->shader_info
->fs
.force_persample
= true;
4170 } else if (variable
->data
.centroid
)
4171 interp_type
= INTERP_CENTROID
;
4173 interp_type
= INTERP_CENTER
;
4175 interp
= lookup_interp_param(ctx
, variable
->data
.interpolation
, interp_type
);
4179 for (unsigned i
= 0; i
< attrib_count
; ++i
)
4180 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
+ i
, 0)] = interp
;
4185 handle_shader_input_decl(struct nir_to_llvm_context
*ctx
,
4186 struct nir_variable
*variable
)
4188 switch (ctx
->stage
) {
4189 case MESA_SHADER_VERTEX
:
4190 handle_vs_input_decl(ctx
, variable
);
4192 case MESA_SHADER_FRAGMENT
:
4193 handle_fs_input_decl(ctx
, variable
);
4202 handle_fs_inputs_pre(struct nir_to_llvm_context
*ctx
,
4203 struct nir_shader
*nir
)
4206 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_INPUTS
; ++i
) {
4207 LLVMValueRef interp_param
;
4208 LLVMValueRef
*inputs
= ctx
->inputs
+radeon_llvm_reg_index_soa(i
, 0);
4210 if (!(ctx
->input_mask
& (1ull << i
)))
4213 if (i
>= VARYING_SLOT_VAR0
|| i
== VARYING_SLOT_PNTC
||
4214 i
== VARYING_SLOT_PRIMITIVE_ID
|| i
== VARYING_SLOT_LAYER
) {
4215 interp_param
= *inputs
;
4216 interp_fs_input(ctx
, index
, interp_param
, ctx
->prim_mask
,
4220 ctx
->shader_info
->fs
.flat_shaded_mask
|= 1u << index
;
4222 } else if (i
== VARYING_SLOT_POS
) {
4223 for(int i
= 0; i
< 3; ++i
)
4224 inputs
[i
] = ctx
->frag_pos
[i
];
4226 inputs
[3] = ac_build_fdiv(&ctx
->ac
, ctx
->f32one
, ctx
->frag_pos
[3]);
4229 ctx
->shader_info
->fs
.num_interp
= index
;
4230 if (ctx
->input_mask
& (1 << VARYING_SLOT_PNTC
))
4231 ctx
->shader_info
->fs
.has_pcoord
= true;
4232 if (ctx
->input_mask
& (1 << VARYING_SLOT_PRIMITIVE_ID
))
4233 ctx
->shader_info
->fs
.prim_id_input
= true;
4234 if (ctx
->input_mask
& (1 << VARYING_SLOT_LAYER
))
4235 ctx
->shader_info
->fs
.layer_input
= true;
4236 ctx
->shader_info
->fs
.input_mask
= ctx
->input_mask
>> VARYING_SLOT_VAR0
;
4240 ac_build_alloca(struct nir_to_llvm_context
*ctx
,
4244 LLVMBuilderRef builder
= ctx
->builder
;
4245 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
4246 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
4247 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
4248 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
4249 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ctx
->context
);
4253 LLVMPositionBuilderBefore(first_builder
, first_instr
);
4255 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
4258 res
= LLVMBuildAlloca(first_builder
, type
, name
);
4259 LLVMBuildStore(builder
, LLVMConstNull(type
), res
);
4261 LLVMDisposeBuilder(first_builder
);
4266 static LLVMValueRef
si_build_alloca_undef(struct nir_to_llvm_context
*ctx
,
4270 LLVMValueRef ptr
= ac_build_alloca(ctx
, type
, name
);
4271 LLVMBuildStore(ctx
->builder
, LLVMGetUndef(type
), ptr
);
4276 handle_shader_output_decl(struct nir_to_llvm_context
*ctx
,
4277 struct nir_variable
*variable
)
4279 int idx
= variable
->data
.location
+ variable
->data
.index
;
4280 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4281 uint64_t mask_attribs
;
4282 variable
->data
.driver_location
= idx
* 4;
4284 mask_attribs
= ((1ull << attrib_count
) - 1) << idx
;
4285 if (ctx
->stage
== MESA_SHADER_VERTEX
||
4286 ctx
->stage
== MESA_SHADER_GEOMETRY
) {
4287 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
4288 int length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
4289 if (ctx
->stage
== MESA_SHADER_VERTEX
) {
4290 ctx
->shader_info
->vs
.outinfo
.clip_dist_mask
= (1 << ctx
->num_output_clips
) - 1;
4291 ctx
->shader_info
->vs
.outinfo
.cull_dist_mask
= (1 << ctx
->num_output_culls
) - 1;
4298 mask_attribs
= 1ull << idx
;
4302 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
4303 for (unsigned chan
= 0; chan
< 4; chan
++) {
4304 ctx
->outputs
[radeon_llvm_reg_index_soa(idx
+ i
, chan
)] =
4305 si_build_alloca_undef(ctx
, ctx
->f32
, "");
4308 ctx
->output_mask
|= mask_attribs
;
4312 setup_locals(struct nir_to_llvm_context
*ctx
,
4313 struct nir_function
*func
)
4316 ctx
->num_locals
= 0;
4317 nir_foreach_variable(variable
, &func
->impl
->locals
) {
4318 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4319 variable
->data
.driver_location
= ctx
->num_locals
* 4;
4320 ctx
->num_locals
+= attrib_count
;
4322 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
4326 for (i
= 0; i
< ctx
->num_locals
; i
++) {
4327 for (j
= 0; j
< 4; j
++) {
4328 ctx
->locals
[i
* 4 + j
] =
4329 si_build_alloca_undef(ctx
, ctx
->f32
, "temp");
4335 emit_float_saturate(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
, float lo
, float hi
)
4337 v
= to_float(ctx
, v
);
4338 v
= emit_intrin_2f_param(ctx
, "llvm.maxnum.f32", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, lo
));
4339 return emit_intrin_2f_param(ctx
, "llvm.minnum.f32", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, hi
));
4343 static LLVMValueRef
emit_pack_int16(struct nir_to_llvm_context
*ctx
,
4344 LLVMValueRef src0
, LLVMValueRef src1
)
4346 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
4347 LLVMValueRef comp
[2];
4349 comp
[0] = LLVMBuildAnd(ctx
->builder
, src0
, LLVMConstInt(ctx
-> i32
, 65535, 0), "");
4350 comp
[1] = LLVMBuildAnd(ctx
->builder
, src1
, LLVMConstInt(ctx
-> i32
, 65535, 0), "");
4351 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
4352 return LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
4355 /* Initialize arguments for the shader export intrinsic */
4357 si_llvm_init_export_args(struct nir_to_llvm_context
*ctx
,
4358 LLVMValueRef
*values
,
4360 struct ac_export_args
*args
)
4362 /* Default is 0xf. Adjusted below depending on the format. */
4363 args
->enabled_channels
= 0xf;
4365 /* Specify whether the EXEC mask represents the valid mask */
4366 args
->valid_mask
= 0;
4368 /* Specify whether this is the last export */
4371 /* Specify the target we are exporting */
4372 args
->target
= target
;
4374 args
->compr
= false;
4375 args
->out
[0] = LLVMGetUndef(ctx
->f32
);
4376 args
->out
[1] = LLVMGetUndef(ctx
->f32
);
4377 args
->out
[2] = LLVMGetUndef(ctx
->f32
);
4378 args
->out
[3] = LLVMGetUndef(ctx
->f32
);
4383 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&& target
>= V_008DFC_SQ_EXP_MRT
) {
4384 LLVMValueRef val
[4];
4385 unsigned index
= target
- V_008DFC_SQ_EXP_MRT
;
4386 unsigned col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * index
)) & 0xf;
4387 bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> index
) & 1;
4389 switch(col_format
) {
4390 case V_028714_SPI_SHADER_ZERO
:
4391 args
->enabled_channels
= 0; /* writemask */
4392 args
->target
= V_008DFC_SQ_EXP_NULL
;
4395 case V_028714_SPI_SHADER_32_R
:
4396 args
->enabled_channels
= 1;
4397 args
->out
[0] = values
[0];
4400 case V_028714_SPI_SHADER_32_GR
:
4401 args
->enabled_channels
= 0x3;
4402 args
->out
[0] = values
[0];
4403 args
->out
[1] = values
[1];
4406 case V_028714_SPI_SHADER_32_AR
:
4407 args
->enabled_channels
= 0x9;
4408 args
->out
[0] = values
[0];
4409 args
->out
[3] = values
[3];
4412 case V_028714_SPI_SHADER_FP16_ABGR
:
4415 for (unsigned chan
= 0; chan
< 2; chan
++) {
4416 LLVMValueRef pack_args
[2] = {
4418 values
[2 * chan
+ 1]
4420 LLVMValueRef packed
;
4422 packed
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, pack_args
);
4423 args
->out
[chan
] = packed
;
4427 case V_028714_SPI_SHADER_UNORM16_ABGR
:
4428 for (unsigned chan
= 0; chan
< 4; chan
++) {
4429 val
[chan
] = ac_build_clamp(&ctx
->ac
, values
[chan
]);
4430 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
4431 LLVMConstReal(ctx
->f32
, 65535), "");
4432 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
4433 LLVMConstReal(ctx
->f32
, 0.5), "");
4434 val
[chan
] = LLVMBuildFPToUI(ctx
->builder
, val
[chan
],
4439 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
4440 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
4443 case V_028714_SPI_SHADER_SNORM16_ABGR
:
4444 for (unsigned chan
= 0; chan
< 4; chan
++) {
4445 val
[chan
] = emit_float_saturate(ctx
, values
[chan
], -1, 1);
4446 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
4447 LLVMConstReal(ctx
->f32
, 32767), "");
4449 /* If positive, add 0.5, else add -0.5. */
4450 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
4451 LLVMBuildSelect(ctx
->builder
,
4452 LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
,
4453 val
[chan
], ctx
->f32zero
, ""),
4454 LLVMConstReal(ctx
->f32
, 0.5),
4455 LLVMConstReal(ctx
->f32
, -0.5), ""), "");
4456 val
[chan
] = LLVMBuildFPToSI(ctx
->builder
, val
[chan
], ctx
->i32
, "");
4460 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
4461 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
4464 case V_028714_SPI_SHADER_UINT16_ABGR
: {
4465 LLVMValueRef max
= LLVMConstInt(ctx
->i32
, is_int8
? 255 : 65535, 0);
4467 for (unsigned chan
= 0; chan
< 4; chan
++) {
4468 val
[chan
] = to_integer(ctx
, values
[chan
]);
4469 val
[chan
] = emit_minmax_int(ctx
, LLVMIntULT
, val
[chan
], max
);
4473 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
4474 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
4478 case V_028714_SPI_SHADER_SINT16_ABGR
: {
4479 LLVMValueRef max
= LLVMConstInt(ctx
->i32
, is_int8
? 127 : 32767, 0);
4480 LLVMValueRef min
= LLVMConstInt(ctx
->i32
, is_int8
? -128 : -32768, 0);
4483 for (unsigned chan
= 0; chan
< 4; chan
++) {
4484 val
[chan
] = to_integer(ctx
, values
[chan
]);
4485 val
[chan
] = emit_minmax_int(ctx
, LLVMIntSLT
, val
[chan
], max
);
4486 val
[chan
] = emit_minmax_int(ctx
, LLVMIntSGT
, val
[chan
], min
);
4490 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
4491 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
4496 case V_028714_SPI_SHADER_32_ABGR
:
4497 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
4501 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
4503 for (unsigned i
= 0; i
< 4; ++i
)
4504 args
->out
[i
] = to_float(ctx
, args
->out
[i
]);
4508 handle_vs_outputs_post(struct nir_to_llvm_context
*ctx
,
4509 struct ac_vs_output_info
*outinfo
)
4511 uint32_t param_count
= 0;
4513 unsigned pos_idx
, num_pos_exports
= 0;
4514 struct ac_export_args args
, pos_args
[4] = {};
4515 LLVMValueRef psize_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
4518 outinfo
->prim_id_output
= 0xffffffff;
4519 outinfo
->layer_output
= 0xffffffff;
4520 if (ctx
->output_mask
& (1ull << VARYING_SLOT_CLIP_DIST0
)) {
4521 LLVMValueRef slots
[8];
4524 if (outinfo
->cull_dist_mask
)
4525 outinfo
->cull_dist_mask
<<= ctx
->num_output_clips
;
4527 i
= VARYING_SLOT_CLIP_DIST0
;
4528 for (j
= 0; j
< ctx
->num_output_clips
+ ctx
->num_output_culls
; j
++)
4529 slots
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4530 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4532 for (i
= ctx
->num_output_clips
+ ctx
->num_output_culls
; i
< 8; i
++)
4533 slots
[i
] = LLVMGetUndef(ctx
->f32
);
4535 if (ctx
->num_output_clips
+ ctx
->num_output_culls
> 4) {
4536 target
= V_008DFC_SQ_EXP_POS
+ 3;
4537 si_llvm_init_export_args(ctx
, &slots
[4], target
, &args
);
4538 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
4539 &args
, sizeof(args
));
4542 target
= V_008DFC_SQ_EXP_POS
+ 2;
4543 si_llvm_init_export_args(ctx
, &slots
[0], target
, &args
);
4544 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
4545 &args
, sizeof(args
));
4549 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
4550 LLVMValueRef values
[4];
4551 if (!(ctx
->output_mask
& (1ull << i
)))
4554 for (unsigned j
= 0; j
< 4; j
++)
4555 values
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4556 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4558 if (i
== VARYING_SLOT_POS
) {
4559 target
= V_008DFC_SQ_EXP_POS
;
4560 } else if (i
== VARYING_SLOT_CLIP_DIST0
) {
4562 } else if (i
== VARYING_SLOT_PSIZ
) {
4563 outinfo
->writes_pointsize
= true;
4564 psize_value
= values
[0];
4566 } else if (i
== VARYING_SLOT_LAYER
) {
4567 outinfo
->writes_layer
= true;
4568 layer_value
= values
[0];
4569 outinfo
->layer_output
= param_count
;
4570 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
4572 } else if (i
== VARYING_SLOT_VIEWPORT
) {
4573 outinfo
->writes_viewport_index
= true;
4574 viewport_index_value
= values
[0];
4576 } else if (i
== VARYING_SLOT_PRIMITIVE_ID
) {
4577 outinfo
->prim_id_output
= param_count
;
4578 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
4580 } else if (i
>= VARYING_SLOT_VAR0
) {
4581 outinfo
->export_mask
|= 1u << (i
- VARYING_SLOT_VAR0
);
4582 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
4586 si_llvm_init_export_args(ctx
, values
, target
, &args
);
4588 if (target
>= V_008DFC_SQ_EXP_POS
&&
4589 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
4590 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
4591 &args
, sizeof(args
));
4593 ac_build_export(&ctx
->ac
, &args
);
4597 /* We need to add the position output manually if it's missing. */
4598 if (!pos_args
[0].out
[0]) {
4599 pos_args
[0].enabled_channels
= 0xf;
4600 pos_args
[0].valid_mask
= 0;
4601 pos_args
[0].done
= 0;
4602 pos_args
[0].target
= V_008DFC_SQ_EXP_POS
;
4603 pos_args
[0].compr
= 0;
4604 pos_args
[0].out
[0] = ctx
->f32zero
; /* X */
4605 pos_args
[0].out
[1] = ctx
->f32zero
; /* Y */
4606 pos_args
[0].out
[2] = ctx
->f32zero
; /* Z */
4607 pos_args
[0].out
[3] = ctx
->f32one
; /* W */
4610 uint32_t mask
= ((outinfo
->writes_pointsize
== true ? 1 : 0) |
4611 (outinfo
->writes_layer
== true ? 4 : 0) |
4612 (outinfo
->writes_viewport_index
== true ? 8 : 0));
4614 pos_args
[1].enabled_channels
= mask
;
4615 pos_args
[1].valid_mask
= 0;
4616 pos_args
[1].done
= 0;
4617 pos_args
[1].target
= V_008DFC_SQ_EXP_POS
+ 1;
4618 pos_args
[1].compr
= 0;
4619 pos_args
[1].out
[0] = ctx
->f32zero
; /* X */
4620 pos_args
[1].out
[1] = ctx
->f32zero
; /* Y */
4621 pos_args
[1].out
[2] = ctx
->f32zero
; /* Z */
4622 pos_args
[1].out
[3] = ctx
->f32zero
; /* W */
4624 if (outinfo
->writes_pointsize
== true)
4625 pos_args
[1].out
[0] = psize_value
;
4626 if (outinfo
->writes_layer
== true)
4627 pos_args
[1].out
[2] = layer_value
;
4628 if (outinfo
->writes_viewport_index
== true)
4629 pos_args
[1].out
[3] = viewport_index_value
;
4631 for (i
= 0; i
< 4; i
++) {
4632 if (pos_args
[i
].out
[0])
4637 for (i
= 0; i
< 4; i
++) {
4638 if (!pos_args
[i
].out
[0])
4641 /* Specify the target we are exporting */
4642 pos_args
[i
].target
= V_008DFC_SQ_EXP_POS
+ pos_idx
++;
4643 if (pos_idx
== num_pos_exports
)
4644 pos_args
[i
].done
= 1;
4645 ac_build_export(&ctx
->ac
, &pos_args
[i
]);
4648 outinfo
->pos_exports
= num_pos_exports
;
4649 outinfo
->param_exports
= param_count
;
4653 handle_es_outputs_post(struct nir_to_llvm_context
*ctx
,
4654 struct ac_es_output_info
*outinfo
)
4657 uint64_t max_output_written
= 0;
4658 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
4659 LLVMValueRef
*out_ptr
= &ctx
->outputs
[i
* 4];
4663 if (!(ctx
->output_mask
& (1ull << i
)))
4666 if (i
== VARYING_SLOT_CLIP_DIST0
)
4667 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
4669 param_index
= shader_io_get_unique_index(i
);
4671 max_output_written
= MAX2(param_index
+ (length
> 4), max_output_written
);
4673 for (j
= 0; j
< length
; j
++) {
4674 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], "");
4675 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->i32
, "");
4677 ac_build_buffer_store_dword(&ctx
->ac
,
4680 NULL
, ctx
->es2gs_offset
,
4681 (4 * param_index
+ j
) * 4,
4685 outinfo
->esgs_itemsize
= (max_output_written
+ 1) * 16;
4689 si_export_mrt_color(struct nir_to_llvm_context
*ctx
,
4690 LLVMValueRef
*color
, unsigned param
, bool is_last
)
4693 struct ac_export_args args
;
4696 si_llvm_init_export_args(ctx
, color
, param
,
4700 args
.valid_mask
= 1; /* whether the EXEC mask is valid */
4701 args
.done
= 1; /* DONE bit */
4702 } else if (!args
.enabled_channels
)
4703 return; /* unnecessary NULL export */
4705 ac_build_export(&ctx
->ac
, &args
);
4709 si_export_mrt_z(struct nir_to_llvm_context
*ctx
,
4710 LLVMValueRef depth
, LLVMValueRef stencil
,
4711 LLVMValueRef samplemask
)
4713 struct ac_export_args args
;
4715 args
.enabled_channels
= 0;
4716 args
.valid_mask
= 1;
4718 args
.target
= V_008DFC_SQ_EXP_MRTZ
;
4721 args
.out
[0] = LLVMGetUndef(ctx
->f32
); /* R, depth */
4722 args
.out
[1] = LLVMGetUndef(ctx
->f32
); /* G, stencil test val[0:7], stencil op val[8:15] */
4723 args
.out
[2] = LLVMGetUndef(ctx
->f32
); /* B, sample mask */
4724 args
.out
[3] = LLVMGetUndef(ctx
->f32
); /* A, alpha to mask */
4727 args
.out
[0] = depth
;
4728 args
.enabled_channels
|= 0x1;
4732 args
.out
[1] = stencil
;
4733 args
.enabled_channels
|= 0x2;
4737 args
.out
[2] = samplemask
;
4738 args
.enabled_channels
|= 0x4;
4741 /* SI (except OLAND) has a bug that it only looks
4742 * at the X writemask component. */
4743 if (ctx
->options
->chip_class
== SI
&&
4744 ctx
->options
->family
!= CHIP_OLAND
)
4745 args
.enabled_channels
|= 0x1;
4747 ac_build_export(&ctx
->ac
, &args
);
4751 handle_fs_outputs_post(struct nir_to_llvm_context
*ctx
)
4754 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
4756 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
4757 LLVMValueRef values
[4];
4759 if (!(ctx
->output_mask
& (1ull << i
)))
4762 if (i
== FRAG_RESULT_DEPTH
) {
4763 ctx
->shader_info
->fs
.writes_z
= true;
4764 depth
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4765 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
4766 } else if (i
== FRAG_RESULT_STENCIL
) {
4767 ctx
->shader_info
->fs
.writes_stencil
= true;
4768 stencil
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4769 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
4770 } else if (i
== FRAG_RESULT_SAMPLE_MASK
) {
4771 ctx
->shader_info
->fs
.writes_sample_mask
= true;
4772 samplemask
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4773 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
4776 for (unsigned j
= 0; j
< 4; j
++)
4777 values
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4778 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4780 if (!ctx
->shader_info
->fs
.writes_z
&& !ctx
->shader_info
->fs
.writes_stencil
&& !ctx
->shader_info
->fs
.writes_sample_mask
)
4781 last
= ctx
->output_mask
<= ((1ull << (i
+ 1)) - 1);
4783 si_export_mrt_color(ctx
, values
, V_008DFC_SQ_EXP_MRT
+ index
, last
);
4788 if (depth
|| stencil
|| samplemask
)
4789 si_export_mrt_z(ctx
, depth
, stencil
, samplemask
);
4791 si_export_mrt_color(ctx
, NULL
, V_008DFC_SQ_EXP_NULL
, true);
4793 ctx
->shader_info
->fs
.output_mask
= index
? ((1ull << index
) - 1) : 0;
4797 emit_gs_epilogue(struct nir_to_llvm_context
*ctx
)
4799 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_NOP
| AC_SENDMSG_GS_DONE
, ctx
->gs_wave_id
);
4803 handle_shader_outputs_post(struct nir_to_llvm_context
*ctx
)
4805 switch (ctx
->stage
) {
4806 case MESA_SHADER_VERTEX
:
4807 if (ctx
->options
->key
.vs
.as_es
)
4808 handle_es_outputs_post(ctx
, &ctx
->shader_info
->vs
.es_info
);
4810 handle_vs_outputs_post(ctx
, &ctx
->shader_info
->vs
.outinfo
);
4812 case MESA_SHADER_FRAGMENT
:
4813 handle_fs_outputs_post(ctx
);
4815 case MESA_SHADER_GEOMETRY
:
4816 emit_gs_epilogue(ctx
);
4824 handle_shared_compute_var(struct nir_to_llvm_context
*ctx
,
4825 struct nir_variable
*variable
, uint32_t *offset
, int idx
)
4827 unsigned size
= glsl_count_attribute_slots(variable
->type
, false);
4828 variable
->data
.driver_location
= *offset
;
4832 static void ac_llvm_finalize_module(struct nir_to_llvm_context
* ctx
)
4834 LLVMPassManagerRef passmgr
;
4835 /* Create the pass manager */
4836 passmgr
= LLVMCreateFunctionPassManagerForModule(
4839 /* This pass should eliminate all the load and store instructions */
4840 LLVMAddPromoteMemoryToRegisterPass(passmgr
);
4842 /* Add some optimization passes */
4843 LLVMAddScalarReplAggregatesPass(passmgr
);
4844 LLVMAddLICMPass(passmgr
);
4845 LLVMAddAggressiveDCEPass(passmgr
);
4846 LLVMAddCFGSimplificationPass(passmgr
);
4847 LLVMAddInstructionCombiningPass(passmgr
);
4850 LLVMInitializeFunctionPassManager(passmgr
);
4851 LLVMRunFunctionPassManager(passmgr
, ctx
->main_function
);
4852 LLVMFinalizeFunctionPassManager(passmgr
);
4854 LLVMDisposeBuilder(ctx
->builder
);
4855 LLVMDisposePassManager(passmgr
);
4859 ac_setup_rings(struct nir_to_llvm_context
*ctx
)
4861 if ((ctx
->stage
== MESA_SHADER_VERTEX
&& ctx
->options
->key
.vs
.as_es
) ||
4862 (ctx
->stage
== MESA_SHADER_TESS_EVAL
&& ctx
->options
->key
.tes
.as_es
)) {
4863 ctx
->esgs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_ESGS_VS
, false));
4866 if (ctx
->is_gs_copy_shader
) {
4867 ctx
->gsvs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_GSVS_VS
, false));
4869 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
4871 ctx
->esgs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_ESGS_GS
, false));
4872 ctx
->gsvs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_GSVS_GS
, false));
4874 ctx
->gsvs_ring
= LLVMBuildBitCast(ctx
->builder
, ctx
->gsvs_ring
, ctx
->v4i32
, "");
4876 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->gsvs_num_entries
, LLVMConstInt(ctx
->i32
, 2, false), "");
4877 tmp
= LLVMBuildExtractElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->i32one
, "");
4878 tmp
= LLVMBuildOr(ctx
->builder
, tmp
, ctx
->gsvs_ring_stride
, "");
4879 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, tmp
, ctx
->i32one
, "");
4881 ctx
->gsvs_ring
= LLVMBuildBitCast(ctx
->builder
, ctx
->gsvs_ring
, ctx
->v16i8
, "");
4884 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
4885 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
4886 ctx
->hs_ring_tess_offchip
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_HS_TESS_OFFCHIP
, false));
4887 ctx
->hs_ring_tess_factor
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_HS_TESS_FACTOR
, false));
4892 LLVMModuleRef
ac_translate_nir_to_llvm(LLVMTargetMachineRef tm
,
4893 struct nir_shader
*nir
,
4894 struct ac_shader_variant_info
*shader_info
,
4895 const struct ac_nir_compiler_options
*options
)
4897 struct nir_to_llvm_context ctx
= {0};
4898 struct nir_function
*func
;
4900 ctx
.options
= options
;
4901 ctx
.shader_info
= shader_info
;
4902 ctx
.context
= LLVMContextCreate();
4903 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
4905 ac_llvm_context_init(&ctx
.ac
, ctx
.context
);
4906 ctx
.ac
.module
= ctx
.module
;
4908 ctx
.has_ds_bpermute
= ctx
.options
->chip_class
>= VI
;
4910 memset(shader_info
, 0, sizeof(*shader_info
));
4912 LLVMSetTarget(ctx
.module
, options
->supports_spill
? "amdgcn-mesa-mesa3d" : "amdgcn--");
4914 LLVMTargetDataRef data_layout
= LLVMCreateTargetDataLayout(tm
);
4915 char *data_layout_str
= LLVMCopyStringRepOfTargetData(data_layout
);
4916 LLVMSetDataLayout(ctx
.module
, data_layout_str
);
4917 LLVMDisposeTargetData(data_layout
);
4918 LLVMDisposeMessage(data_layout_str
);
4922 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
4923 ctx
.ac
.builder
= ctx
.builder
;
4924 ctx
.stage
= nir
->stage
;
4926 for (i
= 0; i
< AC_UD_MAX_SETS
; i
++)
4927 shader_info
->user_sgprs_locs
.descriptor_sets
[i
].sgpr_idx
= -1;
4928 for (i
= 0; i
< AC_UD_MAX_UD
; i
++)
4929 shader_info
->user_sgprs_locs
.shader_data
[i
].sgpr_idx
= -1;
4931 create_function(&ctx
);
4933 if (nir
->stage
== MESA_SHADER_COMPUTE
) {
4935 nir_foreach_variable(variable
, &nir
->shared
)
4939 uint32_t shared_size
= 0;
4941 LLVMTypeRef i8p
= LLVMPointerType(ctx
.i8
, LOCAL_ADDR_SPACE
);
4942 nir_foreach_variable(variable
, &nir
->shared
) {
4943 handle_shared_compute_var(&ctx
, variable
, &shared_size
, idx
);
4948 var
= LLVMAddGlobalInAddressSpace(ctx
.module
,
4949 LLVMArrayType(ctx
.i8
, shared_size
),
4952 LLVMSetAlignment(var
, 4);
4953 ctx
.shared_memory
= LLVMBuildBitCast(ctx
.builder
, var
, i8p
, "");
4955 } else if (nir
->stage
== MESA_SHADER_GEOMETRY
) {
4956 ctx
.gs_next_vertex
= ac_build_alloca(&ctx
, ctx
.i32
, "gs_next_vertex");
4958 ctx
.gs_max_out_vertices
= nir
->info
->gs
.vertices_out
;
4961 ac_setup_rings(&ctx
);
4963 nir_foreach_variable(variable
, &nir
->inputs
)
4964 handle_shader_input_decl(&ctx
, variable
);
4966 if (nir
->stage
== MESA_SHADER_FRAGMENT
)
4967 handle_fs_inputs_pre(&ctx
, nir
);
4969 ctx
.num_output_clips
= nir
->info
->clip_distance_array_size
;
4970 ctx
.num_output_culls
= nir
->info
->cull_distance_array_size
;
4972 nir_foreach_variable(variable
, &nir
->outputs
)
4973 handle_shader_output_decl(&ctx
, variable
);
4975 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4976 _mesa_key_pointer_equal
);
4977 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4978 _mesa_key_pointer_equal
);
4980 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
4982 setup_locals(&ctx
, func
);
4984 visit_cf_list(&ctx
, &func
->impl
->body
);
4985 phi_post_pass(&ctx
);
4987 handle_shader_outputs_post(&ctx
);
4988 LLVMBuildRetVoid(ctx
.builder
);
4990 ac_llvm_finalize_module(&ctx
);
4992 ralloc_free(ctx
.defs
);
4993 ralloc_free(ctx
.phis
);
4995 if (nir
->stage
== MESA_SHADER_GEOMETRY
) {
4996 unsigned addclip
= ctx
.num_output_clips
+ ctx
.num_output_culls
> 4;
4997 shader_info
->gs
.gsvs_vertex_size
= (util_bitcount64(ctx
.output_mask
) + addclip
) * 16;
4998 shader_info
->gs
.max_gsvs_emit_size
= shader_info
->gs
.gsvs_vertex_size
*
4999 nir
->info
->gs
.vertices_out
;
5004 static void ac_diagnostic_handler(LLVMDiagnosticInfoRef di
, void *context
)
5006 unsigned *retval
= (unsigned *)context
;
5007 LLVMDiagnosticSeverity severity
= LLVMGetDiagInfoSeverity(di
);
5008 char *description
= LLVMGetDiagInfoDescription(di
);
5010 if (severity
== LLVMDSError
) {
5012 fprintf(stderr
, "LLVM triggered Diagnostic Handler: %s\n",
5016 LLVMDisposeMessage(description
);
5019 static unsigned ac_llvm_compile(LLVMModuleRef M
,
5020 struct ac_shader_binary
*binary
,
5021 LLVMTargetMachineRef tm
)
5023 unsigned retval
= 0;
5025 LLVMContextRef llvm_ctx
;
5026 LLVMMemoryBufferRef out_buffer
;
5027 unsigned buffer_size
;
5028 const char *buffer_data
;
5031 /* Setup Diagnostic Handler*/
5032 llvm_ctx
= LLVMGetModuleContext(M
);
5034 LLVMContextSetDiagnosticHandler(llvm_ctx
, ac_diagnostic_handler
,
5038 mem_err
= LLVMTargetMachineEmitToMemoryBuffer(tm
, M
, LLVMObjectFile
,
5041 /* Process Errors/Warnings */
5043 fprintf(stderr
, "%s: %s", __FUNCTION__
, err
);
5049 /* Extract Shader Code*/
5050 buffer_size
= LLVMGetBufferSize(out_buffer
);
5051 buffer_data
= LLVMGetBufferStart(out_buffer
);
5053 ac_elf_read(buffer_data
, buffer_size
, binary
);
5056 LLVMDisposeMemoryBuffer(out_buffer
);
5062 static void ac_compile_llvm_module(LLVMTargetMachineRef tm
,
5063 LLVMModuleRef llvm_module
,
5064 struct ac_shader_binary
*binary
,
5065 struct ac_shader_config
*config
,
5066 struct ac_shader_variant_info
*shader_info
,
5067 gl_shader_stage stage
,
5068 bool dump_shader
, bool supports_spill
)
5071 ac_dump_module(llvm_module
);
5073 memset(binary
, 0, sizeof(*binary
));
5074 int v
= ac_llvm_compile(llvm_module
, binary
, tm
);
5076 fprintf(stderr
, "compile failed\n");
5080 fprintf(stderr
, "disasm:\n%s\n", binary
->disasm_string
);
5082 ac_shader_binary_read_config(binary
, config
, 0, supports_spill
);
5084 LLVMContextRef ctx
= LLVMGetModuleContext(llvm_module
);
5085 LLVMDisposeModule(llvm_module
);
5086 LLVMContextDispose(ctx
);
5088 if (stage
== MESA_SHADER_FRAGMENT
) {
5089 shader_info
->num_input_vgprs
= 0;
5090 if (G_0286CC_PERSP_SAMPLE_ENA(config
->spi_ps_input_addr
))
5091 shader_info
->num_input_vgprs
+= 2;
5092 if (G_0286CC_PERSP_CENTER_ENA(config
->spi_ps_input_addr
))
5093 shader_info
->num_input_vgprs
+= 2;
5094 if (G_0286CC_PERSP_CENTROID_ENA(config
->spi_ps_input_addr
))
5095 shader_info
->num_input_vgprs
+= 2;
5096 if (G_0286CC_PERSP_PULL_MODEL_ENA(config
->spi_ps_input_addr
))
5097 shader_info
->num_input_vgprs
+= 3;
5098 if (G_0286CC_LINEAR_SAMPLE_ENA(config
->spi_ps_input_addr
))
5099 shader_info
->num_input_vgprs
+= 2;
5100 if (G_0286CC_LINEAR_CENTER_ENA(config
->spi_ps_input_addr
))
5101 shader_info
->num_input_vgprs
+= 2;
5102 if (G_0286CC_LINEAR_CENTROID_ENA(config
->spi_ps_input_addr
))
5103 shader_info
->num_input_vgprs
+= 2;
5104 if (G_0286CC_LINE_STIPPLE_TEX_ENA(config
->spi_ps_input_addr
))
5105 shader_info
->num_input_vgprs
+= 1;
5106 if (G_0286CC_POS_X_FLOAT_ENA(config
->spi_ps_input_addr
))
5107 shader_info
->num_input_vgprs
+= 1;
5108 if (G_0286CC_POS_Y_FLOAT_ENA(config
->spi_ps_input_addr
))
5109 shader_info
->num_input_vgprs
+= 1;
5110 if (G_0286CC_POS_Z_FLOAT_ENA(config
->spi_ps_input_addr
))
5111 shader_info
->num_input_vgprs
+= 1;
5112 if (G_0286CC_POS_W_FLOAT_ENA(config
->spi_ps_input_addr
))
5113 shader_info
->num_input_vgprs
+= 1;
5114 if (G_0286CC_FRONT_FACE_ENA(config
->spi_ps_input_addr
))
5115 shader_info
->num_input_vgprs
+= 1;
5116 if (G_0286CC_ANCILLARY_ENA(config
->spi_ps_input_addr
))
5117 shader_info
->num_input_vgprs
+= 1;
5118 if (G_0286CC_SAMPLE_COVERAGE_ENA(config
->spi_ps_input_addr
))
5119 shader_info
->num_input_vgprs
+= 1;
5120 if (G_0286CC_POS_FIXED_PT_ENA(config
->spi_ps_input_addr
))
5121 shader_info
->num_input_vgprs
+= 1;
5123 config
->num_vgprs
= MAX2(config
->num_vgprs
, shader_info
->num_input_vgprs
);
5125 /* +3 for scratch wave offset and VCC */
5126 config
->num_sgprs
= MAX2(config
->num_sgprs
,
5127 shader_info
->num_input_sgprs
+ 3);
5130 void ac_compile_nir_shader(LLVMTargetMachineRef tm
,
5131 struct ac_shader_binary
*binary
,
5132 struct ac_shader_config
*config
,
5133 struct ac_shader_variant_info
*shader_info
,
5134 struct nir_shader
*nir
,
5135 const struct ac_nir_compiler_options
*options
,
5139 LLVMModuleRef llvm_module
= ac_translate_nir_to_llvm(tm
, nir
, shader_info
,
5142 ac_compile_llvm_module(tm
, llvm_module
, binary
, config
, shader_info
, nir
->stage
, dump_shader
, options
->supports_spill
);
5143 switch (nir
->stage
) {
5144 case MESA_SHADER_COMPUTE
:
5145 for (int i
= 0; i
< 3; ++i
)
5146 shader_info
->cs
.block_size
[i
] = nir
->info
->cs
.local_size
[i
];
5148 case MESA_SHADER_FRAGMENT
:
5149 shader_info
->fs
.early_fragment_test
= nir
->info
->fs
.early_fragment_tests
;
5151 case MESA_SHADER_GEOMETRY
:
5152 shader_info
->gs
.vertices_in
= nir
->info
->gs
.vertices_in
;
5153 shader_info
->gs
.vertices_out
= nir
->info
->gs
.vertices_out
;
5154 shader_info
->gs
.output_prim
= nir
->info
->gs
.output_primitive
;
5155 shader_info
->gs
.invocations
= nir
->info
->gs
.invocations
;
5157 case MESA_SHADER_VERTEX
:
5158 shader_info
->vs
.as_es
= options
->key
.vs
.as_es
;
5166 ac_gs_copy_shader_emit(struct nir_to_llvm_context
*ctx
)
5168 LLVMValueRef args
[9];
5169 args
[0] = ctx
->gsvs_ring
;
5170 args
[1] = LLVMBuildMul(ctx
->builder
, ctx
->vertex_id
, LLVMConstInt(ctx
->i32
, 4, false), "");
5171 args
[3] = ctx
->i32zero
;
5172 args
[4] = ctx
->i32one
; /* OFFEN */
5173 args
[5] = ctx
->i32zero
; /* IDXEN */
5174 args
[6] = ctx
->i32one
; /* GLC */
5175 args
[7] = ctx
->i32one
; /* SLC */
5176 args
[8] = ctx
->i32zero
; /* TFE */
5180 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5184 if (!(ctx
->output_mask
& (1ull << i
)))
5187 if (i
== VARYING_SLOT_CLIP_DIST0
) {
5188 /* unpack clip and cull from a single set of slots */
5189 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
5194 for (unsigned j
= 0; j
< length
; j
++) {
5196 args
[2] = LLVMConstInt(ctx
->i32
,
5198 ctx
->gs_max_out_vertices
* 16 * 4, false);
5200 value
= ac_build_intrinsic(&ctx
->ac
,
5201 "llvm.SI.buffer.load.dword.i32.i32",
5203 AC_FUNC_ATTR_READONLY
|
5204 AC_FUNC_ATTR_LEGACY
);
5206 LLVMBuildStore(ctx
->builder
,
5207 to_float(ctx
, value
), ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)]);
5211 handle_vs_outputs_post(ctx
, &ctx
->shader_info
->vs
.outinfo
);
5214 void ac_create_gs_copy_shader(LLVMTargetMachineRef tm
,
5215 struct nir_shader
*geom_shader
,
5216 struct ac_shader_binary
*binary
,
5217 struct ac_shader_config
*config
,
5218 struct ac_shader_variant_info
*shader_info
,
5219 const struct ac_nir_compiler_options
*options
,
5222 struct nir_to_llvm_context ctx
= {0};
5223 ctx
.context
= LLVMContextCreate();
5224 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
5225 ctx
.options
= options
;
5226 ctx
.shader_info
= shader_info
;
5228 ac_llvm_context_init(&ctx
.ac
, ctx
.context
);
5229 ctx
.ac
.module
= ctx
.module
;
5231 ctx
.is_gs_copy_shader
= true;
5232 LLVMSetTarget(ctx
.module
, "amdgcn--");
5235 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
5236 ctx
.ac
.builder
= ctx
.builder
;
5237 ctx
.stage
= MESA_SHADER_VERTEX
;
5239 create_function(&ctx
);
5241 ctx
.gs_max_out_vertices
= geom_shader
->info
->gs
.vertices_out
;
5242 ac_setup_rings(&ctx
);
5244 ctx
.num_output_clips
= geom_shader
->info
->clip_distance_array_size
;
5245 ctx
.num_output_culls
= geom_shader
->info
->cull_distance_array_size
;
5247 nir_foreach_variable(variable
, &geom_shader
->outputs
)
5248 handle_shader_output_decl(&ctx
, variable
);
5250 ac_gs_copy_shader_emit(&ctx
);
5252 LLVMBuildRetVoid(ctx
.builder
);
5254 ac_llvm_finalize_module(&ctx
);
5256 ac_compile_llvm_module(tm
, ctx
.module
, binary
, config
, shader_info
,
5258 dump_shader
, options
->supports_spill
);