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>
33 #include "ac_shader_info.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_pos_offset
;
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
;
171 unsigned tes_primitive_mode
;
172 uint64_t tess_outputs_written
;
173 uint64_t tess_patch_outputs_written
;
176 static LLVMValueRef
get_sampler_desc(struct nir_to_llvm_context
*ctx
,
177 nir_deref_var
*deref
,
178 enum desc_type desc_type
);
179 static unsigned radeon_llvm_reg_index_soa(unsigned index
, unsigned chan
)
181 return (index
* 4) + chan
;
184 static unsigned shader_io_get_unique_index(gl_varying_slot slot
)
186 /* handle patch indices separate */
187 if (slot
== VARYING_SLOT_TESS_LEVEL_OUTER
)
189 if (slot
== VARYING_SLOT_TESS_LEVEL_INNER
)
191 if (slot
>= VARYING_SLOT_PATCH0
&& slot
<= VARYING_SLOT_TESS_MAX
)
192 return 2 + (slot
- VARYING_SLOT_PATCH0
);
194 if (slot
== VARYING_SLOT_POS
)
196 if (slot
== VARYING_SLOT_PSIZ
)
198 if (slot
== VARYING_SLOT_CLIP_DIST0
)
200 /* 3 is reserved for clip dist as well */
201 if (slot
>= VARYING_SLOT_VAR0
&& slot
<= VARYING_SLOT_VAR31
)
202 return 4 + (slot
- VARYING_SLOT_VAR0
);
203 unreachable("illegal slot in get unique index\n");
206 static unsigned llvm_get_type_size(LLVMTypeRef type
)
208 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
211 case LLVMIntegerTypeKind
:
212 return LLVMGetIntTypeWidth(type
) / 8;
213 case LLVMFloatTypeKind
:
215 case LLVMPointerTypeKind
:
217 case LLVMVectorTypeKind
:
218 return LLVMGetVectorSize(type
) *
219 llvm_get_type_size(LLVMGetElementType(type
));
226 static void set_llvm_calling_convention(LLVMValueRef func
,
227 gl_shader_stage stage
)
229 enum radeon_llvm_calling_convention calling_conv
;
232 case MESA_SHADER_VERTEX
:
233 case MESA_SHADER_TESS_CTRL
:
234 case MESA_SHADER_TESS_EVAL
:
235 calling_conv
= RADEON_LLVM_AMDGPU_VS
;
237 case MESA_SHADER_GEOMETRY
:
238 calling_conv
= RADEON_LLVM_AMDGPU_GS
;
240 case MESA_SHADER_FRAGMENT
:
241 calling_conv
= RADEON_LLVM_AMDGPU_PS
;
243 case MESA_SHADER_COMPUTE
:
244 calling_conv
= RADEON_LLVM_AMDGPU_CS
;
247 unreachable("Unhandle shader type");
250 LLVMSetFunctionCallConv(func
, calling_conv
);
254 create_llvm_function(LLVMContextRef ctx
, LLVMModuleRef module
,
255 LLVMBuilderRef builder
, LLVMTypeRef
*return_types
,
256 unsigned num_return_elems
, LLVMTypeRef
*param_types
,
257 unsigned param_count
, unsigned array_params_mask
,
258 unsigned sgpr_params
, bool unsafe_math
)
260 LLVMTypeRef main_function_type
, ret_type
;
261 LLVMBasicBlockRef main_function_body
;
263 if (num_return_elems
)
264 ret_type
= LLVMStructTypeInContext(ctx
, return_types
,
265 num_return_elems
, true);
267 ret_type
= LLVMVoidTypeInContext(ctx
);
269 /* Setup the function */
271 LLVMFunctionType(ret_type
, param_types
, param_count
, 0);
272 LLVMValueRef main_function
=
273 LLVMAddFunction(module
, "main", main_function_type
);
275 LLVMAppendBasicBlockInContext(ctx
, main_function
, "main_body");
276 LLVMPositionBuilderAtEnd(builder
, main_function_body
);
278 LLVMSetFunctionCallConv(main_function
, RADEON_LLVM_AMDGPU_CS
);
279 for (unsigned i
= 0; i
< sgpr_params
; ++i
) {
280 if (array_params_mask
& (1 << i
)) {
281 LLVMValueRef P
= LLVMGetParam(main_function
, i
);
282 ac_add_function_attr(ctx
, main_function
, i
+ 1, AC_FUNC_ATTR_BYVAL
);
283 ac_add_attr_dereferenceable(P
, UINT64_MAX
);
286 ac_add_function_attr(ctx
, main_function
, i
+ 1, AC_FUNC_ATTR_INREG
);
291 /* These were copied from some LLVM test. */
292 LLVMAddTargetDependentFunctionAttr(main_function
,
293 "less-precise-fpmad",
295 LLVMAddTargetDependentFunctionAttr(main_function
,
298 LLVMAddTargetDependentFunctionAttr(main_function
,
301 LLVMAddTargetDependentFunctionAttr(main_function
,
305 return main_function
;
308 static LLVMTypeRef
const_array(LLVMTypeRef elem_type
, int num_elements
)
310 return LLVMPointerType(LLVMArrayType(elem_type
, num_elements
),
314 static LLVMValueRef
get_shared_memory_ptr(struct nir_to_llvm_context
*ctx
,
322 offset
= LLVMConstInt(ctx
->i32
, idx
* 16, false);
324 ptr
= ctx
->shared_memory
;
325 ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &offset
, 1, "");
326 addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
327 ptr
= LLVMBuildBitCast(ctx
->builder
, ptr
, LLVMPointerType(type
, addr_space
), "");
331 static LLVMTypeRef
to_integer_type_scalar(struct nir_to_llvm_context
*ctx
, LLVMTypeRef t
)
333 if (t
== ctx
->f16
|| t
== ctx
->i16
)
335 else if (t
== ctx
->f32
|| t
== ctx
->i32
)
337 else if (t
== ctx
->f64
|| t
== ctx
->i64
)
340 unreachable("Unhandled integer size");
343 static LLVMTypeRef
to_integer_type(struct nir_to_llvm_context
*ctx
, LLVMTypeRef t
)
345 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
346 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
347 return LLVMVectorType(to_integer_type_scalar(ctx
, elem_type
),
348 LLVMGetVectorSize(t
));
350 return to_integer_type_scalar(ctx
, t
);
353 static LLVMValueRef
to_integer(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
)
355 LLVMTypeRef type
= LLVMTypeOf(v
);
356 return LLVMBuildBitCast(ctx
->builder
, v
, to_integer_type(ctx
, type
), "");
359 static LLVMTypeRef
to_float_type_scalar(struct nir_to_llvm_context
*ctx
, LLVMTypeRef t
)
361 if (t
== ctx
->i16
|| t
== ctx
->f16
)
363 else if (t
== ctx
->i32
|| t
== ctx
->f32
)
365 else if (t
== ctx
->i64
|| t
== ctx
->f64
)
368 unreachable("Unhandled float size");
371 static LLVMTypeRef
to_float_type(struct nir_to_llvm_context
*ctx
, LLVMTypeRef t
)
373 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
374 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
375 return LLVMVectorType(to_float_type_scalar(ctx
, elem_type
),
376 LLVMGetVectorSize(t
));
378 return to_float_type_scalar(ctx
, t
);
381 static LLVMValueRef
to_float(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
)
383 LLVMTypeRef type
= LLVMTypeOf(v
);
384 return LLVMBuildBitCast(ctx
->builder
, v
, to_float_type(ctx
, type
), "");
387 static int get_elem_bits(struct nir_to_llvm_context
*ctx
, LLVMTypeRef type
)
389 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
390 type
= LLVMGetElementType(type
);
392 if (LLVMGetTypeKind(type
) == LLVMIntegerTypeKind
)
393 return LLVMGetIntTypeWidth(type
);
395 if (type
== ctx
->f16
)
397 if (type
== ctx
->f32
)
399 if (type
== ctx
->f64
)
402 unreachable("Unhandled type kind in get_elem_bits");
405 static LLVMValueRef
unpack_param(struct nir_to_llvm_context
*ctx
,
406 LLVMValueRef param
, unsigned rshift
,
409 LLVMValueRef value
= param
;
411 value
= LLVMBuildLShr(ctx
->builder
, value
,
412 LLVMConstInt(ctx
->i32
, rshift
, false), "");
414 if (rshift
+ bitwidth
< 32) {
415 unsigned mask
= (1 << bitwidth
) - 1;
416 value
= LLVMBuildAnd(ctx
->builder
, value
,
417 LLVMConstInt(ctx
->i32
, mask
, false), "");
422 static LLVMValueRef
get_rel_patch_id(struct nir_to_llvm_context
*ctx
)
424 switch (ctx
->stage
) {
425 case MESA_SHADER_TESS_CTRL
:
426 return unpack_param(ctx
, ctx
->tcs_rel_ids
, 0, 8);
427 case MESA_SHADER_TESS_EVAL
:
428 return ctx
->tes_rel_patch_id
;
431 unreachable("Illegal stage");
435 /* Tessellation shaders pass outputs to the next shader using LDS.
437 * LS outputs = TCS inputs
438 * TCS outputs = TES inputs
441 * - TCS inputs for patch 0
442 * - TCS inputs for patch 1
443 * - TCS inputs for patch 2 = get_tcs_in_current_patch_offset (if RelPatchID==2)
445 * - TCS outputs for patch 0 = get_tcs_out_patch0_offset
446 * - Per-patch TCS outputs for patch 0 = get_tcs_out_patch0_patch_data_offset
447 * - TCS outputs for patch 1
448 * - Per-patch TCS outputs for patch 1
449 * - TCS outputs for patch 2 = get_tcs_out_current_patch_offset (if RelPatchID==2)
450 * - Per-patch TCS outputs for patch 2 = get_tcs_out_current_patch_data_offset (if RelPatchID==2)
453 * All three shaders VS(LS), TCS, TES share the same LDS space.
456 get_tcs_in_patch_stride(struct nir_to_llvm_context
*ctx
)
458 if (ctx
->stage
== MESA_SHADER_VERTEX
)
459 return unpack_param(ctx
, ctx
->ls_out_layout
, 0, 13);
460 else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
461 return unpack_param(ctx
, ctx
->tcs_in_layout
, 0, 13);
469 get_tcs_out_patch_stride(struct nir_to_llvm_context
*ctx
)
471 return unpack_param(ctx
, ctx
->tcs_out_layout
, 0, 13);
475 get_tcs_out_patch0_offset(struct nir_to_llvm_context
*ctx
)
477 return LLVMBuildMul(ctx
->builder
,
478 unpack_param(ctx
, ctx
->tcs_out_offsets
, 0, 16),
479 LLVMConstInt(ctx
->i32
, 4, false), "");
483 get_tcs_out_patch0_patch_data_offset(struct nir_to_llvm_context
*ctx
)
485 return LLVMBuildMul(ctx
->builder
,
486 unpack_param(ctx
, ctx
->tcs_out_offsets
, 16, 16),
487 LLVMConstInt(ctx
->i32
, 4, false), "");
491 get_tcs_in_current_patch_offset(struct nir_to_llvm_context
*ctx
)
493 LLVMValueRef patch_stride
= get_tcs_in_patch_stride(ctx
);
494 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
496 return LLVMBuildMul(ctx
->builder
, patch_stride
, rel_patch_id
, "");
500 get_tcs_out_current_patch_offset(struct nir_to_llvm_context
*ctx
)
502 LLVMValueRef patch0_offset
= get_tcs_out_patch0_offset(ctx
);
503 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
504 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
506 return LLVMBuildAdd(ctx
->builder
, patch0_offset
,
507 LLVMBuildMul(ctx
->builder
, patch_stride
,
513 get_tcs_out_current_patch_data_offset(struct nir_to_llvm_context
*ctx
)
515 LLVMValueRef patch0_patch_data_offset
=
516 get_tcs_out_patch0_patch_data_offset(ctx
);
517 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
518 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
520 return LLVMBuildAdd(ctx
->builder
, patch0_patch_data_offset
,
521 LLVMBuildMul(ctx
->builder
, patch_stride
,
526 static void set_userdata_location(struct ac_userdata_info
*ud_info
, uint8_t sgpr_idx
, uint8_t num_sgprs
)
528 ud_info
->sgpr_idx
= sgpr_idx
;
529 ud_info
->num_sgprs
= num_sgprs
;
530 ud_info
->indirect
= false;
531 ud_info
->indirect_offset
= 0;
534 static void set_userdata_location_shader(struct nir_to_llvm_context
*ctx
,
535 int idx
, uint8_t sgpr_idx
, uint8_t num_sgprs
)
537 set_userdata_location(&ctx
->shader_info
->user_sgprs_locs
.shader_data
[idx
], sgpr_idx
, num_sgprs
);
541 static void set_userdata_location_indirect(struct ac_userdata_info
*ud_info
, uint8_t sgpr_idx
, uint8_t num_sgprs
,
542 uint32_t indirect_offset
)
544 ud_info
->sgpr_idx
= sgpr_idx
;
545 ud_info
->num_sgprs
= num_sgprs
;
546 ud_info
->indirect
= true;
547 ud_info
->indirect_offset
= indirect_offset
;
551 static void declare_tess_lds(struct nir_to_llvm_context
*ctx
)
553 unsigned lds_size
= ctx
->options
->chip_class
>= CIK
? 65536 : 32768;
554 ctx
->lds
= LLVMBuildIntToPtr(ctx
->builder
, ctx
->i32zero
,
555 LLVMPointerType(LLVMArrayType(ctx
->i32
, lds_size
/ 4), LOCAL_ADDR_SPACE
),
559 static void create_function(struct nir_to_llvm_context
*ctx
)
561 LLVMTypeRef arg_types
[23];
562 unsigned arg_idx
= 0;
563 unsigned array_params_mask
= 0;
564 unsigned sgpr_count
= 0, user_sgpr_count
;
566 unsigned num_sets
= ctx
->options
->layout
? ctx
->options
->layout
->num_sets
: 0;
567 unsigned user_sgpr_idx
;
568 bool need_ring_offsets
= false;
570 /* until we sort out scratch/global buffers always assign ring offsets for gs/vs/es */
571 if (ctx
->stage
== MESA_SHADER_GEOMETRY
||
572 ctx
->stage
== MESA_SHADER_VERTEX
||
573 ctx
->stage
== MESA_SHADER_TESS_CTRL
||
574 ctx
->stage
== MESA_SHADER_TESS_EVAL
||
575 ctx
->stage
== MESA_SHADER_FRAGMENT
||
576 ctx
->is_gs_copy_shader
)
577 need_ring_offsets
= true;
579 if (need_ring_offsets
&& !ctx
->options
->supports_spill
) {
580 arg_types
[arg_idx
++] = const_array(ctx
->v16i8
, 16); /* address of rings */
583 /* 1 for each descriptor set */
584 for (unsigned i
= 0; i
< num_sets
; ++i
) {
585 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& (1 << ctx
->stage
)) {
586 array_params_mask
|= (1 << arg_idx
);
587 arg_types
[arg_idx
++] = const_array(ctx
->i8
, 1024 * 1024);
591 if (ctx
->shader_info
->info
.needs_push_constants
) {
592 /* 1 for push constants and dynamic descriptors */
593 array_params_mask
|= (1 << arg_idx
);
594 arg_types
[arg_idx
++] = const_array(ctx
->i8
, 1024 * 1024);
597 switch (ctx
->stage
) {
598 case MESA_SHADER_COMPUTE
:
599 if (ctx
->shader_info
->info
.cs
.grid_components_used
)
600 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, ctx
->shader_info
->info
.cs
.grid_components_used
); /* grid size */
601 user_sgpr_count
= arg_idx
;
602 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3);
603 arg_types
[arg_idx
++] = ctx
->i32
;
604 sgpr_count
= arg_idx
;
606 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3);
608 case MESA_SHADER_VERTEX
:
609 if (!ctx
->is_gs_copy_shader
) {
610 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
)
611 arg_types
[arg_idx
++] = const_array(ctx
->v16i8
, 16); /* vertex buffers */
612 arg_types
[arg_idx
++] = ctx
->i32
; // base vertex
613 arg_types
[arg_idx
++] = ctx
->i32
; // start instance
614 if (ctx
->shader_info
->info
.vs
.needs_draw_id
)
615 arg_types
[arg_idx
++] = ctx
->i32
; // draw index
617 user_sgpr_count
= arg_idx
;
618 if (ctx
->options
->key
.vs
.as_es
)
619 arg_types
[arg_idx
++] = ctx
->i32
; //es2gs offset
620 else if (ctx
->options
->key
.vs
.as_ls
) {
621 arg_types
[arg_idx
++] = ctx
->i32
; //ls out layout
624 sgpr_count
= arg_idx
;
625 arg_types
[arg_idx
++] = ctx
->i32
; // vertex id
626 if (!ctx
->is_gs_copy_shader
) {
627 arg_types
[arg_idx
++] = ctx
->i32
; // rel auto id
628 arg_types
[arg_idx
++] = ctx
->i32
; // vs prim id
629 arg_types
[arg_idx
++] = ctx
->i32
; // instance id
632 case MESA_SHADER_TESS_CTRL
:
633 arg_types
[arg_idx
++] = ctx
->i32
; // tcs offchip layout
634 arg_types
[arg_idx
++] = ctx
->i32
; // tcs out offsets
635 arg_types
[arg_idx
++] = ctx
->i32
; // tcs out layout
636 arg_types
[arg_idx
++] = ctx
->i32
; // tcs in layout
637 user_sgpr_count
= arg_idx
;
638 arg_types
[arg_idx
++] = ctx
->i32
; // param oc lds
639 arg_types
[arg_idx
++] = ctx
->i32
; // tess factor offset
640 sgpr_count
= arg_idx
;
641 arg_types
[arg_idx
++] = ctx
->i32
; // patch id
642 arg_types
[arg_idx
++] = ctx
->i32
; // rel ids;
644 case MESA_SHADER_TESS_EVAL
:
645 arg_types
[arg_idx
++] = ctx
->i32
; // tcs offchip layout
646 user_sgpr_count
= arg_idx
;
647 if (ctx
->options
->key
.tes
.as_es
) {
648 arg_types
[arg_idx
++] = ctx
->i32
; // OC LDS
649 arg_types
[arg_idx
++] = ctx
->i32
; //
650 arg_types
[arg_idx
++] = ctx
->i32
; // es2gs offset
652 arg_types
[arg_idx
++] = ctx
->i32
; //
653 arg_types
[arg_idx
++] = ctx
->i32
; // OC LDS
655 sgpr_count
= arg_idx
;
656 arg_types
[arg_idx
++] = ctx
->f32
; // tes_u
657 arg_types
[arg_idx
++] = ctx
->f32
; // tes_v
658 arg_types
[arg_idx
++] = ctx
->i32
; // tes rel patch id
659 arg_types
[arg_idx
++] = ctx
->i32
; // tes patch id
661 case MESA_SHADER_GEOMETRY
:
662 arg_types
[arg_idx
++] = ctx
->i32
; // gsvs stride
663 arg_types
[arg_idx
++] = ctx
->i32
; // gsvs num entires
664 user_sgpr_count
= arg_idx
;
665 arg_types
[arg_idx
++] = ctx
->i32
; // gs2vs offset
666 arg_types
[arg_idx
++] = ctx
->i32
; // wave id
667 sgpr_count
= arg_idx
;
668 arg_types
[arg_idx
++] = ctx
->i32
; // vtx0
669 arg_types
[arg_idx
++] = ctx
->i32
; // vtx1
670 arg_types
[arg_idx
++] = ctx
->i32
; // prim id
671 arg_types
[arg_idx
++] = ctx
->i32
; // vtx2
672 arg_types
[arg_idx
++] = ctx
->i32
; // vtx3
673 arg_types
[arg_idx
++] = ctx
->i32
; // vtx4
674 arg_types
[arg_idx
++] = ctx
->i32
; // vtx5
675 arg_types
[arg_idx
++] = ctx
->i32
; // GS instance id
677 case MESA_SHADER_FRAGMENT
:
678 if (ctx
->shader_info
->info
.ps
.needs_sample_positions
)
679 arg_types
[arg_idx
++] = ctx
->i32
; /* sample position offset */
680 user_sgpr_count
= arg_idx
;
681 arg_types
[arg_idx
++] = ctx
->i32
; /* prim mask */
682 sgpr_count
= arg_idx
;
683 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp sample */
684 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp center */
685 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp centroid */
686 arg_types
[arg_idx
++] = ctx
->v3i32
; /* persp pull model */
687 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear sample */
688 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear center */
689 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear centroid */
690 arg_types
[arg_idx
++] = ctx
->f32
; /* line stipple tex */
691 arg_types
[arg_idx
++] = ctx
->f32
; /* pos x float */
692 arg_types
[arg_idx
++] = ctx
->f32
; /* pos y float */
693 arg_types
[arg_idx
++] = ctx
->f32
; /* pos z float */
694 arg_types
[arg_idx
++] = ctx
->f32
; /* pos w float */
695 arg_types
[arg_idx
++] = ctx
->i32
; /* front face */
696 arg_types
[arg_idx
++] = ctx
->i32
; /* ancillary */
697 arg_types
[arg_idx
++] = ctx
->i32
; /* sample coverage */
698 arg_types
[arg_idx
++] = ctx
->i32
; /* fixed pt */
701 unreachable("Shader stage not implemented");
704 ctx
->main_function
= create_llvm_function(
705 ctx
->context
, ctx
->module
, ctx
->builder
, NULL
, 0, arg_types
,
706 arg_idx
, array_params_mask
, sgpr_count
, ctx
->options
->unsafe_math
);
707 set_llvm_calling_convention(ctx
->main_function
, ctx
->stage
);
709 ctx
->shader_info
->num_input_sgprs
= 0;
710 ctx
->shader_info
->num_input_vgprs
= 0;
712 ctx
->shader_info
->num_user_sgprs
= ctx
->options
->supports_spill
? 2 : 0;
713 for (i
= 0; i
< user_sgpr_count
; i
++)
714 ctx
->shader_info
->num_user_sgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
716 ctx
->shader_info
->num_input_sgprs
= ctx
->shader_info
->num_user_sgprs
;
717 for (; i
< sgpr_count
; i
++)
718 ctx
->shader_info
->num_input_sgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
720 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
721 for (; i
< arg_idx
; ++i
)
722 ctx
->shader_info
->num_input_vgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
727 if (ctx
->options
->supports_spill
|| need_ring_offsets
) {
728 set_userdata_location_shader(ctx
, AC_UD_SCRATCH_RING_OFFSETS
, user_sgpr_idx
, 2);
730 if (ctx
->options
->supports_spill
) {
731 ctx
->ring_offsets
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.implicit.buffer.ptr",
732 LLVMPointerType(ctx
->i8
, CONST_ADDR_SPACE
),
733 NULL
, 0, AC_FUNC_ATTR_READNONE
);
734 ctx
->ring_offsets
= LLVMBuildBitCast(ctx
->builder
, ctx
->ring_offsets
,
735 const_array(ctx
->v16i8
, 16), "");
737 ctx
->ring_offsets
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
740 for (unsigned i
= 0; i
< num_sets
; ++i
) {
741 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& (1 << ctx
->stage
)) {
742 set_userdata_location(&ctx
->shader_info
->user_sgprs_locs
.descriptor_sets
[i
], user_sgpr_idx
, 2);
744 ctx
->descriptor_sets
[i
] =
745 LLVMGetParam(ctx
->main_function
, arg_idx
++);
747 ctx
->descriptor_sets
[i
] = NULL
;
750 if (ctx
->shader_info
->info
.needs_push_constants
) {
751 ctx
->push_constants
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
752 set_userdata_location_shader(ctx
, AC_UD_PUSH_CONSTANTS
, user_sgpr_idx
, 2);
756 switch (ctx
->stage
) {
757 case MESA_SHADER_COMPUTE
:
758 if (ctx
->shader_info
->info
.cs
.grid_components_used
) {
759 set_userdata_location_shader(ctx
, AC_UD_CS_GRID_SIZE
, user_sgpr_idx
, ctx
->shader_info
->info
.cs
.grid_components_used
);
760 user_sgpr_idx
+= ctx
->shader_info
->info
.cs
.grid_components_used
;
761 ctx
->num_work_groups
=
762 LLVMGetParam(ctx
->main_function
, arg_idx
++);
765 LLVMGetParam(ctx
->main_function
, arg_idx
++);
767 LLVMGetParam(ctx
->main_function
, arg_idx
++);
768 ctx
->local_invocation_ids
=
769 LLVMGetParam(ctx
->main_function
, arg_idx
++);
771 case MESA_SHADER_VERTEX
:
772 if (!ctx
->is_gs_copy_shader
) {
773 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
) {
774 set_userdata_location_shader(ctx
, AC_UD_VS_VERTEX_BUFFERS
, user_sgpr_idx
, 2);
776 ctx
->vertex_buffers
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
779 if (ctx
->shader_info
->info
.vs
.needs_draw_id
)
782 set_userdata_location_shader(ctx
, AC_UD_VS_BASE_VERTEX_START_INSTANCE
, user_sgpr_idx
, vs_num
);
783 user_sgpr_idx
+= vs_num
;
785 ctx
->base_vertex
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
786 ctx
->start_instance
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
787 if (ctx
->shader_info
->info
.vs
.needs_draw_id
)
788 ctx
->draw_index
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
790 if (ctx
->options
->key
.vs
.as_es
)
791 ctx
->es2gs_offset
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
792 else if (ctx
->options
->key
.vs
.as_ls
) {
793 set_userdata_location_shader(ctx
, AC_UD_VS_LS_TCS_IN_LAYOUT
, user_sgpr_idx
, 1);
795 ctx
->ls_out_layout
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
797 ctx
->vertex_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
798 if (!ctx
->is_gs_copy_shader
) {
799 ctx
->rel_auto_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
800 ctx
->vs_prim_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
801 ctx
->instance_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
803 if (ctx
->options
->key
.vs
.as_ls
)
804 declare_tess_lds(ctx
);
806 case MESA_SHADER_TESS_CTRL
:
807 set_userdata_location_shader(ctx
, AC_UD_TCS_OFFCHIP_LAYOUT
, user_sgpr_idx
, 4);
809 ctx
->tcs_offchip_layout
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
810 ctx
->tcs_out_offsets
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
811 ctx
->tcs_out_layout
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
812 ctx
->tcs_in_layout
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
813 ctx
->oc_lds
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
814 ctx
->tess_factor_offset
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
815 ctx
->tcs_patch_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
816 ctx
->tcs_rel_ids
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
818 declare_tess_lds(ctx
);
820 case MESA_SHADER_TESS_EVAL
:
821 set_userdata_location_shader(ctx
, AC_UD_TES_OFFCHIP_LAYOUT
, user_sgpr_idx
, 1);
823 ctx
->tcs_offchip_layout
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
824 if (ctx
->options
->key
.tes
.as_es
) {
825 ctx
->oc_lds
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
827 ctx
->es2gs_offset
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
830 ctx
->oc_lds
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
832 ctx
->tes_u
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
833 ctx
->tes_v
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
834 ctx
->tes_rel_patch_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
835 ctx
->tes_patch_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
837 case MESA_SHADER_GEOMETRY
:
838 set_userdata_location_shader(ctx
, AC_UD_GS_VS_RING_STRIDE_ENTRIES
, user_sgpr_idx
, 2);
840 ctx
->gsvs_ring_stride
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
841 ctx
->gsvs_num_entries
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
842 ctx
->gs2vs_offset
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
843 ctx
->gs_wave_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
844 ctx
->gs_vtx_offset
[0] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
845 ctx
->gs_vtx_offset
[1] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
846 ctx
->gs_prim_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
847 ctx
->gs_vtx_offset
[2] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
848 ctx
->gs_vtx_offset
[3] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
849 ctx
->gs_vtx_offset
[4] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
850 ctx
->gs_vtx_offset
[5] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
851 ctx
->gs_invocation_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
853 case MESA_SHADER_FRAGMENT
:
854 if (ctx
->shader_info
->info
.ps
.needs_sample_positions
) {
855 set_userdata_location_shader(ctx
, AC_UD_PS_SAMPLE_POS_OFFSET
, user_sgpr_idx
, 1);
857 ctx
->sample_pos_offset
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
859 ctx
->prim_mask
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
860 ctx
->persp_sample
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
861 ctx
->persp_center
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
862 ctx
->persp_centroid
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
864 ctx
->linear_sample
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
865 ctx
->linear_center
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
866 ctx
->linear_centroid
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
867 arg_idx
++; /* line stipple */
868 ctx
->frag_pos
[0] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
869 ctx
->frag_pos
[1] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
870 ctx
->frag_pos
[2] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
871 ctx
->frag_pos
[3] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
872 ctx
->front_face
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
873 ctx
->ancillary
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
874 ctx
->sample_coverage
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
877 unreachable("Shader stage not implemented");
881 static void setup_types(struct nir_to_llvm_context
*ctx
)
883 LLVMValueRef args
[4];
885 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->context
);
886 ctx
->i1
= LLVMIntTypeInContext(ctx
->context
, 1);
887 ctx
->i8
= LLVMIntTypeInContext(ctx
->context
, 8);
888 ctx
->i16
= LLVMIntTypeInContext(ctx
->context
, 16);
889 ctx
->i32
= LLVMIntTypeInContext(ctx
->context
, 32);
890 ctx
->i64
= LLVMIntTypeInContext(ctx
->context
, 64);
891 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
892 ctx
->v3i32
= LLVMVectorType(ctx
->i32
, 3);
893 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
894 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
895 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
896 ctx
->f16
= LLVMHalfTypeInContext(ctx
->context
);
897 ctx
->f64
= LLVMDoubleTypeInContext(ctx
->context
);
898 ctx
->v2f32
= LLVMVectorType(ctx
->f32
, 2);
899 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
900 ctx
->v16i8
= LLVMVectorType(ctx
->i8
, 16);
902 ctx
->i1false
= LLVMConstInt(ctx
->i1
, 0, false);
903 ctx
->i1true
= LLVMConstInt(ctx
->i1
, 1, false);
904 ctx
->i32zero
= LLVMConstInt(ctx
->i32
, 0, false);
905 ctx
->i32one
= LLVMConstInt(ctx
->i32
, 1, false);
906 ctx
->f32zero
= LLVMConstReal(ctx
->f32
, 0.0);
907 ctx
->f32one
= LLVMConstReal(ctx
->f32
, 1.0);
909 args
[0] = ctx
->f32zero
;
910 args
[1] = ctx
->f32zero
;
911 args
[2] = ctx
->f32zero
;
912 args
[3] = ctx
->f32one
;
913 ctx
->v4f32empty
= LLVMConstVector(args
, 4);
915 ctx
->uniform_md_kind
=
916 LLVMGetMDKindIDInContext(ctx
->context
, "amdgpu.uniform", 14);
917 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
919 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
922 static int get_llvm_num_components(LLVMValueRef value
)
924 LLVMTypeRef type
= LLVMTypeOf(value
);
925 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
926 ? LLVMGetVectorSize(type
)
928 return num_components
;
931 static LLVMValueRef
llvm_extract_elem(struct nir_to_llvm_context
*ctx
,
935 int count
= get_llvm_num_components(value
);
937 assert(index
< count
);
941 return LLVMBuildExtractElement(ctx
->builder
, value
,
942 LLVMConstInt(ctx
->i32
, index
, false), "");
945 static LLVMValueRef
trim_vector(struct nir_to_llvm_context
*ctx
,
946 LLVMValueRef value
, unsigned count
)
948 unsigned num_components
= get_llvm_num_components(value
);
949 if (count
== num_components
)
952 LLVMValueRef masks
[] = {
953 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
954 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false)};
957 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
960 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
961 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
965 build_store_values_extended(struct nir_to_llvm_context
*ctx
,
966 LLVMValueRef
*values
,
967 unsigned value_count
,
968 unsigned value_stride
,
971 LLVMBuilderRef builder
= ctx
->builder
;
974 if (value_count
== 1) {
975 LLVMBuildStore(builder
, vec
, values
[0]);
979 for (i
= 0; i
< value_count
; i
++) {
980 LLVMValueRef ptr
= values
[i
* value_stride
];
981 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
982 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
983 LLVMBuildStore(builder
, value
, ptr
);
987 static LLVMTypeRef
get_def_type(struct nir_to_llvm_context
*ctx
,
990 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->context
, def
->bit_size
);
991 if (def
->num_components
> 1) {
992 type
= LLVMVectorType(type
, def
->num_components
);
997 static LLVMValueRef
get_src(struct nir_to_llvm_context
*ctx
, nir_src src
)
1000 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->defs
, src
.ssa
);
1001 return (LLVMValueRef
)entry
->data
;
1005 static LLVMBasicBlockRef
get_block(struct nir_to_llvm_context
*ctx
,
1006 struct nir_block
*b
)
1008 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->defs
, b
);
1009 return (LLVMBasicBlockRef
)entry
->data
;
1012 static LLVMValueRef
get_alu_src(struct nir_to_llvm_context
*ctx
,
1014 unsigned num_components
)
1016 LLVMValueRef value
= get_src(ctx
, src
.src
);
1017 bool need_swizzle
= false;
1020 LLVMTypeRef type
= LLVMTypeOf(value
);
1021 unsigned src_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
1022 ? LLVMGetVectorSize(type
)
1025 for (unsigned i
= 0; i
< num_components
; ++i
) {
1026 assert(src
.swizzle
[i
] < src_components
);
1027 if (src
.swizzle
[i
] != i
)
1028 need_swizzle
= true;
1031 if (need_swizzle
|| num_components
!= src_components
) {
1032 LLVMValueRef masks
[] = {
1033 LLVMConstInt(ctx
->i32
, src
.swizzle
[0], false),
1034 LLVMConstInt(ctx
->i32
, src
.swizzle
[1], false),
1035 LLVMConstInt(ctx
->i32
, src
.swizzle
[2], false),
1036 LLVMConstInt(ctx
->i32
, src
.swizzle
[3], false)};
1038 if (src_components
> 1 && num_components
== 1) {
1039 value
= LLVMBuildExtractElement(ctx
->builder
, value
,
1041 } else if (src_components
== 1 && num_components
> 1) {
1042 LLVMValueRef values
[] = {value
, value
, value
, value
};
1043 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
1045 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
1046 value
= LLVMBuildShuffleVector(ctx
->builder
, value
, value
,
1050 assert(!src
.negate
);
1055 static LLVMValueRef
emit_int_cmp(struct nir_to_llvm_context
*ctx
,
1056 LLVMIntPredicate pred
, LLVMValueRef src0
,
1059 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
1060 return LLVMBuildSelect(ctx
->builder
, result
,
1061 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
1062 LLVMConstInt(ctx
->i32
, 0, false), "");
1065 static LLVMValueRef
emit_float_cmp(struct nir_to_llvm_context
*ctx
,
1066 LLVMRealPredicate pred
, LLVMValueRef src0
,
1069 LLVMValueRef result
;
1070 src0
= to_float(ctx
, src0
);
1071 src1
= to_float(ctx
, src1
);
1072 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
1073 return LLVMBuildSelect(ctx
->builder
, result
,
1074 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
1075 LLVMConstInt(ctx
->i32
, 0, false), "");
1078 static LLVMValueRef
emit_intrin_1f_param(struct nir_to_llvm_context
*ctx
,
1080 LLVMTypeRef result_type
,
1084 LLVMValueRef params
[] = {
1085 to_float(ctx
, src0
),
1088 sprintf(name
, "%s.f%d", intrin
, get_elem_bits(ctx
, result_type
));
1089 return ac_build_intrinsic(&ctx
->ac
, name
, result_type
, params
, 1, AC_FUNC_ATTR_READNONE
);
1092 static LLVMValueRef
emit_intrin_2f_param(struct nir_to_llvm_context
*ctx
,
1094 LLVMTypeRef result_type
,
1095 LLVMValueRef src0
, LLVMValueRef src1
)
1098 LLVMValueRef params
[] = {
1099 to_float(ctx
, src0
),
1100 to_float(ctx
, src1
),
1103 sprintf(name
, "%s.f%d", intrin
, get_elem_bits(ctx
, result_type
));
1104 return ac_build_intrinsic(&ctx
->ac
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
1107 static LLVMValueRef
emit_intrin_3f_param(struct nir_to_llvm_context
*ctx
,
1109 LLVMTypeRef result_type
,
1110 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
1113 LLVMValueRef params
[] = {
1114 to_float(ctx
, src0
),
1115 to_float(ctx
, src1
),
1116 to_float(ctx
, src2
),
1119 sprintf(name
, "%s.f%d", intrin
, get_elem_bits(ctx
, result_type
));
1120 return ac_build_intrinsic(&ctx
->ac
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
1123 static LLVMValueRef
emit_bcsel(struct nir_to_llvm_context
*ctx
,
1124 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
1126 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
1128 return LLVMBuildSelect(ctx
->builder
, v
, src1
, src2
, "");
1131 static LLVMValueRef
emit_find_lsb(struct nir_to_llvm_context
*ctx
,
1134 LLVMValueRef params
[2] = {
1137 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
1138 * add special code to check for x=0. The reason is that
1139 * the LLVM behavior for x=0 is different from what we
1142 * The hardware already implements the correct behavior.
1144 LLVMConstInt(ctx
->i32
, 1, false),
1146 return ac_build_intrinsic(&ctx
->ac
, "llvm.cttz.i32", ctx
->i32
, params
, 2, AC_FUNC_ATTR_READNONE
);
1149 static LLVMValueRef
emit_ifind_msb(struct nir_to_llvm_context
*ctx
,
1152 return ac_build_imsb(&ctx
->ac
, src0
, ctx
->i32
);
1155 static LLVMValueRef
emit_ufind_msb(struct nir_to_llvm_context
*ctx
,
1158 return ac_build_umsb(&ctx
->ac
, src0
, ctx
->i32
);
1161 static LLVMValueRef
emit_minmax_int(struct nir_to_llvm_context
*ctx
,
1162 LLVMIntPredicate pred
,
1163 LLVMValueRef src0
, LLVMValueRef src1
)
1165 return LLVMBuildSelect(ctx
->builder
,
1166 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
1171 static LLVMValueRef
emit_iabs(struct nir_to_llvm_context
*ctx
,
1174 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
1175 LLVMBuildNeg(ctx
->builder
, src0
, ""));
1178 static LLVMValueRef
emit_fsign(struct nir_to_llvm_context
*ctx
,
1181 LLVMValueRef cmp
, val
;
1183 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, ctx
->f32zero
, "");
1184 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->f32one
, src0
, "");
1185 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, ctx
->f32zero
, "");
1186 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(ctx
->f32
, -1.0), "");
1190 static LLVMValueRef
emit_isign(struct nir_to_llvm_context
*ctx
,
1193 LLVMValueRef cmp
, val
;
1195 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, ctx
->i32zero
, "");
1196 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->i32one
, src0
, "");
1197 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, ctx
->i32zero
, "");
1198 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(ctx
->i32
, -1, true), "");
1202 static LLVMValueRef
emit_ffract(struct nir_to_llvm_context
*ctx
,
1205 const char *intr
= "llvm.floor.f32";
1206 LLVMValueRef fsrc0
= to_float(ctx
, src0
);
1207 LLVMValueRef params
[] = {
1210 LLVMValueRef floor
= ac_build_intrinsic(&ctx
->ac
, intr
,
1211 ctx
->f32
, params
, 1,
1212 AC_FUNC_ATTR_READNONE
);
1213 return LLVMBuildFSub(ctx
->builder
, fsrc0
, floor
, "");
1216 static LLVMValueRef
emit_uint_carry(struct nir_to_llvm_context
*ctx
,
1218 LLVMValueRef src0
, LLVMValueRef src1
)
1220 LLVMTypeRef ret_type
;
1221 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
1223 LLVMValueRef params
[] = { src0
, src1
};
1224 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
1227 res
= ac_build_intrinsic(&ctx
->ac
, intrin
, ret_type
,
1228 params
, 2, AC_FUNC_ATTR_READNONE
);
1230 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
1231 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
1235 static LLVMValueRef
emit_b2f(struct nir_to_llvm_context
*ctx
,
1238 return LLVMBuildAnd(ctx
->builder
, src0
, LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""), "");
1241 static LLVMValueRef
emit_umul_high(struct nir_to_llvm_context
*ctx
,
1242 LLVMValueRef src0
, LLVMValueRef src1
)
1244 LLVMValueRef dst64
, result
;
1245 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
1246 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
1248 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1249 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1250 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1254 static LLVMValueRef
emit_imul_high(struct nir_to_llvm_context
*ctx
,
1255 LLVMValueRef src0
, LLVMValueRef src1
)
1257 LLVMValueRef dst64
, result
;
1258 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
1259 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
1261 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1262 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1263 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1267 static LLVMValueRef
emit_bitfield_extract(struct nir_to_llvm_context
*ctx
,
1269 LLVMValueRef srcs
[3])
1271 LLVMValueRef result
;
1272 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
1274 result
= ac_build_bfe(&ctx
->ac
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
1275 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
1279 static LLVMValueRef
emit_bitfield_insert(struct nir_to_llvm_context
*ctx
,
1280 LLVMValueRef src0
, LLVMValueRef src1
,
1281 LLVMValueRef src2
, LLVMValueRef src3
)
1283 LLVMValueRef bfi_args
[3], result
;
1285 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
1286 LLVMBuildSub(ctx
->builder
,
1287 LLVMBuildShl(ctx
->builder
,
1292 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
1295 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
1298 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
1299 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
1301 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
1302 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
1303 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
1305 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
1309 static LLVMValueRef
emit_pack_half_2x16(struct nir_to_llvm_context
*ctx
,
1312 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1314 LLVMValueRef comp
[2];
1316 src0
= to_float(ctx
, src0
);
1317 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32zero
, "");
1318 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32one
, "");
1319 for (i
= 0; i
< 2; i
++) {
1320 comp
[i
] = LLVMBuildFPTrunc(ctx
->builder
, comp
[i
], ctx
->f16
, "");
1321 comp
[i
] = LLVMBuildBitCast(ctx
->builder
, comp
[i
], ctx
->i16
, "");
1322 comp
[i
] = LLVMBuildZExt(ctx
->builder
, comp
[i
], ctx
->i32
, "");
1325 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
1326 comp
[0] = LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
1331 static LLVMValueRef
emit_unpack_half_2x16(struct nir_to_llvm_context
*ctx
,
1334 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1335 LLVMValueRef temps
[2], result
, val
;
1338 for (i
= 0; i
< 2; i
++) {
1339 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
1340 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
1341 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
1342 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
1345 result
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), temps
[0],
1347 result
= LLVMBuildInsertElement(ctx
->builder
, result
, temps
[1],
1352 static LLVMValueRef
emit_ddxy(struct nir_to_llvm_context
*ctx
,
1358 LLVMValueRef result
;
1359 ctx
->has_ddxy
= true;
1361 if (!ctx
->lds
&& !ctx
->has_ds_bpermute
)
1362 ctx
->lds
= LLVMAddGlobalInAddressSpace(ctx
->module
,
1363 LLVMArrayType(ctx
->i32
, 64),
1364 "ddxy_lds", LOCAL_ADDR_SPACE
);
1366 if (op
== nir_op_fddx_fine
|| op
== nir_op_fddx
)
1367 mask
= AC_TID_MASK_LEFT
;
1368 else if (op
== nir_op_fddy_fine
|| op
== nir_op_fddy
)
1369 mask
= AC_TID_MASK_TOP
;
1371 mask
= AC_TID_MASK_TOP_LEFT
;
1373 /* for DDX we want to next X pixel, DDY next Y pixel. */
1374 if (op
== nir_op_fddx_fine
||
1375 op
== nir_op_fddx_coarse
||
1381 result
= ac_build_ddxy(&ctx
->ac
, ctx
->has_ds_bpermute
,
1382 mask
, idx
, ctx
->lds
,
1388 * this takes an I,J coordinate pair,
1389 * and works out the X and Y derivatives.
1390 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
1392 static LLVMValueRef
emit_ddxy_interp(
1393 struct nir_to_llvm_context
*ctx
,
1394 LLVMValueRef interp_ij
)
1396 LLVMValueRef result
[4], a
;
1399 for (i
= 0; i
< 2; i
++) {
1400 a
= LLVMBuildExtractElement(ctx
->builder
, interp_ij
,
1401 LLVMConstInt(ctx
->i32
, i
, false), "");
1402 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
1403 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
1405 return ac_build_gather_values(&ctx
->ac
, result
, 4);
1408 static void visit_alu(struct nir_to_llvm_context
*ctx
, nir_alu_instr
*instr
)
1410 LLVMValueRef src
[4], result
= NULL
;
1411 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
1412 unsigned src_components
;
1413 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
1415 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
1416 switch (instr
->op
) {
1422 case nir_op_pack_half_2x16
:
1425 case nir_op_unpack_half_2x16
:
1429 src_components
= num_components
;
1432 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1433 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
1435 switch (instr
->op
) {
1441 src
[0] = to_float(ctx
, src
[0]);
1442 result
= LLVMBuildFNeg(ctx
->builder
, src
[0], "");
1445 result
= LLVMBuildNeg(ctx
->builder
, src
[0], "");
1448 result
= LLVMBuildNot(ctx
->builder
, src
[0], "");
1451 result
= LLVMBuildAdd(ctx
->builder
, src
[0], src
[1], "");
1454 src
[0] = to_float(ctx
, src
[0]);
1455 src
[1] = to_float(ctx
, src
[1]);
1456 result
= LLVMBuildFAdd(ctx
->builder
, src
[0], src
[1], "");
1459 src
[0] = to_float(ctx
, src
[0]);
1460 src
[1] = to_float(ctx
, src
[1]);
1461 result
= LLVMBuildFSub(ctx
->builder
, src
[0], src
[1], "");
1464 result
= LLVMBuildSub(ctx
->builder
, src
[0], src
[1], "");
1467 result
= LLVMBuildMul(ctx
->builder
, src
[0], src
[1], "");
1470 result
= LLVMBuildSRem(ctx
->builder
, src
[0], src
[1], "");
1473 result
= LLVMBuildURem(ctx
->builder
, src
[0], src
[1], "");
1476 src
[0] = to_float(ctx
, src
[0]);
1477 src
[1] = to_float(ctx
, src
[1]);
1478 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1479 result
= emit_intrin_1f_param(ctx
, "llvm.floor",
1480 to_float_type(ctx
, def_type
), result
);
1481 result
= LLVMBuildFMul(ctx
->builder
, src
[1] , result
, "");
1482 result
= LLVMBuildFSub(ctx
->builder
, src
[0], result
, "");
1485 src
[0] = to_float(ctx
, src
[0]);
1486 src
[1] = to_float(ctx
, src
[1]);
1487 result
= LLVMBuildFRem(ctx
->builder
, src
[0], src
[1], "");
1490 result
= LLVMBuildSRem(ctx
->builder
, src
[0], src
[1], "");
1493 result
= LLVMBuildSDiv(ctx
->builder
, src
[0], src
[1], "");
1496 result
= LLVMBuildUDiv(ctx
->builder
, src
[0], src
[1], "");
1499 src
[0] = to_float(ctx
, src
[0]);
1500 src
[1] = to_float(ctx
, src
[1]);
1501 result
= LLVMBuildFMul(ctx
->builder
, src
[0], src
[1], "");
1504 src
[0] = to_float(ctx
, src
[0]);
1505 src
[1] = to_float(ctx
, src
[1]);
1506 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1509 src
[0] = to_float(ctx
, src
[0]);
1510 result
= ac_build_fdiv(&ctx
->ac
, ctx
->f32one
, src
[0]);
1513 result
= LLVMBuildAnd(ctx
->builder
, src
[0], src
[1], "");
1516 result
= LLVMBuildOr(ctx
->builder
, src
[0], src
[1], "");
1519 result
= LLVMBuildXor(ctx
->builder
, src
[0], src
[1], "");
1522 result
= LLVMBuildShl(ctx
->builder
, src
[0], src
[1], "");
1525 result
= LLVMBuildAShr(ctx
->builder
, src
[0], src
[1], "");
1528 result
= LLVMBuildLShr(ctx
->builder
, src
[0], src
[1], "");
1531 result
= emit_int_cmp(ctx
, LLVMIntSLT
, src
[0], src
[1]);
1534 result
= emit_int_cmp(ctx
, LLVMIntNE
, src
[0], src
[1]);
1537 result
= emit_int_cmp(ctx
, LLVMIntEQ
, src
[0], src
[1]);
1540 result
= emit_int_cmp(ctx
, LLVMIntSGE
, src
[0], src
[1]);
1543 result
= emit_int_cmp(ctx
, LLVMIntULT
, src
[0], src
[1]);
1546 result
= emit_int_cmp(ctx
, LLVMIntUGE
, src
[0], src
[1]);
1549 result
= emit_float_cmp(ctx
, LLVMRealUEQ
, src
[0], src
[1]);
1552 result
= emit_float_cmp(ctx
, LLVMRealUNE
, src
[0], src
[1]);
1555 result
= emit_float_cmp(ctx
, LLVMRealULT
, src
[0], src
[1]);
1558 result
= emit_float_cmp(ctx
, LLVMRealUGE
, src
[0], src
[1]);
1561 result
= emit_intrin_1f_param(ctx
, "llvm.fabs",
1562 to_float_type(ctx
, def_type
), src
[0]);
1565 result
= emit_iabs(ctx
, src
[0]);
1568 result
= emit_minmax_int(ctx
, LLVMIntSGT
, src
[0], src
[1]);
1571 result
= emit_minmax_int(ctx
, LLVMIntSLT
, src
[0], src
[1]);
1574 result
= emit_minmax_int(ctx
, LLVMIntUGT
, src
[0], src
[1]);
1577 result
= emit_minmax_int(ctx
, LLVMIntULT
, src
[0], src
[1]);
1580 result
= emit_isign(ctx
, src
[0]);
1583 src
[0] = to_float(ctx
, src
[0]);
1584 result
= emit_fsign(ctx
, src
[0]);
1587 result
= emit_intrin_1f_param(ctx
, "llvm.floor",
1588 to_float_type(ctx
, def_type
), src
[0]);
1591 result
= emit_intrin_1f_param(ctx
, "llvm.trunc",
1592 to_float_type(ctx
, def_type
), src
[0]);
1595 result
= emit_intrin_1f_param(ctx
, "llvm.ceil",
1596 to_float_type(ctx
, def_type
), src
[0]);
1598 case nir_op_fround_even
:
1599 result
= emit_intrin_1f_param(ctx
, "llvm.rint",
1600 to_float_type(ctx
, def_type
),src
[0]);
1603 result
= emit_ffract(ctx
, src
[0]);
1606 result
= emit_intrin_1f_param(ctx
, "llvm.sin",
1607 to_float_type(ctx
, def_type
), src
[0]);
1610 result
= emit_intrin_1f_param(ctx
, "llvm.cos",
1611 to_float_type(ctx
, def_type
), src
[0]);
1614 result
= emit_intrin_1f_param(ctx
, "llvm.sqrt",
1615 to_float_type(ctx
, def_type
), src
[0]);
1618 result
= emit_intrin_1f_param(ctx
, "llvm.exp2",
1619 to_float_type(ctx
, def_type
), src
[0]);
1622 result
= emit_intrin_1f_param(ctx
, "llvm.log2",
1623 to_float_type(ctx
, def_type
), src
[0]);
1626 result
= emit_intrin_1f_param(ctx
, "llvm.sqrt",
1627 to_float_type(ctx
, def_type
), src
[0]);
1628 result
= ac_build_fdiv(&ctx
->ac
, ctx
->f32one
, result
);
1631 result
= emit_intrin_2f_param(ctx
, "llvm.pow",
1632 to_float_type(ctx
, def_type
), src
[0], src
[1]);
1635 result
= emit_intrin_2f_param(ctx
, "llvm.maxnum",
1636 to_float_type(ctx
, def_type
), src
[0], src
[1]);
1639 result
= emit_intrin_2f_param(ctx
, "llvm.minnum",
1640 to_float_type(ctx
, def_type
), src
[0], src
[1]);
1643 result
= emit_intrin_3f_param(ctx
, "llvm.fma",
1644 to_float_type(ctx
, def_type
), src
[0], src
[1], src
[2]);
1646 case nir_op_ibitfield_extract
:
1647 result
= emit_bitfield_extract(ctx
, true, src
);
1649 case nir_op_ubitfield_extract
:
1650 result
= emit_bitfield_extract(ctx
, false, src
);
1652 case nir_op_bitfield_insert
:
1653 result
= emit_bitfield_insert(ctx
, src
[0], src
[1], src
[2], src
[3]);
1655 case nir_op_bitfield_reverse
:
1656 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.bitreverse.i32", ctx
->i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1658 case nir_op_bit_count
:
1659 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i32", ctx
->i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1664 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1665 src
[i
] = to_integer(ctx
, src
[i
]);
1666 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
1670 src
[0] = to_float(ctx
, src
[0]);
1671 result
= LLVMBuildFPToSI(ctx
->builder
, src
[0], def_type
, "");
1675 src
[0] = to_float(ctx
, src
[0]);
1676 result
= LLVMBuildFPToUI(ctx
->builder
, src
[0], def_type
, "");
1680 result
= LLVMBuildSIToFP(ctx
->builder
, src
[0], to_float_type(ctx
, def_type
), "");
1684 result
= LLVMBuildUIToFP(ctx
->builder
, src
[0], to_float_type(ctx
, def_type
), "");
1687 result
= LLVMBuildFPExt(ctx
->builder
, src
[0], to_float_type(ctx
, def_type
), "");
1690 result
= LLVMBuildFPTrunc(ctx
->builder
, src
[0], to_float_type(ctx
, def_type
), "");
1694 if (get_elem_bits(ctx
, LLVMTypeOf(src
[0])) < get_elem_bits(ctx
, def_type
))
1695 result
= LLVMBuildZExt(ctx
->builder
, src
[0], def_type
, "");
1697 result
= LLVMBuildTrunc(ctx
->builder
, src
[0], def_type
, "");
1701 if (get_elem_bits(ctx
, LLVMTypeOf(src
[0])) < get_elem_bits(ctx
, def_type
))
1702 result
= LLVMBuildSExt(ctx
->builder
, src
[0], def_type
, "");
1704 result
= LLVMBuildTrunc(ctx
->builder
, src
[0], def_type
, "");
1707 result
= emit_bcsel(ctx
, src
[0], src
[1], src
[2]);
1709 case nir_op_find_lsb
:
1710 result
= emit_find_lsb(ctx
, src
[0]);
1712 case nir_op_ufind_msb
:
1713 result
= emit_ufind_msb(ctx
, src
[0]);
1715 case nir_op_ifind_msb
:
1716 result
= emit_ifind_msb(ctx
, src
[0]);
1718 case nir_op_uadd_carry
:
1719 result
= emit_uint_carry(ctx
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
1721 case nir_op_usub_borrow
:
1722 result
= emit_uint_carry(ctx
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
1725 result
= emit_b2f(ctx
, src
[0]);
1727 case nir_op_fquantize2f16
:
1728 src
[0] = to_float(ctx
, src
[0]);
1729 result
= LLVMBuildFPTrunc(ctx
->builder
, src
[0], ctx
->f16
, "");
1730 /* need to convert back up to f32 */
1731 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
1733 case nir_op_umul_high
:
1734 result
= emit_umul_high(ctx
, src
[0], src
[1]);
1736 case nir_op_imul_high
:
1737 result
= emit_imul_high(ctx
, src
[0], src
[1]);
1739 case nir_op_pack_half_2x16
:
1740 result
= emit_pack_half_2x16(ctx
, src
[0]);
1742 case nir_op_unpack_half_2x16
:
1743 result
= emit_unpack_half_2x16(ctx
, src
[0]);
1747 case nir_op_fddx_fine
:
1748 case nir_op_fddy_fine
:
1749 case nir_op_fddx_coarse
:
1750 case nir_op_fddy_coarse
:
1751 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
1754 fprintf(stderr
, "Unknown NIR alu instr: ");
1755 nir_print_instr(&instr
->instr
, stderr
);
1756 fprintf(stderr
, "\n");
1761 assert(instr
->dest
.dest
.is_ssa
);
1762 result
= to_integer(ctx
, result
);
1763 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.dest
.ssa
,
1768 static void visit_load_const(struct nir_to_llvm_context
*ctx
,
1769 nir_load_const_instr
*instr
)
1771 LLVMValueRef values
[4], value
= NULL
;
1772 LLVMTypeRef element_type
=
1773 LLVMIntTypeInContext(ctx
->context
, instr
->def
.bit_size
);
1775 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1776 switch (instr
->def
.bit_size
) {
1778 values
[i
] = LLVMConstInt(element_type
,
1779 instr
->value
.u32
[i
], false);
1782 values
[i
] = LLVMConstInt(element_type
,
1783 instr
->value
.u64
[i
], false);
1787 "unsupported nir load_const bit_size: %d\n",
1788 instr
->def
.bit_size
);
1792 if (instr
->def
.num_components
> 1) {
1793 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1797 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, value
);
1800 static LLVMValueRef
cast_ptr(struct nir_to_llvm_context
*ctx
, LLVMValueRef ptr
,
1803 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
1804 return LLVMBuildBitCast(ctx
->builder
, ptr
,
1805 LLVMPointerType(type
, addr_space
), "");
1809 get_buffer_size(struct nir_to_llvm_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1812 LLVMBuildExtractElement(ctx
->builder
, descriptor
,
1813 LLVMConstInt(ctx
->i32
, 2, false), "");
1816 if (ctx
->options
->chip_class
>= VI
&& in_elements
) {
1817 /* On VI, the descriptor contains the size in bytes,
1818 * but TXQ must return the size in elements.
1819 * The stride is always non-zero for resources using TXQ.
1821 LLVMValueRef stride
=
1822 LLVMBuildExtractElement(ctx
->builder
, descriptor
,
1823 LLVMConstInt(ctx
->i32
, 1, false), "");
1824 stride
= LLVMBuildLShr(ctx
->builder
, stride
,
1825 LLVMConstInt(ctx
->i32
, 16, false), "");
1826 stride
= LLVMBuildAnd(ctx
->builder
, stride
,
1827 LLVMConstInt(ctx
->i32
, 0x3fff, false), "");
1829 size
= LLVMBuildUDiv(ctx
->builder
, size
, stride
, "");
1835 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
1838 static void build_int_type_name(
1840 char *buf
, unsigned bufsize
)
1842 assert(bufsize
>= 6);
1844 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
1845 snprintf(buf
, bufsize
, "v%ui32",
1846 LLVMGetVectorSize(type
));
1851 static LLVMValueRef
radv_lower_gather4_integer(struct nir_to_llvm_context
*ctx
,
1852 struct ac_image_args
*args
,
1853 nir_tex_instr
*instr
)
1855 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
1856 LLVMValueRef coord
= args
->addr
;
1857 LLVMValueRef half_texel
[2];
1858 LLVMValueRef compare_cube_wa
;
1859 LLVMValueRef result
;
1861 unsigned coord_vgpr_index
= (unsigned)args
->offset
+ (unsigned)args
->compare
;
1865 struct ac_image_args txq_args
= { 0 };
1867 txq_args
.da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
1868 txq_args
.opcode
= ac_image_get_resinfo
;
1869 txq_args
.dmask
= 0xf;
1870 txq_args
.addr
= ctx
->i32zero
;
1871 txq_args
.resource
= args
->resource
;
1872 LLVMValueRef size
= ac_build_image_opcode(&ctx
->ac
, &txq_args
);
1874 for (c
= 0; c
< 2; c
++) {
1875 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
1876 LLVMConstInt(ctx
->i32
, c
, false), "");
1877 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1878 half_texel
[c
] = ac_build_fdiv(&ctx
->ac
, ctx
->f32one
, half_texel
[c
]);
1879 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1880 LLVMConstReal(ctx
->f32
, -0.5), "");
1884 LLVMValueRef orig_coords
= args
->addr
;
1886 for (c
= 0; c
< 2; c
++) {
1888 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, coord_vgpr_index
+ c
, 0);
1889 tmp
= LLVMBuildExtractElement(ctx
->builder
, coord
, index
, "");
1890 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1891 tmp
= LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
1892 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1893 coord
= LLVMBuildInsertElement(ctx
->builder
, coord
, tmp
, index
, "");
1898 * Apparantly cube has issue with integer types that the workaround doesn't solve,
1899 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
1900 * workaround by sampling using a scaled type and converting.
1901 * This is taken from amdgpu-pro shaders.
1903 /* NOTE this produces some ugly code compared to amdgpu-pro,
1904 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
1905 * and then reads them back. -pro generates two selects,
1906 * one s_cmp for the descriptor rewriting
1907 * one v_cmp for the coordinate and result changes.
1909 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1910 LLVMValueRef tmp
, tmp2
;
1912 /* workaround 8/8/8/8 uint/sint cube gather bug */
1913 /* first detect it then change to a scaled read and f2i */
1914 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32one
, "");
1917 /* extract the DATA_FORMAT */
1918 tmp
= ac_build_bfe(&ctx
->ac
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
1919 LLVMConstInt(ctx
->i32
, 6, false), false);
1921 /* is the DATA_FORMAT == 8_8_8_8 */
1922 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
1924 if (stype
== GLSL_TYPE_UINT
)
1925 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
1926 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
1927 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
1929 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
1930 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
1931 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
1933 /* replace the NUM FORMAT in the descriptor */
1934 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT_GFX6
, false), "");
1935 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
1937 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32one
, "");
1939 /* don't modify the coordinates for this case */
1940 coord
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, orig_coords
, coord
, "");
1943 result
= ac_build_image_opcode(&ctx
->ac
, args
);
1945 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1946 LLVMValueRef tmp
, tmp2
;
1948 /* if the cube workaround is in place, f2i the result. */
1949 for (c
= 0; c
< 4; c
++) {
1950 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
1951 if (stype
== GLSL_TYPE_UINT
)
1952 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
1954 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
1955 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1956 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
1957 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
1958 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1959 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
1965 static LLVMValueRef
build_tex_intrinsic(struct nir_to_llvm_context
*ctx
,
1966 nir_tex_instr
*instr
,
1967 struct ac_image_args
*args
)
1969 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
1970 return ac_build_buffer_load_format(&ctx
->ac
,
1973 LLVMConstInt(ctx
->i32
, 0, false),
1977 args
->opcode
= ac_image_sample
;
1978 args
->compare
= instr
->is_shadow
;
1980 switch (instr
->op
) {
1982 case nir_texop_txf_ms
:
1983 case nir_texop_samples_identical
:
1984 args
->opcode
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
? ac_image_load
: ac_image_load_mip
;
1985 args
->compare
= false;
1986 args
->offset
= false;
1995 case nir_texop_query_levels
:
1996 args
->opcode
= ac_image_get_resinfo
;
1999 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
2000 args
->level_zero
= true;
2006 args
->opcode
= ac_image_gather4
;
2007 args
->level_zero
= true;
2010 args
->opcode
= ac_image_get_lod
;
2011 args
->compare
= false;
2012 args
->offset
= false;
2018 if (instr
->op
== nir_texop_tg4
) {
2019 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
2020 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
2021 return radv_lower_gather4_integer(ctx
, args
, instr
);
2024 return ac_build_image_opcode(&ctx
->ac
, args
);
2027 static LLVMValueRef
visit_vulkan_resource_index(struct nir_to_llvm_context
*ctx
,
2028 nir_intrinsic_instr
*instr
)
2030 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
2031 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
2032 unsigned binding
= nir_intrinsic_binding(instr
);
2033 LLVMValueRef desc_ptr
= ctx
->descriptor_sets
[desc_set
];
2034 struct radv_pipeline_layout
*pipeline_layout
= ctx
->options
->layout
;
2035 struct radv_descriptor_set_layout
*layout
= pipeline_layout
->set
[desc_set
].layout
;
2036 unsigned base_offset
= layout
->binding
[binding
].offset
;
2037 LLVMValueRef offset
, stride
;
2039 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
2040 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
2041 unsigned idx
= pipeline_layout
->set
[desc_set
].dynamic_offset_start
+
2042 layout
->binding
[binding
].dynamic_offset_offset
;
2043 desc_ptr
= ctx
->push_constants
;
2044 base_offset
= pipeline_layout
->push_constant_size
+ 16 * idx
;
2045 stride
= LLVMConstInt(ctx
->i32
, 16, false);
2047 stride
= LLVMConstInt(ctx
->i32
, layout
->binding
[binding
].size
, false);
2049 offset
= LLVMConstInt(ctx
->i32
, base_offset
, false);
2050 index
= LLVMBuildMul(ctx
->builder
, index
, stride
, "");
2051 offset
= LLVMBuildAdd(ctx
->builder
, offset
, index
, "");
2053 desc_ptr
= ac_build_gep0(&ctx
->ac
, desc_ptr
, offset
);
2054 desc_ptr
= cast_ptr(ctx
, desc_ptr
, ctx
->v4i32
);
2055 LLVMSetMetadata(desc_ptr
, ctx
->uniform_md_kind
, ctx
->empty_md
);
2057 return LLVMBuildLoad(ctx
->builder
, desc_ptr
, "");
2060 static LLVMValueRef
visit_load_push_constant(struct nir_to_llvm_context
*ctx
,
2061 nir_intrinsic_instr
*instr
)
2063 LLVMValueRef ptr
, addr
;
2065 addr
= LLVMConstInt(ctx
->i32
, nir_intrinsic_base(instr
), 0);
2066 addr
= LLVMBuildAdd(ctx
->builder
, addr
, get_src(ctx
, instr
->src
[0]), "");
2068 ptr
= ac_build_gep0(&ctx
->ac
, ctx
->push_constants
, addr
);
2069 ptr
= cast_ptr(ctx
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
2071 return LLVMBuildLoad(ctx
->builder
, ptr
, "");
2074 static LLVMValueRef
visit_get_buffer_size(struct nir_to_llvm_context
*ctx
,
2075 nir_intrinsic_instr
*instr
)
2077 LLVMValueRef desc
= get_src(ctx
, instr
->src
[0]);
2079 return get_buffer_size(ctx
, desc
, false);
2081 static void visit_store_ssbo(struct nir_to_llvm_context
*ctx
,
2082 nir_intrinsic_instr
*instr
)
2084 const char *store_name
;
2085 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
2086 LLVMTypeRef data_type
= ctx
->f32
;
2087 int elem_size_mult
= get_elem_bits(ctx
, LLVMTypeOf(src_data
)) / 32;
2088 int components_32bit
= elem_size_mult
* instr
->num_components
;
2089 unsigned writemask
= nir_intrinsic_write_mask(instr
);
2090 LLVMValueRef base_data
, base_offset
;
2091 LLVMValueRef params
[6];
2093 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
2094 ctx
->shader_info
->fs
.writes_memory
= true;
2096 params
[1] = get_src(ctx
, instr
->src
[1]);
2097 params
[2] = LLVMConstInt(ctx
->i32
, 0, false); /* vindex */
2098 params
[4] = ctx
->i1false
; /* glc */
2099 params
[5] = ctx
->i1false
; /* slc */
2101 if (components_32bit
> 1)
2102 data_type
= LLVMVectorType(ctx
->f32
, components_32bit
);
2104 base_data
= to_float(ctx
, src_data
);
2105 base_data
= trim_vector(ctx
, base_data
, instr
->num_components
);
2106 base_data
= LLVMBuildBitCast(ctx
->builder
, base_data
,
2108 base_offset
= get_src(ctx
, instr
->src
[2]); /* voffset */
2112 LLVMValueRef offset
;
2114 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
2116 /* Due to an LLVM limitation, split 3-element writes
2117 * into a 2-element and a 1-element write. */
2119 writemask
|= 1 << (start
+ 2);
2123 start
*= elem_size_mult
;
2124 count
*= elem_size_mult
;
2127 writemask
|= ((1u << (count
- 4)) - 1u) << (start
+ 4);
2132 store_name
= "llvm.amdgcn.buffer.store.v4f32";
2134 } else if (count
== 2) {
2135 tmp
= LLVMBuildExtractElement(ctx
->builder
,
2136 base_data
, LLVMConstInt(ctx
->i32
, start
, false), "");
2137 data
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), tmp
,
2140 tmp
= LLVMBuildExtractElement(ctx
->builder
,
2141 base_data
, LLVMConstInt(ctx
->i32
, start
+ 1, false), "");
2142 data
= LLVMBuildInsertElement(ctx
->builder
, data
, tmp
,
2144 store_name
= "llvm.amdgcn.buffer.store.v2f32";
2148 if (get_llvm_num_components(base_data
) > 1)
2149 data
= LLVMBuildExtractElement(ctx
->builder
, base_data
,
2150 LLVMConstInt(ctx
->i32
, start
, false), "");
2153 store_name
= "llvm.amdgcn.buffer.store.f32";
2156 offset
= base_offset
;
2158 offset
= LLVMBuildAdd(ctx
->builder
, offset
, LLVMConstInt(ctx
->i32
, start
* 4, false), "");
2162 ac_build_intrinsic(&ctx
->ac
, store_name
,
2163 ctx
->voidt
, params
, 6, 0);
2167 static LLVMValueRef
visit_atomic_ssbo(struct nir_to_llvm_context
*ctx
,
2168 nir_intrinsic_instr
*instr
)
2171 LLVMValueRef params
[6];
2173 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
2174 ctx
->shader_info
->fs
.writes_memory
= true;
2176 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
2177 params
[arg_count
++] = llvm_extract_elem(ctx
, get_src(ctx
, instr
->src
[3]), 0);
2179 params
[arg_count
++] = llvm_extract_elem(ctx
, get_src(ctx
, instr
->src
[2]), 0);
2180 params
[arg_count
++] = get_src(ctx
, instr
->src
[0]);
2181 params
[arg_count
++] = LLVMConstInt(ctx
->i32
, 0, false); /* vindex */
2182 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
2183 params
[arg_count
++] = ctx
->i1false
; /* slc */
2185 switch (instr
->intrinsic
) {
2186 case nir_intrinsic_ssbo_atomic_add
:
2187 name
= "llvm.amdgcn.buffer.atomic.add";
2189 case nir_intrinsic_ssbo_atomic_imin
:
2190 name
= "llvm.amdgcn.buffer.atomic.smin";
2192 case nir_intrinsic_ssbo_atomic_umin
:
2193 name
= "llvm.amdgcn.buffer.atomic.umin";
2195 case nir_intrinsic_ssbo_atomic_imax
:
2196 name
= "llvm.amdgcn.buffer.atomic.smax";
2198 case nir_intrinsic_ssbo_atomic_umax
:
2199 name
= "llvm.amdgcn.buffer.atomic.umax";
2201 case nir_intrinsic_ssbo_atomic_and
:
2202 name
= "llvm.amdgcn.buffer.atomic.and";
2204 case nir_intrinsic_ssbo_atomic_or
:
2205 name
= "llvm.amdgcn.buffer.atomic.or";
2207 case nir_intrinsic_ssbo_atomic_xor
:
2208 name
= "llvm.amdgcn.buffer.atomic.xor";
2210 case nir_intrinsic_ssbo_atomic_exchange
:
2211 name
= "llvm.amdgcn.buffer.atomic.swap";
2213 case nir_intrinsic_ssbo_atomic_comp_swap
:
2214 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
2220 return ac_build_intrinsic(&ctx
->ac
, name
, ctx
->i32
, params
, arg_count
, 0);
2223 static LLVMValueRef
visit_load_buffer(struct nir_to_llvm_context
*ctx
,
2224 nir_intrinsic_instr
*instr
)
2226 LLVMValueRef results
[2];
2227 int load_components
;
2228 int num_components
= instr
->num_components
;
2229 if (instr
->dest
.ssa
.bit_size
== 64)
2230 num_components
*= 2;
2232 for (int i
= 0; i
< num_components
; i
+= load_components
) {
2233 load_components
= MIN2(num_components
- i
, 4);
2234 const char *load_name
;
2235 LLVMTypeRef data_type
= ctx
->f32
;
2236 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, i
* 4, false);
2237 offset
= LLVMBuildAdd(ctx
->builder
, get_src(ctx
, instr
->src
[1]), offset
, "");
2239 if (load_components
== 3)
2240 data_type
= LLVMVectorType(ctx
->f32
, 4);
2241 else if (load_components
> 1)
2242 data_type
= LLVMVectorType(ctx
->f32
, load_components
);
2244 if (load_components
>= 3)
2245 load_name
= "llvm.amdgcn.buffer.load.v4f32";
2246 else if (load_components
== 2)
2247 load_name
= "llvm.amdgcn.buffer.load.v2f32";
2248 else if (load_components
== 1)
2249 load_name
= "llvm.amdgcn.buffer.load.f32";
2251 unreachable("unhandled number of components");
2253 LLVMValueRef params
[] = {
2254 get_src(ctx
, instr
->src
[0]),
2255 LLVMConstInt(ctx
->i32
, 0, false),
2261 results
[i
] = ac_build_intrinsic(&ctx
->ac
, load_name
, data_type
, params
, 5, 0);
2265 LLVMValueRef ret
= results
[0];
2266 if (num_components
> 4 || num_components
== 3) {
2267 LLVMValueRef masks
[] = {
2268 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
2269 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false),
2270 LLVMConstInt(ctx
->i32
, 4, false), LLVMConstInt(ctx
->i32
, 5, false),
2271 LLVMConstInt(ctx
->i32
, 6, false), LLVMConstInt(ctx
->i32
, 7, false)
2274 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
2275 ret
= LLVMBuildShuffleVector(ctx
->builder
, results
[0],
2276 results
[num_components
> 4 ? 1 : 0], swizzle
, "");
2279 return LLVMBuildBitCast(ctx
->builder
, ret
,
2280 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2283 static LLVMValueRef
visit_load_ubo_buffer(struct nir_to_llvm_context
*ctx
,
2284 nir_intrinsic_instr
*instr
)
2286 LLVMValueRef results
[8], ret
;
2287 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
2288 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
2289 int num_components
= instr
->num_components
;
2291 rsrc
= LLVMBuildBitCast(ctx
->builder
, rsrc
, LLVMVectorType(ctx
->i8
, 16), "");
2293 if (instr
->dest
.ssa
.bit_size
== 64)
2294 num_components
*= 2;
2296 for (unsigned i
= 0; i
< num_components
; ++i
) {
2297 LLVMValueRef params
[] = {
2299 LLVMBuildAdd(ctx
->builder
, LLVMConstInt(ctx
->i32
, 4 * i
, 0),
2302 results
[i
] = ac_build_intrinsic(&ctx
->ac
, "llvm.SI.load.const", ctx
->f32
,
2304 AC_FUNC_ATTR_READNONE
|
2305 AC_FUNC_ATTR_LEGACY
);
2309 ret
= ac_build_gather_values(&ctx
->ac
, results
, instr
->num_components
);
2310 return LLVMBuildBitCast(ctx
->builder
, ret
,
2311 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2315 radv_get_deref_offset(struct nir_to_llvm_context
*ctx
, nir_deref_var
*deref
,
2316 bool vs_in
, unsigned *vertex_index_out
,
2317 LLVMValueRef
*vertex_index_ref
,
2318 unsigned *const_out
, LLVMValueRef
*indir_out
)
2320 unsigned const_offset
= 0;
2321 nir_deref
*tail
= &deref
->deref
;
2322 LLVMValueRef offset
= NULL
;
2324 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
2326 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2327 if (vertex_index_out
)
2328 *vertex_index_out
= deref_array
->base_offset
;
2330 if (vertex_index_ref
) {
2331 LLVMValueRef vtx
= LLVMConstInt(ctx
->i32
, deref_array
->base_offset
, false);
2332 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
2333 vtx
= LLVMBuildAdd(ctx
->builder
, vtx
, get_src(ctx
, deref_array
->indirect
), "");
2335 *vertex_index_ref
= vtx
;
2339 if (deref
->var
->data
.compact
) {
2340 assert(tail
->child
->deref_type
== nir_deref_type_array
);
2341 assert(glsl_type_is_scalar(glsl_without_array(deref
->var
->type
)));
2342 nir_deref_array
*deref_array
= nir_deref_as_array(tail
->child
);
2343 /* We always lower indirect dereferences for "compact" array vars. */
2344 assert(deref_array
->deref_array_type
== nir_deref_array_type_direct
);
2346 const_offset
= deref_array
->base_offset
;
2350 while (tail
->child
!= NULL
) {
2351 const struct glsl_type
*parent_type
= tail
->type
;
2354 if (tail
->deref_type
== nir_deref_type_array
) {
2355 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2356 LLVMValueRef index
, stride
, local_offset
;
2357 unsigned size
= glsl_count_attribute_slots(tail
->type
, vs_in
);
2359 const_offset
+= size
* deref_array
->base_offset
;
2360 if (deref_array
->deref_array_type
== nir_deref_array_type_direct
)
2363 assert(deref_array
->deref_array_type
== nir_deref_array_type_indirect
);
2364 index
= get_src(ctx
, deref_array
->indirect
);
2365 stride
= LLVMConstInt(ctx
->i32
, size
, 0);
2366 local_offset
= LLVMBuildMul(ctx
->builder
, stride
, index
, "");
2369 offset
= LLVMBuildAdd(ctx
->builder
, offset
, local_offset
, "");
2371 offset
= local_offset
;
2372 } else if (tail
->deref_type
== nir_deref_type_struct
) {
2373 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
2375 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
2376 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
2377 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
2380 unreachable("unsupported deref type");
2384 if (const_offset
&& offset
)
2385 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
2386 LLVMConstInt(ctx
->i32
, const_offset
, 0),
2389 *const_out
= const_offset
;
2390 *indir_out
= offset
;
2394 lds_load(struct nir_to_llvm_context
*ctx
,
2395 LLVMValueRef dw_addr
)
2398 value
= ac_build_indexed_load(&ctx
->ac
, ctx
->lds
, dw_addr
, false);
2403 lds_store(struct nir_to_llvm_context
*ctx
,
2404 LLVMValueRef dw_addr
, LLVMValueRef value
)
2406 value
= LLVMBuildBitCast(ctx
->builder
, value
, ctx
->i32
, "");
2407 ac_build_indexed_store(&ctx
->ac
, ctx
->lds
,
2411 /* The offchip buffer layout for TCS->TES is
2413 * - attribute 0 of patch 0 vertex 0
2414 * - attribute 0 of patch 0 vertex 1
2415 * - attribute 0 of patch 0 vertex 2
2417 * - attribute 0 of patch 1 vertex 0
2418 * - attribute 0 of patch 1 vertex 1
2420 * - attribute 1 of patch 0 vertex 0
2421 * - attribute 1 of patch 0 vertex 1
2423 * - per patch attribute 0 of patch 0
2424 * - per patch attribute 0 of patch 1
2427 * Note that every attribute has 4 components.
2429 static LLVMValueRef
get_tcs_tes_buffer_address(struct nir_to_llvm_context
*ctx
,
2430 LLVMValueRef vertex_index
,
2431 LLVMValueRef param_index
)
2433 LLVMValueRef base_addr
, vertices_per_patch
, num_patches
, total_vertices
;
2434 LLVMValueRef param_stride
, constant16
;
2435 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
2437 vertices_per_patch
= unpack_param(ctx
, ctx
->tcs_offchip_layout
, 9, 6);
2438 num_patches
= unpack_param(ctx
, ctx
->tcs_offchip_layout
, 0, 9);
2439 total_vertices
= LLVMBuildMul(ctx
->builder
, vertices_per_patch
,
2442 constant16
= LLVMConstInt(ctx
->i32
, 16, false);
2444 base_addr
= LLVMBuildMul(ctx
->builder
, rel_patch_id
,
2445 vertices_per_patch
, "");
2447 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2450 param_stride
= total_vertices
;
2452 base_addr
= rel_patch_id
;
2453 param_stride
= num_patches
;
2456 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2457 LLVMBuildMul(ctx
->builder
, param_index
,
2458 param_stride
, ""), "");
2460 base_addr
= LLVMBuildMul(ctx
->builder
, base_addr
, constant16
, "");
2462 if (!vertex_index
) {
2463 LLVMValueRef patch_data_offset
=
2464 unpack_param(ctx
, ctx
->tcs_offchip_layout
, 16, 16);
2466 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2467 patch_data_offset
, "");
2472 static LLVMValueRef
get_tcs_tes_buffer_address_params(struct nir_to_llvm_context
*ctx
,
2474 unsigned const_index
,
2476 LLVMValueRef vertex_index
,
2477 LLVMValueRef indir_index
)
2479 LLVMValueRef param_index
;
2482 param_index
= LLVMBuildAdd(ctx
->builder
, LLVMConstInt(ctx
->i32
, param
, false),
2485 if (const_index
&& !is_compact
)
2486 param
+= const_index
;
2487 param_index
= LLVMConstInt(ctx
->i32
, param
, false);
2489 return get_tcs_tes_buffer_address(ctx
, vertex_index
, param_index
);
2493 mark_tess_output(struct nir_to_llvm_context
*ctx
,
2494 bool is_patch
, uint32_t param
)
2498 ctx
->tess_patch_outputs_written
|= (1ull << param
);
2500 ctx
->tess_outputs_written
|= (1ull << param
);
2504 get_dw_address(struct nir_to_llvm_context
*ctx
,
2505 LLVMValueRef dw_addr
,
2507 unsigned const_index
,
2508 bool compact_const_index
,
2509 LLVMValueRef vertex_index
,
2510 LLVMValueRef stride
,
2511 LLVMValueRef indir_index
)
2516 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2517 LLVMBuildMul(ctx
->builder
,
2523 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2524 LLVMBuildMul(ctx
->builder
, indir_index
,
2525 LLVMConstInt(ctx
->i32
, 4, false), ""), "");
2526 else if (const_index
&& !compact_const_index
)
2527 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2528 LLVMConstInt(ctx
->i32
, const_index
, false), "");
2530 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2531 LLVMConstInt(ctx
->i32
, param
* 4, false), "");
2533 if (const_index
&& compact_const_index
)
2534 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2535 LLVMConstInt(ctx
->i32
, const_index
, false), "");
2540 load_tcs_input(struct nir_to_llvm_context
*ctx
,
2541 nir_intrinsic_instr
*instr
)
2543 LLVMValueRef dw_addr
, stride
;
2544 unsigned const_index
;
2545 LLVMValueRef vertex_index
;
2546 LLVMValueRef indir_index
;
2548 LLVMValueRef value
[4], result
;
2549 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2550 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2551 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2552 radv_get_deref_offset(ctx
, instr
->variables
[0],
2553 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2554 &const_index
, &indir_index
);
2556 stride
= unpack_param(ctx
, ctx
->tcs_in_layout
, 13, 8);
2557 dw_addr
= get_tcs_in_current_patch_offset(ctx
);
2558 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2561 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
2562 value
[i
] = lds_load(ctx
, dw_addr
);
2563 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2566 result
= ac_build_gather_values(&ctx
->ac
, value
, instr
->num_components
);
2567 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2572 load_tcs_output(struct nir_to_llvm_context
*ctx
,
2573 nir_intrinsic_instr
*instr
)
2575 LLVMValueRef dw_addr
, stride
;
2576 LLVMValueRef value
[4], result
;
2577 LLVMValueRef vertex_index
= NULL
;
2578 LLVMValueRef indir_index
= NULL
;
2579 unsigned const_index
= 0;
2581 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2582 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2583 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2584 radv_get_deref_offset(ctx
, instr
->variables
[0],
2585 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2586 &const_index
, &indir_index
);
2588 if (!instr
->variables
[0]->var
->data
.patch
) {
2589 stride
= unpack_param(ctx
, ctx
->tcs_out_layout
, 13, 8);
2590 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
2592 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
2595 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2598 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
2599 value
[i
] = lds_load(ctx
, dw_addr
);
2600 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2603 result
= ac_build_gather_values(&ctx
->ac
, value
, instr
->num_components
);
2604 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2609 store_tcs_output(struct nir_to_llvm_context
*ctx
,
2610 nir_intrinsic_instr
*instr
,
2614 LLVMValueRef stride
, dw_addr
;
2615 LLVMValueRef buf_addr
= NULL
;
2616 LLVMValueRef vertex_index
= NULL
;
2617 LLVMValueRef indir_index
= NULL
;
2618 unsigned const_index
= 0;
2620 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2621 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2623 radv_get_deref_offset(ctx
, instr
->variables
[0],
2624 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2625 &const_index
, &indir_index
);
2627 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2628 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CLIP_DIST0
&&
2629 is_compact
&& const_index
> 3) {
2634 if (!instr
->variables
[0]->var
->data
.patch
) {
2635 stride
= unpack_param(ctx
, ctx
->tcs_out_layout
, 13, 8);
2636 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
2638 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
2641 mark_tess_output(ctx
, instr
->variables
[0]->var
->data
.patch
, param
);
2643 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2645 buf_addr
= get_tcs_tes_buffer_address_params(ctx
, param
, const_index
, is_compact
,
2646 vertex_index
, indir_index
);
2648 unsigned base
= is_compact
? const_index
: 0;
2649 for (unsigned chan
= 0; chan
< 8; chan
++) {
2650 bool is_tess_factor
= false;
2651 if (!(writemask
& (1 << chan
)))
2653 LLVMValueRef value
= llvm_extract_elem(ctx
, src
, chan
);
2655 lds_store(ctx
, dw_addr
, value
);
2657 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_TESS_LEVEL_INNER
||
2658 instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_TESS_LEVEL_OUTER
)
2659 is_tess_factor
= true;
2661 if (!is_tess_factor
&& writemask
!= 0xF)
2662 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, value
, 1,
2663 buf_addr
, ctx
->oc_lds
,
2664 4 * (base
+ chan
), 1, 0, true, false);
2666 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2670 if (writemask
== 0xF) {
2671 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, src
, 4,
2672 buf_addr
, ctx
->oc_lds
,
2673 (base
* 4), 1, 0, true, false);
2678 load_tes_input(struct nir_to_llvm_context
*ctx
,
2679 nir_intrinsic_instr
*instr
)
2681 LLVMValueRef buf_addr
;
2682 LLVMValueRef result
;
2683 LLVMValueRef vertex_index
= NULL
;
2684 LLVMValueRef indir_index
= NULL
;
2685 unsigned const_index
= 0;
2687 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2688 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2690 radv_get_deref_offset(ctx
, instr
->variables
[0],
2691 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2692 &const_index
, &indir_index
);
2693 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2694 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CLIP_DIST0
&&
2695 is_compact
&& const_index
> 3) {
2699 buf_addr
= get_tcs_tes_buffer_address_params(ctx
, param
, const_index
,
2700 is_compact
, vertex_index
, indir_index
);
2702 result
= ac_build_buffer_load(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, instr
->num_components
, NULL
,
2703 buf_addr
, ctx
->oc_lds
, is_compact
? (4 * const_index
) : 0, 1, 0, true);
2704 result
= trim_vector(ctx
, result
, instr
->num_components
);
2705 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2710 load_gs_input(struct nir_to_llvm_context
*ctx
,
2711 nir_intrinsic_instr
*instr
)
2713 LLVMValueRef indir_index
, vtx_offset
;
2714 unsigned const_index
;
2715 LLVMValueRef args
[9];
2716 unsigned param
, vtx_offset_param
;
2717 LLVMValueRef value
[4], result
;
2718 unsigned vertex_index
;
2719 radv_get_deref_offset(ctx
, instr
->variables
[0],
2720 false, &vertex_index
, NULL
,
2721 &const_index
, &indir_index
);
2722 vtx_offset_param
= vertex_index
;
2723 assert(vtx_offset_param
< 6);
2724 vtx_offset
= LLVMBuildMul(ctx
->builder
, ctx
->gs_vtx_offset
[vtx_offset_param
],
2725 LLVMConstInt(ctx
->i32
, 4, false), "");
2727 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2728 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
2730 args
[0] = ctx
->esgs_ring
;
2731 args
[1] = vtx_offset
;
2732 args
[2] = LLVMConstInt(ctx
->i32
, (param
* 4 + i
+ const_index
) * 256, false);
2733 args
[3] = ctx
->i32zero
;
2734 args
[4] = ctx
->i32one
; /* OFFEN */
2735 args
[5] = ctx
->i32zero
; /* IDXEN */
2736 args
[6] = ctx
->i32one
; /* GLC */
2737 args
[7] = ctx
->i32zero
; /* SLC */
2738 args
[8] = ctx
->i32zero
; /* TFE */
2740 value
[i
] = ac_build_intrinsic(&ctx
->ac
, "llvm.SI.buffer.load.dword.i32.i32",
2742 AC_FUNC_ATTR_READONLY
|
2743 AC_FUNC_ATTR_LEGACY
);
2745 result
= ac_build_gather_values(&ctx
->ac
, value
, instr
->num_components
);
2750 static LLVMValueRef
visit_load_var(struct nir_to_llvm_context
*ctx
,
2751 nir_intrinsic_instr
*instr
)
2753 LLVMValueRef values
[8];
2754 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2755 int ve
= instr
->dest
.ssa
.num_components
;
2756 LLVMValueRef indir_index
;
2758 unsigned const_index
;
2759 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
2760 instr
->variables
[0]->var
->data
.mode
== nir_var_shader_in
;
2761 radv_get_deref_offset(ctx
, instr
->variables
[0], vs_in
, NULL
, NULL
,
2762 &const_index
, &indir_index
);
2764 if (instr
->dest
.ssa
.bit_size
== 64)
2767 switch (instr
->variables
[0]->var
->data
.mode
) {
2768 case nir_var_shader_in
:
2769 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
2770 return load_tcs_input(ctx
, instr
);
2771 if (ctx
->stage
== MESA_SHADER_TESS_EVAL
)
2772 return load_tes_input(ctx
, instr
);
2773 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
2774 return load_gs_input(ctx
, instr
);
2776 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2778 unsigned count
= glsl_count_attribute_slots(
2779 instr
->variables
[0]->var
->type
,
2780 ctx
->stage
== MESA_SHADER_VERTEX
);
2782 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2783 &ctx
->ac
, ctx
->inputs
+ idx
+ chan
, count
,
2786 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2790 values
[chan
] = ctx
->inputs
[idx
+ chan
+ const_index
* 4];
2794 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2796 unsigned count
= glsl_count_attribute_slots(
2797 instr
->variables
[0]->var
->type
, false);
2799 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2800 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2803 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2807 values
[chan
] = LLVMBuildLoad(ctx
->builder
, ctx
->locals
[idx
+ chan
+ const_index
* 4], "");
2811 case nir_var_shader_out
:
2812 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
2813 return load_tcs_output(ctx
, instr
);
2814 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2816 unsigned count
= glsl_count_attribute_slots(
2817 instr
->variables
[0]->var
->type
, false);
2819 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2820 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
2823 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2827 values
[chan
] = LLVMBuildLoad(ctx
->builder
,
2828 ctx
->outputs
[idx
+ chan
+ const_index
* 4],
2833 case nir_var_shared
: {
2834 LLVMValueRef ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
2835 LLVMValueRef derived_ptr
;
2838 indir_index
= LLVMBuildMul(ctx
->builder
, indir_index
, LLVMConstInt(ctx
->i32
, 4, false), "");
2840 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2841 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, chan
, false);
2843 index
= LLVMBuildAdd(ctx
->builder
, index
, indir_index
, "");
2844 derived_ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, "");
2846 values
[chan
] = LLVMBuildLoad(ctx
->builder
, derived_ptr
, "");
2851 unreachable("unhandle variable mode");
2853 ret
= ac_build_gather_values(&ctx
->ac
, values
, ve
);
2854 return LLVMBuildBitCast(ctx
->builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2858 visit_store_var(struct nir_to_llvm_context
*ctx
,
2859 nir_intrinsic_instr
*instr
)
2861 LLVMValueRef temp_ptr
, value
;
2862 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2863 LLVMValueRef src
= to_float(ctx
, get_src(ctx
, instr
->src
[0]));
2864 int writemask
= instr
->const_index
[0];
2865 LLVMValueRef indir_index
;
2866 unsigned const_index
;
2867 radv_get_deref_offset(ctx
, instr
->variables
[0], false,
2868 NULL
, NULL
, &const_index
, &indir_index
);
2870 if (get_elem_bits(ctx
, LLVMTypeOf(src
)) == 64) {
2871 int old_writemask
= writemask
;
2873 src
= LLVMBuildBitCast(ctx
->builder
, src
,
2874 LLVMVectorType(ctx
->f32
, get_llvm_num_components(src
) * 2),
2878 for (unsigned chan
= 0; chan
< 4; chan
++) {
2879 if (old_writemask
& (1 << chan
))
2880 writemask
|= 3u << (2 * chan
);
2884 switch (instr
->variables
[0]->var
->data
.mode
) {
2885 case nir_var_shader_out
:
2887 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2888 store_tcs_output(ctx
, instr
, src
, writemask
);
2892 for (unsigned chan
= 0; chan
< 8; chan
++) {
2894 if (!(writemask
& (1 << chan
)))
2897 value
= llvm_extract_elem(ctx
, src
, chan
);
2899 if (instr
->variables
[0]->var
->data
.compact
)
2902 unsigned count
= glsl_count_attribute_slots(
2903 instr
->variables
[0]->var
->type
, false);
2905 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2906 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
2909 if (get_llvm_num_components(tmp_vec
) > 1) {
2910 tmp_vec
= LLVMBuildInsertElement(ctx
->builder
, tmp_vec
,
2911 value
, indir_index
, "");
2914 build_store_values_extended(ctx
, ctx
->outputs
+ idx
+ chan
,
2915 count
, stride
, tmp_vec
);
2918 temp_ptr
= ctx
->outputs
[idx
+ chan
+ const_index
* stride
];
2920 LLVMBuildStore(ctx
->builder
, value
, temp_ptr
);
2925 for (unsigned chan
= 0; chan
< 8; chan
++) {
2926 if (!(writemask
& (1 << chan
)))
2929 value
= llvm_extract_elem(ctx
, src
, chan
);
2931 unsigned count
= glsl_count_attribute_slots(
2932 instr
->variables
[0]->var
->type
, false);
2934 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2935 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2938 tmp_vec
= LLVMBuildInsertElement(ctx
->builder
, tmp_vec
,
2939 value
, indir_index
, "");
2940 build_store_values_extended(ctx
, ctx
->locals
+ idx
+ chan
,
2943 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
2945 LLVMBuildStore(ctx
->builder
, value
, temp_ptr
);
2949 case nir_var_shared
: {
2950 LLVMValueRef ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
2953 indir_index
= LLVMBuildMul(ctx
->builder
, indir_index
, LLVMConstInt(ctx
->i32
, 4, false), "");
2955 for (unsigned chan
= 0; chan
< 8; chan
++) {
2956 if (!(writemask
& (1 << chan
)))
2958 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, chan
, false);
2959 LLVMValueRef derived_ptr
;
2962 index
= LLVMBuildAdd(ctx
->builder
, index
, indir_index
, "");
2964 value
= llvm_extract_elem(ctx
, src
, chan
);
2965 derived_ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, "");
2966 LLVMBuildStore(ctx
->builder
,
2967 to_integer(ctx
, value
), derived_ptr
);
2976 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
2979 case GLSL_SAMPLER_DIM_BUF
:
2981 case GLSL_SAMPLER_DIM_1D
:
2982 return array
? 2 : 1;
2983 case GLSL_SAMPLER_DIM_2D
:
2984 return array
? 3 : 2;
2985 case GLSL_SAMPLER_DIM_MS
:
2986 return array
? 4 : 3;
2987 case GLSL_SAMPLER_DIM_3D
:
2988 case GLSL_SAMPLER_DIM_CUBE
:
2990 case GLSL_SAMPLER_DIM_RECT
:
2991 case GLSL_SAMPLER_DIM_SUBPASS
:
2993 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
3003 /* Adjust the sample index according to FMASK.
3005 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3006 * which is the identity mapping. Each nibble says which physical sample
3007 * should be fetched to get that sample.
3009 * For example, 0x11111100 means there are only 2 samples stored and
3010 * the second sample covers 3/4 of the pixel. When reading samples 0
3011 * and 1, return physical sample 0 (determined by the first two 0s
3012 * in FMASK), otherwise return physical sample 1.
3014 * The sample index should be adjusted as follows:
3015 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
3017 static LLVMValueRef
adjust_sample_index_using_fmask(struct nir_to_llvm_context
*ctx
,
3018 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
3019 LLVMValueRef coord_z
,
3020 LLVMValueRef sample_index
,
3021 LLVMValueRef fmask_desc_ptr
)
3023 LLVMValueRef fmask_load_address
[4];
3026 fmask_load_address
[0] = coord_x
;
3027 fmask_load_address
[1] = coord_y
;
3029 fmask_load_address
[2] = coord_z
;
3030 fmask_load_address
[3] = LLVMGetUndef(ctx
->i32
);
3033 struct ac_image_args args
= {0};
3035 args
.opcode
= ac_image_load
;
3036 args
.da
= coord_z
? true : false;
3037 args
.resource
= fmask_desc_ptr
;
3039 args
.addr
= ac_build_gather_values(&ctx
->ac
, fmask_load_address
, coord_z
? 4 : 2);
3041 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
3043 res
= to_integer(ctx
, res
);
3044 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
3045 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
3047 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
3051 LLVMValueRef sample_index4
=
3052 LLVMBuildMul(ctx
->builder
, sample_index
, four
, "");
3053 LLVMValueRef shifted_fmask
=
3054 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
3055 LLVMValueRef final_sample
=
3056 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
3058 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3059 * resource descriptor is 0 (invalid),
3061 LLVMValueRef fmask_desc
=
3062 LLVMBuildBitCast(ctx
->builder
, fmask_desc_ptr
,
3065 LLVMValueRef fmask_word1
=
3066 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
3069 LLVMValueRef word1_is_nonzero
=
3070 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3071 fmask_word1
, ctx
->i32zero
, "");
3073 /* Replace the MSAA sample index. */
3075 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
3076 final_sample
, sample_index
, "");
3077 return sample_index
;
3080 static LLVMValueRef
get_image_coords(struct nir_to_llvm_context
*ctx
,
3081 nir_intrinsic_instr
*instr
)
3083 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
3084 if(instr
->variables
[0]->deref
.child
)
3085 type
= instr
->variables
[0]->deref
.child
->type
;
3087 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
3088 LLVMValueRef coords
[4];
3089 LLVMValueRef masks
[] = {
3090 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
3091 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false),
3094 LLVMValueRef sample_index
= llvm_extract_elem(ctx
, get_src(ctx
, instr
->src
[1]), 0);
3097 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
3098 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3099 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3100 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
3101 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3103 count
= image_type_to_components_count(dim
,
3104 glsl_sampler_type_is_array(type
));
3107 LLVMValueRef fmask_load_address
[3];
3110 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[0], "");
3111 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[1], "");
3112 if (glsl_sampler_type_is_array(type
))
3113 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[2], "");
3115 fmask_load_address
[2] = NULL
;
3117 for (chan
= 0; chan
< 2; ++chan
)
3118 fmask_load_address
[chan
] = LLVMBuildAdd(ctx
->builder
, fmask_load_address
[chan
], LLVMBuildFPToUI(ctx
->builder
, ctx
->frag_pos
[chan
], ctx
->i32
, ""), "");
3120 sample_index
= adjust_sample_index_using_fmask(ctx
,
3121 fmask_load_address
[0],
3122 fmask_load_address
[1],
3123 fmask_load_address
[2],
3125 get_sampler_desc(ctx
, instr
->variables
[0], DESC_FMASK
));
3128 if (instr
->src
[0].ssa
->num_components
)
3129 res
= LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[0], "");
3136 for (chan
= 0; chan
< count
; ++chan
) {
3137 coords
[chan
] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[chan
], "");
3141 for (chan
= 0; chan
< count
; ++chan
)
3142 coords
[chan
] = LLVMBuildAdd(ctx
->builder
, coords
[chan
], LLVMBuildFPToUI(ctx
->builder
, ctx
->frag_pos
[chan
], ctx
->i32
, ""), "");
3145 coords
[count
] = sample_index
;
3150 coords
[3] = LLVMGetUndef(ctx
->i32
);
3153 res
= ac_build_gather_values(&ctx
->ac
, coords
, count
);
3158 static LLVMValueRef
visit_image_load(struct nir_to_llvm_context
*ctx
,
3159 nir_intrinsic_instr
*instr
)
3161 LLVMValueRef params
[7];
3163 char intrinsic_name
[64];
3164 const nir_variable
*var
= instr
->variables
[0]->var
;
3165 const struct glsl_type
*type
= var
->type
;
3166 if(instr
->variables
[0]->deref
.child
)
3167 type
= instr
->variables
[0]->deref
.child
->type
;
3169 type
= glsl_without_array(type
);
3170 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3171 params
[0] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
3172 params
[1] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
3173 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
3174 params
[2] = LLVMConstInt(ctx
->i32
, 0, false); /* voffset */
3175 params
[3] = ctx
->i1false
; /* glc */
3176 params
[4] = ctx
->i1false
; /* slc */
3177 res
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.load.format.v4f32", ctx
->v4f32
,
3180 res
= trim_vector(ctx
, res
, instr
->dest
.ssa
.num_components
);
3181 res
= to_integer(ctx
, res
);
3183 bool is_da
= glsl_sampler_type_is_array(type
) ||
3184 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3185 LLVMValueRef da
= is_da
? ctx
->i1true
: ctx
->i1false
;
3186 LLVMValueRef glc
= ctx
->i1false
;
3187 LLVMValueRef slc
= ctx
->i1false
;
3189 params
[0] = get_image_coords(ctx
, instr
);
3190 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
3191 params
[2] = LLVMConstInt(ctx
->i32
, 15, false); /* dmask */
3192 if (HAVE_LLVM
<= 0x0309) {
3193 params
[3] = ctx
->i1false
; /* r128 */
3198 LLVMValueRef lwe
= ctx
->i1false
;
3205 ac_get_image_intr_name("llvm.amdgcn.image.load",
3206 ctx
->v4f32
, /* vdata */
3207 LLVMTypeOf(params
[0]), /* coords */
3208 LLVMTypeOf(params
[1]), /* rsrc */
3209 intrinsic_name
, sizeof(intrinsic_name
));
3211 res
= ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->v4f32
,
3212 params
, 7, AC_FUNC_ATTR_READONLY
);
3214 return to_integer(ctx
, res
);
3217 static void visit_image_store(struct nir_to_llvm_context
*ctx
,
3218 nir_intrinsic_instr
*instr
)
3220 LLVMValueRef params
[8];
3221 char intrinsic_name
[64];
3222 const nir_variable
*var
= instr
->variables
[0]->var
;
3223 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3225 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
3226 ctx
->shader_info
->fs
.writes_memory
= true;
3228 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3229 params
[0] = to_float(ctx
, get_src(ctx
, instr
->src
[2])); /* data */
3230 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
3231 params
[2] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
3232 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
3233 params
[3] = LLVMConstInt(ctx
->i32
, 0, false); /* voffset */
3234 params
[4] = ctx
->i1false
; /* glc */
3235 params
[5] = ctx
->i1false
; /* slc */
3236 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->voidt
,
3239 bool is_da
= glsl_sampler_type_is_array(type
) ||
3240 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3241 LLVMValueRef da
= is_da
? ctx
->i1true
: ctx
->i1false
;
3242 LLVMValueRef glc
= ctx
->i1false
;
3243 LLVMValueRef slc
= ctx
->i1false
;
3245 params
[0] = to_float(ctx
, get_src(ctx
, instr
->src
[2]));
3246 params
[1] = get_image_coords(ctx
, instr
); /* coords */
3247 params
[2] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
3248 params
[3] = LLVMConstInt(ctx
->i32
, 15, false); /* dmask */
3249 if (HAVE_LLVM
<= 0x0309) {
3250 params
[4] = ctx
->i1false
; /* r128 */
3255 LLVMValueRef lwe
= ctx
->i1false
;
3262 ac_get_image_intr_name("llvm.amdgcn.image.store",
3263 LLVMTypeOf(params
[0]), /* vdata */
3264 LLVMTypeOf(params
[1]), /* coords */
3265 LLVMTypeOf(params
[2]), /* rsrc */
3266 intrinsic_name
, sizeof(intrinsic_name
));
3268 ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->voidt
,
3274 static LLVMValueRef
visit_image_atomic(struct nir_to_llvm_context
*ctx
,
3275 nir_intrinsic_instr
*instr
)
3277 LLVMValueRef params
[6];
3278 int param_count
= 0;
3279 const nir_variable
*var
= instr
->variables
[0]->var
;
3281 const char *base_name
= "llvm.amdgcn.image.atomic";
3282 const char *atomic_name
;
3283 LLVMValueRef coords
;
3284 char intrinsic_name
[32], coords_type
[8];
3285 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3287 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
3288 ctx
->shader_info
->fs
.writes_memory
= true;
3290 params
[param_count
++] = get_src(ctx
, instr
->src
[2]);
3291 if (instr
->intrinsic
== nir_intrinsic_image_atomic_comp_swap
)
3292 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
3294 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3295 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
3296 coords
= params
[param_count
++] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
3297 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
3298 params
[param_count
++] = ctx
->i32zero
; /* voffset */
3299 params
[param_count
++] = ctx
->i1false
; /* glc */
3300 params
[param_count
++] = ctx
->i1false
; /* slc */
3302 bool da
= glsl_sampler_type_is_array(type
) ||
3303 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3305 coords
= params
[param_count
++] = get_image_coords(ctx
, instr
);
3306 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
3307 params
[param_count
++] = ctx
->i1false
; /* r128 */
3308 params
[param_count
++] = da
? ctx
->i1true
: ctx
->i1false
; /* da */
3309 params
[param_count
++] = ctx
->i1false
; /* slc */
3312 switch (instr
->intrinsic
) {
3313 case nir_intrinsic_image_atomic_add
:
3314 atomic_name
= "add";
3316 case nir_intrinsic_image_atomic_min
:
3317 atomic_name
= "smin";
3319 case nir_intrinsic_image_atomic_max
:
3320 atomic_name
= "smax";
3322 case nir_intrinsic_image_atomic_and
:
3323 atomic_name
= "and";
3325 case nir_intrinsic_image_atomic_or
:
3328 case nir_intrinsic_image_atomic_xor
:
3329 atomic_name
= "xor";
3331 case nir_intrinsic_image_atomic_exchange
:
3332 atomic_name
= "swap";
3334 case nir_intrinsic_image_atomic_comp_swap
:
3335 atomic_name
= "cmpswap";
3340 build_int_type_name(LLVMTypeOf(coords
),
3341 coords_type
, sizeof(coords_type
));
3343 snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3344 "%s.%s.%s", base_name
, atomic_name
, coords_type
);
3345 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->i32
, params
, param_count
, 0);
3348 static LLVMValueRef
visit_image_size(struct nir_to_llvm_context
*ctx
,
3349 nir_intrinsic_instr
*instr
)
3352 const nir_variable
*var
= instr
->variables
[0]->var
;
3353 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
3354 bool da
= glsl_sampler_type_is_array(var
->type
) ||
3355 glsl_get_sampler_dim(var
->type
) == GLSL_SAMPLER_DIM_CUBE
;
3356 if(instr
->variables
[0]->deref
.child
)
3357 type
= instr
->variables
[0]->deref
.child
->type
;
3359 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
3360 return get_buffer_size(ctx
, get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
), true);
3362 struct ac_image_args args
= { 0 };
3366 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
3367 args
.opcode
= ac_image_get_resinfo
;
3368 args
.addr
= ctx
->i32zero
;
3370 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
3372 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
3373 glsl_sampler_type_is_array(type
)) {
3374 LLVMValueRef two
= LLVMConstInt(ctx
->i32
, 2, false);
3375 LLVMValueRef six
= LLVMConstInt(ctx
->i32
, 6, false);
3376 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->builder
, res
, two
, "");
3377 z
= LLVMBuildSDiv(ctx
->builder
, z
, six
, "");
3378 res
= LLVMBuildInsertElement(ctx
->builder
, res
, z
, two
, "");
3383 #define NOOP_WAITCNT 0xf7f
3384 #define LGKM_CNT 0x07f
3385 #define VM_CNT 0xf70
3387 static void emit_waitcnt(struct nir_to_llvm_context
*ctx
,
3390 LLVMValueRef args
[1] = {
3391 LLVMConstInt(ctx
->i32
, simm16
, false),
3393 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.s.waitcnt",
3394 ctx
->voidt
, args
, 1, 0);
3397 static void emit_barrier(struct nir_to_llvm_context
*ctx
)
3399 /* SI only (thanks to a hw bug workaround):
3400 * The real barrier instruction isn’t needed, because an entire patch
3401 * always fits into a single wave.
3403 if (ctx
->options
->chip_class
== SI
&&
3404 ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3405 emit_waitcnt(ctx
, LGKM_CNT
& VM_CNT
);
3408 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.s.barrier",
3409 ctx
->voidt
, NULL
, 0, AC_FUNC_ATTR_CONVERGENT
);
3412 static void emit_discard_if(struct nir_to_llvm_context
*ctx
,
3413 nir_intrinsic_instr
*instr
)
3416 ctx
->shader_info
->fs
.can_discard
= true;
3418 cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3419 get_src(ctx
, instr
->src
[0]),
3422 cond
= LLVMBuildSelect(ctx
->builder
, cond
,
3423 LLVMConstReal(ctx
->f32
, -1.0f
),
3425 ac_build_kill(&ctx
->ac
, cond
);
3429 visit_load_local_invocation_index(struct nir_to_llvm_context
*ctx
)
3431 LLVMValueRef result
;
3432 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
3433 result
= LLVMBuildAnd(ctx
->builder
, ctx
->tg_size
,
3434 LLVMConstInt(ctx
->i32
, 0xfc0, false), "");
3436 return LLVMBuildAdd(ctx
->builder
, result
, thread_id
, "");
3439 static LLVMValueRef
visit_var_atomic(struct nir_to_llvm_context
*ctx
,
3440 nir_intrinsic_instr
*instr
)
3442 LLVMValueRef ptr
, result
;
3443 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
3444 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
3445 ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
3447 if (instr
->intrinsic
== nir_intrinsic_var_atomic_comp_swap
) {
3448 LLVMValueRef src1
= get_src(ctx
, instr
->src
[1]);
3449 result
= LLVMBuildAtomicCmpXchg(ctx
->builder
,
3451 LLVMAtomicOrderingSequentiallyConsistent
,
3452 LLVMAtomicOrderingSequentiallyConsistent
,
3455 LLVMAtomicRMWBinOp op
;
3456 switch (instr
->intrinsic
) {
3457 case nir_intrinsic_var_atomic_add
:
3458 op
= LLVMAtomicRMWBinOpAdd
;
3460 case nir_intrinsic_var_atomic_umin
:
3461 op
= LLVMAtomicRMWBinOpUMin
;
3463 case nir_intrinsic_var_atomic_umax
:
3464 op
= LLVMAtomicRMWBinOpUMax
;
3466 case nir_intrinsic_var_atomic_imin
:
3467 op
= LLVMAtomicRMWBinOpMin
;
3469 case nir_intrinsic_var_atomic_imax
:
3470 op
= LLVMAtomicRMWBinOpMax
;
3472 case nir_intrinsic_var_atomic_and
:
3473 op
= LLVMAtomicRMWBinOpAnd
;
3475 case nir_intrinsic_var_atomic_or
:
3476 op
= LLVMAtomicRMWBinOpOr
;
3478 case nir_intrinsic_var_atomic_xor
:
3479 op
= LLVMAtomicRMWBinOpXor
;
3481 case nir_intrinsic_var_atomic_exchange
:
3482 op
= LLVMAtomicRMWBinOpXchg
;
3488 result
= LLVMBuildAtomicRMW(ctx
->builder
, op
, ptr
, to_integer(ctx
, src
),
3489 LLVMAtomicOrderingSequentiallyConsistent
,
3495 #define INTERP_CENTER 0
3496 #define INTERP_CENTROID 1
3497 #define INTERP_SAMPLE 2
3499 static LLVMValueRef
lookup_interp_param(struct nir_to_llvm_context
*ctx
,
3500 enum glsl_interp_mode interp
, unsigned location
)
3503 case INTERP_MODE_FLAT
:
3506 case INTERP_MODE_SMOOTH
:
3507 case INTERP_MODE_NONE
:
3508 if (location
== INTERP_CENTER
)
3509 return ctx
->persp_center
;
3510 else if (location
== INTERP_CENTROID
)
3511 return ctx
->persp_centroid
;
3512 else if (location
== INTERP_SAMPLE
)
3513 return ctx
->persp_sample
;
3515 case INTERP_MODE_NOPERSPECTIVE
:
3516 if (location
== INTERP_CENTER
)
3517 return ctx
->linear_center
;
3518 else if (location
== INTERP_CENTROID
)
3519 return ctx
->linear_centroid
;
3520 else if (location
== INTERP_SAMPLE
)
3521 return ctx
->linear_sample
;
3527 static LLVMValueRef
load_sample_position(struct nir_to_llvm_context
*ctx
,
3528 LLVMValueRef sample_id
)
3530 LLVMValueRef result
;
3531 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_PS_SAMPLE_POSITIONS
, false));
3533 ptr
= LLVMBuildBitCast(ctx
->builder
, ptr
,
3534 const_array(ctx
->v2f32
, 64), "");
3536 sample_id
= LLVMBuildAdd(ctx
->builder
, sample_id
, ctx
->sample_pos_offset
, "");
3537 result
= ac_build_indexed_load(&ctx
->ac
, ptr
, sample_id
, false);
3542 static LLVMValueRef
load_sample_pos(struct nir_to_llvm_context
*ctx
)
3544 LLVMValueRef values
[2];
3546 values
[0] = emit_ffract(ctx
, ctx
->frag_pos
[0]);
3547 values
[1] = emit_ffract(ctx
, ctx
->frag_pos
[1]);
3548 return ac_build_gather_values(&ctx
->ac
, values
, 2);
3551 static LLVMValueRef
visit_interp(struct nir_to_llvm_context
*ctx
,
3552 nir_intrinsic_instr
*instr
)
3554 LLVMValueRef result
[2];
3555 LLVMValueRef interp_param
, attr_number
;
3558 LLVMValueRef src_c0
, src_c1
;
3560 int input_index
= instr
->variables
[0]->var
->data
.location
- VARYING_SLOT_VAR0
;
3561 switch (instr
->intrinsic
) {
3562 case nir_intrinsic_interp_var_at_centroid
:
3563 location
= INTERP_CENTROID
;
3565 case nir_intrinsic_interp_var_at_sample
:
3566 case nir_intrinsic_interp_var_at_offset
:
3567 location
= INTERP_CENTER
;
3568 src0
= get_src(ctx
, instr
->src
[0]);
3574 if (instr
->intrinsic
== nir_intrinsic_interp_var_at_offset
) {
3575 src_c0
= to_float(ctx
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32zero
, ""));
3576 src_c1
= to_float(ctx
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32one
, ""));
3577 } else if (instr
->intrinsic
== nir_intrinsic_interp_var_at_sample
) {
3578 LLVMValueRef sample_position
;
3579 LLVMValueRef halfval
= LLVMConstReal(ctx
->f32
, 0.5f
);
3581 /* fetch sample ID */
3582 sample_position
= load_sample_position(ctx
, src0
);
3584 src_c0
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->i32zero
, "");
3585 src_c0
= LLVMBuildFSub(ctx
->builder
, src_c0
, halfval
, "");
3586 src_c1
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->i32one
, "");
3587 src_c1
= LLVMBuildFSub(ctx
->builder
, src_c1
, halfval
, "");
3589 interp_param
= lookup_interp_param(ctx
, instr
->variables
[0]->var
->data
.interpolation
, location
);
3590 attr_number
= LLVMConstInt(ctx
->i32
, input_index
, false);
3592 if (location
== INTERP_SAMPLE
|| location
== INTERP_CENTER
) {
3593 LLVMValueRef ij_out
[2];
3594 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
, interp_param
);
3597 * take the I then J parameters, and the DDX/Y for it, and
3598 * calculate the IJ inputs for the interpolator.
3599 * temp1 = ddx * offset/sample.x + I;
3600 * interp_param.I = ddy * offset/sample.y + temp1;
3601 * temp1 = ddx * offset/sample.x + J;
3602 * interp_param.J = ddy * offset/sample.y + temp1;
3604 for (unsigned i
= 0; i
< 2; i
++) {
3605 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->i32
, i
, false);
3606 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->i32
, i
+ 2, false);
3607 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->builder
,
3608 ddxy_out
, ix_ll
, "");
3609 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->builder
,
3610 ddxy_out
, iy_ll
, "");
3611 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->builder
,
3612 interp_param
, ix_ll
, "");
3613 LLVMValueRef temp1
, temp2
;
3615 interp_el
= LLVMBuildBitCast(ctx
->builder
, interp_el
,
3618 temp1
= LLVMBuildFMul(ctx
->builder
, ddx_el
, src_c0
, "");
3619 temp1
= LLVMBuildFAdd(ctx
->builder
, temp1
, interp_el
, "");
3621 temp2
= LLVMBuildFMul(ctx
->builder
, ddy_el
, src_c1
, "");
3622 temp2
= LLVMBuildFAdd(ctx
->builder
, temp2
, temp1
, "");
3624 ij_out
[i
] = LLVMBuildBitCast(ctx
->builder
,
3625 temp2
, ctx
->i32
, "");
3627 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
3631 for (chan
= 0; chan
< 2; chan
++) {
3632 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
3635 interp_param
= LLVMBuildBitCast(ctx
->builder
,
3636 interp_param
, LLVMVectorType(ctx
->f32
, 2), "");
3637 LLVMValueRef i
= LLVMBuildExtractElement(
3638 ctx
->builder
, interp_param
, ctx
->i32zero
, "");
3639 LLVMValueRef j
= LLVMBuildExtractElement(
3640 ctx
->builder
, interp_param
, ctx
->i32one
, "");
3642 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
3643 llvm_chan
, attr_number
,
3644 ctx
->prim_mask
, i
, j
);
3646 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
3647 LLVMConstInt(ctx
->i32
, 2, false),
3648 llvm_chan
, attr_number
,
3652 return ac_build_gather_values(&ctx
->ac
, result
, 2);
3656 visit_emit_vertex(struct nir_to_llvm_context
*ctx
,
3657 nir_intrinsic_instr
*instr
)
3659 LLVMValueRef gs_next_vertex
;
3660 LLVMValueRef can_emit
, kill
;
3663 assert(instr
->const_index
[0] == 0);
3664 /* Write vertex attribute values to GSVS ring */
3665 gs_next_vertex
= LLVMBuildLoad(ctx
->builder
,
3666 ctx
->gs_next_vertex
,
3669 /* If this thread has already emitted the declared maximum number of
3670 * vertices, kill it: excessive vertex emissions are not supposed to
3671 * have any effect, and GS threads have no externally observable
3672 * effects other than emitting vertices.
3674 can_emit
= LLVMBuildICmp(ctx
->builder
, LLVMIntULT
, gs_next_vertex
,
3675 LLVMConstInt(ctx
->i32
, ctx
->gs_max_out_vertices
, false), "");
3677 kill
= LLVMBuildSelect(ctx
->builder
, can_emit
,
3678 LLVMConstReal(ctx
->f32
, 1.0f
),
3679 LLVMConstReal(ctx
->f32
, -1.0f
), "");
3680 ac_build_kill(&ctx
->ac
, kill
);
3682 /* loop num outputs */
3684 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
3685 LLVMValueRef
*out_ptr
= &ctx
->outputs
[i
* 4];
3690 if (!(ctx
->output_mask
& (1ull << i
)))
3693 if (i
== VARYING_SLOT_CLIP_DIST0
) {
3694 /* pack clip and cull into a single set of slots */
3695 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
3699 for (unsigned j
= 0; j
< length
; j
++) {
3700 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
,
3702 LLVMValueRef voffset
= LLVMConstInt(ctx
->i32
, (slot
* 4 + j
) * ctx
->gs_max_out_vertices
, false);
3703 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, gs_next_vertex
, "");
3704 voffset
= LLVMBuildMul(ctx
->builder
, voffset
, LLVMConstInt(ctx
->i32
, 4, false), "");
3706 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->i32
, "");
3708 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->gsvs_ring
,
3710 voffset
, ctx
->gs2vs_offset
, 0,
3716 gs_next_vertex
= LLVMBuildAdd(ctx
->builder
, gs_next_vertex
,
3718 LLVMBuildStore(ctx
->builder
, gs_next_vertex
, ctx
->gs_next_vertex
);
3720 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_EMIT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
3724 visit_end_primitive(struct nir_to_llvm_context
*ctx
,
3725 nir_intrinsic_instr
*instr
)
3727 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_CUT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
3731 visit_load_tess_coord(struct nir_to_llvm_context
*ctx
,
3732 nir_intrinsic_instr
*instr
)
3734 LLVMValueRef coord
[4] = {
3741 if (ctx
->tes_primitive_mode
== GL_TRIANGLES
)
3742 coord
[2] = LLVMBuildFSub(ctx
->builder
, ctx
->f32one
,
3743 LLVMBuildFAdd(ctx
->builder
, coord
[0], coord
[1], ""), "");
3745 LLVMValueRef result
= ac_build_gather_values(&ctx
->ac
, coord
, instr
->num_components
);
3746 return LLVMBuildBitCast(ctx
->builder
, result
,
3747 get_def_type(ctx
, &instr
->dest
.ssa
), "");
3750 static void visit_intrinsic(struct nir_to_llvm_context
*ctx
,
3751 nir_intrinsic_instr
*instr
)
3753 LLVMValueRef result
= NULL
;
3755 switch (instr
->intrinsic
) {
3756 case nir_intrinsic_load_work_group_id
: {
3757 result
= ctx
->workgroup_ids
;
3760 case nir_intrinsic_load_base_vertex
: {
3761 result
= ctx
->base_vertex
;
3764 case nir_intrinsic_load_vertex_id_zero_base
: {
3765 result
= ctx
->vertex_id
;
3768 case nir_intrinsic_load_local_invocation_id
: {
3769 result
= ctx
->local_invocation_ids
;
3772 case nir_intrinsic_load_base_instance
:
3773 result
= ctx
->start_instance
;
3775 case nir_intrinsic_load_draw_id
:
3776 result
= ctx
->draw_index
;
3778 case nir_intrinsic_load_invocation_id
:
3779 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
3780 result
= unpack_param(ctx
, ctx
->tcs_rel_ids
, 8, 5);
3782 result
= ctx
->gs_invocation_id
;
3784 case nir_intrinsic_load_primitive_id
:
3785 if (ctx
->stage
== MESA_SHADER_GEOMETRY
)
3786 result
= ctx
->gs_prim_id
;
3787 else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
3788 result
= ctx
->tcs_patch_id
;
3789 else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
)
3790 result
= ctx
->tes_patch_id
;
3792 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
3794 case nir_intrinsic_load_sample_id
:
3795 ctx
->shader_info
->fs
.force_persample
= true;
3796 result
= unpack_param(ctx
, ctx
->ancillary
, 8, 4);
3798 case nir_intrinsic_load_sample_pos
:
3799 ctx
->shader_info
->fs
.force_persample
= true;
3800 result
= load_sample_pos(ctx
);
3802 case nir_intrinsic_load_sample_mask_in
:
3803 result
= ctx
->sample_coverage
;
3805 case nir_intrinsic_load_front_face
:
3806 result
= ctx
->front_face
;
3808 case nir_intrinsic_load_instance_id
:
3809 result
= ctx
->instance_id
;
3810 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
3811 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
3813 case nir_intrinsic_load_num_work_groups
:
3814 result
= ctx
->num_work_groups
;
3816 case nir_intrinsic_load_local_invocation_index
:
3817 result
= visit_load_local_invocation_index(ctx
);
3819 case nir_intrinsic_load_push_constant
:
3820 result
= visit_load_push_constant(ctx
, instr
);
3822 case nir_intrinsic_vulkan_resource_index
:
3823 result
= visit_vulkan_resource_index(ctx
, instr
);
3825 case nir_intrinsic_store_ssbo
:
3826 visit_store_ssbo(ctx
, instr
);
3828 case nir_intrinsic_load_ssbo
:
3829 result
= visit_load_buffer(ctx
, instr
);
3831 case nir_intrinsic_ssbo_atomic_add
:
3832 case nir_intrinsic_ssbo_atomic_imin
:
3833 case nir_intrinsic_ssbo_atomic_umin
:
3834 case nir_intrinsic_ssbo_atomic_imax
:
3835 case nir_intrinsic_ssbo_atomic_umax
:
3836 case nir_intrinsic_ssbo_atomic_and
:
3837 case nir_intrinsic_ssbo_atomic_or
:
3838 case nir_intrinsic_ssbo_atomic_xor
:
3839 case nir_intrinsic_ssbo_atomic_exchange
:
3840 case nir_intrinsic_ssbo_atomic_comp_swap
:
3841 result
= visit_atomic_ssbo(ctx
, instr
);
3843 case nir_intrinsic_load_ubo
:
3844 result
= visit_load_ubo_buffer(ctx
, instr
);
3846 case nir_intrinsic_get_buffer_size
:
3847 result
= visit_get_buffer_size(ctx
, instr
);
3849 case nir_intrinsic_load_var
:
3850 result
= visit_load_var(ctx
, instr
);
3852 case nir_intrinsic_store_var
:
3853 visit_store_var(ctx
, instr
);
3855 case nir_intrinsic_image_load
:
3856 result
= visit_image_load(ctx
, instr
);
3858 case nir_intrinsic_image_store
:
3859 visit_image_store(ctx
, instr
);
3861 case nir_intrinsic_image_atomic_add
:
3862 case nir_intrinsic_image_atomic_min
:
3863 case nir_intrinsic_image_atomic_max
:
3864 case nir_intrinsic_image_atomic_and
:
3865 case nir_intrinsic_image_atomic_or
:
3866 case nir_intrinsic_image_atomic_xor
:
3867 case nir_intrinsic_image_atomic_exchange
:
3868 case nir_intrinsic_image_atomic_comp_swap
:
3869 result
= visit_image_atomic(ctx
, instr
);
3871 case nir_intrinsic_image_size
:
3872 result
= visit_image_size(ctx
, instr
);
3874 case nir_intrinsic_discard
:
3875 ctx
->shader_info
->fs
.can_discard
= true;
3876 ac_build_intrinsic(&ctx
->ac
, "llvm.AMDGPU.kilp",
3878 NULL
, 0, AC_FUNC_ATTR_LEGACY
);
3880 case nir_intrinsic_discard_if
:
3881 emit_discard_if(ctx
, instr
);
3883 case nir_intrinsic_memory_barrier
:
3884 emit_waitcnt(ctx
, VM_CNT
);
3886 case nir_intrinsic_barrier
:
3889 case nir_intrinsic_var_atomic_add
:
3890 case nir_intrinsic_var_atomic_imin
:
3891 case nir_intrinsic_var_atomic_umin
:
3892 case nir_intrinsic_var_atomic_imax
:
3893 case nir_intrinsic_var_atomic_umax
:
3894 case nir_intrinsic_var_atomic_and
:
3895 case nir_intrinsic_var_atomic_or
:
3896 case nir_intrinsic_var_atomic_xor
:
3897 case nir_intrinsic_var_atomic_exchange
:
3898 case nir_intrinsic_var_atomic_comp_swap
:
3899 result
= visit_var_atomic(ctx
, instr
);
3901 case nir_intrinsic_interp_var_at_centroid
:
3902 case nir_intrinsic_interp_var_at_sample
:
3903 case nir_intrinsic_interp_var_at_offset
:
3904 result
= visit_interp(ctx
, instr
);
3906 case nir_intrinsic_emit_vertex
:
3907 visit_emit_vertex(ctx
, instr
);
3909 case nir_intrinsic_end_primitive
:
3910 visit_end_primitive(ctx
, instr
);
3912 case nir_intrinsic_load_tess_coord
:
3913 result
= visit_load_tess_coord(ctx
, instr
);
3915 case nir_intrinsic_load_patch_vertices_in
:
3916 result
= LLVMConstInt(ctx
->i32
, ctx
->options
->key
.tcs
.input_vertices
, false);
3919 fprintf(stderr
, "Unknown intrinsic: ");
3920 nir_print_instr(&instr
->instr
, stderr
);
3921 fprintf(stderr
, "\n");
3925 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
3929 static LLVMValueRef
get_sampler_desc(struct nir_to_llvm_context
*ctx
,
3930 nir_deref_var
*deref
,
3931 enum desc_type desc_type
)
3933 unsigned desc_set
= deref
->var
->data
.descriptor_set
;
3934 LLVMValueRef list
= ctx
->descriptor_sets
[desc_set
];
3935 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[desc_set
].layout
;
3936 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ deref
->var
->data
.binding
;
3937 unsigned offset
= binding
->offset
;
3938 unsigned stride
= binding
->size
;
3940 LLVMBuilderRef builder
= ctx
->builder
;
3942 LLVMValueRef index
= NULL
;
3943 unsigned constant_index
= 0;
3945 assert(deref
->var
->data
.binding
< layout
->binding_count
);
3947 switch (desc_type
) {
3959 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
3969 unreachable("invalid desc_type\n");
3972 if (deref
->deref
.child
) {
3973 nir_deref_array
*child
= (nir_deref_array
*)deref
->deref
.child
;
3975 assert(child
->deref_array_type
!= nir_deref_array_type_wildcard
);
3976 offset
+= child
->base_offset
* stride
;
3977 if (child
->deref_array_type
== nir_deref_array_type_indirect
) {
3978 index
= get_src(ctx
, child
->indirect
);
3981 constant_index
= child
->base_offset
;
3983 if (desc_type
== DESC_SAMPLER
&& binding
->immutable_samplers_offset
&&
3984 (!index
|| binding
->immutable_samplers_equal
)) {
3985 if (binding
->immutable_samplers_equal
)
3988 const uint32_t *samplers
= radv_immutable_samplers(layout
, binding
);
3990 LLVMValueRef constants
[] = {
3991 LLVMConstInt(ctx
->i32
, samplers
[constant_index
* 4 + 0], 0),
3992 LLVMConstInt(ctx
->i32
, samplers
[constant_index
* 4 + 1], 0),
3993 LLVMConstInt(ctx
->i32
, samplers
[constant_index
* 4 + 2], 0),
3994 LLVMConstInt(ctx
->i32
, samplers
[constant_index
* 4 + 3], 0),
3996 return ac_build_gather_values(&ctx
->ac
, constants
, 4);
3999 assert(stride
% type_size
== 0);
4002 index
= ctx
->i32zero
;
4004 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, stride
/ type_size
, 0), "");
4006 list
= ac_build_gep0(&ctx
->ac
, list
, LLVMConstInt(ctx
->i32
, offset
, 0));
4007 list
= LLVMBuildPointerCast(builder
, list
, const_array(type
, 0), "");
4009 return ac_build_indexed_load_const(&ctx
->ac
, list
, index
);
4012 static void set_tex_fetch_args(struct nir_to_llvm_context
*ctx
,
4013 struct ac_image_args
*args
,
4014 nir_tex_instr
*instr
,
4016 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
4017 LLVMValueRef
*param
, unsigned count
,
4020 unsigned is_rect
= 0;
4021 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
4023 if (op
== nir_texop_lod
)
4025 /* Pad to power of two vector */
4026 while (count
< util_next_power_of_two(count
))
4027 param
[count
++] = LLVMGetUndef(ctx
->i32
);
4030 args
->addr
= ac_build_gather_values(&ctx
->ac
, param
, count
);
4032 args
->addr
= param
[0];
4034 args
->resource
= res_ptr
;
4035 args
->sampler
= samp_ptr
;
4037 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
&& op
== nir_texop_txf
) {
4038 args
->addr
= param
[0];
4042 args
->dmask
= dmask
;
4043 args
->unorm
= is_rect
;
4047 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
4050 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
4051 * filtering manually. The driver sets img7 to a mask clearing
4052 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
4053 * s_and_b32 samp0, samp0, img7
4056 * The ANISO_OVERRIDE sampler field enables this fix in TA.
4058 static LLVMValueRef
sici_fix_sampler_aniso(struct nir_to_llvm_context
*ctx
,
4059 LLVMValueRef res
, LLVMValueRef samp
)
4061 LLVMBuilderRef builder
= ctx
->builder
;
4062 LLVMValueRef img7
, samp0
;
4064 if (ctx
->options
->chip_class
>= VI
)
4067 img7
= LLVMBuildExtractElement(builder
, res
,
4068 LLVMConstInt(ctx
->i32
, 7, 0), "");
4069 samp0
= LLVMBuildExtractElement(builder
, samp
,
4070 LLVMConstInt(ctx
->i32
, 0, 0), "");
4071 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
4072 return LLVMBuildInsertElement(builder
, samp
, samp0
,
4073 LLVMConstInt(ctx
->i32
, 0, 0), "");
4076 static void tex_fetch_ptrs(struct nir_to_llvm_context
*ctx
,
4077 nir_tex_instr
*instr
,
4078 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
4079 LLVMValueRef
*fmask_ptr
)
4081 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
4082 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_BUFFER
);
4084 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_IMAGE
);
4087 *samp_ptr
= get_sampler_desc(ctx
, instr
->sampler
, DESC_SAMPLER
);
4089 *samp_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_SAMPLER
);
4090 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
4091 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
4093 if (fmask_ptr
&& !instr
->sampler
&& (instr
->op
== nir_texop_txf_ms
||
4094 instr
->op
== nir_texop_samples_identical
))
4095 *fmask_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_FMASK
);
4098 static LLVMValueRef
apply_round_slice(struct nir_to_llvm_context
*ctx
,
4101 coord
= to_float(ctx
, coord
);
4102 coord
= ac_build_intrinsic(&ctx
->ac
, "llvm.rint.f32", ctx
->f32
, &coord
, 1, 0);
4103 coord
= to_integer(ctx
, coord
);
4107 static void visit_tex(struct nir_to_llvm_context
*ctx
, nir_tex_instr
*instr
)
4109 LLVMValueRef result
= NULL
;
4110 struct ac_image_args args
= { 0 };
4111 unsigned dmask
= 0xf;
4112 LLVMValueRef address
[16];
4113 LLVMValueRef coords
[5];
4114 LLVMValueRef coord
= NULL
, lod
= NULL
, comparator
= NULL
;
4115 LLVMValueRef bias
= NULL
, offsets
= NULL
;
4116 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
, sample_index
= NULL
;
4117 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
4118 LLVMValueRef derivs
[6];
4119 unsigned chan
, count
= 0;
4120 unsigned const_src
= 0, num_deriv_comp
= 0;
4122 tex_fetch_ptrs(ctx
, instr
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
4124 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
4125 switch (instr
->src
[i
].src_type
) {
4126 case nir_tex_src_coord
:
4127 coord
= get_src(ctx
, instr
->src
[i
].src
);
4129 case nir_tex_src_projector
:
4131 case nir_tex_src_comparator
:
4132 comparator
= get_src(ctx
, instr
->src
[i
].src
);
4134 case nir_tex_src_offset
:
4135 offsets
= get_src(ctx
, instr
->src
[i
].src
);
4138 case nir_tex_src_bias
:
4139 bias
= get_src(ctx
, instr
->src
[i
].src
);
4141 case nir_tex_src_lod
:
4142 lod
= get_src(ctx
, instr
->src
[i
].src
);
4144 case nir_tex_src_ms_index
:
4145 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
4147 case nir_tex_src_ms_mcs
:
4149 case nir_tex_src_ddx
:
4150 ddx
= get_src(ctx
, instr
->src
[i
].src
);
4151 num_deriv_comp
= instr
->src
[i
].src
.ssa
->num_components
;
4153 case nir_tex_src_ddy
:
4154 ddy
= get_src(ctx
, instr
->src
[i
].src
);
4156 case nir_tex_src_texture_offset
:
4157 case nir_tex_src_sampler_offset
:
4158 case nir_tex_src_plane
:
4164 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
4165 result
= get_buffer_size(ctx
, res_ptr
, true);
4169 if (instr
->op
== nir_texop_texture_samples
) {
4170 LLVMValueRef res
, samples
, is_msaa
;
4171 res
= LLVMBuildBitCast(ctx
->builder
, res_ptr
, ctx
->v8i32
, "");
4172 samples
= LLVMBuildExtractElement(ctx
->builder
, res
,
4173 LLVMConstInt(ctx
->i32
, 3, false), "");
4174 is_msaa
= LLVMBuildLShr(ctx
->builder
, samples
,
4175 LLVMConstInt(ctx
->i32
, 28, false), "");
4176 is_msaa
= LLVMBuildAnd(ctx
->builder
, is_msaa
,
4177 LLVMConstInt(ctx
->i32
, 0xe, false), "");
4178 is_msaa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, is_msaa
,
4179 LLVMConstInt(ctx
->i32
, 0xe, false), "");
4181 samples
= LLVMBuildLShr(ctx
->builder
, samples
,
4182 LLVMConstInt(ctx
->i32
, 16, false), "");
4183 samples
= LLVMBuildAnd(ctx
->builder
, samples
,
4184 LLVMConstInt(ctx
->i32
, 0xf, false), "");
4185 samples
= LLVMBuildShl(ctx
->builder
, ctx
->i32one
,
4187 samples
= LLVMBuildSelect(ctx
->builder
, is_msaa
, samples
,
4194 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
4195 coords
[chan
] = llvm_extract_elem(ctx
, coord
, chan
);
4197 if (offsets
&& instr
->op
!= nir_texop_txf
) {
4198 LLVMValueRef offset
[3], pack
;
4199 for (chan
= 0; chan
< 3; ++chan
)
4200 offset
[chan
] = ctx
->i32zero
;
4203 for (chan
= 0; chan
< get_llvm_num_components(offsets
); chan
++) {
4204 offset
[chan
] = llvm_extract_elem(ctx
, offsets
, chan
);
4205 offset
[chan
] = LLVMBuildAnd(ctx
->builder
, offset
[chan
],
4206 LLVMConstInt(ctx
->i32
, 0x3f, false), "");
4208 offset
[chan
] = LLVMBuildShl(ctx
->builder
, offset
[chan
],
4209 LLVMConstInt(ctx
->i32
, chan
* 8, false), "");
4211 pack
= LLVMBuildOr(ctx
->builder
, offset
[0], offset
[1], "");
4212 pack
= LLVMBuildOr(ctx
->builder
, pack
, offset
[2], "");
4213 address
[count
++] = pack
;
4216 /* pack LOD bias value */
4217 if (instr
->op
== nir_texop_txb
&& bias
) {
4218 address
[count
++] = bias
;
4221 /* Pack depth comparison value */
4222 if (instr
->is_shadow
&& comparator
) {
4223 address
[count
++] = llvm_extract_elem(ctx
, comparator
, 0);
4226 /* pack derivatives */
4228 switch (instr
->sampler_dim
) {
4229 case GLSL_SAMPLER_DIM_3D
:
4230 case GLSL_SAMPLER_DIM_CUBE
:
4233 case GLSL_SAMPLER_DIM_2D
:
4237 case GLSL_SAMPLER_DIM_1D
:
4242 for (unsigned i
= 0; i
< num_deriv_comp
; i
++) {
4243 derivs
[i
] = to_float(ctx
, llvm_extract_elem(ctx
, ddx
, i
));
4244 derivs
[num_deriv_comp
+ i
] = to_float(ctx
, llvm_extract_elem(ctx
, ddy
, i
));
4248 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& coord
) {
4249 if (instr
->is_array
&& instr
->op
!= nir_texop_lod
)
4250 coords
[3] = apply_round_slice(ctx
, coords
[3]);
4251 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
4252 coords
[chan
] = to_float(ctx
, coords
[chan
]);
4253 if (instr
->coord_components
== 3)
4254 coords
[3] = LLVMGetUndef(ctx
->f32
);
4255 ac_prepare_cube_coords(&ctx
->ac
,
4256 instr
->op
== nir_texop_txd
, instr
->is_array
,
4263 for (unsigned i
= 0; i
< num_deriv_comp
* 2; i
++)
4264 address
[count
++] = derivs
[i
];
4267 /* Pack texture coordinates */
4269 address
[count
++] = coords
[0];
4270 if (instr
->coord_components
> 1) {
4271 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&& instr
->is_array
&& instr
->op
!= nir_texop_txf
) {
4272 coords
[1] = apply_round_slice(ctx
, coords
[1]);
4274 address
[count
++] = coords
[1];
4276 if (instr
->coord_components
> 2) {
4277 /* This seems like a bit of a hack - but it passes Vulkan CTS with it */
4278 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_3D
&&
4279 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_CUBE
&&
4280 instr
->op
!= nir_texop_txf
) {
4281 coords
[2] = apply_round_slice(ctx
, coords
[2]);
4283 address
[count
++] = coords
[2];
4288 if ((instr
->op
== nir_texop_txl
|| instr
->op
== nir_texop_txf
) && lod
) {
4289 address
[count
++] = lod
;
4290 } else if (instr
->op
== nir_texop_txf_ms
&& sample_index
) {
4291 address
[count
++] = sample_index
;
4292 } else if(instr
->op
== nir_texop_txs
) {
4295 address
[count
++] = lod
;
4297 address
[count
++] = ctx
->i32zero
;
4300 for (chan
= 0; chan
< count
; chan
++) {
4301 address
[chan
] = LLVMBuildBitCast(ctx
->builder
,
4302 address
[chan
], ctx
->i32
, "");
4305 if (instr
->op
== nir_texop_samples_identical
) {
4306 LLVMValueRef txf_address
[4];
4307 struct ac_image_args txf_args
= { 0 };
4308 unsigned txf_count
= count
;
4309 memcpy(txf_address
, address
, sizeof(txf_address
));
4311 if (!instr
->is_array
)
4312 txf_address
[2] = ctx
->i32zero
;
4313 txf_address
[3] = ctx
->i32zero
;
4315 set_tex_fetch_args(ctx
, &txf_args
, instr
, nir_texop_txf
,
4317 txf_address
, txf_count
, 0xf);
4319 result
= build_tex_intrinsic(ctx
, instr
, &txf_args
);
4321 result
= LLVMBuildExtractElement(ctx
->builder
, result
, ctx
->i32zero
, "");
4322 result
= emit_int_cmp(ctx
, LLVMIntEQ
, result
, ctx
->i32zero
);
4326 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
4327 instr
->op
!= nir_texop_txs
) {
4328 unsigned sample_chan
= instr
->is_array
? 3 : 2;
4329 address
[sample_chan
] = adjust_sample_index_using_fmask(ctx
,
4332 instr
->is_array
? address
[2] : NULL
,
4333 address
[sample_chan
],
4337 if (offsets
&& instr
->op
== nir_texop_txf
) {
4338 nir_const_value
*const_offset
=
4339 nir_src_as_const_value(instr
->src
[const_src
].src
);
4340 int num_offsets
= instr
->src
[const_src
].src
.ssa
->num_components
;
4341 assert(const_offset
);
4342 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
4343 if (num_offsets
> 2)
4344 address
[2] = LLVMBuildAdd(ctx
->builder
,
4345 address
[2], LLVMConstInt(ctx
->i32
, const_offset
->i32
[2], false), "");
4346 if (num_offsets
> 1)
4347 address
[1] = LLVMBuildAdd(ctx
->builder
,
4348 address
[1], LLVMConstInt(ctx
->i32
, const_offset
->i32
[1], false), "");
4349 address
[0] = LLVMBuildAdd(ctx
->builder
,
4350 address
[0], LLVMConstInt(ctx
->i32
, const_offset
->i32
[0], false), "");
4354 /* TODO TG4 support */
4355 if (instr
->op
== nir_texop_tg4
) {
4356 if (instr
->is_shadow
)
4359 dmask
= 1 << instr
->component
;
4361 set_tex_fetch_args(ctx
, &args
, instr
, instr
->op
,
4362 res_ptr
, samp_ptr
, address
, count
, dmask
);
4364 result
= build_tex_intrinsic(ctx
, instr
, &args
);
4366 if (instr
->op
== nir_texop_query_levels
)
4367 result
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, 3, false), "");
4368 else if (instr
->is_shadow
&& instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&& instr
->op
!= nir_texop_tg4
)
4369 result
= LLVMBuildExtractElement(ctx
->builder
, result
, ctx
->i32zero
, "");
4370 else if (instr
->op
== nir_texop_txs
&&
4371 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
4373 LLVMValueRef two
= LLVMConstInt(ctx
->i32
, 2, false);
4374 LLVMValueRef six
= LLVMConstInt(ctx
->i32
, 6, false);
4375 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->builder
, result
, two
, "");
4376 z
= LLVMBuildSDiv(ctx
->builder
, z
, six
, "");
4377 result
= LLVMBuildInsertElement(ctx
->builder
, result
, z
, two
, "");
4378 } else if (instr
->dest
.ssa
.num_components
!= 4)
4379 result
= trim_vector(ctx
, result
, instr
->dest
.ssa
.num_components
);
4383 assert(instr
->dest
.is_ssa
);
4384 result
= to_integer(ctx
, result
);
4385 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4390 static void visit_phi(struct nir_to_llvm_context
*ctx
, nir_phi_instr
*instr
)
4392 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
4393 LLVMValueRef result
= LLVMBuildPhi(ctx
->builder
, type
, "");
4395 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4396 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
4399 static void visit_post_phi(struct nir_to_llvm_context
*ctx
,
4400 nir_phi_instr
*instr
,
4401 LLVMValueRef llvm_phi
)
4403 nir_foreach_phi_src(src
, instr
) {
4404 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
4405 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
4407 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
4411 static void phi_post_pass(struct nir_to_llvm_context
*ctx
)
4413 struct hash_entry
*entry
;
4414 hash_table_foreach(ctx
->phis
, entry
) {
4415 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
4416 (LLVMValueRef
)entry
->data
);
4421 static void visit_ssa_undef(struct nir_to_llvm_context
*ctx
,
4422 nir_ssa_undef_instr
*instr
)
4424 unsigned num_components
= instr
->def
.num_components
;
4427 if (num_components
== 1)
4428 undef
= LLVMGetUndef(ctx
->i32
);
4430 undef
= LLVMGetUndef(LLVMVectorType(ctx
->i32
, num_components
));
4432 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, undef
);
4435 static void visit_jump(struct nir_to_llvm_context
*ctx
,
4436 nir_jump_instr
*instr
)
4438 switch (instr
->type
) {
4439 case nir_jump_break
:
4440 LLVMBuildBr(ctx
->builder
, ctx
->break_block
);
4441 LLVMClearInsertionPosition(ctx
->builder
);
4443 case nir_jump_continue
:
4444 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
4445 LLVMClearInsertionPosition(ctx
->builder
);
4448 fprintf(stderr
, "Unknown NIR jump instr: ");
4449 nir_print_instr(&instr
->instr
, stderr
);
4450 fprintf(stderr
, "\n");
4455 static void visit_cf_list(struct nir_to_llvm_context
*ctx
,
4456 struct exec_list
*list
);
4458 static void visit_block(struct nir_to_llvm_context
*ctx
, nir_block
*block
)
4460 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->builder
);
4461 nir_foreach_instr(instr
, block
)
4463 switch (instr
->type
) {
4464 case nir_instr_type_alu
:
4465 visit_alu(ctx
, nir_instr_as_alu(instr
));
4467 case nir_instr_type_load_const
:
4468 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
4470 case nir_instr_type_intrinsic
:
4471 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
4473 case nir_instr_type_tex
:
4474 visit_tex(ctx
, nir_instr_as_tex(instr
));
4476 case nir_instr_type_phi
:
4477 visit_phi(ctx
, nir_instr_as_phi(instr
));
4479 case nir_instr_type_ssa_undef
:
4480 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
4482 case nir_instr_type_jump
:
4483 visit_jump(ctx
, nir_instr_as_jump(instr
));
4486 fprintf(stderr
, "Unknown NIR instr type: ");
4487 nir_print_instr(instr
, stderr
);
4488 fprintf(stderr
, "\n");
4493 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
4496 static void visit_if(struct nir_to_llvm_context
*ctx
, nir_if
*if_stmt
)
4498 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
4500 LLVMBasicBlockRef merge_block
=
4501 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
4502 LLVMBasicBlockRef if_block
=
4503 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
4504 LLVMBasicBlockRef else_block
= merge_block
;
4505 if (!exec_list_is_empty(&if_stmt
->else_list
))
4506 else_block
= LLVMAppendBasicBlockInContext(
4507 ctx
->context
, ctx
->main_function
, "");
4509 LLVMValueRef cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, value
,
4510 LLVMConstInt(ctx
->i32
, 0, false), "");
4511 LLVMBuildCondBr(ctx
->builder
, cond
, if_block
, else_block
);
4513 LLVMPositionBuilderAtEnd(ctx
->builder
, if_block
);
4514 visit_cf_list(ctx
, &if_stmt
->then_list
);
4515 if (LLVMGetInsertBlock(ctx
->builder
))
4516 LLVMBuildBr(ctx
->builder
, merge_block
);
4518 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
4519 LLVMPositionBuilderAtEnd(ctx
->builder
, else_block
);
4520 visit_cf_list(ctx
, &if_stmt
->else_list
);
4521 if (LLVMGetInsertBlock(ctx
->builder
))
4522 LLVMBuildBr(ctx
->builder
, merge_block
);
4525 LLVMPositionBuilderAtEnd(ctx
->builder
, merge_block
);
4528 static void visit_loop(struct nir_to_llvm_context
*ctx
, nir_loop
*loop
)
4530 LLVMBasicBlockRef continue_parent
= ctx
->continue_block
;
4531 LLVMBasicBlockRef break_parent
= ctx
->break_block
;
4533 ctx
->continue_block
=
4534 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
4536 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
4538 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
4539 LLVMPositionBuilderAtEnd(ctx
->builder
, ctx
->continue_block
);
4540 visit_cf_list(ctx
, &loop
->body
);
4542 if (LLVMGetInsertBlock(ctx
->builder
))
4543 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
4544 LLVMPositionBuilderAtEnd(ctx
->builder
, ctx
->break_block
);
4546 ctx
->continue_block
= continue_parent
;
4547 ctx
->break_block
= break_parent
;
4550 static void visit_cf_list(struct nir_to_llvm_context
*ctx
,
4551 struct exec_list
*list
)
4553 foreach_list_typed(nir_cf_node
, node
, node
, list
)
4555 switch (node
->type
) {
4556 case nir_cf_node_block
:
4557 visit_block(ctx
, nir_cf_node_as_block(node
));
4560 case nir_cf_node_if
:
4561 visit_if(ctx
, nir_cf_node_as_if(node
));
4564 case nir_cf_node_loop
:
4565 visit_loop(ctx
, nir_cf_node_as_loop(node
));
4575 handle_vs_input_decl(struct nir_to_llvm_context
*ctx
,
4576 struct nir_variable
*variable
)
4578 LLVMValueRef t_list_ptr
= ctx
->vertex_buffers
;
4579 LLVMValueRef t_offset
;
4580 LLVMValueRef t_list
;
4582 LLVMValueRef buffer_index
;
4583 int index
= variable
->data
.location
- VERT_ATTRIB_GENERIC0
;
4584 int idx
= variable
->data
.location
;
4585 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, true);
4587 variable
->data
.driver_location
= idx
* 4;
4589 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << index
)) {
4590 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->instance_id
,
4591 ctx
->start_instance
, "");
4592 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
4593 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
4595 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->vertex_id
,
4596 ctx
->base_vertex
, "");
4598 for (unsigned i
= 0; i
< attrib_count
; ++i
, ++idx
) {
4599 t_offset
= LLVMConstInt(ctx
->i32
, index
+ i
, false);
4601 t_list
= ac_build_indexed_load_const(&ctx
->ac
, t_list_ptr
, t_offset
);
4603 input
= ac_build_buffer_load_format(&ctx
->ac
, t_list
,
4605 LLVMConstInt(ctx
->i32
, 0, false),
4608 for (unsigned chan
= 0; chan
< 4; chan
++) {
4609 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
4610 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
, chan
)] =
4611 to_integer(ctx
, LLVMBuildExtractElement(ctx
->builder
,
4612 input
, llvm_chan
, ""));
4617 static void interp_fs_input(struct nir_to_llvm_context
*ctx
,
4619 LLVMValueRef interp_param
,
4620 LLVMValueRef prim_mask
,
4621 LLVMValueRef result
[4])
4623 LLVMValueRef attr_number
;
4626 bool interp
= interp_param
!= NULL
;
4628 attr_number
= LLVMConstInt(ctx
->i32
, attr
, false);
4630 /* fs.constant returns the param from the middle vertex, so it's not
4631 * really useful for flat shading. It's meant to be used for custom
4632 * interpolation (but the intrinsic can't fetch from the other two
4635 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
4636 * to do the right thing. The only reason we use fs.constant is that
4637 * fs.interp cannot be used on integers, because they can be equal
4641 interp_param
= LLVMBuildBitCast(ctx
->builder
, interp_param
,
4642 LLVMVectorType(ctx
->f32
, 2), "");
4644 i
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
4646 j
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
4650 for (chan
= 0; chan
< 4; chan
++) {
4651 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
4654 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
4659 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
4660 LLVMConstInt(ctx
->i32
, 2, false),
4669 handle_fs_input_decl(struct nir_to_llvm_context
*ctx
,
4670 struct nir_variable
*variable
)
4672 int idx
= variable
->data
.location
;
4673 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4674 LLVMValueRef interp
;
4676 variable
->data
.driver_location
= idx
* 4;
4677 ctx
->input_mask
|= ((1ull << attrib_count
) - 1) << variable
->data
.location
;
4679 if (glsl_get_base_type(glsl_without_array(variable
->type
)) == GLSL_TYPE_FLOAT
) {
4680 unsigned interp_type
;
4681 if (variable
->data
.sample
) {
4682 interp_type
= INTERP_SAMPLE
;
4683 ctx
->shader_info
->fs
.force_persample
= true;
4684 } else if (variable
->data
.centroid
)
4685 interp_type
= INTERP_CENTROID
;
4687 interp_type
= INTERP_CENTER
;
4689 interp
= lookup_interp_param(ctx
, variable
->data
.interpolation
, interp_type
);
4693 for (unsigned i
= 0; i
< attrib_count
; ++i
)
4694 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
+ i
, 0)] = interp
;
4699 handle_shader_input_decl(struct nir_to_llvm_context
*ctx
,
4700 struct nir_variable
*variable
)
4702 switch (ctx
->stage
) {
4703 case MESA_SHADER_VERTEX
:
4704 handle_vs_input_decl(ctx
, variable
);
4706 case MESA_SHADER_FRAGMENT
:
4707 handle_fs_input_decl(ctx
, variable
);
4716 handle_fs_inputs_pre(struct nir_to_llvm_context
*ctx
,
4717 struct nir_shader
*nir
)
4720 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_INPUTS
; ++i
) {
4721 LLVMValueRef interp_param
;
4722 LLVMValueRef
*inputs
= ctx
->inputs
+radeon_llvm_reg_index_soa(i
, 0);
4724 if (!(ctx
->input_mask
& (1ull << i
)))
4727 if (i
>= VARYING_SLOT_VAR0
|| i
== VARYING_SLOT_PNTC
||
4728 i
== VARYING_SLOT_PRIMITIVE_ID
|| i
== VARYING_SLOT_LAYER
) {
4729 interp_param
= *inputs
;
4730 interp_fs_input(ctx
, index
, interp_param
, ctx
->prim_mask
,
4734 ctx
->shader_info
->fs
.flat_shaded_mask
|= 1u << index
;
4736 } else if (i
== VARYING_SLOT_POS
) {
4737 for(int i
= 0; i
< 3; ++i
)
4738 inputs
[i
] = ctx
->frag_pos
[i
];
4740 inputs
[3] = ac_build_fdiv(&ctx
->ac
, ctx
->f32one
, ctx
->frag_pos
[3]);
4743 ctx
->shader_info
->fs
.num_interp
= index
;
4744 if (ctx
->input_mask
& (1 << VARYING_SLOT_PNTC
))
4745 ctx
->shader_info
->fs
.has_pcoord
= true;
4746 if (ctx
->input_mask
& (1 << VARYING_SLOT_PRIMITIVE_ID
))
4747 ctx
->shader_info
->fs
.prim_id_input
= true;
4748 if (ctx
->input_mask
& (1 << VARYING_SLOT_LAYER
))
4749 ctx
->shader_info
->fs
.layer_input
= true;
4750 ctx
->shader_info
->fs
.input_mask
= ctx
->input_mask
>> VARYING_SLOT_VAR0
;
4754 ac_build_alloca(struct nir_to_llvm_context
*ctx
,
4758 LLVMBuilderRef builder
= ctx
->builder
;
4759 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
4760 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
4761 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
4762 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
4763 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ctx
->context
);
4767 LLVMPositionBuilderBefore(first_builder
, first_instr
);
4769 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
4772 res
= LLVMBuildAlloca(first_builder
, type
, name
);
4773 LLVMBuildStore(builder
, LLVMConstNull(type
), res
);
4775 LLVMDisposeBuilder(first_builder
);
4780 static LLVMValueRef
si_build_alloca_undef(struct nir_to_llvm_context
*ctx
,
4784 LLVMValueRef ptr
= ac_build_alloca(ctx
, type
, name
);
4785 LLVMBuildStore(ctx
->builder
, LLVMGetUndef(type
), ptr
);
4790 handle_shader_output_decl(struct nir_to_llvm_context
*ctx
,
4791 struct nir_variable
*variable
)
4793 int idx
= variable
->data
.location
+ variable
->data
.index
;
4794 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4795 uint64_t mask_attribs
;
4796 variable
->data
.driver_location
= idx
* 4;
4798 /* tess ctrl has it's own load/store paths for outputs */
4799 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
4802 mask_attribs
= ((1ull << attrib_count
) - 1) << idx
;
4803 if (ctx
->stage
== MESA_SHADER_VERTEX
||
4804 ctx
->stage
== MESA_SHADER_TESS_EVAL
||
4805 ctx
->stage
== MESA_SHADER_GEOMETRY
) {
4806 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
4807 int length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
4808 if (ctx
->stage
== MESA_SHADER_VERTEX
) {
4809 ctx
->shader_info
->vs
.outinfo
.clip_dist_mask
= (1 << ctx
->num_output_clips
) - 1;
4810 ctx
->shader_info
->vs
.outinfo
.cull_dist_mask
= (1 << ctx
->num_output_culls
) - 1;
4812 if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
4813 ctx
->shader_info
->tes
.outinfo
.clip_dist_mask
= (1 << ctx
->num_output_clips
) - 1;
4814 ctx
->shader_info
->tes
.outinfo
.cull_dist_mask
= (1 << ctx
->num_output_culls
) - 1;
4821 mask_attribs
= 1ull << idx
;
4825 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
4826 for (unsigned chan
= 0; chan
< 4; chan
++) {
4827 ctx
->outputs
[radeon_llvm_reg_index_soa(idx
+ i
, chan
)] =
4828 si_build_alloca_undef(ctx
, ctx
->f32
, "");
4831 ctx
->output_mask
|= mask_attribs
;
4835 setup_locals(struct nir_to_llvm_context
*ctx
,
4836 struct nir_function
*func
)
4839 ctx
->num_locals
= 0;
4840 nir_foreach_variable(variable
, &func
->impl
->locals
) {
4841 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4842 variable
->data
.driver_location
= ctx
->num_locals
* 4;
4843 ctx
->num_locals
+= attrib_count
;
4845 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
4849 for (i
= 0; i
< ctx
->num_locals
; i
++) {
4850 for (j
= 0; j
< 4; j
++) {
4851 ctx
->locals
[i
* 4 + j
] =
4852 si_build_alloca_undef(ctx
, ctx
->f32
, "temp");
4858 emit_float_saturate(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
, float lo
, float hi
)
4860 v
= to_float(ctx
, v
);
4861 v
= emit_intrin_2f_param(ctx
, "llvm.maxnum.f32", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, lo
));
4862 return emit_intrin_2f_param(ctx
, "llvm.minnum.f32", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, hi
));
4866 static LLVMValueRef
emit_pack_int16(struct nir_to_llvm_context
*ctx
,
4867 LLVMValueRef src0
, LLVMValueRef src1
)
4869 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
4870 LLVMValueRef comp
[2];
4872 comp
[0] = LLVMBuildAnd(ctx
->builder
, src0
, LLVMConstInt(ctx
-> i32
, 65535, 0), "");
4873 comp
[1] = LLVMBuildAnd(ctx
->builder
, src1
, LLVMConstInt(ctx
-> i32
, 65535, 0), "");
4874 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
4875 return LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
4878 /* Initialize arguments for the shader export intrinsic */
4880 si_llvm_init_export_args(struct nir_to_llvm_context
*ctx
,
4881 LLVMValueRef
*values
,
4883 struct ac_export_args
*args
)
4885 /* Default is 0xf. Adjusted below depending on the format. */
4886 args
->enabled_channels
= 0xf;
4888 /* Specify whether the EXEC mask represents the valid mask */
4889 args
->valid_mask
= 0;
4891 /* Specify whether this is the last export */
4894 /* Specify the target we are exporting */
4895 args
->target
= target
;
4897 args
->compr
= false;
4898 args
->out
[0] = LLVMGetUndef(ctx
->f32
);
4899 args
->out
[1] = LLVMGetUndef(ctx
->f32
);
4900 args
->out
[2] = LLVMGetUndef(ctx
->f32
);
4901 args
->out
[3] = LLVMGetUndef(ctx
->f32
);
4906 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&& target
>= V_008DFC_SQ_EXP_MRT
) {
4907 LLVMValueRef val
[4];
4908 unsigned index
= target
- V_008DFC_SQ_EXP_MRT
;
4909 unsigned col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * index
)) & 0xf;
4910 bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> index
) & 1;
4912 switch(col_format
) {
4913 case V_028714_SPI_SHADER_ZERO
:
4914 args
->enabled_channels
= 0; /* writemask */
4915 args
->target
= V_008DFC_SQ_EXP_NULL
;
4918 case V_028714_SPI_SHADER_32_R
:
4919 args
->enabled_channels
= 1;
4920 args
->out
[0] = values
[0];
4923 case V_028714_SPI_SHADER_32_GR
:
4924 args
->enabled_channels
= 0x3;
4925 args
->out
[0] = values
[0];
4926 args
->out
[1] = values
[1];
4929 case V_028714_SPI_SHADER_32_AR
:
4930 args
->enabled_channels
= 0x9;
4931 args
->out
[0] = values
[0];
4932 args
->out
[3] = values
[3];
4935 case V_028714_SPI_SHADER_FP16_ABGR
:
4938 for (unsigned chan
= 0; chan
< 2; chan
++) {
4939 LLVMValueRef pack_args
[2] = {
4941 values
[2 * chan
+ 1]
4943 LLVMValueRef packed
;
4945 packed
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, pack_args
);
4946 args
->out
[chan
] = packed
;
4950 case V_028714_SPI_SHADER_UNORM16_ABGR
:
4951 for (unsigned chan
= 0; chan
< 4; chan
++) {
4952 val
[chan
] = ac_build_clamp(&ctx
->ac
, values
[chan
]);
4953 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
4954 LLVMConstReal(ctx
->f32
, 65535), "");
4955 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
4956 LLVMConstReal(ctx
->f32
, 0.5), "");
4957 val
[chan
] = LLVMBuildFPToUI(ctx
->builder
, val
[chan
],
4962 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
4963 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
4966 case V_028714_SPI_SHADER_SNORM16_ABGR
:
4967 for (unsigned chan
= 0; chan
< 4; chan
++) {
4968 val
[chan
] = emit_float_saturate(ctx
, values
[chan
], -1, 1);
4969 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
4970 LLVMConstReal(ctx
->f32
, 32767), "");
4972 /* If positive, add 0.5, else add -0.5. */
4973 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
4974 LLVMBuildSelect(ctx
->builder
,
4975 LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
,
4976 val
[chan
], ctx
->f32zero
, ""),
4977 LLVMConstReal(ctx
->f32
, 0.5),
4978 LLVMConstReal(ctx
->f32
, -0.5), ""), "");
4979 val
[chan
] = LLVMBuildFPToSI(ctx
->builder
, val
[chan
], ctx
->i32
, "");
4983 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
4984 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
4987 case V_028714_SPI_SHADER_UINT16_ABGR
: {
4988 LLVMValueRef max
= LLVMConstInt(ctx
->i32
, is_int8
? 255 : 65535, 0);
4990 for (unsigned chan
= 0; chan
< 4; chan
++) {
4991 val
[chan
] = to_integer(ctx
, values
[chan
]);
4992 val
[chan
] = emit_minmax_int(ctx
, LLVMIntULT
, val
[chan
], max
);
4996 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
4997 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5001 case V_028714_SPI_SHADER_SINT16_ABGR
: {
5002 LLVMValueRef max
= LLVMConstInt(ctx
->i32
, is_int8
? 127 : 32767, 0);
5003 LLVMValueRef min
= LLVMConstInt(ctx
->i32
, is_int8
? -128 : -32768, 0);
5006 for (unsigned chan
= 0; chan
< 4; chan
++) {
5007 val
[chan
] = to_integer(ctx
, values
[chan
]);
5008 val
[chan
] = emit_minmax_int(ctx
, LLVMIntSLT
, val
[chan
], max
);
5009 val
[chan
] = emit_minmax_int(ctx
, LLVMIntSGT
, val
[chan
], min
);
5013 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5014 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5019 case V_028714_SPI_SHADER_32_ABGR
:
5020 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
5024 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
5026 for (unsigned i
= 0; i
< 4; ++i
)
5027 args
->out
[i
] = to_float(ctx
, args
->out
[i
]);
5031 handle_vs_outputs_post(struct nir_to_llvm_context
*ctx
,
5032 struct ac_vs_output_info
*outinfo
)
5034 uint32_t param_count
= 0;
5036 unsigned pos_idx
, num_pos_exports
= 0;
5037 struct ac_export_args args
, pos_args
[4] = {};
5038 LLVMValueRef psize_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
5041 outinfo
->prim_id_output
= 0xffffffff;
5042 outinfo
->layer_output
= 0xffffffff;
5043 if (ctx
->output_mask
& (1ull << VARYING_SLOT_CLIP_DIST0
)) {
5044 LLVMValueRef slots
[8];
5047 if (outinfo
->cull_dist_mask
)
5048 outinfo
->cull_dist_mask
<<= ctx
->num_output_clips
;
5050 i
= VARYING_SLOT_CLIP_DIST0
;
5051 for (j
= 0; j
< ctx
->num_output_clips
+ ctx
->num_output_culls
; j
++)
5052 slots
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
5053 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5055 for (i
= ctx
->num_output_clips
+ ctx
->num_output_culls
; i
< 8; i
++)
5056 slots
[i
] = LLVMGetUndef(ctx
->f32
);
5058 if (ctx
->num_output_clips
+ ctx
->num_output_culls
> 4) {
5059 target
= V_008DFC_SQ_EXP_POS
+ 3;
5060 si_llvm_init_export_args(ctx
, &slots
[4], target
, &args
);
5061 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5062 &args
, sizeof(args
));
5065 target
= V_008DFC_SQ_EXP_POS
+ 2;
5066 si_llvm_init_export_args(ctx
, &slots
[0], target
, &args
);
5067 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5068 &args
, sizeof(args
));
5072 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5073 LLVMValueRef values
[4];
5074 if (!(ctx
->output_mask
& (1ull << i
)))
5077 for (unsigned j
= 0; j
< 4; j
++)
5078 values
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
5079 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5081 if (i
== VARYING_SLOT_POS
) {
5082 target
= V_008DFC_SQ_EXP_POS
;
5083 } else if (i
== VARYING_SLOT_CLIP_DIST0
) {
5085 } else if (i
== VARYING_SLOT_PSIZ
) {
5086 outinfo
->writes_pointsize
= true;
5087 psize_value
= values
[0];
5089 } else if (i
== VARYING_SLOT_LAYER
) {
5090 outinfo
->writes_layer
= true;
5091 layer_value
= values
[0];
5092 outinfo
->layer_output
= param_count
;
5093 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5095 } else if (i
== VARYING_SLOT_VIEWPORT
) {
5096 outinfo
->writes_viewport_index
= true;
5097 viewport_index_value
= values
[0];
5099 } else if (i
== VARYING_SLOT_PRIMITIVE_ID
) {
5100 outinfo
->prim_id_output
= param_count
;
5101 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5103 } else if (i
>= VARYING_SLOT_VAR0
) {
5104 outinfo
->export_mask
|= 1u << (i
- VARYING_SLOT_VAR0
);
5105 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5109 si_llvm_init_export_args(ctx
, values
, target
, &args
);
5111 if (target
>= V_008DFC_SQ_EXP_POS
&&
5112 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
5113 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5114 &args
, sizeof(args
));
5116 ac_build_export(&ctx
->ac
, &args
);
5120 /* We need to add the position output manually if it's missing. */
5121 if (!pos_args
[0].out
[0]) {
5122 pos_args
[0].enabled_channels
= 0xf;
5123 pos_args
[0].valid_mask
= 0;
5124 pos_args
[0].done
= 0;
5125 pos_args
[0].target
= V_008DFC_SQ_EXP_POS
;
5126 pos_args
[0].compr
= 0;
5127 pos_args
[0].out
[0] = ctx
->f32zero
; /* X */
5128 pos_args
[0].out
[1] = ctx
->f32zero
; /* Y */
5129 pos_args
[0].out
[2] = ctx
->f32zero
; /* Z */
5130 pos_args
[0].out
[3] = ctx
->f32one
; /* W */
5133 uint32_t mask
= ((outinfo
->writes_pointsize
== true ? 1 : 0) |
5134 (outinfo
->writes_layer
== true ? 4 : 0) |
5135 (outinfo
->writes_viewport_index
== true ? 8 : 0));
5137 pos_args
[1].enabled_channels
= mask
;
5138 pos_args
[1].valid_mask
= 0;
5139 pos_args
[1].done
= 0;
5140 pos_args
[1].target
= V_008DFC_SQ_EXP_POS
+ 1;
5141 pos_args
[1].compr
= 0;
5142 pos_args
[1].out
[0] = ctx
->f32zero
; /* X */
5143 pos_args
[1].out
[1] = ctx
->f32zero
; /* Y */
5144 pos_args
[1].out
[2] = ctx
->f32zero
; /* Z */
5145 pos_args
[1].out
[3] = ctx
->f32zero
; /* W */
5147 if (outinfo
->writes_pointsize
== true)
5148 pos_args
[1].out
[0] = psize_value
;
5149 if (outinfo
->writes_layer
== true)
5150 pos_args
[1].out
[2] = layer_value
;
5151 if (outinfo
->writes_viewport_index
== true)
5152 pos_args
[1].out
[3] = viewport_index_value
;
5154 for (i
= 0; i
< 4; i
++) {
5155 if (pos_args
[i
].out
[0])
5160 for (i
= 0; i
< 4; i
++) {
5161 if (!pos_args
[i
].out
[0])
5164 /* Specify the target we are exporting */
5165 pos_args
[i
].target
= V_008DFC_SQ_EXP_POS
+ pos_idx
++;
5166 if (pos_idx
== num_pos_exports
)
5167 pos_args
[i
].done
= 1;
5168 ac_build_export(&ctx
->ac
, &pos_args
[i
]);
5171 outinfo
->pos_exports
= num_pos_exports
;
5172 outinfo
->param_exports
= param_count
;
5176 handle_es_outputs_post(struct nir_to_llvm_context
*ctx
,
5177 struct ac_es_output_info
*outinfo
)
5180 uint64_t max_output_written
= 0;
5181 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5182 LLVMValueRef
*out_ptr
= &ctx
->outputs
[i
* 4];
5186 if (!(ctx
->output_mask
& (1ull << i
)))
5189 if (i
== VARYING_SLOT_CLIP_DIST0
)
5190 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
5192 param_index
= shader_io_get_unique_index(i
);
5194 max_output_written
= MAX2(param_index
+ (length
> 4), max_output_written
);
5196 for (j
= 0; j
< length
; j
++) {
5197 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], "");
5198 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->i32
, "");
5200 ac_build_buffer_store_dword(&ctx
->ac
,
5203 NULL
, ctx
->es2gs_offset
,
5204 (4 * param_index
+ j
) * 4,
5208 outinfo
->esgs_itemsize
= (max_output_written
+ 1) * 16;
5212 handle_ls_outputs_post(struct nir_to_llvm_context
*ctx
)
5214 LLVMValueRef vertex_id
= ctx
->rel_auto_id
;
5215 LLVMValueRef vertex_dw_stride
= unpack_param(ctx
, ctx
->ls_out_layout
, 13, 8);
5216 LLVMValueRef base_dw_addr
= LLVMBuildMul(ctx
->builder
, vertex_id
,
5217 vertex_dw_stride
, "");
5219 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5220 LLVMValueRef
*out_ptr
= &ctx
->outputs
[i
* 4];
5223 if (!(ctx
->output_mask
& (1ull << i
)))
5226 if (i
== VARYING_SLOT_CLIP_DIST0
)
5227 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
5228 int param
= shader_io_get_unique_index(i
);
5229 mark_tess_output(ctx
, false, param
);
5231 mark_tess_output(ctx
, false, param
+ 1);
5232 LLVMValueRef dw_addr
= LLVMBuildAdd(ctx
->builder
, base_dw_addr
,
5233 LLVMConstInt(ctx
->i32
, param
* 4, false),
5235 for (unsigned j
= 0; j
< length
; j
++) {
5236 lds_store(ctx
, dw_addr
,
5237 LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], ""));
5238 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
, ctx
->i32one
, "");
5243 struct ac_build_if_state
5245 struct nir_to_llvm_context
*ctx
;
5246 LLVMValueRef condition
;
5247 LLVMBasicBlockRef entry_block
;
5248 LLVMBasicBlockRef true_block
;
5249 LLVMBasicBlockRef false_block
;
5250 LLVMBasicBlockRef merge_block
;
5253 static LLVMBasicBlockRef
5254 ac_build_insert_new_block(struct nir_to_llvm_context
*ctx
, const char *name
)
5256 LLVMBasicBlockRef current_block
;
5257 LLVMBasicBlockRef next_block
;
5258 LLVMBasicBlockRef new_block
;
5260 /* get current basic block */
5261 current_block
= LLVMGetInsertBlock(ctx
->builder
);
5263 /* chqeck if there's another block after this one */
5264 next_block
= LLVMGetNextBasicBlock(current_block
);
5266 /* insert the new block before the next block */
5267 new_block
= LLVMInsertBasicBlockInContext(ctx
->context
, next_block
, name
);
5270 /* append new block after current block */
5271 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
5272 new_block
= LLVMAppendBasicBlockInContext(ctx
->context
, function
, name
);
5278 ac_nir_build_if(struct ac_build_if_state
*ifthen
,
5279 struct nir_to_llvm_context
*ctx
,
5280 LLVMValueRef condition
)
5282 LLVMBasicBlockRef block
= LLVMGetInsertBlock(ctx
->builder
);
5284 memset(ifthen
, 0, sizeof *ifthen
);
5286 ifthen
->condition
= condition
;
5287 ifthen
->entry_block
= block
;
5289 /* create endif/merge basic block for the phi functions */
5290 ifthen
->merge_block
= ac_build_insert_new_block(ctx
, "endif-block");
5292 /* create/insert true_block before merge_block */
5293 ifthen
->true_block
=
5294 LLVMInsertBasicBlockInContext(ctx
->context
,
5295 ifthen
->merge_block
,
5298 /* successive code goes into the true block */
5299 LLVMPositionBuilderAtEnd(ctx
->builder
, ifthen
->true_block
);
5303 * End a conditional.
5306 ac_nir_build_endif(struct ac_build_if_state
*ifthen
)
5308 LLVMBuilderRef builder
= ifthen
->ctx
->builder
;
5310 /* Insert branch to the merge block from current block */
5311 LLVMBuildBr(builder
, ifthen
->merge_block
);
5314 * Now patch in the various branch instructions.
5317 /* Insert the conditional branch instruction at the end of entry_block */
5318 LLVMPositionBuilderAtEnd(builder
, ifthen
->entry_block
);
5319 if (ifthen
->false_block
) {
5320 /* we have an else clause */
5321 LLVMBuildCondBr(builder
, ifthen
->condition
,
5322 ifthen
->true_block
, ifthen
->false_block
);
5325 /* no else clause */
5326 LLVMBuildCondBr(builder
, ifthen
->condition
,
5327 ifthen
->true_block
, ifthen
->merge_block
);
5330 /* Resume building code at end of the ifthen->merge_block */
5331 LLVMPositionBuilderAtEnd(builder
, ifthen
->merge_block
);
5335 write_tess_factors(struct nir_to_llvm_context
*ctx
)
5337 unsigned stride
, outer_comps
, inner_comps
;
5338 struct ac_build_if_state if_ctx
, inner_if_ctx
;
5339 LLVMValueRef invocation_id
= unpack_param(ctx
, ctx
->tcs_rel_ids
, 8, 5);
5340 LLVMValueRef rel_patch_id
= unpack_param(ctx
, ctx
->tcs_rel_ids
, 0, 8);
5341 unsigned tess_inner_index
, tess_outer_index
;
5342 LLVMValueRef lds_base
, lds_inner
, lds_outer
, byteoffset
, buffer
;
5343 LLVMValueRef out
[6], vec0
, vec1
, tf_base
, inner
[4], outer
[4];
5347 switch (ctx
->options
->key
.tcs
.primitive_mode
) {
5367 ac_nir_build_if(&if_ctx
, ctx
,
5368 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
5369 invocation_id
, ctx
->i32zero
, ""));
5371 tess_inner_index
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
5372 tess_outer_index
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
5374 mark_tess_output(ctx
, true, tess_inner_index
);
5375 mark_tess_output(ctx
, true, tess_outer_index
);
5376 lds_base
= get_tcs_out_current_patch_data_offset(ctx
);
5377 lds_inner
= LLVMBuildAdd(ctx
->builder
, lds_base
,
5378 LLVMConstInt(ctx
->i32
, tess_inner_index
* 4, false), "");
5379 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_base
,
5380 LLVMConstInt(ctx
->i32
, tess_outer_index
* 4, false), "");
5382 for (i
= 0; i
< 4; i
++) {
5383 inner
[i
] = LLVMGetUndef(ctx
->i32
);
5384 outer
[i
] = LLVMGetUndef(ctx
->i32
);
5388 if (ctx
->options
->key
.tcs
.primitive_mode
== GL_ISOLINES
) {
5389 outer
[0] = out
[1] = lds_load(ctx
, lds_outer
);
5390 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_outer
,
5391 LLVMConstInt(ctx
->i32
, 1, false), "");
5392 outer
[1] = out
[0] = lds_load(ctx
, lds_outer
);
5394 for (i
= 0; i
< outer_comps
; i
++) {
5396 lds_load(ctx
, lds_outer
);
5397 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_outer
,
5398 LLVMConstInt(ctx
->i32
, 1, false), "");
5400 for (i
= 0; i
< inner_comps
; i
++) {
5401 inner
[i
] = out
[outer_comps
+i
] =
5402 lds_load(ctx
, lds_inner
);
5403 lds_inner
= LLVMBuildAdd(ctx
->builder
, lds_inner
,
5404 LLVMConstInt(ctx
->i32
, 1, false), "");
5408 /* Convert the outputs to vectors for stores. */
5409 vec0
= ac_build_gather_values(&ctx
->ac
, out
, MIN2(stride
, 4));
5413 vec1
= ac_build_gather_values(&ctx
->ac
, out
+ 4, stride
- 4);
5416 buffer
= ctx
->hs_ring_tess_factor
;
5417 tf_base
= ctx
->tess_factor_offset
;
5418 byteoffset
= LLVMBuildMul(ctx
->builder
, rel_patch_id
,
5419 LLVMConstInt(ctx
->i32
, 4 * stride
, false), "");
5421 ac_nir_build_if(&inner_if_ctx
, ctx
,
5422 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
5423 rel_patch_id
, ctx
->i32zero
, ""));
5425 /* Store the dynamic HS control word. */
5426 ac_build_buffer_store_dword(&ctx
->ac
, buffer
,
5427 LLVMConstInt(ctx
->i32
, 0x80000000, false),
5428 1, ctx
->i32zero
, tf_base
,
5429 0, 1, 0, true, false);
5430 ac_nir_build_endif(&inner_if_ctx
);
5432 /* Store the tessellation factors. */
5433 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec0
,
5434 MIN2(stride
, 4), byteoffset
, tf_base
,
5435 4, 1, 0, true, false);
5437 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec1
,
5438 stride
- 4, byteoffset
, tf_base
,
5439 20, 1, 0, true, false);
5441 //TODO store to offchip for TES to read - only if TES reads them
5443 LLVMValueRef inner_vec
, outer_vec
, tf_outer_offset
;
5444 LLVMValueRef tf_inner_offset
;
5445 unsigned param_outer
, param_inner
;
5447 param_outer
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
5448 tf_outer_offset
= get_tcs_tes_buffer_address(ctx
, NULL
,
5449 LLVMConstInt(ctx
->i32
, param_outer
, 0));
5451 outer_vec
= ac_build_gather_values(&ctx
->ac
, outer
,
5452 util_next_power_of_two(outer_comps
));
5454 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, outer_vec
,
5455 outer_comps
, tf_outer_offset
,
5456 ctx
->oc_lds
, 0, 1, 0, true, false);
5458 param_inner
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
5459 tf_inner_offset
= get_tcs_tes_buffer_address(ctx
, NULL
,
5460 LLVMConstInt(ctx
->i32
, param_inner
, 0));
5462 inner_vec
= inner_comps
== 1 ? inner
[0] :
5463 ac_build_gather_values(&ctx
->ac
, inner
, inner_comps
);
5464 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, inner_vec
,
5465 inner_comps
, tf_inner_offset
,
5466 ctx
->oc_lds
, 0, 1, 0, true, false);
5469 ac_nir_build_endif(&if_ctx
);
5473 handle_tcs_outputs_post(struct nir_to_llvm_context
*ctx
)
5475 write_tess_factors(ctx
);
5479 si_export_mrt_color(struct nir_to_llvm_context
*ctx
,
5480 LLVMValueRef
*color
, unsigned param
, bool is_last
)
5483 struct ac_export_args args
;
5486 si_llvm_init_export_args(ctx
, color
, param
,
5490 args
.valid_mask
= 1; /* whether the EXEC mask is valid */
5491 args
.done
= 1; /* DONE bit */
5492 } else if (!args
.enabled_channels
)
5493 return; /* unnecessary NULL export */
5495 ac_build_export(&ctx
->ac
, &args
);
5499 si_export_mrt_z(struct nir_to_llvm_context
*ctx
,
5500 LLVMValueRef depth
, LLVMValueRef stencil
,
5501 LLVMValueRef samplemask
)
5503 struct ac_export_args args
;
5505 args
.enabled_channels
= 0;
5506 args
.valid_mask
= 1;
5508 args
.target
= V_008DFC_SQ_EXP_MRTZ
;
5511 args
.out
[0] = LLVMGetUndef(ctx
->f32
); /* R, depth */
5512 args
.out
[1] = LLVMGetUndef(ctx
->f32
); /* G, stencil test val[0:7], stencil op val[8:15] */
5513 args
.out
[2] = LLVMGetUndef(ctx
->f32
); /* B, sample mask */
5514 args
.out
[3] = LLVMGetUndef(ctx
->f32
); /* A, alpha to mask */
5517 args
.out
[0] = depth
;
5518 args
.enabled_channels
|= 0x1;
5522 args
.out
[1] = stencil
;
5523 args
.enabled_channels
|= 0x2;
5527 args
.out
[2] = samplemask
;
5528 args
.enabled_channels
|= 0x4;
5531 /* SI (except OLAND) has a bug that it only looks
5532 * at the X writemask component. */
5533 if (ctx
->options
->chip_class
== SI
&&
5534 ctx
->options
->family
!= CHIP_OLAND
)
5535 args
.enabled_channels
|= 0x1;
5537 ac_build_export(&ctx
->ac
, &args
);
5541 handle_fs_outputs_post(struct nir_to_llvm_context
*ctx
)
5544 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
5546 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5547 LLVMValueRef values
[4];
5549 if (!(ctx
->output_mask
& (1ull << i
)))
5552 if (i
== FRAG_RESULT_DEPTH
) {
5553 ctx
->shader_info
->fs
.writes_z
= true;
5554 depth
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
5555 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
5556 } else if (i
== FRAG_RESULT_STENCIL
) {
5557 ctx
->shader_info
->fs
.writes_stencil
= true;
5558 stencil
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
5559 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
5560 } else if (i
== FRAG_RESULT_SAMPLE_MASK
) {
5561 ctx
->shader_info
->fs
.writes_sample_mask
= true;
5562 samplemask
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
5563 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
5566 for (unsigned j
= 0; j
< 4; j
++)
5567 values
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
5568 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5570 if (!ctx
->shader_info
->fs
.writes_z
&& !ctx
->shader_info
->fs
.writes_stencil
&& !ctx
->shader_info
->fs
.writes_sample_mask
)
5571 last
= ctx
->output_mask
<= ((1ull << (i
+ 1)) - 1);
5573 si_export_mrt_color(ctx
, values
, V_008DFC_SQ_EXP_MRT
+ index
, last
);
5578 if (depth
|| stencil
|| samplemask
)
5579 si_export_mrt_z(ctx
, depth
, stencil
, samplemask
);
5581 si_export_mrt_color(ctx
, NULL
, V_008DFC_SQ_EXP_NULL
, true);
5583 ctx
->shader_info
->fs
.output_mask
= index
? ((1ull << index
) - 1) : 0;
5587 emit_gs_epilogue(struct nir_to_llvm_context
*ctx
)
5589 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_NOP
| AC_SENDMSG_GS_DONE
, ctx
->gs_wave_id
);
5593 handle_shader_outputs_post(struct nir_to_llvm_context
*ctx
)
5595 switch (ctx
->stage
) {
5596 case MESA_SHADER_VERTEX
:
5597 if (ctx
->options
->key
.vs
.as_ls
)
5598 handle_ls_outputs_post(ctx
);
5599 else if (ctx
->options
->key
.vs
.as_es
)
5600 handle_es_outputs_post(ctx
, &ctx
->shader_info
->vs
.es_info
);
5602 handle_vs_outputs_post(ctx
, &ctx
->shader_info
->vs
.outinfo
);
5604 case MESA_SHADER_FRAGMENT
:
5605 handle_fs_outputs_post(ctx
);
5607 case MESA_SHADER_GEOMETRY
:
5608 emit_gs_epilogue(ctx
);
5610 case MESA_SHADER_TESS_CTRL
:
5611 handle_tcs_outputs_post(ctx
);
5613 case MESA_SHADER_TESS_EVAL
:
5614 if (ctx
->options
->key
.tes
.as_es
)
5615 handle_es_outputs_post(ctx
, &ctx
->shader_info
->tes
.es_info
);
5617 handle_vs_outputs_post(ctx
, &ctx
->shader_info
->tes
.outinfo
);
5625 handle_shared_compute_var(struct nir_to_llvm_context
*ctx
,
5626 struct nir_variable
*variable
, uint32_t *offset
, int idx
)
5628 unsigned size
= glsl_count_attribute_slots(variable
->type
, false);
5629 variable
->data
.driver_location
= *offset
;
5633 static void ac_llvm_finalize_module(struct nir_to_llvm_context
* ctx
)
5635 LLVMPassManagerRef passmgr
;
5636 /* Create the pass manager */
5637 passmgr
= LLVMCreateFunctionPassManagerForModule(
5640 /* This pass should eliminate all the load and store instructions */
5641 LLVMAddPromoteMemoryToRegisterPass(passmgr
);
5643 /* Add some optimization passes */
5644 LLVMAddScalarReplAggregatesPass(passmgr
);
5645 LLVMAddLICMPass(passmgr
);
5646 LLVMAddAggressiveDCEPass(passmgr
);
5647 LLVMAddCFGSimplificationPass(passmgr
);
5648 LLVMAddInstructionCombiningPass(passmgr
);
5651 LLVMInitializeFunctionPassManager(passmgr
);
5652 LLVMRunFunctionPassManager(passmgr
, ctx
->main_function
);
5653 LLVMFinalizeFunctionPassManager(passmgr
);
5655 LLVMDisposeBuilder(ctx
->builder
);
5656 LLVMDisposePassManager(passmgr
);
5660 ac_setup_rings(struct nir_to_llvm_context
*ctx
)
5662 if ((ctx
->stage
== MESA_SHADER_VERTEX
&& ctx
->options
->key
.vs
.as_es
) ||
5663 (ctx
->stage
== MESA_SHADER_TESS_EVAL
&& ctx
->options
->key
.tes
.as_es
)) {
5664 ctx
->esgs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_ESGS_VS
, false));
5667 if (ctx
->is_gs_copy_shader
) {
5668 ctx
->gsvs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_GSVS_VS
, false));
5670 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
5672 ctx
->esgs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_ESGS_GS
, false));
5673 ctx
->gsvs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_GSVS_GS
, false));
5675 ctx
->gsvs_ring
= LLVMBuildBitCast(ctx
->builder
, ctx
->gsvs_ring
, ctx
->v4i32
, "");
5677 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->gsvs_num_entries
, LLVMConstInt(ctx
->i32
, 2, false), "");
5678 tmp
= LLVMBuildExtractElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->i32one
, "");
5679 tmp
= LLVMBuildOr(ctx
->builder
, tmp
, ctx
->gsvs_ring_stride
, "");
5680 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, tmp
, ctx
->i32one
, "");
5682 ctx
->gsvs_ring
= LLVMBuildBitCast(ctx
->builder
, ctx
->gsvs_ring
, ctx
->v16i8
, "");
5685 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
5686 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
5687 ctx
->hs_ring_tess_offchip
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_HS_TESS_OFFCHIP
, false));
5688 ctx
->hs_ring_tess_factor
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_HS_TESS_FACTOR
, false));
5693 LLVMModuleRef
ac_translate_nir_to_llvm(LLVMTargetMachineRef tm
,
5694 struct nir_shader
*nir
,
5695 struct ac_shader_variant_info
*shader_info
,
5696 const struct ac_nir_compiler_options
*options
)
5698 struct nir_to_llvm_context ctx
= {0};
5699 struct nir_function
*func
;
5701 ctx
.options
= options
;
5702 ctx
.shader_info
= shader_info
;
5703 ctx
.context
= LLVMContextCreate();
5704 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
5706 ac_llvm_context_init(&ctx
.ac
, ctx
.context
);
5707 ctx
.ac
.module
= ctx
.module
;
5709 ctx
.has_ds_bpermute
= ctx
.options
->chip_class
>= VI
;
5711 memset(shader_info
, 0, sizeof(*shader_info
));
5713 ac_nir_shader_info_pass(nir
, options
, &shader_info
->info
);
5715 LLVMSetTarget(ctx
.module
, options
->supports_spill
? "amdgcn-mesa-mesa3d" : "amdgcn--");
5717 LLVMTargetDataRef data_layout
= LLVMCreateTargetDataLayout(tm
);
5718 char *data_layout_str
= LLVMCopyStringRepOfTargetData(data_layout
);
5719 LLVMSetDataLayout(ctx
.module
, data_layout_str
);
5720 LLVMDisposeTargetData(data_layout
);
5721 LLVMDisposeMessage(data_layout_str
);
5725 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
5726 ctx
.ac
.builder
= ctx
.builder
;
5727 ctx
.stage
= nir
->stage
;
5729 for (i
= 0; i
< AC_UD_MAX_SETS
; i
++)
5730 shader_info
->user_sgprs_locs
.descriptor_sets
[i
].sgpr_idx
= -1;
5731 for (i
= 0; i
< AC_UD_MAX_UD
; i
++)
5732 shader_info
->user_sgprs_locs
.shader_data
[i
].sgpr_idx
= -1;
5734 create_function(&ctx
);
5736 if (nir
->stage
== MESA_SHADER_COMPUTE
) {
5738 nir_foreach_variable(variable
, &nir
->shared
)
5742 uint32_t shared_size
= 0;
5744 LLVMTypeRef i8p
= LLVMPointerType(ctx
.i8
, LOCAL_ADDR_SPACE
);
5745 nir_foreach_variable(variable
, &nir
->shared
) {
5746 handle_shared_compute_var(&ctx
, variable
, &shared_size
, idx
);
5751 var
= LLVMAddGlobalInAddressSpace(ctx
.module
,
5752 LLVMArrayType(ctx
.i8
, shared_size
),
5755 LLVMSetAlignment(var
, 4);
5756 ctx
.shared_memory
= LLVMBuildBitCast(ctx
.builder
, var
, i8p
, "");
5758 } else if (nir
->stage
== MESA_SHADER_GEOMETRY
) {
5759 ctx
.gs_next_vertex
= ac_build_alloca(&ctx
, ctx
.i32
, "gs_next_vertex");
5761 ctx
.gs_max_out_vertices
= nir
->info
->gs
.vertices_out
;
5762 } else if (nir
->stage
== MESA_SHADER_TESS_EVAL
) {
5763 ctx
.tes_primitive_mode
= nir
->info
->tess
.primitive_mode
;
5766 ac_setup_rings(&ctx
);
5768 nir_foreach_variable(variable
, &nir
->inputs
)
5769 handle_shader_input_decl(&ctx
, variable
);
5771 if (nir
->stage
== MESA_SHADER_FRAGMENT
)
5772 handle_fs_inputs_pre(&ctx
, nir
);
5774 ctx
.num_output_clips
= nir
->info
->clip_distance_array_size
;
5775 ctx
.num_output_culls
= nir
->info
->cull_distance_array_size
;
5777 nir_foreach_variable(variable
, &nir
->outputs
)
5778 handle_shader_output_decl(&ctx
, variable
);
5780 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
5781 _mesa_key_pointer_equal
);
5782 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
5783 _mesa_key_pointer_equal
);
5785 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
5787 setup_locals(&ctx
, func
);
5789 visit_cf_list(&ctx
, &func
->impl
->body
);
5790 phi_post_pass(&ctx
);
5792 handle_shader_outputs_post(&ctx
);
5793 LLVMBuildRetVoid(ctx
.builder
);
5795 ac_llvm_finalize_module(&ctx
);
5797 ralloc_free(ctx
.defs
);
5798 ralloc_free(ctx
.phis
);
5800 if (nir
->stage
== MESA_SHADER_GEOMETRY
) {
5801 unsigned addclip
= ctx
.num_output_clips
+ ctx
.num_output_culls
> 4;
5802 shader_info
->gs
.gsvs_vertex_size
= (util_bitcount64(ctx
.output_mask
) + addclip
) * 16;
5803 shader_info
->gs
.max_gsvs_emit_size
= shader_info
->gs
.gsvs_vertex_size
*
5804 nir
->info
->gs
.vertices_out
;
5805 } else if (nir
->stage
== MESA_SHADER_TESS_CTRL
) {
5806 shader_info
->tcs
.outputs_written
= ctx
.tess_outputs_written
;
5807 shader_info
->tcs
.patch_outputs_written
= ctx
.tess_patch_outputs_written
;
5808 } else if (nir
->stage
== MESA_SHADER_VERTEX
&& ctx
.options
->key
.vs
.as_ls
) {
5809 shader_info
->vs
.outputs_written
= ctx
.tess_outputs_written
;
5815 static void ac_diagnostic_handler(LLVMDiagnosticInfoRef di
, void *context
)
5817 unsigned *retval
= (unsigned *)context
;
5818 LLVMDiagnosticSeverity severity
= LLVMGetDiagInfoSeverity(di
);
5819 char *description
= LLVMGetDiagInfoDescription(di
);
5821 if (severity
== LLVMDSError
) {
5823 fprintf(stderr
, "LLVM triggered Diagnostic Handler: %s\n",
5827 LLVMDisposeMessage(description
);
5830 static unsigned ac_llvm_compile(LLVMModuleRef M
,
5831 struct ac_shader_binary
*binary
,
5832 LLVMTargetMachineRef tm
)
5834 unsigned retval
= 0;
5836 LLVMContextRef llvm_ctx
;
5837 LLVMMemoryBufferRef out_buffer
;
5838 unsigned buffer_size
;
5839 const char *buffer_data
;
5842 /* Setup Diagnostic Handler*/
5843 llvm_ctx
= LLVMGetModuleContext(M
);
5845 LLVMContextSetDiagnosticHandler(llvm_ctx
, ac_diagnostic_handler
,
5849 mem_err
= LLVMTargetMachineEmitToMemoryBuffer(tm
, M
, LLVMObjectFile
,
5852 /* Process Errors/Warnings */
5854 fprintf(stderr
, "%s: %s", __FUNCTION__
, err
);
5860 /* Extract Shader Code*/
5861 buffer_size
= LLVMGetBufferSize(out_buffer
);
5862 buffer_data
= LLVMGetBufferStart(out_buffer
);
5864 ac_elf_read(buffer_data
, buffer_size
, binary
);
5867 LLVMDisposeMemoryBuffer(out_buffer
);
5873 static void ac_compile_llvm_module(LLVMTargetMachineRef tm
,
5874 LLVMModuleRef llvm_module
,
5875 struct ac_shader_binary
*binary
,
5876 struct ac_shader_config
*config
,
5877 struct ac_shader_variant_info
*shader_info
,
5878 gl_shader_stage stage
,
5879 bool dump_shader
, bool supports_spill
)
5882 ac_dump_module(llvm_module
);
5884 memset(binary
, 0, sizeof(*binary
));
5885 int v
= ac_llvm_compile(llvm_module
, binary
, tm
);
5887 fprintf(stderr
, "compile failed\n");
5891 fprintf(stderr
, "disasm:\n%s\n", binary
->disasm_string
);
5893 ac_shader_binary_read_config(binary
, config
, 0, supports_spill
);
5895 LLVMContextRef ctx
= LLVMGetModuleContext(llvm_module
);
5896 LLVMDisposeModule(llvm_module
);
5897 LLVMContextDispose(ctx
);
5899 if (stage
== MESA_SHADER_FRAGMENT
) {
5900 shader_info
->num_input_vgprs
= 0;
5901 if (G_0286CC_PERSP_SAMPLE_ENA(config
->spi_ps_input_addr
))
5902 shader_info
->num_input_vgprs
+= 2;
5903 if (G_0286CC_PERSP_CENTER_ENA(config
->spi_ps_input_addr
))
5904 shader_info
->num_input_vgprs
+= 2;
5905 if (G_0286CC_PERSP_CENTROID_ENA(config
->spi_ps_input_addr
))
5906 shader_info
->num_input_vgprs
+= 2;
5907 if (G_0286CC_PERSP_PULL_MODEL_ENA(config
->spi_ps_input_addr
))
5908 shader_info
->num_input_vgprs
+= 3;
5909 if (G_0286CC_LINEAR_SAMPLE_ENA(config
->spi_ps_input_addr
))
5910 shader_info
->num_input_vgprs
+= 2;
5911 if (G_0286CC_LINEAR_CENTER_ENA(config
->spi_ps_input_addr
))
5912 shader_info
->num_input_vgprs
+= 2;
5913 if (G_0286CC_LINEAR_CENTROID_ENA(config
->spi_ps_input_addr
))
5914 shader_info
->num_input_vgprs
+= 2;
5915 if (G_0286CC_LINE_STIPPLE_TEX_ENA(config
->spi_ps_input_addr
))
5916 shader_info
->num_input_vgprs
+= 1;
5917 if (G_0286CC_POS_X_FLOAT_ENA(config
->spi_ps_input_addr
))
5918 shader_info
->num_input_vgprs
+= 1;
5919 if (G_0286CC_POS_Y_FLOAT_ENA(config
->spi_ps_input_addr
))
5920 shader_info
->num_input_vgprs
+= 1;
5921 if (G_0286CC_POS_Z_FLOAT_ENA(config
->spi_ps_input_addr
))
5922 shader_info
->num_input_vgprs
+= 1;
5923 if (G_0286CC_POS_W_FLOAT_ENA(config
->spi_ps_input_addr
))
5924 shader_info
->num_input_vgprs
+= 1;
5925 if (G_0286CC_FRONT_FACE_ENA(config
->spi_ps_input_addr
))
5926 shader_info
->num_input_vgprs
+= 1;
5927 if (G_0286CC_ANCILLARY_ENA(config
->spi_ps_input_addr
))
5928 shader_info
->num_input_vgprs
+= 1;
5929 if (G_0286CC_SAMPLE_COVERAGE_ENA(config
->spi_ps_input_addr
))
5930 shader_info
->num_input_vgprs
+= 1;
5931 if (G_0286CC_POS_FIXED_PT_ENA(config
->spi_ps_input_addr
))
5932 shader_info
->num_input_vgprs
+= 1;
5934 config
->num_vgprs
= MAX2(config
->num_vgprs
, shader_info
->num_input_vgprs
);
5936 /* +3 for scratch wave offset and VCC */
5937 config
->num_sgprs
= MAX2(config
->num_sgprs
,
5938 shader_info
->num_input_sgprs
+ 3);
5941 void ac_compile_nir_shader(LLVMTargetMachineRef tm
,
5942 struct ac_shader_binary
*binary
,
5943 struct ac_shader_config
*config
,
5944 struct ac_shader_variant_info
*shader_info
,
5945 struct nir_shader
*nir
,
5946 const struct ac_nir_compiler_options
*options
,
5950 LLVMModuleRef llvm_module
= ac_translate_nir_to_llvm(tm
, nir
, shader_info
,
5953 ac_compile_llvm_module(tm
, llvm_module
, binary
, config
, shader_info
, nir
->stage
, dump_shader
, options
->supports_spill
);
5954 switch (nir
->stage
) {
5955 case MESA_SHADER_COMPUTE
:
5956 for (int i
= 0; i
< 3; ++i
)
5957 shader_info
->cs
.block_size
[i
] = nir
->info
->cs
.local_size
[i
];
5959 case MESA_SHADER_FRAGMENT
:
5960 shader_info
->fs
.early_fragment_test
= nir
->info
->fs
.early_fragment_tests
;
5962 case MESA_SHADER_GEOMETRY
:
5963 shader_info
->gs
.vertices_in
= nir
->info
->gs
.vertices_in
;
5964 shader_info
->gs
.vertices_out
= nir
->info
->gs
.vertices_out
;
5965 shader_info
->gs
.output_prim
= nir
->info
->gs
.output_primitive
;
5966 shader_info
->gs
.invocations
= nir
->info
->gs
.invocations
;
5968 case MESA_SHADER_TESS_EVAL
:
5969 shader_info
->tes
.primitive_mode
= nir
->info
->tess
.primitive_mode
;
5970 shader_info
->tes
.spacing
= nir
->info
->tess
.spacing
;
5971 shader_info
->tes
.ccw
= nir
->info
->tess
.ccw
;
5972 shader_info
->tes
.point_mode
= nir
->info
->tess
.point_mode
;
5973 shader_info
->tes
.as_es
= options
->key
.tes
.as_es
;
5975 case MESA_SHADER_TESS_CTRL
:
5976 shader_info
->tcs
.tcs_vertices_out
= nir
->info
->tess
.tcs_vertices_out
;
5978 case MESA_SHADER_VERTEX
:
5979 shader_info
->vs
.as_es
= options
->key
.vs
.as_es
;
5980 shader_info
->vs
.as_ls
= options
->key
.vs
.as_ls
;
5981 /* in LS mode we need at least 1, invocation id needs 3, handled elsewhere */
5982 if (options
->key
.vs
.as_ls
)
5983 shader_info
->vs
.vgpr_comp_cnt
= MAX2(1, shader_info
->vs
.vgpr_comp_cnt
);
5991 ac_gs_copy_shader_emit(struct nir_to_llvm_context
*ctx
)
5993 LLVMValueRef args
[9];
5994 args
[0] = ctx
->gsvs_ring
;
5995 args
[1] = LLVMBuildMul(ctx
->builder
, ctx
->vertex_id
, LLVMConstInt(ctx
->i32
, 4, false), "");
5996 args
[3] = ctx
->i32zero
;
5997 args
[4] = ctx
->i32one
; /* OFFEN */
5998 args
[5] = ctx
->i32zero
; /* IDXEN */
5999 args
[6] = ctx
->i32one
; /* GLC */
6000 args
[7] = ctx
->i32one
; /* SLC */
6001 args
[8] = ctx
->i32zero
; /* TFE */
6005 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
6009 if (!(ctx
->output_mask
& (1ull << i
)))
6012 if (i
== VARYING_SLOT_CLIP_DIST0
) {
6013 /* unpack clip and cull from a single set of slots */
6014 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
6019 for (unsigned j
= 0; j
< length
; j
++) {
6021 args
[2] = LLVMConstInt(ctx
->i32
,
6023 ctx
->gs_max_out_vertices
* 16 * 4, false);
6025 value
= ac_build_intrinsic(&ctx
->ac
,
6026 "llvm.SI.buffer.load.dword.i32.i32",
6028 AC_FUNC_ATTR_READONLY
|
6029 AC_FUNC_ATTR_LEGACY
);
6031 LLVMBuildStore(ctx
->builder
,
6032 to_float(ctx
, value
), ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)]);
6036 handle_vs_outputs_post(ctx
, &ctx
->shader_info
->vs
.outinfo
);
6039 void ac_create_gs_copy_shader(LLVMTargetMachineRef tm
,
6040 struct nir_shader
*geom_shader
,
6041 struct ac_shader_binary
*binary
,
6042 struct ac_shader_config
*config
,
6043 struct ac_shader_variant_info
*shader_info
,
6044 const struct ac_nir_compiler_options
*options
,
6047 struct nir_to_llvm_context ctx
= {0};
6048 ctx
.context
= LLVMContextCreate();
6049 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
6050 ctx
.options
= options
;
6051 ctx
.shader_info
= shader_info
;
6053 ac_llvm_context_init(&ctx
.ac
, ctx
.context
);
6054 ctx
.ac
.module
= ctx
.module
;
6056 ctx
.is_gs_copy_shader
= true;
6057 LLVMSetTarget(ctx
.module
, "amdgcn--");
6060 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
6061 ctx
.ac
.builder
= ctx
.builder
;
6062 ctx
.stage
= MESA_SHADER_VERTEX
;
6064 create_function(&ctx
);
6066 ctx
.gs_max_out_vertices
= geom_shader
->info
->gs
.vertices_out
;
6067 ac_setup_rings(&ctx
);
6069 ctx
.num_output_clips
= geom_shader
->info
->clip_distance_array_size
;
6070 ctx
.num_output_culls
= geom_shader
->info
->cull_distance_array_size
;
6072 nir_foreach_variable(variable
, &geom_shader
->outputs
)
6073 handle_shader_output_decl(&ctx
, variable
);
6075 ac_gs_copy_shader_emit(&ctx
);
6077 LLVMBuildRetVoid(ctx
.builder
);
6079 ac_llvm_finalize_module(&ctx
);
6081 ac_compile_llvm_module(tm
, ctx
.module
, binary
, config
, shader_info
,
6083 dump_shader
, options
->supports_spill
);