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_util.h"
26 #include "ac_binary.h"
29 #include "../vulkan/radv_descriptor_set.h"
30 #include "util/bitscan.h"
31 #include <llvm-c/Transforms/Scalar.h>
33 enum radeon_llvm_calling_convention
{
34 RADEON_LLVM_AMDGPU_VS
= 87,
35 RADEON_LLVM_AMDGPU_GS
= 88,
36 RADEON_LLVM_AMDGPU_PS
= 89,
37 RADEON_LLVM_AMDGPU_CS
= 90,
40 #define CONST_ADDR_SPACE 2
41 #define LOCAL_ADDR_SPACE 3
43 #define RADEON_LLVM_MAX_INPUTS (VARYING_SLOT_VAR31 + 1)
44 #define RADEON_LLVM_MAX_OUTPUTS (VARYING_SLOT_VAR31 + 1)
53 struct nir_to_llvm_context
{
54 struct ac_llvm_context ac
;
55 const struct ac_nir_compiler_options
*options
;
56 struct ac_shader_variant_info
*shader_info
;
58 LLVMContextRef context
;
60 LLVMBuilderRef builder
;
61 LLVMValueRef main_function
;
63 struct hash_table
*defs
;
64 struct hash_table
*phis
;
66 LLVMValueRef descriptor_sets
[AC_UD_MAX_SETS
];
67 LLVMValueRef push_constants
;
68 LLVMValueRef num_work_groups
;
69 LLVMValueRef workgroup_ids
;
70 LLVMValueRef local_invocation_ids
;
73 LLVMValueRef vertex_buffers
;
74 LLVMValueRef base_vertex
;
75 LLVMValueRef start_instance
;
76 LLVMValueRef vertex_id
;
77 LLVMValueRef rel_auto_id
;
78 LLVMValueRef vs_prim_id
;
79 LLVMValueRef instance_id
;
81 LLVMValueRef prim_mask
;
82 LLVMValueRef sample_positions
;
83 LLVMValueRef persp_sample
, persp_center
, persp_centroid
;
84 LLVMValueRef linear_sample
, linear_center
, linear_centroid
;
85 LLVMValueRef front_face
;
86 LLVMValueRef ancillary
;
87 LLVMValueRef frag_pos
[4];
89 LLVMBasicBlockRef continue_block
;
90 LLVMBasicBlockRef break_block
;
108 LLVMValueRef i32zero
;
110 LLVMValueRef f32zero
;
112 LLVMValueRef v4f32empty
;
114 unsigned range_md_kind
;
115 unsigned uniform_md_kind
;
116 unsigned invariant_load_md_kind
;
117 LLVMValueRef empty_md
;
118 gl_shader_stage stage
;
121 LLVMValueRef inputs
[RADEON_LLVM_MAX_INPUTS
* 4];
122 LLVMValueRef outputs
[RADEON_LLVM_MAX_OUTPUTS
* 4];
124 LLVMValueRef shared_memory
;
126 uint64_t output_mask
;
128 LLVMValueRef
*locals
;
133 bool has_ds_bpermute
;
137 LLVMValueRef args
[12];
139 LLVMTypeRef dst_type
;
143 static LLVMValueRef
get_sampler_desc(struct nir_to_llvm_context
*ctx
,
144 nir_deref_var
*deref
,
145 enum desc_type desc_type
);
146 static unsigned radeon_llvm_reg_index_soa(unsigned index
, unsigned chan
)
148 return (index
* 4) + chan
;
151 static unsigned llvm_get_type_size(LLVMTypeRef type
)
153 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
156 case LLVMIntegerTypeKind
:
157 return LLVMGetIntTypeWidth(type
) / 8;
158 case LLVMFloatTypeKind
:
160 case LLVMPointerTypeKind
:
162 case LLVMVectorTypeKind
:
163 return LLVMGetVectorSize(type
) *
164 llvm_get_type_size(LLVMGetElementType(type
));
171 static void set_llvm_calling_convention(LLVMValueRef func
,
172 gl_shader_stage stage
)
174 enum radeon_llvm_calling_convention calling_conv
;
177 case MESA_SHADER_VERTEX
:
178 case MESA_SHADER_TESS_CTRL
:
179 case MESA_SHADER_TESS_EVAL
:
180 calling_conv
= RADEON_LLVM_AMDGPU_VS
;
182 case MESA_SHADER_GEOMETRY
:
183 calling_conv
= RADEON_LLVM_AMDGPU_GS
;
185 case MESA_SHADER_FRAGMENT
:
186 calling_conv
= RADEON_LLVM_AMDGPU_PS
;
188 case MESA_SHADER_COMPUTE
:
189 calling_conv
= RADEON_LLVM_AMDGPU_CS
;
192 unreachable("Unhandle shader type");
195 LLVMSetFunctionCallConv(func
, calling_conv
);
199 create_llvm_function(LLVMContextRef ctx
, LLVMModuleRef module
,
200 LLVMBuilderRef builder
, LLVMTypeRef
*return_types
,
201 unsigned num_return_elems
, LLVMTypeRef
*param_types
,
202 unsigned param_count
, unsigned array_params_mask
,
203 unsigned sgpr_params
, bool unsafe_math
)
205 LLVMTypeRef main_function_type
, ret_type
;
206 LLVMBasicBlockRef main_function_body
;
208 if (num_return_elems
)
209 ret_type
= LLVMStructTypeInContext(ctx
, return_types
,
210 num_return_elems
, true);
212 ret_type
= LLVMVoidTypeInContext(ctx
);
214 /* Setup the function */
216 LLVMFunctionType(ret_type
, param_types
, param_count
, 0);
217 LLVMValueRef main_function
=
218 LLVMAddFunction(module
, "main", main_function_type
);
220 LLVMAppendBasicBlockInContext(ctx
, main_function
, "main_body");
221 LLVMPositionBuilderAtEnd(builder
, main_function_body
);
223 LLVMSetFunctionCallConv(main_function
, RADEON_LLVM_AMDGPU_CS
);
224 for (unsigned i
= 0; i
< sgpr_params
; ++i
) {
225 if (array_params_mask
& (1 << i
)) {
226 LLVMValueRef P
= LLVMGetParam(main_function
, i
);
227 ac_add_function_attr(main_function
, i
+ 1, AC_FUNC_ATTR_BYVAL
);
228 ac_add_attr_dereferenceable(P
, UINT64_MAX
);
231 ac_add_function_attr(main_function
, i
+ 1, AC_FUNC_ATTR_INREG
);
236 /* These were copied from some LLVM test. */
237 LLVMAddTargetDependentFunctionAttr(main_function
,
238 "less-precise-fpmad",
240 LLVMAddTargetDependentFunctionAttr(main_function
,
243 LLVMAddTargetDependentFunctionAttr(main_function
,
246 LLVMAddTargetDependentFunctionAttr(main_function
,
250 return main_function
;
253 static LLVMTypeRef
const_array(LLVMTypeRef elem_type
, int num_elements
)
255 return LLVMPointerType(LLVMArrayType(elem_type
, num_elements
),
259 static LLVMValueRef
get_shared_memory_ptr(struct nir_to_llvm_context
*ctx
,
267 offset
= LLVMConstInt(ctx
->i32
, idx
, false);
269 ptr
= ctx
->shared_memory
;
270 ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &offset
, 1, "");
271 addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
272 ptr
= LLVMBuildBitCast(ctx
->builder
, ptr
, LLVMPointerType(type
, addr_space
), "");
276 static LLVMValueRef
to_integer(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
)
278 LLVMTypeRef type
= LLVMTypeOf(v
);
279 if (type
== ctx
->f32
) {
280 return LLVMBuildBitCast(ctx
->builder
, v
, ctx
->i32
, "");
281 } else if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
282 LLVMTypeRef elem_type
= LLVMGetElementType(type
);
283 if (elem_type
== ctx
->f32
) {
284 LLVMTypeRef nt
= LLVMVectorType(ctx
->i32
, LLVMGetVectorSize(type
));
285 return LLVMBuildBitCast(ctx
->builder
, v
, nt
, "");
291 static LLVMValueRef
to_float(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
)
293 LLVMTypeRef type
= LLVMTypeOf(v
);
294 if (type
== ctx
->i32
) {
295 return LLVMBuildBitCast(ctx
->builder
, v
, ctx
->f32
, "");
296 } else if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
297 LLVMTypeRef elem_type
= LLVMGetElementType(type
);
298 if (elem_type
== ctx
->i32
) {
299 LLVMTypeRef nt
= LLVMVectorType(ctx
->f32
, LLVMGetVectorSize(type
));
300 return LLVMBuildBitCast(ctx
->builder
, v
, nt
, "");
306 static LLVMValueRef
unpack_param(struct nir_to_llvm_context
*ctx
,
307 LLVMValueRef param
, unsigned rshift
,
310 LLVMValueRef value
= param
;
312 value
= LLVMBuildLShr(ctx
->builder
, value
,
313 LLVMConstInt(ctx
->i32
, rshift
, false), "");
315 if (rshift
+ bitwidth
< 32) {
316 unsigned mask
= (1 << bitwidth
) - 1;
317 value
= LLVMBuildAnd(ctx
->builder
, value
,
318 LLVMConstInt(ctx
->i32
, mask
, false), "");
323 static LLVMValueRef
build_gep0(struct nir_to_llvm_context
*ctx
,
324 LLVMValueRef base_ptr
, LLVMValueRef index
)
326 LLVMValueRef indices
[2] = {
330 return LLVMBuildGEP(ctx
->builder
, base_ptr
,
334 static LLVMValueRef
build_indexed_load(struct nir_to_llvm_context
*ctx
,
335 LLVMValueRef base_ptr
, LLVMValueRef index
,
338 LLVMValueRef pointer
;
339 pointer
= build_gep0(ctx
, base_ptr
, index
);
341 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
342 return LLVMBuildLoad(ctx
->builder
, pointer
, "");
345 static LLVMValueRef
build_indexed_load_const(struct nir_to_llvm_context
*ctx
,
346 LLVMValueRef base_ptr
, LLVMValueRef index
)
348 LLVMValueRef result
= build_indexed_load(ctx
, base_ptr
, index
, true);
349 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
353 static void set_userdata_location(struct ac_userdata_info
*ud_info
, uint8_t sgpr_idx
, uint8_t num_sgprs
)
355 ud_info
->sgpr_idx
= sgpr_idx
;
356 ud_info
->num_sgprs
= num_sgprs
;
357 ud_info
->indirect
= false;
358 ud_info
->indirect_offset
= 0;
361 static void set_userdata_location_shader(struct nir_to_llvm_context
*ctx
,
362 int idx
, uint8_t sgpr_idx
, uint8_t num_sgprs
)
364 set_userdata_location(&ctx
->shader_info
->user_sgprs_locs
.shader_data
[idx
], sgpr_idx
, num_sgprs
);
368 static void set_userdata_location_indirect(struct ac_userdata_info
*ud_info
, uint8_t sgpr_idx
, uint8_t num_sgprs
,
369 uint32_t indirect_offset
)
371 ud_info
->sgpr_idx
= sgpr_idx
;
372 ud_info
->num_sgprs
= num_sgprs
;
373 ud_info
->indirect
= true;
374 ud_info
->indirect_offset
= indirect_offset
;
378 static void create_function(struct nir_to_llvm_context
*ctx
)
380 LLVMTypeRef arg_types
[23];
381 unsigned arg_idx
= 0;
382 unsigned array_params_mask
= 0;
383 unsigned sgpr_count
= 0, user_sgpr_count
;
385 unsigned num_sets
= ctx
->options
->layout
? ctx
->options
->layout
->num_sets
: 0;
386 unsigned user_sgpr_idx
;
387 bool need_push_constants
;
389 need_push_constants
= true;
390 if (!ctx
->options
->layout
)
391 need_push_constants
= false;
392 else if (!ctx
->options
->layout
->push_constant_size
&&
393 !ctx
->options
->layout
->dynamic_offset_count
)
394 need_push_constants
= false;
396 /* 1 for each descriptor set */
397 for (unsigned i
= 0; i
< num_sets
; ++i
) {
398 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& (1 << ctx
->stage
)) {
399 array_params_mask
|= (1 << arg_idx
);
400 arg_types
[arg_idx
++] = const_array(ctx
->i8
, 1024 * 1024);
404 if (need_push_constants
) {
405 /* 1 for push constants and dynamic descriptors */
406 array_params_mask
|= (1 << arg_idx
);
407 arg_types
[arg_idx
++] = const_array(ctx
->i8
, 1024 * 1024);
410 switch (ctx
->stage
) {
411 case MESA_SHADER_COMPUTE
:
412 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3); /* grid size */
413 user_sgpr_count
= arg_idx
;
414 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3);
415 arg_types
[arg_idx
++] = ctx
->i32
;
416 sgpr_count
= arg_idx
;
418 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3);
420 case MESA_SHADER_VERTEX
:
421 arg_types
[arg_idx
++] = const_array(ctx
->v16i8
, 16); /* vertex buffers */
422 arg_types
[arg_idx
++] = ctx
->i32
; // base vertex
423 arg_types
[arg_idx
++] = ctx
->i32
; // start instance
424 user_sgpr_count
= sgpr_count
= arg_idx
;
425 arg_types
[arg_idx
++] = ctx
->i32
; // vertex id
426 arg_types
[arg_idx
++] = ctx
->i32
; // rel auto id
427 arg_types
[arg_idx
++] = ctx
->i32
; // vs prim id
428 arg_types
[arg_idx
++] = ctx
->i32
; // instance id
430 case MESA_SHADER_FRAGMENT
:
431 arg_types
[arg_idx
++] = const_array(ctx
->f32
, 32); /* sample positions */
432 user_sgpr_count
= arg_idx
;
433 arg_types
[arg_idx
++] = ctx
->i32
; /* prim mask */
434 sgpr_count
= arg_idx
;
435 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp sample */
436 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp center */
437 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp centroid */
438 arg_types
[arg_idx
++] = ctx
->v3i32
; /* persp pull model */
439 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear sample */
440 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear center */
441 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear centroid */
442 arg_types
[arg_idx
++] = ctx
->f32
; /* line stipple tex */
443 arg_types
[arg_idx
++] = ctx
->f32
; /* pos x float */
444 arg_types
[arg_idx
++] = ctx
->f32
; /* pos y float */
445 arg_types
[arg_idx
++] = ctx
->f32
; /* pos z float */
446 arg_types
[arg_idx
++] = ctx
->f32
; /* pos w float */
447 arg_types
[arg_idx
++] = ctx
->i32
; /* front face */
448 arg_types
[arg_idx
++] = ctx
->i32
; /* ancillary */
449 arg_types
[arg_idx
++] = ctx
->f32
; /* sample coverage */
450 arg_types
[arg_idx
++] = ctx
->i32
; /* fixed pt */
453 unreachable("Shader stage not implemented");
456 ctx
->main_function
= create_llvm_function(
457 ctx
->context
, ctx
->module
, ctx
->builder
, NULL
, 0, arg_types
,
458 arg_idx
, array_params_mask
, sgpr_count
, ctx
->options
->unsafe_math
);
459 set_llvm_calling_convention(ctx
->main_function
, ctx
->stage
);
461 ctx
->shader_info
->num_input_sgprs
= 0;
462 ctx
->shader_info
->num_input_vgprs
= 0;
464 ctx
->shader_info
->num_user_sgprs
= ctx
->options
->supports_spill
? 2 : 0;
465 for (i
= 0; i
< user_sgpr_count
; i
++)
466 ctx
->shader_info
->num_user_sgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
468 ctx
->shader_info
->num_input_sgprs
= ctx
->shader_info
->num_user_sgprs
;
469 for (; i
< sgpr_count
; i
++)
470 ctx
->shader_info
->num_input_sgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
472 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
473 for (; i
< arg_idx
; ++i
)
474 ctx
->shader_info
->num_input_vgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
479 if (ctx
->options
->supports_spill
) {
480 set_userdata_location_shader(ctx
, AC_UD_SCRATCH
, user_sgpr_idx
, 2);
484 for (unsigned i
= 0; i
< num_sets
; ++i
) {
485 if (ctx
->options
->layout
->set
[i
].layout
->shader_stages
& (1 << ctx
->stage
)) {
486 set_userdata_location(&ctx
->shader_info
->user_sgprs_locs
.descriptor_sets
[i
], user_sgpr_idx
, 2);
488 ctx
->descriptor_sets
[i
] =
489 LLVMGetParam(ctx
->main_function
, arg_idx
++);
491 ctx
->descriptor_sets
[i
] = NULL
;
494 if (need_push_constants
) {
495 ctx
->push_constants
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
496 set_userdata_location_shader(ctx
, AC_UD_PUSH_CONSTANTS
, user_sgpr_idx
, 2);
500 switch (ctx
->stage
) {
501 case MESA_SHADER_COMPUTE
:
502 set_userdata_location_shader(ctx
, AC_UD_CS_GRID_SIZE
, user_sgpr_idx
, 3);
504 ctx
->num_work_groups
=
505 LLVMGetParam(ctx
->main_function
, arg_idx
++);
507 LLVMGetParam(ctx
->main_function
, arg_idx
++);
509 LLVMGetParam(ctx
->main_function
, arg_idx
++);
510 ctx
->local_invocation_ids
=
511 LLVMGetParam(ctx
->main_function
, arg_idx
++);
513 case MESA_SHADER_VERTEX
:
514 set_userdata_location_shader(ctx
, AC_UD_VS_VERTEX_BUFFERS
, user_sgpr_idx
, 2);
516 ctx
->vertex_buffers
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
517 set_userdata_location_shader(ctx
, AC_UD_VS_BASE_VERTEX_START_INSTANCE
, user_sgpr_idx
, 2);
519 ctx
->base_vertex
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
520 ctx
->start_instance
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
521 ctx
->vertex_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
522 ctx
->rel_auto_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
523 ctx
->vs_prim_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
524 ctx
->instance_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
526 case MESA_SHADER_FRAGMENT
:
527 set_userdata_location_shader(ctx
, AC_UD_PS_SAMPLE_POS
, user_sgpr_idx
, 2);
529 ctx
->sample_positions
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
530 ctx
->prim_mask
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
531 ctx
->persp_sample
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
532 ctx
->persp_center
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
533 ctx
->persp_centroid
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
535 ctx
->linear_sample
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
536 ctx
->linear_center
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
537 ctx
->linear_centroid
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
538 arg_idx
++; /* line stipple */
539 ctx
->frag_pos
[0] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
540 ctx
->frag_pos
[1] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
541 ctx
->frag_pos
[2] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
542 ctx
->frag_pos
[3] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
543 ctx
->front_face
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
544 ctx
->ancillary
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
547 unreachable("Shader stage not implemented");
551 static void setup_types(struct nir_to_llvm_context
*ctx
)
553 LLVMValueRef args
[4];
555 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->context
);
556 ctx
->i1
= LLVMIntTypeInContext(ctx
->context
, 1);
557 ctx
->i8
= LLVMIntTypeInContext(ctx
->context
, 8);
558 ctx
->i16
= LLVMIntTypeInContext(ctx
->context
, 16);
559 ctx
->i32
= LLVMIntTypeInContext(ctx
->context
, 32);
560 ctx
->i64
= LLVMIntTypeInContext(ctx
->context
, 64);
561 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
562 ctx
->v3i32
= LLVMVectorType(ctx
->i32
, 3);
563 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
564 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
565 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
566 ctx
->f16
= LLVMHalfTypeInContext(ctx
->context
);
567 ctx
->v2f32
= LLVMVectorType(ctx
->f32
, 2);
568 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
569 ctx
->v16i8
= LLVMVectorType(ctx
->i8
, 16);
571 ctx
->i32zero
= LLVMConstInt(ctx
->i32
, 0, false);
572 ctx
->i32one
= LLVMConstInt(ctx
->i32
, 1, false);
573 ctx
->f32zero
= LLVMConstReal(ctx
->f32
, 0.0);
574 ctx
->f32one
= LLVMConstReal(ctx
->f32
, 1.0);
576 args
[0] = ctx
->f32zero
;
577 args
[1] = ctx
->f32zero
;
578 args
[2] = ctx
->f32zero
;
579 args
[3] = ctx
->f32one
;
580 ctx
->v4f32empty
= LLVMConstVector(args
, 4);
582 ctx
->range_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
584 ctx
->invariant_load_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
585 "invariant.load", 14);
586 ctx
->uniform_md_kind
=
587 LLVMGetMDKindIDInContext(ctx
->context
, "amdgpu.uniform", 14);
588 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
590 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
593 static int get_llvm_num_components(LLVMValueRef value
)
595 LLVMTypeRef type
= LLVMTypeOf(value
);
596 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
597 ? LLVMGetVectorSize(type
)
599 return num_components
;
602 static LLVMValueRef
llvm_extract_elem(struct nir_to_llvm_context
*ctx
,
606 int count
= get_llvm_num_components(value
);
608 assert(index
< count
);
612 return LLVMBuildExtractElement(ctx
->builder
, value
,
613 LLVMConstInt(ctx
->i32
, index
, false), "");
616 static LLVMValueRef
trim_vector(struct nir_to_llvm_context
*ctx
,
617 LLVMValueRef value
, unsigned count
)
619 unsigned num_components
= get_llvm_num_components(value
);
620 if (count
== num_components
)
623 LLVMValueRef masks
[] = {
624 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
625 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false)};
628 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
631 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
632 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
636 build_store_values_extended(struct nir_to_llvm_context
*ctx
,
637 LLVMValueRef
*values
,
638 unsigned value_count
,
639 unsigned value_stride
,
642 LLVMBuilderRef builder
= ctx
->builder
;
645 if (value_count
== 1) {
646 LLVMBuildStore(builder
, vec
, values
[0]);
650 for (i
= 0; i
< value_count
; i
++) {
651 LLVMValueRef ptr
= values
[i
* value_stride
];
652 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
653 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
654 LLVMBuildStore(builder
, value
, ptr
);
658 static LLVMTypeRef
get_def_type(struct nir_to_llvm_context
*ctx
,
661 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->context
, def
->bit_size
);
662 if (def
->num_components
> 1) {
663 type
= LLVMVectorType(type
, def
->num_components
);
668 static LLVMValueRef
get_src(struct nir_to_llvm_context
*ctx
, nir_src src
)
671 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->defs
, src
.ssa
);
672 return (LLVMValueRef
)entry
->data
;
676 static LLVMBasicBlockRef
get_block(struct nir_to_llvm_context
*ctx
,
679 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->defs
, b
);
680 return (LLVMBasicBlockRef
)entry
->data
;
683 static LLVMValueRef
get_alu_src(struct nir_to_llvm_context
*ctx
,
685 unsigned num_components
)
687 LLVMValueRef value
= get_src(ctx
, src
.src
);
688 bool need_swizzle
= false;
691 LLVMTypeRef type
= LLVMTypeOf(value
);
692 unsigned src_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
693 ? LLVMGetVectorSize(type
)
696 for (unsigned i
= 0; i
< num_components
; ++i
) {
697 assert(src
.swizzle
[i
] < src_components
);
698 if (src
.swizzle
[i
] != i
)
702 if (need_swizzle
|| num_components
!= src_components
) {
703 LLVMValueRef masks
[] = {
704 LLVMConstInt(ctx
->i32
, src
.swizzle
[0], false),
705 LLVMConstInt(ctx
->i32
, src
.swizzle
[1], false),
706 LLVMConstInt(ctx
->i32
, src
.swizzle
[2], false),
707 LLVMConstInt(ctx
->i32
, src
.swizzle
[3], false)};
709 if (src_components
> 1 && num_components
== 1) {
710 value
= LLVMBuildExtractElement(ctx
->builder
, value
,
712 } else if (src_components
== 1 && num_components
> 1) {
713 LLVMValueRef values
[] = {value
, value
, value
, value
};
714 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
716 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
717 value
= LLVMBuildShuffleVector(ctx
->builder
, value
, value
,
726 static LLVMValueRef
emit_int_cmp(struct nir_to_llvm_context
*ctx
,
727 LLVMIntPredicate pred
, LLVMValueRef src0
,
730 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
731 return LLVMBuildSelect(ctx
->builder
, result
,
732 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
733 LLVMConstInt(ctx
->i32
, 0, false), "");
736 static LLVMValueRef
emit_float_cmp(struct nir_to_llvm_context
*ctx
,
737 LLVMRealPredicate pred
, LLVMValueRef src0
,
741 src0
= to_float(ctx
, src0
);
742 src1
= to_float(ctx
, src1
);
743 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
744 return LLVMBuildSelect(ctx
->builder
, result
,
745 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
746 LLVMConstInt(ctx
->i32
, 0, false), "");
749 static LLVMValueRef
emit_intrin_1f_param(struct nir_to_llvm_context
*ctx
,
753 LLVMValueRef params
[] = {
756 return ac_emit_llvm_intrinsic(&ctx
->ac
, intrin
, ctx
->f32
, params
, 1, AC_FUNC_ATTR_READNONE
);
759 static LLVMValueRef
emit_intrin_2f_param(struct nir_to_llvm_context
*ctx
,
761 LLVMValueRef src0
, LLVMValueRef src1
)
763 LLVMValueRef params
[] = {
767 return ac_emit_llvm_intrinsic(&ctx
->ac
, intrin
, ctx
->f32
, params
, 2, AC_FUNC_ATTR_READNONE
);
770 static LLVMValueRef
emit_intrin_3f_param(struct nir_to_llvm_context
*ctx
,
772 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
774 LLVMValueRef params
[] = {
779 return ac_emit_llvm_intrinsic(&ctx
->ac
, intrin
, ctx
->f32
, params
, 3, AC_FUNC_ATTR_READNONE
);
782 static LLVMValueRef
emit_bcsel(struct nir_to_llvm_context
*ctx
,
783 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
785 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
787 return LLVMBuildSelect(ctx
->builder
, v
, src1
, src2
, "");
790 static LLVMValueRef
emit_find_lsb(struct nir_to_llvm_context
*ctx
,
793 LLVMValueRef params
[2] = {
796 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
797 * add special code to check for x=0. The reason is that
798 * the LLVM behavior for x=0 is different from what we
801 * The hardware already implements the correct behavior.
803 LLVMConstInt(ctx
->i32
, 1, false),
805 return ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.cttz.i32", ctx
->i32
, params
, 2, AC_FUNC_ATTR_READNONE
);
808 static LLVMValueRef
emit_ifind_msb(struct nir_to_llvm_context
*ctx
,
811 LLVMValueRef msb
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.AMDGPU.flbit.i32",
813 AC_FUNC_ATTR_READNONE
);
815 /* The HW returns the last bit index from MSB, but NIR wants
816 * the index from LSB. Invert it by doing "31 - msb". */
817 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
820 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
821 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
822 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
823 src0
, ctx
->i32zero
, ""),
824 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
825 src0
, all_ones
, ""), "");
827 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
830 static LLVMValueRef
emit_ufind_msb(struct nir_to_llvm_context
*ctx
,
833 LLVMValueRef args
[2] = {
837 LLVMValueRef msb
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.ctlz.i32",
838 ctx
->i32
, args
, ARRAY_SIZE(args
),
839 AC_FUNC_ATTR_READNONE
);
841 /* The HW returns the last bit index from MSB, but NIR wants
842 * the index from LSB. Invert it by doing "31 - msb". */
843 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
846 return LLVMBuildSelect(ctx
->builder
,
847 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src0
,
849 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
852 static LLVMValueRef
emit_minmax_int(struct nir_to_llvm_context
*ctx
,
853 LLVMIntPredicate pred
,
854 LLVMValueRef src0
, LLVMValueRef src1
)
856 return LLVMBuildSelect(ctx
->builder
,
857 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
862 static LLVMValueRef
emit_iabs(struct nir_to_llvm_context
*ctx
,
865 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
866 LLVMBuildNeg(ctx
->builder
, src0
, ""));
869 static LLVMValueRef
emit_fsign(struct nir_to_llvm_context
*ctx
,
872 LLVMValueRef cmp
, val
;
874 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, ctx
->f32zero
, "");
875 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->f32one
, src0
, "");
876 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, ctx
->f32zero
, "");
877 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(ctx
->f32
, -1.0), "");
881 static LLVMValueRef
emit_isign(struct nir_to_llvm_context
*ctx
,
884 LLVMValueRef cmp
, val
;
886 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, ctx
->i32zero
, "");
887 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->i32one
, src0
, "");
888 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, ctx
->i32zero
, "");
889 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(ctx
->i32
, -1, true), "");
893 static LLVMValueRef
emit_ffract(struct nir_to_llvm_context
*ctx
,
896 const char *intr
= "llvm.floor.f32";
897 LLVMValueRef fsrc0
= to_float(ctx
, src0
);
898 LLVMValueRef params
[] = {
901 LLVMValueRef floor
= ac_emit_llvm_intrinsic(&ctx
->ac
, intr
,
903 AC_FUNC_ATTR_READNONE
);
904 return LLVMBuildFSub(ctx
->builder
, fsrc0
, floor
, "");
907 static LLVMValueRef
emit_uint_carry(struct nir_to_llvm_context
*ctx
,
909 LLVMValueRef src0
, LLVMValueRef src1
)
911 LLVMTypeRef ret_type
;
912 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
914 LLVMValueRef params
[] = { src0
, src1
};
915 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
918 res
= ac_emit_llvm_intrinsic(&ctx
->ac
, intrin
, ret_type
,
919 params
, 2, AC_FUNC_ATTR_READNONE
);
921 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
922 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
926 static LLVMValueRef
emit_b2f(struct nir_to_llvm_context
*ctx
,
929 return LLVMBuildAnd(ctx
->builder
, src0
, LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""), "");
932 static LLVMValueRef
emit_umul_high(struct nir_to_llvm_context
*ctx
,
933 LLVMValueRef src0
, LLVMValueRef src1
)
935 LLVMValueRef dst64
, result
;
936 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
937 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
939 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
940 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
941 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
945 static LLVMValueRef
emit_imul_high(struct nir_to_llvm_context
*ctx
,
946 LLVMValueRef src0
, LLVMValueRef src1
)
948 LLVMValueRef dst64
, result
;
949 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
950 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
952 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
953 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
954 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
958 static LLVMValueRef
emit_bitfield_extract(struct nir_to_llvm_context
*ctx
,
960 LLVMValueRef srcs
[3])
963 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
964 result
= ac_emit_llvm_intrinsic(&ctx
->ac
, intrin
, ctx
->i32
, srcs
, 3, AC_FUNC_ATTR_READNONE
);
966 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
970 static LLVMValueRef
emit_bitfield_insert(struct nir_to_llvm_context
*ctx
,
971 LLVMValueRef src0
, LLVMValueRef src1
,
972 LLVMValueRef src2
, LLVMValueRef src3
)
974 LLVMValueRef bfi_args
[3], result
;
976 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
977 LLVMBuildSub(ctx
->builder
,
978 LLVMBuildShl(ctx
->builder
,
983 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
986 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
989 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
990 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
992 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
993 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
994 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
996 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
1000 static LLVMValueRef
emit_pack_half_2x16(struct nir_to_llvm_context
*ctx
,
1003 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1005 LLVMValueRef comp
[2];
1007 src0
= to_float(ctx
, src0
);
1008 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32zero
, "");
1009 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32one
, "");
1010 for (i
= 0; i
< 2; i
++) {
1011 comp
[i
] = LLVMBuildFPTrunc(ctx
->builder
, comp
[i
], ctx
->f16
, "");
1012 comp
[i
] = LLVMBuildBitCast(ctx
->builder
, comp
[i
], ctx
->i16
, "");
1013 comp
[i
] = LLVMBuildZExt(ctx
->builder
, comp
[i
], ctx
->i32
, "");
1016 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
1017 comp
[0] = LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
1022 static LLVMValueRef
emit_unpack_half_2x16(struct nir_to_llvm_context
*ctx
,
1025 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1026 LLVMValueRef temps
[2], result
, val
;
1029 for (i
= 0; i
< 2; i
++) {
1030 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
1031 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
1032 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
1033 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
1036 result
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), temps
[0],
1038 result
= LLVMBuildInsertElement(ctx
->builder
, result
, temps
[1],
1044 * Set range metadata on an instruction. This can only be used on load and
1045 * call instructions. If you know an instruction can only produce the values
1046 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
1047 * \p lo is the minimum value inclusive.
1048 * \p hi is the maximum value exclusive.
1050 static void set_range_metadata(struct nir_to_llvm_context
*ctx
,
1051 LLVMValueRef value
, unsigned lo
, unsigned hi
)
1053 LLVMValueRef range_md
, md_args
[2];
1054 LLVMTypeRef type
= LLVMTypeOf(value
);
1055 LLVMContextRef context
= LLVMGetTypeContext(type
);
1057 md_args
[0] = LLVMConstInt(type
, lo
, false);
1058 md_args
[1] = LLVMConstInt(type
, hi
, false);
1059 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
1060 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
1063 static LLVMValueRef
get_thread_id(struct nir_to_llvm_context
*ctx
)
1066 LLVMValueRef tid_args
[2];
1067 tid_args
[0] = LLVMConstInt(ctx
->i32
, 0xffffffff, false);
1068 tid_args
[1] = ctx
->i32zero
;
1069 tid_args
[1] = ac_emit_llvm_intrinsic(&ctx
->ac
,
1070 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
1071 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
1073 tid
= ac_emit_llvm_intrinsic(&ctx
->ac
,
1074 "llvm.amdgcn.mbcnt.hi", ctx
->i32
,
1075 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
1076 set_range_metadata(ctx
, tid
, 0, 64);
1081 * SI implements derivatives using the local data store (LDS)
1082 * All writes to the LDS happen in all executing threads at
1083 * the same time. TID is the Thread ID for the current
1084 * thread and is a value between 0 and 63, representing
1085 * the thread's position in the wavefront.
1087 * For the pixel shader threads are grouped into quads of four pixels.
1088 * The TIDs of the pixels of a quad are:
1096 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
1097 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
1098 * the current pixel's column, and masking with 0xfffffffe yields the TID
1099 * of the left pixel of the current pixel's row.
1101 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
1102 * adding 2 yields the TID of the pixel below the top pixel.
1104 /* masks for thread ID. */
1105 #define TID_MASK_TOP_LEFT 0xfffffffc
1106 #define TID_MASK_TOP 0xfffffffd
1107 #define TID_MASK_LEFT 0xfffffffe
1108 static LLVMValueRef
emit_ddxy(struct nir_to_llvm_context
*ctx
,
1112 LLVMValueRef tl
, trbl
, result
;
1113 LLVMValueRef tl_tid
, trbl_tid
;
1114 LLVMValueRef args
[2];
1115 LLVMValueRef thread_id
;
1118 ctx
->has_ddxy
= true;
1120 if (!ctx
->lds
&& !ctx
->has_ds_bpermute
)
1121 ctx
->lds
= LLVMAddGlobalInAddressSpace(ctx
->module
,
1122 LLVMArrayType(ctx
->i32
, 64),
1123 "ddxy_lds", LOCAL_ADDR_SPACE
);
1125 thread_id
= get_thread_id(ctx
);
1126 if (op
== nir_op_fddx_fine
|| op
== nir_op_fddx
)
1127 mask
= TID_MASK_LEFT
;
1128 else if (op
== nir_op_fddy_fine
|| op
== nir_op_fddy
)
1129 mask
= TID_MASK_TOP
;
1131 mask
= TID_MASK_TOP_LEFT
;
1133 tl_tid
= LLVMBuildAnd(ctx
->builder
, thread_id
,
1134 LLVMConstInt(ctx
->i32
, mask
, false), "");
1135 /* for DDX we want to next X pixel, DDY next Y pixel. */
1136 if (op
== nir_op_fddx_fine
||
1137 op
== nir_op_fddx_coarse
||
1143 trbl_tid
= LLVMBuildAdd(ctx
->builder
, tl_tid
,
1144 LLVMConstInt(ctx
->i32
, idx
, false), "");
1146 if (ctx
->has_ds_bpermute
) {
1147 args
[0] = LLVMBuildMul(ctx
->builder
, tl_tid
,
1148 LLVMConstInt(ctx
->i32
, 4, false), "");
1150 tl
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.amdgcn.ds.bpermute",
1152 AC_FUNC_ATTR_READNONE
);
1154 args
[0] = LLVMBuildMul(ctx
->builder
, trbl_tid
,
1155 LLVMConstInt(ctx
->i32
, 4, false), "");
1156 trbl
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.amdgcn.ds.bpermute",
1158 AC_FUNC_ATTR_READNONE
);
1160 LLVMValueRef store_ptr
, load_ptr0
, load_ptr1
;
1162 store_ptr
= build_gep0(ctx
, ctx
->lds
, thread_id
);
1163 load_ptr0
= build_gep0(ctx
, ctx
->lds
, tl_tid
);
1164 load_ptr1
= build_gep0(ctx
, ctx
->lds
, trbl_tid
);
1166 LLVMBuildStore(ctx
->builder
, src0
, store_ptr
);
1167 tl
= LLVMBuildLoad(ctx
->builder
, load_ptr0
, "");
1168 trbl
= LLVMBuildLoad(ctx
->builder
, load_ptr1
, "");
1170 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, ctx
->f32
, "");
1171 trbl
= LLVMBuildBitCast(ctx
->builder
, trbl
, ctx
->f32
, "");
1172 result
= LLVMBuildFSub(ctx
->builder
, trbl
, tl
, "");
1177 * this takes an I,J coordinate pair,
1178 * and works out the X and Y derivatives.
1179 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
1181 static LLVMValueRef
emit_ddxy_interp(
1182 struct nir_to_llvm_context
*ctx
,
1183 LLVMValueRef interp_ij
)
1185 LLVMValueRef result
[4], a
;
1188 for (i
= 0; i
< 2; i
++) {
1189 a
= LLVMBuildExtractElement(ctx
->builder
, interp_ij
,
1190 LLVMConstInt(ctx
->i32
, i
, false), "");
1191 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
1192 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
1194 return ac_build_gather_values(&ctx
->ac
, result
, 4);
1197 static void visit_alu(struct nir_to_llvm_context
*ctx
, nir_alu_instr
*instr
)
1199 LLVMValueRef src
[4], result
= NULL
;
1200 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
1201 unsigned src_components
;
1203 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
1204 switch (instr
->op
) {
1210 case nir_op_pack_half_2x16
:
1213 case nir_op_unpack_half_2x16
:
1217 src_components
= num_components
;
1220 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1221 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
1223 switch (instr
->op
) {
1229 src
[0] = to_float(ctx
, src
[0]);
1230 result
= LLVMBuildFNeg(ctx
->builder
, src
[0], "");
1233 result
= LLVMBuildNeg(ctx
->builder
, src
[0], "");
1236 result
= LLVMBuildNot(ctx
->builder
, src
[0], "");
1239 result
= LLVMBuildAdd(ctx
->builder
, src
[0], src
[1], "");
1242 src
[0] = to_float(ctx
, src
[0]);
1243 src
[1] = to_float(ctx
, src
[1]);
1244 result
= LLVMBuildFAdd(ctx
->builder
, src
[0], src
[1], "");
1247 src
[0] = to_float(ctx
, src
[0]);
1248 src
[1] = to_float(ctx
, src
[1]);
1249 result
= LLVMBuildFSub(ctx
->builder
, src
[0], src
[1], "");
1252 result
= LLVMBuildSub(ctx
->builder
, src
[0], src
[1], "");
1255 result
= LLVMBuildMul(ctx
->builder
, src
[0], src
[1], "");
1258 result
= LLVMBuildSRem(ctx
->builder
, src
[0], src
[1], "");
1261 result
= LLVMBuildURem(ctx
->builder
, src
[0], src
[1], "");
1264 src
[0] = to_float(ctx
, src
[0]);
1265 src
[1] = to_float(ctx
, src
[1]);
1266 result
= ac_emit_fdiv(&ctx
->ac
, src
[0], src
[1]);
1267 result
= emit_intrin_1f_param(ctx
, "llvm.floor.f32", result
);
1268 result
= LLVMBuildFMul(ctx
->builder
, src
[1] , result
, "");
1269 result
= LLVMBuildFSub(ctx
->builder
, src
[0], result
, "");
1272 src
[0] = to_float(ctx
, src
[0]);
1273 src
[1] = to_float(ctx
, src
[1]);
1274 result
= LLVMBuildFRem(ctx
->builder
, src
[0], src
[1], "");
1277 result
= LLVMBuildSDiv(ctx
->builder
, src
[0], src
[1], "");
1280 result
= LLVMBuildUDiv(ctx
->builder
, src
[0], src
[1], "");
1283 src
[0] = to_float(ctx
, src
[0]);
1284 src
[1] = to_float(ctx
, src
[1]);
1285 result
= LLVMBuildFMul(ctx
->builder
, src
[0], src
[1], "");
1288 src
[0] = to_float(ctx
, src
[0]);
1289 src
[1] = to_float(ctx
, src
[1]);
1290 result
= ac_emit_fdiv(&ctx
->ac
, src
[0], src
[1]);
1293 src
[0] = to_float(ctx
, src
[0]);
1294 result
= ac_emit_fdiv(&ctx
->ac
, ctx
->f32one
, src
[0]);
1297 result
= LLVMBuildAnd(ctx
->builder
, src
[0], src
[1], "");
1300 result
= LLVMBuildOr(ctx
->builder
, src
[0], src
[1], "");
1303 result
= LLVMBuildXor(ctx
->builder
, src
[0], src
[1], "");
1306 result
= LLVMBuildShl(ctx
->builder
, src
[0], src
[1], "");
1309 result
= LLVMBuildAShr(ctx
->builder
, src
[0], src
[1], "");
1312 result
= LLVMBuildLShr(ctx
->builder
, src
[0], src
[1], "");
1315 result
= emit_int_cmp(ctx
, LLVMIntSLT
, src
[0], src
[1]);
1318 result
= emit_int_cmp(ctx
, LLVMIntNE
, src
[0], src
[1]);
1321 result
= emit_int_cmp(ctx
, LLVMIntEQ
, src
[0], src
[1]);
1324 result
= emit_int_cmp(ctx
, LLVMIntSGE
, src
[0], src
[1]);
1327 result
= emit_int_cmp(ctx
, LLVMIntULT
, src
[0], src
[1]);
1330 result
= emit_int_cmp(ctx
, LLVMIntUGE
, src
[0], src
[1]);
1333 result
= emit_float_cmp(ctx
, LLVMRealUEQ
, src
[0], src
[1]);
1336 result
= emit_float_cmp(ctx
, LLVMRealUNE
, src
[0], src
[1]);
1339 result
= emit_float_cmp(ctx
, LLVMRealULT
, src
[0], src
[1]);
1342 result
= emit_float_cmp(ctx
, LLVMRealUGE
, src
[0], src
[1]);
1345 result
= emit_intrin_1f_param(ctx
, "llvm.fabs.f32", src
[0]);
1348 result
= emit_iabs(ctx
, src
[0]);
1351 result
= emit_minmax_int(ctx
, LLVMIntSGT
, src
[0], src
[1]);
1354 result
= emit_minmax_int(ctx
, LLVMIntSLT
, src
[0], src
[1]);
1357 result
= emit_minmax_int(ctx
, LLVMIntUGT
, src
[0], src
[1]);
1360 result
= emit_minmax_int(ctx
, LLVMIntULT
, src
[0], src
[1]);
1363 result
= emit_isign(ctx
, src
[0]);
1366 src
[0] = to_float(ctx
, src
[0]);
1367 result
= emit_fsign(ctx
, src
[0]);
1370 result
= emit_intrin_1f_param(ctx
, "llvm.floor.f32", src
[0]);
1373 result
= emit_intrin_1f_param(ctx
, "llvm.trunc.f32", src
[0]);
1376 result
= emit_intrin_1f_param(ctx
, "llvm.ceil.f32", src
[0]);
1378 case nir_op_fround_even
:
1379 result
= emit_intrin_1f_param(ctx
, "llvm.rint.f32", src
[0]);
1382 result
= emit_ffract(ctx
, src
[0]);
1385 result
= emit_intrin_1f_param(ctx
, "llvm.sin.f32", src
[0]);
1388 result
= emit_intrin_1f_param(ctx
, "llvm.cos.f32", src
[0]);
1391 result
= emit_intrin_1f_param(ctx
, "llvm.sqrt.f32", src
[0]);
1394 result
= emit_intrin_1f_param(ctx
, "llvm.exp2.f32", src
[0]);
1397 result
= emit_intrin_1f_param(ctx
, "llvm.log2.f32", src
[0]);
1400 result
= emit_intrin_1f_param(ctx
, "llvm.sqrt.f32", src
[0]);
1401 result
= ac_emit_fdiv(&ctx
->ac
, ctx
->f32one
, result
);
1404 result
= emit_intrin_2f_param(ctx
, "llvm.pow.f32", src
[0], src
[1]);
1407 result
= emit_intrin_2f_param(ctx
, "llvm.maxnum.f32", src
[0], src
[1]);
1410 result
= emit_intrin_2f_param(ctx
, "llvm.minnum.f32", src
[0], src
[1]);
1413 result
= emit_intrin_3f_param(ctx
, "llvm.fma.f32", src
[0], src
[1], src
[2]);
1415 case nir_op_ibitfield_extract
:
1416 result
= emit_bitfield_extract(ctx
, "llvm.AMDGPU.bfe.i32", src
);
1418 case nir_op_ubitfield_extract
:
1419 result
= emit_bitfield_extract(ctx
, "llvm.AMDGPU.bfe.u32", src
);
1421 case nir_op_bitfield_insert
:
1422 result
= emit_bitfield_insert(ctx
, src
[0], src
[1], src
[2], src
[3]);
1424 case nir_op_bitfield_reverse
:
1425 result
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.bitreverse.i32", ctx
->i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1427 case nir_op_bit_count
:
1428 result
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.ctpop.i32", ctx
->i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1433 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1434 src
[i
] = to_integer(ctx
, src
[i
]);
1435 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
1438 src
[0] = to_float(ctx
, src
[0]);
1439 result
= LLVMBuildFPToSI(ctx
->builder
, src
[0], ctx
->i32
, "");
1442 src
[0] = to_float(ctx
, src
[0]);
1443 result
= LLVMBuildFPToUI(ctx
->builder
, src
[0], ctx
->i32
, "");
1446 result
= LLVMBuildSIToFP(ctx
->builder
, src
[0], ctx
->f32
, "");
1449 result
= LLVMBuildUIToFP(ctx
->builder
, src
[0], ctx
->f32
, "");
1452 result
= emit_bcsel(ctx
, src
[0], src
[1], src
[2]);
1454 case nir_op_find_lsb
:
1455 result
= emit_find_lsb(ctx
, src
[0]);
1457 case nir_op_ufind_msb
:
1458 result
= emit_ufind_msb(ctx
, src
[0]);
1460 case nir_op_ifind_msb
:
1461 result
= emit_ifind_msb(ctx
, src
[0]);
1463 case nir_op_uadd_carry
:
1464 result
= emit_uint_carry(ctx
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
1466 case nir_op_usub_borrow
:
1467 result
= emit_uint_carry(ctx
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
1470 result
= emit_b2f(ctx
, src
[0]);
1472 case nir_op_fquantize2f16
:
1473 src
[0] = to_float(ctx
, src
[0]);
1474 result
= LLVMBuildFPTrunc(ctx
->builder
, src
[0], ctx
->f16
, "");
1475 /* need to convert back up to f32 */
1476 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
1478 case nir_op_umul_high
:
1479 result
= emit_umul_high(ctx
, src
[0], src
[1]);
1481 case nir_op_imul_high
:
1482 result
= emit_imul_high(ctx
, src
[0], src
[1]);
1484 case nir_op_pack_half_2x16
:
1485 result
= emit_pack_half_2x16(ctx
, src
[0]);
1487 case nir_op_unpack_half_2x16
:
1488 result
= emit_unpack_half_2x16(ctx
, src
[0]);
1492 case nir_op_fddx_fine
:
1493 case nir_op_fddy_fine
:
1494 case nir_op_fddx_coarse
:
1495 case nir_op_fddy_coarse
:
1496 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
1499 fprintf(stderr
, "Unknown NIR alu instr: ");
1500 nir_print_instr(&instr
->instr
, stderr
);
1501 fprintf(stderr
, "\n");
1506 assert(instr
->dest
.dest
.is_ssa
);
1507 result
= to_integer(ctx
, result
);
1508 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.dest
.ssa
,
1513 static void visit_load_const(struct nir_to_llvm_context
*ctx
,
1514 nir_load_const_instr
*instr
)
1516 LLVMValueRef values
[4], value
= NULL
;
1517 LLVMTypeRef element_type
=
1518 LLVMIntTypeInContext(ctx
->context
, instr
->def
.bit_size
);
1520 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1521 switch (instr
->def
.bit_size
) {
1523 values
[i
] = LLVMConstInt(element_type
,
1524 instr
->value
.u32
[i
], false);
1527 values
[i
] = LLVMConstInt(element_type
,
1528 instr
->value
.u64
[i
], false);
1532 "unsupported nir load_const bit_size: %d\n",
1533 instr
->def
.bit_size
);
1537 if (instr
->def
.num_components
> 1) {
1538 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1542 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, value
);
1545 static LLVMValueRef
cast_ptr(struct nir_to_llvm_context
*ctx
, LLVMValueRef ptr
,
1548 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
1549 return LLVMBuildBitCast(ctx
->builder
, ptr
,
1550 LLVMPointerType(type
, addr_space
), "");
1554 get_buffer_size(struct nir_to_llvm_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1557 LLVMBuildExtractElement(ctx
->builder
, descriptor
,
1558 LLVMConstInt(ctx
->i32
, 2, false), "");
1561 if (ctx
->options
->chip_class
>= VI
&& in_elements
) {
1562 /* On VI, the descriptor contains the size in bytes,
1563 * but TXQ must return the size in elements.
1564 * The stride is always non-zero for resources using TXQ.
1566 LLVMValueRef stride
=
1567 LLVMBuildExtractElement(ctx
->builder
, descriptor
,
1568 LLVMConstInt(ctx
->i32
, 1, false), "");
1569 stride
= LLVMBuildLShr(ctx
->builder
, stride
,
1570 LLVMConstInt(ctx
->i32
, 16, false), "");
1571 stride
= LLVMBuildAnd(ctx
->builder
, stride
,
1572 LLVMConstInt(ctx
->i32
, 0x3fff, false), "");
1574 size
= LLVMBuildUDiv(ctx
->builder
, size
, stride
, "");
1580 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
1583 static void build_int_type_name(
1585 char *buf
, unsigned bufsize
)
1587 assert(bufsize
>= 6);
1589 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
1590 snprintf(buf
, bufsize
, "v%ui32",
1591 LLVMGetVectorSize(type
));
1596 static LLVMValueRef
radv_lower_gather4_integer(struct nir_to_llvm_context
*ctx
,
1597 struct ac_tex_info
*tinfo
,
1598 nir_tex_instr
*instr
,
1599 const char *intr_name
,
1600 unsigned coord_vgpr_index
)
1602 LLVMValueRef coord
= tinfo
->args
[0];
1603 LLVMValueRef half_texel
[2];
1608 LLVMValueRef txq_args
[10];
1609 int txq_arg_count
= 0;
1611 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
1612 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, false);
1613 txq_args
[txq_arg_count
++] = tinfo
->args
[1];
1614 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0xf, 0); /* dmask */
1615 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* unorm */
1616 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* r128 */
1617 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, da
? 1 : 0, 0);
1618 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* glc */
1619 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* slc */
1620 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* tfe */
1621 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* lwe */
1622 size
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.SI.getresinfo.i32", ctx
->v4i32
,
1623 txq_args
, txq_arg_count
,
1624 AC_FUNC_ATTR_READNONE
);
1626 for (c
= 0; c
< 2; c
++) {
1627 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
1628 LLVMConstInt(ctx
->i32
, c
, false), "");
1629 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1630 half_texel
[c
] = ac_emit_fdiv(&ctx
->ac
, ctx
->f32one
, half_texel
[c
]);
1631 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1632 LLVMConstReal(ctx
->f32
, -0.5), "");
1636 for (c
= 0; c
< 2; c
++) {
1638 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, coord_vgpr_index
+ c
, 0);
1639 tmp
= LLVMBuildExtractElement(ctx
->builder
, coord
, index
, "");
1640 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1641 tmp
= LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
1642 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1643 coord
= LLVMBuildInsertElement(ctx
->builder
, coord
, tmp
, index
, "");
1646 tinfo
->args
[0] = coord
;
1647 return ac_emit_llvm_intrinsic(&ctx
->ac
, intr_name
, tinfo
->dst_type
, tinfo
->args
, tinfo
->arg_count
,
1648 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_NOUNWIND
);
1652 static LLVMValueRef
build_tex_intrinsic(struct nir_to_llvm_context
*ctx
,
1653 nir_tex_instr
*instr
,
1654 struct ac_tex_info
*tinfo
)
1656 const char *name
= "llvm.SI.image.sample";
1657 const char *infix
= "";
1658 char intr_name
[127];
1660 bool is_shadow
= instr
->is_shadow
;
1661 bool has_offset
= tinfo
->has_offset
;
1662 switch (instr
->op
) {
1664 case nir_texop_txf_ms
:
1665 case nir_texop_samples_identical
:
1666 name
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
? "llvm.SI.image.load" :
1667 instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
? "llvm.SI.vs.load.input" :
1668 "llvm.SI.image.load.mip";
1679 name
= "llvm.SI.getresinfo";
1681 case nir_texop_query_levels
:
1682 name
= "llvm.SI.getresinfo";
1685 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
1692 name
= "llvm.SI.gather4";
1696 name
= "llvm.SI.getlod";
1704 build_int_type_name(LLVMTypeOf(tinfo
->args
[0]), type
, sizeof(type
));
1705 sprintf(intr_name
, "%s%s%s%s.%s", name
, is_shadow
? ".c" : "", infix
,
1706 has_offset
? ".o" : "", type
);
1708 if (instr
->op
== nir_texop_tg4
) {
1709 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
1710 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
1711 return radv_lower_gather4_integer(ctx
, tinfo
, instr
, intr_name
,
1712 (int)has_offset
+ (int)is_shadow
);
1715 return ac_emit_llvm_intrinsic(&ctx
->ac
, intr_name
, tinfo
->dst_type
, tinfo
->args
, tinfo
->arg_count
,
1716 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_NOUNWIND
);
1720 static LLVMValueRef
visit_vulkan_resource_index(struct nir_to_llvm_context
*ctx
,
1721 nir_intrinsic_instr
*instr
)
1723 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
1724 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
1725 unsigned binding
= nir_intrinsic_binding(instr
);
1726 LLVMValueRef desc_ptr
= ctx
->descriptor_sets
[desc_set
];
1727 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[desc_set
].layout
;
1728 unsigned base_offset
= layout
->binding
[binding
].offset
;
1729 LLVMValueRef offset
, stride
;
1731 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
1732 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
1733 desc_ptr
= ctx
->push_constants
;
1734 base_offset
= ctx
->options
->layout
->push_constant_size
;
1735 base_offset
+= 16 * layout
->binding
[binding
].dynamic_offset_offset
;
1736 stride
= LLVMConstInt(ctx
->i32
, 16, false);
1738 stride
= LLVMConstInt(ctx
->i32
, layout
->binding
[binding
].size
, false);
1740 offset
= LLVMConstInt(ctx
->i32
, base_offset
, false);
1741 index
= LLVMBuildMul(ctx
->builder
, index
, stride
, "");
1742 offset
= LLVMBuildAdd(ctx
->builder
, offset
, index
, "");
1744 desc_ptr
= build_gep0(ctx
, desc_ptr
, offset
);
1745 desc_ptr
= cast_ptr(ctx
, desc_ptr
, ctx
->v4i32
);
1746 LLVMSetMetadata(desc_ptr
, ctx
->uniform_md_kind
, ctx
->empty_md
);
1748 return LLVMBuildLoad(ctx
->builder
, desc_ptr
, "");
1751 static LLVMValueRef
visit_load_push_constant(struct nir_to_llvm_context
*ctx
,
1752 nir_intrinsic_instr
*instr
)
1754 LLVMValueRef ptr
, addr
;
1756 addr
= LLVMConstInt(ctx
->i32
, nir_intrinsic_base(instr
), 0);
1757 addr
= LLVMBuildAdd(ctx
->builder
, addr
, get_src(ctx
, instr
->src
[0]), "");
1759 ptr
= build_gep0(ctx
, ctx
->push_constants
, addr
);
1760 ptr
= cast_ptr(ctx
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
1762 return LLVMBuildLoad(ctx
->builder
, ptr
, "");
1765 static LLVMValueRef
visit_get_buffer_size(struct nir_to_llvm_context
*ctx
,
1766 nir_intrinsic_instr
*instr
)
1768 LLVMValueRef desc
= get_src(ctx
, instr
->src
[0]);
1770 return get_buffer_size(ctx
, desc
, false);
1772 static void visit_store_ssbo(struct nir_to_llvm_context
*ctx
,
1773 nir_intrinsic_instr
*instr
)
1775 const char *store_name
;
1776 LLVMTypeRef data_type
= ctx
->f32
;
1777 unsigned writemask
= nir_intrinsic_write_mask(instr
);
1778 LLVMValueRef base_data
, base_offset
;
1779 LLVMValueRef params
[6];
1781 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
1782 ctx
->shader_info
->fs
.writes_memory
= true;
1784 params
[1] = get_src(ctx
, instr
->src
[1]);
1785 params
[2] = LLVMConstInt(ctx
->i32
, 0, false); /* vindex */
1786 params
[4] = LLVMConstInt(ctx
->i1
, 0, false); /* glc */
1787 params
[5] = LLVMConstInt(ctx
->i1
, 0, false); /* slc */
1789 if (instr
->num_components
> 1)
1790 data_type
= LLVMVectorType(ctx
->f32
, instr
->num_components
);
1792 base_data
= to_float(ctx
, get_src(ctx
, instr
->src
[0]));
1793 base_data
= trim_vector(ctx
, base_data
, instr
->num_components
);
1794 base_data
= LLVMBuildBitCast(ctx
->builder
, base_data
,
1796 base_offset
= get_src(ctx
, instr
->src
[2]); /* voffset */
1800 LLVMValueRef offset
;
1802 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
1804 /* Due to an LLVM limitation, split 3-element writes
1805 * into a 2-element and a 1-element write. */
1807 writemask
|= 1 << (start
+ 2);
1812 store_name
= "llvm.amdgcn.buffer.store.v4f32";
1814 } else if (count
== 2) {
1815 tmp
= LLVMBuildExtractElement(ctx
->builder
,
1816 base_data
, LLVMConstInt(ctx
->i32
, start
, false), "");
1817 data
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), tmp
,
1820 tmp
= LLVMBuildExtractElement(ctx
->builder
,
1821 base_data
, LLVMConstInt(ctx
->i32
, start
+ 1, false), "");
1822 data
= LLVMBuildInsertElement(ctx
->builder
, data
, tmp
,
1824 store_name
= "llvm.amdgcn.buffer.store.v2f32";
1828 if (get_llvm_num_components(base_data
) > 1)
1829 data
= LLVMBuildExtractElement(ctx
->builder
, base_data
,
1830 LLVMConstInt(ctx
->i32
, start
, false), "");
1833 store_name
= "llvm.amdgcn.buffer.store.f32";
1836 offset
= base_offset
;
1838 offset
= LLVMBuildAdd(ctx
->builder
, offset
, LLVMConstInt(ctx
->i32
, start
* 4, false), "");
1842 ac_emit_llvm_intrinsic(&ctx
->ac
, store_name
,
1843 ctx
->voidt
, params
, 6, 0);
1847 static LLVMValueRef
visit_atomic_ssbo(struct nir_to_llvm_context
*ctx
,
1848 nir_intrinsic_instr
*instr
)
1851 LLVMValueRef params
[6];
1853 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
1854 ctx
->shader_info
->fs
.writes_memory
= true;
1856 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
1857 params
[arg_count
++] = llvm_extract_elem(ctx
, get_src(ctx
, instr
->src
[3]), 0);
1859 params
[arg_count
++] = llvm_extract_elem(ctx
, get_src(ctx
, instr
->src
[2]), 0);
1860 params
[arg_count
++] = get_src(ctx
, instr
->src
[0]);
1861 params
[arg_count
++] = LLVMConstInt(ctx
->i32
, 0, false); /* vindex */
1862 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1863 params
[arg_count
++] = LLVMConstInt(ctx
->i1
, 0, false); /* slc */
1865 switch (instr
->intrinsic
) {
1866 case nir_intrinsic_ssbo_atomic_add
:
1867 name
= "llvm.amdgcn.buffer.atomic.add";
1869 case nir_intrinsic_ssbo_atomic_imin
:
1870 name
= "llvm.amdgcn.buffer.atomic.smin";
1872 case nir_intrinsic_ssbo_atomic_umin
:
1873 name
= "llvm.amdgcn.buffer.atomic.umin";
1875 case nir_intrinsic_ssbo_atomic_imax
:
1876 name
= "llvm.amdgcn.buffer.atomic.smax";
1878 case nir_intrinsic_ssbo_atomic_umax
:
1879 name
= "llvm.amdgcn.buffer.atomic.umax";
1881 case nir_intrinsic_ssbo_atomic_and
:
1882 name
= "llvm.amdgcn.buffer.atomic.and";
1884 case nir_intrinsic_ssbo_atomic_or
:
1885 name
= "llvm.amdgcn.buffer.atomic.or";
1887 case nir_intrinsic_ssbo_atomic_xor
:
1888 name
= "llvm.amdgcn.buffer.atomic.xor";
1890 case nir_intrinsic_ssbo_atomic_exchange
:
1891 name
= "llvm.amdgcn.buffer.atomic.swap";
1893 case nir_intrinsic_ssbo_atomic_comp_swap
:
1894 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
1900 return ac_emit_llvm_intrinsic(&ctx
->ac
, name
, ctx
->i32
, params
, arg_count
, 0);
1903 static LLVMValueRef
visit_load_buffer(struct nir_to_llvm_context
*ctx
,
1904 nir_intrinsic_instr
*instr
)
1906 const char *load_name
;
1907 LLVMTypeRef data_type
= ctx
->f32
;
1908 if (instr
->num_components
== 3)
1909 data_type
= LLVMVectorType(ctx
->f32
, 4);
1910 else if (instr
->num_components
> 1)
1911 data_type
= LLVMVectorType(ctx
->f32
, instr
->num_components
);
1913 if (instr
->num_components
== 4 || instr
->num_components
== 3)
1914 load_name
= "llvm.amdgcn.buffer.load.v4f32";
1915 else if (instr
->num_components
== 2)
1916 load_name
= "llvm.amdgcn.buffer.load.v2f32";
1917 else if (instr
->num_components
== 1)
1918 load_name
= "llvm.amdgcn.buffer.load.f32";
1922 LLVMValueRef params
[] = {
1923 get_src(ctx
, instr
->src
[0]),
1924 LLVMConstInt(ctx
->i32
, 0, false),
1925 get_src(ctx
, instr
->src
[1]),
1926 LLVMConstInt(ctx
->i1
, 0, false),
1927 LLVMConstInt(ctx
->i1
, 0, false),
1931 ac_emit_llvm_intrinsic(&ctx
->ac
, load_name
, data_type
, params
, 5, 0);
1933 if (instr
->num_components
== 3)
1934 ret
= trim_vector(ctx
, ret
, 3);
1936 return LLVMBuildBitCast(ctx
->builder
, ret
,
1937 get_def_type(ctx
, &instr
->dest
.ssa
), "");
1940 static LLVMValueRef
visit_load_ubo_buffer(struct nir_to_llvm_context
*ctx
,
1941 nir_intrinsic_instr
*instr
)
1943 LLVMValueRef results
[4], ret
;
1944 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
1945 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
1947 rsrc
= LLVMBuildBitCast(ctx
->builder
, rsrc
, LLVMVectorType(ctx
->i8
, 16), "");
1949 for (unsigned i
= 0; i
< instr
->num_components
; ++i
) {
1950 LLVMValueRef params
[] = {
1952 LLVMBuildAdd(ctx
->builder
, LLVMConstInt(ctx
->i32
, 4 * i
, 0),
1955 results
[i
] = ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.SI.load.const", ctx
->f32
,
1956 params
, 2, AC_FUNC_ATTR_READNONE
);
1960 ret
= ac_build_gather_values(&ctx
->ac
, results
, instr
->num_components
);
1961 return LLVMBuildBitCast(ctx
->builder
, ret
,
1962 get_def_type(ctx
, &instr
->dest
.ssa
), "");
1966 radv_get_deref_offset(struct nir_to_llvm_context
*ctx
, nir_deref
*tail
,
1967 bool vs_in
, unsigned *const_out
, LLVMValueRef
*indir_out
)
1969 unsigned const_offset
= 0;
1970 LLVMValueRef offset
= NULL
;
1973 while (tail
->child
!= NULL
) {
1974 const struct glsl_type
*parent_type
= tail
->type
;
1977 if (tail
->deref_type
== nir_deref_type_array
) {
1978 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
1979 LLVMValueRef index
, stride
, local_offset
;
1980 unsigned size
= glsl_count_attribute_slots(tail
->type
, vs_in
);
1982 const_offset
+= size
* deref_array
->base_offset
;
1983 if (deref_array
->deref_array_type
== nir_deref_array_type_direct
)
1986 assert(deref_array
->deref_array_type
== nir_deref_array_type_indirect
);
1987 index
= get_src(ctx
, deref_array
->indirect
);
1988 stride
= LLVMConstInt(ctx
->i32
, size
, 0);
1989 local_offset
= LLVMBuildMul(ctx
->builder
, stride
, index
, "");
1992 offset
= LLVMBuildAdd(ctx
->builder
, offset
, local_offset
, "");
1994 offset
= local_offset
;
1995 } else if (tail
->deref_type
== nir_deref_type_struct
) {
1996 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
1998 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
1999 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
2000 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
2003 unreachable("unsupported deref type");
2007 if (const_offset
&& offset
)
2008 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
2009 LLVMConstInt(ctx
->i32
, const_offset
, 0),
2012 *const_out
= const_offset
;
2013 *indir_out
= offset
;
2016 static LLVMValueRef
visit_load_var(struct nir_to_llvm_context
*ctx
,
2017 nir_intrinsic_instr
*instr
)
2019 LLVMValueRef values
[4];
2020 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2021 int ve
= instr
->dest
.ssa
.num_components
;
2022 LLVMValueRef indir_index
;
2023 unsigned const_index
;
2024 switch (instr
->variables
[0]->var
->data
.mode
) {
2025 case nir_var_shader_in
:
2026 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
,
2027 ctx
->stage
== MESA_SHADER_VERTEX
,
2028 &const_index
, &indir_index
);
2029 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2031 unsigned count
= glsl_count_attribute_slots(
2032 instr
->variables
[0]->var
->type
,
2033 ctx
->stage
== MESA_SHADER_VERTEX
);
2034 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2035 &ctx
->ac
, ctx
->inputs
+ idx
+ chan
, count
,
2038 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2042 values
[chan
] = ctx
->inputs
[idx
+ chan
+ const_index
* 4];
2044 return to_integer(ctx
, ac_build_gather_values(&ctx
->ac
, values
, ve
));
2047 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2048 &const_index
, &indir_index
);
2049 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2051 unsigned count
= glsl_count_attribute_slots(
2052 instr
->variables
[0]->var
->type
, false);
2053 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2054 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2057 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2061 values
[chan
] = LLVMBuildLoad(ctx
->builder
, ctx
->locals
[idx
+ chan
+ const_index
* 4], "");
2064 return to_integer(ctx
, ac_build_gather_values(&ctx
->ac
, values
, ve
));
2065 case nir_var_shader_out
:
2066 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2067 &const_index
, &indir_index
);
2068 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2070 unsigned count
= glsl_count_attribute_slots(
2071 instr
->variables
[0]->var
->type
, false);
2072 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2073 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
2076 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2080 values
[chan
] = LLVMBuildLoad(ctx
->builder
,
2081 ctx
->outputs
[idx
+ chan
+ const_index
* 4],
2085 return to_integer(ctx
, ac_build_gather_values(&ctx
->ac
, values
, ve
));
2086 case nir_var_shared
: {
2087 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2088 &const_index
, &indir_index
);
2089 LLVMValueRef ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
2090 LLVMValueRef derived_ptr
;
2092 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2093 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, chan
, false);
2095 index
= LLVMBuildAdd(ctx
->builder
, index
, indir_index
, "");
2096 derived_ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, "");
2097 values
[chan
] = LLVMBuildLoad(ctx
->builder
, derived_ptr
, "");
2099 return to_integer(ctx
, ac_build_gather_values(&ctx
->ac
, values
, ve
));
2108 visit_store_var(struct nir_to_llvm_context
*ctx
,
2109 nir_intrinsic_instr
*instr
)
2111 LLVMValueRef temp_ptr
, value
;
2112 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2113 LLVMValueRef src
= to_float(ctx
, get_src(ctx
, instr
->src
[0]));
2114 int writemask
= instr
->const_index
[0];
2115 LLVMValueRef indir_index
;
2116 unsigned const_index
;
2117 switch (instr
->variables
[0]->var
->data
.mode
) {
2118 case nir_var_shader_out
:
2119 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2120 &const_index
, &indir_index
);
2121 for (unsigned chan
= 0; chan
< 4; chan
++) {
2123 if (!(writemask
& (1 << chan
)))
2125 if (get_llvm_num_components(src
) == 1)
2128 value
= LLVMBuildExtractElement(ctx
->builder
, src
,
2129 LLVMConstInt(ctx
->i32
,
2133 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CLIP_DIST0
||
2134 instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CULL_DIST0
)
2137 unsigned count
= glsl_count_attribute_slots(
2138 instr
->variables
[0]->var
->type
, false);
2139 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2140 &ctx
->ac
, ctx
->outputs
+ idx
+ chan
, count
,
2143 if (get_llvm_num_components(tmp_vec
) > 1) {
2144 tmp_vec
= LLVMBuildInsertElement(ctx
->builder
, tmp_vec
,
2145 value
, indir_index
, "");
2148 build_store_values_extended(ctx
, ctx
->outputs
+ idx
+ chan
,
2149 count
, stride
, tmp_vec
);
2152 temp_ptr
= ctx
->outputs
[idx
+ chan
+ const_index
* stride
];
2154 LLVMBuildStore(ctx
->builder
, value
, temp_ptr
);
2159 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2160 &const_index
, &indir_index
);
2161 for (unsigned chan
= 0; chan
< 4; chan
++) {
2162 if (!(writemask
& (1 << chan
)))
2165 if (get_llvm_num_components(src
) == 1)
2168 value
= LLVMBuildExtractElement(ctx
->builder
, src
,
2169 LLVMConstInt(ctx
->i32
, chan
, false), "");
2171 unsigned count
= glsl_count_attribute_slots(
2172 instr
->variables
[0]->var
->type
, false);
2173 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2174 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2177 tmp_vec
= LLVMBuildInsertElement(ctx
->builder
, tmp_vec
,
2178 value
, indir_index
, "");
2179 build_store_values_extended(ctx
, ctx
->locals
+ idx
+ chan
,
2182 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
2184 LLVMBuildStore(ctx
->builder
, value
, temp_ptr
);
2188 case nir_var_shared
: {
2190 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2191 &const_index
, &indir_index
);
2193 ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
2194 LLVMValueRef derived_ptr
;
2196 for (unsigned chan
= 0; chan
< 4; chan
++) {
2197 if (!(writemask
& (1 << chan
)))
2200 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, chan
, false);
2202 if (get_llvm_num_components(src
) == 1)
2205 value
= LLVMBuildExtractElement(ctx
->builder
, src
,
2206 LLVMConstInt(ctx
->i32
,
2211 index
= LLVMBuildAdd(ctx
->builder
, index
, indir_index
, "");
2213 derived_ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, "");
2214 LLVMBuildStore(ctx
->builder
,
2215 to_integer(ctx
, value
), derived_ptr
);
2224 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
2227 case GLSL_SAMPLER_DIM_BUF
:
2229 case GLSL_SAMPLER_DIM_1D
:
2230 return array
? 2 : 1;
2231 case GLSL_SAMPLER_DIM_2D
:
2232 return array
? 3 : 2;
2233 case GLSL_SAMPLER_DIM_MS
:
2234 return array
? 4 : 3;
2235 case GLSL_SAMPLER_DIM_3D
:
2236 case GLSL_SAMPLER_DIM_CUBE
:
2238 case GLSL_SAMPLER_DIM_RECT
:
2239 case GLSL_SAMPLER_DIM_SUBPASS
:
2241 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
2249 static LLVMValueRef
get_image_coords(struct nir_to_llvm_context
*ctx
,
2250 nir_intrinsic_instr
*instr
)
2252 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
2253 if(instr
->variables
[0]->deref
.child
)
2254 type
= instr
->variables
[0]->deref
.child
->type
;
2256 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
2257 LLVMValueRef coords
[4];
2258 LLVMValueRef masks
[] = {
2259 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
2260 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false),
2264 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2265 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
2266 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2267 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
2268 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2270 count
= image_type_to_components_count(dim
,
2271 glsl_sampler_type_is_array(type
));
2274 if (instr
->src
[0].ssa
->num_components
)
2275 res
= LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[0], "");
2282 for (chan
= 0; chan
< count
; ++chan
) {
2283 coords
[chan
] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[chan
], "");
2287 for (chan
= 0; chan
< count
; ++chan
)
2288 coords
[chan
] = LLVMBuildAdd(ctx
->builder
, coords
[chan
], LLVMBuildFPToUI(ctx
->builder
, ctx
->frag_pos
[chan
], ctx
->i32
, ""), "");
2291 coords
[count
] = llvm_extract_elem(ctx
, get_src(ctx
, instr
->src
[1]), 0);
2296 coords
[3] = LLVMGetUndef(ctx
->i32
);
2299 res
= ac_build_gather_values(&ctx
->ac
, coords
, count
);
2304 static void build_type_name_for_intr(
2306 char *buf
, unsigned bufsize
)
2308 LLVMTypeRef elem_type
= type
;
2310 assert(bufsize
>= 8);
2312 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
2313 int ret
= snprintf(buf
, bufsize
, "v%u",
2314 LLVMGetVectorSize(type
));
2316 char *type_name
= LLVMPrintTypeToString(type
);
2317 fprintf(stderr
, "Error building type name for: %s\n",
2321 elem_type
= LLVMGetElementType(type
);
2325 switch (LLVMGetTypeKind(elem_type
)) {
2327 case LLVMIntegerTypeKind
:
2328 snprintf(buf
, bufsize
, "i%d", LLVMGetIntTypeWidth(elem_type
));
2330 case LLVMFloatTypeKind
:
2331 snprintf(buf
, bufsize
, "f32");
2333 case LLVMDoubleTypeKind
:
2334 snprintf(buf
, bufsize
, "f64");
2339 static void get_image_intr_name(const char *base_name
,
2340 LLVMTypeRef data_type
,
2341 LLVMTypeRef coords_type
,
2342 LLVMTypeRef rsrc_type
,
2343 char *out_name
, unsigned out_len
)
2345 char coords_type_name
[8];
2347 build_type_name_for_intr(coords_type
, coords_type_name
,
2348 sizeof(coords_type_name
));
2350 if (HAVE_LLVM
<= 0x0309) {
2351 snprintf(out_name
, out_len
, "%s.%s", base_name
, coords_type_name
);
2353 char data_type_name
[8];
2354 char rsrc_type_name
[8];
2356 build_type_name_for_intr(data_type
, data_type_name
,
2357 sizeof(data_type_name
));
2358 build_type_name_for_intr(rsrc_type
, rsrc_type_name
,
2359 sizeof(rsrc_type_name
));
2360 snprintf(out_name
, out_len
, "%s.%s.%s.%s", base_name
,
2361 data_type_name
, coords_type_name
, rsrc_type_name
);
2365 static LLVMValueRef
visit_image_load(struct nir_to_llvm_context
*ctx
,
2366 nir_intrinsic_instr
*instr
)
2368 LLVMValueRef params
[7];
2370 char intrinsic_name
[64];
2371 const nir_variable
*var
= instr
->variables
[0]->var
;
2372 const struct glsl_type
*type
= var
->type
;
2373 if(instr
->variables
[0]->deref
.child
)
2374 type
= instr
->variables
[0]->deref
.child
->type
;
2376 type
= glsl_without_array(type
);
2377 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2378 params
[0] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
2379 params
[1] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
2380 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
2381 params
[2] = LLVMConstInt(ctx
->i32
, 0, false); /* voffset */
2382 params
[3] = LLVMConstInt(ctx
->i1
, 0, false); /* glc */
2383 params
[4] = LLVMConstInt(ctx
->i1
, 0, false); /* slc */
2384 res
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.load.format.v4f32", ctx
->v4f32
,
2387 res
= trim_vector(ctx
, res
, instr
->dest
.ssa
.num_components
);
2388 res
= to_integer(ctx
, res
);
2390 bool is_da
= glsl_sampler_type_is_array(type
) ||
2391 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
2392 LLVMValueRef da
= is_da
? ctx
->i32one
: ctx
->i32zero
;
2393 LLVMValueRef glc
= LLVMConstInt(ctx
->i1
, 0, false);
2394 LLVMValueRef slc
= LLVMConstInt(ctx
->i1
, 0, false);
2396 params
[0] = get_image_coords(ctx
, instr
);
2397 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
2398 params
[2] = LLVMConstInt(ctx
->i32
, 15, false); /* dmask */
2399 if (HAVE_LLVM
<= 0x0309) {
2400 params
[3] = LLVMConstInt(ctx
->i1
, 0, false); /* r128 */
2405 LLVMValueRef lwe
= LLVMConstInt(ctx
->i1
, 0, false);
2412 get_image_intr_name("llvm.amdgcn.image.load",
2413 ctx
->v4f32
, /* vdata */
2414 LLVMTypeOf(params
[0]), /* coords */
2415 LLVMTypeOf(params
[1]), /* rsrc */
2416 intrinsic_name
, sizeof(intrinsic_name
));
2418 res
= ac_emit_llvm_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->v4f32
,
2419 params
, 7, AC_FUNC_ATTR_READONLY
);
2421 return to_integer(ctx
, res
);
2424 static void visit_image_store(struct nir_to_llvm_context
*ctx
,
2425 nir_intrinsic_instr
*instr
)
2427 LLVMValueRef params
[8];
2428 char intrinsic_name
[64];
2429 const nir_variable
*var
= instr
->variables
[0]->var
;
2430 LLVMValueRef i1false
= LLVMConstInt(ctx
->i1
, 0, 0);
2431 LLVMValueRef i1true
= LLVMConstInt(ctx
->i1
, 1, 0);
2432 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2434 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
2435 ctx
->shader_info
->fs
.writes_memory
= true;
2437 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2438 params
[0] = to_float(ctx
, get_src(ctx
, instr
->src
[2])); /* data */
2439 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
2440 params
[2] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
2441 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
2442 params
[3] = LLVMConstInt(ctx
->i32
, 0, false); /* voffset */
2443 params
[4] = i1false
; /* glc */
2444 params
[5] = i1false
; /* slc */
2445 ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->voidt
,
2448 bool is_da
= glsl_sampler_type_is_array(type
) ||
2449 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
2450 LLVMValueRef da
= is_da
? i1true
: i1false
;
2451 LLVMValueRef glc
= i1false
;
2452 LLVMValueRef slc
= i1false
;
2454 params
[0] = to_float(ctx
, get_src(ctx
, instr
->src
[2]));
2455 params
[1] = get_image_coords(ctx
, instr
); /* coords */
2456 params
[2] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
2457 params
[3] = LLVMConstInt(ctx
->i32
, 15, false); /* dmask */
2458 if (HAVE_LLVM
<= 0x0309) {
2459 params
[4] = i1false
; /* r128 */
2464 LLVMValueRef lwe
= i1false
;
2471 get_image_intr_name("llvm.amdgcn.image.store",
2472 LLVMTypeOf(params
[0]), /* vdata */
2473 LLVMTypeOf(params
[1]), /* coords */
2474 LLVMTypeOf(params
[2]), /* rsrc */
2475 intrinsic_name
, sizeof(intrinsic_name
));
2477 ac_emit_llvm_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->voidt
,
2483 static LLVMValueRef
visit_image_atomic(struct nir_to_llvm_context
*ctx
,
2484 nir_intrinsic_instr
*instr
)
2486 LLVMValueRef params
[6];
2487 int param_count
= 0;
2488 const nir_variable
*var
= instr
->variables
[0]->var
;
2489 LLVMValueRef i1false
= LLVMConstInt(ctx
->i1
, 0, 0);
2490 LLVMValueRef i1true
= LLVMConstInt(ctx
->i1
, 1, 0);
2491 const char *base_name
= "llvm.amdgcn.image.atomic";
2492 const char *atomic_name
;
2493 LLVMValueRef coords
;
2494 char intrinsic_name
[32], coords_type
[8];
2495 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2497 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
2498 ctx
->shader_info
->fs
.writes_memory
= true;
2500 params
[param_count
++] = get_src(ctx
, instr
->src
[2]);
2501 if (instr
->intrinsic
== nir_intrinsic_image_atomic_comp_swap
)
2502 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
2504 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2505 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
2506 coords
= params
[param_count
++] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
2507 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
2508 params
[param_count
++] = ctx
->i32zero
; /* voffset */
2509 params
[param_count
++] = i1false
; /* glc */
2510 params
[param_count
++] = i1false
; /* slc */
2512 bool da
= glsl_sampler_type_is_array(type
) ||
2513 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
2515 coords
= params
[param_count
++] = get_image_coords(ctx
, instr
);
2516 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
2517 params
[param_count
++] = i1false
; /* r128 */
2518 params
[param_count
++] = da
? i1true
: i1false
; /* da */
2519 params
[param_count
++] = i1false
; /* slc */
2522 switch (instr
->intrinsic
) {
2523 case nir_intrinsic_image_atomic_add
:
2524 atomic_name
= "add";
2526 case nir_intrinsic_image_atomic_min
:
2527 atomic_name
= "smin";
2529 case nir_intrinsic_image_atomic_max
:
2530 atomic_name
= "smax";
2532 case nir_intrinsic_image_atomic_and
:
2533 atomic_name
= "and";
2535 case nir_intrinsic_image_atomic_or
:
2538 case nir_intrinsic_image_atomic_xor
:
2539 atomic_name
= "xor";
2541 case nir_intrinsic_image_atomic_exchange
:
2542 atomic_name
= "swap";
2544 case nir_intrinsic_image_atomic_comp_swap
:
2545 atomic_name
= "cmpswap";
2550 build_int_type_name(LLVMTypeOf(coords
),
2551 coords_type
, sizeof(coords_type
));
2553 snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2554 "%s.%s.%s", base_name
, atomic_name
, coords_type
);
2555 return ac_emit_llvm_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->i32
, params
, param_count
, 0);
2558 static LLVMValueRef
visit_image_size(struct nir_to_llvm_context
*ctx
,
2559 nir_intrinsic_instr
*instr
)
2562 LLVMValueRef params
[10];
2563 const nir_variable
*var
= instr
->variables
[0]->var
;
2564 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
2565 bool da
= glsl_sampler_type_is_array(var
->type
) ||
2566 glsl_get_sampler_dim(var
->type
) == GLSL_SAMPLER_DIM_CUBE
;
2567 if(instr
->variables
[0]->deref
.child
)
2568 type
= instr
->variables
[0]->deref
.child
->type
;
2570 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
2571 return get_buffer_size(ctx
, get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
), true);
2572 params
[0] = ctx
->i32zero
;
2573 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
2574 params
[2] = LLVMConstInt(ctx
->i32
, 15, false);
2575 params
[3] = ctx
->i32zero
;
2576 params
[4] = ctx
->i32zero
;
2577 params
[5] = da
? ctx
->i32one
: ctx
->i32zero
;
2578 params
[6] = ctx
->i32zero
;
2579 params
[7] = ctx
->i32zero
;
2580 params
[8] = ctx
->i32zero
;
2581 params
[9] = ctx
->i32zero
;
2583 res
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.SI.getresinfo.i32", ctx
->v4i32
,
2584 params
, 10, AC_FUNC_ATTR_READNONE
);
2586 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
2587 glsl_sampler_type_is_array(type
)) {
2588 LLVMValueRef two
= LLVMConstInt(ctx
->i32
, 2, false);
2589 LLVMValueRef six
= LLVMConstInt(ctx
->i32
, 6, false);
2590 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->builder
, res
, two
, "");
2591 z
= LLVMBuildSDiv(ctx
->builder
, z
, six
, "");
2592 res
= LLVMBuildInsertElement(ctx
->builder
, res
, z
, two
, "");
2597 static void emit_waitcnt(struct nir_to_llvm_context
*ctx
)
2599 LLVMValueRef args
[1] = {
2600 LLVMConstInt(ctx
->i32
, 0xf70, false),
2602 ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.amdgcn.s.waitcnt",
2603 ctx
->voidt
, args
, 1, 0);
2606 static void emit_barrier(struct nir_to_llvm_context
*ctx
)
2609 ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.amdgcn.s.barrier",
2610 ctx
->voidt
, NULL
, 0, 0);
2613 static void emit_discard_if(struct nir_to_llvm_context
*ctx
,
2614 nir_intrinsic_instr
*instr
)
2617 ctx
->shader_info
->fs
.can_discard
= true;
2619 cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
2620 get_src(ctx
, instr
->src
[0]),
2623 cond
= LLVMBuildSelect(ctx
->builder
, cond
,
2624 LLVMConstReal(ctx
->f32
, -1.0f
),
2626 ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.AMDGPU.kill",
2632 visit_load_local_invocation_index(struct nir_to_llvm_context
*ctx
)
2634 LLVMValueRef result
;
2635 LLVMValueRef thread_id
= get_thread_id(ctx
);
2636 result
= LLVMBuildAnd(ctx
->builder
, ctx
->tg_size
,
2637 LLVMConstInt(ctx
->i32
, 0xfc0, false), "");
2639 return LLVMBuildAdd(ctx
->builder
, result
, thread_id
, "");
2642 static LLVMValueRef
visit_var_atomic(struct nir_to_llvm_context
*ctx
,
2643 nir_intrinsic_instr
*instr
)
2645 LLVMValueRef ptr
, result
;
2646 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2647 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
2648 ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
2650 if (instr
->intrinsic
== nir_intrinsic_var_atomic_comp_swap
) {
2651 LLVMValueRef src1
= get_src(ctx
, instr
->src
[1]);
2652 result
= LLVMBuildAtomicCmpXchg(ctx
->builder
,
2654 LLVMAtomicOrderingSequentiallyConsistent
,
2655 LLVMAtomicOrderingSequentiallyConsistent
,
2658 LLVMAtomicRMWBinOp op
;
2659 switch (instr
->intrinsic
) {
2660 case nir_intrinsic_var_atomic_add
:
2661 op
= LLVMAtomicRMWBinOpAdd
;
2663 case nir_intrinsic_var_atomic_umin
:
2664 op
= LLVMAtomicRMWBinOpUMin
;
2666 case nir_intrinsic_var_atomic_umax
:
2667 op
= LLVMAtomicRMWBinOpUMax
;
2669 case nir_intrinsic_var_atomic_imin
:
2670 op
= LLVMAtomicRMWBinOpMin
;
2672 case nir_intrinsic_var_atomic_imax
:
2673 op
= LLVMAtomicRMWBinOpMax
;
2675 case nir_intrinsic_var_atomic_and
:
2676 op
= LLVMAtomicRMWBinOpAnd
;
2678 case nir_intrinsic_var_atomic_or
:
2679 op
= LLVMAtomicRMWBinOpOr
;
2681 case nir_intrinsic_var_atomic_xor
:
2682 op
= LLVMAtomicRMWBinOpXor
;
2684 case nir_intrinsic_var_atomic_exchange
:
2685 op
= LLVMAtomicRMWBinOpXchg
;
2691 result
= LLVMBuildAtomicRMW(ctx
->builder
, op
, ptr
, to_integer(ctx
, src
),
2692 LLVMAtomicOrderingSequentiallyConsistent
,
2698 #define INTERP_CENTER 0
2699 #define INTERP_CENTROID 1
2700 #define INTERP_SAMPLE 2
2702 static LLVMValueRef
lookup_interp_param(struct nir_to_llvm_context
*ctx
,
2703 enum glsl_interp_mode interp
, unsigned location
)
2706 case INTERP_MODE_FLAT
:
2709 case INTERP_MODE_SMOOTH
:
2710 case INTERP_MODE_NONE
:
2711 if (location
== INTERP_CENTER
)
2712 return ctx
->persp_center
;
2713 else if (location
== INTERP_CENTROID
)
2714 return ctx
->persp_centroid
;
2715 else if (location
== INTERP_SAMPLE
)
2716 return ctx
->persp_sample
;
2718 case INTERP_MODE_NOPERSPECTIVE
:
2719 if (location
== INTERP_CENTER
)
2720 return ctx
->linear_center
;
2721 else if (location
== INTERP_CENTROID
)
2722 return ctx
->linear_centroid
;
2723 else if (location
== INTERP_SAMPLE
)
2724 return ctx
->linear_sample
;
2730 static LLVMValueRef
load_sample_position(struct nir_to_llvm_context
*ctx
,
2731 LLVMValueRef sample_id
)
2733 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
2734 LLVMValueRef offset0
= LLVMBuildMul(ctx
->builder
, sample_id
, LLVMConstInt(ctx
->i32
, 8, false), "");
2735 LLVMValueRef offset1
= LLVMBuildAdd(ctx
->builder
, offset0
, LLVMConstInt(ctx
->i32
, 4, false), "");
2736 LLVMValueRef result
[2];
2738 result
[0] = build_indexed_load_const(ctx
, ctx
->sample_positions
, offset0
);
2739 result
[1] = build_indexed_load_const(ctx
, ctx
->sample_positions
, offset1
);
2741 return ac_build_gather_values(&ctx
->ac
, result
, 2);
2744 static LLVMValueRef
load_sample_pos(struct nir_to_llvm_context
*ctx
)
2746 LLVMValueRef values
[2];
2748 values
[0] = emit_ffract(ctx
, ctx
->frag_pos
[0]);
2749 values
[1] = emit_ffract(ctx
, ctx
->frag_pos
[1]);
2750 return ac_build_gather_values(&ctx
->ac
, values
, 2);
2753 static LLVMValueRef
visit_interp(struct nir_to_llvm_context
*ctx
,
2754 nir_intrinsic_instr
*instr
)
2756 LLVMValueRef result
[2];
2757 LLVMValueRef interp_param
, attr_number
;
2760 LLVMValueRef src_c0
, src_c1
;
2761 const char *intr_name
;
2763 int input_index
= instr
->variables
[0]->var
->data
.location
- VARYING_SLOT_VAR0
;
2764 switch (instr
->intrinsic
) {
2765 case nir_intrinsic_interp_var_at_centroid
:
2766 location
= INTERP_CENTROID
;
2768 case nir_intrinsic_interp_var_at_sample
:
2769 case nir_intrinsic_interp_var_at_offset
:
2770 location
= INTERP_SAMPLE
;
2771 src0
= get_src(ctx
, instr
->src
[0]);
2777 if (instr
->intrinsic
== nir_intrinsic_interp_var_at_offset
) {
2778 src_c0
= to_float(ctx
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32zero
, ""));
2779 src_c1
= to_float(ctx
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32one
, ""));
2780 } else if (instr
->intrinsic
== nir_intrinsic_interp_var_at_sample
) {
2781 LLVMValueRef sample_position
;
2782 LLVMValueRef halfval
= LLVMConstReal(ctx
->f32
, 0.5f
);
2784 /* fetch sample ID */
2785 sample_position
= load_sample_position(ctx
, src0
);
2787 src_c0
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->i32zero
, "");
2788 src_c0
= LLVMBuildFSub(ctx
->builder
, src_c0
, halfval
, "");
2789 src_c1
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->i32one
, "");
2790 src_c1
= LLVMBuildFSub(ctx
->builder
, src_c1
, halfval
, "");
2792 interp_param
= lookup_interp_param(ctx
, instr
->variables
[0]->var
->data
.interpolation
, location
);
2793 attr_number
= LLVMConstInt(ctx
->i32
, input_index
, false);
2795 if (location
== INTERP_SAMPLE
) {
2796 LLVMValueRef ij_out
[2];
2797 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
, interp_param
);
2800 * take the I then J parameters, and the DDX/Y for it, and
2801 * calculate the IJ inputs for the interpolator.
2802 * temp1 = ddx * offset/sample.x + I;
2803 * interp_param.I = ddy * offset/sample.y + temp1;
2804 * temp1 = ddx * offset/sample.x + J;
2805 * interp_param.J = ddy * offset/sample.y + temp1;
2807 for (unsigned i
= 0; i
< 2; i
++) {
2808 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->i32
, i
, false);
2809 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->i32
, i
+ 2, false);
2810 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->builder
,
2811 ddxy_out
, ix_ll
, "");
2812 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->builder
,
2813 ddxy_out
, iy_ll
, "");
2814 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->builder
,
2815 interp_param
, ix_ll
, "");
2816 LLVMValueRef temp1
, temp2
;
2818 interp_el
= LLVMBuildBitCast(ctx
->builder
, interp_el
,
2821 temp1
= LLVMBuildFMul(ctx
->builder
, ddx_el
, src_c0
, "");
2822 temp1
= LLVMBuildFAdd(ctx
->builder
, temp1
, interp_el
, "");
2824 temp2
= LLVMBuildFMul(ctx
->builder
, ddy_el
, src_c1
, "");
2825 temp2
= LLVMBuildFAdd(ctx
->builder
, temp2
, temp1
, "");
2827 ij_out
[i
] = LLVMBuildBitCast(ctx
->builder
,
2828 temp2
, ctx
->i32
, "");
2830 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
2833 intr_name
= interp_param
? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
2834 for (chan
= 0; chan
< 2; chan
++) {
2835 LLVMValueRef args
[4];
2836 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
2838 args
[0] = llvm_chan
;
2839 args
[1] = attr_number
;
2840 args
[2] = ctx
->prim_mask
;
2841 args
[3] = interp_param
;
2842 result
[chan
] = ac_emit_llvm_intrinsic(&ctx
->ac
, intr_name
,
2843 ctx
->f32
, args
, args
[3] ? 4 : 3,
2844 AC_FUNC_ATTR_READNONE
);
2846 return ac_build_gather_values(&ctx
->ac
, result
, 2);
2849 static void visit_intrinsic(struct nir_to_llvm_context
*ctx
,
2850 nir_intrinsic_instr
*instr
)
2852 LLVMValueRef result
= NULL
;
2854 switch (instr
->intrinsic
) {
2855 case nir_intrinsic_load_work_group_id
: {
2856 result
= ctx
->workgroup_ids
;
2859 case nir_intrinsic_load_base_vertex
: {
2860 result
= ctx
->base_vertex
;
2863 case nir_intrinsic_load_vertex_id_zero_base
: {
2864 result
= ctx
->vertex_id
;
2867 case nir_intrinsic_load_local_invocation_id
: {
2868 result
= ctx
->local_invocation_ids
;
2871 case nir_intrinsic_load_base_instance
:
2872 result
= ctx
->start_instance
;
2874 case nir_intrinsic_load_sample_id
:
2875 ctx
->shader_info
->fs
.force_persample
= true;
2876 result
= unpack_param(ctx
, ctx
->ancillary
, 8, 4);
2878 case nir_intrinsic_load_sample_pos
:
2879 ctx
->shader_info
->fs
.force_persample
= true;
2880 result
= load_sample_pos(ctx
);
2882 case nir_intrinsic_load_front_face
:
2883 result
= ctx
->front_face
;
2885 case nir_intrinsic_load_instance_id
:
2886 result
= ctx
->instance_id
;
2887 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
2888 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
2890 case nir_intrinsic_load_num_work_groups
:
2891 result
= ctx
->num_work_groups
;
2893 case nir_intrinsic_load_local_invocation_index
:
2894 result
= visit_load_local_invocation_index(ctx
);
2896 case nir_intrinsic_load_push_constant
:
2897 result
= visit_load_push_constant(ctx
, instr
);
2899 case nir_intrinsic_vulkan_resource_index
:
2900 result
= visit_vulkan_resource_index(ctx
, instr
);
2902 case nir_intrinsic_store_ssbo
:
2903 visit_store_ssbo(ctx
, instr
);
2905 case nir_intrinsic_load_ssbo
:
2906 result
= visit_load_buffer(ctx
, instr
);
2908 case nir_intrinsic_ssbo_atomic_add
:
2909 case nir_intrinsic_ssbo_atomic_imin
:
2910 case nir_intrinsic_ssbo_atomic_umin
:
2911 case nir_intrinsic_ssbo_atomic_imax
:
2912 case nir_intrinsic_ssbo_atomic_umax
:
2913 case nir_intrinsic_ssbo_atomic_and
:
2914 case nir_intrinsic_ssbo_atomic_or
:
2915 case nir_intrinsic_ssbo_atomic_xor
:
2916 case nir_intrinsic_ssbo_atomic_exchange
:
2917 case nir_intrinsic_ssbo_atomic_comp_swap
:
2918 result
= visit_atomic_ssbo(ctx
, instr
);
2920 case nir_intrinsic_load_ubo
:
2921 result
= visit_load_ubo_buffer(ctx
, instr
);
2923 case nir_intrinsic_get_buffer_size
:
2924 result
= visit_get_buffer_size(ctx
, instr
);
2926 case nir_intrinsic_load_var
:
2927 result
= visit_load_var(ctx
, instr
);
2929 case nir_intrinsic_store_var
:
2930 visit_store_var(ctx
, instr
);
2932 case nir_intrinsic_image_load
:
2933 result
= visit_image_load(ctx
, instr
);
2935 case nir_intrinsic_image_store
:
2936 visit_image_store(ctx
, instr
);
2938 case nir_intrinsic_image_atomic_add
:
2939 case nir_intrinsic_image_atomic_min
:
2940 case nir_intrinsic_image_atomic_max
:
2941 case nir_intrinsic_image_atomic_and
:
2942 case nir_intrinsic_image_atomic_or
:
2943 case nir_intrinsic_image_atomic_xor
:
2944 case nir_intrinsic_image_atomic_exchange
:
2945 case nir_intrinsic_image_atomic_comp_swap
:
2946 result
= visit_image_atomic(ctx
, instr
);
2948 case nir_intrinsic_image_size
:
2949 result
= visit_image_size(ctx
, instr
);
2951 case nir_intrinsic_discard
:
2952 ctx
->shader_info
->fs
.can_discard
= true;
2953 ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.AMDGPU.kilp",
2957 case nir_intrinsic_discard_if
:
2958 emit_discard_if(ctx
, instr
);
2960 case nir_intrinsic_memory_barrier
:
2963 case nir_intrinsic_barrier
:
2966 case nir_intrinsic_var_atomic_add
:
2967 case nir_intrinsic_var_atomic_imin
:
2968 case nir_intrinsic_var_atomic_umin
:
2969 case nir_intrinsic_var_atomic_imax
:
2970 case nir_intrinsic_var_atomic_umax
:
2971 case nir_intrinsic_var_atomic_and
:
2972 case nir_intrinsic_var_atomic_or
:
2973 case nir_intrinsic_var_atomic_xor
:
2974 case nir_intrinsic_var_atomic_exchange
:
2975 case nir_intrinsic_var_atomic_comp_swap
:
2976 result
= visit_var_atomic(ctx
, instr
);
2978 case nir_intrinsic_interp_var_at_centroid
:
2979 case nir_intrinsic_interp_var_at_sample
:
2980 case nir_intrinsic_interp_var_at_offset
:
2981 result
= visit_interp(ctx
, instr
);
2984 fprintf(stderr
, "Unknown intrinsic: ");
2985 nir_print_instr(&instr
->instr
, stderr
);
2986 fprintf(stderr
, "\n");
2990 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
2994 static LLVMValueRef
get_sampler_desc(struct nir_to_llvm_context
*ctx
,
2995 nir_deref_var
*deref
,
2996 enum desc_type desc_type
)
2998 unsigned desc_set
= deref
->var
->data
.descriptor_set
;
2999 LLVMValueRef list
= ctx
->descriptor_sets
[desc_set
];
3000 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[desc_set
].layout
;
3001 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ deref
->var
->data
.binding
;
3002 unsigned offset
= binding
->offset
;
3003 unsigned stride
= binding
->size
;
3005 LLVMBuilderRef builder
= ctx
->builder
;
3007 LLVMValueRef index
= NULL
;
3009 assert(deref
->var
->data
.binding
< layout
->binding_count
);
3011 switch (desc_type
) {
3023 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
3033 unreachable("invalid desc_type\n");
3036 if (deref
->deref
.child
) {
3037 nir_deref_array
*child
= (nir_deref_array
*)deref
->deref
.child
;
3039 assert(child
->deref_array_type
!= nir_deref_array_type_wildcard
);
3040 offset
+= child
->base_offset
* stride
;
3041 if (child
->deref_array_type
== nir_deref_array_type_indirect
) {
3042 index
= get_src(ctx
, child
->indirect
);
3046 assert(stride
% type_size
== 0);
3049 index
= ctx
->i32zero
;
3051 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, stride
/ type_size
, 0), "");
3053 list
= build_gep0(ctx
, list
, LLVMConstInt(ctx
->i32
, offset
, 0));
3054 list
= LLVMBuildPointerCast(builder
, list
, const_array(type
, 0), "");
3056 return build_indexed_load_const(ctx
, list
, index
);
3059 static void set_tex_fetch_args(struct nir_to_llvm_context
*ctx
,
3060 struct ac_tex_info
*tinfo
,
3061 nir_tex_instr
*instr
,
3063 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
3064 LLVMValueRef
*param
, unsigned count
,
3068 unsigned is_rect
= 0;
3069 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
3071 if (op
== nir_texop_lod
)
3073 /* Pad to power of two vector */
3074 while (count
< util_next_power_of_two(count
))
3075 param
[count
++] = LLVMGetUndef(ctx
->i32
);
3078 tinfo
->args
[0] = ac_build_gather_values(&ctx
->ac
, param
, count
);
3080 tinfo
->args
[0] = param
[0];
3082 tinfo
->args
[1] = res_ptr
;
3085 if (op
== nir_texop_txf
||
3086 op
== nir_texop_txf_ms
||
3087 op
== nir_texop_query_levels
||
3088 op
== nir_texop_texture_samples
||
3089 op
== nir_texop_txs
)
3090 tinfo
->dst_type
= ctx
->v4i32
;
3092 tinfo
->dst_type
= ctx
->v4f32
;
3093 tinfo
->args
[num_args
++] = samp_ptr
;
3096 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
&& op
== nir_texop_txf
) {
3097 tinfo
->args
[0] = res_ptr
;
3098 tinfo
->args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
3099 tinfo
->args
[2] = param
[0];
3100 tinfo
->arg_count
= 3;
3104 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, dmask
, 0);
3105 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, is_rect
, 0); /* unorm */
3106 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* r128 */
3107 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, da
? 1 : 0, 0);
3108 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* glc */
3109 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* slc */
3110 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* tfe */
3111 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* lwe */
3113 tinfo
->arg_count
= num_args
;
3116 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
3119 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
3120 * filtering manually. The driver sets img7 to a mask clearing
3121 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
3122 * s_and_b32 samp0, samp0, img7
3125 * The ANISO_OVERRIDE sampler field enables this fix in TA.
3127 static LLVMValueRef
sici_fix_sampler_aniso(struct nir_to_llvm_context
*ctx
,
3128 LLVMValueRef res
, LLVMValueRef samp
)
3130 LLVMBuilderRef builder
= ctx
->builder
;
3131 LLVMValueRef img7
, samp0
;
3133 if (ctx
->options
->chip_class
>= VI
)
3136 img7
= LLVMBuildExtractElement(builder
, res
,
3137 LLVMConstInt(ctx
->i32
, 7, 0), "");
3138 samp0
= LLVMBuildExtractElement(builder
, samp
,
3139 LLVMConstInt(ctx
->i32
, 0, 0), "");
3140 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
3141 return LLVMBuildInsertElement(builder
, samp
, samp0
,
3142 LLVMConstInt(ctx
->i32
, 0, 0), "");
3145 static void tex_fetch_ptrs(struct nir_to_llvm_context
*ctx
,
3146 nir_tex_instr
*instr
,
3147 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
3148 LLVMValueRef
*fmask_ptr
)
3150 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
3151 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_BUFFER
);
3153 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_IMAGE
);
3156 *samp_ptr
= get_sampler_desc(ctx
, instr
->sampler
, DESC_SAMPLER
);
3158 *samp_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_SAMPLER
);
3159 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
3160 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
3162 if (fmask_ptr
&& !instr
->sampler
&& (instr
->op
== nir_texop_txf_ms
||
3163 instr
->op
== nir_texop_samples_identical
))
3164 *fmask_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_FMASK
);
3167 static void visit_tex(struct nir_to_llvm_context
*ctx
, nir_tex_instr
*instr
)
3169 LLVMValueRef result
= NULL
;
3170 struct ac_tex_info tinfo
= { 0 };
3171 unsigned dmask
= 0xf;
3172 LLVMValueRef address
[16];
3173 LLVMValueRef coords
[5];
3174 LLVMValueRef coord
= NULL
, lod
= NULL
, comparator
= NULL
;
3175 LLVMValueRef bias
= NULL
, offsets
= NULL
;
3176 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
, sample_index
= NULL
;
3177 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
3178 LLVMValueRef derivs
[6];
3179 unsigned chan
, count
= 0;
3180 unsigned const_src
= 0, num_deriv_comp
= 0;
3182 tex_fetch_ptrs(ctx
, instr
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
3184 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3185 switch (instr
->src
[i
].src_type
) {
3186 case nir_tex_src_coord
:
3187 coord
= get_src(ctx
, instr
->src
[i
].src
);
3189 case nir_tex_src_projector
:
3191 case nir_tex_src_comparator
:
3192 comparator
= get_src(ctx
, instr
->src
[i
].src
);
3194 case nir_tex_src_offset
:
3195 offsets
= get_src(ctx
, instr
->src
[i
].src
);
3198 case nir_tex_src_bias
:
3199 bias
= get_src(ctx
, instr
->src
[i
].src
);
3201 case nir_tex_src_lod
:
3202 lod
= get_src(ctx
, instr
->src
[i
].src
);
3204 case nir_tex_src_ms_index
:
3205 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
3207 case nir_tex_src_ms_mcs
:
3209 case nir_tex_src_ddx
:
3210 ddx
= get_src(ctx
, instr
->src
[i
].src
);
3211 num_deriv_comp
= instr
->src
[i
].src
.ssa
->num_components
;
3213 case nir_tex_src_ddy
:
3214 ddy
= get_src(ctx
, instr
->src
[i
].src
);
3216 case nir_tex_src_texture_offset
:
3217 case nir_tex_src_sampler_offset
:
3218 case nir_tex_src_plane
:
3224 if (instr
->op
== nir_texop_texture_samples
) {
3225 LLVMValueRef res
, samples
, is_msaa
;
3226 res
= LLVMBuildBitCast(ctx
->builder
, res_ptr
, ctx
->v8i32
, "");
3227 samples
= LLVMBuildExtractElement(ctx
->builder
, res
,
3228 LLVMConstInt(ctx
->i32
, 3, false), "");
3229 is_msaa
= LLVMBuildLShr(ctx
->builder
, samples
,
3230 LLVMConstInt(ctx
->i32
, 28, false), "");
3231 is_msaa
= LLVMBuildAnd(ctx
->builder
, is_msaa
,
3232 LLVMConstInt(ctx
->i32
, 0xe, false), "");
3233 is_msaa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, is_msaa
,
3234 LLVMConstInt(ctx
->i32
, 0xe, false), "");
3236 samples
= LLVMBuildLShr(ctx
->builder
, samples
,
3237 LLVMConstInt(ctx
->i32
, 16, false), "");
3238 samples
= LLVMBuildAnd(ctx
->builder
, samples
,
3239 LLVMConstInt(ctx
->i32
, 0xf, false), "");
3240 samples
= LLVMBuildShl(ctx
->builder
, ctx
->i32one
,
3242 samples
= LLVMBuildSelect(ctx
->builder
, is_msaa
, samples
,
3249 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
3250 coords
[chan
] = llvm_extract_elem(ctx
, coord
, chan
);
3252 if (offsets
&& instr
->op
!= nir_texop_txf
) {
3253 LLVMValueRef offset
[3], pack
;
3254 for (chan
= 0; chan
< 3; ++chan
)
3255 offset
[chan
] = ctx
->i32zero
;
3257 tinfo
.has_offset
= true;
3258 for (chan
= 0; chan
< get_llvm_num_components(offsets
); chan
++) {
3259 offset
[chan
] = llvm_extract_elem(ctx
, offsets
, chan
);
3260 offset
[chan
] = LLVMBuildAnd(ctx
->builder
, offset
[chan
],
3261 LLVMConstInt(ctx
->i32
, 0x3f, false), "");
3263 offset
[chan
] = LLVMBuildShl(ctx
->builder
, offset
[chan
],
3264 LLVMConstInt(ctx
->i32
, chan
* 8, false), "");
3266 pack
= LLVMBuildOr(ctx
->builder
, offset
[0], offset
[1], "");
3267 pack
= LLVMBuildOr(ctx
->builder
, pack
, offset
[2], "");
3268 address
[count
++] = pack
;
3271 /* pack LOD bias value */
3272 if (instr
->op
== nir_texop_txb
&& bias
) {
3273 address
[count
++] = bias
;
3276 /* Pack depth comparison value */
3277 if (instr
->is_shadow
&& comparator
) {
3278 address
[count
++] = llvm_extract_elem(ctx
, comparator
, 0);
3281 /* pack derivatives */
3283 switch (instr
->sampler_dim
) {
3284 case GLSL_SAMPLER_DIM_3D
:
3285 case GLSL_SAMPLER_DIM_CUBE
:
3288 case GLSL_SAMPLER_DIM_2D
:
3292 case GLSL_SAMPLER_DIM_1D
:
3297 for (unsigned i
= 0; i
< num_deriv_comp
; i
++) {
3298 derivs
[i
* 2] = to_float(ctx
, llvm_extract_elem(ctx
, ddx
, i
));
3299 derivs
[i
* 2 + 1] = to_float(ctx
, llvm_extract_elem(ctx
, ddy
, i
));
3303 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& coord
) {
3304 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
3305 coords
[chan
] = to_float(ctx
, coords
[chan
]);
3306 if (instr
->coord_components
== 3)
3307 coords
[3] = LLVMGetUndef(ctx
->f32
);
3308 ac_prepare_cube_coords(&ctx
->ac
,
3309 instr
->op
== nir_texop_txd
, instr
->is_array
,
3316 for (unsigned i
= 0; i
< num_deriv_comp
* 2; i
++)
3317 address
[count
++] = derivs
[i
];
3320 /* Pack texture coordinates */
3322 address
[count
++] = coords
[0];
3323 if (instr
->coord_components
> 1)
3324 address
[count
++] = coords
[1];
3325 if (instr
->coord_components
> 2) {
3326 /* This seems like a bit of a hack - but it passes Vulkan CTS with it */
3327 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_3D
&& instr
->op
!= nir_texop_txf
) {
3328 coords
[2] = to_float(ctx
, coords
[2]);
3329 coords
[2] = ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.rint.f32", ctx
->f32
, &coords
[2],
3331 coords
[2] = to_integer(ctx
, coords
[2]);
3333 address
[count
++] = coords
[2];
3338 if ((instr
->op
== nir_texop_txl
|| instr
->op
== nir_texop_txf
) && lod
) {
3339 address
[count
++] = lod
;
3340 } else if (instr
->op
== nir_texop_txf_ms
&& sample_index
) {
3341 address
[count
++] = sample_index
;
3342 } else if(instr
->op
== nir_texop_txs
) {
3345 address
[count
++] = lod
;
3347 address
[count
++] = ctx
->i32zero
;
3350 for (chan
= 0; chan
< count
; chan
++) {
3351 address
[chan
] = LLVMBuildBitCast(ctx
->builder
,
3352 address
[chan
], ctx
->i32
, "");
3355 if (instr
->op
== nir_texop_samples_identical
) {
3356 LLVMValueRef txf_address
[4];
3357 struct ac_tex_info txf_info
= { 0 };
3358 unsigned txf_count
= count
;
3359 memcpy(txf_address
, address
, sizeof(txf_address
));
3361 if (!instr
->is_array
)
3362 txf_address
[2] = ctx
->i32zero
;
3363 txf_address
[3] = ctx
->i32zero
;
3365 set_tex_fetch_args(ctx
, &txf_info
, instr
, nir_texop_txf
,
3367 txf_address
, txf_count
, 0xf);
3369 result
= build_tex_intrinsic(ctx
, instr
, &txf_info
);
3371 result
= LLVMBuildExtractElement(ctx
->builder
, result
, ctx
->i32zero
, "");
3372 result
= emit_int_cmp(ctx
, LLVMIntEQ
, result
, ctx
->i32zero
);
3376 /* Adjust the sample index according to FMASK.
3378 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3379 * which is the identity mapping. Each nibble says which physical sample
3380 * should be fetched to get that sample.
3382 * For example, 0x11111100 means there are only 2 samples stored and
3383 * the second sample covers 3/4 of the pixel. When reading samples 0
3384 * and 1, return physical sample 0 (determined by the first two 0s
3385 * in FMASK), otherwise return physical sample 1.
3387 * The sample index should be adjusted as follows:
3388 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
3390 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
) {
3391 LLVMValueRef txf_address
[4];
3392 struct ac_tex_info txf_info
= { 0 };
3393 unsigned txf_count
= count
;
3394 memcpy(txf_address
, address
, sizeof(txf_address
));
3396 if (!instr
->is_array
)
3397 txf_address
[2] = ctx
->i32zero
;
3398 txf_address
[3] = ctx
->i32zero
;
3400 set_tex_fetch_args(ctx
, &txf_info
, instr
, nir_texop_txf
,
3402 txf_address
, txf_count
, 0xf);
3404 result
= build_tex_intrinsic(ctx
, instr
, &txf_info
);
3405 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
3406 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
3408 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
3412 unsigned sample_chan
= instr
->is_array
? 3 : 2;
3414 LLVMValueRef sample_index4
=
3415 LLVMBuildMul(ctx
->builder
, address
[sample_chan
], four
, "");
3416 LLVMValueRef shifted_fmask
=
3417 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
3418 LLVMValueRef final_sample
=
3419 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
3421 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3422 * resource descriptor is 0 (invalid),
3424 LLVMValueRef fmask_desc
=
3425 LLVMBuildBitCast(ctx
->builder
, fmask_ptr
,
3428 LLVMValueRef fmask_word1
=
3429 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
3432 LLVMValueRef word1_is_nonzero
=
3433 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3434 fmask_word1
, ctx
->i32zero
, "");
3436 /* Replace the MSAA sample index. */
3437 address
[sample_chan
] =
3438 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
3439 final_sample
, address
[sample_chan
], "");
3442 if (offsets
&& instr
->op
== nir_texop_txf
) {
3443 nir_const_value
*const_offset
=
3444 nir_src_as_const_value(instr
->src
[const_src
].src
);
3445 int num_offsets
= instr
->src
[const_src
].src
.ssa
->num_components
;
3446 assert(const_offset
);
3447 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
3448 if (num_offsets
> 2)
3449 address
[2] = LLVMBuildAdd(ctx
->builder
,
3450 address
[2], LLVMConstInt(ctx
->i32
, const_offset
->i32
[2], false), "");
3451 if (num_offsets
> 1)
3452 address
[1] = LLVMBuildAdd(ctx
->builder
,
3453 address
[1], LLVMConstInt(ctx
->i32
, const_offset
->i32
[1], false), "");
3454 address
[0] = LLVMBuildAdd(ctx
->builder
,
3455 address
[0], LLVMConstInt(ctx
->i32
, const_offset
->i32
[0], false), "");
3459 /* TODO TG4 support */
3460 if (instr
->op
== nir_texop_tg4
) {
3461 if (instr
->is_shadow
)
3464 dmask
= 1 << instr
->component
;
3466 set_tex_fetch_args(ctx
, &tinfo
, instr
, instr
->op
,
3467 res_ptr
, samp_ptr
, address
, count
, dmask
);
3469 result
= build_tex_intrinsic(ctx
, instr
, &tinfo
);
3471 if (instr
->op
== nir_texop_query_levels
)
3472 result
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, 3, false), "");
3473 else if (instr
->is_shadow
&& instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&& instr
->op
!= nir_texop_tg4
)
3474 result
= LLVMBuildExtractElement(ctx
->builder
, result
, ctx
->i32zero
, "");
3475 else if (instr
->op
== nir_texop_txs
&&
3476 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
3478 LLVMValueRef two
= LLVMConstInt(ctx
->i32
, 2, false);
3479 LLVMValueRef six
= LLVMConstInt(ctx
->i32
, 6, false);
3480 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->builder
, result
, two
, "");
3481 z
= LLVMBuildSDiv(ctx
->builder
, z
, six
, "");
3482 result
= LLVMBuildInsertElement(ctx
->builder
, result
, z
, two
, "");
3483 } else if (instr
->dest
.ssa
.num_components
!= 4)
3484 result
= trim_vector(ctx
, result
, instr
->dest
.ssa
.num_components
);
3488 assert(instr
->dest
.is_ssa
);
3489 result
= to_integer(ctx
, result
);
3490 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
3495 static void visit_phi(struct nir_to_llvm_context
*ctx
, nir_phi_instr
*instr
)
3497 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
3498 LLVMValueRef result
= LLVMBuildPhi(ctx
->builder
, type
, "");
3500 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
3501 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
3504 static void visit_post_phi(struct nir_to_llvm_context
*ctx
,
3505 nir_phi_instr
*instr
,
3506 LLVMValueRef llvm_phi
)
3508 nir_foreach_phi_src(src
, instr
) {
3509 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
3510 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
3512 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
3516 static void phi_post_pass(struct nir_to_llvm_context
*ctx
)
3518 struct hash_entry
*entry
;
3519 hash_table_foreach(ctx
->phis
, entry
) {
3520 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
3521 (LLVMValueRef
)entry
->data
);
3526 static void visit_ssa_undef(struct nir_to_llvm_context
*ctx
,
3527 nir_ssa_undef_instr
*instr
)
3529 unsigned num_components
= instr
->def
.num_components
;
3532 if (num_components
== 1)
3533 undef
= LLVMGetUndef(ctx
->i32
);
3535 undef
= LLVMGetUndef(LLVMVectorType(ctx
->i32
, num_components
));
3537 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, undef
);
3540 static void visit_jump(struct nir_to_llvm_context
*ctx
,
3541 nir_jump_instr
*instr
)
3543 switch (instr
->type
) {
3544 case nir_jump_break
:
3545 LLVMBuildBr(ctx
->builder
, ctx
->break_block
);
3546 LLVMClearInsertionPosition(ctx
->builder
);
3548 case nir_jump_continue
:
3549 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
3550 LLVMClearInsertionPosition(ctx
->builder
);
3553 fprintf(stderr
, "Unknown NIR jump instr: ");
3554 nir_print_instr(&instr
->instr
, stderr
);
3555 fprintf(stderr
, "\n");
3560 static void visit_cf_list(struct nir_to_llvm_context
*ctx
,
3561 struct exec_list
*list
);
3563 static void visit_block(struct nir_to_llvm_context
*ctx
, nir_block
*block
)
3565 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->builder
);
3566 nir_foreach_instr(instr
, block
)
3568 switch (instr
->type
) {
3569 case nir_instr_type_alu
:
3570 visit_alu(ctx
, nir_instr_as_alu(instr
));
3572 case nir_instr_type_load_const
:
3573 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
3575 case nir_instr_type_intrinsic
:
3576 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
3578 case nir_instr_type_tex
:
3579 visit_tex(ctx
, nir_instr_as_tex(instr
));
3581 case nir_instr_type_phi
:
3582 visit_phi(ctx
, nir_instr_as_phi(instr
));
3584 case nir_instr_type_ssa_undef
:
3585 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
3587 case nir_instr_type_jump
:
3588 visit_jump(ctx
, nir_instr_as_jump(instr
));
3591 fprintf(stderr
, "Unknown NIR instr type: ");
3592 nir_print_instr(instr
, stderr
);
3593 fprintf(stderr
, "\n");
3598 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
3601 static void visit_if(struct nir_to_llvm_context
*ctx
, nir_if
*if_stmt
)
3603 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
3605 LLVMBasicBlockRef merge_block
=
3606 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
3607 LLVMBasicBlockRef if_block
=
3608 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
3609 LLVMBasicBlockRef else_block
= merge_block
;
3610 if (!exec_list_is_empty(&if_stmt
->else_list
))
3611 else_block
= LLVMAppendBasicBlockInContext(
3612 ctx
->context
, ctx
->main_function
, "");
3614 LLVMValueRef cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, value
,
3615 LLVMConstInt(ctx
->i32
, 0, false), "");
3616 LLVMBuildCondBr(ctx
->builder
, cond
, if_block
, else_block
);
3618 LLVMPositionBuilderAtEnd(ctx
->builder
, if_block
);
3619 visit_cf_list(ctx
, &if_stmt
->then_list
);
3620 if (LLVMGetInsertBlock(ctx
->builder
))
3621 LLVMBuildBr(ctx
->builder
, merge_block
);
3623 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
3624 LLVMPositionBuilderAtEnd(ctx
->builder
, else_block
);
3625 visit_cf_list(ctx
, &if_stmt
->else_list
);
3626 if (LLVMGetInsertBlock(ctx
->builder
))
3627 LLVMBuildBr(ctx
->builder
, merge_block
);
3630 LLVMPositionBuilderAtEnd(ctx
->builder
, merge_block
);
3633 static void visit_loop(struct nir_to_llvm_context
*ctx
, nir_loop
*loop
)
3635 LLVMBasicBlockRef continue_parent
= ctx
->continue_block
;
3636 LLVMBasicBlockRef break_parent
= ctx
->break_block
;
3638 ctx
->continue_block
=
3639 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
3641 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
3643 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
3644 LLVMPositionBuilderAtEnd(ctx
->builder
, ctx
->continue_block
);
3645 visit_cf_list(ctx
, &loop
->body
);
3647 if (LLVMGetInsertBlock(ctx
->builder
))
3648 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
3649 LLVMPositionBuilderAtEnd(ctx
->builder
, ctx
->break_block
);
3651 ctx
->continue_block
= continue_parent
;
3652 ctx
->break_block
= break_parent
;
3655 static void visit_cf_list(struct nir_to_llvm_context
*ctx
,
3656 struct exec_list
*list
)
3658 foreach_list_typed(nir_cf_node
, node
, node
, list
)
3660 switch (node
->type
) {
3661 case nir_cf_node_block
:
3662 visit_block(ctx
, nir_cf_node_as_block(node
));
3665 case nir_cf_node_if
:
3666 visit_if(ctx
, nir_cf_node_as_if(node
));
3669 case nir_cf_node_loop
:
3670 visit_loop(ctx
, nir_cf_node_as_loop(node
));
3680 handle_vs_input_decl(struct nir_to_llvm_context
*ctx
,
3681 struct nir_variable
*variable
)
3683 LLVMValueRef t_list_ptr
= ctx
->vertex_buffers
;
3684 LLVMValueRef t_offset
;
3685 LLVMValueRef t_list
;
3686 LLVMValueRef args
[3];
3688 LLVMValueRef buffer_index
;
3689 int index
= variable
->data
.location
- VERT_ATTRIB_GENERIC0
;
3690 int idx
= variable
->data
.location
;
3691 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, true);
3693 variable
->data
.driver_location
= idx
* 4;
3695 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << index
)) {
3696 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->instance_id
,
3697 ctx
->start_instance
, "");
3698 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
3699 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
3701 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->vertex_id
,
3702 ctx
->base_vertex
, "");
3704 for (unsigned i
= 0; i
< attrib_count
; ++i
, ++idx
) {
3705 t_offset
= LLVMConstInt(ctx
->i32
, index
+ i
, false);
3707 t_list
= build_indexed_load_const(ctx
, t_list_ptr
, t_offset
);
3709 args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
3710 args
[2] = buffer_index
;
3711 input
= ac_emit_llvm_intrinsic(&ctx
->ac
,
3712 "llvm.SI.vs.load.input", ctx
->v4f32
, args
, 3,
3713 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_NOUNWIND
);
3715 for (unsigned chan
= 0; chan
< 4; chan
++) {
3716 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
3717 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
, chan
)] =
3718 to_integer(ctx
, LLVMBuildExtractElement(ctx
->builder
,
3719 input
, llvm_chan
, ""));
3725 static void interp_fs_input(struct nir_to_llvm_context
*ctx
,
3727 LLVMValueRef interp_param
,
3728 LLVMValueRef prim_mask
,
3729 LLVMValueRef result
[4])
3731 const char *intr_name
;
3732 LLVMValueRef attr_number
;
3735 attr_number
= LLVMConstInt(ctx
->i32
, attr
, false);
3737 /* fs.constant returns the param from the middle vertex, so it's not
3738 * really useful for flat shading. It's meant to be used for custom
3739 * interpolation (but the intrinsic can't fetch from the other two
3742 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
3743 * to do the right thing. The only reason we use fs.constant is that
3744 * fs.interp cannot be used on integers, because they can be equal
3747 intr_name
= interp_param
? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
3749 for (chan
= 0; chan
< 4; chan
++) {
3750 LLVMValueRef args
[4];
3751 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
3753 args
[0] = llvm_chan
;
3754 args
[1] = attr_number
;
3755 args
[2] = prim_mask
;
3756 args
[3] = interp_param
;
3757 result
[chan
] = ac_emit_llvm_intrinsic(&ctx
->ac
, intr_name
,
3758 ctx
->f32
, args
, args
[3] ? 4 : 3,
3759 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_NOUNWIND
);
3764 handle_fs_input_decl(struct nir_to_llvm_context
*ctx
,
3765 struct nir_variable
*variable
)
3767 int idx
= variable
->data
.location
;
3768 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
3769 LLVMValueRef interp
;
3771 variable
->data
.driver_location
= idx
* 4;
3772 ctx
->input_mask
|= ((1ull << attrib_count
) - 1) << variable
->data
.location
;
3774 if (glsl_get_base_type(glsl_without_array(variable
->type
)) == GLSL_TYPE_FLOAT
) {
3775 unsigned interp_type
;
3776 if (variable
->data
.sample
) {
3777 interp_type
= INTERP_SAMPLE
;
3778 ctx
->shader_info
->fs
.force_persample
= true;
3779 } else if (variable
->data
.centroid
)
3780 interp_type
= INTERP_CENTROID
;
3782 interp_type
= INTERP_CENTER
;
3784 interp
= lookup_interp_param(ctx
, variable
->data
.interpolation
, interp_type
);
3788 for (unsigned i
= 0; i
< attrib_count
; ++i
)
3789 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
+ i
, 0)] = interp
;
3794 handle_shader_input_decl(struct nir_to_llvm_context
*ctx
,
3795 struct nir_variable
*variable
)
3797 switch (ctx
->stage
) {
3798 case MESA_SHADER_VERTEX
:
3799 handle_vs_input_decl(ctx
, variable
);
3801 case MESA_SHADER_FRAGMENT
:
3802 handle_fs_input_decl(ctx
, variable
);
3811 handle_fs_inputs_pre(struct nir_to_llvm_context
*ctx
,
3812 struct nir_shader
*nir
)
3815 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_INPUTS
; ++i
) {
3816 LLVMValueRef interp_param
;
3817 LLVMValueRef
*inputs
= ctx
->inputs
+radeon_llvm_reg_index_soa(i
, 0);
3819 if (!(ctx
->input_mask
& (1ull << i
)))
3822 if (i
>= VARYING_SLOT_VAR0
|| i
== VARYING_SLOT_PNTC
) {
3823 interp_param
= *inputs
;
3824 interp_fs_input(ctx
, index
, interp_param
, ctx
->prim_mask
,
3828 ctx
->shader_info
->fs
.flat_shaded_mask
|= 1u << index
;
3830 } else if (i
== VARYING_SLOT_POS
) {
3831 for(int i
= 0; i
< 3; ++i
)
3832 inputs
[i
] = ctx
->frag_pos
[i
];
3834 inputs
[3] = ac_emit_fdiv(&ctx
->ac
, ctx
->f32one
, ctx
->frag_pos
[3]);
3837 ctx
->shader_info
->fs
.num_interp
= index
;
3838 if (ctx
->input_mask
& (1 << VARYING_SLOT_PNTC
))
3839 ctx
->shader_info
->fs
.has_pcoord
= true;
3840 ctx
->shader_info
->fs
.input_mask
= ctx
->input_mask
>> VARYING_SLOT_VAR0
;
3844 ac_build_alloca(struct nir_to_llvm_context
*ctx
,
3848 LLVMBuilderRef builder
= ctx
->builder
;
3849 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
3850 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
3851 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
3852 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
3853 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ctx
->context
);
3857 LLVMPositionBuilderBefore(first_builder
, first_instr
);
3859 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
3862 res
= LLVMBuildAlloca(first_builder
, type
, name
);
3863 LLVMBuildStore(builder
, LLVMConstNull(type
), res
);
3865 LLVMDisposeBuilder(first_builder
);
3870 static LLVMValueRef
si_build_alloca_undef(struct nir_to_llvm_context
*ctx
,
3874 LLVMValueRef ptr
= ac_build_alloca(ctx
, type
, name
);
3875 LLVMBuildStore(ctx
->builder
, LLVMGetUndef(type
), ptr
);
3880 handle_shader_output_decl(struct nir_to_llvm_context
*ctx
,
3881 struct nir_variable
*variable
)
3883 int idx
= variable
->data
.location
+ variable
->data
.index
;
3884 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
3886 variable
->data
.driver_location
= idx
* 4;
3888 if (ctx
->stage
== MESA_SHADER_VERTEX
) {
3890 if (idx
== VARYING_SLOT_CLIP_DIST0
||
3891 idx
== VARYING_SLOT_CULL_DIST0
) {
3892 int length
= glsl_get_length(variable
->type
);
3893 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
3894 ctx
->shader_info
->vs
.clip_dist_mask
= (1 << length
) - 1;
3895 ctx
->num_clips
= length
;
3896 } else if (idx
== VARYING_SLOT_CULL_DIST0
) {
3897 ctx
->shader_info
->vs
.cull_dist_mask
= (1 << length
) - 1;
3898 ctx
->num_culls
= length
;
3907 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
3908 for (unsigned chan
= 0; chan
< 4; chan
++) {
3909 ctx
->outputs
[radeon_llvm_reg_index_soa(idx
+ i
, chan
)] =
3910 si_build_alloca_undef(ctx
, ctx
->f32
, "");
3913 ctx
->output_mask
|= ((1ull << attrib_count
) - 1) << idx
;
3917 setup_locals(struct nir_to_llvm_context
*ctx
,
3918 struct nir_function
*func
)
3921 ctx
->num_locals
= 0;
3922 nir_foreach_variable(variable
, &func
->impl
->locals
) {
3923 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
3924 variable
->data
.driver_location
= ctx
->num_locals
* 4;
3925 ctx
->num_locals
+= attrib_count
;
3927 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
3931 for (i
= 0; i
< ctx
->num_locals
; i
++) {
3932 for (j
= 0; j
< 4; j
++) {
3933 ctx
->locals
[i
* 4 + j
] =
3934 si_build_alloca_undef(ctx
, ctx
->f32
, "temp");
3940 emit_float_saturate(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
, float lo
, float hi
)
3942 v
= to_float(ctx
, v
);
3943 v
= emit_intrin_2f_param(ctx
, "llvm.maxnum.f32", v
, LLVMConstReal(ctx
->f32
, lo
));
3944 return emit_intrin_2f_param(ctx
, "llvm.minnum.f32", v
, LLVMConstReal(ctx
->f32
, hi
));
3948 static LLVMValueRef
emit_pack_int16(struct nir_to_llvm_context
*ctx
,
3949 LLVMValueRef src0
, LLVMValueRef src1
)
3951 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
3952 LLVMValueRef comp
[2];
3954 comp
[0] = LLVMBuildAnd(ctx
->builder
, src0
, LLVMConstInt(ctx
-> i32
, 65535, 0), "");
3955 comp
[1] = LLVMBuildAnd(ctx
->builder
, src1
, LLVMConstInt(ctx
-> i32
, 65535, 0), "");
3956 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
3957 return LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
3960 /* Initialize arguments for the shader export intrinsic */
3962 si_llvm_init_export_args(struct nir_to_llvm_context
*ctx
,
3963 LLVMValueRef
*values
,
3967 /* Default is 0xf. Adjusted below depending on the format. */
3968 args
[0] = LLVMConstInt(ctx
->i32
, target
!= V_008DFC_SQ_EXP_NULL
? 0xf : 0, false);
3969 /* Specify whether the EXEC mask represents the valid mask */
3970 args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
3972 /* Specify whether this is the last export */
3973 args
[2] = LLVMConstInt(ctx
->i32
, 0, false);
3974 /* Specify the target we are exporting */
3975 args
[3] = LLVMConstInt(ctx
->i32
, target
, false);
3977 args
[4] = LLVMConstInt(ctx
->i32
, 0, false); /* COMPR flag */
3978 args
[5] = LLVMGetUndef(ctx
->f32
);
3979 args
[6] = LLVMGetUndef(ctx
->f32
);
3980 args
[7] = LLVMGetUndef(ctx
->f32
);
3981 args
[8] = LLVMGetUndef(ctx
->f32
);
3986 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&& target
>= V_008DFC_SQ_EXP_MRT
) {
3987 LLVMValueRef val
[4];
3988 unsigned index
= target
- V_008DFC_SQ_EXP_MRT
;
3989 unsigned col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * index
)) & 0xf;
3990 bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> index
) & 1;
3992 switch(col_format
) {
3993 case V_028714_SPI_SHADER_ZERO
:
3994 args
[0] = LLVMConstInt(ctx
->i32
, 0x0, 0);
3995 args
[3] = LLVMConstInt(ctx
->i32
, V_008DFC_SQ_EXP_NULL
, 0);
3998 case V_028714_SPI_SHADER_32_R
:
3999 args
[0] = LLVMConstInt(ctx
->i32
, 0x1, 0);
4000 args
[5] = values
[0];
4003 case V_028714_SPI_SHADER_32_GR
:
4004 args
[0] = LLVMConstInt(ctx
->i32
, 0x3, 0);
4005 args
[5] = values
[0];
4006 args
[6] = values
[1];
4009 case V_028714_SPI_SHADER_32_AR
:
4010 args
[0] = LLVMConstInt(ctx
->i32
, 0x9, 0);
4011 args
[5] = values
[0];
4012 args
[8] = values
[3];
4015 case V_028714_SPI_SHADER_FP16_ABGR
:
4016 args
[4] = ctx
->i32one
;
4018 for (unsigned chan
= 0; chan
< 2; chan
++) {
4019 LLVMValueRef pack_args
[2] = {
4021 values
[2 * chan
+ 1]
4023 LLVMValueRef packed
;
4025 packed
= ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.SI.packf16",
4026 ctx
->i32
, pack_args
, 2,
4027 AC_FUNC_ATTR_READNONE
);
4028 args
[chan
+ 5] = packed
;
4032 case V_028714_SPI_SHADER_UNORM16_ABGR
:
4033 for (unsigned chan
= 0; chan
< 4; chan
++) {
4034 val
[chan
] = emit_float_saturate(ctx
, values
[chan
], 0, 1);
4035 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
4036 LLVMConstReal(ctx
->f32
, 65535), "");
4037 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
4038 LLVMConstReal(ctx
->f32
, 0.5), "");
4039 val
[chan
] = LLVMBuildFPToUI(ctx
->builder
, val
[chan
],
4043 args
[4] = ctx
->i32one
;
4044 args
[5] = emit_pack_int16(ctx
, val
[0], val
[1]);
4045 args
[6] = emit_pack_int16(ctx
, val
[2], val
[3]);
4048 case V_028714_SPI_SHADER_SNORM16_ABGR
:
4049 for (unsigned chan
= 0; chan
< 4; chan
++) {
4050 val
[chan
] = emit_float_saturate(ctx
, values
[chan
], -1, 1);
4051 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
4052 LLVMConstReal(ctx
->f32
, 32767), "");
4054 /* If positive, add 0.5, else add -0.5. */
4055 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
4056 LLVMBuildSelect(ctx
->builder
,
4057 LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
,
4058 val
[chan
], ctx
->f32zero
, ""),
4059 LLVMConstReal(ctx
->f32
, 0.5),
4060 LLVMConstReal(ctx
->f32
, -0.5), ""), "");
4061 val
[chan
] = LLVMBuildFPToSI(ctx
->builder
, val
[chan
], ctx
->i32
, "");
4064 args
[4] = ctx
->i32one
;
4065 args
[5] = emit_pack_int16(ctx
, val
[0], val
[1]);
4066 args
[6] = emit_pack_int16(ctx
, val
[2], val
[3]);
4069 case V_028714_SPI_SHADER_UINT16_ABGR
: {
4070 LLVMValueRef max
= LLVMConstInt(ctx
->i32
, is_int8
? 255 : 65535, 0);
4072 for (unsigned chan
= 0; chan
< 4; chan
++) {
4073 val
[chan
] = to_integer(ctx
, values
[chan
]);
4074 val
[chan
] = emit_minmax_int(ctx
, LLVMIntULT
, val
[chan
], max
);
4077 args
[4] = ctx
->i32one
;
4078 args
[5] = emit_pack_int16(ctx
, val
[0], val
[1]);
4079 args
[6] = emit_pack_int16(ctx
, val
[2], val
[3]);
4083 case V_028714_SPI_SHADER_SINT16_ABGR
: {
4084 LLVMValueRef max
= LLVMConstInt(ctx
->i32
, is_int8
? 127 : 32767, 0);
4085 LLVMValueRef min
= LLVMConstInt(ctx
->i32
, is_int8
? -128 : -32768, 0);
4088 for (unsigned chan
= 0; chan
< 4; chan
++) {
4089 val
[chan
] = to_integer(ctx
, values
[chan
]);
4090 val
[chan
] = emit_minmax_int(ctx
, LLVMIntSLT
, val
[chan
], max
);
4091 val
[chan
] = emit_minmax_int(ctx
, LLVMIntSGT
, val
[chan
], min
);
4094 args
[4] = ctx
->i32one
;
4095 args
[5] = emit_pack_int16(ctx
, val
[0], val
[1]);
4096 args
[6] = emit_pack_int16(ctx
, val
[2], val
[3]);
4101 case V_028714_SPI_SHADER_32_ABGR
:
4102 memcpy(&args
[5], values
, sizeof(values
[0]) * 4);
4106 memcpy(&args
[5], values
, sizeof(values
[0]) * 4);
4108 for (unsigned i
= 5; i
< 9; ++i
)
4109 args
[i
] = to_float(ctx
, args
[i
]);
4113 handle_vs_outputs_post(struct nir_to_llvm_context
*ctx
)
4115 uint32_t param_count
= 0;
4117 unsigned pos_idx
, num_pos_exports
= 0;
4118 LLVMValueRef args
[9];
4119 LLVMValueRef pos_args
[4][9] = { { 0 } };
4120 LLVMValueRef psize_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
4122 const uint64_t clip_mask
= ctx
->output_mask
& ((1ull << VARYING_SLOT_CLIP_DIST0
) |
4123 (1ull << VARYING_SLOT_CLIP_DIST1
) |
4124 (1ull << VARYING_SLOT_CULL_DIST0
) |
4125 (1ull << VARYING_SLOT_CULL_DIST1
));
4128 LLVMValueRef slots
[8];
4131 if (ctx
->shader_info
->vs
.cull_dist_mask
)
4132 ctx
->shader_info
->vs
.cull_dist_mask
<<= ctx
->num_clips
;
4134 i
= VARYING_SLOT_CLIP_DIST0
;
4135 for (j
= 0; j
< ctx
->num_clips
; j
++)
4136 slots
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4137 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4138 i
= VARYING_SLOT_CULL_DIST0
;
4139 for (j
= 0; j
< ctx
->num_culls
; j
++)
4140 slots
[ctx
->num_clips
+ j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4141 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4143 for (i
= ctx
->num_clips
+ ctx
->num_culls
; i
< 8; i
++)
4144 slots
[i
] = LLVMGetUndef(ctx
->f32
);
4146 if (ctx
->num_clips
+ ctx
->num_culls
> 4) {
4147 target
= V_008DFC_SQ_EXP_POS
+ 3;
4148 si_llvm_init_export_args(ctx
, &slots
[4], target
, args
);
4149 memcpy(pos_args
[target
- V_008DFC_SQ_EXP_POS
],
4150 args
, sizeof(args
));
4153 target
= V_008DFC_SQ_EXP_POS
+ 2;
4154 si_llvm_init_export_args(ctx
, &slots
[0], target
, args
);
4155 memcpy(pos_args
[target
- V_008DFC_SQ_EXP_POS
],
4156 args
, sizeof(args
));
4160 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
4161 LLVMValueRef values
[4];
4162 if (!(ctx
->output_mask
& (1ull << i
)))
4165 for (unsigned j
= 0; j
< 4; j
++)
4166 values
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4167 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4169 if (i
== VARYING_SLOT_POS
) {
4170 target
= V_008DFC_SQ_EXP_POS
;
4171 } else if (i
== VARYING_SLOT_CLIP_DIST0
||
4172 i
== VARYING_SLOT_CLIP_DIST1
||
4173 i
== VARYING_SLOT_CULL_DIST0
||
4174 i
== VARYING_SLOT_CULL_DIST1
) {
4176 } else if (i
== VARYING_SLOT_PSIZ
) {
4177 ctx
->shader_info
->vs
.writes_pointsize
= true;
4178 psize_value
= values
[0];
4180 } else if (i
== VARYING_SLOT_LAYER
) {
4181 ctx
->shader_info
->vs
.writes_layer
= true;
4182 layer_value
= values
[0];
4184 } else if (i
== VARYING_SLOT_VIEWPORT
) {
4185 ctx
->shader_info
->vs
.writes_viewport_index
= true;
4186 viewport_index_value
= values
[0];
4188 } else if (i
>= VARYING_SLOT_VAR0
) {
4189 ctx
->shader_info
->vs
.export_mask
|= 1u << (i
- VARYING_SLOT_VAR0
);
4190 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
4194 si_llvm_init_export_args(ctx
, values
, target
, args
);
4196 if (target
>= V_008DFC_SQ_EXP_POS
&&
4197 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
4198 memcpy(pos_args
[target
- V_008DFC_SQ_EXP_POS
],
4199 args
, sizeof(args
));
4201 ac_emit_llvm_intrinsic(&ctx
->ac
,
4208 /* We need to add the position output manually if it's missing. */
4209 if (!pos_args
[0][0]) {
4210 pos_args
[0][0] = LLVMConstInt(ctx
->i32
, 0xf, false);
4211 pos_args
[0][1] = ctx
->i32zero
; /* EXEC mask */
4212 pos_args
[0][2] = ctx
->i32zero
; /* last export? */
4213 pos_args
[0][3] = LLVMConstInt(ctx
->i32
, V_008DFC_SQ_EXP_POS
, false);
4214 pos_args
[0][4] = ctx
->i32zero
; /* COMPR flag */
4215 pos_args
[0][5] = ctx
->f32zero
; /* X */
4216 pos_args
[0][6] = ctx
->f32zero
; /* Y */
4217 pos_args
[0][7] = ctx
->f32zero
; /* Z */
4218 pos_args
[0][8] = ctx
->f32one
; /* W */
4221 uint32_t mask
= ((ctx
->shader_info
->vs
.writes_pointsize
== true ? 1 : 0) |
4222 (ctx
->shader_info
->vs
.writes_layer
== true ? 4 : 0) |
4223 (ctx
->shader_info
->vs
.writes_viewport_index
== true ? 8 : 0));
4225 pos_args
[1][0] = LLVMConstInt(ctx
->i32
, mask
, false); /* writemask */
4226 pos_args
[1][1] = ctx
->i32zero
; /* EXEC mask */
4227 pos_args
[1][2] = ctx
->i32zero
; /* last export? */
4228 pos_args
[1][3] = LLVMConstInt(ctx
->i32
, V_008DFC_SQ_EXP_POS
+ 1, false);
4229 pos_args
[1][4] = ctx
->i32zero
; /* COMPR flag */
4230 pos_args
[1][5] = ctx
->f32zero
; /* X */
4231 pos_args
[1][6] = ctx
->f32zero
; /* Y */
4232 pos_args
[1][7] = ctx
->f32zero
; /* Z */
4233 pos_args
[1][8] = ctx
->f32zero
; /* W */
4235 if (ctx
->shader_info
->vs
.writes_pointsize
== true)
4236 pos_args
[1][5] = psize_value
;
4237 if (ctx
->shader_info
->vs
.writes_layer
== true)
4238 pos_args
[1][7] = layer_value
;
4239 if (ctx
->shader_info
->vs
.writes_viewport_index
== true)
4240 pos_args
[1][8] = viewport_index_value
;
4242 for (i
= 0; i
< 4; i
++) {
4248 for (i
= 0; i
< 4; i
++) {
4249 if (!pos_args
[i
][0])
4252 /* Specify the target we are exporting */
4253 pos_args
[i
][3] = LLVMConstInt(ctx
->i32
, V_008DFC_SQ_EXP_POS
+ pos_idx
++, false);
4254 if (pos_idx
== num_pos_exports
)
4255 pos_args
[i
][2] = ctx
->i32one
;
4256 ac_emit_llvm_intrinsic(&ctx
->ac
,
4262 ctx
->shader_info
->vs
.pos_exports
= num_pos_exports
;
4263 ctx
->shader_info
->vs
.param_exports
= param_count
;
4267 si_export_mrt_color(struct nir_to_llvm_context
*ctx
,
4268 LLVMValueRef
*color
, unsigned param
, bool is_last
)
4270 LLVMValueRef args
[9];
4272 si_llvm_init_export_args(ctx
, color
, param
,
4276 args
[1] = ctx
->i32one
; /* whether the EXEC mask is valid */
4277 args
[2] = ctx
->i32one
; /* DONE bit */
4278 } else if (args
[0] == ctx
->i32zero
)
4279 return; /* unnecessary NULL export */
4281 ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.SI.export",
4282 ctx
->voidt
, args
, 9, 0);
4286 si_export_mrt_z(struct nir_to_llvm_context
*ctx
,
4287 LLVMValueRef depth
, LLVMValueRef stencil
,
4288 LLVMValueRef samplemask
)
4290 LLVMValueRef args
[9];
4292 args
[1] = ctx
->i32one
; /* whether the EXEC mask is valid */
4293 args
[2] = ctx
->i32one
; /* DONE bit */
4294 /* Specify the target we are exporting */
4295 args
[3] = LLVMConstInt(ctx
->i32
, V_008DFC_SQ_EXP_MRTZ
, false);
4297 args
[4] = ctx
->i32zero
; /* COMP flag */
4298 args
[5] = LLVMGetUndef(ctx
->f32
); /* R, depth */
4299 args
[6] = LLVMGetUndef(ctx
->f32
); /* G, stencil test val[0:7], stencil op val[8:15] */
4300 args
[7] = LLVMGetUndef(ctx
->f32
); /* B, sample mask */
4301 args
[8] = LLVMGetUndef(ctx
->f32
); /* A, alpha to mask */
4314 args
[7] = samplemask
;
4318 /* SI (except OLAND) has a bug that it only looks
4319 * at the X writemask component. */
4320 if (ctx
->options
->chip_class
== SI
&&
4321 ctx
->options
->family
!= CHIP_OLAND
)
4324 args
[0] = LLVMConstInt(ctx
->i32
, mask
, false);
4325 ac_emit_llvm_intrinsic(&ctx
->ac
, "llvm.SI.export",
4326 ctx
->voidt
, args
, 9, 0);
4330 handle_fs_outputs_post(struct nir_to_llvm_context
*ctx
)
4333 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
4335 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
4336 LLVMValueRef values
[4];
4338 if (!(ctx
->output_mask
& (1ull << i
)))
4341 if (i
== FRAG_RESULT_DEPTH
) {
4342 ctx
->shader_info
->fs
.writes_z
= true;
4343 depth
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4344 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
4345 } else if (i
== FRAG_RESULT_STENCIL
) {
4346 ctx
->shader_info
->fs
.writes_stencil
= true;
4347 stencil
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4348 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
4351 for (unsigned j
= 0; j
< 4; j
++)
4352 values
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4353 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4355 if (!ctx
->shader_info
->fs
.writes_z
&& !ctx
->shader_info
->fs
.writes_stencil
)
4356 last
= ctx
->output_mask
<= ((1ull << (i
+ 1)) - 1);
4358 si_export_mrt_color(ctx
, values
, V_008DFC_SQ_EXP_MRT
+ index
, last
);
4363 if (depth
|| stencil
)
4364 si_export_mrt_z(ctx
, depth
, stencil
, samplemask
);
4366 si_export_mrt_color(ctx
, NULL
, V_008DFC_SQ_EXP_NULL
, true);
4368 ctx
->shader_info
->fs
.output_mask
= index
? ((1ull << index
) - 1) : 0;
4372 handle_shader_outputs_post(struct nir_to_llvm_context
*ctx
)
4374 switch (ctx
->stage
) {
4375 case MESA_SHADER_VERTEX
:
4376 handle_vs_outputs_post(ctx
);
4378 case MESA_SHADER_FRAGMENT
:
4379 handle_fs_outputs_post(ctx
);
4387 handle_shared_compute_var(struct nir_to_llvm_context
*ctx
,
4388 struct nir_variable
*variable
, uint32_t *offset
, int idx
)
4390 unsigned size
= glsl_count_attribute_slots(variable
->type
, false);
4391 variable
->data
.driver_location
= *offset
;
4395 static void ac_llvm_finalize_module(struct nir_to_llvm_context
* ctx
)
4397 LLVMPassManagerRef passmgr
;
4398 /* Create the pass manager */
4399 passmgr
= LLVMCreateFunctionPassManagerForModule(
4402 /* This pass should eliminate all the load and store instructions */
4403 LLVMAddPromoteMemoryToRegisterPass(passmgr
);
4405 /* Add some optimization passes */
4406 LLVMAddScalarReplAggregatesPass(passmgr
);
4407 LLVMAddLICMPass(passmgr
);
4408 LLVMAddAggressiveDCEPass(passmgr
);
4409 LLVMAddCFGSimplificationPass(passmgr
);
4410 LLVMAddInstructionCombiningPass(passmgr
);
4413 LLVMInitializeFunctionPassManager(passmgr
);
4414 LLVMRunFunctionPassManager(passmgr
, ctx
->main_function
);
4415 LLVMFinalizeFunctionPassManager(passmgr
);
4417 LLVMDisposeBuilder(ctx
->builder
);
4418 LLVMDisposePassManager(passmgr
);
4422 LLVMModuleRef
ac_translate_nir_to_llvm(LLVMTargetMachineRef tm
,
4423 struct nir_shader
*nir
,
4424 struct ac_shader_variant_info
*shader_info
,
4425 const struct ac_nir_compiler_options
*options
)
4427 struct nir_to_llvm_context ctx
= {0};
4428 struct nir_function
*func
;
4430 ctx
.options
= options
;
4431 ctx
.shader_info
= shader_info
;
4432 ctx
.context
= LLVMContextCreate();
4433 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
4435 ac_llvm_context_init(&ctx
.ac
, ctx
.context
);
4436 ctx
.ac
.module
= ctx
.module
;
4438 ctx
.has_ds_bpermute
= ctx
.options
->chip_class
>= VI
;
4440 memset(shader_info
, 0, sizeof(*shader_info
));
4442 LLVMSetTarget(ctx
.module
, options
->supports_spill
? "amdgcn-mesa-mesa3d" : "amdgcn--");
4445 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
4446 ctx
.ac
.builder
= ctx
.builder
;
4447 ctx
.stage
= nir
->stage
;
4449 for (i
= 0; i
< AC_UD_MAX_SETS
; i
++)
4450 shader_info
->user_sgprs_locs
.descriptor_sets
[i
].sgpr_idx
= -1;
4451 for (i
= 0; i
< AC_UD_MAX_UD
; i
++)
4452 shader_info
->user_sgprs_locs
.shader_data
[i
].sgpr_idx
= -1;
4454 create_function(&ctx
);
4456 if (nir
->stage
== MESA_SHADER_COMPUTE
) {
4458 nir_foreach_variable(variable
, &nir
->shared
)
4462 uint32_t shared_size
= 0;
4464 LLVMTypeRef i8p
= LLVMPointerType(ctx
.i8
, LOCAL_ADDR_SPACE
);
4465 nir_foreach_variable(variable
, &nir
->shared
) {
4466 handle_shared_compute_var(&ctx
, variable
, &shared_size
, idx
);
4471 var
= LLVMAddGlobalInAddressSpace(ctx
.module
,
4472 LLVMArrayType(ctx
.i8
, shared_size
),
4475 LLVMSetAlignment(var
, 4);
4476 ctx
.shared_memory
= LLVMBuildBitCast(ctx
.builder
, var
, i8p
, "");
4480 nir_foreach_variable(variable
, &nir
->inputs
)
4481 handle_shader_input_decl(&ctx
, variable
);
4483 if (nir
->stage
== MESA_SHADER_FRAGMENT
)
4484 handle_fs_inputs_pre(&ctx
, nir
);
4486 nir_foreach_variable(variable
, &nir
->outputs
)
4487 handle_shader_output_decl(&ctx
, variable
);
4489 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4490 _mesa_key_pointer_equal
);
4491 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4492 _mesa_key_pointer_equal
);
4494 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
4496 setup_locals(&ctx
, func
);
4498 visit_cf_list(&ctx
, &func
->impl
->body
);
4499 phi_post_pass(&ctx
);
4501 handle_shader_outputs_post(&ctx
);
4502 LLVMBuildRetVoid(ctx
.builder
);
4504 ac_llvm_finalize_module(&ctx
);
4506 ralloc_free(ctx
.defs
);
4507 ralloc_free(ctx
.phis
);
4512 static void ac_diagnostic_handler(LLVMDiagnosticInfoRef di
, void *context
)
4514 unsigned *retval
= (unsigned *)context
;
4515 LLVMDiagnosticSeverity severity
= LLVMGetDiagInfoSeverity(di
);
4516 char *description
= LLVMGetDiagInfoDescription(di
);
4518 if (severity
== LLVMDSError
) {
4520 fprintf(stderr
, "LLVM triggered Diagnostic Handler: %s\n",
4524 LLVMDisposeMessage(description
);
4527 static unsigned ac_llvm_compile(LLVMModuleRef M
,
4528 struct ac_shader_binary
*binary
,
4529 LLVMTargetMachineRef tm
)
4531 unsigned retval
= 0;
4533 LLVMContextRef llvm_ctx
;
4534 LLVMMemoryBufferRef out_buffer
;
4535 unsigned buffer_size
;
4536 const char *buffer_data
;
4539 /* Setup Diagnostic Handler*/
4540 llvm_ctx
= LLVMGetModuleContext(M
);
4542 LLVMContextSetDiagnosticHandler(llvm_ctx
, ac_diagnostic_handler
,
4546 mem_err
= LLVMTargetMachineEmitToMemoryBuffer(tm
, M
, LLVMObjectFile
,
4549 /* Process Errors/Warnings */
4551 fprintf(stderr
, "%s: %s", __FUNCTION__
, err
);
4557 /* Extract Shader Code*/
4558 buffer_size
= LLVMGetBufferSize(out_buffer
);
4559 buffer_data
= LLVMGetBufferStart(out_buffer
);
4561 ac_elf_read(buffer_data
, buffer_size
, binary
);
4564 LLVMDisposeMemoryBuffer(out_buffer
);
4570 static void ac_compile_llvm_module(LLVMTargetMachineRef tm
,
4571 LLVMModuleRef llvm_module
,
4572 struct ac_shader_binary
*binary
,
4573 struct ac_shader_config
*config
,
4574 struct ac_shader_variant_info
*shader_info
,
4575 gl_shader_stage stage
,
4576 bool dump_shader
, bool supports_spill
)
4579 ac_dump_module(llvm_module
);
4581 memset(binary
, 0, sizeof(*binary
));
4582 int v
= ac_llvm_compile(llvm_module
, binary
, tm
);
4584 fprintf(stderr
, "compile failed\n");
4588 fprintf(stderr
, "disasm:\n%s\n", binary
->disasm_string
);
4590 ac_shader_binary_read_config(binary
, config
, 0, supports_spill
);
4592 LLVMContextRef ctx
= LLVMGetModuleContext(llvm_module
);
4593 LLVMDisposeModule(llvm_module
);
4594 LLVMContextDispose(ctx
);
4596 if (stage
== MESA_SHADER_FRAGMENT
) {
4597 shader_info
->num_input_vgprs
= 0;
4598 if (G_0286CC_PERSP_SAMPLE_ENA(config
->spi_ps_input_addr
))
4599 shader_info
->num_input_vgprs
+= 2;
4600 if (G_0286CC_PERSP_CENTER_ENA(config
->spi_ps_input_addr
))
4601 shader_info
->num_input_vgprs
+= 2;
4602 if (G_0286CC_PERSP_CENTROID_ENA(config
->spi_ps_input_addr
))
4603 shader_info
->num_input_vgprs
+= 2;
4604 if (G_0286CC_PERSP_PULL_MODEL_ENA(config
->spi_ps_input_addr
))
4605 shader_info
->num_input_vgprs
+= 3;
4606 if (G_0286CC_LINEAR_SAMPLE_ENA(config
->spi_ps_input_addr
))
4607 shader_info
->num_input_vgprs
+= 2;
4608 if (G_0286CC_LINEAR_CENTER_ENA(config
->spi_ps_input_addr
))
4609 shader_info
->num_input_vgprs
+= 2;
4610 if (G_0286CC_LINEAR_CENTROID_ENA(config
->spi_ps_input_addr
))
4611 shader_info
->num_input_vgprs
+= 2;
4612 if (G_0286CC_LINE_STIPPLE_TEX_ENA(config
->spi_ps_input_addr
))
4613 shader_info
->num_input_vgprs
+= 1;
4614 if (G_0286CC_POS_X_FLOAT_ENA(config
->spi_ps_input_addr
))
4615 shader_info
->num_input_vgprs
+= 1;
4616 if (G_0286CC_POS_Y_FLOAT_ENA(config
->spi_ps_input_addr
))
4617 shader_info
->num_input_vgprs
+= 1;
4618 if (G_0286CC_POS_Z_FLOAT_ENA(config
->spi_ps_input_addr
))
4619 shader_info
->num_input_vgprs
+= 1;
4620 if (G_0286CC_POS_W_FLOAT_ENA(config
->spi_ps_input_addr
))
4621 shader_info
->num_input_vgprs
+= 1;
4622 if (G_0286CC_FRONT_FACE_ENA(config
->spi_ps_input_addr
))
4623 shader_info
->num_input_vgprs
+= 1;
4624 if (G_0286CC_ANCILLARY_ENA(config
->spi_ps_input_addr
))
4625 shader_info
->num_input_vgprs
+= 1;
4626 if (G_0286CC_SAMPLE_COVERAGE_ENA(config
->spi_ps_input_addr
))
4627 shader_info
->num_input_vgprs
+= 1;
4628 if (G_0286CC_POS_FIXED_PT_ENA(config
->spi_ps_input_addr
))
4629 shader_info
->num_input_vgprs
+= 1;
4631 config
->num_vgprs
= MAX2(config
->num_vgprs
, shader_info
->num_input_vgprs
);
4633 /* +3 for scratch wave offset and VCC */
4634 config
->num_sgprs
= MAX2(config
->num_sgprs
,
4635 shader_info
->num_input_sgprs
+ 3);
4638 void ac_compile_nir_shader(LLVMTargetMachineRef tm
,
4639 struct ac_shader_binary
*binary
,
4640 struct ac_shader_config
*config
,
4641 struct ac_shader_variant_info
*shader_info
,
4642 struct nir_shader
*nir
,
4643 const struct ac_nir_compiler_options
*options
,
4647 LLVMModuleRef llvm_module
= ac_translate_nir_to_llvm(tm
, nir
, shader_info
,
4650 ac_compile_llvm_module(tm
, llvm_module
, binary
, config
, shader_info
, nir
->stage
, dump_shader
, options
->supports_spill
);
4651 switch (nir
->stage
) {
4652 case MESA_SHADER_COMPUTE
:
4653 for (int i
= 0; i
< 3; ++i
)
4654 shader_info
->cs
.block_size
[i
] = nir
->info
->cs
.local_size
[i
];
4656 case MESA_SHADER_FRAGMENT
:
4657 shader_info
->fs
.early_fragment_test
= nir
->info
->fs
.early_fragment_tests
;