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_push_constants
;
569 bool need_ring_offsets
= false;
571 /* until we sort out scratch/global buffers always assign ring offsets for gs/vs/es */
572 if (ctx
->stage
== MESA_SHADER_GEOMETRY
||
573 ctx
->stage
== MESA_SHADER_VERTEX
||
574 ctx
->stage
== MESA_SHADER_TESS_CTRL
||
575 ctx
->stage
== MESA_SHADER_TESS_EVAL
||
576 ctx
->stage
== MESA_SHADER_FRAGMENT
||
577 ctx
->is_gs_copy_shader
)
578 need_ring_offsets
= true;
580 need_push_constants
= true;
581 if (!ctx
->options
->layout
)
582 need_push_constants
= false;
583 else if (!ctx
->options
->layout
->push_constant_size
&&
584 !ctx
->options
->layout
->dynamic_offset_count
)
585 need_push_constants
= false;
587 if (need_ring_offsets
&& !ctx
->options
->supports_spill
) {
588 arg_types
[arg_idx
++] = const_array(ctx
->v16i8
, 16); /* address of rings */
591 /* 1 for each descriptor set */
592 for (unsigned i
= 0; i
< num_sets
; ++i
) {
593 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& (1 << ctx
->stage
)) {
594 array_params_mask
|= (1 << arg_idx
);
595 arg_types
[arg_idx
++] = const_array(ctx
->i8
, 1024 * 1024);
599 if (need_push_constants
) {
600 /* 1 for push constants and dynamic descriptors */
601 array_params_mask
|= (1 << arg_idx
);
602 arg_types
[arg_idx
++] = const_array(ctx
->i8
, 1024 * 1024);
605 switch (ctx
->stage
) {
606 case MESA_SHADER_COMPUTE
:
607 if (ctx
->shader_info
->info
.cs
.grid_components_used
)
608 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, ctx
->shader_info
->info
.cs
.grid_components_used
); /* grid size */
609 user_sgpr_count
= arg_idx
;
610 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3);
611 arg_types
[arg_idx
++] = ctx
->i32
;
612 sgpr_count
= arg_idx
;
614 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3);
616 case MESA_SHADER_VERTEX
:
617 if (!ctx
->is_gs_copy_shader
) {
618 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
)
619 arg_types
[arg_idx
++] = const_array(ctx
->v16i8
, 16); /* vertex buffers */
620 arg_types
[arg_idx
++] = ctx
->i32
; // base vertex
621 arg_types
[arg_idx
++] = ctx
->i32
; // start instance
622 if (ctx
->shader_info
->info
.vs
.needs_draw_id
)
623 arg_types
[arg_idx
++] = ctx
->i32
; // draw index
625 user_sgpr_count
= arg_idx
;
626 if (ctx
->options
->key
.vs
.as_es
)
627 arg_types
[arg_idx
++] = ctx
->i32
; //es2gs offset
628 else if (ctx
->options
->key
.vs
.as_ls
) {
629 arg_types
[arg_idx
++] = ctx
->i32
; //ls out layout
632 sgpr_count
= arg_idx
;
633 arg_types
[arg_idx
++] = ctx
->i32
; // vertex id
634 if (!ctx
->is_gs_copy_shader
) {
635 arg_types
[arg_idx
++] = ctx
->i32
; // rel auto id
636 arg_types
[arg_idx
++] = ctx
->i32
; // vs prim id
637 arg_types
[arg_idx
++] = ctx
->i32
; // instance id
640 case MESA_SHADER_TESS_CTRL
:
641 arg_types
[arg_idx
++] = ctx
->i32
; // tcs offchip layout
642 arg_types
[arg_idx
++] = ctx
->i32
; // tcs out offsets
643 arg_types
[arg_idx
++] = ctx
->i32
; // tcs out layout
644 arg_types
[arg_idx
++] = ctx
->i32
; // tcs in layout
645 user_sgpr_count
= arg_idx
;
646 arg_types
[arg_idx
++] = ctx
->i32
; // param oc lds
647 arg_types
[arg_idx
++] = ctx
->i32
; // tess factor offset
648 sgpr_count
= arg_idx
;
649 arg_types
[arg_idx
++] = ctx
->i32
; // patch id
650 arg_types
[arg_idx
++] = ctx
->i32
; // rel ids;
652 case MESA_SHADER_TESS_EVAL
:
653 arg_types
[arg_idx
++] = ctx
->i32
; // tcs offchip layout
654 user_sgpr_count
= arg_idx
;
655 if (ctx
->options
->key
.tes
.as_es
) {
656 arg_types
[arg_idx
++] = ctx
->i32
; // OC LDS
657 arg_types
[arg_idx
++] = ctx
->i32
; //
658 arg_types
[arg_idx
++] = ctx
->i32
; // es2gs offset
660 arg_types
[arg_idx
++] = ctx
->i32
; //
661 arg_types
[arg_idx
++] = ctx
->i32
; // OC LDS
663 sgpr_count
= arg_idx
;
664 arg_types
[arg_idx
++] = ctx
->f32
; // tes_u
665 arg_types
[arg_idx
++] = ctx
->f32
; // tes_v
666 arg_types
[arg_idx
++] = ctx
->i32
; // tes rel patch id
667 arg_types
[arg_idx
++] = ctx
->i32
; // tes patch id
669 case MESA_SHADER_GEOMETRY
:
670 arg_types
[arg_idx
++] = ctx
->i32
; // gsvs stride
671 arg_types
[arg_idx
++] = ctx
->i32
; // gsvs num entires
672 user_sgpr_count
= arg_idx
;
673 arg_types
[arg_idx
++] = ctx
->i32
; // gs2vs offset
674 arg_types
[arg_idx
++] = ctx
->i32
; // wave id
675 sgpr_count
= arg_idx
;
676 arg_types
[arg_idx
++] = ctx
->i32
; // vtx0
677 arg_types
[arg_idx
++] = ctx
->i32
; // vtx1
678 arg_types
[arg_idx
++] = ctx
->i32
; // prim id
679 arg_types
[arg_idx
++] = ctx
->i32
; // vtx2
680 arg_types
[arg_idx
++] = ctx
->i32
; // vtx3
681 arg_types
[arg_idx
++] = ctx
->i32
; // vtx4
682 arg_types
[arg_idx
++] = ctx
->i32
; // vtx5
683 arg_types
[arg_idx
++] = ctx
->i32
; // GS instance id
685 case MESA_SHADER_FRAGMENT
:
686 if (ctx
->shader_info
->info
.ps
.needs_sample_positions
)
687 arg_types
[arg_idx
++] = ctx
->i32
; /* sample position offset */
688 user_sgpr_count
= arg_idx
;
689 arg_types
[arg_idx
++] = ctx
->i32
; /* prim mask */
690 sgpr_count
= arg_idx
;
691 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp sample */
692 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp center */
693 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp centroid */
694 arg_types
[arg_idx
++] = ctx
->v3i32
; /* persp pull model */
695 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear sample */
696 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear center */
697 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear centroid */
698 arg_types
[arg_idx
++] = ctx
->f32
; /* line stipple tex */
699 arg_types
[arg_idx
++] = ctx
->f32
; /* pos x float */
700 arg_types
[arg_idx
++] = ctx
->f32
; /* pos y float */
701 arg_types
[arg_idx
++] = ctx
->f32
; /* pos z float */
702 arg_types
[arg_idx
++] = ctx
->f32
; /* pos w float */
703 arg_types
[arg_idx
++] = ctx
->i32
; /* front face */
704 arg_types
[arg_idx
++] = ctx
->i32
; /* ancillary */
705 arg_types
[arg_idx
++] = ctx
->i32
; /* sample coverage */
706 arg_types
[arg_idx
++] = ctx
->i32
; /* fixed pt */
709 unreachable("Shader stage not implemented");
712 ctx
->main_function
= create_llvm_function(
713 ctx
->context
, ctx
->module
, ctx
->builder
, NULL
, 0, arg_types
,
714 arg_idx
, array_params_mask
, sgpr_count
, ctx
->options
->unsafe_math
);
715 set_llvm_calling_convention(ctx
->main_function
, ctx
->stage
);
717 ctx
->shader_info
->num_input_sgprs
= 0;
718 ctx
->shader_info
->num_input_vgprs
= 0;
720 ctx
->shader_info
->num_user_sgprs
= ctx
->options
->supports_spill
? 2 : 0;
721 for (i
= 0; i
< user_sgpr_count
; i
++)
722 ctx
->shader_info
->num_user_sgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
724 ctx
->shader_info
->num_input_sgprs
= ctx
->shader_info
->num_user_sgprs
;
725 for (; i
< sgpr_count
; i
++)
726 ctx
->shader_info
->num_input_sgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
728 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
729 for (; i
< arg_idx
; ++i
)
730 ctx
->shader_info
->num_input_vgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
735 if (ctx
->options
->supports_spill
|| need_ring_offsets
) {
736 set_userdata_location_shader(ctx
, AC_UD_SCRATCH_RING_OFFSETS
, user_sgpr_idx
, 2);
738 if (ctx
->options
->supports_spill
) {
739 ctx
->ring_offsets
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.implicit.buffer.ptr",
740 LLVMPointerType(ctx
->i8
, CONST_ADDR_SPACE
),
741 NULL
, 0, AC_FUNC_ATTR_READNONE
);
742 ctx
->ring_offsets
= LLVMBuildBitCast(ctx
->builder
, ctx
->ring_offsets
,
743 const_array(ctx
->v16i8
, 16), "");
745 ctx
->ring_offsets
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
748 for (unsigned i
= 0; i
< num_sets
; ++i
) {
749 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& (1 << ctx
->stage
)) {
750 set_userdata_location(&ctx
->shader_info
->user_sgprs_locs
.descriptor_sets
[i
], user_sgpr_idx
, 2);
752 ctx
->descriptor_sets
[i
] =
753 LLVMGetParam(ctx
->main_function
, arg_idx
++);
755 ctx
->descriptor_sets
[i
] = NULL
;
758 if (need_push_constants
) {
759 ctx
->push_constants
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
760 set_userdata_location_shader(ctx
, AC_UD_PUSH_CONSTANTS
, user_sgpr_idx
, 2);
764 switch (ctx
->stage
) {
765 case MESA_SHADER_COMPUTE
:
766 if (ctx
->shader_info
->info
.cs
.grid_components_used
) {
767 set_userdata_location_shader(ctx
, AC_UD_CS_GRID_SIZE
, user_sgpr_idx
, ctx
->shader_info
->info
.cs
.grid_components_used
);
768 user_sgpr_idx
+= ctx
->shader_info
->info
.cs
.grid_components_used
;
769 ctx
->num_work_groups
=
770 LLVMGetParam(ctx
->main_function
, arg_idx
++);
773 LLVMGetParam(ctx
->main_function
, arg_idx
++);
775 LLVMGetParam(ctx
->main_function
, arg_idx
++);
776 ctx
->local_invocation_ids
=
777 LLVMGetParam(ctx
->main_function
, arg_idx
++);
779 case MESA_SHADER_VERTEX
:
780 if (!ctx
->is_gs_copy_shader
) {
781 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
) {
782 set_userdata_location_shader(ctx
, AC_UD_VS_VERTEX_BUFFERS
, user_sgpr_idx
, 2);
784 ctx
->vertex_buffers
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
787 if (ctx
->shader_info
->info
.vs
.needs_draw_id
)
790 set_userdata_location_shader(ctx
, AC_UD_VS_BASE_VERTEX_START_INSTANCE
, user_sgpr_idx
, vs_num
);
791 user_sgpr_idx
+= vs_num
;
793 ctx
->base_vertex
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
794 ctx
->start_instance
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
795 if (ctx
->shader_info
->info
.vs
.needs_draw_id
)
796 ctx
->draw_index
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
798 if (ctx
->options
->key
.vs
.as_es
)
799 ctx
->es2gs_offset
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
800 else if (ctx
->options
->key
.vs
.as_ls
) {
801 set_userdata_location_shader(ctx
, AC_UD_VS_LS_TCS_IN_LAYOUT
, user_sgpr_idx
, 1);
803 ctx
->ls_out_layout
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
805 ctx
->vertex_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
806 if (!ctx
->is_gs_copy_shader
) {
807 ctx
->rel_auto_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
808 ctx
->vs_prim_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
809 ctx
->instance_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
811 if (ctx
->options
->key
.vs
.as_ls
)
812 declare_tess_lds(ctx
);
814 case MESA_SHADER_TESS_CTRL
:
815 set_userdata_location_shader(ctx
, AC_UD_TCS_OFFCHIP_LAYOUT
, user_sgpr_idx
, 4);
817 ctx
->tcs_offchip_layout
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
818 ctx
->tcs_out_offsets
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
819 ctx
->tcs_out_layout
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
820 ctx
->tcs_in_layout
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
821 ctx
->oc_lds
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
822 ctx
->tess_factor_offset
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
823 ctx
->tcs_patch_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
824 ctx
->tcs_rel_ids
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
826 declare_tess_lds(ctx
);
828 case MESA_SHADER_TESS_EVAL
:
829 set_userdata_location_shader(ctx
, AC_UD_TES_OFFCHIP_LAYOUT
, user_sgpr_idx
, 1);
831 ctx
->tcs_offchip_layout
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
832 if (ctx
->options
->key
.tes
.as_es
) {
833 ctx
->oc_lds
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
835 ctx
->es2gs_offset
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
838 ctx
->oc_lds
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
840 ctx
->tes_u
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
841 ctx
->tes_v
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
842 ctx
->tes_rel_patch_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
843 ctx
->tes_patch_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
845 case MESA_SHADER_GEOMETRY
:
846 set_userdata_location_shader(ctx
, AC_UD_GS_VS_RING_STRIDE_ENTRIES
, user_sgpr_idx
, 2);
848 ctx
->gsvs_ring_stride
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
849 ctx
->gsvs_num_entries
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
850 ctx
->gs2vs_offset
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
851 ctx
->gs_wave_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
852 ctx
->gs_vtx_offset
[0] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
853 ctx
->gs_vtx_offset
[1] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
854 ctx
->gs_prim_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
855 ctx
->gs_vtx_offset
[2] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
856 ctx
->gs_vtx_offset
[3] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
857 ctx
->gs_vtx_offset
[4] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
858 ctx
->gs_vtx_offset
[5] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
859 ctx
->gs_invocation_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
861 case MESA_SHADER_FRAGMENT
:
862 if (ctx
->shader_info
->info
.ps
.needs_sample_positions
) {
863 set_userdata_location_shader(ctx
, AC_UD_PS_SAMPLE_POS_OFFSET
, user_sgpr_idx
, 1);
865 ctx
->sample_pos_offset
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
867 ctx
->prim_mask
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
868 ctx
->persp_sample
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
869 ctx
->persp_center
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
870 ctx
->persp_centroid
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
872 ctx
->linear_sample
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
873 ctx
->linear_center
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
874 ctx
->linear_centroid
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
875 arg_idx
++; /* line stipple */
876 ctx
->frag_pos
[0] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
877 ctx
->frag_pos
[1] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
878 ctx
->frag_pos
[2] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
879 ctx
->frag_pos
[3] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
880 ctx
->front_face
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
881 ctx
->ancillary
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
882 ctx
->sample_coverage
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
885 unreachable("Shader stage not implemented");
889 static void setup_types(struct nir_to_llvm_context
*ctx
)
891 LLVMValueRef args
[4];
893 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->context
);
894 ctx
->i1
= LLVMIntTypeInContext(ctx
->context
, 1);
895 ctx
->i8
= LLVMIntTypeInContext(ctx
->context
, 8);
896 ctx
->i16
= LLVMIntTypeInContext(ctx
->context
, 16);
897 ctx
->i32
= LLVMIntTypeInContext(ctx
->context
, 32);
898 ctx
->i64
= LLVMIntTypeInContext(ctx
->context
, 64);
899 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
900 ctx
->v3i32
= LLVMVectorType(ctx
->i32
, 3);
901 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
902 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
903 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
904 ctx
->f16
= LLVMHalfTypeInContext(ctx
->context
);
905 ctx
->f64
= LLVMDoubleTypeInContext(ctx
->context
);
906 ctx
->v2f32
= LLVMVectorType(ctx
->f32
, 2);
907 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
908 ctx
->v16i8
= LLVMVectorType(ctx
->i8
, 16);
910 ctx
->i1false
= LLVMConstInt(ctx
->i1
, 0, false);
911 ctx
->i1true
= LLVMConstInt(ctx
->i1
, 1, false);
912 ctx
->i32zero
= LLVMConstInt(ctx
->i32
, 0, false);
913 ctx
->i32one
= LLVMConstInt(ctx
->i32
, 1, false);
914 ctx
->f32zero
= LLVMConstReal(ctx
->f32
, 0.0);
915 ctx
->f32one
= LLVMConstReal(ctx
->f32
, 1.0);
917 args
[0] = ctx
->f32zero
;
918 args
[1] = ctx
->f32zero
;
919 args
[2] = ctx
->f32zero
;
920 args
[3] = ctx
->f32one
;
921 ctx
->v4f32empty
= LLVMConstVector(args
, 4);
923 ctx
->uniform_md_kind
=
924 LLVMGetMDKindIDInContext(ctx
->context
, "amdgpu.uniform", 14);
925 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
927 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
930 static int get_llvm_num_components(LLVMValueRef value
)
932 LLVMTypeRef type
= LLVMTypeOf(value
);
933 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
934 ? LLVMGetVectorSize(type
)
936 return num_components
;
939 static LLVMValueRef
llvm_extract_elem(struct nir_to_llvm_context
*ctx
,
943 int count
= get_llvm_num_components(value
);
945 assert(index
< count
);
949 return LLVMBuildExtractElement(ctx
->builder
, value
,
950 LLVMConstInt(ctx
->i32
, index
, false), "");
953 static LLVMValueRef
trim_vector(struct nir_to_llvm_context
*ctx
,
954 LLVMValueRef value
, unsigned count
)
956 unsigned num_components
= get_llvm_num_components(value
);
957 if (count
== num_components
)
960 LLVMValueRef masks
[] = {
961 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
962 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false)};
965 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
968 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
969 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
973 build_store_values_extended(struct nir_to_llvm_context
*ctx
,
974 LLVMValueRef
*values
,
975 unsigned value_count
,
976 unsigned value_stride
,
979 LLVMBuilderRef builder
= ctx
->builder
;
982 if (value_count
== 1) {
983 LLVMBuildStore(builder
, vec
, values
[0]);
987 for (i
= 0; i
< value_count
; i
++) {
988 LLVMValueRef ptr
= values
[i
* value_stride
];
989 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
990 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
991 LLVMBuildStore(builder
, value
, ptr
);
995 static LLVMTypeRef
get_def_type(struct nir_to_llvm_context
*ctx
,
998 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->context
, def
->bit_size
);
999 if (def
->num_components
> 1) {
1000 type
= LLVMVectorType(type
, def
->num_components
);
1005 static LLVMValueRef
get_src(struct nir_to_llvm_context
*ctx
, nir_src src
)
1008 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->defs
, src
.ssa
);
1009 return (LLVMValueRef
)entry
->data
;
1013 static LLVMBasicBlockRef
get_block(struct nir_to_llvm_context
*ctx
,
1014 struct nir_block
*b
)
1016 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->defs
, b
);
1017 return (LLVMBasicBlockRef
)entry
->data
;
1020 static LLVMValueRef
get_alu_src(struct nir_to_llvm_context
*ctx
,
1022 unsigned num_components
)
1024 LLVMValueRef value
= get_src(ctx
, src
.src
);
1025 bool need_swizzle
= false;
1028 LLVMTypeRef type
= LLVMTypeOf(value
);
1029 unsigned src_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
1030 ? LLVMGetVectorSize(type
)
1033 for (unsigned i
= 0; i
< num_components
; ++i
) {
1034 assert(src
.swizzle
[i
] < src_components
);
1035 if (src
.swizzle
[i
] != i
)
1036 need_swizzle
= true;
1039 if (need_swizzle
|| num_components
!= src_components
) {
1040 LLVMValueRef masks
[] = {
1041 LLVMConstInt(ctx
->i32
, src
.swizzle
[0], false),
1042 LLVMConstInt(ctx
->i32
, src
.swizzle
[1], false),
1043 LLVMConstInt(ctx
->i32
, src
.swizzle
[2], false),
1044 LLVMConstInt(ctx
->i32
, src
.swizzle
[3], false)};
1046 if (src_components
> 1 && num_components
== 1) {
1047 value
= LLVMBuildExtractElement(ctx
->builder
, value
,
1049 } else if (src_components
== 1 && num_components
> 1) {
1050 LLVMValueRef values
[] = {value
, value
, value
, value
};
1051 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
1053 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
1054 value
= LLVMBuildShuffleVector(ctx
->builder
, value
, value
,
1058 assert(!src
.negate
);
1063 static LLVMValueRef
emit_int_cmp(struct nir_to_llvm_context
*ctx
,
1064 LLVMIntPredicate pred
, LLVMValueRef src0
,
1067 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
1068 return LLVMBuildSelect(ctx
->builder
, result
,
1069 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
1070 LLVMConstInt(ctx
->i32
, 0, false), "");
1073 static LLVMValueRef
emit_float_cmp(struct nir_to_llvm_context
*ctx
,
1074 LLVMRealPredicate pred
, LLVMValueRef src0
,
1077 LLVMValueRef result
;
1078 src0
= to_float(ctx
, src0
);
1079 src1
= to_float(ctx
, src1
);
1080 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
1081 return LLVMBuildSelect(ctx
->builder
, result
,
1082 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
1083 LLVMConstInt(ctx
->i32
, 0, false), "");
1086 static LLVMValueRef
emit_intrin_1f_param(struct nir_to_llvm_context
*ctx
,
1088 LLVMTypeRef result_type
,
1092 LLVMValueRef params
[] = {
1093 to_float(ctx
, src0
),
1096 sprintf(name
, "%s.f%d", intrin
, get_elem_bits(ctx
, result_type
));
1097 return ac_build_intrinsic(&ctx
->ac
, name
, result_type
, params
, 1, AC_FUNC_ATTR_READNONE
);
1100 static LLVMValueRef
emit_intrin_2f_param(struct nir_to_llvm_context
*ctx
,
1102 LLVMTypeRef result_type
,
1103 LLVMValueRef src0
, LLVMValueRef src1
)
1106 LLVMValueRef params
[] = {
1107 to_float(ctx
, src0
),
1108 to_float(ctx
, src1
),
1111 sprintf(name
, "%s.f%d", intrin
, get_elem_bits(ctx
, result_type
));
1112 return ac_build_intrinsic(&ctx
->ac
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
1115 static LLVMValueRef
emit_intrin_3f_param(struct nir_to_llvm_context
*ctx
,
1117 LLVMTypeRef result_type
,
1118 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
1121 LLVMValueRef params
[] = {
1122 to_float(ctx
, src0
),
1123 to_float(ctx
, src1
),
1124 to_float(ctx
, src2
),
1127 sprintf(name
, "%s.f%d", intrin
, get_elem_bits(ctx
, result_type
));
1128 return ac_build_intrinsic(&ctx
->ac
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
1131 static LLVMValueRef
emit_bcsel(struct nir_to_llvm_context
*ctx
,
1132 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
1134 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
1136 return LLVMBuildSelect(ctx
->builder
, v
, src1
, src2
, "");
1139 static LLVMValueRef
emit_find_lsb(struct nir_to_llvm_context
*ctx
,
1142 LLVMValueRef params
[2] = {
1145 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
1146 * add special code to check for x=0. The reason is that
1147 * the LLVM behavior for x=0 is different from what we
1150 * The hardware already implements the correct behavior.
1152 LLVMConstInt(ctx
->i32
, 1, false),
1154 return ac_build_intrinsic(&ctx
->ac
, "llvm.cttz.i32", ctx
->i32
, params
, 2, AC_FUNC_ATTR_READNONE
);
1157 static LLVMValueRef
emit_ifind_msb(struct nir_to_llvm_context
*ctx
,
1160 return ac_build_imsb(&ctx
->ac
, src0
, ctx
->i32
);
1163 static LLVMValueRef
emit_ufind_msb(struct nir_to_llvm_context
*ctx
,
1166 return ac_build_umsb(&ctx
->ac
, src0
, ctx
->i32
);
1169 static LLVMValueRef
emit_minmax_int(struct nir_to_llvm_context
*ctx
,
1170 LLVMIntPredicate pred
,
1171 LLVMValueRef src0
, LLVMValueRef src1
)
1173 return LLVMBuildSelect(ctx
->builder
,
1174 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
1179 static LLVMValueRef
emit_iabs(struct nir_to_llvm_context
*ctx
,
1182 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
1183 LLVMBuildNeg(ctx
->builder
, src0
, ""));
1186 static LLVMValueRef
emit_fsign(struct nir_to_llvm_context
*ctx
,
1189 LLVMValueRef cmp
, val
;
1191 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, ctx
->f32zero
, "");
1192 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->f32one
, src0
, "");
1193 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, ctx
->f32zero
, "");
1194 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(ctx
->f32
, -1.0), "");
1198 static LLVMValueRef
emit_isign(struct nir_to_llvm_context
*ctx
,
1201 LLVMValueRef cmp
, val
;
1203 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, ctx
->i32zero
, "");
1204 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->i32one
, src0
, "");
1205 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, ctx
->i32zero
, "");
1206 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(ctx
->i32
, -1, true), "");
1210 static LLVMValueRef
emit_ffract(struct nir_to_llvm_context
*ctx
,
1213 const char *intr
= "llvm.floor.f32";
1214 LLVMValueRef fsrc0
= to_float(ctx
, src0
);
1215 LLVMValueRef params
[] = {
1218 LLVMValueRef floor
= ac_build_intrinsic(&ctx
->ac
, intr
,
1219 ctx
->f32
, params
, 1,
1220 AC_FUNC_ATTR_READNONE
);
1221 return LLVMBuildFSub(ctx
->builder
, fsrc0
, floor
, "");
1224 static LLVMValueRef
emit_uint_carry(struct nir_to_llvm_context
*ctx
,
1226 LLVMValueRef src0
, LLVMValueRef src1
)
1228 LLVMTypeRef ret_type
;
1229 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
1231 LLVMValueRef params
[] = { src0
, src1
};
1232 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
1235 res
= ac_build_intrinsic(&ctx
->ac
, intrin
, ret_type
,
1236 params
, 2, AC_FUNC_ATTR_READNONE
);
1238 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
1239 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
1243 static LLVMValueRef
emit_b2f(struct nir_to_llvm_context
*ctx
,
1246 return LLVMBuildAnd(ctx
->builder
, src0
, LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""), "");
1249 static LLVMValueRef
emit_umul_high(struct nir_to_llvm_context
*ctx
,
1250 LLVMValueRef src0
, LLVMValueRef src1
)
1252 LLVMValueRef dst64
, result
;
1253 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
1254 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
1256 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1257 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1258 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1262 static LLVMValueRef
emit_imul_high(struct nir_to_llvm_context
*ctx
,
1263 LLVMValueRef src0
, LLVMValueRef src1
)
1265 LLVMValueRef dst64
, result
;
1266 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
1267 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
1269 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1270 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1271 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1275 static LLVMValueRef
emit_bitfield_extract(struct nir_to_llvm_context
*ctx
,
1277 LLVMValueRef srcs
[3])
1279 LLVMValueRef result
;
1280 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
1282 result
= ac_build_bfe(&ctx
->ac
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
1283 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
1287 static LLVMValueRef
emit_bitfield_insert(struct nir_to_llvm_context
*ctx
,
1288 LLVMValueRef src0
, LLVMValueRef src1
,
1289 LLVMValueRef src2
, LLVMValueRef src3
)
1291 LLVMValueRef bfi_args
[3], result
;
1293 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
1294 LLVMBuildSub(ctx
->builder
,
1295 LLVMBuildShl(ctx
->builder
,
1300 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
1303 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
1306 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
1307 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
1309 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
1310 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
1311 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
1313 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
1317 static LLVMValueRef
emit_pack_half_2x16(struct nir_to_llvm_context
*ctx
,
1320 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1322 LLVMValueRef comp
[2];
1324 src0
= to_float(ctx
, src0
);
1325 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32zero
, "");
1326 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32one
, "");
1327 for (i
= 0; i
< 2; i
++) {
1328 comp
[i
] = LLVMBuildFPTrunc(ctx
->builder
, comp
[i
], ctx
->f16
, "");
1329 comp
[i
] = LLVMBuildBitCast(ctx
->builder
, comp
[i
], ctx
->i16
, "");
1330 comp
[i
] = LLVMBuildZExt(ctx
->builder
, comp
[i
], ctx
->i32
, "");
1333 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
1334 comp
[0] = LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
1339 static LLVMValueRef
emit_unpack_half_2x16(struct nir_to_llvm_context
*ctx
,
1342 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1343 LLVMValueRef temps
[2], result
, val
;
1346 for (i
= 0; i
< 2; i
++) {
1347 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
1348 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
1349 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
1350 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
1353 result
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), temps
[0],
1355 result
= LLVMBuildInsertElement(ctx
->builder
, result
, temps
[1],
1360 static LLVMValueRef
emit_ddxy(struct nir_to_llvm_context
*ctx
,
1366 LLVMValueRef result
;
1367 ctx
->has_ddxy
= true;
1369 if (!ctx
->lds
&& !ctx
->has_ds_bpermute
)
1370 ctx
->lds
= LLVMAddGlobalInAddressSpace(ctx
->module
,
1371 LLVMArrayType(ctx
->i32
, 64),
1372 "ddxy_lds", LOCAL_ADDR_SPACE
);
1374 if (op
== nir_op_fddx_fine
|| op
== nir_op_fddx
)
1375 mask
= AC_TID_MASK_LEFT
;
1376 else if (op
== nir_op_fddy_fine
|| op
== nir_op_fddy
)
1377 mask
= AC_TID_MASK_TOP
;
1379 mask
= AC_TID_MASK_TOP_LEFT
;
1381 /* for DDX we want to next X pixel, DDY next Y pixel. */
1382 if (op
== nir_op_fddx_fine
||
1383 op
== nir_op_fddx_coarse
||
1389 result
= ac_build_ddxy(&ctx
->ac
, ctx
->has_ds_bpermute
,
1390 mask
, idx
, ctx
->lds
,
1396 * this takes an I,J coordinate pair,
1397 * and works out the X and Y derivatives.
1398 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
1400 static LLVMValueRef
emit_ddxy_interp(
1401 struct nir_to_llvm_context
*ctx
,
1402 LLVMValueRef interp_ij
)
1404 LLVMValueRef result
[4], a
;
1407 for (i
= 0; i
< 2; i
++) {
1408 a
= LLVMBuildExtractElement(ctx
->builder
, interp_ij
,
1409 LLVMConstInt(ctx
->i32
, i
, false), "");
1410 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
1411 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
1413 return ac_build_gather_values(&ctx
->ac
, result
, 4);
1416 static void visit_alu(struct nir_to_llvm_context
*ctx
, nir_alu_instr
*instr
)
1418 LLVMValueRef src
[4], result
= NULL
;
1419 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
1420 unsigned src_components
;
1421 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
1423 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
1424 switch (instr
->op
) {
1430 case nir_op_pack_half_2x16
:
1433 case nir_op_unpack_half_2x16
:
1437 src_components
= num_components
;
1440 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1441 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
1443 switch (instr
->op
) {
1449 src
[0] = to_float(ctx
, src
[0]);
1450 result
= LLVMBuildFNeg(ctx
->builder
, src
[0], "");
1453 result
= LLVMBuildNeg(ctx
->builder
, src
[0], "");
1456 result
= LLVMBuildNot(ctx
->builder
, src
[0], "");
1459 result
= LLVMBuildAdd(ctx
->builder
, src
[0], src
[1], "");
1462 src
[0] = to_float(ctx
, src
[0]);
1463 src
[1] = to_float(ctx
, src
[1]);
1464 result
= LLVMBuildFAdd(ctx
->builder
, src
[0], src
[1], "");
1467 src
[0] = to_float(ctx
, src
[0]);
1468 src
[1] = to_float(ctx
, src
[1]);
1469 result
= LLVMBuildFSub(ctx
->builder
, src
[0], src
[1], "");
1472 result
= LLVMBuildSub(ctx
->builder
, src
[0], src
[1], "");
1475 result
= LLVMBuildMul(ctx
->builder
, src
[0], src
[1], "");
1478 result
= LLVMBuildSRem(ctx
->builder
, src
[0], src
[1], "");
1481 result
= LLVMBuildURem(ctx
->builder
, src
[0], src
[1], "");
1484 src
[0] = to_float(ctx
, src
[0]);
1485 src
[1] = to_float(ctx
, src
[1]);
1486 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1487 result
= emit_intrin_1f_param(ctx
, "llvm.floor",
1488 to_float_type(ctx
, def_type
), result
);
1489 result
= LLVMBuildFMul(ctx
->builder
, src
[1] , result
, "");
1490 result
= LLVMBuildFSub(ctx
->builder
, src
[0], result
, "");
1493 src
[0] = to_float(ctx
, src
[0]);
1494 src
[1] = to_float(ctx
, src
[1]);
1495 result
= LLVMBuildFRem(ctx
->builder
, src
[0], src
[1], "");
1498 result
= LLVMBuildSRem(ctx
->builder
, src
[0], src
[1], "");
1501 result
= LLVMBuildSDiv(ctx
->builder
, src
[0], src
[1], "");
1504 result
= LLVMBuildUDiv(ctx
->builder
, src
[0], src
[1], "");
1507 src
[0] = to_float(ctx
, src
[0]);
1508 src
[1] = to_float(ctx
, src
[1]);
1509 result
= LLVMBuildFMul(ctx
->builder
, src
[0], src
[1], "");
1512 src
[0] = to_float(ctx
, src
[0]);
1513 src
[1] = to_float(ctx
, src
[1]);
1514 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
1517 src
[0] = to_float(ctx
, src
[0]);
1518 result
= ac_build_fdiv(&ctx
->ac
, ctx
->f32one
, src
[0]);
1521 result
= LLVMBuildAnd(ctx
->builder
, src
[0], src
[1], "");
1524 result
= LLVMBuildOr(ctx
->builder
, src
[0], src
[1], "");
1527 result
= LLVMBuildXor(ctx
->builder
, src
[0], src
[1], "");
1530 result
= LLVMBuildShl(ctx
->builder
, src
[0], src
[1], "");
1533 result
= LLVMBuildAShr(ctx
->builder
, src
[0], src
[1], "");
1536 result
= LLVMBuildLShr(ctx
->builder
, src
[0], src
[1], "");
1539 result
= emit_int_cmp(ctx
, LLVMIntSLT
, src
[0], src
[1]);
1542 result
= emit_int_cmp(ctx
, LLVMIntNE
, src
[0], src
[1]);
1545 result
= emit_int_cmp(ctx
, LLVMIntEQ
, src
[0], src
[1]);
1548 result
= emit_int_cmp(ctx
, LLVMIntSGE
, src
[0], src
[1]);
1551 result
= emit_int_cmp(ctx
, LLVMIntULT
, src
[0], src
[1]);
1554 result
= emit_int_cmp(ctx
, LLVMIntUGE
, src
[0], src
[1]);
1557 result
= emit_float_cmp(ctx
, LLVMRealUEQ
, src
[0], src
[1]);
1560 result
= emit_float_cmp(ctx
, LLVMRealUNE
, src
[0], src
[1]);
1563 result
= emit_float_cmp(ctx
, LLVMRealULT
, src
[0], src
[1]);
1566 result
= emit_float_cmp(ctx
, LLVMRealUGE
, src
[0], src
[1]);
1569 result
= emit_intrin_1f_param(ctx
, "llvm.fabs",
1570 to_float_type(ctx
, def_type
), src
[0]);
1573 result
= emit_iabs(ctx
, src
[0]);
1576 result
= emit_minmax_int(ctx
, LLVMIntSGT
, src
[0], src
[1]);
1579 result
= emit_minmax_int(ctx
, LLVMIntSLT
, src
[0], src
[1]);
1582 result
= emit_minmax_int(ctx
, LLVMIntUGT
, src
[0], src
[1]);
1585 result
= emit_minmax_int(ctx
, LLVMIntULT
, src
[0], src
[1]);
1588 result
= emit_isign(ctx
, src
[0]);
1591 src
[0] = to_float(ctx
, src
[0]);
1592 result
= emit_fsign(ctx
, src
[0]);
1595 result
= emit_intrin_1f_param(ctx
, "llvm.floor",
1596 to_float_type(ctx
, def_type
), src
[0]);
1599 result
= emit_intrin_1f_param(ctx
, "llvm.trunc",
1600 to_float_type(ctx
, def_type
), src
[0]);
1603 result
= emit_intrin_1f_param(ctx
, "llvm.ceil",
1604 to_float_type(ctx
, def_type
), src
[0]);
1606 case nir_op_fround_even
:
1607 result
= emit_intrin_1f_param(ctx
, "llvm.rint",
1608 to_float_type(ctx
, def_type
),src
[0]);
1611 result
= emit_ffract(ctx
, src
[0]);
1614 result
= emit_intrin_1f_param(ctx
, "llvm.sin",
1615 to_float_type(ctx
, def_type
), src
[0]);
1618 result
= emit_intrin_1f_param(ctx
, "llvm.cos",
1619 to_float_type(ctx
, def_type
), src
[0]);
1622 result
= emit_intrin_1f_param(ctx
, "llvm.sqrt",
1623 to_float_type(ctx
, def_type
), src
[0]);
1626 result
= emit_intrin_1f_param(ctx
, "llvm.exp2",
1627 to_float_type(ctx
, def_type
), src
[0]);
1630 result
= emit_intrin_1f_param(ctx
, "llvm.log2",
1631 to_float_type(ctx
, def_type
), src
[0]);
1634 result
= emit_intrin_1f_param(ctx
, "llvm.sqrt",
1635 to_float_type(ctx
, def_type
), src
[0]);
1636 result
= ac_build_fdiv(&ctx
->ac
, ctx
->f32one
, result
);
1639 result
= emit_intrin_2f_param(ctx
, "llvm.pow",
1640 to_float_type(ctx
, def_type
), src
[0], src
[1]);
1643 result
= emit_intrin_2f_param(ctx
, "llvm.maxnum",
1644 to_float_type(ctx
, def_type
), src
[0], src
[1]);
1647 result
= emit_intrin_2f_param(ctx
, "llvm.minnum",
1648 to_float_type(ctx
, def_type
), src
[0], src
[1]);
1651 result
= emit_intrin_3f_param(ctx
, "llvm.fma",
1652 to_float_type(ctx
, def_type
), src
[0], src
[1], src
[2]);
1654 case nir_op_ibitfield_extract
:
1655 result
= emit_bitfield_extract(ctx
, true, src
);
1657 case nir_op_ubitfield_extract
:
1658 result
= emit_bitfield_extract(ctx
, false, src
);
1660 case nir_op_bitfield_insert
:
1661 result
= emit_bitfield_insert(ctx
, src
[0], src
[1], src
[2], src
[3]);
1663 case nir_op_bitfield_reverse
:
1664 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.bitreverse.i32", ctx
->i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1666 case nir_op_bit_count
:
1667 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i32", ctx
->i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1672 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1673 src
[i
] = to_integer(ctx
, src
[i
]);
1674 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
1678 src
[0] = to_float(ctx
, src
[0]);
1679 result
= LLVMBuildFPToSI(ctx
->builder
, src
[0], def_type
, "");
1683 src
[0] = to_float(ctx
, src
[0]);
1684 result
= LLVMBuildFPToUI(ctx
->builder
, src
[0], def_type
, "");
1688 result
= LLVMBuildSIToFP(ctx
->builder
, src
[0], to_float_type(ctx
, def_type
), "");
1692 result
= LLVMBuildUIToFP(ctx
->builder
, src
[0], to_float_type(ctx
, def_type
), "");
1695 result
= LLVMBuildFPExt(ctx
->builder
, src
[0], to_float_type(ctx
, def_type
), "");
1698 result
= LLVMBuildFPTrunc(ctx
->builder
, src
[0], to_float_type(ctx
, def_type
), "");
1702 if (get_elem_bits(ctx
, LLVMTypeOf(src
[0])) < get_elem_bits(ctx
, def_type
))
1703 result
= LLVMBuildZExt(ctx
->builder
, src
[0], def_type
, "");
1705 result
= LLVMBuildTrunc(ctx
->builder
, src
[0], def_type
, "");
1709 if (get_elem_bits(ctx
, LLVMTypeOf(src
[0])) < get_elem_bits(ctx
, def_type
))
1710 result
= LLVMBuildSExt(ctx
->builder
, src
[0], def_type
, "");
1712 result
= LLVMBuildTrunc(ctx
->builder
, src
[0], def_type
, "");
1715 result
= emit_bcsel(ctx
, src
[0], src
[1], src
[2]);
1717 case nir_op_find_lsb
:
1718 result
= emit_find_lsb(ctx
, src
[0]);
1720 case nir_op_ufind_msb
:
1721 result
= emit_ufind_msb(ctx
, src
[0]);
1723 case nir_op_ifind_msb
:
1724 result
= emit_ifind_msb(ctx
, src
[0]);
1726 case nir_op_uadd_carry
:
1727 result
= emit_uint_carry(ctx
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
1729 case nir_op_usub_borrow
:
1730 result
= emit_uint_carry(ctx
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
1733 result
= emit_b2f(ctx
, src
[0]);
1735 case nir_op_fquantize2f16
:
1736 src
[0] = to_float(ctx
, src
[0]);
1737 result
= LLVMBuildFPTrunc(ctx
->builder
, src
[0], ctx
->f16
, "");
1738 /* need to convert back up to f32 */
1739 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
1741 case nir_op_umul_high
:
1742 result
= emit_umul_high(ctx
, src
[0], src
[1]);
1744 case nir_op_imul_high
:
1745 result
= emit_imul_high(ctx
, src
[0], src
[1]);
1747 case nir_op_pack_half_2x16
:
1748 result
= emit_pack_half_2x16(ctx
, src
[0]);
1750 case nir_op_unpack_half_2x16
:
1751 result
= emit_unpack_half_2x16(ctx
, src
[0]);
1755 case nir_op_fddx_fine
:
1756 case nir_op_fddy_fine
:
1757 case nir_op_fddx_coarse
:
1758 case nir_op_fddy_coarse
:
1759 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
1762 fprintf(stderr
, "Unknown NIR alu instr: ");
1763 nir_print_instr(&instr
->instr
, stderr
);
1764 fprintf(stderr
, "\n");
1769 assert(instr
->dest
.dest
.is_ssa
);
1770 result
= to_integer(ctx
, result
);
1771 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.dest
.ssa
,
1776 static void visit_load_const(struct nir_to_llvm_context
*ctx
,
1777 nir_load_const_instr
*instr
)
1779 LLVMValueRef values
[4], value
= NULL
;
1780 LLVMTypeRef element_type
=
1781 LLVMIntTypeInContext(ctx
->context
, instr
->def
.bit_size
);
1783 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1784 switch (instr
->def
.bit_size
) {
1786 values
[i
] = LLVMConstInt(element_type
,
1787 instr
->value
.u32
[i
], false);
1790 values
[i
] = LLVMConstInt(element_type
,
1791 instr
->value
.u64
[i
], false);
1795 "unsupported nir load_const bit_size: %d\n",
1796 instr
->def
.bit_size
);
1800 if (instr
->def
.num_components
> 1) {
1801 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1805 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, value
);
1808 static LLVMValueRef
cast_ptr(struct nir_to_llvm_context
*ctx
, LLVMValueRef ptr
,
1811 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
1812 return LLVMBuildBitCast(ctx
->builder
, ptr
,
1813 LLVMPointerType(type
, addr_space
), "");
1817 get_buffer_size(struct nir_to_llvm_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1820 LLVMBuildExtractElement(ctx
->builder
, descriptor
,
1821 LLVMConstInt(ctx
->i32
, 2, false), "");
1824 if (ctx
->options
->chip_class
>= VI
&& in_elements
) {
1825 /* On VI, the descriptor contains the size in bytes,
1826 * but TXQ must return the size in elements.
1827 * The stride is always non-zero for resources using TXQ.
1829 LLVMValueRef stride
=
1830 LLVMBuildExtractElement(ctx
->builder
, descriptor
,
1831 LLVMConstInt(ctx
->i32
, 1, false), "");
1832 stride
= LLVMBuildLShr(ctx
->builder
, stride
,
1833 LLVMConstInt(ctx
->i32
, 16, false), "");
1834 stride
= LLVMBuildAnd(ctx
->builder
, stride
,
1835 LLVMConstInt(ctx
->i32
, 0x3fff, false), "");
1837 size
= LLVMBuildUDiv(ctx
->builder
, size
, stride
, "");
1843 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
1846 static void build_int_type_name(
1848 char *buf
, unsigned bufsize
)
1850 assert(bufsize
>= 6);
1852 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
1853 snprintf(buf
, bufsize
, "v%ui32",
1854 LLVMGetVectorSize(type
));
1859 static LLVMValueRef
radv_lower_gather4_integer(struct nir_to_llvm_context
*ctx
,
1860 struct ac_image_args
*args
,
1861 nir_tex_instr
*instr
)
1863 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
1864 LLVMValueRef coord
= args
->addr
;
1865 LLVMValueRef half_texel
[2];
1866 LLVMValueRef compare_cube_wa
;
1867 LLVMValueRef result
;
1869 unsigned coord_vgpr_index
= (unsigned)args
->offset
+ (unsigned)args
->compare
;
1873 struct ac_image_args txq_args
= { 0 };
1875 txq_args
.da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
1876 txq_args
.opcode
= ac_image_get_resinfo
;
1877 txq_args
.dmask
= 0xf;
1878 txq_args
.addr
= ctx
->i32zero
;
1879 txq_args
.resource
= args
->resource
;
1880 LLVMValueRef size
= ac_build_image_opcode(&ctx
->ac
, &txq_args
);
1882 for (c
= 0; c
< 2; c
++) {
1883 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
1884 LLVMConstInt(ctx
->i32
, c
, false), "");
1885 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1886 half_texel
[c
] = ac_build_fdiv(&ctx
->ac
, ctx
->f32one
, half_texel
[c
]);
1887 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1888 LLVMConstReal(ctx
->f32
, -0.5), "");
1892 LLVMValueRef orig_coords
= args
->addr
;
1894 for (c
= 0; c
< 2; c
++) {
1896 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, coord_vgpr_index
+ c
, 0);
1897 tmp
= LLVMBuildExtractElement(ctx
->builder
, coord
, index
, "");
1898 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1899 tmp
= LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
1900 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1901 coord
= LLVMBuildInsertElement(ctx
->builder
, coord
, tmp
, index
, "");
1906 * Apparantly cube has issue with integer types that the workaround doesn't solve,
1907 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
1908 * workaround by sampling using a scaled type and converting.
1909 * This is taken from amdgpu-pro shaders.
1911 /* NOTE this produces some ugly code compared to amdgpu-pro,
1912 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
1913 * and then reads them back. -pro generates two selects,
1914 * one s_cmp for the descriptor rewriting
1915 * one v_cmp for the coordinate and result changes.
1917 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1918 LLVMValueRef tmp
, tmp2
;
1920 /* workaround 8/8/8/8 uint/sint cube gather bug */
1921 /* first detect it then change to a scaled read and f2i */
1922 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32one
, "");
1925 /* extract the DATA_FORMAT */
1926 tmp
= ac_build_bfe(&ctx
->ac
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
1927 LLVMConstInt(ctx
->i32
, 6, false), false);
1929 /* is the DATA_FORMAT == 8_8_8_8 */
1930 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
1932 if (stype
== GLSL_TYPE_UINT
)
1933 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
1934 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
1935 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
1937 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
1938 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
1939 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
1941 /* replace the NUM FORMAT in the descriptor */
1942 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT_GFX6
, false), "");
1943 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
1945 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32one
, "");
1947 /* don't modify the coordinates for this case */
1948 coord
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, orig_coords
, coord
, "");
1951 result
= ac_build_image_opcode(&ctx
->ac
, args
);
1953 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1954 LLVMValueRef tmp
, tmp2
;
1956 /* if the cube workaround is in place, f2i the result. */
1957 for (c
= 0; c
< 4; c
++) {
1958 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
1959 if (stype
== GLSL_TYPE_UINT
)
1960 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
1962 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
1963 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1964 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
1965 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
1966 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1967 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
1973 static LLVMValueRef
build_tex_intrinsic(struct nir_to_llvm_context
*ctx
,
1974 nir_tex_instr
*instr
,
1975 struct ac_image_args
*args
)
1977 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
1978 return ac_build_buffer_load_format(&ctx
->ac
,
1981 LLVMConstInt(ctx
->i32
, 0, false),
1985 args
->opcode
= ac_image_sample
;
1986 args
->compare
= instr
->is_shadow
;
1988 switch (instr
->op
) {
1990 case nir_texop_txf_ms
:
1991 case nir_texop_samples_identical
:
1992 args
->opcode
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
? ac_image_load
: ac_image_load_mip
;
1993 args
->compare
= false;
1994 args
->offset
= false;
2003 case nir_texop_query_levels
:
2004 args
->opcode
= ac_image_get_resinfo
;
2007 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
2008 args
->level_zero
= true;
2014 args
->opcode
= ac_image_gather4
;
2015 args
->level_zero
= true;
2018 args
->opcode
= ac_image_get_lod
;
2019 args
->compare
= false;
2020 args
->offset
= false;
2026 if (instr
->op
== nir_texop_tg4
) {
2027 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
2028 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
2029 return radv_lower_gather4_integer(ctx
, args
, instr
);
2032 return ac_build_image_opcode(&ctx
->ac
, args
);
2035 static LLVMValueRef
visit_vulkan_resource_index(struct nir_to_llvm_context
*ctx
,
2036 nir_intrinsic_instr
*instr
)
2038 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
2039 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
2040 unsigned binding
= nir_intrinsic_binding(instr
);
2041 LLVMValueRef desc_ptr
= ctx
->descriptor_sets
[desc_set
];
2042 struct radv_pipeline_layout
*pipeline_layout
= ctx
->options
->layout
;
2043 struct radv_descriptor_set_layout
*layout
= pipeline_layout
->set
[desc_set
].layout
;
2044 unsigned base_offset
= layout
->binding
[binding
].offset
;
2045 LLVMValueRef offset
, stride
;
2047 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
2048 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
2049 unsigned idx
= pipeline_layout
->set
[desc_set
].dynamic_offset_start
+
2050 layout
->binding
[binding
].dynamic_offset_offset
;
2051 desc_ptr
= ctx
->push_constants
;
2052 base_offset
= pipeline_layout
->push_constant_size
+ 16 * idx
;
2053 stride
= LLVMConstInt(ctx
->i32
, 16, false);
2055 stride
= LLVMConstInt(ctx
->i32
, layout
->binding
[binding
].size
, false);
2057 offset
= LLVMConstInt(ctx
->i32
, base_offset
, false);
2058 index
= LLVMBuildMul(ctx
->builder
, index
, stride
, "");
2059 offset
= LLVMBuildAdd(ctx
->builder
, offset
, index
, "");
2061 desc_ptr
= ac_build_gep0(&ctx
->ac
, desc_ptr
, offset
);
2062 desc_ptr
= cast_ptr(ctx
, desc_ptr
, ctx
->v4i32
);
2063 LLVMSetMetadata(desc_ptr
, ctx
->uniform_md_kind
, ctx
->empty_md
);
2065 return LLVMBuildLoad(ctx
->builder
, desc_ptr
, "");
2068 static LLVMValueRef
visit_load_push_constant(struct nir_to_llvm_context
*ctx
,
2069 nir_intrinsic_instr
*instr
)
2071 LLVMValueRef ptr
, addr
;
2073 addr
= LLVMConstInt(ctx
->i32
, nir_intrinsic_base(instr
), 0);
2074 addr
= LLVMBuildAdd(ctx
->builder
, addr
, get_src(ctx
, instr
->src
[0]), "");
2076 ptr
= ac_build_gep0(&ctx
->ac
, ctx
->push_constants
, addr
);
2077 ptr
= cast_ptr(ctx
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
2079 return LLVMBuildLoad(ctx
->builder
, ptr
, "");
2082 static LLVMValueRef
visit_get_buffer_size(struct nir_to_llvm_context
*ctx
,
2083 nir_intrinsic_instr
*instr
)
2085 LLVMValueRef desc
= get_src(ctx
, instr
->src
[0]);
2087 return get_buffer_size(ctx
, desc
, false);
2089 static void visit_store_ssbo(struct nir_to_llvm_context
*ctx
,
2090 nir_intrinsic_instr
*instr
)
2092 const char *store_name
;
2093 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
2094 LLVMTypeRef data_type
= ctx
->f32
;
2095 int elem_size_mult
= get_elem_bits(ctx
, LLVMTypeOf(src_data
)) / 32;
2096 int components_32bit
= elem_size_mult
* instr
->num_components
;
2097 unsigned writemask
= nir_intrinsic_write_mask(instr
);
2098 LLVMValueRef base_data
, base_offset
;
2099 LLVMValueRef params
[6];
2101 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
2102 ctx
->shader_info
->fs
.writes_memory
= true;
2104 params
[1] = get_src(ctx
, instr
->src
[1]);
2105 params
[2] = LLVMConstInt(ctx
->i32
, 0, false); /* vindex */
2106 params
[4] = ctx
->i1false
; /* glc */
2107 params
[5] = ctx
->i1false
; /* slc */
2109 if (components_32bit
> 1)
2110 data_type
= LLVMVectorType(ctx
->f32
, components_32bit
);
2112 base_data
= to_float(ctx
, src_data
);
2113 base_data
= trim_vector(ctx
, base_data
, instr
->num_components
);
2114 base_data
= LLVMBuildBitCast(ctx
->builder
, base_data
,
2116 base_offset
= get_src(ctx
, instr
->src
[2]); /* voffset */
2120 LLVMValueRef offset
;
2122 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
2124 /* Due to an LLVM limitation, split 3-element writes
2125 * into a 2-element and a 1-element write. */
2127 writemask
|= 1 << (start
+ 2);
2131 start
*= elem_size_mult
;
2132 count
*= elem_size_mult
;
2135 writemask
|= ((1u << (count
- 4)) - 1u) << (start
+ 4);
2140 store_name
= "llvm.amdgcn.buffer.store.v4f32";
2142 } else if (count
== 2) {
2143 tmp
= LLVMBuildExtractElement(ctx
->builder
,
2144 base_data
, LLVMConstInt(ctx
->i32
, start
, false), "");
2145 data
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), tmp
,
2148 tmp
= LLVMBuildExtractElement(ctx
->builder
,
2149 base_data
, LLVMConstInt(ctx
->i32
, start
+ 1, false), "");
2150 data
= LLVMBuildInsertElement(ctx
->builder
, data
, tmp
,
2152 store_name
= "llvm.amdgcn.buffer.store.v2f32";
2156 if (get_llvm_num_components(base_data
) > 1)
2157 data
= LLVMBuildExtractElement(ctx
->builder
, base_data
,
2158 LLVMConstInt(ctx
->i32
, start
, false), "");
2161 store_name
= "llvm.amdgcn.buffer.store.f32";
2164 offset
= base_offset
;
2166 offset
= LLVMBuildAdd(ctx
->builder
, offset
, LLVMConstInt(ctx
->i32
, start
* 4, false), "");
2170 ac_build_intrinsic(&ctx
->ac
, store_name
,
2171 ctx
->voidt
, params
, 6, 0);
2175 static LLVMValueRef
visit_atomic_ssbo(struct nir_to_llvm_context
*ctx
,
2176 nir_intrinsic_instr
*instr
)
2179 LLVMValueRef params
[6];
2181 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
2182 ctx
->shader_info
->fs
.writes_memory
= true;
2184 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
2185 params
[arg_count
++] = llvm_extract_elem(ctx
, get_src(ctx
, instr
->src
[3]), 0);
2187 params
[arg_count
++] = llvm_extract_elem(ctx
, get_src(ctx
, instr
->src
[2]), 0);
2188 params
[arg_count
++] = get_src(ctx
, instr
->src
[0]);
2189 params
[arg_count
++] = LLVMConstInt(ctx
->i32
, 0, false); /* vindex */
2190 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
2191 params
[arg_count
++] = ctx
->i1false
; /* slc */
2193 switch (instr
->intrinsic
) {
2194 case nir_intrinsic_ssbo_atomic_add
:
2195 name
= "llvm.amdgcn.buffer.atomic.add";
2197 case nir_intrinsic_ssbo_atomic_imin
:
2198 name
= "llvm.amdgcn.buffer.atomic.smin";
2200 case nir_intrinsic_ssbo_atomic_umin
:
2201 name
= "llvm.amdgcn.buffer.atomic.umin";
2203 case nir_intrinsic_ssbo_atomic_imax
:
2204 name
= "llvm.amdgcn.buffer.atomic.smax";
2206 case nir_intrinsic_ssbo_atomic_umax
:
2207 name
= "llvm.amdgcn.buffer.atomic.umax";
2209 case nir_intrinsic_ssbo_atomic_and
:
2210 name
= "llvm.amdgcn.buffer.atomic.and";
2212 case nir_intrinsic_ssbo_atomic_or
:
2213 name
= "llvm.amdgcn.buffer.atomic.or";
2215 case nir_intrinsic_ssbo_atomic_xor
:
2216 name
= "llvm.amdgcn.buffer.atomic.xor";
2218 case nir_intrinsic_ssbo_atomic_exchange
:
2219 name
= "llvm.amdgcn.buffer.atomic.swap";
2221 case nir_intrinsic_ssbo_atomic_comp_swap
:
2222 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
2228 return ac_build_intrinsic(&ctx
->ac
, name
, ctx
->i32
, params
, arg_count
, 0);
2231 static LLVMValueRef
visit_load_buffer(struct nir_to_llvm_context
*ctx
,
2232 nir_intrinsic_instr
*instr
)
2234 LLVMValueRef results
[2];
2235 int load_components
;
2236 int num_components
= instr
->num_components
;
2237 if (instr
->dest
.ssa
.bit_size
== 64)
2238 num_components
*= 2;
2240 for (int i
= 0; i
< num_components
; i
+= load_components
) {
2241 load_components
= MIN2(num_components
- i
, 4);
2242 const char *load_name
;
2243 LLVMTypeRef data_type
= ctx
->f32
;
2244 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, i
* 4, false);
2245 offset
= LLVMBuildAdd(ctx
->builder
, get_src(ctx
, instr
->src
[1]), offset
, "");
2247 if (load_components
== 3)
2248 data_type
= LLVMVectorType(ctx
->f32
, 4);
2249 else if (load_components
> 1)
2250 data_type
= LLVMVectorType(ctx
->f32
, load_components
);
2252 if (load_components
>= 3)
2253 load_name
= "llvm.amdgcn.buffer.load.v4f32";
2254 else if (load_components
== 2)
2255 load_name
= "llvm.amdgcn.buffer.load.v2f32";
2256 else if (load_components
== 1)
2257 load_name
= "llvm.amdgcn.buffer.load.f32";
2259 unreachable("unhandled number of components");
2261 LLVMValueRef params
[] = {
2262 get_src(ctx
, instr
->src
[0]),
2263 LLVMConstInt(ctx
->i32
, 0, false),
2269 results
[i
] = ac_build_intrinsic(&ctx
->ac
, load_name
, data_type
, params
, 5, 0);
2273 LLVMValueRef ret
= results
[0];
2274 if (num_components
> 4 || num_components
== 3) {
2275 LLVMValueRef masks
[] = {
2276 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
2277 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false),
2278 LLVMConstInt(ctx
->i32
, 4, false), LLVMConstInt(ctx
->i32
, 5, false),
2279 LLVMConstInt(ctx
->i32
, 6, false), LLVMConstInt(ctx
->i32
, 7, false)
2282 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
2283 ret
= LLVMBuildShuffleVector(ctx
->builder
, results
[0],
2284 results
[num_components
> 4 ? 1 : 0], swizzle
, "");
2287 return LLVMBuildBitCast(ctx
->builder
, ret
,
2288 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2291 static LLVMValueRef
visit_load_ubo_buffer(struct nir_to_llvm_context
*ctx
,
2292 nir_intrinsic_instr
*instr
)
2294 LLVMValueRef results
[8], ret
;
2295 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
2296 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
2297 int num_components
= instr
->num_components
;
2299 rsrc
= LLVMBuildBitCast(ctx
->builder
, rsrc
, LLVMVectorType(ctx
->i8
, 16), "");
2301 if (instr
->dest
.ssa
.bit_size
== 64)
2302 num_components
*= 2;
2304 for (unsigned i
= 0; i
< num_components
; ++i
) {
2305 LLVMValueRef params
[] = {
2307 LLVMBuildAdd(ctx
->builder
, LLVMConstInt(ctx
->i32
, 4 * i
, 0),
2310 results
[i
] = ac_build_intrinsic(&ctx
->ac
, "llvm.SI.load.const", ctx
->f32
,
2312 AC_FUNC_ATTR_READNONE
|
2313 AC_FUNC_ATTR_LEGACY
);
2317 ret
= ac_build_gather_values(&ctx
->ac
, results
, instr
->num_components
);
2318 return LLVMBuildBitCast(ctx
->builder
, ret
,
2319 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2323 radv_get_deref_offset(struct nir_to_llvm_context
*ctx
, nir_deref_var
*deref
,
2324 bool vs_in
, unsigned *vertex_index_out
,
2325 LLVMValueRef
*vertex_index_ref
,
2326 unsigned *const_out
, LLVMValueRef
*indir_out
)
2328 unsigned const_offset
= 0;
2329 nir_deref
*tail
= &deref
->deref
;
2330 LLVMValueRef offset
= NULL
;
2332 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
2334 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2335 if (vertex_index_out
)
2336 *vertex_index_out
= deref_array
->base_offset
;
2338 if (vertex_index_ref
) {
2339 LLVMValueRef vtx
= LLVMConstInt(ctx
->i32
, deref_array
->base_offset
, false);
2340 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
2341 vtx
= LLVMBuildAdd(ctx
->builder
, vtx
, get_src(ctx
, deref_array
->indirect
), "");
2343 *vertex_index_ref
= vtx
;
2347 if (deref
->var
->data
.compact
) {
2348 assert(tail
->child
->deref_type
== nir_deref_type_array
);
2349 assert(glsl_type_is_scalar(glsl_without_array(deref
->var
->type
)));
2350 nir_deref_array
*deref_array
= nir_deref_as_array(tail
->child
);
2351 /* We always lower indirect dereferences for "compact" array vars. */
2352 assert(deref_array
->deref_array_type
== nir_deref_array_type_direct
);
2354 const_offset
= deref_array
->base_offset
;
2358 while (tail
->child
!= NULL
) {
2359 const struct glsl_type
*parent_type
= tail
->type
;
2362 if (tail
->deref_type
== nir_deref_type_array
) {
2363 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2364 LLVMValueRef index
, stride
, local_offset
;
2365 unsigned size
= glsl_count_attribute_slots(tail
->type
, vs_in
);
2367 const_offset
+= size
* deref_array
->base_offset
;
2368 if (deref_array
->deref_array_type
== nir_deref_array_type_direct
)
2371 assert(deref_array
->deref_array_type
== nir_deref_array_type_indirect
);
2372 index
= get_src(ctx
, deref_array
->indirect
);
2373 stride
= LLVMConstInt(ctx
->i32
, size
, 0);
2374 local_offset
= LLVMBuildMul(ctx
->builder
, stride
, index
, "");
2377 offset
= LLVMBuildAdd(ctx
->builder
, offset
, local_offset
, "");
2379 offset
= local_offset
;
2380 } else if (tail
->deref_type
== nir_deref_type_struct
) {
2381 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
2383 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
2384 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
2385 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
2388 unreachable("unsupported deref type");
2392 if (const_offset
&& offset
)
2393 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
2394 LLVMConstInt(ctx
->i32
, const_offset
, 0),
2397 *const_out
= const_offset
;
2398 *indir_out
= offset
;
2402 lds_load(struct nir_to_llvm_context
*ctx
,
2403 LLVMValueRef dw_addr
)
2406 value
= ac_build_indexed_load(&ctx
->ac
, ctx
->lds
, dw_addr
, false);
2411 lds_store(struct nir_to_llvm_context
*ctx
,
2412 LLVMValueRef dw_addr
, LLVMValueRef value
)
2414 value
= LLVMBuildBitCast(ctx
->builder
, value
, ctx
->i32
, "");
2415 ac_build_indexed_store(&ctx
->ac
, ctx
->lds
,
2419 /* The offchip buffer layout for TCS->TES is
2421 * - attribute 0 of patch 0 vertex 0
2422 * - attribute 0 of patch 0 vertex 1
2423 * - attribute 0 of patch 0 vertex 2
2425 * - attribute 0 of patch 1 vertex 0
2426 * - attribute 0 of patch 1 vertex 1
2428 * - attribute 1 of patch 0 vertex 0
2429 * - attribute 1 of patch 0 vertex 1
2431 * - per patch attribute 0 of patch 0
2432 * - per patch attribute 0 of patch 1
2435 * Note that every attribute has 4 components.
2437 static LLVMValueRef
get_tcs_tes_buffer_address(struct nir_to_llvm_context
*ctx
,
2438 LLVMValueRef vertex_index
,
2439 LLVMValueRef param_index
)
2441 LLVMValueRef base_addr
, vertices_per_patch
, num_patches
, total_vertices
;
2442 LLVMValueRef param_stride
, constant16
;
2443 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
2445 vertices_per_patch
= unpack_param(ctx
, ctx
->tcs_offchip_layout
, 9, 6);
2446 num_patches
= unpack_param(ctx
, ctx
->tcs_offchip_layout
, 0, 9);
2447 total_vertices
= LLVMBuildMul(ctx
->builder
, vertices_per_patch
,
2450 constant16
= LLVMConstInt(ctx
->i32
, 16, false);
2452 base_addr
= LLVMBuildMul(ctx
->builder
, rel_patch_id
,
2453 vertices_per_patch
, "");
2455 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2458 param_stride
= total_vertices
;
2460 base_addr
= rel_patch_id
;
2461 param_stride
= num_patches
;
2464 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2465 LLVMBuildMul(ctx
->builder
, param_index
,
2466 param_stride
, ""), "");
2468 base_addr
= LLVMBuildMul(ctx
->builder
, base_addr
, constant16
, "");
2470 if (!vertex_index
) {
2471 LLVMValueRef patch_data_offset
=
2472 unpack_param(ctx
, ctx
->tcs_offchip_layout
, 16, 16);
2474 base_addr
= LLVMBuildAdd(ctx
->builder
, base_addr
,
2475 patch_data_offset
, "");
2480 static LLVMValueRef
get_tcs_tes_buffer_address_params(struct nir_to_llvm_context
*ctx
,
2482 unsigned const_index
,
2484 LLVMValueRef vertex_index
,
2485 LLVMValueRef indir_index
)
2487 LLVMValueRef param_index
;
2490 param_index
= LLVMBuildAdd(ctx
->builder
, LLVMConstInt(ctx
->i32
, param
, false),
2493 if (const_index
&& !is_compact
)
2494 param
+= const_index
;
2495 param_index
= LLVMConstInt(ctx
->i32
, param
, false);
2497 return get_tcs_tes_buffer_address(ctx
, vertex_index
, param_index
);
2501 mark_tess_output(struct nir_to_llvm_context
*ctx
,
2502 bool is_patch
, uint32_t param
)
2506 ctx
->tess_patch_outputs_written
|= (1ull << param
);
2508 ctx
->tess_outputs_written
|= (1ull << param
);
2512 get_dw_address(struct nir_to_llvm_context
*ctx
,
2513 LLVMValueRef dw_addr
,
2515 unsigned const_index
,
2516 bool compact_const_index
,
2517 LLVMValueRef vertex_index
,
2518 LLVMValueRef stride
,
2519 LLVMValueRef indir_index
)
2524 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2525 LLVMBuildMul(ctx
->builder
,
2531 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2532 LLVMBuildMul(ctx
->builder
, indir_index
,
2533 LLVMConstInt(ctx
->i32
, 4, false), ""), "");
2534 else if (const_index
&& !compact_const_index
)
2535 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2536 LLVMConstInt(ctx
->i32
, const_index
, false), "");
2538 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2539 LLVMConstInt(ctx
->i32
, param
* 4, false), "");
2541 if (const_index
&& compact_const_index
)
2542 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2543 LLVMConstInt(ctx
->i32
, const_index
, false), "");
2548 load_tcs_input(struct nir_to_llvm_context
*ctx
,
2549 nir_intrinsic_instr
*instr
)
2551 LLVMValueRef dw_addr
, stride
;
2552 unsigned const_index
;
2553 LLVMValueRef vertex_index
;
2554 LLVMValueRef indir_index
;
2556 LLVMValueRef value
[4], result
;
2557 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2558 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2559 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2560 radv_get_deref_offset(ctx
, instr
->variables
[0],
2561 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2562 &const_index
, &indir_index
);
2564 stride
= unpack_param(ctx
, ctx
->tcs_in_layout
, 13, 8);
2565 dw_addr
= get_tcs_in_current_patch_offset(ctx
);
2566 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2569 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
2570 value
[i
] = lds_load(ctx
, dw_addr
);
2571 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2574 result
= ac_build_gather_values(&ctx
->ac
, value
, instr
->num_components
);
2575 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2580 load_tcs_output(struct nir_to_llvm_context
*ctx
,
2581 nir_intrinsic_instr
*instr
)
2583 LLVMValueRef dw_addr
, stride
;
2584 LLVMValueRef value
[4], result
;
2585 LLVMValueRef vertex_index
= NULL
;
2586 LLVMValueRef indir_index
= NULL
;
2587 unsigned const_index
= 0;
2589 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2590 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2591 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2592 radv_get_deref_offset(ctx
, instr
->variables
[0],
2593 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2594 &const_index
, &indir_index
);
2596 if (!instr
->variables
[0]->var
->data
.patch
) {
2597 stride
= unpack_param(ctx
, ctx
->tcs_out_layout
, 13, 8);
2598 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
2600 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
2603 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2606 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
2607 value
[i
] = lds_load(ctx
, dw_addr
);
2608 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2611 result
= ac_build_gather_values(&ctx
->ac
, value
, instr
->num_components
);
2612 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2617 store_tcs_output(struct nir_to_llvm_context
*ctx
,
2618 nir_intrinsic_instr
*instr
,
2622 LLVMValueRef stride
, dw_addr
;
2623 LLVMValueRef buf_addr
= NULL
;
2624 LLVMValueRef vertex_index
= NULL
;
2625 LLVMValueRef indir_index
= NULL
;
2626 unsigned const_index
= 0;
2628 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2629 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2631 radv_get_deref_offset(ctx
, instr
->variables
[0],
2632 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2633 &const_index
, &indir_index
);
2635 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2636 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CLIP_DIST0
&&
2637 is_compact
&& const_index
> 3) {
2642 if (!instr
->variables
[0]->var
->data
.patch
) {
2643 stride
= unpack_param(ctx
, ctx
->tcs_out_layout
, 13, 8);
2644 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
2646 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
2649 mark_tess_output(ctx
, instr
->variables
[0]->var
->data
.patch
, param
);
2651 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
2653 buf_addr
= get_tcs_tes_buffer_address_params(ctx
, param
, const_index
, is_compact
,
2654 vertex_index
, indir_index
);
2656 unsigned base
= is_compact
? const_index
: 0;
2657 for (unsigned chan
= 0; chan
< 8; chan
++) {
2658 bool is_tess_factor
= false;
2659 if (!(writemask
& (1 << chan
)))
2661 LLVMValueRef value
= llvm_extract_elem(ctx
, src
, chan
);
2663 lds_store(ctx
, dw_addr
, value
);
2665 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_TESS_LEVEL_INNER
||
2666 instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_TESS_LEVEL_OUTER
)
2667 is_tess_factor
= true;
2669 if (!is_tess_factor
&& writemask
!= 0xF)
2670 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, value
, 1,
2671 buf_addr
, ctx
->oc_lds
,
2672 4 * (base
+ chan
), 1, 0, true, false);
2674 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
,
2678 if (writemask
== 0xF) {
2679 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, src
, 4,
2680 buf_addr
, ctx
->oc_lds
,
2681 (base
* 4), 1, 0, true, false);
2686 load_tes_input(struct nir_to_llvm_context
*ctx
,
2687 nir_intrinsic_instr
*instr
)
2689 LLVMValueRef buf_addr
;
2690 LLVMValueRef result
;
2691 LLVMValueRef vertex_index
= NULL
;
2692 LLVMValueRef indir_index
= NULL
;
2693 unsigned const_index
= 0;
2695 const bool per_vertex
= nir_is_per_vertex_io(instr
->variables
[0]->var
, ctx
->stage
);
2696 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
2698 radv_get_deref_offset(ctx
, instr
->variables
[0],
2699 false, NULL
, per_vertex
? &vertex_index
: NULL
,
2700 &const_index
, &indir_index
);
2701 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2702 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CLIP_DIST0
&&
2703 is_compact
&& const_index
> 3) {
2707 buf_addr
= get_tcs_tes_buffer_address_params(ctx
, param
, const_index
,
2708 is_compact
, vertex_index
, indir_index
);
2710 result
= ac_build_buffer_load(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, instr
->num_components
, NULL
,
2711 buf_addr
, ctx
->oc_lds
, is_compact
? (4 * const_index
) : 0, 1, 0, true);
2712 result
= trim_vector(ctx
, result
, instr
->num_components
);
2713 result
= LLVMBuildBitCast(ctx
->builder
, result
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2718 load_gs_input(struct nir_to_llvm_context
*ctx
,
2719 nir_intrinsic_instr
*instr
)
2721 LLVMValueRef indir_index
, vtx_offset
;
2722 unsigned const_index
;
2723 LLVMValueRef args
[9];
2724 unsigned param
, vtx_offset_param
;
2725 LLVMValueRef value
[4], result
;
2726 unsigned vertex_index
;
2727 radv_get_deref_offset(ctx
, instr
->variables
[0],
2728 false, &vertex_index
, NULL
,
2729 &const_index
, &indir_index
);
2730 vtx_offset_param
= vertex_index
;
2731 assert(vtx_offset_param
< 6);
2732 vtx_offset
= LLVMBuildMul(ctx
->builder
, ctx
->gs_vtx_offset
[vtx_offset_param
],
2733 LLVMConstInt(ctx
->i32
, 4, false), "");
2735 param
= shader_io_get_unique_index(instr
->variables
[0]->var
->data
.location
);
2736 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
2738 args
[0] = ctx
->esgs_ring
;
2739 args
[1] = vtx_offset
;
2740 args
[2] = LLVMConstInt(ctx
->i32
, (param
* 4 + i
+ const_index
) * 256, false);
2741 args
[3] = ctx
->i32zero
;
2742 args
[4] = ctx
->i32one
; /* OFFEN */
2743 args
[5] = ctx
->i32zero
; /* IDXEN */
2744 args
[6] = ctx
->i32one
; /* GLC */
2745 args
[7] = ctx
->i32zero
; /* SLC */
2746 args
[8] = ctx
->i32zero
; /* TFE */
2748 value
[i
] = ac_build_intrinsic(&ctx
->ac
, "llvm.SI.buffer.load.dword.i32.i32",
2750 AC_FUNC_ATTR_READONLY
|
2751 AC_FUNC_ATTR_LEGACY
);
2753 result
= ac_build_gather_values(&ctx
->ac
, value
, instr
->num_components
);
2758 static LLVMValueRef
visit_load_var(struct nir_to_llvm_context
*ctx
,
2759 nir_intrinsic_instr
*instr
)
2761 LLVMValueRef values
[8];
2762 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2763 int ve
= instr
->dest
.ssa
.num_components
;
2764 LLVMValueRef indir_index
;
2766 unsigned const_index
;
2767 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
2768 instr
->variables
[0]->var
->data
.mode
== nir_var_shader_in
;
2769 radv_get_deref_offset(ctx
, instr
->variables
[0], vs_in
, NULL
, NULL
,
2770 &const_index
, &indir_index
);
2772 if (instr
->dest
.ssa
.bit_size
== 64)
2775 switch (instr
->variables
[0]->var
->data
.mode
) {
2776 case nir_var_shader_in
:
2777 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
2778 return load_tcs_input(ctx
, instr
);
2779 if (ctx
->stage
== MESA_SHADER_TESS_EVAL
)
2780 return load_tes_input(ctx
, instr
);
2781 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
2782 return load_gs_input(ctx
, instr
);
2784 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2786 unsigned count
= glsl_count_attribute_slots(
2787 instr
->variables
[0]->var
->type
,
2788 ctx
->stage
== MESA_SHADER_VERTEX
);
2790 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2791 &ctx
->ac
, ctx
->inputs
+ idx
+ chan
, count
,
2794 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2798 values
[chan
] = ctx
->inputs
[idx
+ chan
+ const_index
* 4];
2802 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2804 unsigned count
= glsl_count_attribute_slots(
2805 instr
->variables
[0]->var
->type
, false);
2807 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2808 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2811 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2815 values
[chan
] = LLVMBuildLoad(ctx
->builder
, ctx
->locals
[idx
+ chan
+ const_index
* 4], "");
2819 case nir_var_shader_out
:
2820 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
2821 return load_tcs_output(ctx
, instr
);
2822 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2824 unsigned count
= glsl_count_attribute_slots(
2825 instr
->variables
[0]->var
->type
, false);
2827 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2828 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
2831 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2835 values
[chan
] = LLVMBuildLoad(ctx
->builder
,
2836 ctx
->outputs
[idx
+ chan
+ const_index
* 4],
2841 case nir_var_shared
: {
2842 LLVMValueRef ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
2843 LLVMValueRef derived_ptr
;
2846 indir_index
= LLVMBuildMul(ctx
->builder
, indir_index
, LLVMConstInt(ctx
->i32
, 4, false), "");
2848 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2849 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, chan
, false);
2851 index
= LLVMBuildAdd(ctx
->builder
, index
, indir_index
, "");
2852 derived_ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, "");
2854 values
[chan
] = LLVMBuildLoad(ctx
->builder
, derived_ptr
, "");
2859 unreachable("unhandle variable mode");
2861 ret
= ac_build_gather_values(&ctx
->ac
, values
, ve
);
2862 return LLVMBuildBitCast(ctx
->builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2866 visit_store_var(struct nir_to_llvm_context
*ctx
,
2867 nir_intrinsic_instr
*instr
)
2869 LLVMValueRef temp_ptr
, value
;
2870 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2871 LLVMValueRef src
= to_float(ctx
, get_src(ctx
, instr
->src
[0]));
2872 int writemask
= instr
->const_index
[0];
2873 LLVMValueRef indir_index
;
2874 unsigned const_index
;
2875 radv_get_deref_offset(ctx
, instr
->variables
[0], false,
2876 NULL
, NULL
, &const_index
, &indir_index
);
2878 if (get_elem_bits(ctx
, LLVMTypeOf(src
)) == 64) {
2879 int old_writemask
= writemask
;
2881 src
= LLVMBuildBitCast(ctx
->builder
, src
,
2882 LLVMVectorType(ctx
->f32
, get_llvm_num_components(src
) * 2),
2886 for (unsigned chan
= 0; chan
< 4; chan
++) {
2887 if (old_writemask
& (1 << chan
))
2888 writemask
|= 3u << (2 * chan
);
2892 switch (instr
->variables
[0]->var
->data
.mode
) {
2893 case nir_var_shader_out
:
2895 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2896 store_tcs_output(ctx
, instr
, src
, writemask
);
2900 for (unsigned chan
= 0; chan
< 8; chan
++) {
2902 if (!(writemask
& (1 << chan
)))
2905 value
= llvm_extract_elem(ctx
, src
, chan
);
2907 if (instr
->variables
[0]->var
->data
.compact
)
2910 unsigned count
= glsl_count_attribute_slots(
2911 instr
->variables
[0]->var
->type
, false);
2913 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2914 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
2917 if (get_llvm_num_components(tmp_vec
) > 1) {
2918 tmp_vec
= LLVMBuildInsertElement(ctx
->builder
, tmp_vec
,
2919 value
, indir_index
, "");
2922 build_store_values_extended(ctx
, ctx
->outputs
+ idx
+ chan
,
2923 count
, stride
, tmp_vec
);
2926 temp_ptr
= ctx
->outputs
[idx
+ chan
+ const_index
* stride
];
2928 LLVMBuildStore(ctx
->builder
, value
, temp_ptr
);
2933 for (unsigned chan
= 0; chan
< 8; chan
++) {
2934 if (!(writemask
& (1 << chan
)))
2937 value
= llvm_extract_elem(ctx
, src
, chan
);
2939 unsigned count
= glsl_count_attribute_slots(
2940 instr
->variables
[0]->var
->type
, false);
2942 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2943 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2946 tmp_vec
= LLVMBuildInsertElement(ctx
->builder
, tmp_vec
,
2947 value
, indir_index
, "");
2948 build_store_values_extended(ctx
, ctx
->locals
+ idx
+ chan
,
2951 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
2953 LLVMBuildStore(ctx
->builder
, value
, temp_ptr
);
2957 case nir_var_shared
: {
2958 LLVMValueRef ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
2961 indir_index
= LLVMBuildMul(ctx
->builder
, indir_index
, LLVMConstInt(ctx
->i32
, 4, false), "");
2963 for (unsigned chan
= 0; chan
< 8; chan
++) {
2964 if (!(writemask
& (1 << chan
)))
2966 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, chan
, false);
2967 LLVMValueRef derived_ptr
;
2970 index
= LLVMBuildAdd(ctx
->builder
, index
, indir_index
, "");
2972 value
= llvm_extract_elem(ctx
, src
, chan
);
2973 derived_ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, "");
2974 LLVMBuildStore(ctx
->builder
,
2975 to_integer(ctx
, value
), derived_ptr
);
2984 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
2987 case GLSL_SAMPLER_DIM_BUF
:
2989 case GLSL_SAMPLER_DIM_1D
:
2990 return array
? 2 : 1;
2991 case GLSL_SAMPLER_DIM_2D
:
2992 return array
? 3 : 2;
2993 case GLSL_SAMPLER_DIM_MS
:
2994 return array
? 4 : 3;
2995 case GLSL_SAMPLER_DIM_3D
:
2996 case GLSL_SAMPLER_DIM_CUBE
:
2998 case GLSL_SAMPLER_DIM_RECT
:
2999 case GLSL_SAMPLER_DIM_SUBPASS
:
3001 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
3011 /* Adjust the sample index according to FMASK.
3013 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3014 * which is the identity mapping. Each nibble says which physical sample
3015 * should be fetched to get that sample.
3017 * For example, 0x11111100 means there are only 2 samples stored and
3018 * the second sample covers 3/4 of the pixel. When reading samples 0
3019 * and 1, return physical sample 0 (determined by the first two 0s
3020 * in FMASK), otherwise return physical sample 1.
3022 * The sample index should be adjusted as follows:
3023 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
3025 static LLVMValueRef
adjust_sample_index_using_fmask(struct nir_to_llvm_context
*ctx
,
3026 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
3027 LLVMValueRef coord_z
,
3028 LLVMValueRef sample_index
,
3029 LLVMValueRef fmask_desc_ptr
)
3031 LLVMValueRef fmask_load_address
[4];
3034 fmask_load_address
[0] = coord_x
;
3035 fmask_load_address
[1] = coord_y
;
3037 fmask_load_address
[2] = coord_z
;
3038 fmask_load_address
[3] = LLVMGetUndef(ctx
->i32
);
3041 struct ac_image_args args
= {0};
3043 args
.opcode
= ac_image_load
;
3044 args
.da
= coord_z
? true : false;
3045 args
.resource
= fmask_desc_ptr
;
3047 args
.addr
= ac_build_gather_values(&ctx
->ac
, fmask_load_address
, coord_z
? 4 : 2);
3049 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
3051 res
= to_integer(ctx
, res
);
3052 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
3053 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
3055 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
3059 LLVMValueRef sample_index4
=
3060 LLVMBuildMul(ctx
->builder
, sample_index
, four
, "");
3061 LLVMValueRef shifted_fmask
=
3062 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
3063 LLVMValueRef final_sample
=
3064 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
3066 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3067 * resource descriptor is 0 (invalid),
3069 LLVMValueRef fmask_desc
=
3070 LLVMBuildBitCast(ctx
->builder
, fmask_desc_ptr
,
3073 LLVMValueRef fmask_word1
=
3074 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
3077 LLVMValueRef word1_is_nonzero
=
3078 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3079 fmask_word1
, ctx
->i32zero
, "");
3081 /* Replace the MSAA sample index. */
3083 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
3084 final_sample
, sample_index
, "");
3085 return sample_index
;
3088 static LLVMValueRef
get_image_coords(struct nir_to_llvm_context
*ctx
,
3089 nir_intrinsic_instr
*instr
)
3091 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
3092 if(instr
->variables
[0]->deref
.child
)
3093 type
= instr
->variables
[0]->deref
.child
->type
;
3095 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
3096 LLVMValueRef coords
[4];
3097 LLVMValueRef masks
[] = {
3098 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
3099 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false),
3102 LLVMValueRef sample_index
= llvm_extract_elem(ctx
, get_src(ctx
, instr
->src
[1]), 0);
3105 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
3106 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3107 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3108 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
3109 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3111 count
= image_type_to_components_count(dim
,
3112 glsl_sampler_type_is_array(type
));
3115 LLVMValueRef fmask_load_address
[3];
3118 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[0], "");
3119 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[1], "");
3120 if (glsl_sampler_type_is_array(type
))
3121 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[2], "");
3123 fmask_load_address
[2] = NULL
;
3125 for (chan
= 0; chan
< 2; ++chan
)
3126 fmask_load_address
[chan
] = LLVMBuildAdd(ctx
->builder
, fmask_load_address
[chan
], LLVMBuildFPToUI(ctx
->builder
, ctx
->frag_pos
[chan
], ctx
->i32
, ""), "");
3128 sample_index
= adjust_sample_index_using_fmask(ctx
,
3129 fmask_load_address
[0],
3130 fmask_load_address
[1],
3131 fmask_load_address
[2],
3133 get_sampler_desc(ctx
, instr
->variables
[0], DESC_FMASK
));
3136 if (instr
->src
[0].ssa
->num_components
)
3137 res
= LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[0], "");
3144 for (chan
= 0; chan
< count
; ++chan
) {
3145 coords
[chan
] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[chan
], "");
3149 for (chan
= 0; chan
< count
; ++chan
)
3150 coords
[chan
] = LLVMBuildAdd(ctx
->builder
, coords
[chan
], LLVMBuildFPToUI(ctx
->builder
, ctx
->frag_pos
[chan
], ctx
->i32
, ""), "");
3153 coords
[count
] = sample_index
;
3158 coords
[3] = LLVMGetUndef(ctx
->i32
);
3161 res
= ac_build_gather_values(&ctx
->ac
, coords
, count
);
3166 static LLVMValueRef
visit_image_load(struct nir_to_llvm_context
*ctx
,
3167 nir_intrinsic_instr
*instr
)
3169 LLVMValueRef params
[7];
3171 char intrinsic_name
[64];
3172 const nir_variable
*var
= instr
->variables
[0]->var
;
3173 const struct glsl_type
*type
= var
->type
;
3174 if(instr
->variables
[0]->deref
.child
)
3175 type
= instr
->variables
[0]->deref
.child
->type
;
3177 type
= glsl_without_array(type
);
3178 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3179 params
[0] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
3180 params
[1] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
3181 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
3182 params
[2] = LLVMConstInt(ctx
->i32
, 0, false); /* voffset */
3183 params
[3] = ctx
->i1false
; /* glc */
3184 params
[4] = ctx
->i1false
; /* slc */
3185 res
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.load.format.v4f32", ctx
->v4f32
,
3188 res
= trim_vector(ctx
, res
, instr
->dest
.ssa
.num_components
);
3189 res
= to_integer(ctx
, res
);
3191 bool is_da
= glsl_sampler_type_is_array(type
) ||
3192 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3193 LLVMValueRef da
= is_da
? ctx
->i1true
: ctx
->i1false
;
3194 LLVMValueRef glc
= ctx
->i1false
;
3195 LLVMValueRef slc
= ctx
->i1false
;
3197 params
[0] = get_image_coords(ctx
, instr
);
3198 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
3199 params
[2] = LLVMConstInt(ctx
->i32
, 15, false); /* dmask */
3200 if (HAVE_LLVM
<= 0x0309) {
3201 params
[3] = ctx
->i1false
; /* r128 */
3206 LLVMValueRef lwe
= ctx
->i1false
;
3213 ac_get_image_intr_name("llvm.amdgcn.image.load",
3214 ctx
->v4f32
, /* vdata */
3215 LLVMTypeOf(params
[0]), /* coords */
3216 LLVMTypeOf(params
[1]), /* rsrc */
3217 intrinsic_name
, sizeof(intrinsic_name
));
3219 res
= ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->v4f32
,
3220 params
, 7, AC_FUNC_ATTR_READONLY
);
3222 return to_integer(ctx
, res
);
3225 static void visit_image_store(struct nir_to_llvm_context
*ctx
,
3226 nir_intrinsic_instr
*instr
)
3228 LLVMValueRef params
[8];
3229 char intrinsic_name
[64];
3230 const nir_variable
*var
= instr
->variables
[0]->var
;
3231 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3233 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
3234 ctx
->shader_info
->fs
.writes_memory
= true;
3236 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3237 params
[0] = to_float(ctx
, get_src(ctx
, instr
->src
[2])); /* data */
3238 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
3239 params
[2] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
3240 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
3241 params
[3] = LLVMConstInt(ctx
->i32
, 0, false); /* voffset */
3242 params
[4] = ctx
->i1false
; /* glc */
3243 params
[5] = ctx
->i1false
; /* slc */
3244 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->voidt
,
3247 bool is_da
= glsl_sampler_type_is_array(type
) ||
3248 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3249 LLVMValueRef da
= is_da
? ctx
->i1true
: ctx
->i1false
;
3250 LLVMValueRef glc
= ctx
->i1false
;
3251 LLVMValueRef slc
= ctx
->i1false
;
3253 params
[0] = to_float(ctx
, get_src(ctx
, instr
->src
[2]));
3254 params
[1] = get_image_coords(ctx
, instr
); /* coords */
3255 params
[2] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
3256 params
[3] = LLVMConstInt(ctx
->i32
, 15, false); /* dmask */
3257 if (HAVE_LLVM
<= 0x0309) {
3258 params
[4] = ctx
->i1false
; /* r128 */
3263 LLVMValueRef lwe
= ctx
->i1false
;
3270 ac_get_image_intr_name("llvm.amdgcn.image.store",
3271 LLVMTypeOf(params
[0]), /* vdata */
3272 LLVMTypeOf(params
[1]), /* coords */
3273 LLVMTypeOf(params
[2]), /* rsrc */
3274 intrinsic_name
, sizeof(intrinsic_name
));
3276 ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->voidt
,
3282 static LLVMValueRef
visit_image_atomic(struct nir_to_llvm_context
*ctx
,
3283 nir_intrinsic_instr
*instr
)
3285 LLVMValueRef params
[6];
3286 int param_count
= 0;
3287 const nir_variable
*var
= instr
->variables
[0]->var
;
3289 const char *base_name
= "llvm.amdgcn.image.atomic";
3290 const char *atomic_name
;
3291 LLVMValueRef coords
;
3292 char intrinsic_name
[32], coords_type
[8];
3293 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3295 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
3296 ctx
->shader_info
->fs
.writes_memory
= true;
3298 params
[param_count
++] = get_src(ctx
, instr
->src
[2]);
3299 if (instr
->intrinsic
== nir_intrinsic_image_atomic_comp_swap
)
3300 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
3302 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
3303 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
3304 coords
= params
[param_count
++] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
3305 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
3306 params
[param_count
++] = ctx
->i32zero
; /* voffset */
3307 params
[param_count
++] = ctx
->i1false
; /* glc */
3308 params
[param_count
++] = ctx
->i1false
; /* slc */
3310 bool da
= glsl_sampler_type_is_array(type
) ||
3311 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
3313 coords
= params
[param_count
++] = get_image_coords(ctx
, instr
);
3314 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
3315 params
[param_count
++] = ctx
->i1false
; /* r128 */
3316 params
[param_count
++] = da
? ctx
->i1true
: ctx
->i1false
; /* da */
3317 params
[param_count
++] = ctx
->i1false
; /* slc */
3320 switch (instr
->intrinsic
) {
3321 case nir_intrinsic_image_atomic_add
:
3322 atomic_name
= "add";
3324 case nir_intrinsic_image_atomic_min
:
3325 atomic_name
= "smin";
3327 case nir_intrinsic_image_atomic_max
:
3328 atomic_name
= "smax";
3330 case nir_intrinsic_image_atomic_and
:
3331 atomic_name
= "and";
3333 case nir_intrinsic_image_atomic_or
:
3336 case nir_intrinsic_image_atomic_xor
:
3337 atomic_name
= "xor";
3339 case nir_intrinsic_image_atomic_exchange
:
3340 atomic_name
= "swap";
3342 case nir_intrinsic_image_atomic_comp_swap
:
3343 atomic_name
= "cmpswap";
3348 build_int_type_name(LLVMTypeOf(coords
),
3349 coords_type
, sizeof(coords_type
));
3351 snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3352 "%s.%s.%s", base_name
, atomic_name
, coords_type
);
3353 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->i32
, params
, param_count
, 0);
3356 static LLVMValueRef
visit_image_size(struct nir_to_llvm_context
*ctx
,
3357 nir_intrinsic_instr
*instr
)
3360 const nir_variable
*var
= instr
->variables
[0]->var
;
3361 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
3362 bool da
= glsl_sampler_type_is_array(var
->type
) ||
3363 glsl_get_sampler_dim(var
->type
) == GLSL_SAMPLER_DIM_CUBE
;
3364 if(instr
->variables
[0]->deref
.child
)
3365 type
= instr
->variables
[0]->deref
.child
->type
;
3367 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
3368 return get_buffer_size(ctx
, get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
), true);
3370 struct ac_image_args args
= { 0 };
3374 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
3375 args
.opcode
= ac_image_get_resinfo
;
3376 args
.addr
= ctx
->i32zero
;
3378 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
3380 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
3381 glsl_sampler_type_is_array(type
)) {
3382 LLVMValueRef two
= LLVMConstInt(ctx
->i32
, 2, false);
3383 LLVMValueRef six
= LLVMConstInt(ctx
->i32
, 6, false);
3384 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->builder
, res
, two
, "");
3385 z
= LLVMBuildSDiv(ctx
->builder
, z
, six
, "");
3386 res
= LLVMBuildInsertElement(ctx
->builder
, res
, z
, two
, "");
3391 #define NOOP_WAITCNT 0xf7f
3392 #define LGKM_CNT 0x07f
3393 #define VM_CNT 0xf70
3395 static void emit_waitcnt(struct nir_to_llvm_context
*ctx
,
3398 LLVMValueRef args
[1] = {
3399 LLVMConstInt(ctx
->i32
, simm16
, false),
3401 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.s.waitcnt",
3402 ctx
->voidt
, args
, 1, 0);
3405 static void emit_barrier(struct nir_to_llvm_context
*ctx
)
3407 /* SI only (thanks to a hw bug workaround):
3408 * The real barrier instruction isn’t needed, because an entire patch
3409 * always fits into a single wave.
3411 if (ctx
->options
->chip_class
== SI
&&
3412 ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3413 emit_waitcnt(ctx
, LGKM_CNT
& VM_CNT
);
3416 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.s.barrier",
3417 ctx
->voidt
, NULL
, 0, AC_FUNC_ATTR_CONVERGENT
);
3420 static void emit_discard_if(struct nir_to_llvm_context
*ctx
,
3421 nir_intrinsic_instr
*instr
)
3424 ctx
->shader_info
->fs
.can_discard
= true;
3426 cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3427 get_src(ctx
, instr
->src
[0]),
3430 cond
= LLVMBuildSelect(ctx
->builder
, cond
,
3431 LLVMConstReal(ctx
->f32
, -1.0f
),
3433 ac_build_kill(&ctx
->ac
, cond
);
3437 visit_load_local_invocation_index(struct nir_to_llvm_context
*ctx
)
3439 LLVMValueRef result
;
3440 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
3441 result
= LLVMBuildAnd(ctx
->builder
, ctx
->tg_size
,
3442 LLVMConstInt(ctx
->i32
, 0xfc0, false), "");
3444 return LLVMBuildAdd(ctx
->builder
, result
, thread_id
, "");
3447 static LLVMValueRef
visit_var_atomic(struct nir_to_llvm_context
*ctx
,
3448 nir_intrinsic_instr
*instr
)
3450 LLVMValueRef ptr
, result
;
3451 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
3452 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
3453 ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
3455 if (instr
->intrinsic
== nir_intrinsic_var_atomic_comp_swap
) {
3456 LLVMValueRef src1
= get_src(ctx
, instr
->src
[1]);
3457 result
= LLVMBuildAtomicCmpXchg(ctx
->builder
,
3459 LLVMAtomicOrderingSequentiallyConsistent
,
3460 LLVMAtomicOrderingSequentiallyConsistent
,
3463 LLVMAtomicRMWBinOp op
;
3464 switch (instr
->intrinsic
) {
3465 case nir_intrinsic_var_atomic_add
:
3466 op
= LLVMAtomicRMWBinOpAdd
;
3468 case nir_intrinsic_var_atomic_umin
:
3469 op
= LLVMAtomicRMWBinOpUMin
;
3471 case nir_intrinsic_var_atomic_umax
:
3472 op
= LLVMAtomicRMWBinOpUMax
;
3474 case nir_intrinsic_var_atomic_imin
:
3475 op
= LLVMAtomicRMWBinOpMin
;
3477 case nir_intrinsic_var_atomic_imax
:
3478 op
= LLVMAtomicRMWBinOpMax
;
3480 case nir_intrinsic_var_atomic_and
:
3481 op
= LLVMAtomicRMWBinOpAnd
;
3483 case nir_intrinsic_var_atomic_or
:
3484 op
= LLVMAtomicRMWBinOpOr
;
3486 case nir_intrinsic_var_atomic_xor
:
3487 op
= LLVMAtomicRMWBinOpXor
;
3489 case nir_intrinsic_var_atomic_exchange
:
3490 op
= LLVMAtomicRMWBinOpXchg
;
3496 result
= LLVMBuildAtomicRMW(ctx
->builder
, op
, ptr
, to_integer(ctx
, src
),
3497 LLVMAtomicOrderingSequentiallyConsistent
,
3503 #define INTERP_CENTER 0
3504 #define INTERP_CENTROID 1
3505 #define INTERP_SAMPLE 2
3507 static LLVMValueRef
lookup_interp_param(struct nir_to_llvm_context
*ctx
,
3508 enum glsl_interp_mode interp
, unsigned location
)
3511 case INTERP_MODE_FLAT
:
3514 case INTERP_MODE_SMOOTH
:
3515 case INTERP_MODE_NONE
:
3516 if (location
== INTERP_CENTER
)
3517 return ctx
->persp_center
;
3518 else if (location
== INTERP_CENTROID
)
3519 return ctx
->persp_centroid
;
3520 else if (location
== INTERP_SAMPLE
)
3521 return ctx
->persp_sample
;
3523 case INTERP_MODE_NOPERSPECTIVE
:
3524 if (location
== INTERP_CENTER
)
3525 return ctx
->linear_center
;
3526 else if (location
== INTERP_CENTROID
)
3527 return ctx
->linear_centroid
;
3528 else if (location
== INTERP_SAMPLE
)
3529 return ctx
->linear_sample
;
3535 static LLVMValueRef
load_sample_position(struct nir_to_llvm_context
*ctx
,
3536 LLVMValueRef sample_id
)
3538 LLVMValueRef result
;
3539 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_PS_SAMPLE_POSITIONS
, false));
3541 ptr
= LLVMBuildBitCast(ctx
->builder
, ptr
,
3542 const_array(ctx
->v2f32
, 64), "");
3544 sample_id
= LLVMBuildAdd(ctx
->builder
, sample_id
, ctx
->sample_pos_offset
, "");
3545 result
= ac_build_indexed_load(&ctx
->ac
, ptr
, sample_id
, false);
3550 static LLVMValueRef
load_sample_pos(struct nir_to_llvm_context
*ctx
)
3552 LLVMValueRef values
[2];
3554 values
[0] = emit_ffract(ctx
, ctx
->frag_pos
[0]);
3555 values
[1] = emit_ffract(ctx
, ctx
->frag_pos
[1]);
3556 return ac_build_gather_values(&ctx
->ac
, values
, 2);
3559 static LLVMValueRef
visit_interp(struct nir_to_llvm_context
*ctx
,
3560 nir_intrinsic_instr
*instr
)
3562 LLVMValueRef result
[2];
3563 LLVMValueRef interp_param
, attr_number
;
3566 LLVMValueRef src_c0
, src_c1
;
3568 int input_index
= instr
->variables
[0]->var
->data
.location
- VARYING_SLOT_VAR0
;
3569 switch (instr
->intrinsic
) {
3570 case nir_intrinsic_interp_var_at_centroid
:
3571 location
= INTERP_CENTROID
;
3573 case nir_intrinsic_interp_var_at_sample
:
3574 case nir_intrinsic_interp_var_at_offset
:
3575 location
= INTERP_CENTER
;
3576 src0
= get_src(ctx
, instr
->src
[0]);
3582 if (instr
->intrinsic
== nir_intrinsic_interp_var_at_offset
) {
3583 src_c0
= to_float(ctx
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32zero
, ""));
3584 src_c1
= to_float(ctx
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32one
, ""));
3585 } else if (instr
->intrinsic
== nir_intrinsic_interp_var_at_sample
) {
3586 LLVMValueRef sample_position
;
3587 LLVMValueRef halfval
= LLVMConstReal(ctx
->f32
, 0.5f
);
3589 /* fetch sample ID */
3590 sample_position
= load_sample_position(ctx
, src0
);
3592 src_c0
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->i32zero
, "");
3593 src_c0
= LLVMBuildFSub(ctx
->builder
, src_c0
, halfval
, "");
3594 src_c1
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->i32one
, "");
3595 src_c1
= LLVMBuildFSub(ctx
->builder
, src_c1
, halfval
, "");
3597 interp_param
= lookup_interp_param(ctx
, instr
->variables
[0]->var
->data
.interpolation
, location
);
3598 attr_number
= LLVMConstInt(ctx
->i32
, input_index
, false);
3600 if (location
== INTERP_SAMPLE
|| location
== INTERP_CENTER
) {
3601 LLVMValueRef ij_out
[2];
3602 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
, interp_param
);
3605 * take the I then J parameters, and the DDX/Y for it, and
3606 * calculate the IJ inputs for the interpolator.
3607 * temp1 = ddx * offset/sample.x + I;
3608 * interp_param.I = ddy * offset/sample.y + temp1;
3609 * temp1 = ddx * offset/sample.x + J;
3610 * interp_param.J = ddy * offset/sample.y + temp1;
3612 for (unsigned i
= 0; i
< 2; i
++) {
3613 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->i32
, i
, false);
3614 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->i32
, i
+ 2, false);
3615 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->builder
,
3616 ddxy_out
, ix_ll
, "");
3617 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->builder
,
3618 ddxy_out
, iy_ll
, "");
3619 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->builder
,
3620 interp_param
, ix_ll
, "");
3621 LLVMValueRef temp1
, temp2
;
3623 interp_el
= LLVMBuildBitCast(ctx
->builder
, interp_el
,
3626 temp1
= LLVMBuildFMul(ctx
->builder
, ddx_el
, src_c0
, "");
3627 temp1
= LLVMBuildFAdd(ctx
->builder
, temp1
, interp_el
, "");
3629 temp2
= LLVMBuildFMul(ctx
->builder
, ddy_el
, src_c1
, "");
3630 temp2
= LLVMBuildFAdd(ctx
->builder
, temp2
, temp1
, "");
3632 ij_out
[i
] = LLVMBuildBitCast(ctx
->builder
,
3633 temp2
, ctx
->i32
, "");
3635 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
3639 for (chan
= 0; chan
< 2; chan
++) {
3640 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
3643 interp_param
= LLVMBuildBitCast(ctx
->builder
,
3644 interp_param
, LLVMVectorType(ctx
->f32
, 2), "");
3645 LLVMValueRef i
= LLVMBuildExtractElement(
3646 ctx
->builder
, interp_param
, ctx
->i32zero
, "");
3647 LLVMValueRef j
= LLVMBuildExtractElement(
3648 ctx
->builder
, interp_param
, ctx
->i32one
, "");
3650 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
3651 llvm_chan
, attr_number
,
3652 ctx
->prim_mask
, i
, j
);
3654 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
3655 LLVMConstInt(ctx
->i32
, 2, false),
3656 llvm_chan
, attr_number
,
3660 return ac_build_gather_values(&ctx
->ac
, result
, 2);
3664 visit_emit_vertex(struct nir_to_llvm_context
*ctx
,
3665 nir_intrinsic_instr
*instr
)
3667 LLVMValueRef gs_next_vertex
;
3668 LLVMValueRef can_emit
, kill
;
3671 assert(instr
->const_index
[0] == 0);
3672 /* Write vertex attribute values to GSVS ring */
3673 gs_next_vertex
= LLVMBuildLoad(ctx
->builder
,
3674 ctx
->gs_next_vertex
,
3677 /* If this thread has already emitted the declared maximum number of
3678 * vertices, kill it: excessive vertex emissions are not supposed to
3679 * have any effect, and GS threads have no externally observable
3680 * effects other than emitting vertices.
3682 can_emit
= LLVMBuildICmp(ctx
->builder
, LLVMIntULT
, gs_next_vertex
,
3683 LLVMConstInt(ctx
->i32
, ctx
->gs_max_out_vertices
, false), "");
3685 kill
= LLVMBuildSelect(ctx
->builder
, can_emit
,
3686 LLVMConstReal(ctx
->f32
, 1.0f
),
3687 LLVMConstReal(ctx
->f32
, -1.0f
), "");
3688 ac_build_kill(&ctx
->ac
, kill
);
3690 /* loop num outputs */
3692 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
3693 LLVMValueRef
*out_ptr
= &ctx
->outputs
[i
* 4];
3698 if (!(ctx
->output_mask
& (1ull << i
)))
3701 if (i
== VARYING_SLOT_CLIP_DIST0
) {
3702 /* pack clip and cull into a single set of slots */
3703 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
3707 for (unsigned j
= 0; j
< length
; j
++) {
3708 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
,
3710 LLVMValueRef voffset
= LLVMConstInt(ctx
->i32
, (slot
* 4 + j
) * ctx
->gs_max_out_vertices
, false);
3711 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, gs_next_vertex
, "");
3712 voffset
= LLVMBuildMul(ctx
->builder
, voffset
, LLVMConstInt(ctx
->i32
, 4, false), "");
3714 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->i32
, "");
3716 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->gsvs_ring
,
3718 voffset
, ctx
->gs2vs_offset
, 0,
3724 gs_next_vertex
= LLVMBuildAdd(ctx
->builder
, gs_next_vertex
,
3726 LLVMBuildStore(ctx
->builder
, gs_next_vertex
, ctx
->gs_next_vertex
);
3728 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_EMIT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
3732 visit_end_primitive(struct nir_to_llvm_context
*ctx
,
3733 nir_intrinsic_instr
*instr
)
3735 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_CUT
| AC_SENDMSG_GS
| (0 << 8), ctx
->gs_wave_id
);
3739 visit_load_tess_coord(struct nir_to_llvm_context
*ctx
,
3740 nir_intrinsic_instr
*instr
)
3742 LLVMValueRef coord
[4] = {
3749 if (ctx
->tes_primitive_mode
== GL_TRIANGLES
)
3750 coord
[2] = LLVMBuildFSub(ctx
->builder
, ctx
->f32one
,
3751 LLVMBuildFAdd(ctx
->builder
, coord
[0], coord
[1], ""), "");
3753 LLVMValueRef result
= ac_build_gather_values(&ctx
->ac
, coord
, instr
->num_components
);
3754 return LLVMBuildBitCast(ctx
->builder
, result
,
3755 get_def_type(ctx
, &instr
->dest
.ssa
), "");
3758 static void visit_intrinsic(struct nir_to_llvm_context
*ctx
,
3759 nir_intrinsic_instr
*instr
)
3761 LLVMValueRef result
= NULL
;
3763 switch (instr
->intrinsic
) {
3764 case nir_intrinsic_load_work_group_id
: {
3765 result
= ctx
->workgroup_ids
;
3768 case nir_intrinsic_load_base_vertex
: {
3769 result
= ctx
->base_vertex
;
3772 case nir_intrinsic_load_vertex_id_zero_base
: {
3773 result
= ctx
->vertex_id
;
3776 case nir_intrinsic_load_local_invocation_id
: {
3777 result
= ctx
->local_invocation_ids
;
3780 case nir_intrinsic_load_base_instance
:
3781 result
= ctx
->start_instance
;
3783 case nir_intrinsic_load_draw_id
:
3784 result
= ctx
->draw_index
;
3786 case nir_intrinsic_load_invocation_id
:
3787 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
3788 result
= unpack_param(ctx
, ctx
->tcs_rel_ids
, 8, 5);
3790 result
= ctx
->gs_invocation_id
;
3792 case nir_intrinsic_load_primitive_id
:
3793 if (ctx
->stage
== MESA_SHADER_GEOMETRY
)
3794 result
= ctx
->gs_prim_id
;
3795 else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
3796 result
= ctx
->tcs_patch_id
;
3797 else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
)
3798 result
= ctx
->tes_patch_id
;
3800 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
3802 case nir_intrinsic_load_sample_id
:
3803 ctx
->shader_info
->fs
.force_persample
= true;
3804 result
= unpack_param(ctx
, ctx
->ancillary
, 8, 4);
3806 case nir_intrinsic_load_sample_pos
:
3807 ctx
->shader_info
->fs
.force_persample
= true;
3808 result
= load_sample_pos(ctx
);
3810 case nir_intrinsic_load_sample_mask_in
:
3811 result
= ctx
->sample_coverage
;
3813 case nir_intrinsic_load_front_face
:
3814 result
= ctx
->front_face
;
3816 case nir_intrinsic_load_instance_id
:
3817 result
= ctx
->instance_id
;
3818 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
3819 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
3821 case nir_intrinsic_load_num_work_groups
:
3822 result
= ctx
->num_work_groups
;
3824 case nir_intrinsic_load_local_invocation_index
:
3825 result
= visit_load_local_invocation_index(ctx
);
3827 case nir_intrinsic_load_push_constant
:
3828 result
= visit_load_push_constant(ctx
, instr
);
3830 case nir_intrinsic_vulkan_resource_index
:
3831 result
= visit_vulkan_resource_index(ctx
, instr
);
3833 case nir_intrinsic_store_ssbo
:
3834 visit_store_ssbo(ctx
, instr
);
3836 case nir_intrinsic_load_ssbo
:
3837 result
= visit_load_buffer(ctx
, instr
);
3839 case nir_intrinsic_ssbo_atomic_add
:
3840 case nir_intrinsic_ssbo_atomic_imin
:
3841 case nir_intrinsic_ssbo_atomic_umin
:
3842 case nir_intrinsic_ssbo_atomic_imax
:
3843 case nir_intrinsic_ssbo_atomic_umax
:
3844 case nir_intrinsic_ssbo_atomic_and
:
3845 case nir_intrinsic_ssbo_atomic_or
:
3846 case nir_intrinsic_ssbo_atomic_xor
:
3847 case nir_intrinsic_ssbo_atomic_exchange
:
3848 case nir_intrinsic_ssbo_atomic_comp_swap
:
3849 result
= visit_atomic_ssbo(ctx
, instr
);
3851 case nir_intrinsic_load_ubo
:
3852 result
= visit_load_ubo_buffer(ctx
, instr
);
3854 case nir_intrinsic_get_buffer_size
:
3855 result
= visit_get_buffer_size(ctx
, instr
);
3857 case nir_intrinsic_load_var
:
3858 result
= visit_load_var(ctx
, instr
);
3860 case nir_intrinsic_store_var
:
3861 visit_store_var(ctx
, instr
);
3863 case nir_intrinsic_image_load
:
3864 result
= visit_image_load(ctx
, instr
);
3866 case nir_intrinsic_image_store
:
3867 visit_image_store(ctx
, instr
);
3869 case nir_intrinsic_image_atomic_add
:
3870 case nir_intrinsic_image_atomic_min
:
3871 case nir_intrinsic_image_atomic_max
:
3872 case nir_intrinsic_image_atomic_and
:
3873 case nir_intrinsic_image_atomic_or
:
3874 case nir_intrinsic_image_atomic_xor
:
3875 case nir_intrinsic_image_atomic_exchange
:
3876 case nir_intrinsic_image_atomic_comp_swap
:
3877 result
= visit_image_atomic(ctx
, instr
);
3879 case nir_intrinsic_image_size
:
3880 result
= visit_image_size(ctx
, instr
);
3882 case nir_intrinsic_discard
:
3883 ctx
->shader_info
->fs
.can_discard
= true;
3884 ac_build_intrinsic(&ctx
->ac
, "llvm.AMDGPU.kilp",
3886 NULL
, 0, AC_FUNC_ATTR_LEGACY
);
3888 case nir_intrinsic_discard_if
:
3889 emit_discard_if(ctx
, instr
);
3891 case nir_intrinsic_memory_barrier
:
3892 emit_waitcnt(ctx
, VM_CNT
);
3894 case nir_intrinsic_barrier
:
3897 case nir_intrinsic_var_atomic_add
:
3898 case nir_intrinsic_var_atomic_imin
:
3899 case nir_intrinsic_var_atomic_umin
:
3900 case nir_intrinsic_var_atomic_imax
:
3901 case nir_intrinsic_var_atomic_umax
:
3902 case nir_intrinsic_var_atomic_and
:
3903 case nir_intrinsic_var_atomic_or
:
3904 case nir_intrinsic_var_atomic_xor
:
3905 case nir_intrinsic_var_atomic_exchange
:
3906 case nir_intrinsic_var_atomic_comp_swap
:
3907 result
= visit_var_atomic(ctx
, instr
);
3909 case nir_intrinsic_interp_var_at_centroid
:
3910 case nir_intrinsic_interp_var_at_sample
:
3911 case nir_intrinsic_interp_var_at_offset
:
3912 result
= visit_interp(ctx
, instr
);
3914 case nir_intrinsic_emit_vertex
:
3915 visit_emit_vertex(ctx
, instr
);
3917 case nir_intrinsic_end_primitive
:
3918 visit_end_primitive(ctx
, instr
);
3920 case nir_intrinsic_load_tess_coord
:
3921 result
= visit_load_tess_coord(ctx
, instr
);
3923 case nir_intrinsic_load_patch_vertices_in
:
3924 result
= LLVMConstInt(ctx
->i32
, ctx
->options
->key
.tcs
.input_vertices
, false);
3927 fprintf(stderr
, "Unknown intrinsic: ");
3928 nir_print_instr(&instr
->instr
, stderr
);
3929 fprintf(stderr
, "\n");
3933 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
3937 static LLVMValueRef
get_sampler_desc(struct nir_to_llvm_context
*ctx
,
3938 nir_deref_var
*deref
,
3939 enum desc_type desc_type
)
3941 unsigned desc_set
= deref
->var
->data
.descriptor_set
;
3942 LLVMValueRef list
= ctx
->descriptor_sets
[desc_set
];
3943 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[desc_set
].layout
;
3944 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ deref
->var
->data
.binding
;
3945 unsigned offset
= binding
->offset
;
3946 unsigned stride
= binding
->size
;
3948 LLVMBuilderRef builder
= ctx
->builder
;
3950 LLVMValueRef index
= NULL
;
3951 unsigned constant_index
= 0;
3953 assert(deref
->var
->data
.binding
< layout
->binding_count
);
3955 switch (desc_type
) {
3967 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
3977 unreachable("invalid desc_type\n");
3980 if (deref
->deref
.child
) {
3981 nir_deref_array
*child
= (nir_deref_array
*)deref
->deref
.child
;
3983 assert(child
->deref_array_type
!= nir_deref_array_type_wildcard
);
3984 offset
+= child
->base_offset
* stride
;
3985 if (child
->deref_array_type
== nir_deref_array_type_indirect
) {
3986 index
= get_src(ctx
, child
->indirect
);
3989 constant_index
= child
->base_offset
;
3991 if (desc_type
== DESC_SAMPLER
&& binding
->immutable_samplers_offset
&&
3992 (!index
|| binding
->immutable_samplers_equal
)) {
3993 if (binding
->immutable_samplers_equal
)
3996 const uint32_t *samplers
= radv_immutable_samplers(layout
, binding
);
3998 LLVMValueRef constants
[] = {
3999 LLVMConstInt(ctx
->i32
, samplers
[constant_index
* 4 + 0], 0),
4000 LLVMConstInt(ctx
->i32
, samplers
[constant_index
* 4 + 1], 0),
4001 LLVMConstInt(ctx
->i32
, samplers
[constant_index
* 4 + 2], 0),
4002 LLVMConstInt(ctx
->i32
, samplers
[constant_index
* 4 + 3], 0),
4004 return ac_build_gather_values(&ctx
->ac
, constants
, 4);
4007 assert(stride
% type_size
== 0);
4010 index
= ctx
->i32zero
;
4012 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, stride
/ type_size
, 0), "");
4014 list
= ac_build_gep0(&ctx
->ac
, list
, LLVMConstInt(ctx
->i32
, offset
, 0));
4015 list
= LLVMBuildPointerCast(builder
, list
, const_array(type
, 0), "");
4017 return ac_build_indexed_load_const(&ctx
->ac
, list
, index
);
4020 static void set_tex_fetch_args(struct nir_to_llvm_context
*ctx
,
4021 struct ac_image_args
*args
,
4022 nir_tex_instr
*instr
,
4024 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
4025 LLVMValueRef
*param
, unsigned count
,
4028 unsigned is_rect
= 0;
4029 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
4031 if (op
== nir_texop_lod
)
4033 /* Pad to power of two vector */
4034 while (count
< util_next_power_of_two(count
))
4035 param
[count
++] = LLVMGetUndef(ctx
->i32
);
4038 args
->addr
= ac_build_gather_values(&ctx
->ac
, param
, count
);
4040 args
->addr
= param
[0];
4042 args
->resource
= res_ptr
;
4043 args
->sampler
= samp_ptr
;
4045 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
&& op
== nir_texop_txf
) {
4046 args
->addr
= param
[0];
4050 args
->dmask
= dmask
;
4051 args
->unorm
= is_rect
;
4055 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
4058 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
4059 * filtering manually. The driver sets img7 to a mask clearing
4060 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
4061 * s_and_b32 samp0, samp0, img7
4064 * The ANISO_OVERRIDE sampler field enables this fix in TA.
4066 static LLVMValueRef
sici_fix_sampler_aniso(struct nir_to_llvm_context
*ctx
,
4067 LLVMValueRef res
, LLVMValueRef samp
)
4069 LLVMBuilderRef builder
= ctx
->builder
;
4070 LLVMValueRef img7
, samp0
;
4072 if (ctx
->options
->chip_class
>= VI
)
4075 img7
= LLVMBuildExtractElement(builder
, res
,
4076 LLVMConstInt(ctx
->i32
, 7, 0), "");
4077 samp0
= LLVMBuildExtractElement(builder
, samp
,
4078 LLVMConstInt(ctx
->i32
, 0, 0), "");
4079 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
4080 return LLVMBuildInsertElement(builder
, samp
, samp0
,
4081 LLVMConstInt(ctx
->i32
, 0, 0), "");
4084 static void tex_fetch_ptrs(struct nir_to_llvm_context
*ctx
,
4085 nir_tex_instr
*instr
,
4086 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
4087 LLVMValueRef
*fmask_ptr
)
4089 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
4090 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_BUFFER
);
4092 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_IMAGE
);
4095 *samp_ptr
= get_sampler_desc(ctx
, instr
->sampler
, DESC_SAMPLER
);
4097 *samp_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_SAMPLER
);
4098 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
4099 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
4101 if (fmask_ptr
&& !instr
->sampler
&& (instr
->op
== nir_texop_txf_ms
||
4102 instr
->op
== nir_texop_samples_identical
))
4103 *fmask_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_FMASK
);
4106 static LLVMValueRef
apply_round_slice(struct nir_to_llvm_context
*ctx
,
4109 coord
= to_float(ctx
, coord
);
4110 coord
= ac_build_intrinsic(&ctx
->ac
, "llvm.rint.f32", ctx
->f32
, &coord
, 1, 0);
4111 coord
= to_integer(ctx
, coord
);
4115 static void visit_tex(struct nir_to_llvm_context
*ctx
, nir_tex_instr
*instr
)
4117 LLVMValueRef result
= NULL
;
4118 struct ac_image_args args
= { 0 };
4119 unsigned dmask
= 0xf;
4120 LLVMValueRef address
[16];
4121 LLVMValueRef coords
[5];
4122 LLVMValueRef coord
= NULL
, lod
= NULL
, comparator
= NULL
;
4123 LLVMValueRef bias
= NULL
, offsets
= NULL
;
4124 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
, sample_index
= NULL
;
4125 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
4126 LLVMValueRef derivs
[6];
4127 unsigned chan
, count
= 0;
4128 unsigned const_src
= 0, num_deriv_comp
= 0;
4130 tex_fetch_ptrs(ctx
, instr
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
4132 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
4133 switch (instr
->src
[i
].src_type
) {
4134 case nir_tex_src_coord
:
4135 coord
= get_src(ctx
, instr
->src
[i
].src
);
4137 case nir_tex_src_projector
:
4139 case nir_tex_src_comparator
:
4140 comparator
= get_src(ctx
, instr
->src
[i
].src
);
4142 case nir_tex_src_offset
:
4143 offsets
= get_src(ctx
, instr
->src
[i
].src
);
4146 case nir_tex_src_bias
:
4147 bias
= get_src(ctx
, instr
->src
[i
].src
);
4149 case nir_tex_src_lod
:
4150 lod
= get_src(ctx
, instr
->src
[i
].src
);
4152 case nir_tex_src_ms_index
:
4153 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
4155 case nir_tex_src_ms_mcs
:
4157 case nir_tex_src_ddx
:
4158 ddx
= get_src(ctx
, instr
->src
[i
].src
);
4159 num_deriv_comp
= instr
->src
[i
].src
.ssa
->num_components
;
4161 case nir_tex_src_ddy
:
4162 ddy
= get_src(ctx
, instr
->src
[i
].src
);
4164 case nir_tex_src_texture_offset
:
4165 case nir_tex_src_sampler_offset
:
4166 case nir_tex_src_plane
:
4172 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
4173 result
= get_buffer_size(ctx
, res_ptr
, true);
4177 if (instr
->op
== nir_texop_texture_samples
) {
4178 LLVMValueRef res
, samples
, is_msaa
;
4179 res
= LLVMBuildBitCast(ctx
->builder
, res_ptr
, ctx
->v8i32
, "");
4180 samples
= LLVMBuildExtractElement(ctx
->builder
, res
,
4181 LLVMConstInt(ctx
->i32
, 3, false), "");
4182 is_msaa
= LLVMBuildLShr(ctx
->builder
, samples
,
4183 LLVMConstInt(ctx
->i32
, 28, false), "");
4184 is_msaa
= LLVMBuildAnd(ctx
->builder
, is_msaa
,
4185 LLVMConstInt(ctx
->i32
, 0xe, false), "");
4186 is_msaa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, is_msaa
,
4187 LLVMConstInt(ctx
->i32
, 0xe, false), "");
4189 samples
= LLVMBuildLShr(ctx
->builder
, samples
,
4190 LLVMConstInt(ctx
->i32
, 16, false), "");
4191 samples
= LLVMBuildAnd(ctx
->builder
, samples
,
4192 LLVMConstInt(ctx
->i32
, 0xf, false), "");
4193 samples
= LLVMBuildShl(ctx
->builder
, ctx
->i32one
,
4195 samples
= LLVMBuildSelect(ctx
->builder
, is_msaa
, samples
,
4202 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
4203 coords
[chan
] = llvm_extract_elem(ctx
, coord
, chan
);
4205 if (offsets
&& instr
->op
!= nir_texop_txf
) {
4206 LLVMValueRef offset
[3], pack
;
4207 for (chan
= 0; chan
< 3; ++chan
)
4208 offset
[chan
] = ctx
->i32zero
;
4211 for (chan
= 0; chan
< get_llvm_num_components(offsets
); chan
++) {
4212 offset
[chan
] = llvm_extract_elem(ctx
, offsets
, chan
);
4213 offset
[chan
] = LLVMBuildAnd(ctx
->builder
, offset
[chan
],
4214 LLVMConstInt(ctx
->i32
, 0x3f, false), "");
4216 offset
[chan
] = LLVMBuildShl(ctx
->builder
, offset
[chan
],
4217 LLVMConstInt(ctx
->i32
, chan
* 8, false), "");
4219 pack
= LLVMBuildOr(ctx
->builder
, offset
[0], offset
[1], "");
4220 pack
= LLVMBuildOr(ctx
->builder
, pack
, offset
[2], "");
4221 address
[count
++] = pack
;
4224 /* pack LOD bias value */
4225 if (instr
->op
== nir_texop_txb
&& bias
) {
4226 address
[count
++] = bias
;
4229 /* Pack depth comparison value */
4230 if (instr
->is_shadow
&& comparator
) {
4231 address
[count
++] = llvm_extract_elem(ctx
, comparator
, 0);
4234 /* pack derivatives */
4236 switch (instr
->sampler_dim
) {
4237 case GLSL_SAMPLER_DIM_3D
:
4238 case GLSL_SAMPLER_DIM_CUBE
:
4241 case GLSL_SAMPLER_DIM_2D
:
4245 case GLSL_SAMPLER_DIM_1D
:
4250 for (unsigned i
= 0; i
< num_deriv_comp
; i
++) {
4251 derivs
[i
] = to_float(ctx
, llvm_extract_elem(ctx
, ddx
, i
));
4252 derivs
[num_deriv_comp
+ i
] = to_float(ctx
, llvm_extract_elem(ctx
, ddy
, i
));
4256 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& coord
) {
4257 if (instr
->is_array
&& instr
->op
!= nir_texop_lod
)
4258 coords
[3] = apply_round_slice(ctx
, coords
[3]);
4259 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
4260 coords
[chan
] = to_float(ctx
, coords
[chan
]);
4261 if (instr
->coord_components
== 3)
4262 coords
[3] = LLVMGetUndef(ctx
->f32
);
4263 ac_prepare_cube_coords(&ctx
->ac
,
4264 instr
->op
== nir_texop_txd
, instr
->is_array
,
4271 for (unsigned i
= 0; i
< num_deriv_comp
* 2; i
++)
4272 address
[count
++] = derivs
[i
];
4275 /* Pack texture coordinates */
4277 address
[count
++] = coords
[0];
4278 if (instr
->coord_components
> 1) {
4279 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&& instr
->is_array
&& instr
->op
!= nir_texop_txf
) {
4280 coords
[1] = apply_round_slice(ctx
, coords
[1]);
4282 address
[count
++] = coords
[1];
4284 if (instr
->coord_components
> 2) {
4285 /* This seems like a bit of a hack - but it passes Vulkan CTS with it */
4286 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_3D
&&
4287 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_CUBE
&&
4288 instr
->op
!= nir_texop_txf
) {
4289 coords
[2] = apply_round_slice(ctx
, coords
[2]);
4291 address
[count
++] = coords
[2];
4296 if ((instr
->op
== nir_texop_txl
|| instr
->op
== nir_texop_txf
) && lod
) {
4297 address
[count
++] = lod
;
4298 } else if (instr
->op
== nir_texop_txf_ms
&& sample_index
) {
4299 address
[count
++] = sample_index
;
4300 } else if(instr
->op
== nir_texop_txs
) {
4303 address
[count
++] = lod
;
4305 address
[count
++] = ctx
->i32zero
;
4308 for (chan
= 0; chan
< count
; chan
++) {
4309 address
[chan
] = LLVMBuildBitCast(ctx
->builder
,
4310 address
[chan
], ctx
->i32
, "");
4313 if (instr
->op
== nir_texop_samples_identical
) {
4314 LLVMValueRef txf_address
[4];
4315 struct ac_image_args txf_args
= { 0 };
4316 unsigned txf_count
= count
;
4317 memcpy(txf_address
, address
, sizeof(txf_address
));
4319 if (!instr
->is_array
)
4320 txf_address
[2] = ctx
->i32zero
;
4321 txf_address
[3] = ctx
->i32zero
;
4323 set_tex_fetch_args(ctx
, &txf_args
, instr
, nir_texop_txf
,
4325 txf_address
, txf_count
, 0xf);
4327 result
= build_tex_intrinsic(ctx
, instr
, &txf_args
);
4329 result
= LLVMBuildExtractElement(ctx
->builder
, result
, ctx
->i32zero
, "");
4330 result
= emit_int_cmp(ctx
, LLVMIntEQ
, result
, ctx
->i32zero
);
4334 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
4335 instr
->op
!= nir_texop_txs
) {
4336 unsigned sample_chan
= instr
->is_array
? 3 : 2;
4337 address
[sample_chan
] = adjust_sample_index_using_fmask(ctx
,
4340 instr
->is_array
? address
[2] : NULL
,
4341 address
[sample_chan
],
4345 if (offsets
&& instr
->op
== nir_texop_txf
) {
4346 nir_const_value
*const_offset
=
4347 nir_src_as_const_value(instr
->src
[const_src
].src
);
4348 int num_offsets
= instr
->src
[const_src
].src
.ssa
->num_components
;
4349 assert(const_offset
);
4350 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
4351 if (num_offsets
> 2)
4352 address
[2] = LLVMBuildAdd(ctx
->builder
,
4353 address
[2], LLVMConstInt(ctx
->i32
, const_offset
->i32
[2], false), "");
4354 if (num_offsets
> 1)
4355 address
[1] = LLVMBuildAdd(ctx
->builder
,
4356 address
[1], LLVMConstInt(ctx
->i32
, const_offset
->i32
[1], false), "");
4357 address
[0] = LLVMBuildAdd(ctx
->builder
,
4358 address
[0], LLVMConstInt(ctx
->i32
, const_offset
->i32
[0], false), "");
4362 /* TODO TG4 support */
4363 if (instr
->op
== nir_texop_tg4
) {
4364 if (instr
->is_shadow
)
4367 dmask
= 1 << instr
->component
;
4369 set_tex_fetch_args(ctx
, &args
, instr
, instr
->op
,
4370 res_ptr
, samp_ptr
, address
, count
, dmask
);
4372 result
= build_tex_intrinsic(ctx
, instr
, &args
);
4374 if (instr
->op
== nir_texop_query_levels
)
4375 result
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, 3, false), "");
4376 else if (instr
->is_shadow
&& instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&& instr
->op
!= nir_texop_tg4
)
4377 result
= LLVMBuildExtractElement(ctx
->builder
, result
, ctx
->i32zero
, "");
4378 else if (instr
->op
== nir_texop_txs
&&
4379 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
4381 LLVMValueRef two
= LLVMConstInt(ctx
->i32
, 2, false);
4382 LLVMValueRef six
= LLVMConstInt(ctx
->i32
, 6, false);
4383 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->builder
, result
, two
, "");
4384 z
= LLVMBuildSDiv(ctx
->builder
, z
, six
, "");
4385 result
= LLVMBuildInsertElement(ctx
->builder
, result
, z
, two
, "");
4386 } else if (instr
->dest
.ssa
.num_components
!= 4)
4387 result
= trim_vector(ctx
, result
, instr
->dest
.ssa
.num_components
);
4391 assert(instr
->dest
.is_ssa
);
4392 result
= to_integer(ctx
, result
);
4393 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4398 static void visit_phi(struct nir_to_llvm_context
*ctx
, nir_phi_instr
*instr
)
4400 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
4401 LLVMValueRef result
= LLVMBuildPhi(ctx
->builder
, type
, "");
4403 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
4404 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
4407 static void visit_post_phi(struct nir_to_llvm_context
*ctx
,
4408 nir_phi_instr
*instr
,
4409 LLVMValueRef llvm_phi
)
4411 nir_foreach_phi_src(src
, instr
) {
4412 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
4413 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
4415 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
4419 static void phi_post_pass(struct nir_to_llvm_context
*ctx
)
4421 struct hash_entry
*entry
;
4422 hash_table_foreach(ctx
->phis
, entry
) {
4423 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
4424 (LLVMValueRef
)entry
->data
);
4429 static void visit_ssa_undef(struct nir_to_llvm_context
*ctx
,
4430 nir_ssa_undef_instr
*instr
)
4432 unsigned num_components
= instr
->def
.num_components
;
4435 if (num_components
== 1)
4436 undef
= LLVMGetUndef(ctx
->i32
);
4438 undef
= LLVMGetUndef(LLVMVectorType(ctx
->i32
, num_components
));
4440 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, undef
);
4443 static void visit_jump(struct nir_to_llvm_context
*ctx
,
4444 nir_jump_instr
*instr
)
4446 switch (instr
->type
) {
4447 case nir_jump_break
:
4448 LLVMBuildBr(ctx
->builder
, ctx
->break_block
);
4449 LLVMClearInsertionPosition(ctx
->builder
);
4451 case nir_jump_continue
:
4452 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
4453 LLVMClearInsertionPosition(ctx
->builder
);
4456 fprintf(stderr
, "Unknown NIR jump instr: ");
4457 nir_print_instr(&instr
->instr
, stderr
);
4458 fprintf(stderr
, "\n");
4463 static void visit_cf_list(struct nir_to_llvm_context
*ctx
,
4464 struct exec_list
*list
);
4466 static void visit_block(struct nir_to_llvm_context
*ctx
, nir_block
*block
)
4468 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->builder
);
4469 nir_foreach_instr(instr
, block
)
4471 switch (instr
->type
) {
4472 case nir_instr_type_alu
:
4473 visit_alu(ctx
, nir_instr_as_alu(instr
));
4475 case nir_instr_type_load_const
:
4476 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
4478 case nir_instr_type_intrinsic
:
4479 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
4481 case nir_instr_type_tex
:
4482 visit_tex(ctx
, nir_instr_as_tex(instr
));
4484 case nir_instr_type_phi
:
4485 visit_phi(ctx
, nir_instr_as_phi(instr
));
4487 case nir_instr_type_ssa_undef
:
4488 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
4490 case nir_instr_type_jump
:
4491 visit_jump(ctx
, nir_instr_as_jump(instr
));
4494 fprintf(stderr
, "Unknown NIR instr type: ");
4495 nir_print_instr(instr
, stderr
);
4496 fprintf(stderr
, "\n");
4501 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
4504 static void visit_if(struct nir_to_llvm_context
*ctx
, nir_if
*if_stmt
)
4506 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
4508 LLVMBasicBlockRef merge_block
=
4509 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
4510 LLVMBasicBlockRef if_block
=
4511 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
4512 LLVMBasicBlockRef else_block
= merge_block
;
4513 if (!exec_list_is_empty(&if_stmt
->else_list
))
4514 else_block
= LLVMAppendBasicBlockInContext(
4515 ctx
->context
, ctx
->main_function
, "");
4517 LLVMValueRef cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, value
,
4518 LLVMConstInt(ctx
->i32
, 0, false), "");
4519 LLVMBuildCondBr(ctx
->builder
, cond
, if_block
, else_block
);
4521 LLVMPositionBuilderAtEnd(ctx
->builder
, if_block
);
4522 visit_cf_list(ctx
, &if_stmt
->then_list
);
4523 if (LLVMGetInsertBlock(ctx
->builder
))
4524 LLVMBuildBr(ctx
->builder
, merge_block
);
4526 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
4527 LLVMPositionBuilderAtEnd(ctx
->builder
, else_block
);
4528 visit_cf_list(ctx
, &if_stmt
->else_list
);
4529 if (LLVMGetInsertBlock(ctx
->builder
))
4530 LLVMBuildBr(ctx
->builder
, merge_block
);
4533 LLVMPositionBuilderAtEnd(ctx
->builder
, merge_block
);
4536 static void visit_loop(struct nir_to_llvm_context
*ctx
, nir_loop
*loop
)
4538 LLVMBasicBlockRef continue_parent
= ctx
->continue_block
;
4539 LLVMBasicBlockRef break_parent
= ctx
->break_block
;
4541 ctx
->continue_block
=
4542 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
4544 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
4546 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
4547 LLVMPositionBuilderAtEnd(ctx
->builder
, ctx
->continue_block
);
4548 visit_cf_list(ctx
, &loop
->body
);
4550 if (LLVMGetInsertBlock(ctx
->builder
))
4551 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
4552 LLVMPositionBuilderAtEnd(ctx
->builder
, ctx
->break_block
);
4554 ctx
->continue_block
= continue_parent
;
4555 ctx
->break_block
= break_parent
;
4558 static void visit_cf_list(struct nir_to_llvm_context
*ctx
,
4559 struct exec_list
*list
)
4561 foreach_list_typed(nir_cf_node
, node
, node
, list
)
4563 switch (node
->type
) {
4564 case nir_cf_node_block
:
4565 visit_block(ctx
, nir_cf_node_as_block(node
));
4568 case nir_cf_node_if
:
4569 visit_if(ctx
, nir_cf_node_as_if(node
));
4572 case nir_cf_node_loop
:
4573 visit_loop(ctx
, nir_cf_node_as_loop(node
));
4583 handle_vs_input_decl(struct nir_to_llvm_context
*ctx
,
4584 struct nir_variable
*variable
)
4586 LLVMValueRef t_list_ptr
= ctx
->vertex_buffers
;
4587 LLVMValueRef t_offset
;
4588 LLVMValueRef t_list
;
4590 LLVMValueRef buffer_index
;
4591 int index
= variable
->data
.location
- VERT_ATTRIB_GENERIC0
;
4592 int idx
= variable
->data
.location
;
4593 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, true);
4595 variable
->data
.driver_location
= idx
* 4;
4597 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << index
)) {
4598 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->instance_id
,
4599 ctx
->start_instance
, "");
4600 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
4601 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
4603 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->vertex_id
,
4604 ctx
->base_vertex
, "");
4606 for (unsigned i
= 0; i
< attrib_count
; ++i
, ++idx
) {
4607 t_offset
= LLVMConstInt(ctx
->i32
, index
+ i
, false);
4609 t_list
= ac_build_indexed_load_const(&ctx
->ac
, t_list_ptr
, t_offset
);
4611 input
= ac_build_buffer_load_format(&ctx
->ac
, t_list
,
4613 LLVMConstInt(ctx
->i32
, 0, false),
4616 for (unsigned chan
= 0; chan
< 4; chan
++) {
4617 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
4618 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
, chan
)] =
4619 to_integer(ctx
, LLVMBuildExtractElement(ctx
->builder
,
4620 input
, llvm_chan
, ""));
4625 static void interp_fs_input(struct nir_to_llvm_context
*ctx
,
4627 LLVMValueRef interp_param
,
4628 LLVMValueRef prim_mask
,
4629 LLVMValueRef result
[4])
4631 LLVMValueRef attr_number
;
4634 bool interp
= interp_param
!= NULL
;
4636 attr_number
= LLVMConstInt(ctx
->i32
, attr
, false);
4638 /* fs.constant returns the param from the middle vertex, so it's not
4639 * really useful for flat shading. It's meant to be used for custom
4640 * interpolation (but the intrinsic can't fetch from the other two
4643 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
4644 * to do the right thing. The only reason we use fs.constant is that
4645 * fs.interp cannot be used on integers, because they can be equal
4649 interp_param
= LLVMBuildBitCast(ctx
->builder
, interp_param
,
4650 LLVMVectorType(ctx
->f32
, 2), "");
4652 i
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
4654 j
= LLVMBuildExtractElement(ctx
->builder
, interp_param
,
4658 for (chan
= 0; chan
< 4; chan
++) {
4659 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
4662 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
4667 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
4668 LLVMConstInt(ctx
->i32
, 2, false),
4677 handle_fs_input_decl(struct nir_to_llvm_context
*ctx
,
4678 struct nir_variable
*variable
)
4680 int idx
= variable
->data
.location
;
4681 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4682 LLVMValueRef interp
;
4684 variable
->data
.driver_location
= idx
* 4;
4685 ctx
->input_mask
|= ((1ull << attrib_count
) - 1) << variable
->data
.location
;
4687 if (glsl_get_base_type(glsl_without_array(variable
->type
)) == GLSL_TYPE_FLOAT
) {
4688 unsigned interp_type
;
4689 if (variable
->data
.sample
) {
4690 interp_type
= INTERP_SAMPLE
;
4691 ctx
->shader_info
->fs
.force_persample
= true;
4692 } else if (variable
->data
.centroid
)
4693 interp_type
= INTERP_CENTROID
;
4695 interp_type
= INTERP_CENTER
;
4697 interp
= lookup_interp_param(ctx
, variable
->data
.interpolation
, interp_type
);
4701 for (unsigned i
= 0; i
< attrib_count
; ++i
)
4702 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
+ i
, 0)] = interp
;
4707 handle_shader_input_decl(struct nir_to_llvm_context
*ctx
,
4708 struct nir_variable
*variable
)
4710 switch (ctx
->stage
) {
4711 case MESA_SHADER_VERTEX
:
4712 handle_vs_input_decl(ctx
, variable
);
4714 case MESA_SHADER_FRAGMENT
:
4715 handle_fs_input_decl(ctx
, variable
);
4724 handle_fs_inputs_pre(struct nir_to_llvm_context
*ctx
,
4725 struct nir_shader
*nir
)
4728 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_INPUTS
; ++i
) {
4729 LLVMValueRef interp_param
;
4730 LLVMValueRef
*inputs
= ctx
->inputs
+radeon_llvm_reg_index_soa(i
, 0);
4732 if (!(ctx
->input_mask
& (1ull << i
)))
4735 if (i
>= VARYING_SLOT_VAR0
|| i
== VARYING_SLOT_PNTC
||
4736 i
== VARYING_SLOT_PRIMITIVE_ID
|| i
== VARYING_SLOT_LAYER
) {
4737 interp_param
= *inputs
;
4738 interp_fs_input(ctx
, index
, interp_param
, ctx
->prim_mask
,
4742 ctx
->shader_info
->fs
.flat_shaded_mask
|= 1u << index
;
4744 } else if (i
== VARYING_SLOT_POS
) {
4745 for(int i
= 0; i
< 3; ++i
)
4746 inputs
[i
] = ctx
->frag_pos
[i
];
4748 inputs
[3] = ac_build_fdiv(&ctx
->ac
, ctx
->f32one
, ctx
->frag_pos
[3]);
4751 ctx
->shader_info
->fs
.num_interp
= index
;
4752 if (ctx
->input_mask
& (1 << VARYING_SLOT_PNTC
))
4753 ctx
->shader_info
->fs
.has_pcoord
= true;
4754 if (ctx
->input_mask
& (1 << VARYING_SLOT_PRIMITIVE_ID
))
4755 ctx
->shader_info
->fs
.prim_id_input
= true;
4756 if (ctx
->input_mask
& (1 << VARYING_SLOT_LAYER
))
4757 ctx
->shader_info
->fs
.layer_input
= true;
4758 ctx
->shader_info
->fs
.input_mask
= ctx
->input_mask
>> VARYING_SLOT_VAR0
;
4762 ac_build_alloca(struct nir_to_llvm_context
*ctx
,
4766 LLVMBuilderRef builder
= ctx
->builder
;
4767 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
4768 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
4769 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
4770 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
4771 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ctx
->context
);
4775 LLVMPositionBuilderBefore(first_builder
, first_instr
);
4777 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
4780 res
= LLVMBuildAlloca(first_builder
, type
, name
);
4781 LLVMBuildStore(builder
, LLVMConstNull(type
), res
);
4783 LLVMDisposeBuilder(first_builder
);
4788 static LLVMValueRef
si_build_alloca_undef(struct nir_to_llvm_context
*ctx
,
4792 LLVMValueRef ptr
= ac_build_alloca(ctx
, type
, name
);
4793 LLVMBuildStore(ctx
->builder
, LLVMGetUndef(type
), ptr
);
4798 handle_shader_output_decl(struct nir_to_llvm_context
*ctx
,
4799 struct nir_variable
*variable
)
4801 int idx
= variable
->data
.location
+ variable
->data
.index
;
4802 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4803 uint64_t mask_attribs
;
4804 variable
->data
.driver_location
= idx
* 4;
4806 /* tess ctrl has it's own load/store paths for outputs */
4807 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
4810 mask_attribs
= ((1ull << attrib_count
) - 1) << idx
;
4811 if (ctx
->stage
== MESA_SHADER_VERTEX
||
4812 ctx
->stage
== MESA_SHADER_TESS_EVAL
||
4813 ctx
->stage
== MESA_SHADER_GEOMETRY
) {
4814 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
4815 int length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
4816 if (ctx
->stage
== MESA_SHADER_VERTEX
) {
4817 ctx
->shader_info
->vs
.outinfo
.clip_dist_mask
= (1 << ctx
->num_output_clips
) - 1;
4818 ctx
->shader_info
->vs
.outinfo
.cull_dist_mask
= (1 << ctx
->num_output_culls
) - 1;
4820 if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
4821 ctx
->shader_info
->tes
.outinfo
.clip_dist_mask
= (1 << ctx
->num_output_clips
) - 1;
4822 ctx
->shader_info
->tes
.outinfo
.cull_dist_mask
= (1 << ctx
->num_output_culls
) - 1;
4829 mask_attribs
= 1ull << idx
;
4833 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
4834 for (unsigned chan
= 0; chan
< 4; chan
++) {
4835 ctx
->outputs
[radeon_llvm_reg_index_soa(idx
+ i
, chan
)] =
4836 si_build_alloca_undef(ctx
, ctx
->f32
, "");
4839 ctx
->output_mask
|= mask_attribs
;
4843 setup_locals(struct nir_to_llvm_context
*ctx
,
4844 struct nir_function
*func
)
4847 ctx
->num_locals
= 0;
4848 nir_foreach_variable(variable
, &func
->impl
->locals
) {
4849 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4850 variable
->data
.driver_location
= ctx
->num_locals
* 4;
4851 ctx
->num_locals
+= attrib_count
;
4853 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
4857 for (i
= 0; i
< ctx
->num_locals
; i
++) {
4858 for (j
= 0; j
< 4; j
++) {
4859 ctx
->locals
[i
* 4 + j
] =
4860 si_build_alloca_undef(ctx
, ctx
->f32
, "temp");
4866 emit_float_saturate(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
, float lo
, float hi
)
4868 v
= to_float(ctx
, v
);
4869 v
= emit_intrin_2f_param(ctx
, "llvm.maxnum.f32", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, lo
));
4870 return emit_intrin_2f_param(ctx
, "llvm.minnum.f32", ctx
->f32
, v
, LLVMConstReal(ctx
->f32
, hi
));
4874 static LLVMValueRef
emit_pack_int16(struct nir_to_llvm_context
*ctx
,
4875 LLVMValueRef src0
, LLVMValueRef src1
)
4877 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
4878 LLVMValueRef comp
[2];
4880 comp
[0] = LLVMBuildAnd(ctx
->builder
, src0
, LLVMConstInt(ctx
-> i32
, 65535, 0), "");
4881 comp
[1] = LLVMBuildAnd(ctx
->builder
, src1
, LLVMConstInt(ctx
-> i32
, 65535, 0), "");
4882 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
4883 return LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
4886 /* Initialize arguments for the shader export intrinsic */
4888 si_llvm_init_export_args(struct nir_to_llvm_context
*ctx
,
4889 LLVMValueRef
*values
,
4891 struct ac_export_args
*args
)
4893 /* Default is 0xf. Adjusted below depending on the format. */
4894 args
->enabled_channels
= 0xf;
4896 /* Specify whether the EXEC mask represents the valid mask */
4897 args
->valid_mask
= 0;
4899 /* Specify whether this is the last export */
4902 /* Specify the target we are exporting */
4903 args
->target
= target
;
4905 args
->compr
= false;
4906 args
->out
[0] = LLVMGetUndef(ctx
->f32
);
4907 args
->out
[1] = LLVMGetUndef(ctx
->f32
);
4908 args
->out
[2] = LLVMGetUndef(ctx
->f32
);
4909 args
->out
[3] = LLVMGetUndef(ctx
->f32
);
4914 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&& target
>= V_008DFC_SQ_EXP_MRT
) {
4915 LLVMValueRef val
[4];
4916 unsigned index
= target
- V_008DFC_SQ_EXP_MRT
;
4917 unsigned col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * index
)) & 0xf;
4918 bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> index
) & 1;
4920 switch(col_format
) {
4921 case V_028714_SPI_SHADER_ZERO
:
4922 args
->enabled_channels
= 0; /* writemask */
4923 args
->target
= V_008DFC_SQ_EXP_NULL
;
4926 case V_028714_SPI_SHADER_32_R
:
4927 args
->enabled_channels
= 1;
4928 args
->out
[0] = values
[0];
4931 case V_028714_SPI_SHADER_32_GR
:
4932 args
->enabled_channels
= 0x3;
4933 args
->out
[0] = values
[0];
4934 args
->out
[1] = values
[1];
4937 case V_028714_SPI_SHADER_32_AR
:
4938 args
->enabled_channels
= 0x9;
4939 args
->out
[0] = values
[0];
4940 args
->out
[3] = values
[3];
4943 case V_028714_SPI_SHADER_FP16_ABGR
:
4946 for (unsigned chan
= 0; chan
< 2; chan
++) {
4947 LLVMValueRef pack_args
[2] = {
4949 values
[2 * chan
+ 1]
4951 LLVMValueRef packed
;
4953 packed
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, pack_args
);
4954 args
->out
[chan
] = packed
;
4958 case V_028714_SPI_SHADER_UNORM16_ABGR
:
4959 for (unsigned chan
= 0; chan
< 4; chan
++) {
4960 val
[chan
] = ac_build_clamp(&ctx
->ac
, values
[chan
]);
4961 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
4962 LLVMConstReal(ctx
->f32
, 65535), "");
4963 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
4964 LLVMConstReal(ctx
->f32
, 0.5), "");
4965 val
[chan
] = LLVMBuildFPToUI(ctx
->builder
, val
[chan
],
4970 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
4971 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
4974 case V_028714_SPI_SHADER_SNORM16_ABGR
:
4975 for (unsigned chan
= 0; chan
< 4; chan
++) {
4976 val
[chan
] = emit_float_saturate(ctx
, values
[chan
], -1, 1);
4977 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
4978 LLVMConstReal(ctx
->f32
, 32767), "");
4980 /* If positive, add 0.5, else add -0.5. */
4981 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
4982 LLVMBuildSelect(ctx
->builder
,
4983 LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
,
4984 val
[chan
], ctx
->f32zero
, ""),
4985 LLVMConstReal(ctx
->f32
, 0.5),
4986 LLVMConstReal(ctx
->f32
, -0.5), ""), "");
4987 val
[chan
] = LLVMBuildFPToSI(ctx
->builder
, val
[chan
], ctx
->i32
, "");
4991 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
4992 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
4995 case V_028714_SPI_SHADER_UINT16_ABGR
: {
4996 LLVMValueRef max
= LLVMConstInt(ctx
->i32
, is_int8
? 255 : 65535, 0);
4998 for (unsigned chan
= 0; chan
< 4; chan
++) {
4999 val
[chan
] = to_integer(ctx
, values
[chan
]);
5000 val
[chan
] = emit_minmax_int(ctx
, LLVMIntULT
, val
[chan
], max
);
5004 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5005 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5009 case V_028714_SPI_SHADER_SINT16_ABGR
: {
5010 LLVMValueRef max
= LLVMConstInt(ctx
->i32
, is_int8
? 127 : 32767, 0);
5011 LLVMValueRef min
= LLVMConstInt(ctx
->i32
, is_int8
? -128 : -32768, 0);
5014 for (unsigned chan
= 0; chan
< 4; chan
++) {
5015 val
[chan
] = to_integer(ctx
, values
[chan
]);
5016 val
[chan
] = emit_minmax_int(ctx
, LLVMIntSLT
, val
[chan
], max
);
5017 val
[chan
] = emit_minmax_int(ctx
, LLVMIntSGT
, val
[chan
], min
);
5021 args
->out
[0] = emit_pack_int16(ctx
, val
[0], val
[1]);
5022 args
->out
[1] = emit_pack_int16(ctx
, val
[2], val
[3]);
5027 case V_028714_SPI_SHADER_32_ABGR
:
5028 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
5032 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
5034 for (unsigned i
= 0; i
< 4; ++i
)
5035 args
->out
[i
] = to_float(ctx
, args
->out
[i
]);
5039 handle_vs_outputs_post(struct nir_to_llvm_context
*ctx
,
5040 struct ac_vs_output_info
*outinfo
)
5042 uint32_t param_count
= 0;
5044 unsigned pos_idx
, num_pos_exports
= 0;
5045 struct ac_export_args args
, pos_args
[4] = {};
5046 LLVMValueRef psize_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
5049 outinfo
->prim_id_output
= 0xffffffff;
5050 outinfo
->layer_output
= 0xffffffff;
5051 if (ctx
->output_mask
& (1ull << VARYING_SLOT_CLIP_DIST0
)) {
5052 LLVMValueRef slots
[8];
5055 if (outinfo
->cull_dist_mask
)
5056 outinfo
->cull_dist_mask
<<= ctx
->num_output_clips
;
5058 i
= VARYING_SLOT_CLIP_DIST0
;
5059 for (j
= 0; j
< ctx
->num_output_clips
+ ctx
->num_output_culls
; j
++)
5060 slots
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
5061 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5063 for (i
= ctx
->num_output_clips
+ ctx
->num_output_culls
; i
< 8; i
++)
5064 slots
[i
] = LLVMGetUndef(ctx
->f32
);
5066 if (ctx
->num_output_clips
+ ctx
->num_output_culls
> 4) {
5067 target
= V_008DFC_SQ_EXP_POS
+ 3;
5068 si_llvm_init_export_args(ctx
, &slots
[4], target
, &args
);
5069 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5070 &args
, sizeof(args
));
5073 target
= V_008DFC_SQ_EXP_POS
+ 2;
5074 si_llvm_init_export_args(ctx
, &slots
[0], target
, &args
);
5075 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5076 &args
, sizeof(args
));
5080 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5081 LLVMValueRef values
[4];
5082 if (!(ctx
->output_mask
& (1ull << i
)))
5085 for (unsigned j
= 0; j
< 4; j
++)
5086 values
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
5087 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5089 if (i
== VARYING_SLOT_POS
) {
5090 target
= V_008DFC_SQ_EXP_POS
;
5091 } else if (i
== VARYING_SLOT_CLIP_DIST0
) {
5093 } else if (i
== VARYING_SLOT_PSIZ
) {
5094 outinfo
->writes_pointsize
= true;
5095 psize_value
= values
[0];
5097 } else if (i
== VARYING_SLOT_LAYER
) {
5098 outinfo
->writes_layer
= true;
5099 layer_value
= values
[0];
5100 outinfo
->layer_output
= param_count
;
5101 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5103 } else if (i
== VARYING_SLOT_VIEWPORT
) {
5104 outinfo
->writes_viewport_index
= true;
5105 viewport_index_value
= values
[0];
5107 } else if (i
== VARYING_SLOT_PRIMITIVE_ID
) {
5108 outinfo
->prim_id_output
= param_count
;
5109 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5111 } else if (i
>= VARYING_SLOT_VAR0
) {
5112 outinfo
->export_mask
|= 1u << (i
- VARYING_SLOT_VAR0
);
5113 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
5117 si_llvm_init_export_args(ctx
, values
, target
, &args
);
5119 if (target
>= V_008DFC_SQ_EXP_POS
&&
5120 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
5121 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
5122 &args
, sizeof(args
));
5124 ac_build_export(&ctx
->ac
, &args
);
5128 /* We need to add the position output manually if it's missing. */
5129 if (!pos_args
[0].out
[0]) {
5130 pos_args
[0].enabled_channels
= 0xf;
5131 pos_args
[0].valid_mask
= 0;
5132 pos_args
[0].done
= 0;
5133 pos_args
[0].target
= V_008DFC_SQ_EXP_POS
;
5134 pos_args
[0].compr
= 0;
5135 pos_args
[0].out
[0] = ctx
->f32zero
; /* X */
5136 pos_args
[0].out
[1] = ctx
->f32zero
; /* Y */
5137 pos_args
[0].out
[2] = ctx
->f32zero
; /* Z */
5138 pos_args
[0].out
[3] = ctx
->f32one
; /* W */
5141 uint32_t mask
= ((outinfo
->writes_pointsize
== true ? 1 : 0) |
5142 (outinfo
->writes_layer
== true ? 4 : 0) |
5143 (outinfo
->writes_viewport_index
== true ? 8 : 0));
5145 pos_args
[1].enabled_channels
= mask
;
5146 pos_args
[1].valid_mask
= 0;
5147 pos_args
[1].done
= 0;
5148 pos_args
[1].target
= V_008DFC_SQ_EXP_POS
+ 1;
5149 pos_args
[1].compr
= 0;
5150 pos_args
[1].out
[0] = ctx
->f32zero
; /* X */
5151 pos_args
[1].out
[1] = ctx
->f32zero
; /* Y */
5152 pos_args
[1].out
[2] = ctx
->f32zero
; /* Z */
5153 pos_args
[1].out
[3] = ctx
->f32zero
; /* W */
5155 if (outinfo
->writes_pointsize
== true)
5156 pos_args
[1].out
[0] = psize_value
;
5157 if (outinfo
->writes_layer
== true)
5158 pos_args
[1].out
[2] = layer_value
;
5159 if (outinfo
->writes_viewport_index
== true)
5160 pos_args
[1].out
[3] = viewport_index_value
;
5162 for (i
= 0; i
< 4; i
++) {
5163 if (pos_args
[i
].out
[0])
5168 for (i
= 0; i
< 4; i
++) {
5169 if (!pos_args
[i
].out
[0])
5172 /* Specify the target we are exporting */
5173 pos_args
[i
].target
= V_008DFC_SQ_EXP_POS
+ pos_idx
++;
5174 if (pos_idx
== num_pos_exports
)
5175 pos_args
[i
].done
= 1;
5176 ac_build_export(&ctx
->ac
, &pos_args
[i
]);
5179 outinfo
->pos_exports
= num_pos_exports
;
5180 outinfo
->param_exports
= param_count
;
5184 handle_es_outputs_post(struct nir_to_llvm_context
*ctx
,
5185 struct ac_es_output_info
*outinfo
)
5188 uint64_t max_output_written
= 0;
5189 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5190 LLVMValueRef
*out_ptr
= &ctx
->outputs
[i
* 4];
5194 if (!(ctx
->output_mask
& (1ull << i
)))
5197 if (i
== VARYING_SLOT_CLIP_DIST0
)
5198 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
5200 param_index
= shader_io_get_unique_index(i
);
5202 max_output_written
= MAX2(param_index
+ (length
> 4), max_output_written
);
5204 for (j
= 0; j
< length
; j
++) {
5205 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], "");
5206 out_val
= LLVMBuildBitCast(ctx
->builder
, out_val
, ctx
->i32
, "");
5208 ac_build_buffer_store_dword(&ctx
->ac
,
5211 NULL
, ctx
->es2gs_offset
,
5212 (4 * param_index
+ j
) * 4,
5216 outinfo
->esgs_itemsize
= (max_output_written
+ 1) * 16;
5220 handle_ls_outputs_post(struct nir_to_llvm_context
*ctx
)
5222 LLVMValueRef vertex_id
= ctx
->rel_auto_id
;
5223 LLVMValueRef vertex_dw_stride
= unpack_param(ctx
, ctx
->ls_out_layout
, 13, 8);
5224 LLVMValueRef base_dw_addr
= LLVMBuildMul(ctx
->builder
, vertex_id
,
5225 vertex_dw_stride
, "");
5227 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5228 LLVMValueRef
*out_ptr
= &ctx
->outputs
[i
* 4];
5231 if (!(ctx
->output_mask
& (1ull << i
)))
5234 if (i
== VARYING_SLOT_CLIP_DIST0
)
5235 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
5236 int param
= shader_io_get_unique_index(i
);
5237 mark_tess_output(ctx
, false, param
);
5239 mark_tess_output(ctx
, false, param
+ 1);
5240 LLVMValueRef dw_addr
= LLVMBuildAdd(ctx
->builder
, base_dw_addr
,
5241 LLVMConstInt(ctx
->i32
, param
* 4, false),
5243 for (unsigned j
= 0; j
< length
; j
++) {
5244 lds_store(ctx
, dw_addr
,
5245 LLVMBuildLoad(ctx
->builder
, out_ptr
[j
], ""));
5246 dw_addr
= LLVMBuildAdd(ctx
->builder
, dw_addr
, ctx
->i32one
, "");
5251 struct ac_build_if_state
5253 struct nir_to_llvm_context
*ctx
;
5254 LLVMValueRef condition
;
5255 LLVMBasicBlockRef entry_block
;
5256 LLVMBasicBlockRef true_block
;
5257 LLVMBasicBlockRef false_block
;
5258 LLVMBasicBlockRef merge_block
;
5261 static LLVMBasicBlockRef
5262 ac_build_insert_new_block(struct nir_to_llvm_context
*ctx
, const char *name
)
5264 LLVMBasicBlockRef current_block
;
5265 LLVMBasicBlockRef next_block
;
5266 LLVMBasicBlockRef new_block
;
5268 /* get current basic block */
5269 current_block
= LLVMGetInsertBlock(ctx
->builder
);
5271 /* chqeck if there's another block after this one */
5272 next_block
= LLVMGetNextBasicBlock(current_block
);
5274 /* insert the new block before the next block */
5275 new_block
= LLVMInsertBasicBlockInContext(ctx
->context
, next_block
, name
);
5278 /* append new block after current block */
5279 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
5280 new_block
= LLVMAppendBasicBlockInContext(ctx
->context
, function
, name
);
5286 ac_nir_build_if(struct ac_build_if_state
*ifthen
,
5287 struct nir_to_llvm_context
*ctx
,
5288 LLVMValueRef condition
)
5290 LLVMBasicBlockRef block
= LLVMGetInsertBlock(ctx
->builder
);
5292 memset(ifthen
, 0, sizeof *ifthen
);
5294 ifthen
->condition
= condition
;
5295 ifthen
->entry_block
= block
;
5297 /* create endif/merge basic block for the phi functions */
5298 ifthen
->merge_block
= ac_build_insert_new_block(ctx
, "endif-block");
5300 /* create/insert true_block before merge_block */
5301 ifthen
->true_block
=
5302 LLVMInsertBasicBlockInContext(ctx
->context
,
5303 ifthen
->merge_block
,
5306 /* successive code goes into the true block */
5307 LLVMPositionBuilderAtEnd(ctx
->builder
, ifthen
->true_block
);
5311 * End a conditional.
5314 ac_nir_build_endif(struct ac_build_if_state
*ifthen
)
5316 LLVMBuilderRef builder
= ifthen
->ctx
->builder
;
5318 /* Insert branch to the merge block from current block */
5319 LLVMBuildBr(builder
, ifthen
->merge_block
);
5322 * Now patch in the various branch instructions.
5325 /* Insert the conditional branch instruction at the end of entry_block */
5326 LLVMPositionBuilderAtEnd(builder
, ifthen
->entry_block
);
5327 if (ifthen
->false_block
) {
5328 /* we have an else clause */
5329 LLVMBuildCondBr(builder
, ifthen
->condition
,
5330 ifthen
->true_block
, ifthen
->false_block
);
5333 /* no else clause */
5334 LLVMBuildCondBr(builder
, ifthen
->condition
,
5335 ifthen
->true_block
, ifthen
->merge_block
);
5338 /* Resume building code at end of the ifthen->merge_block */
5339 LLVMPositionBuilderAtEnd(builder
, ifthen
->merge_block
);
5343 write_tess_factors(struct nir_to_llvm_context
*ctx
)
5345 unsigned stride
, outer_comps
, inner_comps
;
5346 struct ac_build_if_state if_ctx
, inner_if_ctx
;
5347 LLVMValueRef invocation_id
= unpack_param(ctx
, ctx
->tcs_rel_ids
, 8, 5);
5348 LLVMValueRef rel_patch_id
= unpack_param(ctx
, ctx
->tcs_rel_ids
, 0, 8);
5349 unsigned tess_inner_index
, tess_outer_index
;
5350 LLVMValueRef lds_base
, lds_inner
, lds_outer
, byteoffset
, buffer
;
5351 LLVMValueRef out
[6], vec0
, vec1
, tf_base
, inner
[4], outer
[4];
5355 switch (ctx
->options
->key
.tcs
.primitive_mode
) {
5375 ac_nir_build_if(&if_ctx
, ctx
,
5376 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
5377 invocation_id
, ctx
->i32zero
, ""));
5379 tess_inner_index
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
5380 tess_outer_index
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
5382 mark_tess_output(ctx
, true, tess_inner_index
);
5383 mark_tess_output(ctx
, true, tess_outer_index
);
5384 lds_base
= get_tcs_out_current_patch_data_offset(ctx
);
5385 lds_inner
= LLVMBuildAdd(ctx
->builder
, lds_base
,
5386 LLVMConstInt(ctx
->i32
, tess_inner_index
* 4, false), "");
5387 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_base
,
5388 LLVMConstInt(ctx
->i32
, tess_outer_index
* 4, false), "");
5390 for (i
= 0; i
< 4; i
++) {
5391 inner
[i
] = LLVMGetUndef(ctx
->i32
);
5392 outer
[i
] = LLVMGetUndef(ctx
->i32
);
5396 if (ctx
->options
->key
.tcs
.primitive_mode
== GL_ISOLINES
) {
5397 outer
[0] = out
[1] = lds_load(ctx
, lds_outer
);
5398 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_outer
,
5399 LLVMConstInt(ctx
->i32
, 1, false), "");
5400 outer
[1] = out
[0] = lds_load(ctx
, lds_outer
);
5402 for (i
= 0; i
< outer_comps
; i
++) {
5404 lds_load(ctx
, lds_outer
);
5405 lds_outer
= LLVMBuildAdd(ctx
->builder
, lds_outer
,
5406 LLVMConstInt(ctx
->i32
, 1, false), "");
5408 for (i
= 0; i
< inner_comps
; i
++) {
5409 inner
[i
] = out
[outer_comps
+i
] =
5410 lds_load(ctx
, lds_inner
);
5411 lds_inner
= LLVMBuildAdd(ctx
->builder
, lds_inner
,
5412 LLVMConstInt(ctx
->i32
, 1, false), "");
5416 /* Convert the outputs to vectors for stores. */
5417 vec0
= ac_build_gather_values(&ctx
->ac
, out
, MIN2(stride
, 4));
5421 vec1
= ac_build_gather_values(&ctx
->ac
, out
+ 4, stride
- 4);
5424 buffer
= ctx
->hs_ring_tess_factor
;
5425 tf_base
= ctx
->tess_factor_offset
;
5426 byteoffset
= LLVMBuildMul(ctx
->builder
, rel_patch_id
,
5427 LLVMConstInt(ctx
->i32
, 4 * stride
, false), "");
5429 ac_nir_build_if(&inner_if_ctx
, ctx
,
5430 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
5431 rel_patch_id
, ctx
->i32zero
, ""));
5433 /* Store the dynamic HS control word. */
5434 ac_build_buffer_store_dword(&ctx
->ac
, buffer
,
5435 LLVMConstInt(ctx
->i32
, 0x80000000, false),
5436 1, ctx
->i32zero
, tf_base
,
5437 0, 1, 0, true, false);
5438 ac_nir_build_endif(&inner_if_ctx
);
5440 /* Store the tessellation factors. */
5441 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec0
,
5442 MIN2(stride
, 4), byteoffset
, tf_base
,
5443 4, 1, 0, true, false);
5445 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec1
,
5446 stride
- 4, byteoffset
, tf_base
,
5447 20, 1, 0, true, false);
5449 //TODO store to offchip for TES to read - only if TES reads them
5451 LLVMValueRef inner_vec
, outer_vec
, tf_outer_offset
;
5452 LLVMValueRef tf_inner_offset
;
5453 unsigned param_outer
, param_inner
;
5455 param_outer
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
5456 tf_outer_offset
= get_tcs_tes_buffer_address(ctx
, NULL
,
5457 LLVMConstInt(ctx
->i32
, param_outer
, 0));
5459 outer_vec
= ac_build_gather_values(&ctx
->ac
, outer
,
5460 util_next_power_of_two(outer_comps
));
5462 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, outer_vec
,
5463 outer_comps
, tf_outer_offset
,
5464 ctx
->oc_lds
, 0, 1, 0, true, false);
5466 param_inner
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
5467 tf_inner_offset
= get_tcs_tes_buffer_address(ctx
, NULL
,
5468 LLVMConstInt(ctx
->i32
, param_inner
, 0));
5470 inner_vec
= inner_comps
== 1 ? inner
[0] :
5471 ac_build_gather_values(&ctx
->ac
, inner
, inner_comps
);
5472 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, inner_vec
,
5473 inner_comps
, tf_inner_offset
,
5474 ctx
->oc_lds
, 0, 1, 0, true, false);
5477 ac_nir_build_endif(&if_ctx
);
5481 handle_tcs_outputs_post(struct nir_to_llvm_context
*ctx
)
5483 write_tess_factors(ctx
);
5487 si_export_mrt_color(struct nir_to_llvm_context
*ctx
,
5488 LLVMValueRef
*color
, unsigned param
, bool is_last
)
5491 struct ac_export_args args
;
5494 si_llvm_init_export_args(ctx
, color
, param
,
5498 args
.valid_mask
= 1; /* whether the EXEC mask is valid */
5499 args
.done
= 1; /* DONE bit */
5500 } else if (!args
.enabled_channels
)
5501 return; /* unnecessary NULL export */
5503 ac_build_export(&ctx
->ac
, &args
);
5507 si_export_mrt_z(struct nir_to_llvm_context
*ctx
,
5508 LLVMValueRef depth
, LLVMValueRef stencil
,
5509 LLVMValueRef samplemask
)
5511 struct ac_export_args args
;
5513 args
.enabled_channels
= 0;
5514 args
.valid_mask
= 1;
5516 args
.target
= V_008DFC_SQ_EXP_MRTZ
;
5519 args
.out
[0] = LLVMGetUndef(ctx
->f32
); /* R, depth */
5520 args
.out
[1] = LLVMGetUndef(ctx
->f32
); /* G, stencil test val[0:7], stencil op val[8:15] */
5521 args
.out
[2] = LLVMGetUndef(ctx
->f32
); /* B, sample mask */
5522 args
.out
[3] = LLVMGetUndef(ctx
->f32
); /* A, alpha to mask */
5525 args
.out
[0] = depth
;
5526 args
.enabled_channels
|= 0x1;
5530 args
.out
[1] = stencil
;
5531 args
.enabled_channels
|= 0x2;
5535 args
.out
[2] = samplemask
;
5536 args
.enabled_channels
|= 0x4;
5539 /* SI (except OLAND) has a bug that it only looks
5540 * at the X writemask component. */
5541 if (ctx
->options
->chip_class
== SI
&&
5542 ctx
->options
->family
!= CHIP_OLAND
)
5543 args
.enabled_channels
|= 0x1;
5545 ac_build_export(&ctx
->ac
, &args
);
5549 handle_fs_outputs_post(struct nir_to_llvm_context
*ctx
)
5552 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
5554 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
5555 LLVMValueRef values
[4];
5557 if (!(ctx
->output_mask
& (1ull << i
)))
5560 if (i
== FRAG_RESULT_DEPTH
) {
5561 ctx
->shader_info
->fs
.writes_z
= true;
5562 depth
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
5563 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
5564 } else if (i
== FRAG_RESULT_STENCIL
) {
5565 ctx
->shader_info
->fs
.writes_stencil
= true;
5566 stencil
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
5567 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
5568 } else if (i
== FRAG_RESULT_SAMPLE_MASK
) {
5569 ctx
->shader_info
->fs
.writes_sample_mask
= true;
5570 samplemask
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
5571 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
5574 for (unsigned j
= 0; j
< 4; j
++)
5575 values
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
5576 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
5578 if (!ctx
->shader_info
->fs
.writes_z
&& !ctx
->shader_info
->fs
.writes_stencil
&& !ctx
->shader_info
->fs
.writes_sample_mask
)
5579 last
= ctx
->output_mask
<= ((1ull << (i
+ 1)) - 1);
5581 si_export_mrt_color(ctx
, values
, V_008DFC_SQ_EXP_MRT
+ index
, last
);
5586 if (depth
|| stencil
|| samplemask
)
5587 si_export_mrt_z(ctx
, depth
, stencil
, samplemask
);
5589 si_export_mrt_color(ctx
, NULL
, V_008DFC_SQ_EXP_NULL
, true);
5591 ctx
->shader_info
->fs
.output_mask
= index
? ((1ull << index
) - 1) : 0;
5595 emit_gs_epilogue(struct nir_to_llvm_context
*ctx
)
5597 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_NOP
| AC_SENDMSG_GS_DONE
, ctx
->gs_wave_id
);
5601 handle_shader_outputs_post(struct nir_to_llvm_context
*ctx
)
5603 switch (ctx
->stage
) {
5604 case MESA_SHADER_VERTEX
:
5605 if (ctx
->options
->key
.vs
.as_ls
)
5606 handle_ls_outputs_post(ctx
);
5607 else if (ctx
->options
->key
.vs
.as_es
)
5608 handle_es_outputs_post(ctx
, &ctx
->shader_info
->vs
.es_info
);
5610 handle_vs_outputs_post(ctx
, &ctx
->shader_info
->vs
.outinfo
);
5612 case MESA_SHADER_FRAGMENT
:
5613 handle_fs_outputs_post(ctx
);
5615 case MESA_SHADER_GEOMETRY
:
5616 emit_gs_epilogue(ctx
);
5618 case MESA_SHADER_TESS_CTRL
:
5619 handle_tcs_outputs_post(ctx
);
5621 case MESA_SHADER_TESS_EVAL
:
5622 if (ctx
->options
->key
.tes
.as_es
)
5623 handle_es_outputs_post(ctx
, &ctx
->shader_info
->tes
.es_info
);
5625 handle_vs_outputs_post(ctx
, &ctx
->shader_info
->tes
.outinfo
);
5633 handle_shared_compute_var(struct nir_to_llvm_context
*ctx
,
5634 struct nir_variable
*variable
, uint32_t *offset
, int idx
)
5636 unsigned size
= glsl_count_attribute_slots(variable
->type
, false);
5637 variable
->data
.driver_location
= *offset
;
5641 static void ac_llvm_finalize_module(struct nir_to_llvm_context
* ctx
)
5643 LLVMPassManagerRef passmgr
;
5644 /* Create the pass manager */
5645 passmgr
= LLVMCreateFunctionPassManagerForModule(
5648 /* This pass should eliminate all the load and store instructions */
5649 LLVMAddPromoteMemoryToRegisterPass(passmgr
);
5651 /* Add some optimization passes */
5652 LLVMAddScalarReplAggregatesPass(passmgr
);
5653 LLVMAddLICMPass(passmgr
);
5654 LLVMAddAggressiveDCEPass(passmgr
);
5655 LLVMAddCFGSimplificationPass(passmgr
);
5656 LLVMAddInstructionCombiningPass(passmgr
);
5659 LLVMInitializeFunctionPassManager(passmgr
);
5660 LLVMRunFunctionPassManager(passmgr
, ctx
->main_function
);
5661 LLVMFinalizeFunctionPassManager(passmgr
);
5663 LLVMDisposeBuilder(ctx
->builder
);
5664 LLVMDisposePassManager(passmgr
);
5668 ac_setup_rings(struct nir_to_llvm_context
*ctx
)
5670 if ((ctx
->stage
== MESA_SHADER_VERTEX
&& ctx
->options
->key
.vs
.as_es
) ||
5671 (ctx
->stage
== MESA_SHADER_TESS_EVAL
&& ctx
->options
->key
.tes
.as_es
)) {
5672 ctx
->esgs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_ESGS_VS
, false));
5675 if (ctx
->is_gs_copy_shader
) {
5676 ctx
->gsvs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_GSVS_VS
, false));
5678 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
5680 ctx
->esgs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_ESGS_GS
, false));
5681 ctx
->gsvs_ring
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_GSVS_GS
, false));
5683 ctx
->gsvs_ring
= LLVMBuildBitCast(ctx
->builder
, ctx
->gsvs_ring
, ctx
->v4i32
, "");
5685 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->gsvs_num_entries
, LLVMConstInt(ctx
->i32
, 2, false), "");
5686 tmp
= LLVMBuildExtractElement(ctx
->builder
, ctx
->gsvs_ring
, ctx
->i32one
, "");
5687 tmp
= LLVMBuildOr(ctx
->builder
, tmp
, ctx
->gsvs_ring_stride
, "");
5688 ctx
->gsvs_ring
= LLVMBuildInsertElement(ctx
->builder
, ctx
->gsvs_ring
, tmp
, ctx
->i32one
, "");
5690 ctx
->gsvs_ring
= LLVMBuildBitCast(ctx
->builder
, ctx
->gsvs_ring
, ctx
->v16i8
, "");
5693 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
5694 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
5695 ctx
->hs_ring_tess_offchip
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_HS_TESS_OFFCHIP
, false));
5696 ctx
->hs_ring_tess_factor
= ac_build_indexed_load_const(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->i32
, RING_HS_TESS_FACTOR
, false));
5701 LLVMModuleRef
ac_translate_nir_to_llvm(LLVMTargetMachineRef tm
,
5702 struct nir_shader
*nir
,
5703 struct ac_shader_variant_info
*shader_info
,
5704 const struct ac_nir_compiler_options
*options
)
5706 struct nir_to_llvm_context ctx
= {0};
5707 struct nir_function
*func
;
5709 ctx
.options
= options
;
5710 ctx
.shader_info
= shader_info
;
5711 ctx
.context
= LLVMContextCreate();
5712 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
5714 ac_llvm_context_init(&ctx
.ac
, ctx
.context
);
5715 ctx
.ac
.module
= ctx
.module
;
5717 ctx
.has_ds_bpermute
= ctx
.options
->chip_class
>= VI
;
5719 memset(shader_info
, 0, sizeof(*shader_info
));
5721 ac_nir_shader_info_pass(nir
, options
, &shader_info
->info
);
5723 LLVMSetTarget(ctx
.module
, options
->supports_spill
? "amdgcn-mesa-mesa3d" : "amdgcn--");
5725 LLVMTargetDataRef data_layout
= LLVMCreateTargetDataLayout(tm
);
5726 char *data_layout_str
= LLVMCopyStringRepOfTargetData(data_layout
);
5727 LLVMSetDataLayout(ctx
.module
, data_layout_str
);
5728 LLVMDisposeTargetData(data_layout
);
5729 LLVMDisposeMessage(data_layout_str
);
5733 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
5734 ctx
.ac
.builder
= ctx
.builder
;
5735 ctx
.stage
= nir
->stage
;
5737 for (i
= 0; i
< AC_UD_MAX_SETS
; i
++)
5738 shader_info
->user_sgprs_locs
.descriptor_sets
[i
].sgpr_idx
= -1;
5739 for (i
= 0; i
< AC_UD_MAX_UD
; i
++)
5740 shader_info
->user_sgprs_locs
.shader_data
[i
].sgpr_idx
= -1;
5742 create_function(&ctx
);
5744 if (nir
->stage
== MESA_SHADER_COMPUTE
) {
5746 nir_foreach_variable(variable
, &nir
->shared
)
5750 uint32_t shared_size
= 0;
5752 LLVMTypeRef i8p
= LLVMPointerType(ctx
.i8
, LOCAL_ADDR_SPACE
);
5753 nir_foreach_variable(variable
, &nir
->shared
) {
5754 handle_shared_compute_var(&ctx
, variable
, &shared_size
, idx
);
5759 var
= LLVMAddGlobalInAddressSpace(ctx
.module
,
5760 LLVMArrayType(ctx
.i8
, shared_size
),
5763 LLVMSetAlignment(var
, 4);
5764 ctx
.shared_memory
= LLVMBuildBitCast(ctx
.builder
, var
, i8p
, "");
5766 } else if (nir
->stage
== MESA_SHADER_GEOMETRY
) {
5767 ctx
.gs_next_vertex
= ac_build_alloca(&ctx
, ctx
.i32
, "gs_next_vertex");
5769 ctx
.gs_max_out_vertices
= nir
->info
->gs
.vertices_out
;
5770 } else if (nir
->stage
== MESA_SHADER_TESS_EVAL
) {
5771 ctx
.tes_primitive_mode
= nir
->info
->tess
.primitive_mode
;
5774 ac_setup_rings(&ctx
);
5776 nir_foreach_variable(variable
, &nir
->inputs
)
5777 handle_shader_input_decl(&ctx
, variable
);
5779 if (nir
->stage
== MESA_SHADER_FRAGMENT
)
5780 handle_fs_inputs_pre(&ctx
, nir
);
5782 ctx
.num_output_clips
= nir
->info
->clip_distance_array_size
;
5783 ctx
.num_output_culls
= nir
->info
->cull_distance_array_size
;
5785 nir_foreach_variable(variable
, &nir
->outputs
)
5786 handle_shader_output_decl(&ctx
, variable
);
5788 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
5789 _mesa_key_pointer_equal
);
5790 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
5791 _mesa_key_pointer_equal
);
5793 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
5795 setup_locals(&ctx
, func
);
5797 visit_cf_list(&ctx
, &func
->impl
->body
);
5798 phi_post_pass(&ctx
);
5800 handle_shader_outputs_post(&ctx
);
5801 LLVMBuildRetVoid(ctx
.builder
);
5803 ac_llvm_finalize_module(&ctx
);
5805 ralloc_free(ctx
.defs
);
5806 ralloc_free(ctx
.phis
);
5808 if (nir
->stage
== MESA_SHADER_GEOMETRY
) {
5809 unsigned addclip
= ctx
.num_output_clips
+ ctx
.num_output_culls
> 4;
5810 shader_info
->gs
.gsvs_vertex_size
= (util_bitcount64(ctx
.output_mask
) + addclip
) * 16;
5811 shader_info
->gs
.max_gsvs_emit_size
= shader_info
->gs
.gsvs_vertex_size
*
5812 nir
->info
->gs
.vertices_out
;
5813 } else if (nir
->stage
== MESA_SHADER_TESS_CTRL
) {
5814 shader_info
->tcs
.outputs_written
= ctx
.tess_outputs_written
;
5815 shader_info
->tcs
.patch_outputs_written
= ctx
.tess_patch_outputs_written
;
5816 } else if (nir
->stage
== MESA_SHADER_VERTEX
&& ctx
.options
->key
.vs
.as_ls
) {
5817 shader_info
->vs
.outputs_written
= ctx
.tess_outputs_written
;
5823 static void ac_diagnostic_handler(LLVMDiagnosticInfoRef di
, void *context
)
5825 unsigned *retval
= (unsigned *)context
;
5826 LLVMDiagnosticSeverity severity
= LLVMGetDiagInfoSeverity(di
);
5827 char *description
= LLVMGetDiagInfoDescription(di
);
5829 if (severity
== LLVMDSError
) {
5831 fprintf(stderr
, "LLVM triggered Diagnostic Handler: %s\n",
5835 LLVMDisposeMessage(description
);
5838 static unsigned ac_llvm_compile(LLVMModuleRef M
,
5839 struct ac_shader_binary
*binary
,
5840 LLVMTargetMachineRef tm
)
5842 unsigned retval
= 0;
5844 LLVMContextRef llvm_ctx
;
5845 LLVMMemoryBufferRef out_buffer
;
5846 unsigned buffer_size
;
5847 const char *buffer_data
;
5850 /* Setup Diagnostic Handler*/
5851 llvm_ctx
= LLVMGetModuleContext(M
);
5853 LLVMContextSetDiagnosticHandler(llvm_ctx
, ac_diagnostic_handler
,
5857 mem_err
= LLVMTargetMachineEmitToMemoryBuffer(tm
, M
, LLVMObjectFile
,
5860 /* Process Errors/Warnings */
5862 fprintf(stderr
, "%s: %s", __FUNCTION__
, err
);
5868 /* Extract Shader Code*/
5869 buffer_size
= LLVMGetBufferSize(out_buffer
);
5870 buffer_data
= LLVMGetBufferStart(out_buffer
);
5872 ac_elf_read(buffer_data
, buffer_size
, binary
);
5875 LLVMDisposeMemoryBuffer(out_buffer
);
5881 static void ac_compile_llvm_module(LLVMTargetMachineRef tm
,
5882 LLVMModuleRef llvm_module
,
5883 struct ac_shader_binary
*binary
,
5884 struct ac_shader_config
*config
,
5885 struct ac_shader_variant_info
*shader_info
,
5886 gl_shader_stage stage
,
5887 bool dump_shader
, bool supports_spill
)
5890 ac_dump_module(llvm_module
);
5892 memset(binary
, 0, sizeof(*binary
));
5893 int v
= ac_llvm_compile(llvm_module
, binary
, tm
);
5895 fprintf(stderr
, "compile failed\n");
5899 fprintf(stderr
, "disasm:\n%s\n", binary
->disasm_string
);
5901 ac_shader_binary_read_config(binary
, config
, 0, supports_spill
);
5903 LLVMContextRef ctx
= LLVMGetModuleContext(llvm_module
);
5904 LLVMDisposeModule(llvm_module
);
5905 LLVMContextDispose(ctx
);
5907 if (stage
== MESA_SHADER_FRAGMENT
) {
5908 shader_info
->num_input_vgprs
= 0;
5909 if (G_0286CC_PERSP_SAMPLE_ENA(config
->spi_ps_input_addr
))
5910 shader_info
->num_input_vgprs
+= 2;
5911 if (G_0286CC_PERSP_CENTER_ENA(config
->spi_ps_input_addr
))
5912 shader_info
->num_input_vgprs
+= 2;
5913 if (G_0286CC_PERSP_CENTROID_ENA(config
->spi_ps_input_addr
))
5914 shader_info
->num_input_vgprs
+= 2;
5915 if (G_0286CC_PERSP_PULL_MODEL_ENA(config
->spi_ps_input_addr
))
5916 shader_info
->num_input_vgprs
+= 3;
5917 if (G_0286CC_LINEAR_SAMPLE_ENA(config
->spi_ps_input_addr
))
5918 shader_info
->num_input_vgprs
+= 2;
5919 if (G_0286CC_LINEAR_CENTER_ENA(config
->spi_ps_input_addr
))
5920 shader_info
->num_input_vgprs
+= 2;
5921 if (G_0286CC_LINEAR_CENTROID_ENA(config
->spi_ps_input_addr
))
5922 shader_info
->num_input_vgprs
+= 2;
5923 if (G_0286CC_LINE_STIPPLE_TEX_ENA(config
->spi_ps_input_addr
))
5924 shader_info
->num_input_vgprs
+= 1;
5925 if (G_0286CC_POS_X_FLOAT_ENA(config
->spi_ps_input_addr
))
5926 shader_info
->num_input_vgprs
+= 1;
5927 if (G_0286CC_POS_Y_FLOAT_ENA(config
->spi_ps_input_addr
))
5928 shader_info
->num_input_vgprs
+= 1;
5929 if (G_0286CC_POS_Z_FLOAT_ENA(config
->spi_ps_input_addr
))
5930 shader_info
->num_input_vgprs
+= 1;
5931 if (G_0286CC_POS_W_FLOAT_ENA(config
->spi_ps_input_addr
))
5932 shader_info
->num_input_vgprs
+= 1;
5933 if (G_0286CC_FRONT_FACE_ENA(config
->spi_ps_input_addr
))
5934 shader_info
->num_input_vgprs
+= 1;
5935 if (G_0286CC_ANCILLARY_ENA(config
->spi_ps_input_addr
))
5936 shader_info
->num_input_vgprs
+= 1;
5937 if (G_0286CC_SAMPLE_COVERAGE_ENA(config
->spi_ps_input_addr
))
5938 shader_info
->num_input_vgprs
+= 1;
5939 if (G_0286CC_POS_FIXED_PT_ENA(config
->spi_ps_input_addr
))
5940 shader_info
->num_input_vgprs
+= 1;
5942 config
->num_vgprs
= MAX2(config
->num_vgprs
, shader_info
->num_input_vgprs
);
5944 /* +3 for scratch wave offset and VCC */
5945 config
->num_sgprs
= MAX2(config
->num_sgprs
,
5946 shader_info
->num_input_sgprs
+ 3);
5949 void ac_compile_nir_shader(LLVMTargetMachineRef tm
,
5950 struct ac_shader_binary
*binary
,
5951 struct ac_shader_config
*config
,
5952 struct ac_shader_variant_info
*shader_info
,
5953 struct nir_shader
*nir
,
5954 const struct ac_nir_compiler_options
*options
,
5958 LLVMModuleRef llvm_module
= ac_translate_nir_to_llvm(tm
, nir
, shader_info
,
5961 ac_compile_llvm_module(tm
, llvm_module
, binary
, config
, shader_info
, nir
->stage
, dump_shader
, options
->supports_spill
);
5962 switch (nir
->stage
) {
5963 case MESA_SHADER_COMPUTE
:
5964 for (int i
= 0; i
< 3; ++i
)
5965 shader_info
->cs
.block_size
[i
] = nir
->info
->cs
.local_size
[i
];
5967 case MESA_SHADER_FRAGMENT
:
5968 shader_info
->fs
.early_fragment_test
= nir
->info
->fs
.early_fragment_tests
;
5970 case MESA_SHADER_GEOMETRY
:
5971 shader_info
->gs
.vertices_in
= nir
->info
->gs
.vertices_in
;
5972 shader_info
->gs
.vertices_out
= nir
->info
->gs
.vertices_out
;
5973 shader_info
->gs
.output_prim
= nir
->info
->gs
.output_primitive
;
5974 shader_info
->gs
.invocations
= nir
->info
->gs
.invocations
;
5976 case MESA_SHADER_TESS_EVAL
:
5977 shader_info
->tes
.primitive_mode
= nir
->info
->tess
.primitive_mode
;
5978 shader_info
->tes
.spacing
= nir
->info
->tess
.spacing
;
5979 shader_info
->tes
.ccw
= nir
->info
->tess
.ccw
;
5980 shader_info
->tes
.point_mode
= nir
->info
->tess
.point_mode
;
5981 shader_info
->tes
.as_es
= options
->key
.tes
.as_es
;
5983 case MESA_SHADER_TESS_CTRL
:
5984 shader_info
->tcs
.tcs_vertices_out
= nir
->info
->tess
.tcs_vertices_out
;
5986 case MESA_SHADER_VERTEX
:
5987 shader_info
->vs
.as_es
= options
->key
.vs
.as_es
;
5988 shader_info
->vs
.as_ls
= options
->key
.vs
.as_ls
;
5989 /* in LS mode we need at least 1, invocation id needs 3, handled elsewhere */
5990 if (options
->key
.vs
.as_ls
)
5991 shader_info
->vs
.vgpr_comp_cnt
= MAX2(1, shader_info
->vs
.vgpr_comp_cnt
);
5999 ac_gs_copy_shader_emit(struct nir_to_llvm_context
*ctx
)
6001 LLVMValueRef args
[9];
6002 args
[0] = ctx
->gsvs_ring
;
6003 args
[1] = LLVMBuildMul(ctx
->builder
, ctx
->vertex_id
, LLVMConstInt(ctx
->i32
, 4, false), "");
6004 args
[3] = ctx
->i32zero
;
6005 args
[4] = ctx
->i32one
; /* OFFEN */
6006 args
[5] = ctx
->i32zero
; /* IDXEN */
6007 args
[6] = ctx
->i32one
; /* GLC */
6008 args
[7] = ctx
->i32one
; /* SLC */
6009 args
[8] = ctx
->i32zero
; /* TFE */
6013 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
6017 if (!(ctx
->output_mask
& (1ull << i
)))
6020 if (i
== VARYING_SLOT_CLIP_DIST0
) {
6021 /* unpack clip and cull from a single set of slots */
6022 length
= ctx
->num_output_clips
+ ctx
->num_output_culls
;
6027 for (unsigned j
= 0; j
< length
; j
++) {
6029 args
[2] = LLVMConstInt(ctx
->i32
,
6031 ctx
->gs_max_out_vertices
* 16 * 4, false);
6033 value
= ac_build_intrinsic(&ctx
->ac
,
6034 "llvm.SI.buffer.load.dword.i32.i32",
6036 AC_FUNC_ATTR_READONLY
|
6037 AC_FUNC_ATTR_LEGACY
);
6039 LLVMBuildStore(ctx
->builder
,
6040 to_float(ctx
, value
), ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)]);
6044 handle_vs_outputs_post(ctx
, &ctx
->shader_info
->vs
.outinfo
);
6047 void ac_create_gs_copy_shader(LLVMTargetMachineRef tm
,
6048 struct nir_shader
*geom_shader
,
6049 struct ac_shader_binary
*binary
,
6050 struct ac_shader_config
*config
,
6051 struct ac_shader_variant_info
*shader_info
,
6052 const struct ac_nir_compiler_options
*options
,
6055 struct nir_to_llvm_context ctx
= {0};
6056 ctx
.context
= LLVMContextCreate();
6057 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
6058 ctx
.options
= options
;
6059 ctx
.shader_info
= shader_info
;
6061 ac_llvm_context_init(&ctx
.ac
, ctx
.context
);
6062 ctx
.ac
.module
= ctx
.module
;
6064 ctx
.is_gs_copy_shader
= true;
6065 LLVMSetTarget(ctx
.module
, "amdgcn--");
6068 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
6069 ctx
.ac
.builder
= ctx
.builder
;
6070 ctx
.stage
= MESA_SHADER_VERTEX
;
6072 create_function(&ctx
);
6074 ctx
.gs_max_out_vertices
= geom_shader
->info
->gs
.vertices_out
;
6075 ac_setup_rings(&ctx
);
6077 ctx
.num_output_clips
= geom_shader
->info
->clip_distance_array_size
;
6078 ctx
.num_output_culls
= geom_shader
->info
->cull_distance_array_size
;
6080 nir_foreach_variable(variable
, &geom_shader
->outputs
)
6081 handle_shader_output_decl(&ctx
, variable
);
6083 ac_gs_copy_shader_emit(&ctx
);
6085 LLVMBuildRetVoid(ctx
.builder
);
6087 ac_llvm_finalize_module(&ctx
);
6089 ac_compile_llvm_module(tm
, ctx
.module
, binary
, config
, shader_info
,
6091 dump_shader
, options
->supports_spill
);