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 const struct ac_nir_compiler_options
*options
;
55 struct ac_shader_variant_info
*shader_info
;
57 LLVMContextRef context
;
59 LLVMBuilderRef builder
;
60 LLVMValueRef main_function
;
62 struct hash_table
*defs
;
63 struct hash_table
*phis
;
65 LLVMValueRef descriptor_sets
[4];
66 LLVMValueRef push_constants
;
67 LLVMValueRef num_work_groups
;
68 LLVMValueRef workgroup_ids
;
69 LLVMValueRef local_invocation_ids
;
72 LLVMValueRef vertex_buffers
;
73 LLVMValueRef base_vertex
;
74 LLVMValueRef start_instance
;
75 LLVMValueRef vertex_id
;
76 LLVMValueRef rel_auto_id
;
77 LLVMValueRef vs_prim_id
;
78 LLVMValueRef instance_id
;
80 LLVMValueRef prim_mask
;
81 LLVMValueRef sample_positions
;
82 LLVMValueRef persp_sample
, persp_center
, persp_centroid
;
83 LLVMValueRef linear_sample
, linear_center
, linear_centroid
;
84 LLVMValueRef front_face
;
85 LLVMValueRef ancillary
;
86 LLVMValueRef frag_pos
[4];
88 LLVMBasicBlockRef continue_block
;
89 LLVMBasicBlockRef break_block
;
107 LLVMValueRef i32zero
;
109 LLVMValueRef f32zero
;
111 LLVMValueRef v4f32empty
;
113 unsigned range_md_kind
;
114 unsigned uniform_md_kind
;
115 unsigned fpmath_md_kind
;
116 unsigned invariant_load_md_kind
;
117 LLVMValueRef empty_md
;
118 LLVMValueRef fpmath_md_2p5_ulp
;
119 gl_shader_stage stage
;
122 LLVMValueRef inputs
[RADEON_LLVM_MAX_INPUTS
* 4];
123 LLVMValueRef outputs
[RADEON_LLVM_MAX_OUTPUTS
* 4];
125 LLVMValueRef shared_memory
;
127 uint64_t output_mask
;
129 LLVMValueRef
*locals
;
134 bool has_ds_bpermute
;
138 LLVMValueRef args
[12];
140 LLVMTypeRef dst_type
;
145 AC_FUNC_ATTR_ALWAYSINLINE
= (1 << 0),
146 AC_FUNC_ATTR_BYVAL
= (1 << 1),
147 AC_FUNC_ATTR_INREG
= (1 << 2),
148 AC_FUNC_ATTR_NOALIAS
= (1 << 3),
149 AC_FUNC_ATTR_NOUNWIND
= (1 << 4),
150 AC_FUNC_ATTR_READNONE
= (1 << 5),
151 AC_FUNC_ATTR_READONLY
= (1 << 6),
152 AC_FUNC_ATTR_LAST
= (1 << 7)
155 #if HAVE_LLVM < 0x0400
156 static LLVMAttribute
ac_attr_to_llvm_attr(enum ac_func_attr attr
)
159 case AC_FUNC_ATTR_ALWAYSINLINE
: return LLVMAlwaysInlineAttribute
;
160 case AC_FUNC_ATTR_BYVAL
: return LLVMByValAttribute
;
161 case AC_FUNC_ATTR_INREG
: return LLVMInRegAttribute
;
162 case AC_FUNC_ATTR_NOALIAS
: return LLVMNoAliasAttribute
;
163 case AC_FUNC_ATTR_NOUNWIND
: return LLVMNoUnwindAttribute
;
164 case AC_FUNC_ATTR_READNONE
: return LLVMReadNoneAttribute
;
165 case AC_FUNC_ATTR_READONLY
: return LLVMReadOnlyAttribute
;
167 fprintf(stderr
, "Unhandled function attribute: %x\n", attr
);
174 static const char *attr_to_str(enum ac_func_attr attr
)
177 case AC_FUNC_ATTR_ALWAYSINLINE
: return "alwaysinline";
178 case AC_FUNC_ATTR_BYVAL
: return "byval";
179 case AC_FUNC_ATTR_INREG
: return "inreg";
180 case AC_FUNC_ATTR_NOALIAS
: return "noalias";
181 case AC_FUNC_ATTR_NOUNWIND
: return "nounwind";
182 case AC_FUNC_ATTR_READNONE
: return "readnone";
183 case AC_FUNC_ATTR_READONLY
: return "readonly";
185 fprintf(stderr
, "Unhandled function attribute: %x\n", attr
);
193 ac_add_function_attr(LLVMValueRef function
,
195 enum ac_func_attr attr
)
198 #if HAVE_LLVM < 0x0400
199 LLVMAttribute llvm_attr
= ac_attr_to_llvm_attr(attr
);
200 if (attr_idx
== -1) {
201 LLVMAddFunctionAttr(function
, llvm_attr
);
203 LLVMAddAttribute(LLVMGetParam(function
, attr_idx
- 1), llvm_attr
);
206 LLVMContextRef context
= LLVMGetModuleContext(LLVMGetGlobalParent(function
));
207 const char *attr_name
= attr_to_str(attr
);
208 unsigned kind_id
= LLVMGetEnumAttributeKindForName(attr_name
,
210 LLVMAttributeRef llvm_attr
= LLVMCreateEnumAttribute(context
, kind_id
, 0);
211 LLVMAddAttributeAtIndex(function
, attr_idx
, llvm_attr
);
216 emit_llvm_intrinsic(struct nir_to_llvm_context
*ctx
, const char *name
,
217 LLVMTypeRef return_type
, LLVMValueRef
*params
,
218 unsigned param_count
, unsigned attr_mask
);
219 static LLVMValueRef
get_sampler_desc(struct nir_to_llvm_context
*ctx
,
220 nir_deref_var
*deref
,
221 enum desc_type desc_type
);
222 static unsigned radeon_llvm_reg_index_soa(unsigned index
, unsigned chan
)
224 return (index
* 4) + chan
;
227 static unsigned llvm_get_type_size(LLVMTypeRef type
)
229 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
232 case LLVMIntegerTypeKind
:
233 return LLVMGetIntTypeWidth(type
) / 8;
234 case LLVMFloatTypeKind
:
236 case LLVMPointerTypeKind
:
238 case LLVMVectorTypeKind
:
239 return LLVMGetVectorSize(type
) *
240 llvm_get_type_size(LLVMGetElementType(type
));
247 static void set_llvm_calling_convention(LLVMValueRef func
,
248 gl_shader_stage stage
)
250 enum radeon_llvm_calling_convention calling_conv
;
253 case MESA_SHADER_VERTEX
:
254 case MESA_SHADER_TESS_CTRL
:
255 case MESA_SHADER_TESS_EVAL
:
256 calling_conv
= RADEON_LLVM_AMDGPU_VS
;
258 case MESA_SHADER_GEOMETRY
:
259 calling_conv
= RADEON_LLVM_AMDGPU_GS
;
261 case MESA_SHADER_FRAGMENT
:
262 calling_conv
= RADEON_LLVM_AMDGPU_PS
;
264 case MESA_SHADER_COMPUTE
:
265 calling_conv
= RADEON_LLVM_AMDGPU_CS
;
268 unreachable("Unhandle shader type");
271 LLVMSetFunctionCallConv(func
, calling_conv
);
275 create_llvm_function(LLVMContextRef ctx
, LLVMModuleRef module
,
276 LLVMBuilderRef builder
, LLVMTypeRef
*return_types
,
277 unsigned num_return_elems
, LLVMTypeRef
*param_types
,
278 unsigned param_count
, unsigned array_params
,
279 unsigned sgpr_params
, bool unsafe_math
)
281 LLVMTypeRef main_function_type
, ret_type
;
282 LLVMBasicBlockRef main_function_body
;
284 if (num_return_elems
)
285 ret_type
= LLVMStructTypeInContext(ctx
, return_types
,
286 num_return_elems
, true);
288 ret_type
= LLVMVoidTypeInContext(ctx
);
290 /* Setup the function */
292 LLVMFunctionType(ret_type
, param_types
, param_count
, 0);
293 LLVMValueRef main_function
=
294 LLVMAddFunction(module
, "main", main_function_type
);
296 LLVMAppendBasicBlockInContext(ctx
, main_function
, "main_body");
297 LLVMPositionBuilderAtEnd(builder
, main_function_body
);
299 LLVMSetFunctionCallConv(main_function
, RADEON_LLVM_AMDGPU_CS
);
300 for (unsigned i
= 0; i
< sgpr_params
; ++i
) {
301 if (i
< array_params
) {
302 LLVMValueRef P
= LLVMGetParam(main_function
, i
);
303 ac_add_function_attr(main_function
, i
+ 1, AC_FUNC_ATTR_BYVAL
);
304 ac_add_attr_dereferenceable(P
, UINT64_MAX
);
307 ac_add_function_attr(main_function
, i
+ 1, AC_FUNC_ATTR_INREG
);
312 /* These were copied from some LLVM test. */
313 LLVMAddTargetDependentFunctionAttr(main_function
,
314 "less-precise-fpmad",
316 LLVMAddTargetDependentFunctionAttr(main_function
,
319 LLVMAddTargetDependentFunctionAttr(main_function
,
322 LLVMAddTargetDependentFunctionAttr(main_function
,
326 return main_function
;
329 static LLVMTypeRef
const_array(LLVMTypeRef elem_type
, int num_elements
)
331 return LLVMPointerType(LLVMArrayType(elem_type
, num_elements
),
335 static LLVMValueRef
get_shared_memory_ptr(struct nir_to_llvm_context
*ctx
,
343 offset
= LLVMConstInt(ctx
->i32
, idx
, false);
345 ptr
= ctx
->shared_memory
;
346 ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &offset
, 1, "");
347 addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
348 ptr
= LLVMBuildBitCast(ctx
->builder
, ptr
, LLVMPointerType(type
, addr_space
), "");
352 static LLVMValueRef
to_integer(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
)
354 LLVMTypeRef type
= LLVMTypeOf(v
);
355 if (type
== ctx
->f32
) {
356 return LLVMBuildBitCast(ctx
->builder
, v
, ctx
->i32
, "");
357 } else if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
358 LLVMTypeRef elem_type
= LLVMGetElementType(type
);
359 if (elem_type
== ctx
->f32
) {
360 LLVMTypeRef nt
= LLVMVectorType(ctx
->i32
, LLVMGetVectorSize(type
));
361 return LLVMBuildBitCast(ctx
->builder
, v
, nt
, "");
367 static LLVMValueRef
to_float(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
)
369 LLVMTypeRef type
= LLVMTypeOf(v
);
370 if (type
== ctx
->i32
) {
371 return LLVMBuildBitCast(ctx
->builder
, v
, ctx
->f32
, "");
372 } else if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
373 LLVMTypeRef elem_type
= LLVMGetElementType(type
);
374 if (elem_type
== ctx
->i32
) {
375 LLVMTypeRef nt
= LLVMVectorType(ctx
->f32
, LLVMGetVectorSize(type
));
376 return LLVMBuildBitCast(ctx
->builder
, v
, nt
, "");
382 static LLVMValueRef
unpack_param(struct nir_to_llvm_context
*ctx
,
383 LLVMValueRef param
, unsigned rshift
,
386 LLVMValueRef value
= param
;
388 value
= LLVMBuildLShr(ctx
->builder
, value
,
389 LLVMConstInt(ctx
->i32
, rshift
, false), "");
391 if (rshift
+ bitwidth
< 32) {
392 unsigned mask
= (1 << bitwidth
) - 1;
393 value
= LLVMBuildAnd(ctx
->builder
, value
,
394 LLVMConstInt(ctx
->i32
, mask
, false), "");
399 static LLVMValueRef
build_gep0(struct nir_to_llvm_context
*ctx
,
400 LLVMValueRef base_ptr
, LLVMValueRef index
)
402 LLVMValueRef indices
[2] = {
406 return LLVMBuildGEP(ctx
->builder
, base_ptr
,
410 static LLVMValueRef
build_indexed_load(struct nir_to_llvm_context
*ctx
,
411 LLVMValueRef base_ptr
, LLVMValueRef index
,
414 LLVMValueRef pointer
;
415 pointer
= build_gep0(ctx
, base_ptr
, index
);
417 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
418 return LLVMBuildLoad(ctx
->builder
, pointer
, "");
421 static LLVMValueRef
build_indexed_load_const(struct nir_to_llvm_context
*ctx
,
422 LLVMValueRef base_ptr
, LLVMValueRef index
)
424 LLVMValueRef result
= build_indexed_load(ctx
, base_ptr
, index
, true);
425 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
429 static void create_function(struct nir_to_llvm_context
*ctx
,
430 struct nir_shader
*nir
)
432 LLVMTypeRef arg_types
[23];
433 unsigned arg_idx
= 0;
434 unsigned array_count
= 0;
435 unsigned sgpr_count
= 0, user_sgpr_count
;
438 /* 1 for each descriptor set */
439 for (unsigned i
= 0; i
< 4; ++i
)
440 arg_types
[arg_idx
++] = const_array(ctx
->i8
, 1024 * 1024);
442 /* 1 for push constants and dynamic descriptors */
443 arg_types
[arg_idx
++] = const_array(ctx
->i8
, 1024 * 1024);
445 array_count
= arg_idx
;
446 switch (nir
->stage
) {
447 case MESA_SHADER_COMPUTE
:
448 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3); /* grid size */
449 user_sgpr_count
= arg_idx
;
450 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3);
451 arg_types
[arg_idx
++] = ctx
->i32
;
452 sgpr_count
= arg_idx
;
454 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3);
456 case MESA_SHADER_VERTEX
:
457 arg_types
[arg_idx
++] = const_array(ctx
->v16i8
, 16); /* vertex buffers */
458 arg_types
[arg_idx
++] = ctx
->i32
; // base vertex
459 arg_types
[arg_idx
++] = ctx
->i32
; // start instance
460 user_sgpr_count
= sgpr_count
= arg_idx
;
461 arg_types
[arg_idx
++] = ctx
->i32
; // vertex id
462 arg_types
[arg_idx
++] = ctx
->i32
; // rel auto id
463 arg_types
[arg_idx
++] = ctx
->i32
; // vs prim id
464 arg_types
[arg_idx
++] = ctx
->i32
; // instance id
466 case MESA_SHADER_FRAGMENT
:
467 arg_types
[arg_idx
++] = const_array(ctx
->f32
, 32); /* sample positions */
468 user_sgpr_count
= arg_idx
;
469 arg_types
[arg_idx
++] = ctx
->i32
; /* prim mask */
470 sgpr_count
= arg_idx
;
471 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp sample */
472 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp center */
473 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp centroid */
474 arg_types
[arg_idx
++] = ctx
->v3i32
; /* persp pull model */
475 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear sample */
476 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear center */
477 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear centroid */
478 arg_types
[arg_idx
++] = ctx
->f32
; /* line stipple tex */
479 arg_types
[arg_idx
++] = ctx
->f32
; /* pos x float */
480 arg_types
[arg_idx
++] = ctx
->f32
; /* pos y float */
481 arg_types
[arg_idx
++] = ctx
->f32
; /* pos z float */
482 arg_types
[arg_idx
++] = ctx
->f32
; /* pos w float */
483 arg_types
[arg_idx
++] = ctx
->i32
; /* front face */
484 arg_types
[arg_idx
++] = ctx
->i32
; /* ancillary */
485 arg_types
[arg_idx
++] = ctx
->f32
; /* sample coverage */
486 arg_types
[arg_idx
++] = ctx
->i32
; /* fixed pt */
489 unreachable("Shader stage not implemented");
492 ctx
->main_function
= create_llvm_function(
493 ctx
->context
, ctx
->module
, ctx
->builder
, NULL
, 0, arg_types
,
494 arg_idx
, array_count
, sgpr_count
, ctx
->options
->unsafe_math
);
495 set_llvm_calling_convention(ctx
->main_function
, nir
->stage
);
498 ctx
->shader_info
->num_input_sgprs
= 0;
499 ctx
->shader_info
->num_input_vgprs
= 0;
501 for (i
= 0; i
< user_sgpr_count
; i
++)
502 ctx
->shader_info
->num_user_sgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
504 ctx
->shader_info
->num_input_sgprs
= ctx
->shader_info
->num_user_sgprs
;
505 for (; i
< sgpr_count
; i
++)
506 ctx
->shader_info
->num_input_sgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
508 if (nir
->stage
!= MESA_SHADER_FRAGMENT
)
509 for (; i
< arg_idx
; ++i
)
510 ctx
->shader_info
->num_input_vgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
513 for (unsigned i
= 0; i
< 4; ++i
)
514 ctx
->descriptor_sets
[i
] =
515 LLVMGetParam(ctx
->main_function
, arg_idx
++);
517 ctx
->push_constants
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
519 switch (nir
->stage
) {
520 case MESA_SHADER_COMPUTE
:
521 ctx
->num_work_groups
=
522 LLVMGetParam(ctx
->main_function
, arg_idx
++);
524 LLVMGetParam(ctx
->main_function
, arg_idx
++);
526 LLVMGetParam(ctx
->main_function
, arg_idx
++);
527 ctx
->local_invocation_ids
=
528 LLVMGetParam(ctx
->main_function
, arg_idx
++);
530 case MESA_SHADER_VERTEX
:
531 ctx
->vertex_buffers
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
532 ctx
->base_vertex
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
533 ctx
->start_instance
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
534 ctx
->vertex_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
535 ctx
->rel_auto_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
536 ctx
->vs_prim_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
537 ctx
->instance_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
539 case MESA_SHADER_FRAGMENT
:
540 ctx
->sample_positions
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
541 ctx
->prim_mask
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
542 ctx
->persp_sample
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
543 ctx
->persp_center
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
544 ctx
->persp_centroid
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
546 ctx
->linear_sample
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
547 ctx
->linear_center
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
548 ctx
->linear_centroid
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
549 arg_idx
++; /* line stipple */
550 ctx
->frag_pos
[0] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
551 ctx
->frag_pos
[1] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
552 ctx
->frag_pos
[2] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
553 ctx
->frag_pos
[3] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
554 ctx
->front_face
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
555 ctx
->ancillary
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
558 unreachable("Shader stage not implemented");
562 static void setup_types(struct nir_to_llvm_context
*ctx
)
564 LLVMValueRef args
[4];
566 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->context
);
567 ctx
->i1
= LLVMIntTypeInContext(ctx
->context
, 1);
568 ctx
->i8
= LLVMIntTypeInContext(ctx
->context
, 8);
569 ctx
->i16
= LLVMIntTypeInContext(ctx
->context
, 16);
570 ctx
->i32
= LLVMIntTypeInContext(ctx
->context
, 32);
571 ctx
->i64
= LLVMIntTypeInContext(ctx
->context
, 64);
572 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
573 ctx
->v3i32
= LLVMVectorType(ctx
->i32
, 3);
574 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
575 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
576 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
577 ctx
->f16
= LLVMHalfTypeInContext(ctx
->context
);
578 ctx
->v2f32
= LLVMVectorType(ctx
->f32
, 2);
579 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
580 ctx
->v16i8
= LLVMVectorType(ctx
->i8
, 16);
582 ctx
->i32zero
= LLVMConstInt(ctx
->i32
, 0, false);
583 ctx
->i32one
= LLVMConstInt(ctx
->i32
, 1, false);
584 ctx
->f32zero
= LLVMConstReal(ctx
->f32
, 0.0);
585 ctx
->f32one
= LLVMConstReal(ctx
->f32
, 1.0);
587 args
[0] = ctx
->f32zero
;
588 args
[1] = ctx
->f32zero
;
589 args
[2] = ctx
->f32zero
;
590 args
[3] = ctx
->f32one
;
591 ctx
->v4f32empty
= LLVMConstVector(args
, 4);
593 ctx
->range_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
595 ctx
->invariant_load_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
596 "invariant.load", 14);
597 ctx
->uniform_md_kind
=
598 LLVMGetMDKindIDInContext(ctx
->context
, "amdgpu.uniform", 14);
599 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
601 ctx
->fpmath_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
, "fpmath", 6);
603 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
604 ctx
->fpmath_md_2p5_ulp
= LLVMMDNodeInContext(ctx
->context
, args
, 1);
607 static int get_llvm_num_components(LLVMValueRef value
)
609 LLVMTypeRef type
= LLVMTypeOf(value
);
610 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
611 ? LLVMGetVectorSize(type
)
613 return num_components
;
616 static LLVMValueRef
llvm_extract_elem(struct nir_to_llvm_context
*ctx
,
620 int count
= get_llvm_num_components(value
);
622 assert(index
< count
);
626 return LLVMBuildExtractElement(ctx
->builder
, value
,
627 LLVMConstInt(ctx
->i32
, index
, false), "");
630 static LLVMValueRef
trim_vector(struct nir_to_llvm_context
*ctx
,
631 LLVMValueRef value
, unsigned count
)
633 unsigned num_components
= get_llvm_num_components(value
);
634 if (count
== num_components
)
637 LLVMValueRef masks
[] = {
638 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
639 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false)};
642 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
645 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
646 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
650 build_gather_values_extended(struct nir_to_llvm_context
*ctx
,
651 LLVMValueRef
*values
,
652 unsigned value_count
,
653 unsigned value_stride
,
656 LLVMBuilderRef builder
= ctx
->builder
;
661 if (value_count
== 1) {
663 return LLVMBuildLoad(builder
, values
[0], "");
667 for (i
= 0; i
< value_count
; i
++) {
668 LLVMValueRef value
= values
[i
* value_stride
];
670 value
= LLVMBuildLoad(builder
, value
, "");
673 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
674 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
675 vec
= LLVMBuildInsertElement(builder
, vec
, value
, index
, "");
682 build_store_values_extended(struct nir_to_llvm_context
*ctx
,
683 LLVMValueRef
*values
,
684 unsigned value_count
,
685 unsigned value_stride
,
688 LLVMBuilderRef builder
= ctx
->builder
;
691 if (value_count
== 1) {
692 LLVMBuildStore(builder
, vec
, values
[0]);
696 for (i
= 0; i
< value_count
; i
++) {
697 LLVMValueRef ptr
= values
[i
* value_stride
];
698 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
699 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
700 LLVMBuildStore(builder
, value
, ptr
);
705 build_gather_values(struct nir_to_llvm_context
*ctx
,
706 LLVMValueRef
*values
,
707 unsigned value_count
)
709 return build_gather_values_extended(ctx
, values
, value_count
, 1, false);
712 static LLVMTypeRef
get_def_type(struct nir_to_llvm_context
*ctx
,
715 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->context
, def
->bit_size
);
716 if (def
->num_components
> 1) {
717 type
= LLVMVectorType(type
, def
->num_components
);
722 static LLVMValueRef
get_src(struct nir_to_llvm_context
*ctx
, nir_src src
)
725 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->defs
, src
.ssa
);
726 return (LLVMValueRef
)entry
->data
;
730 static LLVMBasicBlockRef
get_block(struct nir_to_llvm_context
*ctx
,
733 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->defs
, b
);
734 return (LLVMBasicBlockRef
)entry
->data
;
737 static LLVMValueRef
get_alu_src(struct nir_to_llvm_context
*ctx
,
739 unsigned num_components
)
741 LLVMValueRef value
= get_src(ctx
, src
.src
);
742 bool need_swizzle
= false;
745 LLVMTypeRef type
= LLVMTypeOf(value
);
746 unsigned src_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
747 ? LLVMGetVectorSize(type
)
750 for (unsigned i
= 0; i
< num_components
; ++i
) {
751 assert(src
.swizzle
[i
] < src_components
);
752 if (src
.swizzle
[i
] != i
)
756 if (need_swizzle
|| num_components
!= src_components
) {
757 LLVMValueRef masks
[] = {
758 LLVMConstInt(ctx
->i32
, src
.swizzle
[0], false),
759 LLVMConstInt(ctx
->i32
, src
.swizzle
[1], false),
760 LLVMConstInt(ctx
->i32
, src
.swizzle
[2], false),
761 LLVMConstInt(ctx
->i32
, src
.swizzle
[3], false)};
763 if (src_components
> 1 && num_components
== 1) {
764 value
= LLVMBuildExtractElement(ctx
->builder
, value
,
766 } else if (src_components
== 1 && num_components
> 1) {
767 LLVMValueRef values
[] = {value
, value
, value
, value
};
768 value
= build_gather_values(ctx
, values
, num_components
);
770 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
771 value
= LLVMBuildShuffleVector(ctx
->builder
, value
, value
,
780 static LLVMValueRef
emit_int_cmp(struct nir_to_llvm_context
*ctx
,
781 LLVMIntPredicate pred
, LLVMValueRef src0
,
784 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
785 return LLVMBuildSelect(ctx
->builder
, result
,
786 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
787 LLVMConstInt(ctx
->i32
, 0, false), "");
790 static LLVMValueRef
emit_float_cmp(struct nir_to_llvm_context
*ctx
,
791 LLVMRealPredicate pred
, LLVMValueRef src0
,
795 src0
= to_float(ctx
, src0
);
796 src1
= to_float(ctx
, src1
);
797 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
798 return LLVMBuildSelect(ctx
->builder
, result
,
799 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
800 LLVMConstInt(ctx
->i32
, 0, false), "");
803 static LLVMValueRef
emit_intrin_1f_param(struct nir_to_llvm_context
*ctx
,
807 LLVMValueRef params
[] = {
810 return emit_llvm_intrinsic(ctx
, intrin
, ctx
->f32
, params
, 1, AC_FUNC_ATTR_READNONE
);
813 static LLVMValueRef
emit_intrin_2f_param(struct nir_to_llvm_context
*ctx
,
815 LLVMValueRef src0
, LLVMValueRef src1
)
817 LLVMValueRef params
[] = {
821 return emit_llvm_intrinsic(ctx
, intrin
, ctx
->f32
, params
, 2, AC_FUNC_ATTR_READNONE
);
824 static LLVMValueRef
emit_intrin_3f_param(struct nir_to_llvm_context
*ctx
,
826 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
828 LLVMValueRef params
[] = {
833 return emit_llvm_intrinsic(ctx
, intrin
, ctx
->f32
, params
, 3, AC_FUNC_ATTR_READNONE
);
836 static LLVMValueRef
emit_bcsel(struct nir_to_llvm_context
*ctx
,
837 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
839 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
841 return LLVMBuildSelect(ctx
->builder
, v
, src1
, src2
, "");
844 static LLVMValueRef
emit_find_lsb(struct nir_to_llvm_context
*ctx
,
847 LLVMValueRef params
[2] = {
850 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
851 * add special code to check for x=0. The reason is that
852 * the LLVM behavior for x=0 is different from what we
855 * The hardware already implements the correct behavior.
857 LLVMConstInt(ctx
->i32
, 1, false),
859 return emit_llvm_intrinsic(ctx
, "llvm.cttz.i32", ctx
->i32
, params
, 2, AC_FUNC_ATTR_READNONE
);
862 static LLVMValueRef
emit_ifind_msb(struct nir_to_llvm_context
*ctx
,
865 LLVMValueRef msb
= emit_llvm_intrinsic(ctx
, "llvm.AMDGPU.flbit.i32",
867 AC_FUNC_ATTR_READNONE
);
869 /* The HW returns the last bit index from MSB, but NIR wants
870 * the index from LSB. Invert it by doing "31 - msb". */
871 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
874 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
875 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
876 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
877 src0
, ctx
->i32zero
, ""),
878 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
879 src0
, all_ones
, ""), "");
881 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
884 static LLVMValueRef
emit_ufind_msb(struct nir_to_llvm_context
*ctx
,
887 LLVMValueRef args
[2] = {
891 LLVMValueRef msb
= emit_llvm_intrinsic(ctx
, "llvm.ctlz.i32",
892 ctx
->i32
, args
, ARRAY_SIZE(args
),
893 AC_FUNC_ATTR_READNONE
);
895 /* The HW returns the last bit index from MSB, but NIR wants
896 * the index from LSB. Invert it by doing "31 - msb". */
897 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
900 return LLVMBuildSelect(ctx
->builder
,
901 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src0
,
903 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
906 static LLVMValueRef
emit_minmax_int(struct nir_to_llvm_context
*ctx
,
907 LLVMIntPredicate pred
,
908 LLVMValueRef src0
, LLVMValueRef src1
)
910 return LLVMBuildSelect(ctx
->builder
,
911 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
916 static LLVMValueRef
emit_iabs(struct nir_to_llvm_context
*ctx
,
919 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
920 LLVMBuildNeg(ctx
->builder
, src0
, ""));
923 static LLVMValueRef
emit_fsign(struct nir_to_llvm_context
*ctx
,
926 LLVMValueRef cmp
, val
;
928 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, ctx
->f32zero
, "");
929 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->f32one
, src0
, "");
930 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, ctx
->f32zero
, "");
931 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(ctx
->f32
, -1.0), "");
935 static LLVMValueRef
emit_isign(struct nir_to_llvm_context
*ctx
,
938 LLVMValueRef cmp
, val
;
940 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, ctx
->i32zero
, "");
941 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->i32one
, src0
, "");
942 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, ctx
->i32zero
, "");
943 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(ctx
->i32
, -1, true), "");
947 static LLVMValueRef
emit_ffract(struct nir_to_llvm_context
*ctx
,
950 const char *intr
= "llvm.floor.f32";
951 LLVMValueRef fsrc0
= to_float(ctx
, src0
);
952 LLVMValueRef params
[] = {
955 LLVMValueRef floor
= emit_llvm_intrinsic(ctx
, intr
,
957 AC_FUNC_ATTR_READNONE
);
958 return LLVMBuildFSub(ctx
->builder
, fsrc0
, floor
, "");
961 static LLVMValueRef
emit_uint_carry(struct nir_to_llvm_context
*ctx
,
963 LLVMValueRef src0
, LLVMValueRef src1
)
965 LLVMTypeRef ret_type
;
966 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
968 LLVMValueRef params
[] = { src0
, src1
};
969 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
972 res
= emit_llvm_intrinsic(ctx
, intrin
, ret_type
,
973 params
, 2, AC_FUNC_ATTR_READNONE
);
975 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
976 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
980 static LLVMValueRef
emit_b2f(struct nir_to_llvm_context
*ctx
,
983 return LLVMBuildAnd(ctx
->builder
, src0
, LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""), "");
986 static LLVMValueRef
emit_umul_high(struct nir_to_llvm_context
*ctx
,
987 LLVMValueRef src0
, LLVMValueRef src1
)
989 LLVMValueRef dst64
, result
;
990 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
991 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
993 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
994 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
995 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
999 static LLVMValueRef
emit_imul_high(struct nir_to_llvm_context
*ctx
,
1000 LLVMValueRef src0
, LLVMValueRef src1
)
1002 LLVMValueRef dst64
, result
;
1003 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
1004 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
1006 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
1007 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
1008 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
1012 static LLVMValueRef
emit_bitfield_extract(struct nir_to_llvm_context
*ctx
,
1014 LLVMValueRef srcs
[3])
1016 LLVMValueRef result
;
1017 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
1018 result
= emit_llvm_intrinsic(ctx
, intrin
, ctx
->i32
, srcs
, 3, AC_FUNC_ATTR_READNONE
);
1020 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
1024 static LLVMValueRef
emit_bitfield_insert(struct nir_to_llvm_context
*ctx
,
1025 LLVMValueRef src0
, LLVMValueRef src1
,
1026 LLVMValueRef src2
, LLVMValueRef src3
)
1028 LLVMValueRef bfi_args
[3], result
;
1030 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
1031 LLVMBuildSub(ctx
->builder
,
1032 LLVMBuildShl(ctx
->builder
,
1037 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
1040 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
1043 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
1044 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
1046 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
1047 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
1048 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
1050 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
1054 static LLVMValueRef
emit_pack_half_2x16(struct nir_to_llvm_context
*ctx
,
1057 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1059 LLVMValueRef comp
[2];
1061 src0
= to_float(ctx
, src0
);
1062 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32zero
, "");
1063 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32one
, "");
1064 for (i
= 0; i
< 2; i
++) {
1065 comp
[i
] = LLVMBuildFPTrunc(ctx
->builder
, comp
[i
], ctx
->f16
, "");
1066 comp
[i
] = LLVMBuildBitCast(ctx
->builder
, comp
[i
], ctx
->i16
, "");
1067 comp
[i
] = LLVMBuildZExt(ctx
->builder
, comp
[i
], ctx
->i32
, "");
1070 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
1071 comp
[0] = LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
1076 static LLVMValueRef
emit_unpack_half_2x16(struct nir_to_llvm_context
*ctx
,
1079 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1080 LLVMValueRef temps
[2], result
, val
;
1083 for (i
= 0; i
< 2; i
++) {
1084 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
1085 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
1086 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
1087 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
1090 result
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), temps
[0],
1092 result
= LLVMBuildInsertElement(ctx
->builder
, result
, temps
[1],
1098 * Set range metadata on an instruction. This can only be used on load and
1099 * call instructions. If you know an instruction can only produce the values
1100 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
1101 * \p lo is the minimum value inclusive.
1102 * \p hi is the maximum value exclusive.
1104 static void set_range_metadata(struct nir_to_llvm_context
*ctx
,
1105 LLVMValueRef value
, unsigned lo
, unsigned hi
)
1107 LLVMValueRef range_md
, md_args
[2];
1108 LLVMTypeRef type
= LLVMTypeOf(value
);
1109 LLVMContextRef context
= LLVMGetTypeContext(type
);
1111 md_args
[0] = LLVMConstInt(type
, lo
, false);
1112 md_args
[1] = LLVMConstInt(type
, hi
, false);
1113 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
1114 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
1117 static LLVMValueRef
get_thread_id(struct nir_to_llvm_context
*ctx
)
1120 LLVMValueRef tid_args
[2];
1121 tid_args
[0] = LLVMConstInt(ctx
->i32
, 0xffffffff, false);
1122 tid_args
[1] = ctx
->i32zero
;
1123 tid_args
[1] = emit_llvm_intrinsic(ctx
,
1124 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
1125 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
1127 tid
= emit_llvm_intrinsic(ctx
,
1128 "llvm.amdgcn.mbcnt.hi", ctx
->i32
,
1129 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
1130 set_range_metadata(ctx
, tid
, 0, 64);
1135 * SI implements derivatives using the local data store (LDS)
1136 * All writes to the LDS happen in all executing threads at
1137 * the same time. TID is the Thread ID for the current
1138 * thread and is a value between 0 and 63, representing
1139 * the thread's position in the wavefront.
1141 * For the pixel shader threads are grouped into quads of four pixels.
1142 * The TIDs of the pixels of a quad are:
1150 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
1151 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
1152 * the current pixel's column, and masking with 0xfffffffe yields the TID
1153 * of the left pixel of the current pixel's row.
1155 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
1156 * adding 2 yields the TID of the pixel below the top pixel.
1158 /* masks for thread ID. */
1159 #define TID_MASK_TOP_LEFT 0xfffffffc
1160 #define TID_MASK_TOP 0xfffffffd
1161 #define TID_MASK_LEFT 0xfffffffe
1162 static LLVMValueRef
emit_ddxy(struct nir_to_llvm_context
*ctx
,
1166 LLVMValueRef tl
, trbl
, result
;
1167 LLVMValueRef tl_tid
, trbl_tid
;
1168 LLVMValueRef args
[2];
1169 LLVMValueRef thread_id
;
1172 ctx
->has_ddxy
= true;
1174 if (!ctx
->lds
&& !ctx
->has_ds_bpermute
)
1175 ctx
->lds
= LLVMAddGlobalInAddressSpace(ctx
->module
,
1176 LLVMArrayType(ctx
->i32
, 64),
1177 "ddxy_lds", LOCAL_ADDR_SPACE
);
1179 thread_id
= get_thread_id(ctx
);
1180 if (op
== nir_op_fddx_fine
|| op
== nir_op_fddx
)
1181 mask
= TID_MASK_LEFT
;
1182 else if (op
== nir_op_fddy_fine
|| op
== nir_op_fddy
)
1183 mask
= TID_MASK_TOP
;
1185 mask
= TID_MASK_TOP_LEFT
;
1187 tl_tid
= LLVMBuildAnd(ctx
->builder
, thread_id
,
1188 LLVMConstInt(ctx
->i32
, mask
, false), "");
1189 /* for DDX we want to next X pixel, DDY next Y pixel. */
1190 if (op
== nir_op_fddx_fine
||
1191 op
== nir_op_fddx_coarse
||
1197 trbl_tid
= LLVMBuildAdd(ctx
->builder
, tl_tid
,
1198 LLVMConstInt(ctx
->i32
, idx
, false), "");
1200 if (ctx
->has_ds_bpermute
) {
1201 args
[0] = LLVMBuildMul(ctx
->builder
, tl_tid
,
1202 LLVMConstInt(ctx
->i32
, 4, false), "");
1204 tl
= emit_llvm_intrinsic(ctx
, "llvm.amdgcn.ds.bpermute",
1206 AC_FUNC_ATTR_READNONE
);
1208 args
[0] = LLVMBuildMul(ctx
->builder
, trbl_tid
,
1209 LLVMConstInt(ctx
->i32
, 4, false), "");
1210 trbl
= emit_llvm_intrinsic(ctx
, "llvm.amdgcn.ds.bpermute",
1212 AC_FUNC_ATTR_READNONE
);
1214 LLVMValueRef store_ptr
, load_ptr0
, load_ptr1
;
1216 store_ptr
= build_gep0(ctx
, ctx
->lds
, thread_id
);
1217 load_ptr0
= build_gep0(ctx
, ctx
->lds
, tl_tid
);
1218 load_ptr1
= build_gep0(ctx
, ctx
->lds
, trbl_tid
);
1220 LLVMBuildStore(ctx
->builder
, src0
, store_ptr
);
1221 tl
= LLVMBuildLoad(ctx
->builder
, load_ptr0
, "");
1222 trbl
= LLVMBuildLoad(ctx
->builder
, load_ptr1
, "");
1224 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, ctx
->f32
, "");
1225 trbl
= LLVMBuildBitCast(ctx
->builder
, trbl
, ctx
->f32
, "");
1226 result
= LLVMBuildFSub(ctx
->builder
, trbl
, tl
, "");
1231 * this takes an I,J coordinate pair,
1232 * and works out the X and Y derivatives.
1233 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
1235 static LLVMValueRef
emit_ddxy_interp(
1236 struct nir_to_llvm_context
*ctx
,
1237 LLVMValueRef interp_ij
)
1239 LLVMValueRef result
[4], a
;
1242 for (i
= 0; i
< 2; i
++) {
1243 a
= LLVMBuildExtractElement(ctx
->builder
, interp_ij
,
1244 LLVMConstInt(ctx
->i32
, i
, false), "");
1245 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
1246 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
1248 return build_gather_values(ctx
, result
, 4);
1251 static LLVMValueRef
emit_fdiv(struct nir_to_llvm_context
*ctx
,
1255 LLVMValueRef ret
= LLVMBuildFDiv(ctx
->builder
, num
, den
, "");
1257 if (!LLVMIsConstant(ret
))
1258 LLVMSetMetadata(ret
, ctx
->fpmath_md_kind
, ctx
->fpmath_md_2p5_ulp
);
1262 static void visit_alu(struct nir_to_llvm_context
*ctx
, nir_alu_instr
*instr
)
1264 LLVMValueRef src
[4], result
= NULL
;
1265 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
1266 unsigned src_components
;
1268 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
1269 switch (instr
->op
) {
1275 case nir_op_pack_half_2x16
:
1278 case nir_op_unpack_half_2x16
:
1282 src_components
= num_components
;
1285 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1286 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
1288 switch (instr
->op
) {
1294 src
[0] = to_float(ctx
, src
[0]);
1295 result
= LLVMBuildFNeg(ctx
->builder
, src
[0], "");
1298 result
= LLVMBuildNeg(ctx
->builder
, src
[0], "");
1301 result
= LLVMBuildNot(ctx
->builder
, src
[0], "");
1304 result
= LLVMBuildAdd(ctx
->builder
, src
[0], src
[1], "");
1307 src
[0] = to_float(ctx
, src
[0]);
1308 src
[1] = to_float(ctx
, src
[1]);
1309 result
= LLVMBuildFAdd(ctx
->builder
, src
[0], src
[1], "");
1312 src
[0] = to_float(ctx
, src
[0]);
1313 src
[1] = to_float(ctx
, src
[1]);
1314 result
= LLVMBuildFSub(ctx
->builder
, src
[0], src
[1], "");
1317 result
= LLVMBuildSub(ctx
->builder
, src
[0], src
[1], "");
1320 result
= LLVMBuildMul(ctx
->builder
, src
[0], src
[1], "");
1323 result
= LLVMBuildSRem(ctx
->builder
, src
[0], src
[1], "");
1326 result
= LLVMBuildURem(ctx
->builder
, src
[0], src
[1], "");
1329 src
[0] = to_float(ctx
, src
[0]);
1330 src
[1] = to_float(ctx
, src
[1]);
1331 result
= emit_fdiv(ctx
, src
[0], src
[1]);
1332 result
= emit_intrin_1f_param(ctx
, "llvm.floor.f32", result
);
1333 result
= LLVMBuildFMul(ctx
->builder
, src
[1] , result
, "");
1334 result
= LLVMBuildFSub(ctx
->builder
, src
[0], result
, "");
1337 src
[0] = to_float(ctx
, src
[0]);
1338 src
[1] = to_float(ctx
, src
[1]);
1339 result
= LLVMBuildFRem(ctx
->builder
, src
[0], src
[1], "");
1342 result
= LLVMBuildSDiv(ctx
->builder
, src
[0], src
[1], "");
1345 result
= LLVMBuildUDiv(ctx
->builder
, src
[0], src
[1], "");
1348 src
[0] = to_float(ctx
, src
[0]);
1349 src
[1] = to_float(ctx
, src
[1]);
1350 result
= LLVMBuildFMul(ctx
->builder
, src
[0], src
[1], "");
1353 src
[0] = to_float(ctx
, src
[0]);
1354 src
[1] = to_float(ctx
, src
[1]);
1355 result
= emit_fdiv(ctx
, src
[0], src
[1]);
1358 src
[0] = to_float(ctx
, src
[0]);
1359 result
= emit_fdiv(ctx
, ctx
->f32one
, src
[0]);
1362 result
= LLVMBuildAnd(ctx
->builder
, src
[0], src
[1], "");
1365 result
= LLVMBuildOr(ctx
->builder
, src
[0], src
[1], "");
1368 result
= LLVMBuildXor(ctx
->builder
, src
[0], src
[1], "");
1371 result
= LLVMBuildShl(ctx
->builder
, src
[0], src
[1], "");
1374 result
= LLVMBuildAShr(ctx
->builder
, src
[0], src
[1], "");
1377 result
= LLVMBuildLShr(ctx
->builder
, src
[0], src
[1], "");
1380 result
= emit_int_cmp(ctx
, LLVMIntSLT
, src
[0], src
[1]);
1383 result
= emit_int_cmp(ctx
, LLVMIntNE
, src
[0], src
[1]);
1386 result
= emit_int_cmp(ctx
, LLVMIntEQ
, src
[0], src
[1]);
1389 result
= emit_int_cmp(ctx
, LLVMIntSGE
, src
[0], src
[1]);
1392 result
= emit_int_cmp(ctx
, LLVMIntULT
, src
[0], src
[1]);
1395 result
= emit_int_cmp(ctx
, LLVMIntUGE
, src
[0], src
[1]);
1398 result
= emit_float_cmp(ctx
, LLVMRealUEQ
, src
[0], src
[1]);
1401 result
= emit_float_cmp(ctx
, LLVMRealUNE
, src
[0], src
[1]);
1404 result
= emit_float_cmp(ctx
, LLVMRealULT
, src
[0], src
[1]);
1407 result
= emit_float_cmp(ctx
, LLVMRealUGE
, src
[0], src
[1]);
1410 result
= emit_intrin_1f_param(ctx
, "llvm.fabs.f32", src
[0]);
1413 result
= emit_iabs(ctx
, src
[0]);
1416 result
= emit_minmax_int(ctx
, LLVMIntSGT
, src
[0], src
[1]);
1419 result
= emit_minmax_int(ctx
, LLVMIntSLT
, src
[0], src
[1]);
1422 result
= emit_minmax_int(ctx
, LLVMIntUGT
, src
[0], src
[1]);
1425 result
= emit_minmax_int(ctx
, LLVMIntULT
, src
[0], src
[1]);
1428 result
= emit_isign(ctx
, src
[0]);
1431 src
[0] = to_float(ctx
, src
[0]);
1432 result
= emit_fsign(ctx
, src
[0]);
1435 result
= emit_intrin_1f_param(ctx
, "llvm.floor.f32", src
[0]);
1438 result
= emit_intrin_1f_param(ctx
, "llvm.trunc.f32", src
[0]);
1441 result
= emit_intrin_1f_param(ctx
, "llvm.ceil.f32", src
[0]);
1443 case nir_op_fround_even
:
1444 result
= emit_intrin_1f_param(ctx
, "llvm.rint.f32", src
[0]);
1447 result
= emit_ffract(ctx
, src
[0]);
1450 result
= emit_intrin_1f_param(ctx
, "llvm.sin.f32", src
[0]);
1453 result
= emit_intrin_1f_param(ctx
, "llvm.cos.f32", src
[0]);
1456 result
= emit_intrin_1f_param(ctx
, "llvm.sqrt.f32", src
[0]);
1459 result
= emit_intrin_1f_param(ctx
, "llvm.exp2.f32", src
[0]);
1462 result
= emit_intrin_1f_param(ctx
, "llvm.log2.f32", src
[0]);
1465 result
= emit_intrin_1f_param(ctx
, "llvm.sqrt.f32", src
[0]);
1466 result
= emit_fdiv(ctx
, ctx
->f32one
, result
);
1469 result
= emit_intrin_2f_param(ctx
, "llvm.pow.f32", src
[0], src
[1]);
1472 result
= emit_intrin_2f_param(ctx
, "llvm.maxnum.f32", src
[0], src
[1]);
1475 result
= emit_intrin_2f_param(ctx
, "llvm.minnum.f32", src
[0], src
[1]);
1478 result
= emit_intrin_3f_param(ctx
, "llvm.fma.f32", src
[0], src
[1], src
[2]);
1480 case nir_op_ibitfield_extract
:
1481 result
= emit_bitfield_extract(ctx
, "llvm.AMDGPU.bfe.i32", src
);
1483 case nir_op_ubitfield_extract
:
1484 result
= emit_bitfield_extract(ctx
, "llvm.AMDGPU.bfe.u32", src
);
1486 case nir_op_bitfield_insert
:
1487 result
= emit_bitfield_insert(ctx
, src
[0], src
[1], src
[2], src
[3]);
1489 case nir_op_bitfield_reverse
:
1490 result
= emit_llvm_intrinsic(ctx
, "llvm.bitreverse.i32", ctx
->i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1492 case nir_op_bit_count
:
1493 result
= emit_llvm_intrinsic(ctx
, "llvm.ctpop.i32", ctx
->i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1498 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1499 src
[i
] = to_integer(ctx
, src
[i
]);
1500 result
= build_gather_values(ctx
, src
, num_components
);
1503 src
[0] = to_float(ctx
, src
[0]);
1504 result
= LLVMBuildFPToSI(ctx
->builder
, src
[0], ctx
->i32
, "");
1507 src
[0] = to_float(ctx
, src
[0]);
1508 result
= LLVMBuildFPToUI(ctx
->builder
, src
[0], ctx
->i32
, "");
1511 result
= LLVMBuildSIToFP(ctx
->builder
, src
[0], ctx
->f32
, "");
1514 result
= LLVMBuildUIToFP(ctx
->builder
, src
[0], ctx
->f32
, "");
1517 result
= emit_bcsel(ctx
, src
[0], src
[1], src
[2]);
1519 case nir_op_find_lsb
:
1520 result
= emit_find_lsb(ctx
, src
[0]);
1522 case nir_op_ufind_msb
:
1523 result
= emit_ufind_msb(ctx
, src
[0]);
1525 case nir_op_ifind_msb
:
1526 result
= emit_ifind_msb(ctx
, src
[0]);
1528 case nir_op_uadd_carry
:
1529 result
= emit_uint_carry(ctx
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
1531 case nir_op_usub_borrow
:
1532 result
= emit_uint_carry(ctx
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
1535 result
= emit_b2f(ctx
, src
[0]);
1537 case nir_op_fquantize2f16
:
1538 src
[0] = to_float(ctx
, src
[0]);
1539 result
= LLVMBuildFPTrunc(ctx
->builder
, src
[0], ctx
->f16
, "");
1540 /* need to convert back up to f32 */
1541 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
1543 case nir_op_umul_high
:
1544 result
= emit_umul_high(ctx
, src
[0], src
[1]);
1546 case nir_op_imul_high
:
1547 result
= emit_imul_high(ctx
, src
[0], src
[1]);
1549 case nir_op_pack_half_2x16
:
1550 result
= emit_pack_half_2x16(ctx
, src
[0]);
1552 case nir_op_unpack_half_2x16
:
1553 result
= emit_unpack_half_2x16(ctx
, src
[0]);
1557 case nir_op_fddx_fine
:
1558 case nir_op_fddy_fine
:
1559 case nir_op_fddx_coarse
:
1560 case nir_op_fddy_coarse
:
1561 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
1564 fprintf(stderr
, "Unknown NIR alu instr: ");
1565 nir_print_instr(&instr
->instr
, stderr
);
1566 fprintf(stderr
, "\n");
1571 assert(instr
->dest
.dest
.is_ssa
);
1572 result
= to_integer(ctx
, result
);
1573 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.dest
.ssa
,
1578 static void visit_load_const(struct nir_to_llvm_context
*ctx
,
1579 nir_load_const_instr
*instr
)
1581 LLVMValueRef values
[4], value
= NULL
;
1582 LLVMTypeRef element_type
=
1583 LLVMIntTypeInContext(ctx
->context
, instr
->def
.bit_size
);
1585 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1586 switch (instr
->def
.bit_size
) {
1588 values
[i
] = LLVMConstInt(element_type
,
1589 instr
->value
.u32
[i
], false);
1592 values
[i
] = LLVMConstInt(element_type
,
1593 instr
->value
.u64
[i
], false);
1597 "unsupported nir load_const bit_size: %d\n",
1598 instr
->def
.bit_size
);
1602 if (instr
->def
.num_components
> 1) {
1603 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1607 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, value
);
1610 static LLVMValueRef
cast_ptr(struct nir_to_llvm_context
*ctx
, LLVMValueRef ptr
,
1613 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
1614 return LLVMBuildBitCast(ctx
->builder
, ptr
,
1615 LLVMPointerType(type
, addr_space
), "");
1619 emit_llvm_intrinsic(struct nir_to_llvm_context
*ctx
, const char *name
,
1620 LLVMTypeRef return_type
, LLVMValueRef
*params
,
1621 unsigned param_count
, unsigned attrib_mask
)
1623 LLVMValueRef function
;
1625 function
= LLVMGetNamedFunction(ctx
->module
, name
);
1627 LLVMTypeRef param_types
[32], function_type
;
1630 assert(param_count
<= 32);
1632 for (i
= 0; i
< param_count
; ++i
) {
1634 param_types
[i
] = LLVMTypeOf(params
[i
]);
1637 LLVMFunctionType(return_type
, param_types
, param_count
, 0);
1638 function
= LLVMAddFunction(ctx
->module
, name
, function_type
);
1640 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
1641 LLVMSetLinkage(function
, LLVMExternalLinkage
);
1643 attrib_mask
|= AC_FUNC_ATTR_NOUNWIND
;
1644 while (attrib_mask
) {
1645 enum ac_func_attr attr
= 1u << u_bit_scan(&attrib_mask
);
1646 ac_add_function_attr(function
, -1, attr
);
1649 return LLVMBuildCall(ctx
->builder
, function
, params
, param_count
, "");
1653 get_buffer_size(struct nir_to_llvm_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1656 LLVMBuildExtractElement(ctx
->builder
, descriptor
,
1657 LLVMConstInt(ctx
->i32
, 2, false), "");
1660 if (ctx
->options
->chip_class
>= VI
&& in_elements
) {
1661 /* On VI, the descriptor contains the size in bytes,
1662 * but TXQ must return the size in elements.
1663 * The stride is always non-zero for resources using TXQ.
1665 LLVMValueRef stride
=
1666 LLVMBuildExtractElement(ctx
->builder
, descriptor
,
1667 LLVMConstInt(ctx
->i32
, 1, false), "");
1668 stride
= LLVMBuildLShr(ctx
->builder
, stride
,
1669 LLVMConstInt(ctx
->i32
, 16, false), "");
1670 stride
= LLVMBuildAnd(ctx
->builder
, stride
,
1671 LLVMConstInt(ctx
->i32
, 0x3fff, false), "");
1673 size
= LLVMBuildUDiv(ctx
->builder
, size
, stride
, "");
1679 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
1682 static void build_int_type_name(
1684 char *buf
, unsigned bufsize
)
1686 assert(bufsize
>= 6);
1688 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
1689 snprintf(buf
, bufsize
, "v%ui32",
1690 LLVMGetVectorSize(type
));
1695 static LLVMValueRef
radv_lower_gather4_integer(struct nir_to_llvm_context
*ctx
,
1696 struct ac_tex_info
*tinfo
,
1697 nir_tex_instr
*instr
,
1698 const char *intr_name
,
1699 unsigned coord_vgpr_index
)
1701 LLVMValueRef coord
= tinfo
->args
[0];
1702 LLVMValueRef half_texel
[2];
1707 LLVMValueRef txq_args
[10];
1708 int txq_arg_count
= 0;
1710 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
1711 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, false);
1712 txq_args
[txq_arg_count
++] = tinfo
->args
[1];
1713 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0xf, 0); /* dmask */
1714 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* unorm */
1715 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* r128 */
1716 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, da
? 1 : 0, 0);
1717 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* glc */
1718 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* slc */
1719 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* tfe */
1720 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* lwe */
1721 size
= emit_llvm_intrinsic(ctx
, "llvm.SI.getresinfo.i32", ctx
->v4i32
,
1722 txq_args
, txq_arg_count
,
1723 AC_FUNC_ATTR_READNONE
);
1725 for (c
= 0; c
< 2; c
++) {
1726 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
1727 LLVMConstInt(ctx
->i32
, c
, false), "");
1728 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1729 half_texel
[c
] = emit_fdiv(ctx
, ctx
->f32one
, half_texel
[c
]);
1730 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1731 LLVMConstReal(ctx
->f32
, -0.5), "");
1735 for (c
= 0; c
< 2; c
++) {
1737 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, coord_vgpr_index
+ c
, 0);
1738 tmp
= LLVMBuildExtractElement(ctx
->builder
, coord
, index
, "");
1739 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1740 tmp
= LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
1741 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1742 coord
= LLVMBuildInsertElement(ctx
->builder
, coord
, tmp
, index
, "");
1745 tinfo
->args
[0] = coord
;
1746 return emit_llvm_intrinsic(ctx
, intr_name
, tinfo
->dst_type
, tinfo
->args
, tinfo
->arg_count
,
1747 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_NOUNWIND
);
1751 static LLVMValueRef
build_tex_intrinsic(struct nir_to_llvm_context
*ctx
,
1752 nir_tex_instr
*instr
,
1753 struct ac_tex_info
*tinfo
)
1755 const char *name
= "llvm.SI.image.sample";
1756 const char *infix
= "";
1757 char intr_name
[127];
1759 bool is_shadow
= instr
->is_shadow
;
1760 bool has_offset
= tinfo
->has_offset
;
1761 switch (instr
->op
) {
1763 case nir_texop_txf_ms
:
1764 case nir_texop_samples_identical
:
1765 name
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
? "llvm.SI.image.load" :
1766 instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
? "llvm.SI.vs.load.input" :
1767 "llvm.SI.image.load.mip";
1778 name
= "llvm.SI.getresinfo";
1780 case nir_texop_query_levels
:
1781 name
= "llvm.SI.getresinfo";
1784 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
1791 name
= "llvm.SI.gather4";
1795 name
= "llvm.SI.getlod";
1803 build_int_type_name(LLVMTypeOf(tinfo
->args
[0]), type
, sizeof(type
));
1804 sprintf(intr_name
, "%s%s%s%s.%s", name
, is_shadow
? ".c" : "", infix
,
1805 has_offset
? ".o" : "", type
);
1807 if (instr
->op
== nir_texop_tg4
) {
1808 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
1809 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
1810 return radv_lower_gather4_integer(ctx
, tinfo
, instr
, intr_name
,
1811 (int)has_offset
+ (int)is_shadow
);
1814 return emit_llvm_intrinsic(ctx
, intr_name
, tinfo
->dst_type
, tinfo
->args
, tinfo
->arg_count
,
1815 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_NOUNWIND
);
1819 static LLVMValueRef
visit_vulkan_resource_index(struct nir_to_llvm_context
*ctx
,
1820 nir_intrinsic_instr
*instr
)
1822 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
1823 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
1824 unsigned binding
= nir_intrinsic_binding(instr
);
1825 LLVMValueRef desc_ptr
= ctx
->descriptor_sets
[desc_set
];
1826 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[desc_set
].layout
;
1827 unsigned base_offset
= layout
->binding
[binding
].offset
;
1828 LLVMValueRef offset
, stride
;
1830 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
1831 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
1832 desc_ptr
= ctx
->push_constants
;
1833 base_offset
= ctx
->options
->layout
->push_constant_size
;
1834 base_offset
+= 16 * layout
->binding
[binding
].dynamic_offset_offset
;
1835 stride
= LLVMConstInt(ctx
->i32
, 16, false);
1837 stride
= LLVMConstInt(ctx
->i32
, layout
->binding
[binding
].size
, false);
1839 offset
= LLVMConstInt(ctx
->i32
, base_offset
, false);
1840 index
= LLVMBuildMul(ctx
->builder
, index
, stride
, "");
1841 offset
= LLVMBuildAdd(ctx
->builder
, offset
, index
, "");
1843 LLVMValueRef indices
[] = {ctx
->i32zero
, offset
};
1844 desc_ptr
= LLVMBuildGEP(ctx
->builder
, desc_ptr
, indices
, 2, "");
1845 desc_ptr
= cast_ptr(ctx
, desc_ptr
, ctx
->v4i32
);
1846 LLVMSetMetadata(desc_ptr
, ctx
->uniform_md_kind
, ctx
->empty_md
);
1848 return LLVMBuildLoad(ctx
->builder
, desc_ptr
, "");
1851 static LLVMValueRef
visit_load_push_constant(struct nir_to_llvm_context
*ctx
,
1852 nir_intrinsic_instr
*instr
)
1856 LLVMValueRef indices
[] = {ctx
->i32zero
, get_src(ctx
, instr
->src
[0])};
1857 ptr
= LLVMBuildGEP(ctx
->builder
, ctx
->push_constants
, indices
, 2, "");
1858 ptr
= cast_ptr(ctx
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
1860 return LLVMBuildLoad(ctx
->builder
, ptr
, "");
1863 static LLVMValueRef
visit_get_buffer_size(struct nir_to_llvm_context
*ctx
,
1864 nir_intrinsic_instr
*instr
)
1866 LLVMValueRef desc
= get_src(ctx
, instr
->src
[0]);
1868 return get_buffer_size(ctx
, desc
, false);
1870 static void visit_store_ssbo(struct nir_to_llvm_context
*ctx
,
1871 nir_intrinsic_instr
*instr
)
1873 const char *store_name
;
1874 LLVMTypeRef data_type
= ctx
->f32
;
1875 unsigned writemask
= nir_intrinsic_write_mask(instr
);
1876 LLVMValueRef base_data
, base_offset
;
1877 LLVMValueRef params
[6];
1879 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
1880 ctx
->shader_info
->fs
.writes_memory
= true;
1882 params
[1] = get_src(ctx
, instr
->src
[1]);
1883 params
[2] = LLVMConstInt(ctx
->i32
, 0, false); /* vindex */
1884 params
[4] = LLVMConstInt(ctx
->i1
, 0, false); /* glc */
1885 params
[5] = LLVMConstInt(ctx
->i1
, 0, false); /* slc */
1887 if (instr
->num_components
> 1)
1888 data_type
= LLVMVectorType(ctx
->f32
, instr
->num_components
);
1890 base_data
= to_float(ctx
, get_src(ctx
, instr
->src
[0]));
1891 base_data
= trim_vector(ctx
, base_data
, instr
->num_components
);
1892 base_data
= LLVMBuildBitCast(ctx
->builder
, base_data
,
1894 base_offset
= get_src(ctx
, instr
->src
[2]); /* voffset */
1898 LLVMValueRef offset
;
1900 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
1902 /* Due to an LLVM limitation, split 3-element writes
1903 * into a 2-element and a 1-element write. */
1905 writemask
|= 1 << (start
+ 2);
1910 store_name
= "llvm.amdgcn.buffer.store.v4f32";
1912 } else if (count
== 2) {
1913 tmp
= LLVMBuildExtractElement(ctx
->builder
,
1914 base_data
, LLVMConstInt(ctx
->i32
, start
, false), "");
1915 data
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), tmp
,
1918 tmp
= LLVMBuildExtractElement(ctx
->builder
,
1919 base_data
, LLVMConstInt(ctx
->i32
, start
+ 1, false), "");
1920 data
= LLVMBuildInsertElement(ctx
->builder
, data
, tmp
,
1922 store_name
= "llvm.amdgcn.buffer.store.v2f32";
1926 if (get_llvm_num_components(base_data
) > 1)
1927 data
= LLVMBuildExtractElement(ctx
->builder
, base_data
,
1928 LLVMConstInt(ctx
->i32
, start
, false), "");
1931 store_name
= "llvm.amdgcn.buffer.store.f32";
1934 offset
= base_offset
;
1936 offset
= LLVMBuildAdd(ctx
->builder
, offset
, LLVMConstInt(ctx
->i32
, start
* 4, false), "");
1940 emit_llvm_intrinsic(ctx
, store_name
,
1941 LLVMVoidTypeInContext(ctx
->context
), params
, 6, 0);
1945 static LLVMValueRef
visit_atomic_ssbo(struct nir_to_llvm_context
*ctx
,
1946 nir_intrinsic_instr
*instr
)
1949 LLVMValueRef params
[6];
1951 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
1952 ctx
->shader_info
->fs
.writes_memory
= true;
1954 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
1955 params
[arg_count
++] = get_src(ctx
, instr
->src
[3]);
1957 params
[arg_count
++] = get_src(ctx
, instr
->src
[2]);
1958 params
[arg_count
++] = get_src(ctx
, instr
->src
[0]);
1959 params
[arg_count
++] = LLVMConstInt(ctx
->i32
, 0, false); /* vindex */
1960 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1961 params
[arg_count
++] = LLVMConstInt(ctx
->i1
, 0, false); /* slc */
1963 switch (instr
->intrinsic
) {
1964 case nir_intrinsic_ssbo_atomic_add
:
1965 name
= "llvm.amdgcn.buffer.atomic.add";
1967 case nir_intrinsic_ssbo_atomic_imin
:
1968 name
= "llvm.amdgcn.buffer.atomic.smin";
1970 case nir_intrinsic_ssbo_atomic_umin
:
1971 name
= "llvm.amdgcn.buffer.atomic.umin";
1973 case nir_intrinsic_ssbo_atomic_imax
:
1974 name
= "llvm.amdgcn.buffer.atomic.smax";
1976 case nir_intrinsic_ssbo_atomic_umax
:
1977 name
= "llvm.amdgcn.buffer.atomic.umax";
1979 case nir_intrinsic_ssbo_atomic_and
:
1980 name
= "llvm.amdgcn.buffer.atomic.and";
1982 case nir_intrinsic_ssbo_atomic_or
:
1983 name
= "llvm.amdgcn.buffer.atomic.or";
1985 case nir_intrinsic_ssbo_atomic_xor
:
1986 name
= "llvm.amdgcn.buffer.atomic.xor";
1988 case nir_intrinsic_ssbo_atomic_exchange
:
1989 name
= "llvm.amdgcn.buffer.atomic.swap";
1991 case nir_intrinsic_ssbo_atomic_comp_swap
:
1992 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
1998 return emit_llvm_intrinsic(ctx
, name
, ctx
->i32
, params
, arg_count
, 0);
2001 static LLVMValueRef
visit_load_buffer(struct nir_to_llvm_context
*ctx
,
2002 nir_intrinsic_instr
*instr
)
2004 const char *load_name
;
2005 LLVMTypeRef data_type
= ctx
->f32
;
2006 if (instr
->num_components
== 3)
2007 data_type
= LLVMVectorType(ctx
->f32
, 4);
2008 else if (instr
->num_components
> 1)
2009 data_type
= LLVMVectorType(ctx
->f32
, instr
->num_components
);
2011 if (instr
->num_components
== 4 || instr
->num_components
== 3)
2012 load_name
= "llvm.amdgcn.buffer.load.v4f32";
2013 else if (instr
->num_components
== 2)
2014 load_name
= "llvm.amdgcn.buffer.load.v2f32";
2015 else if (instr
->num_components
== 1)
2016 load_name
= "llvm.amdgcn.buffer.load.f32";
2020 LLVMValueRef params
[] = {
2021 get_src(ctx
, instr
->src
[0]),
2022 LLVMConstInt(ctx
->i32
, 0, false),
2023 get_src(ctx
, instr
->src
[1]),
2024 LLVMConstInt(ctx
->i1
, 0, false),
2025 LLVMConstInt(ctx
->i1
, 0, false),
2029 emit_llvm_intrinsic(ctx
, load_name
, data_type
, params
, 5, 0);
2031 if (instr
->num_components
== 3)
2032 ret
= trim_vector(ctx
, ret
, 3);
2034 return LLVMBuildBitCast(ctx
->builder
, ret
,
2035 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2038 static LLVMValueRef
visit_load_ubo_buffer(struct nir_to_llvm_context
*ctx
,
2039 nir_intrinsic_instr
*instr
)
2041 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[instr
->intrinsic
];
2042 const char *load_name
;
2043 LLVMTypeRef data_type
= ctx
->f32
;
2044 LLVMValueRef results
[4], ret
;
2045 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
2046 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
2048 rsrc
= LLVMBuildBitCast(ctx
->builder
, rsrc
, LLVMVectorType(ctx
->i8
, 16), "");
2050 for (unsigned i
= 0; i
< instr
->num_components
; ++i
) {
2051 LLVMValueRef params
[] = {
2053 LLVMBuildAdd(ctx
->builder
, LLVMConstInt(ctx
->i32
, 4 * i
, 0),
2056 results
[i
] = emit_llvm_intrinsic(ctx
, "llvm.SI.load.const", ctx
->f32
,
2057 params
, 2, AC_FUNC_ATTR_READNONE
);
2061 ret
= build_gather_values(ctx
, results
, instr
->num_components
);
2062 return LLVMBuildBitCast(ctx
->builder
, ret
,
2063 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2067 radv_get_deref_offset(struct nir_to_llvm_context
*ctx
, nir_deref
*tail
,
2068 bool vs_in
, unsigned *const_out
, LLVMValueRef
*indir_out
)
2070 unsigned const_offset
= 0;
2071 LLVMValueRef offset
= NULL
;
2074 while (tail
->child
!= NULL
) {
2075 const struct glsl_type
*parent_type
= tail
->type
;
2078 if (tail
->deref_type
== nir_deref_type_array
) {
2079 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2080 LLVMValueRef index
, stride
, local_offset
;
2081 unsigned size
= glsl_count_attribute_slots(tail
->type
, vs_in
);
2083 const_offset
+= size
* deref_array
->base_offset
;
2084 if (deref_array
->deref_array_type
== nir_deref_array_type_direct
)
2087 assert(deref_array
->deref_array_type
== nir_deref_array_type_indirect
);
2088 index
= get_src(ctx
, deref_array
->indirect
);
2089 stride
= LLVMConstInt(ctx
->i32
, size
, 0);
2090 local_offset
= LLVMBuildMul(ctx
->builder
, stride
, index
, "");
2093 offset
= LLVMBuildAdd(ctx
->builder
, offset
, local_offset
, "");
2095 offset
= local_offset
;
2096 } else if (tail
->deref_type
== nir_deref_type_struct
) {
2097 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
2099 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
2100 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
2101 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
2104 unreachable("unsupported deref type");
2108 if (const_offset
&& offset
)
2109 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
2110 LLVMConstInt(ctx
->i32
, const_offset
, 0),
2113 *const_out
= const_offset
;
2114 *indir_out
= offset
;
2117 static LLVMValueRef
visit_load_var(struct nir_to_llvm_context
*ctx
,
2118 nir_intrinsic_instr
*instr
)
2120 LLVMValueRef values
[4];
2121 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2122 int ve
= instr
->dest
.ssa
.num_components
;
2123 LLVMValueRef indir_index
;
2124 unsigned const_index
;
2125 switch (instr
->variables
[0]->var
->data
.mode
) {
2126 case nir_var_shader_in
:
2127 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
,
2128 ctx
->stage
== MESA_SHADER_VERTEX
,
2129 &const_index
, &indir_index
);
2130 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2132 unsigned count
= glsl_count_attribute_slots(
2133 instr
->variables
[0]->var
->type
,
2134 ctx
->stage
== MESA_SHADER_VERTEX
);
2135 LLVMValueRef tmp_vec
= build_gather_values_extended(
2136 ctx
, ctx
->inputs
+ idx
+ chan
, count
,
2139 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2143 values
[chan
] = ctx
->inputs
[idx
+ chan
+ const_index
* 4];
2145 return to_integer(ctx
, build_gather_values(ctx
, values
, ve
));
2148 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2149 &const_index
, &indir_index
);
2150 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2152 unsigned count
= glsl_count_attribute_slots(
2153 instr
->variables
[0]->var
->type
, false);
2154 LLVMValueRef tmp_vec
= build_gather_values_extended(
2155 ctx
, ctx
->locals
+ idx
+ chan
, count
,
2158 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2162 values
[chan
] = LLVMBuildLoad(ctx
->builder
, ctx
->locals
[idx
+ chan
+ const_index
* 4], "");
2165 return to_integer(ctx
, build_gather_values(ctx
, values
, ve
));
2166 case nir_var_shader_out
:
2167 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2168 &const_index
, &indir_index
);
2169 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2171 unsigned count
= glsl_count_attribute_slots(
2172 instr
->variables
[0]->var
->type
, false);
2173 LLVMValueRef tmp_vec
= build_gather_values_extended(
2174 ctx
, ctx
->outputs
+ idx
+ chan
, count
,
2177 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2181 values
[chan
] = LLVMBuildLoad(ctx
->builder
,
2182 ctx
->outputs
[idx
+ chan
+ const_index
* 4],
2186 return to_integer(ctx
, build_gather_values(ctx
, values
, ve
));
2187 case nir_var_shared
: {
2188 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2189 &const_index
, &indir_index
);
2190 LLVMValueRef ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
2191 LLVMValueRef derived_ptr
;
2192 LLVMValueRef index
= ctx
->i32zero
;
2194 index
= LLVMBuildAdd(ctx
->builder
, index
, indir_index
, "");
2195 derived_ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, "");
2197 return to_integer(ctx
, LLVMBuildLoad(ctx
->builder
, derived_ptr
, ""));
2207 visit_store_var(struct nir_to_llvm_context
*ctx
,
2208 nir_intrinsic_instr
*instr
)
2210 LLVMValueRef temp_ptr
, value
;
2211 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2212 LLVMValueRef src
= to_float(ctx
, get_src(ctx
, instr
->src
[0]));
2213 int writemask
= instr
->const_index
[0];
2214 LLVMValueRef indir_index
;
2215 unsigned const_index
;
2216 switch (instr
->variables
[0]->var
->data
.mode
) {
2217 case nir_var_shader_out
:
2218 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2219 &const_index
, &indir_index
);
2220 for (unsigned chan
= 0; chan
< 4; chan
++) {
2222 if (!(writemask
& (1 << chan
)))
2224 if (get_llvm_num_components(src
) == 1)
2227 value
= LLVMBuildExtractElement(ctx
->builder
, src
,
2228 LLVMConstInt(ctx
->i32
,
2232 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CLIP_DIST0
||
2233 instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CULL_DIST0
)
2236 unsigned count
= glsl_count_attribute_slots(
2237 instr
->variables
[0]->var
->type
, false);
2238 LLVMValueRef tmp_vec
= build_gather_values_extended(
2239 ctx
, ctx
->outputs
+ idx
+ chan
, count
,
2242 if (get_llvm_num_components(tmp_vec
) > 1) {
2243 tmp_vec
= LLVMBuildInsertElement(ctx
->builder
, tmp_vec
,
2244 value
, indir_index
, "");
2247 build_store_values_extended(ctx
, ctx
->outputs
+ idx
+ chan
,
2248 count
, stride
, tmp_vec
);
2251 temp_ptr
= ctx
->outputs
[idx
+ chan
+ const_index
* stride
];
2253 LLVMBuildStore(ctx
->builder
, value
, temp_ptr
);
2258 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2259 &const_index
, &indir_index
);
2260 for (unsigned chan
= 0; chan
< 4; chan
++) {
2261 if (!(writemask
& (1 << chan
)))
2264 if (get_llvm_num_components(src
) == 1)
2267 value
= LLVMBuildExtractElement(ctx
->builder
, src
,
2268 LLVMConstInt(ctx
->i32
, chan
, false), "");
2270 unsigned count
= glsl_count_attribute_slots(
2271 instr
->variables
[0]->var
->type
, false);
2272 LLVMValueRef tmp_vec
= build_gather_values_extended(
2273 ctx
, ctx
->locals
+ idx
+ chan
, count
,
2276 tmp_vec
= LLVMBuildInsertElement(ctx
->builder
, tmp_vec
,
2277 value
, indir_index
, "");
2278 build_store_values_extended(ctx
, ctx
->locals
+ idx
+ chan
,
2281 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
2283 LLVMBuildStore(ctx
->builder
, value
, temp_ptr
);
2287 case nir_var_shared
: {
2289 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2290 &const_index
, &indir_index
);
2292 ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
2293 LLVMValueRef index
= ctx
->i32zero
;
2294 LLVMValueRef derived_ptr
;
2297 index
= LLVMBuildAdd(ctx
->builder
, index
, indir_index
, "");
2298 derived_ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, "");
2299 LLVMBuildStore(ctx
->builder
,
2300 to_integer(ctx
, src
), derived_ptr
);
2308 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
2311 case GLSL_SAMPLER_DIM_BUF
:
2313 case GLSL_SAMPLER_DIM_1D
:
2314 return array
? 2 : 1;
2315 case GLSL_SAMPLER_DIM_2D
:
2316 return array
? 3 : 2;
2317 case GLSL_SAMPLER_DIM_3D
:
2318 case GLSL_SAMPLER_DIM_CUBE
:
2320 case GLSL_SAMPLER_DIM_RECT
:
2321 case GLSL_SAMPLER_DIM_SUBPASS
:
2329 static LLVMValueRef
get_image_coords(struct nir_to_llvm_context
*ctx
,
2330 nir_intrinsic_instr
*instr
, bool add_frag_pos
)
2332 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
2333 if(instr
->variables
[0]->deref
.child
)
2334 type
= instr
->variables
[0]->deref
.child
->type
;
2336 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
2337 LLVMValueRef coords
[4];
2338 LLVMValueRef masks
[] = {
2339 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
2340 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false),
2344 count
= image_type_to_components_count(glsl_get_sampler_dim(type
),
2345 glsl_sampler_type_is_array(type
));
2348 if (instr
->src
[0].ssa
->num_components
)
2349 res
= LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[0], "");
2354 for (chan
= 0; chan
< count
; ++chan
) {
2355 coords
[chan
] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[chan
], "");
2359 for (chan
= 0; chan
< count
; ++chan
)
2360 coords
[chan
] = LLVMBuildAdd(ctx
->builder
, coords
[chan
], LLVMBuildFPToUI(ctx
->builder
, ctx
->frag_pos
[chan
], ctx
->i32
, ""), "");
2363 coords
[3] = LLVMGetUndef(ctx
->i32
);
2366 res
= build_gather_values(ctx
, coords
, count
);
2371 static void build_type_name_for_intr(
2373 char *buf
, unsigned bufsize
)
2375 LLVMTypeRef elem_type
= type
;
2377 assert(bufsize
>= 8);
2379 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
2380 int ret
= snprintf(buf
, bufsize
, "v%u",
2381 LLVMGetVectorSize(type
));
2383 char *type_name
= LLVMPrintTypeToString(type
);
2384 fprintf(stderr
, "Error building type name for: %s\n",
2388 elem_type
= LLVMGetElementType(type
);
2392 switch (LLVMGetTypeKind(elem_type
)) {
2394 case LLVMIntegerTypeKind
:
2395 snprintf(buf
, bufsize
, "i%d", LLVMGetIntTypeWidth(elem_type
));
2397 case LLVMFloatTypeKind
:
2398 snprintf(buf
, bufsize
, "f32");
2400 case LLVMDoubleTypeKind
:
2401 snprintf(buf
, bufsize
, "f64");
2406 static void get_image_intr_name(const char *base_name
,
2407 LLVMTypeRef data_type
,
2408 LLVMTypeRef coords_type
,
2409 LLVMTypeRef rsrc_type
,
2410 char *out_name
, unsigned out_len
)
2412 char coords_type_name
[8];
2414 build_type_name_for_intr(coords_type
, coords_type_name
,
2415 sizeof(coords_type_name
));
2417 if (HAVE_LLVM
<= 0x0309) {
2418 snprintf(out_name
, out_len
, "%s.%s", base_name
, coords_type_name
);
2420 char data_type_name
[8];
2421 char rsrc_type_name
[8];
2423 build_type_name_for_intr(data_type
, data_type_name
,
2424 sizeof(data_type_name
));
2425 build_type_name_for_intr(rsrc_type
, rsrc_type_name
,
2426 sizeof(rsrc_type_name
));
2427 snprintf(out_name
, out_len
, "%s.%s.%s.%s", base_name
,
2428 data_type_name
, coords_type_name
, rsrc_type_name
);
2432 static LLVMValueRef
visit_image_load(struct nir_to_llvm_context
*ctx
,
2433 nir_intrinsic_instr
*instr
)
2435 LLVMValueRef params
[7];
2437 char intrinsic_name
[64];
2438 const nir_variable
*var
= instr
->variables
[0]->var
;
2439 const struct glsl_type
*type
= var
->type
;
2440 if(instr
->variables
[0]->deref
.child
)
2441 type
= instr
->variables
[0]->deref
.child
->type
;
2443 type
= glsl_without_array(type
);
2444 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2445 params
[0] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
2446 params
[1] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
2447 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
2448 params
[2] = LLVMConstInt(ctx
->i32
, 0, false); /* voffset */
2449 params
[3] = LLVMConstInt(ctx
->i1
, 0, false); /* glc */
2450 params
[4] = LLVMConstInt(ctx
->i1
, 0, false); /* slc */
2451 res
= emit_llvm_intrinsic(ctx
, "llvm.amdgcn.buffer.load.format.v4f32", ctx
->v4f32
,
2454 res
= trim_vector(ctx
, res
, instr
->dest
.ssa
.num_components
);
2455 res
= to_integer(ctx
, res
);
2457 bool is_da
= glsl_sampler_type_is_array(type
) ||
2458 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
2459 bool add_frag_pos
= glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_SUBPASS
;
2460 LLVMValueRef da
= is_da
? ctx
->i32one
: ctx
->i32zero
;
2461 LLVMValueRef glc
= LLVMConstInt(ctx
->i1
, 0, false);
2462 LLVMValueRef slc
= LLVMConstInt(ctx
->i1
, 0, false);
2464 params
[0] = get_image_coords(ctx
, instr
, add_frag_pos
);
2465 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
2466 params
[2] = LLVMConstInt(ctx
->i32
, 15, false); /* dmask */
2467 if (HAVE_LLVM
<= 0x0309) {
2468 params
[3] = LLVMConstInt(ctx
->i1
, 0, false); /* r128 */
2473 LLVMValueRef lwe
= LLVMConstInt(ctx
->i1
, 0, false);
2480 get_image_intr_name("llvm.amdgcn.image.load",
2481 ctx
->v4f32
, /* vdata */
2482 LLVMTypeOf(params
[0]), /* coords */
2483 LLVMTypeOf(params
[1]), /* rsrc */
2484 intrinsic_name
, sizeof(intrinsic_name
));
2486 res
= emit_llvm_intrinsic(ctx
, intrinsic_name
, ctx
->v4f32
,
2487 params
, 7, AC_FUNC_ATTR_READONLY
);
2489 return to_integer(ctx
, res
);
2492 static void visit_image_store(struct nir_to_llvm_context
*ctx
,
2493 nir_intrinsic_instr
*instr
)
2495 LLVMValueRef params
[8];
2496 char intrinsic_name
[64];
2497 const nir_variable
*var
= instr
->variables
[0]->var
;
2498 LLVMValueRef i1false
= LLVMConstInt(ctx
->i1
, 0, 0);
2499 LLVMValueRef i1true
= LLVMConstInt(ctx
->i1
, 1, 0);
2500 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2502 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
2503 ctx
->shader_info
->fs
.writes_memory
= true;
2505 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2506 params
[0] = to_float(ctx
, get_src(ctx
, instr
->src
[2])); /* data */
2507 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
2508 params
[2] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
2509 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
2510 params
[3] = LLVMConstInt(ctx
->i32
, 0, false); /* voffset */
2511 params
[4] = i1false
; /* glc */
2512 params
[5] = i1false
; /* slc */
2513 emit_llvm_intrinsic(ctx
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->voidt
,
2516 bool is_da
= glsl_sampler_type_is_array(type
) ||
2517 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
2518 LLVMValueRef da
= is_da
? i1true
: i1false
;
2519 LLVMValueRef glc
= i1false
;
2520 LLVMValueRef slc
= i1false
;
2522 params
[0] = to_float(ctx
, get_src(ctx
, instr
->src
[2]));
2523 params
[1] = get_image_coords(ctx
, instr
, false); /* coords */
2524 params
[2] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
2525 params
[3] = LLVMConstInt(ctx
->i32
, 15, false); /* dmask */
2526 if (HAVE_LLVM
<= 0x0309) {
2527 params
[4] = i1false
; /* r128 */
2532 LLVMValueRef lwe
= i1false
;
2539 get_image_intr_name("llvm.amdgcn.image.store",
2540 LLVMTypeOf(params
[0]), /* vdata */
2541 LLVMTypeOf(params
[1]), /* coords */
2542 LLVMTypeOf(params
[2]), /* rsrc */
2543 intrinsic_name
, sizeof(intrinsic_name
));
2545 emit_llvm_intrinsic(ctx
, intrinsic_name
, ctx
->voidt
,
2551 static LLVMValueRef
visit_image_atomic(struct nir_to_llvm_context
*ctx
,
2552 nir_intrinsic_instr
*instr
)
2554 LLVMValueRef params
[6];
2555 int param_count
= 0;
2556 const nir_variable
*var
= instr
->variables
[0]->var
;
2557 LLVMValueRef i1false
= LLVMConstInt(ctx
->i1
, 0, 0);
2558 LLVMValueRef i1true
= LLVMConstInt(ctx
->i1
, 1, 0);
2559 const char *base_name
= "llvm.amdgcn.image.atomic";
2560 const char *atomic_name
;
2561 LLVMValueRef coords
;
2562 char intrinsic_name
[32], coords_type
[8];
2563 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2565 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
2566 ctx
->shader_info
->fs
.writes_memory
= true;
2568 params
[param_count
++] = get_src(ctx
, instr
->src
[2]);
2569 if (instr
->intrinsic
== nir_intrinsic_image_atomic_comp_swap
)
2570 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
2572 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2573 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
2574 coords
= params
[param_count
++] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
2575 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
2576 params
[param_count
++] = ctx
->i32zero
; /* voffset */
2577 params
[param_count
++] = i1false
; /* glc */
2578 params
[param_count
++] = i1false
; /* slc */
2580 bool da
= glsl_sampler_type_is_array(type
) ||
2581 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
2583 coords
= params
[param_count
++] = get_image_coords(ctx
, instr
, false);
2584 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
2585 params
[param_count
++] = i1false
; /* r128 */
2586 params
[param_count
++] = da
? i1true
: i1false
; /* da */
2587 params
[param_count
++] = i1false
; /* slc */
2590 switch (instr
->intrinsic
) {
2591 case nir_intrinsic_image_atomic_add
:
2592 atomic_name
= "add";
2594 case nir_intrinsic_image_atomic_min
:
2595 atomic_name
= "smin";
2597 case nir_intrinsic_image_atomic_max
:
2598 atomic_name
= "smax";
2600 case nir_intrinsic_image_atomic_and
:
2601 atomic_name
= "and";
2603 case nir_intrinsic_image_atomic_or
:
2606 case nir_intrinsic_image_atomic_xor
:
2607 atomic_name
= "xor";
2609 case nir_intrinsic_image_atomic_exchange
:
2610 atomic_name
= "swap";
2612 case nir_intrinsic_image_atomic_comp_swap
:
2613 atomic_name
= "cmpswap";
2618 build_int_type_name(LLVMTypeOf(coords
),
2619 coords_type
, sizeof(coords_type
));
2621 snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2622 "%s.%s.%s", base_name
, atomic_name
, coords_type
);
2623 return emit_llvm_intrinsic(ctx
, intrinsic_name
, ctx
->i32
, params
, param_count
, 0);
2626 static LLVMValueRef
visit_image_size(struct nir_to_llvm_context
*ctx
,
2627 nir_intrinsic_instr
*instr
)
2630 LLVMValueRef params
[10];
2631 const nir_variable
*var
= instr
->variables
[0]->var
;
2632 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
2633 bool da
= glsl_sampler_type_is_array(var
->type
) ||
2634 glsl_get_sampler_dim(var
->type
) == GLSL_SAMPLER_DIM_CUBE
;
2635 if(instr
->variables
[0]->deref
.child
)
2636 type
= instr
->variables
[0]->deref
.child
->type
;
2638 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
2639 return get_buffer_size(ctx
, get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
), true);
2640 params
[0] = ctx
->i32zero
;
2641 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
2642 params
[2] = LLVMConstInt(ctx
->i32
, 15, false);
2643 params
[3] = ctx
->i32zero
;
2644 params
[4] = ctx
->i32zero
;
2645 params
[5] = da
? ctx
->i32one
: ctx
->i32zero
;
2646 params
[6] = ctx
->i32zero
;
2647 params
[7] = ctx
->i32zero
;
2648 params
[8] = ctx
->i32zero
;
2649 params
[9] = ctx
->i32zero
;
2651 res
= emit_llvm_intrinsic(ctx
, "llvm.SI.getresinfo.i32", ctx
->v4i32
,
2652 params
, 10, AC_FUNC_ATTR_READNONE
);
2654 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
2655 glsl_sampler_type_is_array(type
)) {
2656 LLVMValueRef two
= LLVMConstInt(ctx
->i32
, 2, false);
2657 LLVMValueRef six
= LLVMConstInt(ctx
->i32
, 6, false);
2658 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->builder
, res
, two
, "");
2659 z
= LLVMBuildSDiv(ctx
->builder
, z
, six
, "");
2660 res
= LLVMBuildInsertElement(ctx
->builder
, res
, z
, two
, "");
2665 static void emit_waitcnt(struct nir_to_llvm_context
*ctx
)
2667 LLVMValueRef args
[1] = {
2668 LLVMConstInt(ctx
->i32
, 0xf70, false),
2670 emit_llvm_intrinsic(ctx
, "llvm.amdgcn.s.waitcnt",
2671 ctx
->voidt
, args
, 1, 0);
2674 static void emit_barrier(struct nir_to_llvm_context
*ctx
)
2677 emit_llvm_intrinsic(ctx
, "llvm.amdgcn.s.barrier",
2678 ctx
->voidt
, NULL
, 0, 0);
2681 static void emit_discard_if(struct nir_to_llvm_context
*ctx
,
2682 nir_intrinsic_instr
*instr
)
2685 ctx
->shader_info
->fs
.can_discard
= true;
2687 cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
2688 get_src(ctx
, instr
->src
[0]),
2691 cond
= LLVMBuildSelect(ctx
->builder
, cond
,
2692 LLVMConstReal(ctx
->f32
, -1.0f
),
2694 emit_llvm_intrinsic(ctx
, "llvm.AMDGPU.kill",
2695 LLVMVoidTypeInContext(ctx
->context
),
2700 visit_load_local_invocation_index(struct nir_to_llvm_context
*ctx
)
2702 LLVMValueRef result
;
2703 LLVMValueRef thread_id
= get_thread_id(ctx
);
2704 result
= LLVMBuildAnd(ctx
->builder
, ctx
->tg_size
,
2705 LLVMConstInt(ctx
->i32
, 0xfc0, false), "");
2707 return LLVMBuildAdd(ctx
->builder
, result
, thread_id
, "");
2710 static LLVMValueRef
visit_var_atomic(struct nir_to_llvm_context
*ctx
,
2711 nir_intrinsic_instr
*instr
)
2713 LLVMValueRef ptr
, result
;
2714 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2715 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
2716 ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
2718 if (instr
->intrinsic
== nir_intrinsic_var_atomic_comp_swap
) {
2719 LLVMValueRef src1
= get_src(ctx
, instr
->src
[1]);
2720 result
= LLVMBuildAtomicCmpXchg(ctx
->builder
,
2722 LLVMAtomicOrderingSequentiallyConsistent
,
2723 LLVMAtomicOrderingSequentiallyConsistent
,
2726 LLVMAtomicRMWBinOp op
;
2727 switch (instr
->intrinsic
) {
2728 case nir_intrinsic_var_atomic_add
:
2729 op
= LLVMAtomicRMWBinOpAdd
;
2731 case nir_intrinsic_var_atomic_umin
:
2732 op
= LLVMAtomicRMWBinOpUMin
;
2734 case nir_intrinsic_var_atomic_umax
:
2735 op
= LLVMAtomicRMWBinOpUMax
;
2737 case nir_intrinsic_var_atomic_imin
:
2738 op
= LLVMAtomicRMWBinOpMin
;
2740 case nir_intrinsic_var_atomic_imax
:
2741 op
= LLVMAtomicRMWBinOpMax
;
2743 case nir_intrinsic_var_atomic_and
:
2744 op
= LLVMAtomicRMWBinOpAnd
;
2746 case nir_intrinsic_var_atomic_or
:
2747 op
= LLVMAtomicRMWBinOpOr
;
2749 case nir_intrinsic_var_atomic_xor
:
2750 op
= LLVMAtomicRMWBinOpXor
;
2752 case nir_intrinsic_var_atomic_exchange
:
2753 op
= LLVMAtomicRMWBinOpXchg
;
2759 result
= LLVMBuildAtomicRMW(ctx
->builder
, op
, ptr
, to_integer(ctx
, src
),
2760 LLVMAtomicOrderingSequentiallyConsistent
,
2766 #define INTERP_CENTER 0
2767 #define INTERP_CENTROID 1
2768 #define INTERP_SAMPLE 2
2770 static LLVMValueRef
lookup_interp_param(struct nir_to_llvm_context
*ctx
,
2771 enum glsl_interp_mode interp
, unsigned location
)
2774 case INTERP_MODE_FLAT
:
2777 case INTERP_MODE_SMOOTH
:
2778 case INTERP_MODE_NONE
:
2779 if (location
== INTERP_CENTER
)
2780 return ctx
->persp_center
;
2781 else if (location
== INTERP_CENTROID
)
2782 return ctx
->persp_centroid
;
2783 else if (location
== INTERP_SAMPLE
)
2784 return ctx
->persp_sample
;
2786 case INTERP_MODE_NOPERSPECTIVE
:
2787 if (location
== INTERP_CENTER
)
2788 return ctx
->linear_center
;
2789 else if (location
== INTERP_CENTROID
)
2790 return ctx
->linear_centroid
;
2791 else if (location
== INTERP_SAMPLE
)
2792 return ctx
->linear_sample
;
2798 static LLVMValueRef
load_sample_position(struct nir_to_llvm_context
*ctx
,
2799 LLVMValueRef sample_id
)
2801 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
2802 LLVMValueRef offset0
= LLVMBuildMul(ctx
->builder
, sample_id
, LLVMConstInt(ctx
->i32
, 8, false), "");
2803 LLVMValueRef offset1
= LLVMBuildAdd(ctx
->builder
, offset0
, LLVMConstInt(ctx
->i32
, 4, false), "");
2804 LLVMValueRef result
[2];
2806 result
[0] = build_indexed_load_const(ctx
, ctx
->sample_positions
, offset0
);
2807 result
[1] = build_indexed_load_const(ctx
, ctx
->sample_positions
, offset1
);
2809 return build_gather_values(ctx
, result
, 2);
2812 static LLVMValueRef
load_sample_pos(struct nir_to_llvm_context
*ctx
)
2814 LLVMValueRef values
[2];
2816 values
[0] = emit_ffract(ctx
, ctx
->frag_pos
[0]);
2817 values
[1] = emit_ffract(ctx
, ctx
->frag_pos
[1]);
2818 return build_gather_values(ctx
, values
, 2);
2821 static LLVMValueRef
visit_interp(struct nir_to_llvm_context
*ctx
,
2822 nir_intrinsic_instr
*instr
)
2824 LLVMValueRef result
[2];
2825 LLVMValueRef interp_param
, attr_number
;
2828 LLVMValueRef src_c0
, src_c1
;
2829 const char *intr_name
;
2831 int input_index
= instr
->variables
[0]->var
->data
.location
- VARYING_SLOT_VAR0
;
2832 switch (instr
->intrinsic
) {
2833 case nir_intrinsic_interp_var_at_centroid
:
2834 location
= INTERP_CENTROID
;
2836 case nir_intrinsic_interp_var_at_sample
:
2837 case nir_intrinsic_interp_var_at_offset
:
2838 location
= INTERP_SAMPLE
;
2839 src0
= get_src(ctx
, instr
->src
[0]);
2845 if (instr
->intrinsic
== nir_intrinsic_interp_var_at_offset
) {
2846 src_c0
= to_float(ctx
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32zero
, ""));
2847 src_c1
= to_float(ctx
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32one
, ""));
2848 } else if (instr
->intrinsic
== nir_intrinsic_interp_var_at_sample
) {
2849 LLVMValueRef sample_position
;
2850 LLVMValueRef halfval
= LLVMConstReal(ctx
->f32
, 0.5f
);
2852 /* fetch sample ID */
2853 sample_position
= load_sample_position(ctx
, src0
);
2855 src_c0
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->i32zero
, "");
2856 src_c0
= LLVMBuildFSub(ctx
->builder
, src_c0
, halfval
, "");
2857 src_c1
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->i32one
, "");
2858 src_c1
= LLVMBuildFSub(ctx
->builder
, src_c1
, halfval
, "");
2860 interp_param
= lookup_interp_param(ctx
, instr
->variables
[0]->var
->data
.interpolation
, location
);
2861 attr_number
= LLVMConstInt(ctx
->i32
, input_index
, false);
2863 if (location
== INTERP_SAMPLE
) {
2864 LLVMValueRef ij_out
[2];
2865 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
, interp_param
);
2868 * take the I then J parameters, and the DDX/Y for it, and
2869 * calculate the IJ inputs for the interpolator.
2870 * temp1 = ddx * offset/sample.x + I;
2871 * interp_param.I = ddy * offset/sample.y + temp1;
2872 * temp1 = ddx * offset/sample.x + J;
2873 * interp_param.J = ddy * offset/sample.y + temp1;
2875 for (unsigned i
= 0; i
< 2; i
++) {
2876 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->i32
, i
, false);
2877 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->i32
, i
+ 2, false);
2878 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->builder
,
2879 ddxy_out
, ix_ll
, "");
2880 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->builder
,
2881 ddxy_out
, iy_ll
, "");
2882 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->builder
,
2883 interp_param
, ix_ll
, "");
2884 LLVMValueRef temp1
, temp2
;
2886 interp_el
= LLVMBuildBitCast(ctx
->builder
, interp_el
,
2889 temp1
= LLVMBuildFMul(ctx
->builder
, ddx_el
, src_c0
, "");
2890 temp1
= LLVMBuildFAdd(ctx
->builder
, temp1
, interp_el
, "");
2892 temp2
= LLVMBuildFMul(ctx
->builder
, ddy_el
, src_c1
, "");
2893 temp2
= LLVMBuildFAdd(ctx
->builder
, temp2
, temp1
, "");
2895 ij_out
[i
] = LLVMBuildBitCast(ctx
->builder
,
2896 temp2
, ctx
->i32
, "");
2898 interp_param
= build_gather_values(ctx
, ij_out
, 2);
2901 intr_name
= interp_param
? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
2902 for (chan
= 0; chan
< 2; chan
++) {
2903 LLVMValueRef args
[4];
2904 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
2906 args
[0] = llvm_chan
;
2907 args
[1] = attr_number
;
2908 args
[2] = ctx
->prim_mask
;
2909 args
[3] = interp_param
;
2910 result
[chan
] = emit_llvm_intrinsic(ctx
, intr_name
,
2911 ctx
->f32
, args
, args
[3] ? 4 : 3,
2912 AC_FUNC_ATTR_READNONE
);
2914 return build_gather_values(ctx
, result
, 2);
2917 static void visit_intrinsic(struct nir_to_llvm_context
*ctx
,
2918 nir_intrinsic_instr
*instr
)
2920 LLVMValueRef result
= NULL
;
2922 switch (instr
->intrinsic
) {
2923 case nir_intrinsic_load_work_group_id
: {
2924 result
= ctx
->workgroup_ids
;
2927 case nir_intrinsic_load_base_vertex
: {
2928 result
= ctx
->base_vertex
;
2931 case nir_intrinsic_load_vertex_id_zero_base
: {
2932 result
= ctx
->vertex_id
;
2935 case nir_intrinsic_load_local_invocation_id
: {
2936 result
= ctx
->local_invocation_ids
;
2939 case nir_intrinsic_load_base_instance
:
2940 result
= ctx
->start_instance
;
2942 case nir_intrinsic_load_sample_id
:
2943 ctx
->shader_info
->fs
.force_persample
= true;
2944 result
= unpack_param(ctx
, ctx
->ancillary
, 8, 4);
2946 case nir_intrinsic_load_sample_pos
:
2947 ctx
->shader_info
->fs
.force_persample
= true;
2948 result
= load_sample_pos(ctx
);
2950 case nir_intrinsic_load_front_face
:
2951 result
= ctx
->front_face
;
2953 case nir_intrinsic_load_instance_id
:
2954 result
= ctx
->instance_id
;
2955 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
2956 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
2958 case nir_intrinsic_load_num_work_groups
:
2959 result
= ctx
->num_work_groups
;
2961 case nir_intrinsic_load_local_invocation_index
:
2962 result
= visit_load_local_invocation_index(ctx
);
2964 case nir_intrinsic_load_push_constant
:
2965 result
= visit_load_push_constant(ctx
, instr
);
2967 case nir_intrinsic_vulkan_resource_index
:
2968 result
= visit_vulkan_resource_index(ctx
, instr
);
2970 case nir_intrinsic_store_ssbo
:
2971 visit_store_ssbo(ctx
, instr
);
2973 case nir_intrinsic_load_ssbo
:
2974 result
= visit_load_buffer(ctx
, instr
);
2976 case nir_intrinsic_ssbo_atomic_add
:
2977 case nir_intrinsic_ssbo_atomic_imin
:
2978 case nir_intrinsic_ssbo_atomic_umin
:
2979 case nir_intrinsic_ssbo_atomic_imax
:
2980 case nir_intrinsic_ssbo_atomic_umax
:
2981 case nir_intrinsic_ssbo_atomic_and
:
2982 case nir_intrinsic_ssbo_atomic_or
:
2983 case nir_intrinsic_ssbo_atomic_xor
:
2984 case nir_intrinsic_ssbo_atomic_exchange
:
2985 case nir_intrinsic_ssbo_atomic_comp_swap
:
2986 result
= visit_atomic_ssbo(ctx
, instr
);
2988 case nir_intrinsic_load_ubo
:
2989 result
= visit_load_ubo_buffer(ctx
, instr
);
2991 case nir_intrinsic_get_buffer_size
:
2992 result
= visit_get_buffer_size(ctx
, instr
);
2994 case nir_intrinsic_load_var
:
2995 result
= visit_load_var(ctx
, instr
);
2997 case nir_intrinsic_store_var
:
2998 visit_store_var(ctx
, instr
);
3000 case nir_intrinsic_image_load
:
3001 result
= visit_image_load(ctx
, instr
);
3003 case nir_intrinsic_image_store
:
3004 visit_image_store(ctx
, instr
);
3006 case nir_intrinsic_image_atomic_add
:
3007 case nir_intrinsic_image_atomic_min
:
3008 case nir_intrinsic_image_atomic_max
:
3009 case nir_intrinsic_image_atomic_and
:
3010 case nir_intrinsic_image_atomic_or
:
3011 case nir_intrinsic_image_atomic_xor
:
3012 case nir_intrinsic_image_atomic_exchange
:
3013 case nir_intrinsic_image_atomic_comp_swap
:
3014 result
= visit_image_atomic(ctx
, instr
);
3016 case nir_intrinsic_image_size
:
3017 result
= visit_image_size(ctx
, instr
);
3019 case nir_intrinsic_discard
:
3020 ctx
->shader_info
->fs
.can_discard
= true;
3021 emit_llvm_intrinsic(ctx
, "llvm.AMDGPU.kilp",
3022 LLVMVoidTypeInContext(ctx
->context
),
3025 case nir_intrinsic_discard_if
:
3026 emit_discard_if(ctx
, instr
);
3028 case nir_intrinsic_memory_barrier
:
3031 case nir_intrinsic_barrier
:
3034 case nir_intrinsic_var_atomic_add
:
3035 case nir_intrinsic_var_atomic_imin
:
3036 case nir_intrinsic_var_atomic_umin
:
3037 case nir_intrinsic_var_atomic_imax
:
3038 case nir_intrinsic_var_atomic_umax
:
3039 case nir_intrinsic_var_atomic_and
:
3040 case nir_intrinsic_var_atomic_or
:
3041 case nir_intrinsic_var_atomic_xor
:
3042 case nir_intrinsic_var_atomic_exchange
:
3043 case nir_intrinsic_var_atomic_comp_swap
:
3044 result
= visit_var_atomic(ctx
, instr
);
3046 case nir_intrinsic_interp_var_at_centroid
:
3047 case nir_intrinsic_interp_var_at_sample
:
3048 case nir_intrinsic_interp_var_at_offset
:
3049 result
= visit_interp(ctx
, instr
);
3052 fprintf(stderr
, "Unknown intrinsic: ");
3053 nir_print_instr(&instr
->instr
, stderr
);
3054 fprintf(stderr
, "\n");
3058 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
3062 static LLVMValueRef
get_sampler_desc(struct nir_to_llvm_context
*ctx
,
3063 nir_deref_var
*deref
,
3064 enum desc_type desc_type
)
3066 unsigned desc_set
= deref
->var
->data
.descriptor_set
;
3067 LLVMValueRef list
= ctx
->descriptor_sets
[desc_set
];
3068 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[desc_set
].layout
;
3069 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ deref
->var
->data
.binding
;
3070 unsigned offset
= binding
->offset
;
3071 unsigned stride
= binding
->size
;
3073 LLVMBuilderRef builder
= ctx
->builder
;
3075 LLVMValueRef indices
[2];
3076 LLVMValueRef index
= NULL
;
3078 assert(deref
->var
->data
.binding
< layout
->binding_count
);
3080 switch (desc_type
) {
3092 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
3103 if (deref
->deref
.child
) {
3104 nir_deref_array
*child
= (nir_deref_array
*)deref
->deref
.child
;
3106 assert(child
->deref_array_type
!= nir_deref_array_type_wildcard
);
3107 offset
+= child
->base_offset
* stride
;
3108 if (child
->deref_array_type
== nir_deref_array_type_indirect
) {
3109 index
= get_src(ctx
, child
->indirect
);
3113 assert(stride
% type_size
== 0);
3116 index
= ctx
->i32zero
;
3118 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, stride
/ type_size
, 0), "");
3119 indices
[0] = ctx
->i32zero
;
3120 indices
[1] = LLVMConstInt(ctx
->i32
, offset
, 0);
3121 list
= LLVMBuildGEP(builder
, list
, indices
, 2, "");
3122 list
= LLVMBuildPointerCast(builder
, list
, const_array(type
, 0), "");
3124 return build_indexed_load_const(ctx
, list
, index
);
3127 static void set_tex_fetch_args(struct nir_to_llvm_context
*ctx
,
3128 struct ac_tex_info
*tinfo
,
3129 nir_tex_instr
*instr
,
3131 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
3132 LLVMValueRef
*param
, unsigned count
,
3136 unsigned is_rect
= 0;
3137 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
3139 if (op
== nir_texop_lod
)
3141 /* Pad to power of two vector */
3142 while (count
< util_next_power_of_two(count
))
3143 param
[count
++] = LLVMGetUndef(ctx
->i32
);
3146 tinfo
->args
[0] = build_gather_values(ctx
, param
, count
);
3148 tinfo
->args
[0] = param
[0];
3150 tinfo
->args
[1] = res_ptr
;
3153 if (op
== nir_texop_txf
||
3154 op
== nir_texop_txf_ms
||
3155 op
== nir_texop_query_levels
||
3156 op
== nir_texop_texture_samples
||
3157 op
== nir_texop_txs
)
3158 tinfo
->dst_type
= ctx
->v4i32
;
3160 tinfo
->dst_type
= ctx
->v4f32
;
3161 tinfo
->args
[num_args
++] = samp_ptr
;
3164 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
&& op
== nir_texop_txf
) {
3165 tinfo
->args
[0] = res_ptr
;
3166 tinfo
->args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
3167 tinfo
->args
[2] = param
[0];
3168 tinfo
->arg_count
= 3;
3172 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, dmask
, 0);
3173 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, is_rect
, 0); /* unorm */
3174 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* r128 */
3175 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, da
? 1 : 0, 0);
3176 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* glc */
3177 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* slc */
3178 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* tfe */
3179 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* lwe */
3181 tinfo
->arg_count
= num_args
;
3184 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
3187 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
3188 * filtering manually. The driver sets img7 to a mask clearing
3189 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
3190 * s_and_b32 samp0, samp0, img7
3193 * The ANISO_OVERRIDE sampler field enables this fix in TA.
3195 static LLVMValueRef
sici_fix_sampler_aniso(struct nir_to_llvm_context
*ctx
,
3196 LLVMValueRef res
, LLVMValueRef samp
)
3198 LLVMBuilderRef builder
= ctx
->builder
;
3199 LLVMValueRef img7
, samp0
;
3201 if (ctx
->options
->chip_class
>= VI
)
3204 img7
= LLVMBuildExtractElement(builder
, res
,
3205 LLVMConstInt(ctx
->i32
, 7, 0), "");
3206 samp0
= LLVMBuildExtractElement(builder
, samp
,
3207 LLVMConstInt(ctx
->i32
, 0, 0), "");
3208 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
3209 return LLVMBuildInsertElement(builder
, samp
, samp0
,
3210 LLVMConstInt(ctx
->i32
, 0, 0), "");
3213 static void tex_fetch_ptrs(struct nir_to_llvm_context
*ctx
,
3214 nir_tex_instr
*instr
,
3215 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
3216 LLVMValueRef
*fmask_ptr
)
3218 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
3219 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_BUFFER
);
3221 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_IMAGE
);
3224 *samp_ptr
= get_sampler_desc(ctx
, instr
->sampler
, DESC_SAMPLER
);
3226 *samp_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_SAMPLER
);
3227 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
3228 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
3230 if (fmask_ptr
&& !instr
->sampler
&& (instr
->op
== nir_texop_txf_ms
||
3231 instr
->op
== nir_texop_samples_identical
))
3232 *fmask_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_FMASK
);
3235 static LLVMValueRef
build_cube_intrinsic(struct nir_to_llvm_context
*ctx
,
3239 LLVMValueRef v
, cube_vec
;
3242 LLVMTypeRef f32
= LLVMTypeOf(in
[0]);
3243 LLVMValueRef out
[4];
3245 out
[0] = emit_llvm_intrinsic(ctx
, "llvm.amdgcn.cubetc",
3246 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
3247 out
[1] = emit_llvm_intrinsic(ctx
, "llvm.amdgcn.cubesc",
3248 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
3249 out
[2] = emit_llvm_intrinsic(ctx
, "llvm.amdgcn.cubema",
3250 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
3251 out
[3] = emit_llvm_intrinsic(ctx
, "llvm.amdgcn.cubeid",
3252 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
3254 return build_gather_values(ctx
, out
, 4);
3260 c
[3] = LLVMGetUndef(LLVMTypeOf(in
[0]));
3261 cube_vec
= build_gather_values(ctx
, c
, 4);
3262 v
= emit_llvm_intrinsic(ctx
, "llvm.AMDGPU.cube", LLVMTypeOf(cube_vec
),
3263 &cube_vec
, 1, AC_FUNC_ATTR_READNONE
);
3268 static void cube_to_2d_coords(struct nir_to_llvm_context
*ctx
,
3269 LLVMValueRef
*in
, LLVMValueRef
*out
)
3271 LLVMValueRef coords
[4];
3272 LLVMValueRef mad_args
[3];
3277 v
= build_cube_intrinsic(ctx
, in
);
3278 for (i
= 0; i
< 4; i
++)
3279 coords
[i
] = LLVMBuildExtractElement(ctx
->builder
, v
,
3280 LLVMConstInt(ctx
->i32
, i
, false), "");
3282 coords
[2] = emit_llvm_intrinsic(ctx
, "llvm.fabs.f32", ctx
->f32
,
3283 &coords
[2], 1, AC_FUNC_ATTR_READNONE
);
3284 coords
[2] = emit_fdiv(ctx
, ctx
->f32one
, coords
[2]);
3286 mad_args
[1] = coords
[2];
3287 mad_args
[2] = LLVMConstReal(ctx
->f32
, 1.5);
3288 mad_args
[0] = coords
[0];
3291 tmp
= LLVMBuildFMul(ctx
->builder
, mad_args
[0], mad_args
[1], "");
3292 coords
[0] = LLVMBuildFAdd(ctx
->builder
, tmp
, mad_args
[2], "");
3294 mad_args
[0] = coords
[1];
3297 tmp
= LLVMBuildFMul(ctx
->builder
, mad_args
[0], mad_args
[1], "");
3298 coords
[1] = LLVMBuildFAdd(ctx
->builder
, tmp
, mad_args
[2], "");
3300 /* apply xyz = yxw swizzle to cooords */
3306 static void emit_prepare_cube_coords(struct nir_to_llvm_context
*ctx
,
3307 LLVMValueRef
*coords_arg
, int num_coords
,
3309 bool is_array
, LLVMValueRef
*derivs_arg
)
3311 LLVMValueRef coords
[4];
3313 cube_to_2d_coords(ctx
, coords_arg
, coords
);
3315 if (is_deriv
&& derivs_arg
) {
3316 LLVMValueRef derivs
[4];
3319 /* Convert cube derivatives to 2D derivatives. */
3320 for (axis
= 0; axis
< 2; axis
++) {
3321 LLVMValueRef shifted_cube_coords
[4], shifted_coords
[4];
3323 /* Shift the cube coordinates by the derivatives to get
3324 * the cube coordinates of the "neighboring pixel".
3326 for (i
= 0; i
< 3; i
++)
3327 shifted_cube_coords
[i
] =
3328 LLVMBuildFAdd(ctx
->builder
, coords_arg
[i
],
3329 derivs_arg
[axis
*3+i
], "");
3330 shifted_cube_coords
[3] = LLVMGetUndef(ctx
->f32
);
3332 /* Project the shifted cube coordinates onto the face. */
3333 cube_to_2d_coords(ctx
, shifted_cube_coords
,
3336 /* Subtract both sets of 2D coordinates to get 2D derivatives.
3337 * This won't work if the shifted coordinates ended up
3338 * in a different face.
3340 for (i
= 0; i
< 2; i
++)
3341 derivs
[axis
* 2 + i
] =
3342 LLVMBuildFSub(ctx
->builder
, shifted_coords
[i
],
3346 memcpy(derivs_arg
, derivs
, sizeof(derivs
));
3350 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
3351 /* coords_arg.w component - array_index for cube arrays */
3352 LLVMValueRef tmp
= LLVMBuildFMul(ctx
->builder
, coords_arg
[3], LLVMConstReal(ctx
->f32
, 8.0), "");
3353 coords
[2] = LLVMBuildFAdd(ctx
->builder
, tmp
, coords
[2], "");
3356 memcpy(coords_arg
, coords
, sizeof(coords
));
3359 static void visit_tex(struct nir_to_llvm_context
*ctx
, nir_tex_instr
*instr
)
3361 LLVMValueRef result
= NULL
;
3362 struct ac_tex_info tinfo
= { 0 };
3363 unsigned dmask
= 0xf;
3364 LLVMValueRef address
[16];
3365 LLVMValueRef coords
[5];
3366 LLVMValueRef coord
= NULL
, lod
= NULL
, comparitor
= NULL
;
3367 LLVMValueRef bias
= NULL
, offsets
= NULL
;
3368 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
, sample_index
= NULL
;
3369 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
3370 LLVMValueRef derivs
[6];
3371 unsigned chan
, count
= 0;
3372 unsigned const_src
= 0, num_deriv_comp
= 0;
3374 tex_fetch_ptrs(ctx
, instr
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
3376 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3377 switch (instr
->src
[i
].src_type
) {
3378 case nir_tex_src_coord
:
3379 coord
= get_src(ctx
, instr
->src
[i
].src
);
3381 case nir_tex_src_projector
:
3383 case nir_tex_src_comparitor
:
3384 comparitor
= get_src(ctx
, instr
->src
[i
].src
);
3386 case nir_tex_src_offset
:
3387 offsets
= get_src(ctx
, instr
->src
[i
].src
);
3390 case nir_tex_src_bias
:
3391 bias
= get_src(ctx
, instr
->src
[i
].src
);
3393 case nir_tex_src_lod
:
3394 lod
= get_src(ctx
, instr
->src
[i
].src
);
3396 case nir_tex_src_ms_index
:
3397 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
3399 case nir_tex_src_ms_mcs
:
3401 case nir_tex_src_ddx
:
3402 ddx
= get_src(ctx
, instr
->src
[i
].src
);
3403 num_deriv_comp
= instr
->src
[i
].src
.ssa
->num_components
;
3405 case nir_tex_src_ddy
:
3406 ddy
= get_src(ctx
, instr
->src
[i
].src
);
3408 case nir_tex_src_texture_offset
:
3409 case nir_tex_src_sampler_offset
:
3410 case nir_tex_src_plane
:
3416 if (instr
->op
== nir_texop_texture_samples
) {
3417 LLVMValueRef res
, samples
, is_msaa
;
3418 res
= LLVMBuildBitCast(ctx
->builder
, res_ptr
, ctx
->v8i32
, "");
3419 samples
= LLVMBuildExtractElement(ctx
->builder
, res
,
3420 LLVMConstInt(ctx
->i32
, 3, false), "");
3421 is_msaa
= LLVMBuildLShr(ctx
->builder
, samples
,
3422 LLVMConstInt(ctx
->i32
, 28, false), "");
3423 is_msaa
= LLVMBuildAnd(ctx
->builder
, is_msaa
,
3424 LLVMConstInt(ctx
->i32
, 0xe, false), "");
3425 is_msaa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, is_msaa
,
3426 LLVMConstInt(ctx
->i32
, 0xe, false), "");
3428 samples
= LLVMBuildLShr(ctx
->builder
, samples
,
3429 LLVMConstInt(ctx
->i32
, 16, false), "");
3430 samples
= LLVMBuildAnd(ctx
->builder
, samples
,
3431 LLVMConstInt(ctx
->i32
, 0xf, false), "");
3432 samples
= LLVMBuildShl(ctx
->builder
, ctx
->i32one
,
3434 samples
= LLVMBuildSelect(ctx
->builder
, is_msaa
, samples
,
3441 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
3442 coords
[chan
] = llvm_extract_elem(ctx
, coord
, chan
);
3444 if (offsets
&& instr
->op
!= nir_texop_txf
) {
3445 LLVMValueRef offset
[3], pack
;
3446 for (chan
= 0; chan
< 3; ++chan
)
3447 offset
[chan
] = ctx
->i32zero
;
3449 tinfo
.has_offset
= true;
3450 for (chan
= 0; chan
< get_llvm_num_components(offsets
); chan
++) {
3451 offset
[chan
] = llvm_extract_elem(ctx
, offsets
, chan
);
3452 offset
[chan
] = LLVMBuildAnd(ctx
->builder
, offset
[chan
],
3453 LLVMConstInt(ctx
->i32
, 0x3f, false), "");
3455 offset
[chan
] = LLVMBuildShl(ctx
->builder
, offset
[chan
],
3456 LLVMConstInt(ctx
->i32
, chan
* 8, false), "");
3458 pack
= LLVMBuildOr(ctx
->builder
, offset
[0], offset
[1], "");
3459 pack
= LLVMBuildOr(ctx
->builder
, pack
, offset
[2], "");
3460 address
[count
++] = pack
;
3463 /* pack LOD bias value */
3464 if (instr
->op
== nir_texop_txb
&& bias
) {
3465 address
[count
++] = bias
;
3468 /* Pack depth comparison value */
3469 if (instr
->is_shadow
&& comparitor
) {
3470 address
[count
++] = llvm_extract_elem(ctx
, comparitor
, 0);
3473 /* pack derivatives */
3475 switch (instr
->sampler_dim
) {
3476 case GLSL_SAMPLER_DIM_3D
:
3477 case GLSL_SAMPLER_DIM_CUBE
:
3480 case GLSL_SAMPLER_DIM_2D
:
3484 case GLSL_SAMPLER_DIM_1D
:
3489 for (unsigned i
= 0; i
< num_deriv_comp
; i
++) {
3490 derivs
[i
* 2] = to_float(ctx
, llvm_extract_elem(ctx
, ddx
, i
));
3491 derivs
[i
* 2 + 1] = to_float(ctx
, llvm_extract_elem(ctx
, ddy
, i
));
3495 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& coord
) {
3496 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
3497 coords
[chan
] = to_float(ctx
, coords
[chan
]);
3498 if (instr
->coord_components
== 3)
3499 coords
[3] = LLVMGetUndef(ctx
->f32
);
3500 emit_prepare_cube_coords(ctx
, coords
, instr
->coord_components
, instr
->op
== nir_texop_txd
, instr
->is_array
, derivs
);
3506 for (unsigned i
= 0; i
< num_deriv_comp
* 2; i
++)
3507 address
[count
++] = derivs
[i
];
3510 /* Pack texture coordinates */
3512 address
[count
++] = coords
[0];
3513 if (instr
->coord_components
> 1)
3514 address
[count
++] = coords
[1];
3515 if (instr
->coord_components
> 2) {
3516 /* This seems like a bit of a hack - but it passes Vulkan CTS with it */
3517 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_3D
&& instr
->op
!= nir_texop_txf
) {
3518 coords
[2] = to_float(ctx
, coords
[2]);
3519 coords
[2] = emit_llvm_intrinsic(ctx
, "llvm.rint.f32", ctx
->f32
, &coords
[2],
3521 coords
[2] = to_integer(ctx
, coords
[2]);
3523 address
[count
++] = coords
[2];
3528 if ((instr
->op
== nir_texop_txl
|| instr
->op
== nir_texop_txf
) && lod
) {
3529 address
[count
++] = lod
;
3530 } else if (instr
->op
== nir_texop_txf_ms
&& sample_index
) {
3531 address
[count
++] = sample_index
;
3532 } else if(instr
->op
== nir_texop_txs
) {
3535 address
[count
++] = lod
;
3537 address
[count
++] = ctx
->i32zero
;
3540 for (chan
= 0; chan
< count
; chan
++) {
3541 address
[chan
] = LLVMBuildBitCast(ctx
->builder
,
3542 address
[chan
], ctx
->i32
, "");
3545 if (instr
->op
== nir_texop_samples_identical
) {
3546 LLVMValueRef txf_address
[4];
3547 struct ac_tex_info txf_info
= { 0 };
3548 unsigned txf_count
= count
;
3549 memcpy(txf_address
, address
, sizeof(txf_address
));
3551 if (!instr
->is_array
)
3552 txf_address
[2] = ctx
->i32zero
;
3553 txf_address
[3] = ctx
->i32zero
;
3555 set_tex_fetch_args(ctx
, &txf_info
, instr
, nir_texop_txf
,
3557 txf_address
, txf_count
, 0xf);
3559 result
= build_tex_intrinsic(ctx
, instr
, &txf_info
);
3561 result
= LLVMBuildExtractElement(ctx
->builder
, result
, ctx
->i32zero
, "");
3562 result
= emit_int_cmp(ctx
, LLVMIntEQ
, result
, ctx
->i32zero
);
3566 /* Adjust the sample index according to FMASK.
3568 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3569 * which is the identity mapping. Each nibble says which physical sample
3570 * should be fetched to get that sample.
3572 * For example, 0x11111100 means there are only 2 samples stored and
3573 * the second sample covers 3/4 of the pixel. When reading samples 0
3574 * and 1, return physical sample 0 (determined by the first two 0s
3575 * in FMASK), otherwise return physical sample 1.
3577 * The sample index should be adjusted as follows:
3578 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
3580 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
) {
3581 LLVMValueRef txf_address
[4];
3582 struct ac_tex_info txf_info
= { 0 };
3583 unsigned txf_count
= count
;
3584 memcpy(txf_address
, address
, sizeof(txf_address
));
3586 if (!instr
->is_array
)
3587 txf_address
[2] = ctx
->i32zero
;
3588 txf_address
[3] = ctx
->i32zero
;
3590 set_tex_fetch_args(ctx
, &txf_info
, instr
, nir_texop_txf
,
3592 txf_address
, txf_count
, 0xf);
3594 result
= build_tex_intrinsic(ctx
, instr
, &txf_info
);
3595 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
3596 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
3598 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
3602 unsigned sample_chan
= instr
->is_array
? 3 : 2;
3604 LLVMValueRef sample_index4
=
3605 LLVMBuildMul(ctx
->builder
, address
[sample_chan
], four
, "");
3606 LLVMValueRef shifted_fmask
=
3607 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
3608 LLVMValueRef final_sample
=
3609 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
3611 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3612 * resource descriptor is 0 (invalid),
3614 LLVMValueRef fmask_desc
=
3615 LLVMBuildBitCast(ctx
->builder
, fmask_ptr
,
3618 LLVMValueRef fmask_word1
=
3619 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
3622 LLVMValueRef word1_is_nonzero
=
3623 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3624 fmask_word1
, ctx
->i32zero
, "");
3626 /* Replace the MSAA sample index. */
3627 address
[sample_chan
] =
3628 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
3629 final_sample
, address
[sample_chan
], "");
3632 if (offsets
&& instr
->op
== nir_texop_txf
) {
3633 nir_const_value
*const_offset
=
3634 nir_src_as_const_value(instr
->src
[const_src
].src
);
3635 int num_offsets
= instr
->src
[const_src
].src
.ssa
->num_components
;
3636 assert(const_offset
);
3637 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
3638 if (num_offsets
> 2)
3639 address
[2] = LLVMBuildAdd(ctx
->builder
,
3640 address
[2], LLVMConstInt(ctx
->i32
, const_offset
->i32
[2], false), "");
3641 if (num_offsets
> 1)
3642 address
[1] = LLVMBuildAdd(ctx
->builder
,
3643 address
[1], LLVMConstInt(ctx
->i32
, const_offset
->i32
[1], false), "");
3644 address
[0] = LLVMBuildAdd(ctx
->builder
,
3645 address
[0], LLVMConstInt(ctx
->i32
, const_offset
->i32
[0], false), "");
3649 /* TODO TG4 support */
3650 if (instr
->op
== nir_texop_tg4
) {
3651 if (instr
->is_shadow
)
3654 dmask
= 1 << instr
->component
;
3656 set_tex_fetch_args(ctx
, &tinfo
, instr
, instr
->op
,
3657 res_ptr
, samp_ptr
, address
, count
, dmask
);
3659 result
= build_tex_intrinsic(ctx
, instr
, &tinfo
);
3661 if (instr
->op
== nir_texop_query_levels
)
3662 result
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, 3, false), "");
3663 else if (instr
->is_shadow
&& instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
)
3664 result
= LLVMBuildExtractElement(ctx
->builder
, result
, ctx
->i32zero
, "");
3665 else if (instr
->op
== nir_texop_txs
&&
3666 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
3668 LLVMValueRef two
= LLVMConstInt(ctx
->i32
, 2, false);
3669 LLVMValueRef six
= LLVMConstInt(ctx
->i32
, 6, false);
3670 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->builder
, result
, two
, "");
3671 z
= LLVMBuildSDiv(ctx
->builder
, z
, six
, "");
3672 result
= LLVMBuildInsertElement(ctx
->builder
, result
, z
, two
, "");
3673 } else if (instr
->dest
.ssa
.num_components
!= 4)
3674 result
= trim_vector(ctx
, result
, instr
->dest
.ssa
.num_components
);
3678 assert(instr
->dest
.is_ssa
);
3679 result
= to_integer(ctx
, result
);
3680 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
3685 static void visit_phi(struct nir_to_llvm_context
*ctx
, nir_phi_instr
*instr
)
3687 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
3688 LLVMValueRef result
= LLVMBuildPhi(ctx
->builder
, type
, "");
3690 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
3691 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
3694 static void visit_post_phi(struct nir_to_llvm_context
*ctx
,
3695 nir_phi_instr
*instr
,
3696 LLVMValueRef llvm_phi
)
3698 nir_foreach_phi_src(src
, instr
) {
3699 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
3700 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
3702 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
3706 static void phi_post_pass(struct nir_to_llvm_context
*ctx
)
3708 struct hash_entry
*entry
;
3709 hash_table_foreach(ctx
->phis
, entry
) {
3710 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
3711 (LLVMValueRef
)entry
->data
);
3716 static void visit_ssa_undef(struct nir_to_llvm_context
*ctx
,
3717 nir_ssa_undef_instr
*instr
)
3719 unsigned num_components
= instr
->def
.num_components
;
3722 if (num_components
== 1)
3723 undef
= LLVMGetUndef(ctx
->i32
);
3725 undef
= LLVMGetUndef(LLVMVectorType(ctx
->i32
, num_components
));
3727 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, undef
);
3730 static void visit_jump(struct nir_to_llvm_context
*ctx
,
3731 nir_jump_instr
*instr
)
3733 switch (instr
->type
) {
3734 case nir_jump_break
:
3735 LLVMBuildBr(ctx
->builder
, ctx
->break_block
);
3736 LLVMClearInsertionPosition(ctx
->builder
);
3738 case nir_jump_continue
:
3739 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
3740 LLVMClearInsertionPosition(ctx
->builder
);
3743 fprintf(stderr
, "Unknown NIR jump instr: ");
3744 nir_print_instr(&instr
->instr
, stderr
);
3745 fprintf(stderr
, "\n");
3750 static void visit_cf_list(struct nir_to_llvm_context
*ctx
,
3751 struct exec_list
*list
);
3753 static void visit_block(struct nir_to_llvm_context
*ctx
, nir_block
*block
)
3755 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->builder
);
3756 nir_foreach_instr(instr
, block
)
3758 switch (instr
->type
) {
3759 case nir_instr_type_alu
:
3760 visit_alu(ctx
, nir_instr_as_alu(instr
));
3762 case nir_instr_type_load_const
:
3763 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
3765 case nir_instr_type_intrinsic
:
3766 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
3768 case nir_instr_type_tex
:
3769 visit_tex(ctx
, nir_instr_as_tex(instr
));
3771 case nir_instr_type_phi
:
3772 visit_phi(ctx
, nir_instr_as_phi(instr
));
3774 case nir_instr_type_ssa_undef
:
3775 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
3777 case nir_instr_type_jump
:
3778 visit_jump(ctx
, nir_instr_as_jump(instr
));
3781 fprintf(stderr
, "Unknown NIR instr type: ");
3782 nir_print_instr(instr
, stderr
);
3783 fprintf(stderr
, "\n");
3788 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
3791 static void visit_if(struct nir_to_llvm_context
*ctx
, nir_if
*if_stmt
)
3793 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
3795 LLVMBasicBlockRef merge_block
=
3796 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
3797 LLVMBasicBlockRef if_block
=
3798 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
3799 LLVMBasicBlockRef else_block
= merge_block
;
3800 if (!exec_list_is_empty(&if_stmt
->else_list
))
3801 else_block
= LLVMAppendBasicBlockInContext(
3802 ctx
->context
, ctx
->main_function
, "");
3804 LLVMValueRef cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, value
,
3805 LLVMConstInt(ctx
->i32
, 0, false), "");
3806 LLVMBuildCondBr(ctx
->builder
, cond
, if_block
, else_block
);
3808 LLVMPositionBuilderAtEnd(ctx
->builder
, if_block
);
3809 visit_cf_list(ctx
, &if_stmt
->then_list
);
3810 if (LLVMGetInsertBlock(ctx
->builder
))
3811 LLVMBuildBr(ctx
->builder
, merge_block
);
3813 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
3814 LLVMPositionBuilderAtEnd(ctx
->builder
, else_block
);
3815 visit_cf_list(ctx
, &if_stmt
->else_list
);
3816 if (LLVMGetInsertBlock(ctx
->builder
))
3817 LLVMBuildBr(ctx
->builder
, merge_block
);
3820 LLVMPositionBuilderAtEnd(ctx
->builder
, merge_block
);
3823 static void visit_loop(struct nir_to_llvm_context
*ctx
, nir_loop
*loop
)
3825 LLVMBasicBlockRef continue_parent
= ctx
->continue_block
;
3826 LLVMBasicBlockRef break_parent
= ctx
->break_block
;
3828 ctx
->continue_block
=
3829 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
3831 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
3833 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
3834 LLVMPositionBuilderAtEnd(ctx
->builder
, ctx
->continue_block
);
3835 visit_cf_list(ctx
, &loop
->body
);
3837 if (LLVMGetInsertBlock(ctx
->builder
))
3838 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
3839 LLVMPositionBuilderAtEnd(ctx
->builder
, ctx
->break_block
);
3841 ctx
->continue_block
= continue_parent
;
3842 ctx
->break_block
= break_parent
;
3845 static void visit_cf_list(struct nir_to_llvm_context
*ctx
,
3846 struct exec_list
*list
)
3848 foreach_list_typed(nir_cf_node
, node
, node
, list
)
3850 switch (node
->type
) {
3851 case nir_cf_node_block
:
3852 visit_block(ctx
, nir_cf_node_as_block(node
));
3855 case nir_cf_node_if
:
3856 visit_if(ctx
, nir_cf_node_as_if(node
));
3859 case nir_cf_node_loop
:
3860 visit_loop(ctx
, nir_cf_node_as_loop(node
));
3870 handle_vs_input_decl(struct nir_to_llvm_context
*ctx
,
3871 struct nir_variable
*variable
)
3873 LLVMValueRef t_list_ptr
= ctx
->vertex_buffers
;
3874 LLVMValueRef t_offset
;
3875 LLVMValueRef t_list
;
3876 LLVMValueRef args
[3];
3878 LLVMValueRef buffer_index
;
3879 int index
= variable
->data
.location
- VERT_ATTRIB_GENERIC0
;
3880 int idx
= variable
->data
.location
;
3881 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, true);
3883 variable
->data
.driver_location
= idx
* 4;
3885 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << index
)) {
3886 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->instance_id
,
3887 ctx
->start_instance
, "");
3888 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
3889 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
3891 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->vertex_id
,
3892 ctx
->base_vertex
, "");
3894 for (unsigned i
= 0; i
< attrib_count
; ++i
, ++idx
) {
3895 t_offset
= LLVMConstInt(ctx
->i32
, index
+ i
, false);
3897 t_list
= build_indexed_load_const(ctx
, t_list_ptr
, t_offset
);
3899 args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
3900 args
[2] = buffer_index
;
3901 input
= emit_llvm_intrinsic(ctx
,
3902 "llvm.SI.vs.load.input", ctx
->v4f32
, args
, 3,
3903 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_NOUNWIND
);
3905 for (unsigned chan
= 0; chan
< 4; chan
++) {
3906 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
3907 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
, chan
)] =
3908 to_integer(ctx
, LLVMBuildExtractElement(ctx
->builder
,
3909 input
, llvm_chan
, ""));
3915 static void interp_fs_input(struct nir_to_llvm_context
*ctx
,
3917 LLVMValueRef interp_param
,
3918 LLVMValueRef prim_mask
,
3919 LLVMValueRef result
[4])
3921 const char *intr_name
;
3922 LLVMValueRef attr_number
;
3925 attr_number
= LLVMConstInt(ctx
->i32
, attr
, false);
3927 /* fs.constant returns the param from the middle vertex, so it's not
3928 * really useful for flat shading. It's meant to be used for custom
3929 * interpolation (but the intrinsic can't fetch from the other two
3932 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
3933 * to do the right thing. The only reason we use fs.constant is that
3934 * fs.interp cannot be used on integers, because they can be equal
3937 intr_name
= interp_param
? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
3939 for (chan
= 0; chan
< 4; chan
++) {
3940 LLVMValueRef args
[4];
3941 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
3943 args
[0] = llvm_chan
;
3944 args
[1] = attr_number
;
3945 args
[2] = prim_mask
;
3946 args
[3] = interp_param
;
3947 result
[chan
] = emit_llvm_intrinsic(ctx
, intr_name
,
3948 ctx
->f32
, args
, args
[3] ? 4 : 3,
3949 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_NOUNWIND
);
3954 handle_fs_input_decl(struct nir_to_llvm_context
*ctx
,
3955 struct nir_variable
*variable
)
3957 int idx
= variable
->data
.location
;
3958 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
3959 LLVMValueRef interp
;
3961 variable
->data
.driver_location
= idx
* 4;
3962 ctx
->input_mask
|= ((1ull << attrib_count
) - 1) << variable
->data
.location
;
3964 if (glsl_get_base_type(glsl_without_array(variable
->type
)) == GLSL_TYPE_FLOAT
) {
3965 unsigned interp_type
;
3966 if (variable
->data
.sample
) {
3967 interp_type
= INTERP_SAMPLE
;
3968 ctx
->shader_info
->fs
.force_persample
= true;
3969 } else if (variable
->data
.centroid
)
3970 interp_type
= INTERP_CENTROID
;
3972 interp_type
= INTERP_CENTER
;
3974 interp
= lookup_interp_param(ctx
, variable
->data
.interpolation
, interp_type
);
3978 for (unsigned i
= 0; i
< attrib_count
; ++i
)
3979 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
+ i
, 0)] = interp
;
3984 handle_shader_input_decl(struct nir_to_llvm_context
*ctx
,
3985 struct nir_variable
*variable
)
3987 switch (ctx
->stage
) {
3988 case MESA_SHADER_VERTEX
:
3989 handle_vs_input_decl(ctx
, variable
);
3991 case MESA_SHADER_FRAGMENT
:
3992 handle_fs_input_decl(ctx
, variable
);
4001 handle_fs_inputs_pre(struct nir_to_llvm_context
*ctx
,
4002 struct nir_shader
*nir
)
4005 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_INPUTS
; ++i
) {
4006 LLVMValueRef interp_param
;
4007 LLVMValueRef
*inputs
= ctx
->inputs
+radeon_llvm_reg_index_soa(i
, 0);
4009 if (!(ctx
->input_mask
& (1ull << i
)))
4012 if (i
>= VARYING_SLOT_VAR0
|| i
== VARYING_SLOT_PNTC
) {
4013 interp_param
= *inputs
;
4014 interp_fs_input(ctx
, index
, interp_param
, ctx
->prim_mask
,
4018 ctx
->shader_info
->fs
.flat_shaded_mask
|= 1u << index
;
4020 } else if (i
== VARYING_SLOT_POS
) {
4021 for(int i
= 0; i
< 3; ++i
)
4022 inputs
[i
] = ctx
->frag_pos
[i
];
4024 inputs
[3] = emit_fdiv(ctx
, ctx
->f32one
, ctx
->frag_pos
[3]);
4027 ctx
->shader_info
->fs
.num_interp
= index
;
4028 if (ctx
->input_mask
& (1 << VARYING_SLOT_PNTC
))
4029 ctx
->shader_info
->fs
.has_pcoord
= true;
4030 ctx
->shader_info
->fs
.input_mask
= ctx
->input_mask
>> VARYING_SLOT_VAR0
;
4034 ac_build_alloca(struct nir_to_llvm_context
*ctx
,
4038 LLVMBuilderRef builder
= ctx
->builder
;
4039 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
4040 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
4041 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
4042 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
4043 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ctx
->context
);
4047 LLVMPositionBuilderBefore(first_builder
, first_instr
);
4049 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
4052 res
= LLVMBuildAlloca(first_builder
, type
, name
);
4053 LLVMBuildStore(builder
, LLVMConstNull(type
), res
);
4055 LLVMDisposeBuilder(first_builder
);
4060 static LLVMValueRef
si_build_alloca_undef(struct nir_to_llvm_context
*ctx
,
4064 LLVMValueRef ptr
= ac_build_alloca(ctx
, type
, name
);
4065 LLVMBuildStore(ctx
->builder
, LLVMGetUndef(type
), ptr
);
4070 handle_shader_output_decl(struct nir_to_llvm_context
*ctx
,
4071 struct nir_variable
*variable
)
4073 int idx
= variable
->data
.location
;
4074 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4076 variable
->data
.driver_location
= idx
* 4;
4078 if (ctx
->stage
== MESA_SHADER_VERTEX
) {
4080 if (idx
== VARYING_SLOT_CLIP_DIST0
||
4081 idx
== VARYING_SLOT_CULL_DIST0
) {
4082 int length
= glsl_get_length(variable
->type
);
4083 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
4084 ctx
->shader_info
->vs
.clip_dist_mask
= (1 << length
) - 1;
4085 ctx
->num_clips
= length
;
4086 } else if (idx
== VARYING_SLOT_CULL_DIST0
) {
4087 ctx
->shader_info
->vs
.cull_dist_mask
= (1 << length
) - 1;
4088 ctx
->num_culls
= length
;
4097 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
4098 for (unsigned chan
= 0; chan
< 4; chan
++) {
4099 ctx
->outputs
[radeon_llvm_reg_index_soa(idx
+ i
, chan
)] =
4100 si_build_alloca_undef(ctx
, ctx
->f32
, "");
4103 ctx
->output_mask
|= ((1ull << attrib_count
) - 1) << variable
->data
.location
;
4107 setup_locals(struct nir_to_llvm_context
*ctx
,
4108 struct nir_function
*func
)
4111 ctx
->num_locals
= 0;
4112 nir_foreach_variable(variable
, &func
->impl
->locals
) {
4113 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4114 variable
->data
.driver_location
= ctx
->num_locals
* 4;
4115 ctx
->num_locals
+= attrib_count
;
4117 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
4121 for (i
= 0; i
< ctx
->num_locals
; i
++) {
4122 for (j
= 0; j
< 4; j
++) {
4123 ctx
->locals
[i
* 4 + j
] =
4124 si_build_alloca_undef(ctx
, ctx
->f32
, "temp");
4130 emit_float_saturate(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
, float lo
, float hi
)
4132 v
= to_float(ctx
, v
);
4133 v
= emit_intrin_2f_param(ctx
, "llvm.maxnum.f32", v
, LLVMConstReal(ctx
->f32
, lo
));
4134 return emit_intrin_2f_param(ctx
, "llvm.minnum.f32", v
, LLVMConstReal(ctx
->f32
, hi
));
4138 static LLVMValueRef
emit_pack_int16(struct nir_to_llvm_context
*ctx
,
4139 LLVMValueRef src0
, LLVMValueRef src1
)
4141 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
4142 LLVMValueRef comp
[2];
4144 comp
[0] = LLVMBuildAnd(ctx
->builder
, src0
, LLVMConstInt(ctx
-> i32
, 65535, 0), "");
4145 comp
[1] = LLVMBuildAnd(ctx
->builder
, src1
, LLVMConstInt(ctx
-> i32
, 65535, 0), "");
4146 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
4147 return LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
4150 /* Initialize arguments for the shader export intrinsic */
4152 si_llvm_init_export_args(struct nir_to_llvm_context
*ctx
,
4153 LLVMValueRef
*values
,
4157 /* Default is 0xf. Adjusted below depending on the format. */
4158 args
[0] = LLVMConstInt(ctx
->i32
, target
!= V_008DFC_SQ_EXP_NULL
? 0xf : 0, false);
4159 /* Specify whether the EXEC mask represents the valid mask */
4160 args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
4162 /* Specify whether this is the last export */
4163 args
[2] = LLVMConstInt(ctx
->i32
, 0, false);
4164 /* Specify the target we are exporting */
4165 args
[3] = LLVMConstInt(ctx
->i32
, target
, false);
4167 args
[4] = LLVMConstInt(ctx
->i32
, 0, false); /* COMPR flag */
4168 args
[5] = LLVMGetUndef(ctx
->f32
);
4169 args
[6] = LLVMGetUndef(ctx
->f32
);
4170 args
[7] = LLVMGetUndef(ctx
->f32
);
4171 args
[8] = LLVMGetUndef(ctx
->f32
);
4176 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&& target
>= V_008DFC_SQ_EXP_MRT
) {
4177 LLVMValueRef val
[4];
4178 unsigned index
= target
- V_008DFC_SQ_EXP_MRT
;
4179 unsigned col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * index
)) & 0xf;
4180 bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> index
) & 1;
4182 switch(col_format
) {
4183 case V_028714_SPI_SHADER_ZERO
:
4184 args
[0] = LLVMConstInt(ctx
->i32
, 0x0, 0);
4185 args
[3] = LLVMConstInt(ctx
->i32
, V_008DFC_SQ_EXP_NULL
, 0);
4188 case V_028714_SPI_SHADER_32_R
:
4189 args
[0] = LLVMConstInt(ctx
->i32
, 0x1, 0);
4190 args
[5] = values
[0];
4193 case V_028714_SPI_SHADER_32_GR
:
4194 args
[0] = LLVMConstInt(ctx
->i32
, 0x3, 0);
4195 args
[5] = values
[0];
4196 args
[6] = values
[1];
4199 case V_028714_SPI_SHADER_32_AR
:
4200 args
[0] = LLVMConstInt(ctx
->i32
, 0x9, 0);
4201 args
[5] = values
[0];
4202 args
[8] = values
[3];
4205 case V_028714_SPI_SHADER_FP16_ABGR
:
4206 args
[4] = ctx
->i32one
;
4208 for (unsigned chan
= 0; chan
< 2; chan
++) {
4209 LLVMValueRef pack_args
[2] = {
4211 values
[2 * chan
+ 1]
4213 LLVMValueRef packed
;
4215 packed
= emit_llvm_intrinsic(ctx
, "llvm.SI.packf16",
4216 ctx
->i32
, pack_args
, 2,
4217 AC_FUNC_ATTR_READNONE
);
4218 args
[chan
+ 5] = packed
;
4222 case V_028714_SPI_SHADER_UNORM16_ABGR
:
4223 for (unsigned chan
= 0; chan
< 4; chan
++) {
4224 val
[chan
] = emit_float_saturate(ctx
, values
[chan
], 0, 1);
4225 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
4226 LLVMConstReal(ctx
->f32
, 65535), "");
4227 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
4228 LLVMConstReal(ctx
->f32
, 0.5), "");
4229 val
[chan
] = LLVMBuildFPToUI(ctx
->builder
, val
[chan
],
4233 args
[4] = ctx
->i32one
;
4234 args
[5] = emit_pack_int16(ctx
, val
[0], val
[1]);
4235 args
[6] = emit_pack_int16(ctx
, val
[2], val
[3]);
4238 case V_028714_SPI_SHADER_SNORM16_ABGR
:
4239 for (unsigned chan
= 0; chan
< 4; chan
++) {
4240 val
[chan
] = emit_float_saturate(ctx
, values
[chan
], -1, 1);
4241 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
4242 LLVMConstReal(ctx
->f32
, 32767), "");
4244 /* If positive, add 0.5, else add -0.5. */
4245 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
4246 LLVMBuildSelect(ctx
->builder
,
4247 LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
,
4248 val
[chan
], ctx
->f32zero
, ""),
4249 LLVMConstReal(ctx
->f32
, 0.5),
4250 LLVMConstReal(ctx
->f32
, -0.5), ""), "");
4251 val
[chan
] = LLVMBuildFPToSI(ctx
->builder
, val
[chan
], ctx
->i32
, "");
4254 args
[4] = ctx
->i32one
;
4255 args
[5] = emit_pack_int16(ctx
, val
[0], val
[1]);
4256 args
[6] = emit_pack_int16(ctx
, val
[2], val
[3]);
4259 case V_028714_SPI_SHADER_UINT16_ABGR
: {
4260 LLVMValueRef max
= LLVMConstInt(ctx
->i32
, is_int8
? 255 : 65535, 0);
4262 for (unsigned chan
= 0; chan
< 4; chan
++) {
4263 val
[chan
] = to_integer(ctx
, values
[chan
]);
4264 val
[chan
] = emit_minmax_int(ctx
, LLVMIntULT
, val
[chan
], max
);
4267 args
[4] = ctx
->i32one
;
4268 args
[5] = emit_pack_int16(ctx
, val
[0], val
[1]);
4269 args
[6] = emit_pack_int16(ctx
, val
[2], val
[3]);
4273 case V_028714_SPI_SHADER_SINT16_ABGR
: {
4274 LLVMValueRef max
= LLVMConstInt(ctx
->i32
, is_int8
? 127 : 32767, 0);
4275 LLVMValueRef min
= LLVMConstInt(ctx
->i32
, is_int8
? -128 : -32768, 0);
4278 for (unsigned chan
= 0; chan
< 4; chan
++) {
4279 val
[chan
] = to_integer(ctx
, values
[chan
]);
4280 val
[chan
] = emit_minmax_int(ctx
, LLVMIntSLT
, val
[chan
], max
);
4281 val
[chan
] = emit_minmax_int(ctx
, LLVMIntSGT
, val
[chan
], min
);
4284 args
[4] = ctx
->i32one
;
4285 args
[5] = emit_pack_int16(ctx
, val
[0], val
[1]);
4286 args
[6] = emit_pack_int16(ctx
, val
[2], val
[3]);
4291 case V_028714_SPI_SHADER_32_ABGR
:
4292 memcpy(&args
[5], values
, sizeof(values
[0]) * 4);
4296 memcpy(&args
[5], values
, sizeof(values
[0]) * 4);
4298 for (unsigned i
= 5; i
< 9; ++i
)
4299 args
[i
] = to_float(ctx
, args
[i
]);
4303 handle_vs_outputs_post(struct nir_to_llvm_context
*ctx
,
4304 struct nir_shader
*nir
)
4306 uint32_t param_count
= 0;
4308 unsigned pos_idx
, num_pos_exports
= 0;
4309 LLVMValueRef args
[9];
4310 LLVMValueRef pos_args
[4][9] = { { 0 } };
4311 LLVMValueRef psize_value
= 0;
4313 const uint64_t clip_mask
= ctx
->output_mask
& ((1ull << VARYING_SLOT_CLIP_DIST0
) |
4314 (1ull << VARYING_SLOT_CLIP_DIST1
) |
4315 (1ull << VARYING_SLOT_CULL_DIST0
) |
4316 (1ull << VARYING_SLOT_CULL_DIST1
));
4319 LLVMValueRef slots
[8];
4322 if (ctx
->shader_info
->vs
.cull_dist_mask
)
4323 ctx
->shader_info
->vs
.cull_dist_mask
<<= ctx
->num_clips
;
4325 i
= VARYING_SLOT_CLIP_DIST0
;
4326 for (j
= 0; j
< ctx
->num_clips
; j
++)
4327 slots
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4328 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4329 i
= VARYING_SLOT_CULL_DIST0
;
4330 for (j
= 0; j
< ctx
->num_culls
; j
++)
4331 slots
[ctx
->num_clips
+ j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4332 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4334 for (i
= ctx
->num_clips
+ ctx
->num_culls
; i
< 8; i
++)
4335 slots
[i
] = LLVMGetUndef(ctx
->f32
);
4337 if (ctx
->num_clips
+ ctx
->num_culls
> 4) {
4338 target
= V_008DFC_SQ_EXP_POS
+ 3;
4339 si_llvm_init_export_args(ctx
, &slots
[4], target
, args
);
4340 memcpy(pos_args
[target
- V_008DFC_SQ_EXP_POS
],
4341 args
, sizeof(args
));
4344 target
= V_008DFC_SQ_EXP_POS
+ 2;
4345 si_llvm_init_export_args(ctx
, &slots
[0], target
, args
);
4346 memcpy(pos_args
[target
- V_008DFC_SQ_EXP_POS
],
4347 args
, sizeof(args
));
4351 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
4352 LLVMValueRef values
[4];
4353 if (!(ctx
->output_mask
& (1ull << i
)))
4356 for (unsigned j
= 0; j
< 4; j
++)
4357 values
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4358 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4360 if (i
== VARYING_SLOT_POS
) {
4361 target
= V_008DFC_SQ_EXP_POS
;
4362 } else if (i
== VARYING_SLOT_CLIP_DIST0
||
4363 i
== VARYING_SLOT_CLIP_DIST1
||
4364 i
== VARYING_SLOT_CULL_DIST0
||
4365 i
== VARYING_SLOT_CULL_DIST1
) {
4367 } else if (i
== VARYING_SLOT_PSIZ
) {
4368 ctx
->shader_info
->vs
.writes_pointsize
= true;
4369 psize_value
= values
[0];
4371 } else if (i
>= VARYING_SLOT_VAR0
) {
4372 ctx
->shader_info
->vs
.export_mask
|= 1u << (i
- VARYING_SLOT_VAR0
);
4373 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
4377 si_llvm_init_export_args(ctx
, values
, target
, args
);
4379 if (target
>= V_008DFC_SQ_EXP_POS
&&
4380 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
4381 memcpy(pos_args
[target
- V_008DFC_SQ_EXP_POS
],
4382 args
, sizeof(args
));
4384 emit_llvm_intrinsic(ctx
,
4386 LLVMVoidTypeInContext(ctx
->context
),
4391 /* We need to add the position output manually if it's missing. */
4392 if (!pos_args
[0][0]) {
4393 pos_args
[0][0] = LLVMConstInt(ctx
->i32
, 0xf, false);
4394 pos_args
[0][1] = ctx
->i32zero
; /* EXEC mask */
4395 pos_args
[0][2] = ctx
->i32zero
; /* last export? */
4396 pos_args
[0][3] = LLVMConstInt(ctx
->i32
, V_008DFC_SQ_EXP_POS
, false);
4397 pos_args
[0][4] = ctx
->i32zero
; /* COMPR flag */
4398 pos_args
[0][5] = ctx
->f32zero
; /* X */
4399 pos_args
[0][6] = ctx
->f32zero
; /* Y */
4400 pos_args
[0][7] = ctx
->f32zero
; /* Z */
4401 pos_args
[0][8] = ctx
->f32one
; /* W */
4404 if (ctx
->shader_info
->vs
.writes_pointsize
== true) {
4405 pos_args
[1][0] = LLVMConstInt(ctx
->i32
, (ctx
->shader_info
->vs
.writes_pointsize
== true), false); /* writemask */
4406 pos_args
[1][1] = ctx
->i32zero
; /* EXEC mask */
4407 pos_args
[1][2] = ctx
->i32zero
; /* last export? */
4408 pos_args
[1][3] = LLVMConstInt(ctx
->i32
, V_008DFC_SQ_EXP_POS
+ 1, false);
4409 pos_args
[1][4] = ctx
->i32zero
; /* COMPR flag */
4410 pos_args
[1][5] = ctx
->f32zero
; /* X */
4411 pos_args
[1][6] = ctx
->f32zero
; /* Y */
4412 pos_args
[1][7] = ctx
->f32zero
; /* Z */
4413 pos_args
[1][8] = ctx
->f32zero
; /* W */
4415 if (ctx
->shader_info
->vs
.writes_pointsize
== true)
4416 pos_args
[1][5] = psize_value
;
4418 for (i
= 0; i
< 4; i
++) {
4424 for (i
= 0; i
< 4; i
++) {
4425 if (!pos_args
[i
][0])
4428 /* Specify the target we are exporting */
4429 pos_args
[i
][3] = LLVMConstInt(ctx
->i32
, V_008DFC_SQ_EXP_POS
+ pos_idx
++, false);
4430 if (pos_idx
== num_pos_exports
)
4431 pos_args
[i
][2] = ctx
->i32one
;
4432 emit_llvm_intrinsic(ctx
,
4434 LLVMVoidTypeInContext(ctx
->context
),
4438 ctx
->shader_info
->vs
.pos_exports
= num_pos_exports
;
4439 ctx
->shader_info
->vs
.param_exports
= param_count
;
4443 si_export_mrt_color(struct nir_to_llvm_context
*ctx
,
4444 LLVMValueRef
*color
, unsigned param
, bool is_last
)
4446 LLVMValueRef args
[9];
4448 si_llvm_init_export_args(ctx
, color
, param
,
4452 args
[1] = ctx
->i32one
; /* whether the EXEC mask is valid */
4453 args
[2] = ctx
->i32one
; /* DONE bit */
4454 } else if (args
[0] == ctx
->i32zero
)
4455 return; /* unnecessary NULL export */
4457 emit_llvm_intrinsic(ctx
, "llvm.SI.export",
4458 ctx
->voidt
, args
, 9, 0);
4462 si_export_mrt_z(struct nir_to_llvm_context
*ctx
,
4463 LLVMValueRef depth
, LLVMValueRef stencil
,
4464 LLVMValueRef samplemask
)
4466 LLVMValueRef args
[9];
4468 args
[1] = ctx
->i32one
; /* whether the EXEC mask is valid */
4469 args
[2] = ctx
->i32one
; /* DONE bit */
4470 /* Specify the target we are exporting */
4471 args
[3] = LLVMConstInt(ctx
->i32
, V_008DFC_SQ_EXP_MRTZ
, false);
4473 args
[4] = ctx
->i32zero
; /* COMP flag */
4474 args
[5] = LLVMGetUndef(ctx
->f32
); /* R, depth */
4475 args
[6] = LLVMGetUndef(ctx
->f32
); /* G, stencil test val[0:7], stencil op val[8:15] */
4476 args
[7] = LLVMGetUndef(ctx
->f32
); /* B, sample mask */
4477 args
[8] = LLVMGetUndef(ctx
->f32
); /* A, alpha to mask */
4490 args
[7] = samplemask
;
4494 /* SI (except OLAND) has a bug that it only looks
4495 * at the X writemask component. */
4496 if (ctx
->options
->chip_class
== SI
&&
4497 ctx
->options
->family
!= CHIP_OLAND
)
4500 args
[0] = LLVMConstInt(ctx
->i32
, mask
, false);
4501 emit_llvm_intrinsic(ctx
, "llvm.SI.export",
4502 ctx
->voidt
, args
, 9, 0);
4506 handle_fs_outputs_post(struct nir_to_llvm_context
*ctx
,
4507 struct nir_shader
*nir
)
4510 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
4512 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
4513 LLVMValueRef values
[4];
4515 if (!(ctx
->output_mask
& (1ull << i
)))
4518 if (i
== FRAG_RESULT_DEPTH
) {
4519 ctx
->shader_info
->fs
.writes_z
= true;
4520 depth
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4521 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
4522 } else if (i
== FRAG_RESULT_STENCIL
) {
4523 ctx
->shader_info
->fs
.writes_stencil
= true;
4524 stencil
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4525 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
4528 for (unsigned j
= 0; j
< 4; j
++)
4529 values
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4530 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4532 if (!ctx
->shader_info
->fs
.writes_z
&& !ctx
->shader_info
->fs
.writes_stencil
)
4533 last
= ctx
->output_mask
<= ((1ull << (i
+ 1)) - 1);
4535 si_export_mrt_color(ctx
, values
, V_008DFC_SQ_EXP_MRT
+ index
, last
);
4540 if (depth
|| stencil
)
4541 si_export_mrt_z(ctx
, depth
, stencil
, samplemask
);
4543 si_export_mrt_color(ctx
, NULL
, V_008DFC_SQ_EXP_NULL
, true);
4545 ctx
->shader_info
->fs
.output_mask
= index
? ((1ull << index
) - 1) : 0;
4549 handle_shader_outputs_post(struct nir_to_llvm_context
*ctx
,
4550 struct nir_shader
*nir
)
4552 switch (ctx
->stage
) {
4553 case MESA_SHADER_VERTEX
:
4554 handle_vs_outputs_post(ctx
, nir
);
4556 case MESA_SHADER_FRAGMENT
:
4557 handle_fs_outputs_post(ctx
, nir
);
4565 handle_shared_compute_var(struct nir_to_llvm_context
*ctx
,
4566 struct nir_variable
*variable
, uint32_t *offset
, int idx
)
4568 unsigned size
= glsl_count_attribute_slots(variable
->type
, false);
4569 variable
->data
.driver_location
= *offset
;
4573 static void ac_llvm_finalize_module(struct nir_to_llvm_context
* ctx
)
4575 LLVMPassManagerRef passmgr
;
4576 /* Create the pass manager */
4577 passmgr
= LLVMCreateFunctionPassManagerForModule(
4580 /* This pass should eliminate all the load and store instructions */
4581 LLVMAddPromoteMemoryToRegisterPass(passmgr
);
4583 /* Add some optimization passes */
4584 LLVMAddScalarReplAggregatesPass(passmgr
);
4585 LLVMAddLICMPass(passmgr
);
4586 LLVMAddAggressiveDCEPass(passmgr
);
4587 LLVMAddCFGSimplificationPass(passmgr
);
4588 LLVMAddInstructionCombiningPass(passmgr
);
4591 LLVMInitializeFunctionPassManager(passmgr
);
4592 LLVMRunFunctionPassManager(passmgr
, ctx
->main_function
);
4593 LLVMFinalizeFunctionPassManager(passmgr
);
4595 LLVMDisposeBuilder(ctx
->builder
);
4596 LLVMDisposePassManager(passmgr
);
4600 LLVMModuleRef
ac_translate_nir_to_llvm(LLVMTargetMachineRef tm
,
4601 struct nir_shader
*nir
,
4602 struct ac_shader_variant_info
*shader_info
,
4603 const struct ac_nir_compiler_options
*options
)
4605 struct nir_to_llvm_context ctx
= {0};
4606 struct nir_function
*func
;
4607 ctx
.options
= options
;
4608 ctx
.shader_info
= shader_info
;
4609 ctx
.context
= LLVMContextCreate();
4610 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
4612 ctx
.has_ds_bpermute
= ctx
.options
->chip_class
>= VI
;
4614 memset(shader_info
, 0, sizeof(*shader_info
));
4616 LLVMSetTarget(ctx
.module
, "amdgcn--");
4619 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
4620 ctx
.stage
= nir
->stage
;
4622 create_function(&ctx
, nir
);
4624 if (nir
->stage
== MESA_SHADER_COMPUTE
) {
4626 nir_foreach_variable(variable
, &nir
->shared
)
4630 uint32_t shared_size
= 0;
4632 LLVMTypeRef i8p
= LLVMPointerType(ctx
.i8
, LOCAL_ADDR_SPACE
);
4633 nir_foreach_variable(variable
, &nir
->shared
) {
4634 handle_shared_compute_var(&ctx
, variable
, &shared_size
, idx
);
4639 var
= LLVMAddGlobalInAddressSpace(ctx
.module
,
4640 LLVMArrayType(ctx
.i8
, shared_size
),
4643 LLVMSetAlignment(var
, 4);
4644 ctx
.shared_memory
= LLVMBuildBitCast(ctx
.builder
, var
, i8p
, "");
4648 nir_foreach_variable(variable
, &nir
->inputs
)
4649 handle_shader_input_decl(&ctx
, variable
);
4651 if (nir
->stage
== MESA_SHADER_FRAGMENT
)
4652 handle_fs_inputs_pre(&ctx
, nir
);
4654 nir_foreach_variable(variable
, &nir
->outputs
)
4655 handle_shader_output_decl(&ctx
, variable
);
4657 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4658 _mesa_key_pointer_equal
);
4659 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4660 _mesa_key_pointer_equal
);
4662 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
4664 setup_locals(&ctx
, func
);
4666 visit_cf_list(&ctx
, &func
->impl
->body
);
4667 phi_post_pass(&ctx
);
4669 handle_shader_outputs_post(&ctx
, nir
);
4670 LLVMBuildRetVoid(ctx
.builder
);
4672 ac_llvm_finalize_module(&ctx
);
4674 ralloc_free(ctx
.defs
);
4675 ralloc_free(ctx
.phis
);
4680 static void ac_diagnostic_handler(LLVMDiagnosticInfoRef di
, void *context
)
4682 unsigned *retval
= (unsigned *)context
;
4683 LLVMDiagnosticSeverity severity
= LLVMGetDiagInfoSeverity(di
);
4684 char *description
= LLVMGetDiagInfoDescription(di
);
4686 if (severity
== LLVMDSError
) {
4688 fprintf(stderr
, "LLVM triggered Diagnostic Handler: %s\n",
4692 LLVMDisposeMessage(description
);
4695 static unsigned ac_llvm_compile(LLVMModuleRef M
,
4696 struct ac_shader_binary
*binary
,
4697 LLVMTargetMachineRef tm
)
4699 unsigned retval
= 0;
4701 LLVMContextRef llvm_ctx
;
4702 LLVMMemoryBufferRef out_buffer
;
4703 unsigned buffer_size
;
4704 const char *buffer_data
;
4707 /* Setup Diagnostic Handler*/
4708 llvm_ctx
= LLVMGetModuleContext(M
);
4710 LLVMContextSetDiagnosticHandler(llvm_ctx
, ac_diagnostic_handler
,
4714 mem_err
= LLVMTargetMachineEmitToMemoryBuffer(tm
, M
, LLVMObjectFile
,
4717 /* Process Errors/Warnings */
4719 fprintf(stderr
, "%s: %s", __FUNCTION__
, err
);
4725 /* Extract Shader Code*/
4726 buffer_size
= LLVMGetBufferSize(out_buffer
);
4727 buffer_data
= LLVMGetBufferStart(out_buffer
);
4729 ac_elf_read(buffer_data
, buffer_size
, binary
);
4732 LLVMDisposeMemoryBuffer(out_buffer
);
4738 void ac_compile_nir_shader(LLVMTargetMachineRef tm
,
4739 struct ac_shader_binary
*binary
,
4740 struct ac_shader_config
*config
,
4741 struct ac_shader_variant_info
*shader_info
,
4742 struct nir_shader
*nir
,
4743 const struct ac_nir_compiler_options
*options
,
4747 LLVMModuleRef llvm_module
= ac_translate_nir_to_llvm(tm
, nir
, shader_info
,
4750 LLVMDumpModule(llvm_module
);
4752 memset(binary
, 0, sizeof(*binary
));
4753 int v
= ac_llvm_compile(llvm_module
, binary
, tm
);
4755 fprintf(stderr
, "compile failed\n");
4759 fprintf(stderr
, "disasm:\n%s\n", binary
->disasm_string
);
4761 ac_shader_binary_read_config(binary
, config
, 0);
4763 LLVMContextRef ctx
= LLVMGetModuleContext(llvm_module
);
4764 LLVMDisposeModule(llvm_module
);
4765 LLVMContextDispose(ctx
);
4767 if (nir
->stage
== MESA_SHADER_FRAGMENT
) {
4768 shader_info
->num_input_vgprs
= 0;
4769 if (G_0286CC_PERSP_SAMPLE_ENA(config
->spi_ps_input_addr
))
4770 shader_info
->num_input_vgprs
+= 2;
4771 if (G_0286CC_PERSP_CENTER_ENA(config
->spi_ps_input_addr
))
4772 shader_info
->num_input_vgprs
+= 2;
4773 if (G_0286CC_PERSP_CENTROID_ENA(config
->spi_ps_input_addr
))
4774 shader_info
->num_input_vgprs
+= 2;
4775 if (G_0286CC_PERSP_PULL_MODEL_ENA(config
->spi_ps_input_addr
))
4776 shader_info
->num_input_vgprs
+= 3;
4777 if (G_0286CC_LINEAR_SAMPLE_ENA(config
->spi_ps_input_addr
))
4778 shader_info
->num_input_vgprs
+= 2;
4779 if (G_0286CC_LINEAR_CENTER_ENA(config
->spi_ps_input_addr
))
4780 shader_info
->num_input_vgprs
+= 2;
4781 if (G_0286CC_LINEAR_CENTROID_ENA(config
->spi_ps_input_addr
))
4782 shader_info
->num_input_vgprs
+= 2;
4783 if (G_0286CC_LINE_STIPPLE_TEX_ENA(config
->spi_ps_input_addr
))
4784 shader_info
->num_input_vgprs
+= 1;
4785 if (G_0286CC_POS_X_FLOAT_ENA(config
->spi_ps_input_addr
))
4786 shader_info
->num_input_vgprs
+= 1;
4787 if (G_0286CC_POS_Y_FLOAT_ENA(config
->spi_ps_input_addr
))
4788 shader_info
->num_input_vgprs
+= 1;
4789 if (G_0286CC_POS_Z_FLOAT_ENA(config
->spi_ps_input_addr
))
4790 shader_info
->num_input_vgprs
+= 1;
4791 if (G_0286CC_POS_W_FLOAT_ENA(config
->spi_ps_input_addr
))
4792 shader_info
->num_input_vgprs
+= 1;
4793 if (G_0286CC_FRONT_FACE_ENA(config
->spi_ps_input_addr
))
4794 shader_info
->num_input_vgprs
+= 1;
4795 if (G_0286CC_ANCILLARY_ENA(config
->spi_ps_input_addr
))
4796 shader_info
->num_input_vgprs
+= 1;
4797 if (G_0286CC_SAMPLE_COVERAGE_ENA(config
->spi_ps_input_addr
))
4798 shader_info
->num_input_vgprs
+= 1;
4799 if (G_0286CC_POS_FIXED_PT_ENA(config
->spi_ps_input_addr
))
4800 shader_info
->num_input_vgprs
+= 1;
4802 config
->num_vgprs
= MAX2(config
->num_vgprs
, shader_info
->num_input_vgprs
);
4804 /* +3 for scratch wave offset and VCC */
4805 config
->num_sgprs
= MAX2(config
->num_sgprs
,
4806 shader_info
->num_input_sgprs
+ 3);
4807 if (nir
->stage
== MESA_SHADER_COMPUTE
) {
4808 for (int i
= 0; i
< 3; ++i
)
4809 shader_info
->cs
.block_size
[i
] = nir
->info
->cs
.local_size
[i
];
4812 if (nir
->stage
== MESA_SHADER_FRAGMENT
)
4813 shader_info
->fs
.early_fragment_test
= nir
->info
->fs
.early_fragment_tests
;