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
)
2921 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.s.barrier",
2922 ctx
->voidt
, NULL
, 0, 0);
2925 static void emit_discard_if(struct nir_to_llvm_context
*ctx
,
2926 nir_intrinsic_instr
*instr
)
2929 ctx
->shader_info
->fs
.can_discard
= true;
2931 cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
2932 get_src(ctx
, instr
->src
[0]),
2935 cond
= LLVMBuildSelect(ctx
->builder
, cond
,
2936 LLVMConstReal(ctx
->f32
, -1.0f
),
2938 ac_build_kill(&ctx
->ac
, cond
);
2942 visit_load_local_invocation_index(struct nir_to_llvm_context
*ctx
)
2944 LLVMValueRef result
;
2945 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
2946 result
= LLVMBuildAnd(ctx
->builder
, ctx
->tg_size
,
2947 LLVMConstInt(ctx
->i32
, 0xfc0, false), "");
2949 return LLVMBuildAdd(ctx
->builder
, result
, thread_id
, "");
2952 static LLVMValueRef
visit_var_atomic(struct nir_to_llvm_context
*ctx
,
2953 nir_intrinsic_instr
*instr
)
2955 LLVMValueRef ptr
, result
;
2956 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2957 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
2958 ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
2960 if (instr
->intrinsic
== nir_intrinsic_var_atomic_comp_swap
) {
2961 LLVMValueRef src1
= get_src(ctx
, instr
->src
[1]);
2962 result
= LLVMBuildAtomicCmpXchg(ctx
->builder
,
2964 LLVMAtomicOrderingSequentiallyConsistent
,
2965 LLVMAtomicOrderingSequentiallyConsistent
,
2968 LLVMAtomicRMWBinOp op
;
2969 switch (instr
->intrinsic
) {
2970 case nir_intrinsic_var_atomic_add
:
2971 op
= LLVMAtomicRMWBinOpAdd
;
2973 case nir_intrinsic_var_atomic_umin
:
2974 op
= LLVMAtomicRMWBinOpUMin
;
2976 case nir_intrinsic_var_atomic_umax
:
2977 op
= LLVMAtomicRMWBinOpUMax
;
2979 case nir_intrinsic_var_atomic_imin
:
2980 op
= LLVMAtomicRMWBinOpMin
;
2982 case nir_intrinsic_var_atomic_imax
:
2983 op
= LLVMAtomicRMWBinOpMax
;
2985 case nir_intrinsic_var_atomic_and
:
2986 op
= LLVMAtomicRMWBinOpAnd
;
2988 case nir_intrinsic_var_atomic_or
:
2989 op
= LLVMAtomicRMWBinOpOr
;
2991 case nir_intrinsic_var_atomic_xor
:
2992 op
= LLVMAtomicRMWBinOpXor
;
2994 case nir_intrinsic_var_atomic_exchange
:
2995 op
= LLVMAtomicRMWBinOpXchg
;
3001 result
= LLVMBuildAtomicRMW(ctx
->builder
, op
, ptr
, to_integer(ctx
, src
),
3002 LLVMAtomicOrderingSequentiallyConsistent
,
3008 #define INTERP_CENTER 0
3009 #define INTERP_CENTROID 1
3010 #define INTERP_SAMPLE 2
3012 static LLVMValueRef
lookup_interp_param(struct nir_to_llvm_context
*ctx
,
3013 enum glsl_interp_mode interp
, unsigned location
)
3016 case INTERP_MODE_FLAT
:
3019 case INTERP_MODE_SMOOTH
:
3020 case INTERP_MODE_NONE
:
3021 if (location
== INTERP_CENTER
)
3022 return ctx
->persp_center
;
3023 else if (location
== INTERP_CENTROID
)
3024 return ctx
->persp_centroid
;
3025 else if (location
== INTERP_SAMPLE
)
3026 return ctx
->persp_sample
;
3028 case INTERP_MODE_NOPERSPECTIVE
:
3029 if (location
== INTERP_CENTER
)
3030 return ctx
->linear_center
;
3031 else if (location
== INTERP_CENTROID
)
3032 return ctx
->linear_centroid
;
3033 else if (location
== INTERP_SAMPLE
)
3034 return ctx
->linear_sample
;
3040 static LLVMValueRef
load_sample_position(struct nir_to_llvm_context
*ctx
,
3041 LLVMValueRef sample_id
)
3043 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
3044 LLVMValueRef offset0
= LLVMBuildMul(ctx
->builder
, sample_id
, LLVMConstInt(ctx
->i32
, 8, false), "");
3045 LLVMValueRef offset1
= LLVMBuildAdd(ctx
->builder
, offset0
, LLVMConstInt(ctx
->i32
, 4, false), "");
3046 LLVMValueRef result
[2];
3048 result
[0] = ac_build_indexed_load_const(&ctx
->ac
, ctx
->sample_positions
, offset0
);
3049 result
[1] = ac_build_indexed_load_const(&ctx
->ac
, ctx
->sample_positions
, offset1
);
3051 return ac_build_gather_values(&ctx
->ac
, result
, 2);
3054 static LLVMValueRef
load_sample_pos(struct nir_to_llvm_context
*ctx
)
3056 LLVMValueRef values
[2];
3058 values
[0] = emit_ffract(ctx
, ctx
->frag_pos
[0]);
3059 values
[1] = emit_ffract(ctx
, ctx
->frag_pos
[1]);
3060 return ac_build_gather_values(&ctx
->ac
, values
, 2);
3063 static LLVMValueRef
visit_interp(struct nir_to_llvm_context
*ctx
,
3064 nir_intrinsic_instr
*instr
)
3066 LLVMValueRef result
[2];
3067 LLVMValueRef interp_param
, attr_number
;
3070 LLVMValueRef src_c0
, src_c1
;
3072 int input_index
= instr
->variables
[0]->var
->data
.location
- VARYING_SLOT_VAR0
;
3073 switch (instr
->intrinsic
) {
3074 case nir_intrinsic_interp_var_at_centroid
:
3075 location
= INTERP_CENTROID
;
3077 case nir_intrinsic_interp_var_at_sample
:
3078 location
= INTERP_SAMPLE
;
3079 src0
= get_src(ctx
, instr
->src
[0]);
3081 case nir_intrinsic_interp_var_at_offset
:
3082 location
= INTERP_CENTER
;
3083 src0
= get_src(ctx
, instr
->src
[0]);
3088 if (instr
->intrinsic
== nir_intrinsic_interp_var_at_offset
) {
3089 src_c0
= to_float(ctx
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32zero
, ""));
3090 src_c1
= to_float(ctx
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32one
, ""));
3091 } else if (instr
->intrinsic
== nir_intrinsic_interp_var_at_sample
) {
3092 LLVMValueRef sample_position
;
3093 LLVMValueRef halfval
= LLVMConstReal(ctx
->f32
, 0.5f
);
3095 /* fetch sample ID */
3096 sample_position
= load_sample_position(ctx
, src0
);
3098 src_c0
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->i32zero
, "");
3099 src_c0
= LLVMBuildFSub(ctx
->builder
, src_c0
, halfval
, "");
3100 src_c1
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->i32one
, "");
3101 src_c1
= LLVMBuildFSub(ctx
->builder
, src_c1
, halfval
, "");
3103 interp_param
= lookup_interp_param(ctx
, instr
->variables
[0]->var
->data
.interpolation
, location
);
3104 attr_number
= LLVMConstInt(ctx
->i32
, input_index
, false);
3106 if (location
== INTERP_SAMPLE
|| location
== INTERP_CENTER
) {
3107 LLVMValueRef ij_out
[2];
3108 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
, interp_param
);
3111 * take the I then J parameters, and the DDX/Y for it, and
3112 * calculate the IJ inputs for the interpolator.
3113 * temp1 = ddx * offset/sample.x + I;
3114 * interp_param.I = ddy * offset/sample.y + temp1;
3115 * temp1 = ddx * offset/sample.x + J;
3116 * interp_param.J = ddy * offset/sample.y + temp1;
3118 for (unsigned i
= 0; i
< 2; i
++) {
3119 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->i32
, i
, false);
3120 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->i32
, i
+ 2, false);
3121 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->builder
,
3122 ddxy_out
, ix_ll
, "");
3123 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->builder
,
3124 ddxy_out
, iy_ll
, "");
3125 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->builder
,
3126 interp_param
, ix_ll
, "");
3127 LLVMValueRef temp1
, temp2
;
3129 interp_el
= LLVMBuildBitCast(ctx
->builder
, interp_el
,
3132 temp1
= LLVMBuildFMul(ctx
->builder
, ddx_el
, src_c0
, "");
3133 temp1
= LLVMBuildFAdd(ctx
->builder
, temp1
, interp_el
, "");
3135 temp2
= LLVMBuildFMul(ctx
->builder
, ddy_el
, src_c1
, "");
3136 temp2
= LLVMBuildFAdd(ctx
->builder
, temp2
, temp1
, "");
3138 ij_out
[i
] = LLVMBuildBitCast(ctx
->builder
,
3139 temp2
, ctx
->i32
, "");
3141 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
3145 for (chan
= 0; chan
< 2; chan
++) {
3146 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
3149 interp_param
= LLVMBuildBitCast(ctx
->builder
,
3150 interp_param
, LLVMVectorType(ctx
->f32
, 2), "");
3151 LLVMValueRef i
= LLVMBuildExtractElement(
3152 ctx
->builder
, interp_param
, ctx
->i32zero
, "");
3153 LLVMValueRef j
= LLVMBuildExtractElement(
3154 ctx
->builder
, interp_param
, ctx
->i32one
, "");
3156 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
3157 llvm_chan
, attr_number
,
3158 ctx
->prim_mask
, i
, j
);
3160 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
3161 LLVMConstInt(ctx
->i32
, 2, false),
3162 llvm_chan
, attr_number
,
3166 return ac_build_gather_values(&ctx
->ac
, result
, 2);
3170 visit_emit_vertex(struct nir_to_llvm_context
*ctx
,
3171 nir_intrinsic_instr
*instr
)
3173 LLVMValueRef gs_next_vertex
;
3174 LLVMValueRef can_emit
, kill
;
3177 assert(instr
->const_index
[0] == 0);
3178 /* Write vertex attribute values to GSVS ring */
3179 gs_next_vertex
= LLVMBuildLoad(ctx
->builder
,
3180 ctx
->gs_next_vertex
,
3183 /* If this thread has already emitted the declared maximum number of
3184 * vertices, kill it: excessive vertex emissions are not supposed to
3185 * have any effect, and GS threads have no externally observable
3186 * effects other than emitting vertices.
3188 can_emit
= LLVMBuildICmp(ctx
->builder
, LLVMIntULT
, gs_next_vertex
,
3189 LLVMConstInt(ctx
->i32
, ctx
->gs_max_out_vertices
, false), "");
3191 kill
= LLVMBuildSelect(ctx
->builder
, can_emit
,
3192 LLVMConstReal(ctx
->f32
, 1.0f
),
3193 LLVMConstReal(ctx
->f32
, -1.0f
), "");
3194 ac_build_kill(&ctx
->ac
, kill
);
3196 /* loop num outputs */
3198 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
3199 LLVMValueRef
*out_ptr
= &ctx
->outputs
[i
* 4];
3204 if (!(ctx
->output_mask
& (1ull << i
)))
3207 if (i
== VARYING_SLOT_CLIP_DIST0
) {
3208 /* pack clip and cull into a single set of slots */
3209 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
3213 for (unsigned j
= 0; j
< length
; j
++) {
3214 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
,
3216 LLVMValueRef voffset
= LLVMConstInt(ctx
->i32
, (slot
* 4 + j
) * ctx
->gs_max_out_vertices
, false);
3217 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, gs_next_vertex
, "");
3218 voffset
= LLVMBuildMul(ctx
->builder
, voffset
, LLVMConstInt(ctx
->i32
, 4, false), "");
3220 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->i32
, "");
3222 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->gsvs_ring
,
3224 voffset
, ctx
->gs2vs_offset
, 0,
3230 gs_next_vertex
= LLVMBuildAdd(ctx
->builder
, gs_next_vertex
,
3232 LLVMBuildStore(ctx
->builder
, gs_next_vertex
, ctx
->gs_next_vertex
);
3234 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_EMIT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
3238 visit_end_primitive(struct nir_to_llvm_context
*ctx
,
3239 nir_intrinsic_instr
*instr
)
3241 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_CUT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
3244 static void visit_intrinsic(struct nir_to_llvm_context
*ctx
,
3245 nir_intrinsic_instr
*instr
)
3247 LLVMValueRef result
= NULL
;
3249 switch (instr
->intrinsic
) {
3250 case nir_intrinsic_load_work_group_id
: {
3251 result
= ctx
->workgroup_ids
;
3254 case nir_intrinsic_load_base_vertex
: {
3255 result
= ctx
->base_vertex
;
3258 case nir_intrinsic_load_vertex_id_zero_base
: {
3259 result
= ctx
->vertex_id
;
3262 case nir_intrinsic_load_local_invocation_id
: {
3263 result
= ctx
->local_invocation_ids
;
3266 case nir_intrinsic_load_base_instance
:
3267 result
= ctx
->start_instance
;
3269 case nir_intrinsic_load_draw_id
:
3270 result
= ctx
->draw_index
;
3272 case nir_intrinsic_load_invocation_id
:
3273 result
= ctx
->gs_invocation_id
;
3275 case nir_intrinsic_load_primitive_id
:
3276 if (ctx
->stage
== MESA_SHADER_GEOMETRY
)
3277 result
= ctx
->gs_prim_id
;
3279 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
3281 case nir_intrinsic_load_sample_id
:
3282 ctx
->shader_info
->fs
.force_persample
= true;
3283 result
= unpack_param(ctx
, ctx
->ancillary
, 8, 4);
3285 case nir_intrinsic_load_sample_pos
:
3286 ctx
->shader_info
->fs
.force_persample
= true;
3287 result
= load_sample_pos(ctx
);
3289 case nir_intrinsic_load_sample_mask_in
:
3290 result
= ctx
->sample_coverage
;
3292 case nir_intrinsic_load_front_face
:
3293 result
= ctx
->front_face
;
3295 case nir_intrinsic_load_instance_id
:
3296 result
= ctx
->instance_id
;
3297 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
3298 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
3300 case nir_intrinsic_load_num_work_groups
:
3301 result
= ctx
->num_work_groups
;
3303 case nir_intrinsic_load_local_invocation_index
:
3304 result
= visit_load_local_invocation_index(ctx
);
3306 case nir_intrinsic_load_push_constant
:
3307 result
= visit_load_push_constant(ctx
, instr
);
3309 case nir_intrinsic_vulkan_resource_index
:
3310 result
= visit_vulkan_resource_index(ctx
, instr
);
3312 case nir_intrinsic_store_ssbo
:
3313 visit_store_ssbo(ctx
, instr
);
3315 case nir_intrinsic_load_ssbo
:
3316 result
= visit_load_buffer(ctx
, instr
);
3318 case nir_intrinsic_ssbo_atomic_add
:
3319 case nir_intrinsic_ssbo_atomic_imin
:
3320 case nir_intrinsic_ssbo_atomic_umin
:
3321 case nir_intrinsic_ssbo_atomic_imax
:
3322 case nir_intrinsic_ssbo_atomic_umax
:
3323 case nir_intrinsic_ssbo_atomic_and
:
3324 case nir_intrinsic_ssbo_atomic_or
:
3325 case nir_intrinsic_ssbo_atomic_xor
:
3326 case nir_intrinsic_ssbo_atomic_exchange
:
3327 case nir_intrinsic_ssbo_atomic_comp_swap
:
3328 result
= visit_atomic_ssbo(ctx
, instr
);
3330 case nir_intrinsic_load_ubo
:
3331 result
= visit_load_ubo_buffer(ctx
, instr
);
3333 case nir_intrinsic_get_buffer_size
:
3334 result
= visit_get_buffer_size(ctx
, instr
);
3336 case nir_intrinsic_load_var
:
3337 result
= visit_load_var(ctx
, instr
);
3339 case nir_intrinsic_store_var
:
3340 visit_store_var(ctx
, instr
);
3342 case nir_intrinsic_image_load
:
3343 result
= visit_image_load(ctx
, instr
);
3345 case nir_intrinsic_image_store
:
3346 visit_image_store(ctx
, instr
);
3348 case nir_intrinsic_image_atomic_add
:
3349 case nir_intrinsic_image_atomic_min
:
3350 case nir_intrinsic_image_atomic_max
:
3351 case nir_intrinsic_image_atomic_and
:
3352 case nir_intrinsic_image_atomic_or
:
3353 case nir_intrinsic_image_atomic_xor
:
3354 case nir_intrinsic_image_atomic_exchange
:
3355 case nir_intrinsic_image_atomic_comp_swap
:
3356 result
= visit_image_atomic(ctx
, instr
);
3358 case nir_intrinsic_image_size
:
3359 result
= visit_image_size(ctx
, instr
);
3361 case nir_intrinsic_discard
:
3362 ctx
->shader_info
->fs
.can_discard
= true;
3363 ac_build_intrinsic(&ctx
->ac
, "llvm.AMDGPU.kilp",
3365 NULL
, 0, AC_FUNC_ATTR_LEGACY
);
3367 case nir_intrinsic_discard_if
:
3368 emit_discard_if(ctx
, instr
);
3370 case nir_intrinsic_memory_barrier
:
3371 emit_waitcnt(ctx
, VM_CNT
);
3373 case nir_intrinsic_barrier
:
3376 case nir_intrinsic_var_atomic_add
:
3377 case nir_intrinsic_var_atomic_imin
:
3378 case nir_intrinsic_var_atomic_umin
:
3379 case nir_intrinsic_var_atomic_imax
:
3380 case nir_intrinsic_var_atomic_umax
:
3381 case nir_intrinsic_var_atomic_and
:
3382 case nir_intrinsic_var_atomic_or
:
3383 case nir_intrinsic_var_atomic_xor
:
3384 case nir_intrinsic_var_atomic_exchange
:
3385 case nir_intrinsic_var_atomic_comp_swap
:
3386 result
= visit_var_atomic(ctx
, instr
);
3388 case nir_intrinsic_interp_var_at_centroid
:
3389 case nir_intrinsic_interp_var_at_sample
:
3390 case nir_intrinsic_interp_var_at_offset
:
3391 result
= visit_interp(ctx
, instr
);
3393 case nir_intrinsic_emit_vertex
:
3394 visit_emit_vertex(ctx
, instr
);
3396 case nir_intrinsic_end_primitive
:
3397 visit_end_primitive(ctx
, instr
);
3400 fprintf(stderr
, "Unknown intrinsic: ");
3401 nir_print_instr(&instr
->instr
, stderr
);
3402 fprintf(stderr
, "\n");
3406 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
3410 static LLVMValueRef
get_sampler_desc(struct nir_to_llvm_context
*ctx
,
3411 nir_deref_var
*deref
,
3412 enum desc_type desc_type
)
3414 unsigned desc_set
= deref
->var
->data
.descriptor_set
;
3415 LLVMValueRef list
= ctx
->descriptor_sets
[desc_set
];
3416 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[desc_set
].layout
;
3417 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ deref
->var
->data
.binding
;
3418 unsigned offset
= binding
->offset
;
3419 unsigned stride
= binding
->size
;
3421 LLVMBuilderRef builder
= ctx
->builder
;
3423 LLVMValueRef index
= NULL
;
3424 unsigned constant_index
= 0;
3426 assert(deref
->var
->data
.binding
< layout
->binding_count
);
3428 switch (desc_type
) {
3440 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
3450 unreachable("invalid desc_type\n");
3453 if (deref
->deref
.child
) {
3454 nir_deref_array
*child
= (nir_deref_array
*)deref
->deref
.child
;
3456 assert(child
->deref_array_type
!= nir_deref_array_type_wildcard
);
3457 offset
+= child
->base_offset
* stride
;
3458 if (child
->deref_array_type
== nir_deref_array_type_indirect
) {
3459 index
= get_src(ctx
, child
->indirect
);
3462 constant_index
= child
->base_offset
;
3464 if (desc_type
== DESC_SAMPLER
&& binding
->immutable_samplers
&&
3465 (!index
|| binding
->immutable_samplers_equal
)) {
3466 if (binding
->immutable_samplers_equal
)
3469 LLVMValueRef constants
[] = {
3470 LLVMConstInt(ctx
->i32
, binding
->immutable_samplers
[constant_index
* 4 + 0], 0),
3471 LLVMConstInt(ctx
->i32
, binding
->immutable_samplers
[constant_index
* 4 + 1], 0),
3472 LLVMConstInt(ctx
->i32
, binding
->immutable_samplers
[constant_index
* 4 + 2], 0),
3473 LLVMConstInt(ctx
->i32
, binding
->immutable_samplers
[constant_index
* 4 + 3], 0),
3475 return ac_build_gather_values(&ctx
->ac
, constants
, 4);
3478 assert(stride
% type_size
== 0);
3481 index
= ctx
->i32zero
;
3483 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, stride
/ type_size
, 0), "");
3485 list
= ac_build_gep0(&ctx
->ac
, list
, LLVMConstInt(ctx
->i32
, offset
, 0));
3486 list
= LLVMBuildPointerCast(builder
, list
, const_array(type
, 0), "");
3488 return ac_build_indexed_load_const(&ctx
->ac
, list
, index
);
3491 static void set_tex_fetch_args(struct nir_to_llvm_context
*ctx
,
3492 struct ac_image_args
*args
,
3493 nir_tex_instr
*instr
,
3495 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
3496 LLVMValueRef
*param
, unsigned count
,
3499 unsigned is_rect
= 0;
3500 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
3502 if (op
== nir_texop_lod
)
3504 /* Pad to power of two vector */
3505 while (count
< util_next_power_of_two(count
))
3506 param
[count
++] = LLVMGetUndef(ctx
->i32
);
3509 args
->addr
= ac_build_gather_values(&ctx
->ac
, param
, count
);
3511 args
->addr
= param
[0];
3513 args
->resource
= res_ptr
;
3514 args
->sampler
= samp_ptr
;
3516 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
&& op
== nir_texop_txf
) {
3517 args
->addr
= param
[0];
3521 args
->dmask
= dmask
;
3522 args
->unorm
= is_rect
;
3526 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
3529 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
3530 * filtering manually. The driver sets img7 to a mask clearing
3531 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
3532 * s_and_b32 samp0, samp0, img7
3535 * The ANISO_OVERRIDE sampler field enables this fix in TA.
3537 static LLVMValueRef
sici_fix_sampler_aniso(struct nir_to_llvm_context
*ctx
,
3538 LLVMValueRef res
, LLVMValueRef samp
)
3540 LLVMBuilderRef builder
= ctx
->builder
;
3541 LLVMValueRef img7
, samp0
;
3543 if (ctx
->options
->chip_class
>= VI
)
3546 img7
= LLVMBuildExtractElement(builder
, res
,
3547 LLVMConstInt(ctx
->i32
, 7, 0), "");
3548 samp0
= LLVMBuildExtractElement(builder
, samp
,
3549 LLVMConstInt(ctx
->i32
, 0, 0), "");
3550 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
3551 return LLVMBuildInsertElement(builder
, samp
, samp0
,
3552 LLVMConstInt(ctx
->i32
, 0, 0), "");
3555 static void tex_fetch_ptrs(struct nir_to_llvm_context
*ctx
,
3556 nir_tex_instr
*instr
,
3557 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
3558 LLVMValueRef
*fmask_ptr
)
3560 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
3561 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_BUFFER
);
3563 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_IMAGE
);
3566 *samp_ptr
= get_sampler_desc(ctx
, instr
->sampler
, DESC_SAMPLER
);
3568 *samp_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_SAMPLER
);
3569 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
3570 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
3572 if (fmask_ptr
&& !instr
->sampler
&& (instr
->op
== nir_texop_txf_ms
||
3573 instr
->op
== nir_texop_samples_identical
))
3574 *fmask_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_FMASK
);
3577 static LLVMValueRef
apply_round_slice(struct nir_to_llvm_context
*ctx
,
3580 coord
= to_float(ctx
, coord
);
3581 coord
= ac_build_intrinsic(&ctx
->ac
, "llvm.rint.f32", ctx
->f32
, &coord
, 1, 0);
3582 coord
= to_integer(ctx
, coord
);
3586 static void visit_tex(struct nir_to_llvm_context
*ctx
, nir_tex_instr
*instr
)
3588 LLVMValueRef result
= NULL
;
3589 struct ac_image_args args
= { 0 };
3590 unsigned dmask
= 0xf;
3591 LLVMValueRef address
[16];
3592 LLVMValueRef coords
[5];
3593 LLVMValueRef coord
= NULL
, lod
= NULL
, comparator
= NULL
;
3594 LLVMValueRef bias
= NULL
, offsets
= NULL
;
3595 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
, sample_index
= NULL
;
3596 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
3597 LLVMValueRef derivs
[6];
3598 unsigned chan
, count
= 0;
3599 unsigned const_src
= 0, num_deriv_comp
= 0;
3601 tex_fetch_ptrs(ctx
, instr
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
3603 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3604 switch (instr
->src
[i
].src_type
) {
3605 case nir_tex_src_coord
:
3606 coord
= get_src(ctx
, instr
->src
[i
].src
);
3608 case nir_tex_src_projector
:
3610 case nir_tex_src_comparator
:
3611 comparator
= get_src(ctx
, instr
->src
[i
].src
);
3613 case nir_tex_src_offset
:
3614 offsets
= get_src(ctx
, instr
->src
[i
].src
);
3617 case nir_tex_src_bias
:
3618 bias
= get_src(ctx
, instr
->src
[i
].src
);
3620 case nir_tex_src_lod
:
3621 lod
= get_src(ctx
, instr
->src
[i
].src
);
3623 case nir_tex_src_ms_index
:
3624 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
3626 case nir_tex_src_ms_mcs
:
3628 case nir_tex_src_ddx
:
3629 ddx
= get_src(ctx
, instr
->src
[i
].src
);
3630 num_deriv_comp
= instr
->src
[i
].src
.ssa
->num_components
;
3632 case nir_tex_src_ddy
:
3633 ddy
= get_src(ctx
, instr
->src
[i
].src
);
3635 case nir_tex_src_texture_offset
:
3636 case nir_tex_src_sampler_offset
:
3637 case nir_tex_src_plane
:
3643 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
3644 result
= get_buffer_size(ctx
, res_ptr
, true);
3648 if (instr
->op
== nir_texop_texture_samples
) {
3649 LLVMValueRef res
, samples
, is_msaa
;
3650 res
= LLVMBuildBitCast(ctx
->builder
, res_ptr
, ctx
->v8i32
, "");
3651 samples
= LLVMBuildExtractElement(ctx
->builder
, res
,
3652 LLVMConstInt(ctx
->i32
, 3, false), "");
3653 is_msaa
= LLVMBuildLShr(ctx
->builder
, samples
,
3654 LLVMConstInt(ctx
->i32
, 28, false), "");
3655 is_msaa
= LLVMBuildAnd(ctx
->builder
, is_msaa
,
3656 LLVMConstInt(ctx
->i32
, 0xe, false), "");
3657 is_msaa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, is_msaa
,
3658 LLVMConstInt(ctx
->i32
, 0xe, false), "");
3660 samples
= LLVMBuildLShr(ctx
->builder
, samples
,
3661 LLVMConstInt(ctx
->i32
, 16, false), "");
3662 samples
= LLVMBuildAnd(ctx
->builder
, samples
,
3663 LLVMConstInt(ctx
->i32
, 0xf, false), "");
3664 samples
= LLVMBuildShl(ctx
->builder
, ctx
->i32one
,
3666 samples
= LLVMBuildSelect(ctx
->builder
, is_msaa
, samples
,
3673 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
3674 coords
[chan
] = llvm_extract_elem(ctx
, coord
, chan
);
3676 if (offsets
&& instr
->op
!= nir_texop_txf
) {
3677 LLVMValueRef offset
[3], pack
;
3678 for (chan
= 0; chan
< 3; ++chan
)
3679 offset
[chan
] = ctx
->i32zero
;
3682 for (chan
= 0; chan
< get_llvm_num_components(offsets
); chan
++) {
3683 offset
[chan
] = llvm_extract_elem(ctx
, offsets
, chan
);
3684 offset
[chan
] = LLVMBuildAnd(ctx
->builder
, offset
[chan
],
3685 LLVMConstInt(ctx
->i32
, 0x3f, false), "");
3687 offset
[chan
] = LLVMBuildShl(ctx
->builder
, offset
[chan
],
3688 LLVMConstInt(ctx
->i32
, chan
* 8, false), "");
3690 pack
= LLVMBuildOr(ctx
->builder
, offset
[0], offset
[1], "");
3691 pack
= LLVMBuildOr(ctx
->builder
, pack
, offset
[2], "");
3692 address
[count
++] = pack
;
3695 /* pack LOD bias value */
3696 if (instr
->op
== nir_texop_txb
&& bias
) {
3697 address
[count
++] = bias
;
3700 /* Pack depth comparison value */
3701 if (instr
->is_shadow
&& comparator
) {
3702 address
[count
++] = llvm_extract_elem(ctx
, comparator
, 0);
3705 /* pack derivatives */
3707 switch (instr
->sampler_dim
) {
3708 case GLSL_SAMPLER_DIM_3D
:
3709 case GLSL_SAMPLER_DIM_CUBE
:
3712 case GLSL_SAMPLER_DIM_2D
:
3716 case GLSL_SAMPLER_DIM_1D
:
3721 for (unsigned i
= 0; i
< num_deriv_comp
; i
++) {
3722 derivs
[i
* 2] = to_float(ctx
, llvm_extract_elem(ctx
, ddx
, i
));
3723 derivs
[i
* 2 + 1] = to_float(ctx
, llvm_extract_elem(ctx
, ddy
, i
));
3727 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& coord
) {
3728 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
3729 coords
[chan
] = to_float(ctx
, coords
[chan
]);
3730 if (instr
->coord_components
== 3)
3731 coords
[3] = LLVMGetUndef(ctx
->f32
);
3732 ac_prepare_cube_coords(&ctx
->ac
,
3733 instr
->op
== nir_texop_txd
, instr
->is_array
,
3740 for (unsigned i
= 0; i
< num_deriv_comp
* 2; i
++)
3741 address
[count
++] = derivs
[i
];
3744 /* Pack texture coordinates */
3746 address
[count
++] = coords
[0];
3747 if (instr
->coord_components
> 1) {
3748 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&& instr
->is_array
&& instr
->op
!= nir_texop_txf
) {
3749 coords
[1] = apply_round_slice(ctx
, coords
[1]);
3751 address
[count
++] = coords
[1];
3753 if (instr
->coord_components
> 2) {
3754 /* This seems like a bit of a hack - but it passes Vulkan CTS with it */
3755 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_3D
&& instr
->op
!= nir_texop_txf
) {
3756 coords
[2] = apply_round_slice(ctx
, coords
[2]);
3758 address
[count
++] = coords
[2];
3763 if ((instr
->op
== nir_texop_txl
|| instr
->op
== nir_texop_txf
) && lod
) {
3764 address
[count
++] = lod
;
3765 } else if (instr
->op
== nir_texop_txf_ms
&& sample_index
) {
3766 address
[count
++] = sample_index
;
3767 } else if(instr
->op
== nir_texop_txs
) {
3770 address
[count
++] = lod
;
3772 address
[count
++] = ctx
->i32zero
;
3775 for (chan
= 0; chan
< count
; chan
++) {
3776 address
[chan
] = LLVMBuildBitCast(ctx
->builder
,
3777 address
[chan
], ctx
->i32
, "");
3780 if (instr
->op
== nir_texop_samples_identical
) {
3781 LLVMValueRef txf_address
[4];
3782 struct ac_image_args txf_args
= { 0 };
3783 unsigned txf_count
= count
;
3784 memcpy(txf_address
, address
, sizeof(txf_address
));
3786 if (!instr
->is_array
)
3787 txf_address
[2] = ctx
->i32zero
;
3788 txf_address
[3] = ctx
->i32zero
;
3790 set_tex_fetch_args(ctx
, &txf_args
, instr
, nir_texop_txf
,
3792 txf_address
, txf_count
, 0xf);
3794 result
= build_tex_intrinsic(ctx
, instr
, &txf_args
);
3796 result
= LLVMBuildExtractElement(ctx
->builder
, result
, ctx
->i32zero
, "");
3797 result
= emit_int_cmp(ctx
, LLVMIntEQ
, result
, ctx
->i32zero
);
3801 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
3802 instr
->op
!= nir_texop_txs
) {
3803 unsigned sample_chan
= instr
->is_array
? 3 : 2;
3804 address
[sample_chan
] = adjust_sample_index_using_fmask(ctx
,
3807 instr
->is_array
? address
[2] : NULL
,
3808 address
[sample_chan
],
3812 if (offsets
&& instr
->op
== nir_texop_txf
) {
3813 nir_const_value
*const_offset
=
3814 nir_src_as_const_value(instr
->src
[const_src
].src
);
3815 int num_offsets
= instr
->src
[const_src
].src
.ssa
->num_components
;
3816 assert(const_offset
);
3817 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
3818 if (num_offsets
> 2)
3819 address
[2] = LLVMBuildAdd(ctx
->builder
,
3820 address
[2], LLVMConstInt(ctx
->i32
, const_offset
->i32
[2], false), "");
3821 if (num_offsets
> 1)
3822 address
[1] = LLVMBuildAdd(ctx
->builder
,
3823 address
[1], LLVMConstInt(ctx
->i32
, const_offset
->i32
[1], false), "");
3824 address
[0] = LLVMBuildAdd(ctx
->builder
,
3825 address
[0], LLVMConstInt(ctx
->i32
, const_offset
->i32
[0], false), "");
3829 /* TODO TG4 support */
3830 if (instr
->op
== nir_texop_tg4
) {
3831 if (instr
->is_shadow
)
3834 dmask
= 1 << instr
->component
;
3836 set_tex_fetch_args(ctx
, &args
, instr
, instr
->op
,
3837 res_ptr
, samp_ptr
, address
, count
, dmask
);
3839 result
= build_tex_intrinsic(ctx
, instr
, &args
);
3841 if (instr
->op
== nir_texop_query_levels
)
3842 result
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, 3, false), "");
3843 else if (instr
->is_shadow
&& instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&& instr
->op
!= nir_texop_tg4
)
3844 result
= LLVMBuildExtractElement(ctx
->builder
, result
, ctx
->i32zero
, "");
3845 else if (instr
->op
== nir_texop_txs
&&
3846 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
3848 LLVMValueRef two
= LLVMConstInt(ctx
->i32
, 2, false);
3849 LLVMValueRef six
= LLVMConstInt(ctx
->i32
, 6, false);
3850 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->builder
, result
, two
, "");
3851 z
= LLVMBuildSDiv(ctx
->builder
, z
, six
, "");
3852 result
= LLVMBuildInsertElement(ctx
->builder
, result
, z
, two
, "");
3853 } else if (instr
->dest
.ssa
.num_components
!= 4)
3854 result
= trim_vector(ctx
, result
, instr
->dest
.ssa
.num_components
);
3858 assert(instr
->dest
.is_ssa
);
3859 result
= to_integer(ctx
, result
);
3860 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
3865 static void visit_phi(struct nir_to_llvm_context
*ctx
, nir_phi_instr
*instr
)
3867 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
3868 LLVMValueRef result
= LLVMBuildPhi(ctx
->builder
, type
, "");
3870 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
3871 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
3874 static void visit_post_phi(struct nir_to_llvm_context
*ctx
,
3875 nir_phi_instr
*instr
,
3876 LLVMValueRef llvm_phi
)
3878 nir_foreach_phi_src(src
, instr
) {
3879 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
3880 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
3882 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
3886 static void phi_post_pass(struct nir_to_llvm_context
*ctx
)
3888 struct hash_entry
*entry
;
3889 hash_table_foreach(ctx
->phis
, entry
) {
3890 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
3891 (LLVMValueRef
)entry
->data
);
3896 static void visit_ssa_undef(struct nir_to_llvm_context
*ctx
,
3897 nir_ssa_undef_instr
*instr
)
3899 unsigned num_components
= instr
->def
.num_components
;
3902 if (num_components
== 1)
3903 undef
= LLVMGetUndef(ctx
->i32
);
3905 undef
= LLVMGetUndef(LLVMVectorType(ctx
->i32
, num_components
));
3907 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, undef
);
3910 static void visit_jump(struct nir_to_llvm_context
*ctx
,
3911 nir_jump_instr
*instr
)
3913 switch (instr
->type
) {
3914 case nir_jump_break
:
3915 LLVMBuildBr(ctx
->builder
, ctx
->break_block
);
3916 LLVMClearInsertionPosition(ctx
->builder
);
3918 case nir_jump_continue
:
3919 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
3920 LLVMClearInsertionPosition(ctx
->builder
);
3923 fprintf(stderr
, "Unknown NIR jump instr: ");
3924 nir_print_instr(&instr
->instr
, stderr
);
3925 fprintf(stderr
, "\n");
3930 static void visit_cf_list(struct nir_to_llvm_context
*ctx
,
3931 struct exec_list
*list
);
3933 static void visit_block(struct nir_to_llvm_context
*ctx
, nir_block
*block
)
3935 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->builder
);
3936 nir_foreach_instr(instr
, block
)
3938 switch (instr
->type
) {
3939 case nir_instr_type_alu
:
3940 visit_alu(ctx
, nir_instr_as_alu(instr
));
3942 case nir_instr_type_load_const
:
3943 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
3945 case nir_instr_type_intrinsic
:
3946 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
3948 case nir_instr_type_tex
:
3949 visit_tex(ctx
, nir_instr_as_tex(instr
));
3951 case nir_instr_type_phi
:
3952 visit_phi(ctx
, nir_instr_as_phi(instr
));
3954 case nir_instr_type_ssa_undef
:
3955 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
3957 case nir_instr_type_jump
:
3958 visit_jump(ctx
, nir_instr_as_jump(instr
));
3961 fprintf(stderr
, "Unknown NIR instr type: ");
3962 nir_print_instr(instr
, stderr
);
3963 fprintf(stderr
, "\n");
3968 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
3971 static void visit_if(struct nir_to_llvm_context
*ctx
, nir_if
*if_stmt
)
3973 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
3975 LLVMBasicBlockRef merge_block
=
3976 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
3977 LLVMBasicBlockRef if_block
=
3978 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
3979 LLVMBasicBlockRef else_block
= merge_block
;
3980 if (!exec_list_is_empty(&if_stmt
->else_list
))
3981 else_block
= LLVMAppendBasicBlockInContext(
3982 ctx
->context
, ctx
->main_function
, "");
3984 LLVMValueRef cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, value
,
3985 LLVMConstInt(ctx
->i32
, 0, false), "");
3986 LLVMBuildCondBr(ctx
->builder
, cond
, if_block
, else_block
);
3988 LLVMPositionBuilderAtEnd(ctx
->builder
, if_block
);
3989 visit_cf_list(ctx
, &if_stmt
->then_list
);
3990 if (LLVMGetInsertBlock(ctx
->builder
))
3991 LLVMBuildBr(ctx
->builder
, merge_block
);
3993 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
3994 LLVMPositionBuilderAtEnd(ctx
->builder
, else_block
);
3995 visit_cf_list(ctx
, &if_stmt
->else_list
);
3996 if (LLVMGetInsertBlock(ctx
->builder
))
3997 LLVMBuildBr(ctx
->builder
, merge_block
);
4000 LLVMPositionBuilderAtEnd(ctx
->builder
, merge_block
);
4003 static void visit_loop(struct nir_to_llvm_context
*ctx
, nir_loop
*loop
)
4005 LLVMBasicBlockRef continue_parent
= ctx
->continue_block
;
4006 LLVMBasicBlockRef break_parent
= ctx
->break_block
;
4008 ctx
->continue_block
=
4009 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
4011 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
4013 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
4014 LLVMPositionBuilderAtEnd(ctx
->builder
, ctx
->continue_block
);
4015 visit_cf_list(ctx
, &loop
->body
);
4017 if (LLVMGetInsertBlock(ctx
->builder
))
4018 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
4019 LLVMPositionBuilderAtEnd(ctx
->builder
, ctx
->break_block
);
4021 ctx
->continue_block
= continue_parent
;
4022 ctx
->break_block
= break_parent
;
4025 static void visit_cf_list(struct nir_to_llvm_context
*ctx
,
4026 struct exec_list
*list
)
4028 foreach_list_typed(nir_cf_node
, node
, node
, list
)
4030 switch (node
->type
) {
4031 case nir_cf_node_block
:
4032 visit_block(ctx
, nir_cf_node_as_block(node
));
4035 case nir_cf_node_if
:
4036 visit_if(ctx
, nir_cf_node_as_if(node
));
4039 case nir_cf_node_loop
:
4040 visit_loop(ctx
, nir_cf_node_as_loop(node
));
4050 handle_vs_input_decl(struct nir_to_llvm_context
*ctx
,
4051 struct nir_variable
*variable
)
4053 LLVMValueRef t_list_ptr
= ctx
->vertex_buffers
;
4054 LLVMValueRef t_offset
;
4055 LLVMValueRef t_list
;
4056 LLVMValueRef args
[3];
4058 LLVMValueRef buffer_index
;
4059 int index
= variable
->data
.location
- VERT_ATTRIB_GENERIC0
;
4060 int idx
= variable
->data
.location
;
4061 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, true);
4063 variable
->data
.driver_location
= idx
* 4;
4065 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << index
)) {
4066 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->instance_id
,
4067 ctx
->start_instance
, "");
4068 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
4069 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
4071 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->vertex_id
,
4072 ctx
->base_vertex
, "");
4074 for (unsigned i
= 0; i
< attrib_count
; ++i
, ++idx
) {
4075 t_offset
= LLVMConstInt(ctx
->i32
, index
+ i
, false);
4077 t_list
= ac_build_indexed_load_const(&ctx
->ac
, t_list_ptr
, t_offset
);
4079 args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
4080 args
[2] = buffer_index
;
4081 input
= ac_build_intrinsic(&ctx
->ac
,
4082 "llvm.SI.vs.load.input", ctx
->v4f32
, args
, 3,
4083 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_NOUNWIND
|
4084 AC_FUNC_ATTR_LEGACY
);
4086 for (unsigned chan
= 0; chan
< 4; chan
++) {
4087 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
4088 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
, chan
)] =
4089 to_integer(ctx
, LLVMBuildExtractElement(ctx
->builder
,
4090 input
, llvm_chan
, ""));
4095 static void interp_fs_input(struct nir_to_llvm_context
*ctx
,
4097 LLVMValueRef interp_param
,
4098 LLVMValueRef prim_mask
,
4099 LLVMValueRef result
[4])
4101 LLVMValueRef attr_number
;
4104 bool interp
= interp_param
!= NULL
;
4106 attr_number
= LLVMConstInt(ctx
->i32
, attr
, false);
4108 /* fs.constant returns the param from the middle vertex, so it's not
4109 * really useful for flat shading. It's meant to be used for custom
4110 * interpolation (but the intrinsic can't fetch from the other two
4113 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
4114 * to do the right thing. The only reason we use fs.constant is that
4115 * fs.interp cannot be used on integers, because they can be equal
4119 interp_param
= LLVMBuildBitCast(ctx
->builder
, interp_param
,
4120 LLVMVectorType(ctx
->f32
, 2), "");
4122 i
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
4124 j
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
4128 for (chan
= 0; chan
< 4; chan
++) {
4129 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
4132 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
4137 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
4138 LLVMConstInt(ctx
->i32
, 2, false),
4147 handle_fs_input_decl(struct nir_to_llvm_context
*ctx
,
4148 struct nir_variable
*variable
)
4150 int idx
= variable
->data
.location
;
4151 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4152 LLVMValueRef interp
;
4154 variable
->data
.driver_location
= idx
* 4;
4155 ctx
->input_mask
|= ((1ull << attrib_count
) - 1) << variable
->data
.location
;
4157 if (glsl_get_base_type(glsl_without_array(variable
->type
)) == GLSL_TYPE_FLOAT
) {
4158 unsigned interp_type
;
4159 if (variable
->data
.sample
) {
4160 interp_type
= INTERP_SAMPLE
;
4161 ctx
->shader_info
->fs
.force_persample
= true;
4162 } else if (variable
->data
.centroid
)
4163 interp_type
= INTERP_CENTROID
;
4165 interp_type
= INTERP_CENTER
;
4167 interp
= lookup_interp_param(ctx
, variable
->data
.interpolation
, interp_type
);
4171 for (unsigned i
= 0; i
< attrib_count
; ++i
)
4172 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
+ i
, 0)] = interp
;
4177 handle_shader_input_decl(struct nir_to_llvm_context
*ctx
,
4178 struct nir_variable
*variable
)
4180 switch (ctx
->stage
) {
4181 case MESA_SHADER_VERTEX
:
4182 handle_vs_input_decl(ctx
, variable
);
4184 case MESA_SHADER_FRAGMENT
:
4185 handle_fs_input_decl(ctx
, variable
);
4194 handle_fs_inputs_pre(struct nir_to_llvm_context
*ctx
,
4195 struct nir_shader
*nir
)
4198 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_INPUTS
; ++i
) {
4199 LLVMValueRef interp_param
;
4200 LLVMValueRef
*inputs
= ctx
->inputs
+radeon_llvm_reg_index_soa(i
, 0);
4202 if (!(ctx
->input_mask
& (1ull << i
)))
4205 if (i
>= VARYING_SLOT_VAR0
|| i
== VARYING_SLOT_PNTC
||
4206 i
== VARYING_SLOT_PRIMITIVE_ID
|| i
== VARYING_SLOT_LAYER
) {
4207 interp_param
= *inputs
;
4208 interp_fs_input(ctx
, index
, interp_param
, ctx
->prim_mask
,
4212 ctx
->shader_info
->fs
.flat_shaded_mask
|= 1u << index
;
4214 } else if (i
== VARYING_SLOT_POS
) {
4215 for(int i
= 0; i
< 3; ++i
)
4216 inputs
[i
] = ctx
->frag_pos
[i
];
4218 inputs
[3] = ac_build_fdiv(&ctx
->ac
, ctx
->f32one
, ctx
->frag_pos
[3]);
4221 ctx
->shader_info
->fs
.num_interp
= index
;
4222 if (ctx
->input_mask
& (1 << VARYING_SLOT_PNTC
))
4223 ctx
->shader_info
->fs
.has_pcoord
= true;
4224 if (ctx
->input_mask
& (1 << VARYING_SLOT_PRIMITIVE_ID
))
4225 ctx
->shader_info
->fs
.prim_id_input
= true;
4226 if (ctx
->input_mask
& (1 << VARYING_SLOT_LAYER
))
4227 ctx
->shader_info
->fs
.layer_input
= true;
4228 ctx
->shader_info
->fs
.input_mask
= ctx
->input_mask
>> VARYING_SLOT_VAR0
;
4232 ac_build_alloca(struct nir_to_llvm_context
*ctx
,
4236 LLVMBuilderRef builder
= ctx
->builder
;
4237 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
4238 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
4239 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
4240 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
4241 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ctx
->context
);
4245 LLVMPositionBuilderBefore(first_builder
, first_instr
);
4247 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
4250 res
= LLVMBuildAlloca(first_builder
, type
, name
);
4251 LLVMBuildStore(builder
, LLVMConstNull(type
), res
);
4253 LLVMDisposeBuilder(first_builder
);
4258 static LLVMValueRef
si_build_alloca_undef(struct nir_to_llvm_context
*ctx
,
4262 LLVMValueRef ptr
= ac_build_alloca(ctx
, type
, name
);
4263 LLVMBuildStore(ctx
->builder
, LLVMGetUndef(type
), ptr
);
4268 handle_shader_output_decl(struct nir_to_llvm_context
*ctx
,
4269 struct nir_variable
*variable
)
4271 int idx
= variable
->data
.location
+ variable
->data
.index
;
4272 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4273 uint64_t mask_attribs
;
4274 variable
->data
.driver_location
= idx
* 4;
4276 mask_attribs
= ((1ull << attrib_count
) - 1) << idx
;
4277 if (ctx
->stage
== MESA_SHADER_VERTEX
||
4278 ctx
->stage
== MESA_SHADER_GEOMETRY
) {
4279 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
4280 int length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
4281 if (ctx
->stage
== MESA_SHADER_VERTEX
) {
4282 ctx
->shader_info
->vs
.outinfo
.clip_dist_mask
= (1 << ctx
->num_output_clips
) - 1;
4283 ctx
->shader_info
->vs
.outinfo
.cull_dist_mask
= (1 << ctx
->num_output_culls
) - 1;
4290 mask_attribs
= 1ull << idx
;
4294 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
4295 for (unsigned chan
= 0; chan
< 4; chan
++) {
4296 ctx
->outputs
[radeon_llvm_reg_index_soa(idx
+ i
, chan
)] =
4297 si_build_alloca_undef(ctx
, ctx
->f32
, "");
4300 ctx
->output_mask
|= mask_attribs
;
4304 setup_locals(struct nir_to_llvm_context
*ctx
,
4305 struct nir_function
*func
)
4308 ctx
->num_locals
= 0;
4309 nir_foreach_variable(variable
, &func
->impl
->locals
) {
4310 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4311 variable
->data
.driver_location
= ctx
->num_locals
* 4;
4312 ctx
->num_locals
+= attrib_count
;
4314 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
4318 for (i
= 0; i
< ctx
->num_locals
; i
++) {
4319 for (j
= 0; j
< 4; j
++) {
4320 ctx
->locals
[i
* 4 + j
] =
4321 si_build_alloca_undef(ctx
, ctx
->f32
, "temp");
4327 emit_float_saturate(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
, float lo
, float hi
)
4329 v
= to_float(ctx
, v
);
4330 v
= emit_intrin_2f_param(ctx
, "llvm.maxnum.f32", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, lo
));
4331 return emit_intrin_2f_param(ctx
, "llvm.minnum.f32", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, hi
));
4335 static LLVMValueRef
emit_pack_int16(struct nir_to_llvm_context
*ctx
,
4336 LLVMValueRef src0
, LLVMValueRef src1
)
4338 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
4339 LLVMValueRef comp
[2];
4341 comp
[0] = LLVMBuildAnd(ctx
->builder
, src0
, LLVMConstInt(ctx
-> i32
, 65535, 0), "");
4342 comp
[1] = LLVMBuildAnd(ctx
->builder
, src1
, LLVMConstInt(ctx
-> i32
, 65535, 0), "");
4343 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
4344 return LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
4347 /* Initialize arguments for the shader export intrinsic */
4349 si_llvm_init_export_args(struct nir_to_llvm_context
*ctx
,
4350 LLVMValueRef
*values
,
4352 struct ac_export_args
*args
)
4354 /* Default is 0xf. Adjusted below depending on the format. */
4355 args
->enabled_channels
= 0xf;
4357 /* Specify whether the EXEC mask represents the valid mask */
4358 args
->valid_mask
= 0;
4360 /* Specify whether this is the last export */
4363 /* Specify the target we are exporting */
4364 args
->target
= target
;
4366 args
->compr
= false;
4367 args
->out
[0] = LLVMGetUndef(ctx
->f32
);
4368 args
->out
[1] = LLVMGetUndef(ctx
->f32
);
4369 args
->out
[2] = LLVMGetUndef(ctx
->f32
);
4370 args
->out
[3] = LLVMGetUndef(ctx
->f32
);
4375 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&& target
>= V_008DFC_SQ_EXP_MRT
) {
4376 LLVMValueRef val
[4];
4377 unsigned index
= target
- V_008DFC_SQ_EXP_MRT
;
4378 unsigned col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * index
)) & 0xf;
4379 bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> index
) & 1;
4381 switch(col_format
) {
4382 case V_028714_SPI_SHADER_ZERO
:
4383 args
->enabled_channels
= 0; /* writemask */
4384 args
->target
= V_008DFC_SQ_EXP_NULL
;
4387 case V_028714_SPI_SHADER_32_R
:
4388 args
->enabled_channels
= 1;
4389 args
->out
[0] = values
[0];
4392 case V_028714_SPI_SHADER_32_GR
:
4393 args
->enabled_channels
= 0x3;
4394 args
->out
[0] = values
[0];
4395 args
->out
[1] = values
[1];
4398 case V_028714_SPI_SHADER_32_AR
:
4399 args
->enabled_channels
= 0x9;
4400 args
->out
[0] = values
[0];
4401 args
->out
[3] = values
[3];
4404 case V_028714_SPI_SHADER_FP16_ABGR
:
4407 for (unsigned chan
= 0; chan
< 2; chan
++) {
4408 LLVMValueRef pack_args
[2] = {
4410 values
[2 * chan
+ 1]
4412 LLVMValueRef packed
;
4414 packed
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, pack_args
);
4415 args
->out
[chan
] = packed
;
4419 case V_028714_SPI_SHADER_UNORM16_ABGR
:
4420 for (unsigned chan
= 0; chan
< 4; chan
++) {
4421 val
[chan
] = ac_build_clamp(&ctx
->ac
, values
[chan
]);
4422 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
4423 LLVMConstReal(ctx
->f32
, 65535), "");
4424 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
4425 LLVMConstReal(ctx
->f32
, 0.5), "");
4426 val
[chan
] = LLVMBuildFPToUI(ctx
->builder
, val
[chan
],
4431 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
4432 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
4435 case V_028714_SPI_SHADER_SNORM16_ABGR
:
4436 for (unsigned chan
= 0; chan
< 4; chan
++) {
4437 val
[chan
] = emit_float_saturate(ctx
, values
[chan
], -1, 1);
4438 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
4439 LLVMConstReal(ctx
->f32
, 32767), "");
4441 /* If positive, add 0.5, else add -0.5. */
4442 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
4443 LLVMBuildSelect(ctx
->builder
,
4444 LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
,
4445 val
[chan
], ctx
->f32zero
, ""),
4446 LLVMConstReal(ctx
->f32
, 0.5),
4447 LLVMConstReal(ctx
->f32
, -0.5), ""), "");
4448 val
[chan
] = LLVMBuildFPToSI(ctx
->builder
, val
[chan
], ctx
->i32
, "");
4452 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
4453 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
4456 case V_028714_SPI_SHADER_UINT16_ABGR
: {
4457 LLVMValueRef max
= LLVMConstInt(ctx
->i32
, is_int8
? 255 : 65535, 0);
4459 for (unsigned chan
= 0; chan
< 4; chan
++) {
4460 val
[chan
] = to_integer(ctx
, values
[chan
]);
4461 val
[chan
] = emit_minmax_int(ctx
, LLVMIntULT
, val
[chan
], max
);
4465 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
4466 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
4470 case V_028714_SPI_SHADER_SINT16_ABGR
: {
4471 LLVMValueRef max
= LLVMConstInt(ctx
->i32
, is_int8
? 127 : 32767, 0);
4472 LLVMValueRef min
= LLVMConstInt(ctx
->i32
, is_int8
? -128 : -32768, 0);
4475 for (unsigned chan
= 0; chan
< 4; chan
++) {
4476 val
[chan
] = to_integer(ctx
, values
[chan
]);
4477 val
[chan
] = emit_minmax_int(ctx
, LLVMIntSLT
, val
[chan
], max
);
4478 val
[chan
] = emit_minmax_int(ctx
, LLVMIntSGT
, val
[chan
], min
);
4482 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
4483 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
4488 case V_028714_SPI_SHADER_32_ABGR
:
4489 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
4493 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
4495 for (unsigned i
= 0; i
< 4; ++i
)
4496 args
->out
[i
] = to_float(ctx
, args
->out
[i
]);
4500 handle_vs_outputs_post(struct nir_to_llvm_context
*ctx
,
4501 struct ac_vs_output_info
*outinfo
)
4503 uint32_t param_count
= 0;
4505 unsigned pos_idx
, num_pos_exports
= 0;
4506 struct ac_export_args args
, pos_args
[4] = {};
4507 LLVMValueRef psize_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
4510 outinfo
->prim_id_output
= 0xffffffff;
4511 outinfo
->layer_output
= 0xffffffff;
4512 if (ctx
->output_mask
& (1ull << VARYING_SLOT_CLIP_DIST0
)) {
4513 LLVMValueRef slots
[8];
4516 if (outinfo
->cull_dist_mask
)
4517 outinfo
->cull_dist_mask
<<= ctx
->num_output_clips
;
4519 i
= VARYING_SLOT_CLIP_DIST0
;
4520 for (j
= 0; j
< ctx
->num_output_clips
+ ctx
->num_output_culls
; j
++)
4521 slots
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4522 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4524 for (i
= ctx
->num_output_clips
+ ctx
->num_output_culls
; i
< 8; i
++)
4525 slots
[i
] = LLVMGetUndef(ctx
->f32
);
4527 if (ctx
->num_output_clips
+ ctx
->num_output_culls
> 4) {
4528 target
= V_008DFC_SQ_EXP_POS
+ 3;
4529 si_llvm_init_export_args(ctx
, &slots
[4], target
, &args
);
4530 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
4531 &args
, sizeof(args
));
4534 target
= V_008DFC_SQ_EXP_POS
+ 2;
4535 si_llvm_init_export_args(ctx
, &slots
[0], target
, &args
);
4536 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
4537 &args
, sizeof(args
));
4541 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
4542 LLVMValueRef values
[4];
4543 if (!(ctx
->output_mask
& (1ull << i
)))
4546 for (unsigned j
= 0; j
< 4; j
++)
4547 values
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4548 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4550 if (i
== VARYING_SLOT_POS
) {
4551 target
= V_008DFC_SQ_EXP_POS
;
4552 } else if (i
== VARYING_SLOT_CLIP_DIST0
) {
4554 } else if (i
== VARYING_SLOT_PSIZ
) {
4555 outinfo
->writes_pointsize
= true;
4556 psize_value
= values
[0];
4558 } else if (i
== VARYING_SLOT_LAYER
) {
4559 outinfo
->writes_layer
= true;
4560 layer_value
= values
[0];
4561 outinfo
->layer_output
= param_count
;
4562 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
4564 } else if (i
== VARYING_SLOT_VIEWPORT
) {
4565 outinfo
->writes_viewport_index
= true;
4566 viewport_index_value
= values
[0];
4568 } else if (i
== VARYING_SLOT_PRIMITIVE_ID
) {
4569 outinfo
->prim_id_output
= param_count
;
4570 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
4572 } else if (i
>= VARYING_SLOT_VAR0
) {
4573 outinfo
->export_mask
|= 1u << (i
- VARYING_SLOT_VAR0
);
4574 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
4578 si_llvm_init_export_args(ctx
, values
, target
, &args
);
4580 if (target
>= V_008DFC_SQ_EXP_POS
&&
4581 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
4582 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
4583 &args
, sizeof(args
));
4585 ac_build_export(&ctx
->ac
, &args
);
4589 /* We need to add the position output manually if it's missing. */
4590 if (!pos_args
[0].out
[0]) {
4591 pos_args
[0].enabled_channels
= 0xf;
4592 pos_args
[0].valid_mask
= 0;
4593 pos_args
[0].done
= 0;
4594 pos_args
[0].target
= V_008DFC_SQ_EXP_POS
;
4595 pos_args
[0].compr
= 0;
4596 pos_args
[0].out
[0] = ctx
->f32zero
; /* X */
4597 pos_args
[0].out
[1] = ctx
->f32zero
; /* Y */
4598 pos_args
[0].out
[2] = ctx
->f32zero
; /* Z */
4599 pos_args
[0].out
[3] = ctx
->f32one
; /* W */
4602 uint32_t mask
= ((outinfo
->writes_pointsize
== true ? 1 : 0) |
4603 (outinfo
->writes_layer
== true ? 4 : 0) |
4604 (outinfo
->writes_viewport_index
== true ? 8 : 0));
4606 pos_args
[1].enabled_channels
= mask
;
4607 pos_args
[1].valid_mask
= 0;
4608 pos_args
[1].done
= 0;
4609 pos_args
[1].target
= V_008DFC_SQ_EXP_POS
+ 1;
4610 pos_args
[1].compr
= 0;
4611 pos_args
[1].out
[0] = ctx
->f32zero
; /* X */
4612 pos_args
[1].out
[1] = ctx
->f32zero
; /* Y */
4613 pos_args
[1].out
[2] = ctx
->f32zero
; /* Z */
4614 pos_args
[1].out
[3] = ctx
->f32zero
; /* W */
4616 if (outinfo
->writes_pointsize
== true)
4617 pos_args
[1].out
[0] = psize_value
;
4618 if (outinfo
->writes_layer
== true)
4619 pos_args
[1].out
[2] = layer_value
;
4620 if (outinfo
->writes_viewport_index
== true)
4621 pos_args
[1].out
[3] = viewport_index_value
;
4623 for (i
= 0; i
< 4; i
++) {
4624 if (pos_args
[i
].out
[0])
4629 for (i
= 0; i
< 4; i
++) {
4630 if (!pos_args
[i
].out
[0])
4633 /* Specify the target we are exporting */
4634 pos_args
[i
].target
= V_008DFC_SQ_EXP_POS
+ pos_idx
++;
4635 if (pos_idx
== num_pos_exports
)
4636 pos_args
[i
].done
= 1;
4637 ac_build_export(&ctx
->ac
, &pos_args
[i
]);
4640 outinfo
->pos_exports
= num_pos_exports
;
4641 outinfo
->param_exports
= param_count
;
4645 handle_es_outputs_post(struct nir_to_llvm_context
*ctx
,
4646 struct ac_es_output_info
*outinfo
)
4649 uint64_t max_output_written
= 0;
4650 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
4651 LLVMValueRef
*out_ptr
= &ctx
->outputs
[i
* 4];
4655 if (!(ctx
->output_mask
& (1ull << i
)))
4658 if (i
== VARYING_SLOT_CLIP_DIST0
)
4659 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
4661 param_index
= shader_io_get_unique_index(i
);
4663 max_output_written
= MAX2(param_index
+ (length
> 4), max_output_written
);
4665 for (j
= 0; j
< length
; j
++) {
4666 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], "");
4667 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->i32
, "");
4669 ac_build_buffer_store_dword(&ctx
->ac
,
4672 NULL
, ctx
->es2gs_offset
,
4673 (4 * param_index
+ j
) * 4,
4677 outinfo
->esgs_itemsize
= (max_output_written
+ 1) * 16;
4681 si_export_mrt_color(struct nir_to_llvm_context
*ctx
,
4682 LLVMValueRef
*color
, unsigned param
, bool is_last
)
4685 struct ac_export_args args
;
4688 si_llvm_init_export_args(ctx
, color
, param
,
4692 args
.valid_mask
= 1; /* whether the EXEC mask is valid */
4693 args
.done
= 1; /* DONE bit */
4694 } else if (!args
.enabled_channels
)
4695 return; /* unnecessary NULL export */
4697 ac_build_export(&ctx
->ac
, &args
);
4701 si_export_mrt_z(struct nir_to_llvm_context
*ctx
,
4702 LLVMValueRef depth
, LLVMValueRef stencil
,
4703 LLVMValueRef samplemask
)
4705 struct ac_export_args args
;
4707 args
.enabled_channels
= 0;
4708 args
.valid_mask
= 1;
4710 args
.target
= V_008DFC_SQ_EXP_MRTZ
;
4713 args
.out
[0] = LLVMGetUndef(ctx
->f32
); /* R, depth */
4714 args
.out
[1] = LLVMGetUndef(ctx
->f32
); /* G, stencil test val[0:7], stencil op val[8:15] */
4715 args
.out
[2] = LLVMGetUndef(ctx
->f32
); /* B, sample mask */
4716 args
.out
[3] = LLVMGetUndef(ctx
->f32
); /* A, alpha to mask */
4719 args
.out
[0] = depth
;
4720 args
.enabled_channels
|= 0x1;
4724 args
.out
[1] = stencil
;
4725 args
.enabled_channels
|= 0x2;
4729 args
.out
[2] = samplemask
;
4730 args
.enabled_channels
|= 0x4;
4733 /* SI (except OLAND) has a bug that it only looks
4734 * at the X writemask component. */
4735 if (ctx
->options
->chip_class
== SI
&&
4736 ctx
->options
->family
!= CHIP_OLAND
)
4737 args
.enabled_channels
|= 0x1;
4739 ac_build_export(&ctx
->ac
, &args
);
4743 handle_fs_outputs_post(struct nir_to_llvm_context
*ctx
)
4746 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
4748 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
4749 LLVMValueRef values
[4];
4751 if (!(ctx
->output_mask
& (1ull << i
)))
4754 if (i
== FRAG_RESULT_DEPTH
) {
4755 ctx
->shader_info
->fs
.writes_z
= true;
4756 depth
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4757 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
4758 } else if (i
== FRAG_RESULT_STENCIL
) {
4759 ctx
->shader_info
->fs
.writes_stencil
= true;
4760 stencil
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4761 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
4762 } else if (i
== FRAG_RESULT_SAMPLE_MASK
) {
4763 ctx
->shader_info
->fs
.writes_sample_mask
= true;
4764 samplemask
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4765 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
4768 for (unsigned j
= 0; j
< 4; j
++)
4769 values
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4770 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4772 if (!ctx
->shader_info
->fs
.writes_z
&& !ctx
->shader_info
->fs
.writes_stencil
&& !ctx
->shader_info
->fs
.writes_sample_mask
)
4773 last
= ctx
->output_mask
<= ((1ull << (i
+ 1)) - 1);
4775 si_export_mrt_color(ctx
, values
, V_008DFC_SQ_EXP_MRT
+ index
, last
);
4780 if (depth
|| stencil
|| samplemask
)
4781 si_export_mrt_z(ctx
, depth
, stencil
, samplemask
);
4783 si_export_mrt_color(ctx
, NULL
, V_008DFC_SQ_EXP_NULL
, true);
4785 ctx
->shader_info
->fs
.output_mask
= index
? ((1ull << index
) - 1) : 0;
4789 emit_gs_epilogue(struct nir_to_llvm_context
*ctx
)
4791 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_NOP
| AC_SENDMSG_GS_DONE
, ctx
->gs_wave_id
);
4795 handle_shader_outputs_post(struct nir_to_llvm_context
*ctx
)
4797 switch (ctx
->stage
) {
4798 case MESA_SHADER_VERTEX
:
4799 if (ctx
->options
->key
.vs
.as_es
)
4800 handle_es_outputs_post(ctx
, &ctx
->shader_info
->vs
.es_info
);
4802 handle_vs_outputs_post(ctx
, &ctx
->shader_info
->vs
.outinfo
);
4804 case MESA_SHADER_FRAGMENT
:
4805 handle_fs_outputs_post(ctx
);
4807 case MESA_SHADER_GEOMETRY
:
4808 emit_gs_epilogue(ctx
);
4816 handle_shared_compute_var(struct nir_to_llvm_context
*ctx
,
4817 struct nir_variable
*variable
, uint32_t *offset
, int idx
)
4819 unsigned size
= glsl_count_attribute_slots(variable
->type
, false);
4820 variable
->data
.driver_location
= *offset
;
4824 static void ac_llvm_finalize_module(struct nir_to_llvm_context
* ctx
)
4826 LLVMPassManagerRef passmgr
;
4827 /* Create the pass manager */
4828 passmgr
= LLVMCreateFunctionPassManagerForModule(
4831 /* This pass should eliminate all the load and store instructions */
4832 LLVMAddPromoteMemoryToRegisterPass(passmgr
);
4834 /* Add some optimization passes */
4835 LLVMAddScalarReplAggregatesPass(passmgr
);
4836 LLVMAddLICMPass(passmgr
);
4837 LLVMAddAggressiveDCEPass(passmgr
);
4838 LLVMAddCFGSimplificationPass(passmgr
);
4839 LLVMAddInstructionCombiningPass(passmgr
);
4842 LLVMInitializeFunctionPassManager(passmgr
);
4843 LLVMRunFunctionPassManager(passmgr
, ctx
->main_function
);
4844 LLVMFinalizeFunctionPassManager(passmgr
);
4846 LLVMDisposeBuilder(ctx
->builder
);
4847 LLVMDisposePassManager(passmgr
);
4851 ac_setup_rings(struct nir_to_llvm_context
*ctx
)
4853 if ((ctx
->stage
== MESA_SHADER_VERTEX
&& ctx
->options
->key
.vs
.as_es
) ||
4854 (ctx
->stage
== MESA_SHADER_TESS_EVAL
&& ctx
->options
->key
.tes
.as_es
)) {
4855 ctx
->esgs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_ESGS_VS
, false));
4858 if (ctx
->is_gs_copy_shader
) {
4859 ctx
->gsvs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_GSVS_VS
, false));
4861 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
4863 ctx
->esgs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_ESGS_GS
, false));
4864 ctx
->gsvs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_GSVS_GS
, false));
4866 ctx
->gsvs_ring
= LLVMBuildBitCast(ctx
->builder
, ctx
->gsvs_ring
, ctx
->v4i32
, "");
4868 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->gsvs_num_entries
, LLVMConstInt(ctx
->i32
, 2, false), "");
4869 tmp
= LLVMBuildExtractElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->i32one
, "");
4870 tmp
= LLVMBuildOr(ctx
->builder
, tmp
, ctx
->gsvs_ring_stride
, "");
4871 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, tmp
, ctx
->i32one
, "");
4873 ctx
->gsvs_ring
= LLVMBuildBitCast(ctx
->builder
, ctx
->gsvs_ring
, ctx
->v16i8
, "");
4876 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
4877 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
4878 ctx
->hs_ring_tess_offchip
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_HS_TESS_OFFCHIP
, false));
4879 ctx
->hs_ring_tess_factor
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_HS_TESS_FACTOR
, false));
4884 LLVMModuleRef
ac_translate_nir_to_llvm(LLVMTargetMachineRef tm
,
4885 struct nir_shader
*nir
,
4886 struct ac_shader_variant_info
*shader_info
,
4887 const struct ac_nir_compiler_options
*options
)
4889 struct nir_to_llvm_context ctx
= {0};
4890 struct nir_function
*func
;
4892 ctx
.options
= options
;
4893 ctx
.shader_info
= shader_info
;
4894 ctx
.context
= LLVMContextCreate();
4895 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
4897 ac_llvm_context_init(&ctx
.ac
, ctx
.context
);
4898 ctx
.ac
.module
= ctx
.module
;
4900 ctx
.has_ds_bpermute
= ctx
.options
->chip_class
>= VI
;
4902 memset(shader_info
, 0, sizeof(*shader_info
));
4904 LLVMSetTarget(ctx
.module
, options
->supports_spill
? "amdgcn-mesa-mesa3d" : "amdgcn--");
4906 LLVMTargetDataRef data_layout
= LLVMCreateTargetDataLayout(tm
);
4907 char *data_layout_str
= LLVMCopyStringRepOfTargetData(data_layout
);
4908 LLVMSetDataLayout(ctx
.module
, data_layout_str
);
4909 LLVMDisposeTargetData(data_layout
);
4910 LLVMDisposeMessage(data_layout_str
);
4914 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
4915 ctx
.ac
.builder
= ctx
.builder
;
4916 ctx
.stage
= nir
->stage
;
4918 for (i
= 0; i
< AC_UD_MAX_SETS
; i
++)
4919 shader_info
->user_sgprs_locs
.descriptor_sets
[i
].sgpr_idx
= -1;
4920 for (i
= 0; i
< AC_UD_MAX_UD
; i
++)
4921 shader_info
->user_sgprs_locs
.shader_data
[i
].sgpr_idx
= -1;
4923 create_function(&ctx
);
4925 if (nir
->stage
== MESA_SHADER_COMPUTE
) {
4927 nir_foreach_variable(variable
, &nir
->shared
)
4931 uint32_t shared_size
= 0;
4933 LLVMTypeRef i8p
= LLVMPointerType(ctx
.i8
, LOCAL_ADDR_SPACE
);
4934 nir_foreach_variable(variable
, &nir
->shared
) {
4935 handle_shared_compute_var(&ctx
, variable
, &shared_size
, idx
);
4940 var
= LLVMAddGlobalInAddressSpace(ctx
.module
,
4941 LLVMArrayType(ctx
.i8
, shared_size
),
4944 LLVMSetAlignment(var
, 4);
4945 ctx
.shared_memory
= LLVMBuildBitCast(ctx
.builder
, var
, i8p
, "");
4947 } else if (nir
->stage
== MESA_SHADER_GEOMETRY
) {
4948 ctx
.gs_next_vertex
= ac_build_alloca(&ctx
, ctx
.i32
, "gs_next_vertex");
4950 ctx
.gs_max_out_vertices
= nir
->info
->gs
.vertices_out
;
4953 ac_setup_rings(&ctx
);
4955 nir_foreach_variable(variable
, &nir
->inputs
)
4956 handle_shader_input_decl(&ctx
, variable
);
4958 if (nir
->stage
== MESA_SHADER_FRAGMENT
)
4959 handle_fs_inputs_pre(&ctx
, nir
);
4961 ctx
.num_output_clips
= nir
->info
->clip_distance_array_size
;
4962 ctx
.num_output_culls
= nir
->info
->cull_distance_array_size
;
4964 nir_foreach_variable(variable
, &nir
->outputs
)
4965 handle_shader_output_decl(&ctx
, variable
);
4967 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4968 _mesa_key_pointer_equal
);
4969 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4970 _mesa_key_pointer_equal
);
4972 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
4974 setup_locals(&ctx
, func
);
4976 visit_cf_list(&ctx
, &func
->impl
->body
);
4977 phi_post_pass(&ctx
);
4979 handle_shader_outputs_post(&ctx
);
4980 LLVMBuildRetVoid(ctx
.builder
);
4982 ac_llvm_finalize_module(&ctx
);
4984 ralloc_free(ctx
.defs
);
4985 ralloc_free(ctx
.phis
);
4987 if (nir
->stage
== MESA_SHADER_GEOMETRY
) {
4988 unsigned addclip
= ctx
.num_output_clips
+ ctx
.num_output_culls
> 4;
4989 shader_info
->gs
.gsvs_vertex_size
= (util_bitcount64(ctx
.output_mask
) + addclip
) * 16;
4990 shader_info
->gs
.max_gsvs_emit_size
= shader_info
->gs
.gsvs_vertex_size
*
4991 nir
->info
->gs
.vertices_out
;
4996 static void ac_diagnostic_handler(LLVMDiagnosticInfoRef di
, void *context
)
4998 unsigned *retval
= (unsigned *)context
;
4999 LLVMDiagnosticSeverity severity
= LLVMGetDiagInfoSeverity(di
);
5000 char *description
= LLVMGetDiagInfoDescription(di
);
5002 if (severity
== LLVMDSError
) {
5004 fprintf(stderr
, "LLVM triggered Diagnostic Handler: %s\n",
5008 LLVMDisposeMessage(description
);
5011 static unsigned ac_llvm_compile(LLVMModuleRef M
,
5012 struct ac_shader_binary
*binary
,
5013 LLVMTargetMachineRef tm
)
5015 unsigned retval
= 0;
5017 LLVMContextRef llvm_ctx
;
5018 LLVMMemoryBufferRef out_buffer
;
5019 unsigned buffer_size
;
5020 const char *buffer_data
;
5023 /* Setup Diagnostic Handler*/
5024 llvm_ctx
= LLVMGetModuleContext(M
);
5026 LLVMContextSetDiagnosticHandler(llvm_ctx
, ac_diagnostic_handler
,
5030 mem_err
= LLVMTargetMachineEmitToMemoryBuffer(tm
, M
, LLVMObjectFile
,
5033 /* Process Errors/Warnings */
5035 fprintf(stderr
, "%s: %s", __FUNCTION__
, err
);
5041 /* Extract Shader Code*/
5042 buffer_size
= LLVMGetBufferSize(out_buffer
);
5043 buffer_data
= LLVMGetBufferStart(out_buffer
);
5045 ac_elf_read(buffer_data
, buffer_size
, binary
);
5048 LLVMDisposeMemoryBuffer(out_buffer
);
5054 static void ac_compile_llvm_module(LLVMTargetMachineRef tm
,
5055 LLVMModuleRef llvm_module
,
5056 struct ac_shader_binary
*binary
,
5057 struct ac_shader_config
*config
,
5058 struct ac_shader_variant_info
*shader_info
,
5059 gl_shader_stage stage
,
5060 bool dump_shader
, bool supports_spill
)
5063 ac_dump_module(llvm_module
);
5065 memset(binary
, 0, sizeof(*binary
));
5066 int v
= ac_llvm_compile(llvm_module
, binary
, tm
);
5068 fprintf(stderr
, "compile failed\n");
5072 fprintf(stderr
, "disasm:\n%s\n", binary
->disasm_string
);
5074 ac_shader_binary_read_config(binary
, config
, 0, supports_spill
);
5076 LLVMContextRef ctx
= LLVMGetModuleContext(llvm_module
);
5077 LLVMDisposeModule(llvm_module
);
5078 LLVMContextDispose(ctx
);
5080 if (stage
== MESA_SHADER_FRAGMENT
) {
5081 shader_info
->num_input_vgprs
= 0;
5082 if (G_0286CC_PERSP_SAMPLE_ENA(config
->spi_ps_input_addr
))
5083 shader_info
->num_input_vgprs
+= 2;
5084 if (G_0286CC_PERSP_CENTER_ENA(config
->spi_ps_input_addr
))
5085 shader_info
->num_input_vgprs
+= 2;
5086 if (G_0286CC_PERSP_CENTROID_ENA(config
->spi_ps_input_addr
))
5087 shader_info
->num_input_vgprs
+= 2;
5088 if (G_0286CC_PERSP_PULL_MODEL_ENA(config
->spi_ps_input_addr
))
5089 shader_info
->num_input_vgprs
+= 3;
5090 if (G_0286CC_LINEAR_SAMPLE_ENA(config
->spi_ps_input_addr
))
5091 shader_info
->num_input_vgprs
+= 2;
5092 if (G_0286CC_LINEAR_CENTER_ENA(config
->spi_ps_input_addr
))
5093 shader_info
->num_input_vgprs
+= 2;
5094 if (G_0286CC_LINEAR_CENTROID_ENA(config
->spi_ps_input_addr
))
5095 shader_info
->num_input_vgprs
+= 2;
5096 if (G_0286CC_LINE_STIPPLE_TEX_ENA(config
->spi_ps_input_addr
))
5097 shader_info
->num_input_vgprs
+= 1;
5098 if (G_0286CC_POS_X_FLOAT_ENA(config
->spi_ps_input_addr
))
5099 shader_info
->num_input_vgprs
+= 1;
5100 if (G_0286CC_POS_Y_FLOAT_ENA(config
->spi_ps_input_addr
))
5101 shader_info
->num_input_vgprs
+= 1;
5102 if (G_0286CC_POS_Z_FLOAT_ENA(config
->spi_ps_input_addr
))
5103 shader_info
->num_input_vgprs
+= 1;
5104 if (G_0286CC_POS_W_FLOAT_ENA(config
->spi_ps_input_addr
))
5105 shader_info
->num_input_vgprs
+= 1;
5106 if (G_0286CC_FRONT_FACE_ENA(config
->spi_ps_input_addr
))
5107 shader_info
->num_input_vgprs
+= 1;
5108 if (G_0286CC_ANCILLARY_ENA(config
->spi_ps_input_addr
))
5109 shader_info
->num_input_vgprs
+= 1;
5110 if (G_0286CC_SAMPLE_COVERAGE_ENA(config
->spi_ps_input_addr
))
5111 shader_info
->num_input_vgprs
+= 1;
5112 if (G_0286CC_POS_FIXED_PT_ENA(config
->spi_ps_input_addr
))
5113 shader_info
->num_input_vgprs
+= 1;
5115 config
->num_vgprs
= MAX2(config
->num_vgprs
, shader_info
->num_input_vgprs
);
5117 /* +3 for scratch wave offset and VCC */
5118 config
->num_sgprs
= MAX2(config
->num_sgprs
,
5119 shader_info
->num_input_sgprs
+ 3);
5122 void ac_compile_nir_shader(LLVMTargetMachineRef tm
,
5123 struct ac_shader_binary
*binary
,
5124 struct ac_shader_config
*config
,
5125 struct ac_shader_variant_info
*shader_info
,
5126 struct nir_shader
*nir
,
5127 const struct ac_nir_compiler_options
*options
,
5131 LLVMModuleRef llvm_module
= ac_translate_nir_to_llvm(tm
, nir
, shader_info
,
5134 ac_compile_llvm_module(tm
, llvm_module
, binary
, config
, shader_info
, nir
->stage
, dump_shader
, options
->supports_spill
);
5135 switch (nir
->stage
) {
5136 case MESA_SHADER_COMPUTE
:
5137 for (int i
= 0; i
< 3; ++i
)
5138 shader_info
->cs
.block_size
[i
] = nir
->info
->cs
.local_size
[i
];
5140 case MESA_SHADER_FRAGMENT
:
5141 shader_info
->fs
.early_fragment_test
= nir
->info
->fs
.early_fragment_tests
;
5143 case MESA_SHADER_GEOMETRY
:
5144 shader_info
->gs
.vertices_in
= nir
->info
->gs
.vertices_in
;
5145 shader_info
->gs
.vertices_out
= nir
->info
->gs
.vertices_out
;
5146 shader_info
->gs
.output_prim
= nir
->info
->gs
.output_primitive
;
5147 shader_info
->gs
.invocations
= nir
->info
->gs
.invocations
;
5149 case MESA_SHADER_VERTEX
:
5150 shader_info
->vs
.as_es
= options
->key
.vs
.as_es
;
5158 ac_gs_copy_shader_emit(struct nir_to_llvm_context
*ctx
)
5160 LLVMValueRef args
[9];
5161 args
[0] = ctx
->gsvs_ring
;
5162 args
[1] = LLVMBuildMul(ctx
->builder
, ctx
->vertex_id
, LLVMConstInt(ctx
->i32
, 4, false), "");
5163 args
[3] = ctx
->i32zero
;
5164 args
[4] = ctx
->i32one
; /* OFFEN */
5165 args
[5] = ctx
->i32zero
; /* IDXEN */
5166 args
[6] = ctx
->i32one
; /* GLC */
5167 args
[7] = ctx
->i32one
; /* SLC */
5168 args
[8] = ctx
->i32zero
; /* TFE */
5172 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5176 if (!(ctx
->output_mask
& (1ull << i
)))
5179 if (i
== VARYING_SLOT_CLIP_DIST0
) {
5180 /* unpack clip and cull from a single set of slots */
5181 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
5186 for (unsigned j
= 0; j
< length
; j
++) {
5188 args
[2] = LLVMConstInt(ctx
->i32
,
5190 ctx
->gs_max_out_vertices
* 16 * 4, false);
5192 value
= ac_build_intrinsic(&ctx
->ac
,
5193 "llvm.SI.buffer.load.dword.i32.i32",
5195 AC_FUNC_ATTR_READONLY
|
5196 AC_FUNC_ATTR_LEGACY
);
5198 LLVMBuildStore(ctx
->builder
,
5199 to_float(ctx
, value
), ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)]);
5203 handle_vs_outputs_post(ctx
, &ctx
->shader_info
->vs
.outinfo
);
5206 void ac_create_gs_copy_shader(LLVMTargetMachineRef tm
,
5207 struct nir_shader
*geom_shader
,
5208 struct ac_shader_binary
*binary
,
5209 struct ac_shader_config
*config
,
5210 struct ac_shader_variant_info
*shader_info
,
5211 const struct ac_nir_compiler_options
*options
,
5214 struct nir_to_llvm_context ctx
= {0};
5215 ctx
.context
= LLVMContextCreate();
5216 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
5217 ctx
.options
= options
;
5218 ctx
.shader_info
= shader_info
;
5220 ac_llvm_context_init(&ctx
.ac
, ctx
.context
);
5221 ctx
.ac
.module
= ctx
.module
;
5223 ctx
.is_gs_copy_shader
= true;
5224 LLVMSetTarget(ctx
.module
, "amdgcn--");
5227 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
5228 ctx
.ac
.builder
= ctx
.builder
;
5229 ctx
.stage
= MESA_SHADER_VERTEX
;
5231 create_function(&ctx
);
5233 ctx
.gs_max_out_vertices
= geom_shader
->info
->gs
.vertices_out
;
5234 ac_setup_rings(&ctx
);
5236 ctx
.num_output_clips
= geom_shader
->info
->clip_distance_array_size
;
5237 ctx
.num_output_culls
= geom_shader
->info
->cull_distance_array_size
;
5239 nir_foreach_variable(variable
, &geom_shader
->outputs
)
5240 handle_shader_output_decl(&ctx
, variable
);
5242 ac_gs_copy_shader_emit(&ctx
);
5244 LLVMBuildRetVoid(ctx
.builder
);
5246 ac_llvm_finalize_module(&ctx
);
5248 ac_compile_llvm_module(tm
, ctx
.module
, binary
, config
, shader_info
,
5250 dump_shader
, options
->supports_spill
);