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
;
136 LLVMValueRef args
[12];
138 LLVMTypeRef dst_type
;
143 AC_FUNC_ATTR_ALWAYSINLINE
= (1 << 0),
144 AC_FUNC_ATTR_BYVAL
= (1 << 1),
145 AC_FUNC_ATTR_INREG
= (1 << 2),
146 AC_FUNC_ATTR_NOALIAS
= (1 << 3),
147 AC_FUNC_ATTR_NOUNWIND
= (1 << 4),
148 AC_FUNC_ATTR_READNONE
= (1 << 5),
149 AC_FUNC_ATTR_READONLY
= (1 << 6),
150 AC_FUNC_ATTR_LAST
= (1 << 7)
153 #if HAVE_LLVM < 0x0400
154 static LLVMAttribute
ac_attr_to_llvm_attr(enum ac_func_attr attr
)
157 case AC_FUNC_ATTR_ALWAYSINLINE
: return LLVMAlwaysInlineAttribute
;
158 case AC_FUNC_ATTR_BYVAL
: return LLVMByValAttribute
;
159 case AC_FUNC_ATTR_INREG
: return LLVMInRegAttribute
;
160 case AC_FUNC_ATTR_NOALIAS
: return LLVMNoAliasAttribute
;
161 case AC_FUNC_ATTR_NOUNWIND
: return LLVMNoUnwindAttribute
;
162 case AC_FUNC_ATTR_READNONE
: return LLVMReadNoneAttribute
;
163 case AC_FUNC_ATTR_READONLY
: return LLVMReadOnlyAttribute
;
165 fprintf(stderr
, "Unhandled function attribute: %x\n", attr
);
172 static const char *attr_to_str(enum ac_func_attr attr
)
175 case AC_FUNC_ATTR_ALWAYSINLINE
: return "alwaysinline";
176 case AC_FUNC_ATTR_BYVAL
: return "byval";
177 case AC_FUNC_ATTR_INREG
: return "inreg";
178 case AC_FUNC_ATTR_NOALIAS
: return "noalias";
179 case AC_FUNC_ATTR_NOUNWIND
: return "nounwind";
180 case AC_FUNC_ATTR_READNONE
: return "readnone";
181 case AC_FUNC_ATTR_READONLY
: return "readonly";
183 fprintf(stderr
, "Unhandled function attribute: %x\n", attr
);
191 ac_add_function_attr(LLVMValueRef function
,
193 enum ac_func_attr attr
)
196 #if HAVE_LLVM < 0x0400
197 LLVMAttribute llvm_attr
= ac_attr_to_llvm_attr(attr
);
198 if (attr_idx
== -1) {
199 LLVMAddFunctionAttr(function
, llvm_attr
);
201 LLVMAddAttribute(LLVMGetParam(function
, attr_idx
- 1), llvm_attr
);
204 LLVMContextRef context
= LLVMGetModuleContext(LLVMGetGlobalParent(function
));
205 const char *attr_name
= attr_to_str(attr
);
206 unsigned kind_id
= LLVMGetEnumAttributeKindForName(attr_name
,
208 LLVMAttributeRef llvm_attr
= LLVMCreateEnumAttribute(context
, kind_id
, 0);
209 LLVMAddAttributeAtIndex(function
, attr_idx
, llvm_attr
);
214 emit_llvm_intrinsic(struct nir_to_llvm_context
*ctx
, const char *name
,
215 LLVMTypeRef return_type
, LLVMValueRef
*params
,
216 unsigned param_count
, unsigned attr_mask
);
217 static LLVMValueRef
get_sampler_desc(struct nir_to_llvm_context
*ctx
,
218 nir_deref_var
*deref
,
219 enum desc_type desc_type
);
220 static unsigned radeon_llvm_reg_index_soa(unsigned index
, unsigned chan
)
222 return (index
* 4) + chan
;
225 static unsigned llvm_get_type_size(LLVMTypeRef type
)
227 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
230 case LLVMIntegerTypeKind
:
231 return LLVMGetIntTypeWidth(type
) / 8;
232 case LLVMFloatTypeKind
:
234 case LLVMPointerTypeKind
:
236 case LLVMVectorTypeKind
:
237 return LLVMGetVectorSize(type
) *
238 llvm_get_type_size(LLVMGetElementType(type
));
245 static void set_llvm_calling_convention(LLVMValueRef func
,
246 gl_shader_stage stage
)
248 enum radeon_llvm_calling_convention calling_conv
;
251 case MESA_SHADER_VERTEX
:
252 case MESA_SHADER_TESS_CTRL
:
253 case MESA_SHADER_TESS_EVAL
:
254 calling_conv
= RADEON_LLVM_AMDGPU_VS
;
256 case MESA_SHADER_GEOMETRY
:
257 calling_conv
= RADEON_LLVM_AMDGPU_GS
;
259 case MESA_SHADER_FRAGMENT
:
260 calling_conv
= RADEON_LLVM_AMDGPU_PS
;
262 case MESA_SHADER_COMPUTE
:
263 calling_conv
= RADEON_LLVM_AMDGPU_CS
;
266 unreachable("Unhandle shader type");
269 LLVMSetFunctionCallConv(func
, calling_conv
);
273 create_llvm_function(LLVMContextRef ctx
, LLVMModuleRef module
,
274 LLVMBuilderRef builder
, LLVMTypeRef
*return_types
,
275 unsigned num_return_elems
, LLVMTypeRef
*param_types
,
276 unsigned param_count
, unsigned array_params
,
277 unsigned sgpr_params
, bool unsafe_math
)
279 LLVMTypeRef main_function_type
, ret_type
;
280 LLVMBasicBlockRef main_function_body
;
282 if (num_return_elems
)
283 ret_type
= LLVMStructTypeInContext(ctx
, return_types
,
284 num_return_elems
, true);
286 ret_type
= LLVMVoidTypeInContext(ctx
);
288 /* Setup the function */
290 LLVMFunctionType(ret_type
, param_types
, param_count
, 0);
291 LLVMValueRef main_function
=
292 LLVMAddFunction(module
, "main", main_function_type
);
294 LLVMAppendBasicBlockInContext(ctx
, main_function
, "main_body");
295 LLVMPositionBuilderAtEnd(builder
, main_function_body
);
297 LLVMSetFunctionCallConv(main_function
, RADEON_LLVM_AMDGPU_CS
);
298 for (unsigned i
= 0; i
< sgpr_params
; ++i
) {
299 if (i
< array_params
) {
300 LLVMValueRef P
= LLVMGetParam(main_function
, i
);
301 ac_add_function_attr(main_function
, i
+ 1, AC_FUNC_ATTR_BYVAL
);
302 ac_add_attr_dereferenceable(P
, UINT64_MAX
);
305 ac_add_function_attr(main_function
, i
+ 1, AC_FUNC_ATTR_INREG
);
310 /* These were copied from some LLVM test. */
311 LLVMAddTargetDependentFunctionAttr(main_function
,
312 "less-precise-fpmad",
314 LLVMAddTargetDependentFunctionAttr(main_function
,
317 LLVMAddTargetDependentFunctionAttr(main_function
,
320 LLVMAddTargetDependentFunctionAttr(main_function
,
324 return main_function
;
327 static LLVMTypeRef
const_array(LLVMTypeRef elem_type
, int num_elements
)
329 return LLVMPointerType(LLVMArrayType(elem_type
, num_elements
),
333 static LLVMValueRef
get_shared_memory_ptr(struct nir_to_llvm_context
*ctx
,
341 offset
= LLVMConstInt(ctx
->i32
, idx
, false);
343 ptr
= ctx
->shared_memory
;
344 ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &offset
, 1, "");
345 addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
346 ptr
= LLVMBuildBitCast(ctx
->builder
, ptr
, LLVMPointerType(type
, addr_space
), "");
350 static LLVMValueRef
to_integer(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
)
352 LLVMTypeRef type
= LLVMTypeOf(v
);
353 if (type
== ctx
->f32
) {
354 return LLVMBuildBitCast(ctx
->builder
, v
, ctx
->i32
, "");
355 } else if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
356 LLVMTypeRef elem_type
= LLVMGetElementType(type
);
357 if (elem_type
== ctx
->f32
) {
358 LLVMTypeRef nt
= LLVMVectorType(ctx
->i32
, LLVMGetVectorSize(type
));
359 return LLVMBuildBitCast(ctx
->builder
, v
, nt
, "");
365 static LLVMValueRef
to_float(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
)
367 LLVMTypeRef type
= LLVMTypeOf(v
);
368 if (type
== ctx
->i32
) {
369 return LLVMBuildBitCast(ctx
->builder
, v
, ctx
->f32
, "");
370 } else if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
371 LLVMTypeRef elem_type
= LLVMGetElementType(type
);
372 if (elem_type
== ctx
->i32
) {
373 LLVMTypeRef nt
= LLVMVectorType(ctx
->f32
, LLVMGetVectorSize(type
));
374 return LLVMBuildBitCast(ctx
->builder
, v
, nt
, "");
380 static LLVMValueRef
build_indexed_load(struct nir_to_llvm_context
*ctx
,
381 LLVMValueRef base_ptr
, LLVMValueRef index
,
384 LLVMValueRef pointer
;
385 LLVMValueRef indices
[] = {ctx
->i32zero
, index
};
387 pointer
= LLVMBuildGEP(ctx
->builder
, base_ptr
, indices
, 2, "");
389 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
390 return LLVMBuildLoad(ctx
->builder
, pointer
, "");
393 static LLVMValueRef
build_indexed_load_const(struct nir_to_llvm_context
*ctx
,
394 LLVMValueRef base_ptr
, LLVMValueRef index
)
396 LLVMValueRef result
= build_indexed_load(ctx
, base_ptr
, index
, true);
397 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
401 static void create_function(struct nir_to_llvm_context
*ctx
,
402 struct nir_shader
*nir
)
404 LLVMTypeRef arg_types
[23];
405 unsigned arg_idx
= 0;
406 unsigned array_count
= 0;
407 unsigned sgpr_count
= 0, user_sgpr_count
;
410 /* 1 for each descriptor set */
411 for (unsigned i
= 0; i
< 4; ++i
)
412 arg_types
[arg_idx
++] = const_array(ctx
->i8
, 1024 * 1024);
414 /* 1 for push constants and dynamic descriptors */
415 arg_types
[arg_idx
++] = const_array(ctx
->i8
, 1024 * 1024);
417 array_count
= arg_idx
;
418 switch (nir
->stage
) {
419 case MESA_SHADER_COMPUTE
:
420 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3); /* grid size */
421 user_sgpr_count
= arg_idx
;
422 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3);
423 arg_types
[arg_idx
++] = ctx
->i32
;
424 sgpr_count
= arg_idx
;
426 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3);
428 case MESA_SHADER_VERTEX
:
429 arg_types
[arg_idx
++] = const_array(ctx
->v16i8
, 16); /* vertex buffers */
430 arg_types
[arg_idx
++] = ctx
->i32
; // base vertex
431 arg_types
[arg_idx
++] = ctx
->i32
; // start instance
432 user_sgpr_count
= sgpr_count
= arg_idx
;
433 arg_types
[arg_idx
++] = ctx
->i32
; // vertex id
434 arg_types
[arg_idx
++] = ctx
->i32
; // rel auto id
435 arg_types
[arg_idx
++] = ctx
->i32
; // vs prim id
436 arg_types
[arg_idx
++] = ctx
->i32
; // instance id
438 case MESA_SHADER_FRAGMENT
:
439 arg_types
[arg_idx
++] = const_array(ctx
->f32
, 32); /* sample positions */
440 user_sgpr_count
= arg_idx
;
441 arg_types
[arg_idx
++] = ctx
->i32
; /* prim mask */
442 sgpr_count
= arg_idx
;
443 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp sample */
444 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp center */
445 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp centroid */
446 arg_types
[arg_idx
++] = ctx
->v3i32
; /* persp pull model */
447 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear sample */
448 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear center */
449 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear centroid */
450 arg_types
[arg_idx
++] = ctx
->f32
; /* line stipple tex */
451 arg_types
[arg_idx
++] = ctx
->f32
; /* pos x float */
452 arg_types
[arg_idx
++] = ctx
->f32
; /* pos y float */
453 arg_types
[arg_idx
++] = ctx
->f32
; /* pos z float */
454 arg_types
[arg_idx
++] = ctx
->f32
; /* pos w float */
455 arg_types
[arg_idx
++] = ctx
->i32
; /* front face */
456 arg_types
[arg_idx
++] = ctx
->i32
; /* ancillary */
457 arg_types
[arg_idx
++] = ctx
->f32
; /* sample coverage */
458 arg_types
[arg_idx
++] = ctx
->i32
; /* fixed pt */
461 unreachable("Shader stage not implemented");
464 ctx
->main_function
= create_llvm_function(
465 ctx
->context
, ctx
->module
, ctx
->builder
, NULL
, 0, arg_types
,
466 arg_idx
, array_count
, sgpr_count
, ctx
->options
->unsafe_math
);
467 set_llvm_calling_convention(ctx
->main_function
, nir
->stage
);
470 ctx
->shader_info
->num_input_sgprs
= 0;
471 ctx
->shader_info
->num_input_vgprs
= 0;
473 for (i
= 0; i
< user_sgpr_count
; i
++)
474 ctx
->shader_info
->num_user_sgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
476 ctx
->shader_info
->num_input_sgprs
= ctx
->shader_info
->num_user_sgprs
;
477 for (; i
< sgpr_count
; i
++)
478 ctx
->shader_info
->num_input_sgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
480 if (nir
->stage
!= MESA_SHADER_FRAGMENT
)
481 for (; i
< arg_idx
; ++i
)
482 ctx
->shader_info
->num_input_vgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
485 for (unsigned i
= 0; i
< 4; ++i
)
486 ctx
->descriptor_sets
[i
] =
487 LLVMGetParam(ctx
->main_function
, arg_idx
++);
489 ctx
->push_constants
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
491 switch (nir
->stage
) {
492 case MESA_SHADER_COMPUTE
:
493 ctx
->num_work_groups
=
494 LLVMGetParam(ctx
->main_function
, arg_idx
++);
496 LLVMGetParam(ctx
->main_function
, arg_idx
++);
498 LLVMGetParam(ctx
->main_function
, arg_idx
++);
499 ctx
->local_invocation_ids
=
500 LLVMGetParam(ctx
->main_function
, arg_idx
++);
502 case MESA_SHADER_VERTEX
:
503 ctx
->vertex_buffers
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
504 ctx
->base_vertex
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
505 ctx
->start_instance
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
506 ctx
->vertex_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
507 ctx
->rel_auto_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
508 ctx
->vs_prim_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
509 ctx
->instance_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
511 case MESA_SHADER_FRAGMENT
:
512 ctx
->sample_positions
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
513 ctx
->prim_mask
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
514 ctx
->persp_sample
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
515 ctx
->persp_center
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
516 ctx
->persp_centroid
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
518 ctx
->linear_sample
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
519 ctx
->linear_center
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
520 ctx
->linear_centroid
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
521 arg_idx
++; /* line stipple */
522 ctx
->frag_pos
[0] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
523 ctx
->frag_pos
[1] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
524 ctx
->frag_pos
[2] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
525 ctx
->frag_pos
[3] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
526 ctx
->front_face
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
527 ctx
->ancillary
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
530 unreachable("Shader stage not implemented");
534 static void setup_types(struct nir_to_llvm_context
*ctx
)
536 LLVMValueRef args
[4];
538 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->context
);
539 ctx
->i1
= LLVMIntTypeInContext(ctx
->context
, 1);
540 ctx
->i8
= LLVMIntTypeInContext(ctx
->context
, 8);
541 ctx
->i16
= LLVMIntTypeInContext(ctx
->context
, 16);
542 ctx
->i32
= LLVMIntTypeInContext(ctx
->context
, 32);
543 ctx
->i64
= LLVMIntTypeInContext(ctx
->context
, 64);
544 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
545 ctx
->v3i32
= LLVMVectorType(ctx
->i32
, 3);
546 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
547 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
548 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
549 ctx
->f16
= LLVMHalfTypeInContext(ctx
->context
);
550 ctx
->v2f32
= LLVMVectorType(ctx
->f32
, 2);
551 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
552 ctx
->v16i8
= LLVMVectorType(ctx
->i8
, 16);
554 ctx
->i32zero
= LLVMConstInt(ctx
->i32
, 0, false);
555 ctx
->i32one
= LLVMConstInt(ctx
->i32
, 1, false);
556 ctx
->f32zero
= LLVMConstReal(ctx
->f32
, 0.0);
557 ctx
->f32one
= LLVMConstReal(ctx
->f32
, 1.0);
559 args
[0] = ctx
->f32zero
;
560 args
[1] = ctx
->f32zero
;
561 args
[2] = ctx
->f32zero
;
562 args
[3] = ctx
->f32one
;
563 ctx
->v4f32empty
= LLVMConstVector(args
, 4);
565 ctx
->range_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
567 ctx
->invariant_load_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
568 "invariant.load", 14);
569 ctx
->uniform_md_kind
=
570 LLVMGetMDKindIDInContext(ctx
->context
, "amdgpu.uniform", 14);
571 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
573 ctx
->fpmath_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
, "fpmath", 6);
575 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
576 ctx
->fpmath_md_2p5_ulp
= LLVMMDNodeInContext(ctx
->context
, args
, 1);
579 static int get_llvm_num_components(LLVMValueRef value
)
581 LLVMTypeRef type
= LLVMTypeOf(value
);
582 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
583 ? LLVMGetVectorSize(type
)
585 return num_components
;
588 static LLVMValueRef
llvm_extract_elem(struct nir_to_llvm_context
*ctx
,
592 int count
= get_llvm_num_components(value
);
594 assert(index
< count
);
598 return LLVMBuildExtractElement(ctx
->builder
, value
,
599 LLVMConstInt(ctx
->i32
, index
, false), "");
602 static LLVMValueRef
trim_vector(struct nir_to_llvm_context
*ctx
,
603 LLVMValueRef value
, unsigned count
)
605 unsigned num_components
= get_llvm_num_components(value
);
606 if (count
== num_components
)
609 LLVMValueRef masks
[] = {
610 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
611 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false)};
614 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
617 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
618 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
622 build_gather_values_extended(struct nir_to_llvm_context
*ctx
,
623 LLVMValueRef
*values
,
624 unsigned value_count
,
625 unsigned value_stride
,
628 LLVMBuilderRef builder
= ctx
->builder
;
633 if (value_count
== 1) {
635 return LLVMBuildLoad(builder
, values
[0], "");
639 for (i
= 0; i
< value_count
; i
++) {
640 LLVMValueRef value
= values
[i
* value_stride
];
642 value
= LLVMBuildLoad(builder
, value
, "");
645 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
646 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
647 vec
= LLVMBuildInsertElement(builder
, vec
, value
, index
, "");
654 build_store_values_extended(struct nir_to_llvm_context
*ctx
,
655 LLVMValueRef
*values
,
656 unsigned value_count
,
657 unsigned value_stride
,
660 LLVMBuilderRef builder
= ctx
->builder
;
663 if (value_count
== 1) {
664 LLVMBuildStore(builder
, vec
, values
[0]);
668 for (i
= 0; i
< value_count
; i
++) {
669 LLVMValueRef ptr
= values
[i
* value_stride
];
670 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
671 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
672 LLVMBuildStore(builder
, value
, ptr
);
677 build_gather_values(struct nir_to_llvm_context
*ctx
,
678 LLVMValueRef
*values
,
679 unsigned value_count
)
681 return build_gather_values_extended(ctx
, values
, value_count
, 1, false);
684 static LLVMTypeRef
get_def_type(struct nir_to_llvm_context
*ctx
,
687 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->context
, def
->bit_size
);
688 if (def
->num_components
> 1) {
689 type
= LLVMVectorType(type
, def
->num_components
);
694 static LLVMValueRef
get_src(struct nir_to_llvm_context
*ctx
, nir_src src
)
697 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->defs
, src
.ssa
);
698 return (LLVMValueRef
)entry
->data
;
702 static LLVMBasicBlockRef
get_block(struct nir_to_llvm_context
*ctx
,
705 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->defs
, b
);
706 return (LLVMBasicBlockRef
)entry
->data
;
709 static LLVMValueRef
get_alu_src(struct nir_to_llvm_context
*ctx
,
711 unsigned num_components
)
713 LLVMValueRef value
= get_src(ctx
, src
.src
);
714 bool need_swizzle
= false;
717 LLVMTypeRef type
= LLVMTypeOf(value
);
718 unsigned src_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
719 ? LLVMGetVectorSize(type
)
722 for (unsigned i
= 0; i
< num_components
; ++i
) {
723 assert(src
.swizzle
[i
] < src_components
);
724 if (src
.swizzle
[i
] != i
)
728 if (need_swizzle
|| num_components
!= src_components
) {
729 LLVMValueRef masks
[] = {
730 LLVMConstInt(ctx
->i32
, src
.swizzle
[0], false),
731 LLVMConstInt(ctx
->i32
, src
.swizzle
[1], false),
732 LLVMConstInt(ctx
->i32
, src
.swizzle
[2], false),
733 LLVMConstInt(ctx
->i32
, src
.swizzle
[3], false)};
735 if (src_components
> 1 && num_components
== 1) {
736 value
= LLVMBuildExtractElement(ctx
->builder
, value
,
738 } else if (src_components
== 1 && num_components
> 1) {
739 LLVMValueRef values
[] = {value
, value
, value
, value
};
740 value
= build_gather_values(ctx
, values
, num_components
);
742 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
743 value
= LLVMBuildShuffleVector(ctx
->builder
, value
, value
,
752 static LLVMValueRef
emit_int_cmp(struct nir_to_llvm_context
*ctx
,
753 LLVMIntPredicate pred
, LLVMValueRef src0
,
756 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
757 return LLVMBuildSelect(ctx
->builder
, result
,
758 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
759 LLVMConstInt(ctx
->i32
, 0, false), "");
762 static LLVMValueRef
emit_float_cmp(struct nir_to_llvm_context
*ctx
,
763 LLVMRealPredicate pred
, LLVMValueRef src0
,
767 src0
= to_float(ctx
, src0
);
768 src1
= to_float(ctx
, src1
);
769 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
770 return LLVMBuildSelect(ctx
->builder
, result
,
771 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
772 LLVMConstInt(ctx
->i32
, 0, false), "");
775 static LLVMValueRef
emit_intrin_1f_param(struct nir_to_llvm_context
*ctx
,
779 LLVMValueRef params
[] = {
782 return emit_llvm_intrinsic(ctx
, intrin
, ctx
->f32
, params
, 1, AC_FUNC_ATTR_READNONE
);
785 static LLVMValueRef
emit_intrin_2f_param(struct nir_to_llvm_context
*ctx
,
787 LLVMValueRef src0
, LLVMValueRef src1
)
789 LLVMValueRef params
[] = {
793 return emit_llvm_intrinsic(ctx
, intrin
, ctx
->f32
, params
, 2, AC_FUNC_ATTR_READNONE
);
796 static LLVMValueRef
emit_intrin_3f_param(struct nir_to_llvm_context
*ctx
,
798 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
800 LLVMValueRef params
[] = {
805 return emit_llvm_intrinsic(ctx
, intrin
, ctx
->f32
, params
, 3, AC_FUNC_ATTR_READNONE
);
808 static LLVMValueRef
emit_bcsel(struct nir_to_llvm_context
*ctx
,
809 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
811 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
813 return LLVMBuildSelect(ctx
->builder
, v
, src1
, src2
, "");
816 static LLVMValueRef
emit_find_lsb(struct nir_to_llvm_context
*ctx
,
819 LLVMValueRef params
[2] = {
822 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
823 * add special code to check for x=0. The reason is that
824 * the LLVM behavior for x=0 is different from what we
827 * The hardware already implements the correct behavior.
829 LLVMConstInt(ctx
->i32
, 1, false),
831 return emit_llvm_intrinsic(ctx
, "llvm.cttz.i32", ctx
->i32
, params
, 2, AC_FUNC_ATTR_READNONE
);
834 static LLVMValueRef
emit_ifind_msb(struct nir_to_llvm_context
*ctx
,
837 LLVMValueRef msb
= emit_llvm_intrinsic(ctx
, "llvm.AMDGPU.flbit.i32",
839 AC_FUNC_ATTR_READNONE
);
841 /* The HW returns the last bit index from MSB, but NIR wants
842 * the index from LSB. Invert it by doing "31 - msb". */
843 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
846 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
847 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
848 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
849 src0
, ctx
->i32zero
, ""),
850 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
851 src0
, all_ones
, ""), "");
853 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
856 static LLVMValueRef
emit_ufind_msb(struct nir_to_llvm_context
*ctx
,
859 LLVMValueRef args
[2] = {
863 LLVMValueRef msb
= emit_llvm_intrinsic(ctx
, "llvm.ctlz.i32",
864 ctx
->i32
, args
, ARRAY_SIZE(args
),
865 AC_FUNC_ATTR_READNONE
);
867 /* The HW returns the last bit index from MSB, but NIR wants
868 * the index from LSB. Invert it by doing "31 - msb". */
869 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
872 return LLVMBuildSelect(ctx
->builder
,
873 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src0
,
875 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
878 static LLVMValueRef
emit_minmax_int(struct nir_to_llvm_context
*ctx
,
879 LLVMIntPredicate pred
,
880 LLVMValueRef src0
, LLVMValueRef src1
)
882 return LLVMBuildSelect(ctx
->builder
,
883 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
888 static LLVMValueRef
emit_iabs(struct nir_to_llvm_context
*ctx
,
891 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
892 LLVMBuildNeg(ctx
->builder
, src0
, ""));
895 static LLVMValueRef
emit_fsign(struct nir_to_llvm_context
*ctx
,
898 LLVMValueRef cmp
, val
;
900 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, ctx
->f32zero
, "");
901 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->f32one
, src0
, "");
902 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, ctx
->f32zero
, "");
903 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(ctx
->f32
, -1.0), "");
907 static LLVMValueRef
emit_isign(struct nir_to_llvm_context
*ctx
,
910 LLVMValueRef cmp
, val
;
912 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, ctx
->i32zero
, "");
913 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->i32one
, src0
, "");
914 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, ctx
->i32zero
, "");
915 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(ctx
->i32
, -1, true), "");
919 static LLVMValueRef
emit_ffract(struct nir_to_llvm_context
*ctx
,
922 const char *intr
= "llvm.floor.f32";
923 LLVMValueRef fsrc0
= to_float(ctx
, src0
);
924 LLVMValueRef params
[] = {
927 LLVMValueRef floor
= emit_llvm_intrinsic(ctx
, intr
,
929 AC_FUNC_ATTR_READNONE
);
930 return LLVMBuildFSub(ctx
->builder
, fsrc0
, floor
, "");
933 static LLVMValueRef
emit_uint_carry(struct nir_to_llvm_context
*ctx
,
935 LLVMValueRef src0
, LLVMValueRef src1
)
937 LLVMTypeRef ret_type
;
938 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
940 LLVMValueRef params
[] = { src0
, src1
};
941 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
944 res
= emit_llvm_intrinsic(ctx
, intrin
, ret_type
,
945 params
, 2, AC_FUNC_ATTR_READNONE
);
947 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
948 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
952 static LLVMValueRef
emit_b2f(struct nir_to_llvm_context
*ctx
,
955 return LLVMBuildAnd(ctx
->builder
, src0
, LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""), "");
958 static LLVMValueRef
emit_umul_high(struct nir_to_llvm_context
*ctx
,
959 LLVMValueRef src0
, LLVMValueRef src1
)
961 LLVMValueRef dst64
, result
;
962 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
963 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
965 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
966 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
967 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
971 static LLVMValueRef
emit_imul_high(struct nir_to_llvm_context
*ctx
,
972 LLVMValueRef src0
, LLVMValueRef src1
)
974 LLVMValueRef dst64
, result
;
975 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
976 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
978 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
979 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
980 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
984 static LLVMValueRef
emit_bitfield_extract(struct nir_to_llvm_context
*ctx
,
986 LLVMValueRef srcs
[3])
989 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
990 result
= emit_llvm_intrinsic(ctx
, intrin
, ctx
->i32
, srcs
, 3, AC_FUNC_ATTR_READNONE
);
992 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
996 static LLVMValueRef
emit_bitfield_insert(struct nir_to_llvm_context
*ctx
,
997 LLVMValueRef src0
, LLVMValueRef src1
,
998 LLVMValueRef src2
, LLVMValueRef src3
)
1000 LLVMValueRef bfi_args
[3], result
;
1002 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
1003 LLVMBuildSub(ctx
->builder
,
1004 LLVMBuildShl(ctx
->builder
,
1009 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
1012 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
1015 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
1016 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
1018 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
1019 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
1020 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
1022 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
1026 static LLVMValueRef
emit_pack_half_2x16(struct nir_to_llvm_context
*ctx
,
1029 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1031 LLVMValueRef comp
[2];
1033 src0
= to_float(ctx
, src0
);
1034 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32zero
, "");
1035 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32one
, "");
1036 for (i
= 0; i
< 2; i
++) {
1037 comp
[i
] = LLVMBuildFPTrunc(ctx
->builder
, comp
[i
], ctx
->f16
, "");
1038 comp
[i
] = LLVMBuildBitCast(ctx
->builder
, comp
[i
], ctx
->i16
, "");
1039 comp
[i
] = LLVMBuildZExt(ctx
->builder
, comp
[i
], ctx
->i32
, "");
1042 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
1043 comp
[0] = LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
1048 static LLVMValueRef
emit_unpack_half_2x16(struct nir_to_llvm_context
*ctx
,
1051 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
1052 LLVMValueRef temps
[2], result
, val
;
1055 for (i
= 0; i
< 2; i
++) {
1056 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
1057 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
1058 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
1059 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
1062 result
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), temps
[0],
1064 result
= LLVMBuildInsertElement(ctx
->builder
, result
, temps
[1],
1070 * Set range metadata on an instruction. This can only be used on load and
1071 * call instructions. If you know an instruction can only produce the values
1072 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
1073 * \p lo is the minimum value inclusive.
1074 * \p hi is the maximum value exclusive.
1076 static void set_range_metadata(struct nir_to_llvm_context
*ctx
,
1077 LLVMValueRef value
, unsigned lo
, unsigned hi
)
1079 LLVMValueRef range_md
, md_args
[2];
1080 LLVMTypeRef type
= LLVMTypeOf(value
);
1081 LLVMContextRef context
= LLVMGetTypeContext(type
);
1083 md_args
[0] = LLVMConstInt(type
, lo
, false);
1084 md_args
[1] = LLVMConstInt(type
, hi
, false);
1085 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
1086 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
1089 static LLVMValueRef
get_thread_id(struct nir_to_llvm_context
*ctx
)
1092 LLVMValueRef tid_args
[2];
1093 tid_args
[0] = LLVMConstInt(ctx
->i32
, 0xffffffff, false);
1094 tid_args
[1] = ctx
->i32zero
;
1095 tid_args
[1] = emit_llvm_intrinsic(ctx
,
1096 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
1097 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
1099 tid
= emit_llvm_intrinsic(ctx
,
1100 "llvm.amdgcn.mbcnt.hi", ctx
->i32
,
1101 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
1102 set_range_metadata(ctx
, tid
, 0, 64);
1107 * SI implements derivatives using the local data store (LDS)
1108 * All writes to the LDS happen in all executing threads at
1109 * the same time. TID is the Thread ID for the current
1110 * thread and is a value between 0 and 63, representing
1111 * the thread's position in the wavefront.
1113 * For the pixel shader threads are grouped into quads of four pixels.
1114 * The TIDs of the pixels of a quad are:
1122 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
1123 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
1124 * the current pixel's column, and masking with 0xfffffffe yields the TID
1125 * of the left pixel of the current pixel's row.
1127 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
1128 * adding 2 yields the TID of the pixel below the top pixel.
1130 /* masks for thread ID. */
1131 #define TID_MASK_TOP_LEFT 0xfffffffc
1132 #define TID_MASK_TOP 0xfffffffd
1133 #define TID_MASK_LEFT 0xfffffffe
1134 static LLVMValueRef
emit_ddxy(struct nir_to_llvm_context
*ctx
,
1135 nir_alu_instr
*instr
,
1138 LLVMValueRef indices
[2];
1139 LLVMValueRef store_ptr
, load_ptr0
, load_ptr1
;
1140 LLVMValueRef tl
, trbl
, result
;
1141 LLVMValueRef tl_tid
, trbl_tid
;
1142 LLVMValueRef args
[2];
1145 ctx
->has_ddxy
= true;
1147 ctx
->lds
= LLVMAddGlobalInAddressSpace(ctx
->module
,
1148 LLVMArrayType(ctx
->i32
, 64),
1149 "ddxy_lds", LOCAL_ADDR_SPACE
);
1151 indices
[0] = ctx
->i32zero
;
1152 indices
[1] = get_thread_id(ctx
);
1153 store_ptr
= LLVMBuildGEP(ctx
->builder
, ctx
->lds
,
1156 if (instr
->op
== nir_op_fddx_fine
|| instr
->op
== nir_op_fddx
)
1157 mask
= TID_MASK_LEFT
;
1158 else if (instr
->op
== nir_op_fddy_fine
|| instr
->op
== nir_op_fddy
)
1159 mask
= TID_MASK_TOP
;
1161 mask
= TID_MASK_TOP_LEFT
;
1163 tl_tid
= LLVMBuildAnd(ctx
->builder
, indices
[1],
1164 LLVMConstInt(ctx
->i32
, mask
, false), "");
1165 indices
[1] = tl_tid
;
1166 load_ptr0
= LLVMBuildGEP(ctx
->builder
, ctx
->lds
,
1169 /* for DDX we want to next X pixel, DDY next Y pixel. */
1170 if (instr
->op
== nir_op_fddx_fine
||
1171 instr
->op
== nir_op_fddx_coarse
||
1172 instr
->op
== nir_op_fddx
)
1177 trbl_tid
= LLVMBuildAdd(ctx
->builder
, indices
[1],
1178 LLVMConstInt(ctx
->i32
, idx
, false), "");
1179 indices
[1] = trbl_tid
;
1180 load_ptr1
= LLVMBuildGEP(ctx
->builder
, ctx
->lds
,
1183 if (ctx
->options
->family
>= CHIP_TONGA
) {
1184 args
[0] = LLVMBuildMul(ctx
->builder
, tl_tid
,
1185 LLVMConstInt(ctx
->i32
, 4, false), "");
1187 tl
= emit_llvm_intrinsic(ctx
, "llvm.amdgcn.ds.bpermute",
1189 AC_FUNC_ATTR_READNONE
);
1191 args
[0] = LLVMBuildMul(ctx
->builder
, trbl_tid
,
1192 LLVMConstInt(ctx
->i32
, 4, false), "");
1193 trbl
= emit_llvm_intrinsic(ctx
, "llvm.amdgcn.ds.bpermute",
1195 AC_FUNC_ATTR_READNONE
);
1197 LLVMBuildStore(ctx
->builder
, src0
, store_ptr
);
1199 tl
= LLVMBuildLoad(ctx
->builder
, load_ptr0
, "");
1200 trbl
= LLVMBuildLoad(ctx
->builder
, load_ptr1
, "");
1202 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, ctx
->f32
, "");
1203 trbl
= LLVMBuildBitCast(ctx
->builder
, trbl
, ctx
->f32
, "");
1204 result
= LLVMBuildFSub(ctx
->builder
, trbl
, tl
, "");
1209 * this takes an I,J coordinate pair,
1210 * and works out the X and Y derivatives.
1211 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
1213 static LLVMValueRef
emit_ddxy_interp(
1214 struct nir_to_llvm_context
*ctx
,
1215 LLVMValueRef interp_ij
)
1217 LLVMValueRef indices
[2];
1218 LLVMValueRef store_ptr
, load_ptr_x
, load_ptr_y
, load_ptr_ddx
, load_ptr_ddy
, temp
, temp2
;
1219 LLVMValueRef tl
, tr
, bl
, result
[4];
1223 ctx
->lds
= LLVMAddGlobalInAddressSpace(ctx
->module
,
1224 LLVMArrayType(ctx
->i32
, 64),
1225 "ddxy_lds", LOCAL_ADDR_SPACE
);
1227 indices
[0] = ctx
->i32zero
;
1228 indices
[1] = get_thread_id(ctx
);
1229 store_ptr
= LLVMBuildGEP(ctx
->builder
, ctx
->lds
,
1232 temp
= LLVMBuildAnd(ctx
->builder
, indices
[1],
1233 LLVMConstInt(ctx
->i32
, TID_MASK_LEFT
, false), "");
1235 temp2
= LLVMBuildAnd(ctx
->builder
, indices
[1],
1236 LLVMConstInt(ctx
->i32
, TID_MASK_TOP
, false), "");
1239 load_ptr_x
= LLVMBuildGEP(ctx
->builder
, ctx
->lds
,
1243 load_ptr_y
= LLVMBuildGEP(ctx
->builder
, ctx
->lds
,
1246 indices
[1] = LLVMBuildAdd(ctx
->builder
, temp
,
1247 LLVMConstInt(ctx
->i32
, 1, false), "");
1248 load_ptr_ddx
= LLVMBuildGEP(ctx
->builder
, ctx
->lds
,
1251 indices
[1] = LLVMBuildAdd(ctx
->builder
, temp2
,
1252 LLVMConstInt(ctx
->i32
, 2, false), "");
1253 load_ptr_ddy
= LLVMBuildGEP(ctx
->builder
, ctx
->lds
,
1256 for (c
= 0; c
< 2; ++c
) {
1257 LLVMValueRef store_val
;
1258 LLVMValueRef c_ll
= LLVMConstInt(ctx
->i32
, c
, false);
1260 store_val
= LLVMBuildExtractElement(ctx
->builder
,
1261 interp_ij
, c_ll
, "");
1262 LLVMBuildStore(ctx
->builder
,
1266 tl
= LLVMBuildLoad(ctx
->builder
, load_ptr_x
, "");
1267 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, ctx
->f32
, "");
1269 tr
= LLVMBuildLoad(ctx
->builder
, load_ptr_ddx
, "");
1270 tr
= LLVMBuildBitCast(ctx
->builder
, tr
, ctx
->f32
, "");
1272 result
[c
] = LLVMBuildFSub(ctx
->builder
, tr
, tl
, "");
1274 tl
= LLVMBuildLoad(ctx
->builder
, load_ptr_y
, "");
1275 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, ctx
->f32
, "");
1277 bl
= LLVMBuildLoad(ctx
->builder
, load_ptr_ddy
, "");
1278 bl
= LLVMBuildBitCast(ctx
->builder
, bl
, ctx
->f32
, "");
1280 result
[c
+ 2] = LLVMBuildFSub(ctx
->builder
, bl
, tl
, "");
1283 return build_gather_values(ctx
, result
, 4);
1286 static LLVMValueRef
emit_fdiv(struct nir_to_llvm_context
*ctx
,
1290 LLVMValueRef ret
= LLVMBuildFDiv(ctx
->builder
, num
, den
, "");
1292 if (!LLVMIsConstant(ret
))
1293 LLVMSetMetadata(ret
, ctx
->fpmath_md_kind
, ctx
->fpmath_md_2p5_ulp
);
1297 static void visit_alu(struct nir_to_llvm_context
*ctx
, nir_alu_instr
*instr
)
1299 LLVMValueRef src
[4], result
= NULL
;
1300 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
1301 unsigned src_components
;
1303 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
1304 switch (instr
->op
) {
1310 case nir_op_pack_half_2x16
:
1313 case nir_op_unpack_half_2x16
:
1317 src_components
= num_components
;
1320 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1321 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
1323 switch (instr
->op
) {
1329 src
[0] = to_float(ctx
, src
[0]);
1330 result
= LLVMBuildFNeg(ctx
->builder
, src
[0], "");
1333 result
= LLVMBuildNeg(ctx
->builder
, src
[0], "");
1336 result
= LLVMBuildNot(ctx
->builder
, src
[0], "");
1339 result
= LLVMBuildAdd(ctx
->builder
, src
[0], src
[1], "");
1342 src
[0] = to_float(ctx
, src
[0]);
1343 src
[1] = to_float(ctx
, src
[1]);
1344 result
= LLVMBuildFAdd(ctx
->builder
, src
[0], src
[1], "");
1347 src
[0] = to_float(ctx
, src
[0]);
1348 src
[1] = to_float(ctx
, src
[1]);
1349 result
= LLVMBuildFSub(ctx
->builder
, src
[0], src
[1], "");
1352 result
= LLVMBuildSub(ctx
->builder
, src
[0], src
[1], "");
1355 result
= LLVMBuildMul(ctx
->builder
, src
[0], src
[1], "");
1358 result
= LLVMBuildSRem(ctx
->builder
, src
[0], src
[1], "");
1361 result
= LLVMBuildURem(ctx
->builder
, src
[0], src
[1], "");
1364 src
[0] = to_float(ctx
, src
[0]);
1365 src
[1] = to_float(ctx
, src
[1]);
1366 result
= emit_fdiv(ctx
, src
[0], src
[1]);
1367 result
= emit_intrin_1f_param(ctx
, "llvm.floor.f32", result
);
1368 result
= LLVMBuildFMul(ctx
->builder
, src
[1] , result
, "");
1369 result
= LLVMBuildFSub(ctx
->builder
, src
[0], result
, "");
1372 src
[0] = to_float(ctx
, src
[0]);
1373 src
[1] = to_float(ctx
, src
[1]);
1374 result
= LLVMBuildFRem(ctx
->builder
, src
[0], src
[1], "");
1377 result
= LLVMBuildSDiv(ctx
->builder
, src
[0], src
[1], "");
1380 result
= LLVMBuildUDiv(ctx
->builder
, src
[0], src
[1], "");
1383 src
[0] = to_float(ctx
, src
[0]);
1384 src
[1] = to_float(ctx
, src
[1]);
1385 result
= LLVMBuildFMul(ctx
->builder
, src
[0], src
[1], "");
1388 src
[0] = to_float(ctx
, src
[0]);
1389 src
[1] = to_float(ctx
, src
[1]);
1390 result
= emit_fdiv(ctx
, src
[0], src
[1]);
1393 src
[0] = to_float(ctx
, src
[0]);
1394 result
= emit_fdiv(ctx
, ctx
->f32one
, src
[0]);
1397 result
= LLVMBuildAnd(ctx
->builder
, src
[0], src
[1], "");
1400 result
= LLVMBuildOr(ctx
->builder
, src
[0], src
[1], "");
1403 result
= LLVMBuildXor(ctx
->builder
, src
[0], src
[1], "");
1406 result
= LLVMBuildShl(ctx
->builder
, src
[0], src
[1], "");
1409 result
= LLVMBuildAShr(ctx
->builder
, src
[0], src
[1], "");
1412 result
= LLVMBuildLShr(ctx
->builder
, src
[0], src
[1], "");
1415 result
= emit_int_cmp(ctx
, LLVMIntSLT
, src
[0], src
[1]);
1418 result
= emit_int_cmp(ctx
, LLVMIntNE
, src
[0], src
[1]);
1421 result
= emit_int_cmp(ctx
, LLVMIntEQ
, src
[0], src
[1]);
1424 result
= emit_int_cmp(ctx
, LLVMIntSGE
, src
[0], src
[1]);
1427 result
= emit_int_cmp(ctx
, LLVMIntULT
, src
[0], src
[1]);
1430 result
= emit_int_cmp(ctx
, LLVMIntUGE
, src
[0], src
[1]);
1433 result
= emit_float_cmp(ctx
, LLVMRealUEQ
, src
[0], src
[1]);
1436 result
= emit_float_cmp(ctx
, LLVMRealUNE
, src
[0], src
[1]);
1439 result
= emit_float_cmp(ctx
, LLVMRealULT
, src
[0], src
[1]);
1442 result
= emit_float_cmp(ctx
, LLVMRealUGE
, src
[0], src
[1]);
1445 result
= emit_intrin_1f_param(ctx
, "llvm.fabs.f32", src
[0]);
1448 result
= emit_iabs(ctx
, src
[0]);
1451 result
= emit_minmax_int(ctx
, LLVMIntSGT
, src
[0], src
[1]);
1454 result
= emit_minmax_int(ctx
, LLVMIntSLT
, src
[0], src
[1]);
1457 result
= emit_minmax_int(ctx
, LLVMIntUGT
, src
[0], src
[1]);
1460 result
= emit_minmax_int(ctx
, LLVMIntULT
, src
[0], src
[1]);
1463 result
= emit_isign(ctx
, src
[0]);
1466 src
[0] = to_float(ctx
, src
[0]);
1467 result
= emit_fsign(ctx
, src
[0]);
1470 result
= emit_intrin_1f_param(ctx
, "llvm.floor.f32", src
[0]);
1473 result
= emit_intrin_1f_param(ctx
, "llvm.trunc.f32", src
[0]);
1476 result
= emit_intrin_1f_param(ctx
, "llvm.ceil.f32", src
[0]);
1478 case nir_op_fround_even
:
1479 result
= emit_intrin_1f_param(ctx
, "llvm.rint.f32", src
[0]);
1482 result
= emit_ffract(ctx
, src
[0]);
1485 result
= emit_intrin_1f_param(ctx
, "llvm.sin.f32", src
[0]);
1488 result
= emit_intrin_1f_param(ctx
, "llvm.cos.f32", src
[0]);
1491 result
= emit_intrin_1f_param(ctx
, "llvm.sqrt.f32", src
[0]);
1494 result
= emit_intrin_1f_param(ctx
, "llvm.exp2.f32", src
[0]);
1497 result
= emit_intrin_1f_param(ctx
, "llvm.log2.f32", src
[0]);
1500 result
= emit_intrin_1f_param(ctx
, "llvm.sqrt.f32", src
[0]);
1501 result
= emit_fdiv(ctx
, ctx
->f32one
, result
);
1504 result
= emit_intrin_2f_param(ctx
, "llvm.pow.f32", src
[0], src
[1]);
1507 result
= emit_intrin_2f_param(ctx
, "llvm.maxnum.f32", src
[0], src
[1]);
1510 result
= emit_intrin_2f_param(ctx
, "llvm.minnum.f32", src
[0], src
[1]);
1513 result
= emit_intrin_3f_param(ctx
, "llvm.fma.f32", src
[0], src
[1], src
[2]);
1515 case nir_op_ibitfield_extract
:
1516 result
= emit_bitfield_extract(ctx
, "llvm.AMDGPU.bfe.i32", src
);
1518 case nir_op_ubitfield_extract
:
1519 result
= emit_bitfield_extract(ctx
, "llvm.AMDGPU.bfe.u32", src
);
1521 case nir_op_bitfield_insert
:
1522 result
= emit_bitfield_insert(ctx
, src
[0], src
[1], src
[2], src
[3]);
1524 case nir_op_bitfield_reverse
:
1525 result
= emit_llvm_intrinsic(ctx
, "llvm.bitreverse.i32", ctx
->i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1527 case nir_op_bit_count
:
1528 result
= emit_llvm_intrinsic(ctx
, "llvm.ctpop.i32", ctx
->i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
1533 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1534 src
[i
] = to_integer(ctx
, src
[i
]);
1535 result
= build_gather_values(ctx
, src
, num_components
);
1538 src
[0] = to_float(ctx
, src
[0]);
1539 result
= LLVMBuildFPToSI(ctx
->builder
, src
[0], ctx
->i32
, "");
1542 src
[0] = to_float(ctx
, src
[0]);
1543 result
= LLVMBuildFPToUI(ctx
->builder
, src
[0], ctx
->i32
, "");
1546 result
= LLVMBuildSIToFP(ctx
->builder
, src
[0], ctx
->f32
, "");
1549 result
= LLVMBuildUIToFP(ctx
->builder
, src
[0], ctx
->f32
, "");
1552 result
= emit_bcsel(ctx
, src
[0], src
[1], src
[2]);
1554 case nir_op_find_lsb
:
1555 result
= emit_find_lsb(ctx
, src
[0]);
1557 case nir_op_ufind_msb
:
1558 result
= emit_ufind_msb(ctx
, src
[0]);
1560 case nir_op_ifind_msb
:
1561 result
= emit_ifind_msb(ctx
, src
[0]);
1563 case nir_op_uadd_carry
:
1564 result
= emit_uint_carry(ctx
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
1566 case nir_op_usub_borrow
:
1567 result
= emit_uint_carry(ctx
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
1570 result
= emit_b2f(ctx
, src
[0]);
1572 case nir_op_fquantize2f16
:
1573 src
[0] = to_float(ctx
, src
[0]);
1574 result
= LLVMBuildFPTrunc(ctx
->builder
, src
[0], ctx
->f16
, "");
1575 /* need to convert back up to f32 */
1576 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
1578 case nir_op_umul_high
:
1579 result
= emit_umul_high(ctx
, src
[0], src
[1]);
1581 case nir_op_imul_high
:
1582 result
= emit_imul_high(ctx
, src
[0], src
[1]);
1584 case nir_op_pack_half_2x16
:
1585 result
= emit_pack_half_2x16(ctx
, src
[0]);
1587 case nir_op_unpack_half_2x16
:
1588 result
= emit_unpack_half_2x16(ctx
, src
[0]);
1592 case nir_op_fddx_fine
:
1593 case nir_op_fddy_fine
:
1594 case nir_op_fddx_coarse
:
1595 case nir_op_fddy_coarse
:
1596 result
= emit_ddxy(ctx
, instr
, src
[0]);
1599 fprintf(stderr
, "Unknown NIR alu instr: ");
1600 nir_print_instr(&instr
->instr
, stderr
);
1601 fprintf(stderr
, "\n");
1606 assert(instr
->dest
.dest
.is_ssa
);
1607 result
= to_integer(ctx
, result
);
1608 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.dest
.ssa
,
1613 static void visit_load_const(struct nir_to_llvm_context
*ctx
,
1614 nir_load_const_instr
*instr
)
1616 LLVMValueRef values
[4], value
= NULL
;
1617 LLVMTypeRef element_type
=
1618 LLVMIntTypeInContext(ctx
->context
, instr
->def
.bit_size
);
1620 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1621 switch (instr
->def
.bit_size
) {
1623 values
[i
] = LLVMConstInt(element_type
,
1624 instr
->value
.u32
[i
], false);
1627 values
[i
] = LLVMConstInt(element_type
,
1628 instr
->value
.u64
[i
], false);
1632 "unsupported nir load_const bit_size: %d\n",
1633 instr
->def
.bit_size
);
1637 if (instr
->def
.num_components
> 1) {
1638 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1642 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, value
);
1645 static LLVMValueRef
cast_ptr(struct nir_to_llvm_context
*ctx
, LLVMValueRef ptr
,
1648 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
1649 return LLVMBuildBitCast(ctx
->builder
, ptr
,
1650 LLVMPointerType(type
, addr_space
), "");
1654 emit_llvm_intrinsic(struct nir_to_llvm_context
*ctx
, const char *name
,
1655 LLVMTypeRef return_type
, LLVMValueRef
*params
,
1656 unsigned param_count
, unsigned attrib_mask
)
1658 LLVMValueRef function
;
1660 function
= LLVMGetNamedFunction(ctx
->module
, name
);
1662 LLVMTypeRef param_types
[32], function_type
;
1665 assert(param_count
<= 32);
1667 for (i
= 0; i
< param_count
; ++i
) {
1669 param_types
[i
] = LLVMTypeOf(params
[i
]);
1672 LLVMFunctionType(return_type
, param_types
, param_count
, 0);
1673 function
= LLVMAddFunction(ctx
->module
, name
, function_type
);
1675 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
1676 LLVMSetLinkage(function
, LLVMExternalLinkage
);
1678 ac_add_function_attr(function
, 0, attrib_mask
| AC_FUNC_ATTR_NOUNWIND
);
1680 return LLVMBuildCall(ctx
->builder
, function
, params
, param_count
, "");
1684 get_buffer_size(struct nir_to_llvm_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1687 LLVMBuildExtractElement(ctx
->builder
, descriptor
,
1688 LLVMConstInt(ctx
->i32
, 2, false), "");
1691 if (ctx
->options
->chip_class
>= VI
&& in_elements
) {
1692 /* On VI, the descriptor contains the size in bytes,
1693 * but TXQ must return the size in elements.
1694 * The stride is always non-zero for resources using TXQ.
1696 LLVMValueRef stride
=
1697 LLVMBuildExtractElement(ctx
->builder
, descriptor
,
1698 LLVMConstInt(ctx
->i32
, 1, false), "");
1699 stride
= LLVMBuildLShr(ctx
->builder
, stride
,
1700 LLVMConstInt(ctx
->i32
, 16, false), "");
1701 stride
= LLVMBuildAnd(ctx
->builder
, stride
,
1702 LLVMConstInt(ctx
->i32
, 0x3fff, false), "");
1704 size
= LLVMBuildUDiv(ctx
->builder
, size
, stride
, "");
1710 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
1713 static void build_int_type_name(
1715 char *buf
, unsigned bufsize
)
1717 assert(bufsize
>= 6);
1719 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
1720 snprintf(buf
, bufsize
, "v%ui32",
1721 LLVMGetVectorSize(type
));
1726 static LLVMValueRef
radv_lower_gather4_integer(struct nir_to_llvm_context
*ctx
,
1727 struct ac_tex_info
*tinfo
,
1728 nir_tex_instr
*instr
,
1729 const char *intr_name
,
1730 unsigned coord_vgpr_index
)
1732 LLVMValueRef coord
= tinfo
->args
[0];
1733 LLVMValueRef half_texel
[2];
1738 LLVMValueRef txq_args
[10];
1739 int txq_arg_count
= 0;
1741 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
1742 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, false);
1743 txq_args
[txq_arg_count
++] = tinfo
->args
[1];
1744 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0xf, 0); /* dmask */
1745 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* unorm */
1746 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* r128 */
1747 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, da
? 1 : 0, 0);
1748 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* glc */
1749 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* slc */
1750 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* tfe */
1751 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* lwe */
1752 size
= emit_llvm_intrinsic(ctx
, "llvm.SI.getresinfo.i32", ctx
->v4i32
,
1753 txq_args
, txq_arg_count
,
1754 AC_FUNC_ATTR_READNONE
);
1756 for (c
= 0; c
< 2; c
++) {
1757 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
1759 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1760 half_texel
[c
] = emit_fdiv(ctx
, ctx
->f32one
, half_texel
[c
]);
1761 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1762 LLVMConstReal(ctx
->f32
, -0.5), "");
1766 for (c
= 0; c
< 2; c
++) {
1768 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, coord_vgpr_index
+ c
, 0);
1769 tmp
= LLVMBuildExtractElement(ctx
->builder
, coord
, index
, "");
1770 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1771 tmp
= LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
1772 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1773 coord
= LLVMBuildInsertElement(ctx
->builder
, coord
, tmp
, index
, "");
1776 tinfo
->args
[0] = coord
;
1777 return emit_llvm_intrinsic(ctx
, intr_name
, tinfo
->dst_type
, tinfo
->args
, tinfo
->arg_count
,
1778 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_NOUNWIND
);
1782 static LLVMValueRef
build_tex_intrinsic(struct nir_to_llvm_context
*ctx
,
1783 nir_tex_instr
*instr
,
1784 struct ac_tex_info
*tinfo
)
1786 const char *name
= "llvm.SI.image.sample";
1787 const char *infix
= "";
1788 char intr_name
[127];
1790 bool is_shadow
= instr
->is_shadow
;
1791 bool has_offset
= tinfo
->has_offset
;
1792 switch (instr
->op
) {
1794 case nir_texop_txf_ms
:
1795 case nir_texop_samples_identical
:
1796 name
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
? "llvm.SI.image.load" :
1797 instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
? "llvm.SI.vs.load.input" :
1798 "llvm.SI.image.load.mip";
1809 name
= "llvm.SI.getresinfo";
1811 case nir_texop_query_levels
:
1812 name
= "llvm.SI.getresinfo";
1815 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
1822 name
= "llvm.SI.gather4";
1826 name
= "llvm.SI.getlod";
1834 build_int_type_name(LLVMTypeOf(tinfo
->args
[0]), type
, sizeof(type
));
1835 sprintf(intr_name
, "%s%s%s%s.%s", name
, is_shadow
? ".c" : "", infix
,
1836 has_offset
? ".o" : "", type
);
1838 if (instr
->op
== nir_texop_tg4
) {
1839 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
1840 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
1841 return radv_lower_gather4_integer(ctx
, tinfo
, instr
, intr_name
,
1842 (int)has_offset
+ (int)is_shadow
);
1845 return emit_llvm_intrinsic(ctx
, intr_name
, tinfo
->dst_type
, tinfo
->args
, tinfo
->arg_count
,
1846 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_NOUNWIND
);
1850 static LLVMValueRef
visit_vulkan_resource_index(struct nir_to_llvm_context
*ctx
,
1851 nir_intrinsic_instr
*instr
)
1853 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
1854 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
1855 unsigned binding
= nir_intrinsic_binding(instr
);
1856 LLVMValueRef desc_ptr
= ctx
->descriptor_sets
[desc_set
];
1857 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[desc_set
].layout
;
1858 unsigned base_offset
= layout
->binding
[binding
].offset
;
1859 LLVMValueRef offset
, stride
;
1861 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
1862 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
1863 desc_ptr
= ctx
->push_constants
;
1864 base_offset
= ctx
->options
->layout
->push_constant_size
;
1865 base_offset
+= 16 * layout
->binding
[binding
].dynamic_offset_offset
;
1866 stride
= LLVMConstInt(ctx
->i32
, 16, false);
1868 stride
= LLVMConstInt(ctx
->i32
, layout
->binding
[binding
].size
, false);
1870 offset
= LLVMConstInt(ctx
->i32
, base_offset
, false);
1871 index
= LLVMBuildMul(ctx
->builder
, index
, stride
, "");
1872 offset
= LLVMBuildAdd(ctx
->builder
, offset
, index
, "");
1874 LLVMValueRef indices
[] = {ctx
->i32zero
, offset
};
1875 desc_ptr
= LLVMBuildGEP(ctx
->builder
, desc_ptr
, indices
, 2, "");
1876 desc_ptr
= cast_ptr(ctx
, desc_ptr
, ctx
->v4i32
);
1877 LLVMSetMetadata(desc_ptr
, ctx
->uniform_md_kind
, ctx
->empty_md
);
1879 return LLVMBuildLoad(ctx
->builder
, desc_ptr
, "");
1882 static LLVMValueRef
visit_load_push_constant(struct nir_to_llvm_context
*ctx
,
1883 nir_intrinsic_instr
*instr
)
1887 LLVMValueRef indices
[] = {ctx
->i32zero
, get_src(ctx
, instr
->src
[0])};
1888 ptr
= LLVMBuildGEP(ctx
->builder
, ctx
->push_constants
, indices
, 2, "");
1889 ptr
= cast_ptr(ctx
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
1891 return LLVMBuildLoad(ctx
->builder
, ptr
, "");
1894 static LLVMValueRef
visit_get_buffer_size(struct nir_to_llvm_context
*ctx
,
1895 nir_intrinsic_instr
*instr
)
1897 LLVMValueRef desc
= get_src(ctx
, instr
->src
[0]);
1899 return get_buffer_size(ctx
, desc
, false);
1901 static void visit_store_ssbo(struct nir_to_llvm_context
*ctx
,
1902 nir_intrinsic_instr
*instr
)
1904 const char *store_name
;
1905 LLVMTypeRef data_type
= ctx
->f32
;
1906 unsigned writemask
= nir_intrinsic_write_mask(instr
);
1907 LLVMValueRef base_data
, base_offset
;
1908 LLVMValueRef params
[6];
1910 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
1911 ctx
->shader_info
->fs
.writes_memory
= true;
1913 params
[1] = get_src(ctx
, instr
->src
[1]);
1914 params
[2] = LLVMConstInt(ctx
->i32
, 0, false); /* vindex */
1915 params
[4] = LLVMConstInt(ctx
->i1
, 0, false); /* glc */
1916 params
[5] = LLVMConstInt(ctx
->i1
, 0, false); /* slc */
1918 if (instr
->num_components
> 1)
1919 data_type
= LLVMVectorType(ctx
->f32
, instr
->num_components
);
1921 base_data
= to_float(ctx
, get_src(ctx
, instr
->src
[0]));
1922 base_data
= trim_vector(ctx
, base_data
, instr
->num_components
);
1923 base_data
= LLVMBuildBitCast(ctx
->builder
, base_data
,
1925 base_offset
= get_src(ctx
, instr
->src
[2]); /* voffset */
1929 LLVMValueRef offset
;
1931 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
1933 /* Due to an LLVM limitation, split 3-element writes
1934 * into a 2-element and a 1-element write. */
1936 writemask
|= 1 << (start
+ 2);
1941 store_name
= "llvm.amdgcn.buffer.store.v4f32";
1943 } else if (count
== 2) {
1944 tmp
= LLVMBuildExtractElement(ctx
->builder
,
1945 base_data
, LLVMConstInt(ctx
->i32
, start
, false), "");
1946 data
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), tmp
,
1949 tmp
= LLVMBuildExtractElement(ctx
->builder
,
1950 base_data
, LLVMConstInt(ctx
->i32
, start
+ 1, false), "");
1951 data
= LLVMBuildInsertElement(ctx
->builder
, data
, tmp
,
1953 store_name
= "llvm.amdgcn.buffer.store.v2f32";
1957 if (get_llvm_num_components(base_data
) > 1)
1958 data
= LLVMBuildExtractElement(ctx
->builder
, base_data
,
1959 LLVMConstInt(ctx
->i32
, start
, false), "");
1962 store_name
= "llvm.amdgcn.buffer.store.f32";
1965 offset
= base_offset
;
1967 offset
= LLVMBuildAdd(ctx
->builder
, offset
, LLVMConstInt(ctx
->i32
, start
* 4, false), "");
1971 emit_llvm_intrinsic(ctx
, store_name
,
1972 LLVMVoidTypeInContext(ctx
->context
), params
, 6, 0);
1976 static LLVMValueRef
visit_atomic_ssbo(struct nir_to_llvm_context
*ctx
,
1977 nir_intrinsic_instr
*instr
)
1980 LLVMValueRef params
[5];
1982 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
1983 ctx
->shader_info
->fs
.writes_memory
= true;
1985 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
1986 params
[arg_count
++] = get_src(ctx
, instr
->src
[3]);
1988 params
[arg_count
++] = get_src(ctx
, instr
->src
[2]);
1989 params
[arg_count
++] = get_src(ctx
, instr
->src
[0]);
1990 params
[arg_count
++] = LLVMConstInt(ctx
->i32
, 0, false); /* vindex */
1991 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1992 params
[arg_count
++] = LLVMConstInt(ctx
->i1
, 0, false); /* slc */
1994 switch (instr
->intrinsic
) {
1995 case nir_intrinsic_ssbo_atomic_add
:
1996 name
= "llvm.amdgcn.buffer.atomic.add";
1998 case nir_intrinsic_ssbo_atomic_imin
:
1999 name
= "llvm.amdgcn.buffer.atomic.smin";
2001 case nir_intrinsic_ssbo_atomic_umin
:
2002 name
= "llvm.amdgcn.buffer.atomic.umin";
2004 case nir_intrinsic_ssbo_atomic_imax
:
2005 name
= "llvm.amdgcn.buffer.atomic.smax";
2007 case nir_intrinsic_ssbo_atomic_umax
:
2008 name
= "llvm.amdgcn.buffer.atomic.umax";
2010 case nir_intrinsic_ssbo_atomic_and
:
2011 name
= "llvm.amdgcn.buffer.atomic.and";
2013 case nir_intrinsic_ssbo_atomic_or
:
2014 name
= "llvm.amdgcn.buffer.atomic.or";
2016 case nir_intrinsic_ssbo_atomic_xor
:
2017 name
= "llvm.amdgcn.buffer.atomic.xor";
2019 case nir_intrinsic_ssbo_atomic_exchange
:
2020 name
= "llvm.amdgcn.buffer.atomic.swap";
2022 case nir_intrinsic_ssbo_atomic_comp_swap
:
2023 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
2029 return emit_llvm_intrinsic(ctx
, name
, ctx
->i32
, params
, arg_count
, 0);
2032 static LLVMValueRef
visit_load_buffer(struct nir_to_llvm_context
*ctx
,
2033 nir_intrinsic_instr
*instr
)
2035 const char *load_name
;
2036 LLVMTypeRef data_type
= ctx
->f32
;
2037 if (instr
->num_components
== 3)
2038 data_type
= LLVMVectorType(ctx
->f32
, 4);
2039 else if (instr
->num_components
> 1)
2040 data_type
= LLVMVectorType(ctx
->f32
, instr
->num_components
);
2042 if (instr
->num_components
== 4 || instr
->num_components
== 3)
2043 load_name
= "llvm.amdgcn.buffer.load.v4f32";
2044 else if (instr
->num_components
== 2)
2045 load_name
= "llvm.amdgcn.buffer.load.v2f32";
2046 else if (instr
->num_components
== 1)
2047 load_name
= "llvm.amdgcn.buffer.load.f32";
2051 LLVMValueRef params
[] = {
2052 get_src(ctx
, instr
->src
[0]),
2053 LLVMConstInt(ctx
->i32
, 0, false),
2054 get_src(ctx
, instr
->src
[1]),
2055 LLVMConstInt(ctx
->i1
, 0, false),
2056 LLVMConstInt(ctx
->i1
, 0, false),
2060 emit_llvm_intrinsic(ctx
, load_name
, data_type
, params
, 5, 0);
2062 if (instr
->num_components
== 3)
2063 ret
= trim_vector(ctx
, ret
, 3);
2065 return LLVMBuildBitCast(ctx
->builder
, ret
,
2066 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2070 radv_get_deref_offset(struct nir_to_llvm_context
*ctx
, nir_deref
*tail
,
2071 bool vs_in
, unsigned *const_out
, LLVMValueRef
*indir_out
)
2073 unsigned const_offset
= 0;
2074 LLVMValueRef offset
= NULL
;
2077 while (tail
->child
!= NULL
) {
2078 const struct glsl_type
*parent_type
= tail
->type
;
2081 if (tail
->deref_type
== nir_deref_type_array
) {
2082 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2083 LLVMValueRef index
, stride
, local_offset
;
2084 unsigned size
= glsl_count_attribute_slots(tail
->type
, vs_in
);
2086 const_offset
+= size
* deref_array
->base_offset
;
2087 if (deref_array
->deref_array_type
== nir_deref_array_type_direct
)
2090 assert(deref_array
->deref_array_type
== nir_deref_array_type_indirect
);
2091 index
= get_src(ctx
, deref_array
->indirect
);
2092 stride
= LLVMConstInt(ctx
->i32
, size
, 0);
2093 local_offset
= LLVMBuildMul(ctx
->builder
, stride
, index
, "");
2096 offset
= LLVMBuildAdd(ctx
->builder
, offset
, local_offset
, "");
2098 offset
= local_offset
;
2099 } else if (tail
->deref_type
== nir_deref_type_struct
) {
2100 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
2102 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
2103 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
2104 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
2107 unreachable("unsupported deref type");
2111 if (const_offset
&& offset
)
2112 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
2113 LLVMConstInt(ctx
->i32
, const_offset
, 0),
2116 *const_out
= const_offset
;
2117 *indir_out
= offset
;
2120 static LLVMValueRef
visit_load_var(struct nir_to_llvm_context
*ctx
,
2121 nir_intrinsic_instr
*instr
)
2123 LLVMValueRef values
[4];
2124 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2125 int ve
= instr
->dest
.ssa
.num_components
;
2126 LLVMValueRef indir_index
;
2127 unsigned const_index
;
2128 switch (instr
->variables
[0]->var
->data
.mode
) {
2129 case nir_var_shader_in
:
2130 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
,
2131 ctx
->stage
== MESA_SHADER_VERTEX
,
2132 &const_index
, &indir_index
);
2133 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2135 unsigned count
= glsl_count_attribute_slots(
2136 instr
->variables
[0]->var
->type
,
2137 ctx
->stage
== MESA_SHADER_VERTEX
);
2138 LLVMValueRef tmp_vec
= build_gather_values_extended(
2139 ctx
, ctx
->inputs
+ idx
+ chan
, count
,
2142 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2146 values
[chan
] = ctx
->inputs
[idx
+ chan
+ const_index
* 4];
2148 return to_integer(ctx
, build_gather_values(ctx
, values
, ve
));
2151 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2152 &const_index
, &indir_index
);
2153 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2155 unsigned count
= glsl_count_attribute_slots(
2156 instr
->variables
[0]->var
->type
, false);
2157 LLVMValueRef tmp_vec
= build_gather_values_extended(
2158 ctx
, ctx
->locals
+ idx
+ chan
, count
,
2161 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2165 values
[chan
] = LLVMBuildLoad(ctx
->builder
, ctx
->locals
[idx
+ chan
+ const_index
* 4], "");
2168 return to_integer(ctx
, build_gather_values(ctx
, values
, ve
));
2169 case nir_var_shader_out
:
2170 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2171 &const_index
, &indir_index
);
2172 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2174 unsigned count
= glsl_count_attribute_slots(
2175 instr
->variables
[0]->var
->type
, false);
2176 LLVMValueRef tmp_vec
= build_gather_values_extended(
2177 ctx
, ctx
->outputs
+ idx
+ chan
, count
,
2180 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2184 values
[chan
] = LLVMBuildLoad(ctx
->builder
,
2185 ctx
->outputs
[idx
+ chan
+ const_index
* 4],
2189 return to_integer(ctx
, build_gather_values(ctx
, values
, ve
));
2190 case nir_var_shared
: {
2191 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2192 &const_index
, &indir_index
);
2193 LLVMValueRef ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
2194 LLVMValueRef derived_ptr
;
2195 LLVMValueRef index
= ctx
->i32zero
;
2197 index
= LLVMBuildAdd(ctx
->builder
, index
, indir_index
, "");
2198 derived_ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, "");
2200 return to_integer(ctx
, LLVMBuildLoad(ctx
->builder
, derived_ptr
, ""));
2210 visit_store_var(struct nir_to_llvm_context
*ctx
,
2211 nir_intrinsic_instr
*instr
)
2213 LLVMValueRef temp_ptr
, value
;
2214 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2215 LLVMValueRef src
= to_float(ctx
, get_src(ctx
, instr
->src
[0]));
2216 int writemask
= instr
->const_index
[0];
2217 LLVMValueRef indir_index
;
2218 unsigned const_index
;
2219 switch (instr
->variables
[0]->var
->data
.mode
) {
2220 case nir_var_shader_out
:
2221 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2222 &const_index
, &indir_index
);
2223 for (unsigned chan
= 0; chan
< 4; chan
++) {
2225 if (!(writemask
& (1 << chan
)))
2227 if (get_llvm_num_components(src
) == 1)
2230 value
= LLVMBuildExtractElement(ctx
->builder
, src
,
2231 LLVMConstInt(ctx
->i32
,
2235 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CLIP_DIST0
||
2236 instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CULL_DIST0
)
2239 unsigned count
= glsl_count_attribute_slots(
2240 instr
->variables
[0]->var
->type
, false);
2241 LLVMValueRef tmp_vec
= build_gather_values_extended(
2242 ctx
, ctx
->outputs
+ idx
+ chan
, count
,
2245 if (get_llvm_num_components(tmp_vec
) > 1) {
2246 tmp_vec
= LLVMBuildInsertElement(ctx
->builder
, tmp_vec
,
2247 value
, indir_index
, "");
2250 build_store_values_extended(ctx
, ctx
->outputs
+ idx
+ chan
,
2251 count
, stride
, tmp_vec
);
2254 temp_ptr
= ctx
->outputs
[idx
+ chan
+ const_index
* stride
];
2256 LLVMBuildStore(ctx
->builder
, value
, temp_ptr
);
2261 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2262 &const_index
, &indir_index
);
2263 for (unsigned chan
= 0; chan
< 4; chan
++) {
2264 if (!(writemask
& (1 << chan
)))
2267 if (get_llvm_num_components(src
) == 1)
2270 value
= LLVMBuildExtractElement(ctx
->builder
, src
,
2271 LLVMConstInt(ctx
->i32
, chan
, false), "");
2273 unsigned count
= glsl_count_attribute_slots(
2274 instr
->variables
[0]->var
->type
, false);
2275 LLVMValueRef tmp_vec
= build_gather_values_extended(
2276 ctx
, ctx
->locals
+ idx
+ chan
, count
,
2279 tmp_vec
= LLVMBuildInsertElement(ctx
->builder
, tmp_vec
,
2280 value
, indir_index
, "");
2281 build_store_values_extended(ctx
, ctx
->locals
+ idx
+ chan
,
2284 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
2286 LLVMBuildStore(ctx
->builder
, value
, temp_ptr
);
2290 case nir_var_shared
: {
2292 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2293 &const_index
, &indir_index
);
2295 ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
2296 LLVMValueRef index
= ctx
->i32zero
;
2297 LLVMValueRef derived_ptr
;
2300 index
= LLVMBuildAdd(ctx
->builder
, index
, indir_index
, "");
2301 derived_ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, "");
2302 LLVMBuildStore(ctx
->builder
,
2303 to_integer(ctx
, src
), derived_ptr
);
2311 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
2314 case GLSL_SAMPLER_DIM_BUF
:
2316 case GLSL_SAMPLER_DIM_1D
:
2317 return array
? 2 : 1;
2318 case GLSL_SAMPLER_DIM_2D
:
2319 return array
? 3 : 2;
2320 case GLSL_SAMPLER_DIM_3D
:
2321 case GLSL_SAMPLER_DIM_CUBE
:
2323 case GLSL_SAMPLER_DIM_RECT
:
2324 case GLSL_SAMPLER_DIM_SUBPASS
:
2332 static LLVMValueRef
get_image_coords(struct nir_to_llvm_context
*ctx
,
2333 nir_intrinsic_instr
*instr
, bool add_frag_pos
)
2335 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
2336 if(instr
->variables
[0]->deref
.child
)
2337 type
= instr
->variables
[0]->deref
.child
->type
;
2339 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
2340 LLVMValueRef coords
[4];
2341 LLVMValueRef masks
[] = {
2342 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
2343 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false),
2347 count
= image_type_to_components_count(glsl_get_sampler_dim(type
),
2348 glsl_sampler_type_is_array(type
));
2351 if (instr
->src
[0].ssa
->num_components
)
2352 res
= LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[0], "");
2357 for (chan
= 0; chan
< count
; ++chan
) {
2358 coords
[chan
] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[chan
], "");
2362 for (chan
= 0; chan
< count
; ++chan
)
2363 coords
[chan
] = LLVMBuildAdd(ctx
->builder
, coords
[chan
], LLVMBuildFPToUI(ctx
->builder
, ctx
->frag_pos
[chan
], ctx
->i32
, ""), "");
2366 coords
[3] = LLVMGetUndef(ctx
->i32
);
2369 res
= build_gather_values(ctx
, coords
, count
);
2374 static void build_type_name_for_intr(
2376 char *buf
, unsigned bufsize
)
2378 LLVMTypeRef elem_type
= type
;
2380 assert(bufsize
>= 8);
2382 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
2383 int ret
= snprintf(buf
, bufsize
, "v%u",
2384 LLVMGetVectorSize(type
));
2386 char *type_name
= LLVMPrintTypeToString(type
);
2387 fprintf(stderr
, "Error building type name for: %s\n",
2391 elem_type
= LLVMGetElementType(type
);
2395 switch (LLVMGetTypeKind(elem_type
)) {
2397 case LLVMIntegerTypeKind
:
2398 snprintf(buf
, bufsize
, "i%d", LLVMGetIntTypeWidth(elem_type
));
2400 case LLVMFloatTypeKind
:
2401 snprintf(buf
, bufsize
, "f32");
2403 case LLVMDoubleTypeKind
:
2404 snprintf(buf
, bufsize
, "f64");
2409 static void get_image_intr_name(const char *base_name
,
2410 LLVMTypeRef data_type
,
2411 LLVMTypeRef coords_type
,
2412 LLVMTypeRef rsrc_type
,
2413 char *out_name
, unsigned out_len
)
2415 char coords_type_name
[8];
2417 build_type_name_for_intr(coords_type
, coords_type_name
,
2418 sizeof(coords_type_name
));
2420 if (HAVE_LLVM
<= 0x0309) {
2421 snprintf(out_name
, out_len
, "%s.%s", base_name
, coords_type_name
);
2423 char data_type_name
[8];
2424 char rsrc_type_name
[8];
2426 build_type_name_for_intr(data_type
, data_type_name
,
2427 sizeof(data_type_name
));
2428 build_type_name_for_intr(rsrc_type
, rsrc_type_name
,
2429 sizeof(rsrc_type_name
));
2430 snprintf(out_name
, out_len
, "%s.%s.%s.%s", base_name
,
2431 data_type_name
, coords_type_name
, rsrc_type_name
);
2435 static LLVMValueRef
visit_image_load(struct nir_to_llvm_context
*ctx
,
2436 nir_intrinsic_instr
*instr
)
2438 LLVMValueRef params
[7];
2440 char intrinsic_name
[64];
2441 const nir_variable
*var
= instr
->variables
[0]->var
;
2442 const struct glsl_type
*type
= var
->type
;
2443 if(instr
->variables
[0]->deref
.child
)
2444 type
= instr
->variables
[0]->deref
.child
->type
;
2446 type
= glsl_without_array(type
);
2447 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2448 params
[0] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
2449 params
[1] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
2450 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
2451 params
[2] = LLVMConstInt(ctx
->i32
, 0, false); /* voffset */
2452 params
[3] = LLVMConstInt(ctx
->i1
, 0, false); /* glc */
2453 params
[4] = LLVMConstInt(ctx
->i1
, 0, false); /* slc */
2454 res
= emit_llvm_intrinsic(ctx
, "llvm.amdgcn.buffer.load.format.v4f32", ctx
->v4f32
,
2457 res
= trim_vector(ctx
, res
, instr
->dest
.ssa
.num_components
);
2458 res
= to_integer(ctx
, res
);
2460 bool is_da
= glsl_sampler_type_is_array(type
) ||
2461 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
2462 bool add_frag_pos
= glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_SUBPASS
;
2463 LLVMValueRef da
= is_da
? ctx
->i32one
: ctx
->i32zero
;
2464 LLVMValueRef glc
= LLVMConstInt(ctx
->i1
, 0, false);
2465 LLVMValueRef slc
= LLVMConstInt(ctx
->i1
, 0, false);
2467 params
[0] = get_image_coords(ctx
, instr
, add_frag_pos
);
2468 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
2469 params
[2] = LLVMConstInt(ctx
->i32
, 15, false); /* dmask */
2470 if (HAVE_LLVM
<= 0x0309) {
2471 params
[3] = LLVMConstInt(ctx
->i1
, 0, false); /* r128 */
2476 LLVMValueRef lwe
= LLVMConstInt(ctx
->i1
, 0, false);
2483 get_image_intr_name("llvm.amdgcn.image.load",
2484 ctx
->v4f32
, /* vdata */
2485 LLVMTypeOf(params
[0]), /* coords */
2486 LLVMTypeOf(params
[1]), /* rsrc */
2487 intrinsic_name
, sizeof(intrinsic_name
));
2489 res
= emit_llvm_intrinsic(ctx
, intrinsic_name
, ctx
->v4f32
,
2490 params
, 7, AC_FUNC_ATTR_READONLY
);
2492 return to_integer(ctx
, res
);
2495 static void visit_image_store(struct nir_to_llvm_context
*ctx
,
2496 nir_intrinsic_instr
*instr
)
2498 LLVMValueRef params
[8];
2499 char intrinsic_name
[64];
2500 const nir_variable
*var
= instr
->variables
[0]->var
;
2501 LLVMValueRef i1false
= LLVMConstInt(ctx
->i1
, 0, 0);
2502 LLVMValueRef i1true
= LLVMConstInt(ctx
->i1
, 1, 0);
2503 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2505 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
2506 ctx
->shader_info
->fs
.writes_memory
= true;
2508 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2509 params
[0] = to_float(ctx
, get_src(ctx
, instr
->src
[2])); /* data */
2510 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
2511 params
[2] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
2512 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
2513 params
[3] = LLVMConstInt(ctx
->i32
, 0, false); /* voffset */
2514 params
[4] = i1false
; /* glc */
2515 params
[5] = i1false
; /* slc */
2516 emit_llvm_intrinsic(ctx
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->voidt
,
2519 bool is_da
= glsl_sampler_type_is_array(type
) ||
2520 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
2521 LLVMValueRef da
= is_da
? i1true
: i1false
;
2522 LLVMValueRef glc
= i1false
;
2523 LLVMValueRef slc
= i1false
;
2525 params
[0] = to_float(ctx
, get_src(ctx
, instr
->src
[2]));
2526 params
[1] = get_image_coords(ctx
, instr
, false); /* coords */
2527 params
[2] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
2528 params
[3] = LLVMConstInt(ctx
->i32
, 15, false); /* dmask */
2529 if (HAVE_LLVM
<= 0x0309) {
2530 params
[4] = i1false
; /* r128 */
2535 LLVMValueRef lwe
= i1false
;
2542 get_image_intr_name("llvm.amdgcn.image.store",
2543 LLVMTypeOf(params
[0]), /* vdata */
2544 LLVMTypeOf(params
[1]), /* coords */
2545 LLVMTypeOf(params
[2]), /* rsrc */
2546 intrinsic_name
, sizeof(intrinsic_name
));
2548 emit_llvm_intrinsic(ctx
, intrinsic_name
, ctx
->voidt
,
2554 static LLVMValueRef
visit_image_atomic(struct nir_to_llvm_context
*ctx
,
2555 nir_intrinsic_instr
*instr
)
2557 LLVMValueRef params
[6];
2558 int param_count
= 0;
2559 const nir_variable
*var
= instr
->variables
[0]->var
;
2560 LLVMValueRef i1false
= LLVMConstInt(ctx
->i1
, 0, 0);
2561 LLVMValueRef i1true
= LLVMConstInt(ctx
->i1
, 1, 0);
2562 const char *base_name
= "llvm.amdgcn.image.atomic";
2563 const char *atomic_name
;
2564 LLVMValueRef coords
;
2565 char intrinsic_name
[32], coords_type
[8];
2566 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2568 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
2569 ctx
->shader_info
->fs
.writes_memory
= true;
2571 params
[param_count
++] = get_src(ctx
, instr
->src
[2]);
2572 if (instr
->intrinsic
== nir_intrinsic_image_atomic_comp_swap
)
2573 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
2575 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2576 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
2577 coords
= params
[param_count
++] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
2578 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
2579 params
[param_count
++] = ctx
->i32zero
; /* voffset */
2580 params
[param_count
++] = i1false
; /* glc */
2581 params
[param_count
++] = i1false
; /* slc */
2583 bool da
= glsl_sampler_type_is_array(type
) ||
2584 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
2586 coords
= params
[param_count
++] = get_image_coords(ctx
, instr
, false);
2587 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
2588 params
[param_count
++] = i1false
; /* r128 */
2589 params
[param_count
++] = da
? i1true
: i1false
; /* da */
2590 params
[param_count
++] = i1false
; /* slc */
2593 switch (instr
->intrinsic
) {
2594 case nir_intrinsic_image_atomic_add
:
2595 atomic_name
= "add";
2597 case nir_intrinsic_image_atomic_min
:
2598 atomic_name
= "smin";
2600 case nir_intrinsic_image_atomic_max
:
2601 atomic_name
= "smax";
2603 case nir_intrinsic_image_atomic_and
:
2604 atomic_name
= "and";
2606 case nir_intrinsic_image_atomic_or
:
2609 case nir_intrinsic_image_atomic_xor
:
2610 atomic_name
= "xor";
2612 case nir_intrinsic_image_atomic_exchange
:
2613 atomic_name
= "swap";
2615 case nir_intrinsic_image_atomic_comp_swap
:
2616 atomic_name
= "cmpswap";
2621 build_int_type_name(LLVMTypeOf(coords
),
2622 coords_type
, sizeof(coords_type
));
2624 snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2625 "%s.%s.%s", base_name
, atomic_name
, coords_type
);
2626 return emit_llvm_intrinsic(ctx
, intrinsic_name
, ctx
->i32
, params
, param_count
, 0);
2629 static LLVMValueRef
visit_image_size(struct nir_to_llvm_context
*ctx
,
2630 nir_intrinsic_instr
*instr
)
2633 LLVMValueRef params
[10];
2634 const nir_variable
*var
= instr
->variables
[0]->var
;
2635 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
2636 bool da
= glsl_sampler_type_is_array(var
->type
) ||
2637 glsl_get_sampler_dim(var
->type
) == GLSL_SAMPLER_DIM_CUBE
;
2638 if(instr
->variables
[0]->deref
.child
)
2639 type
= instr
->variables
[0]->deref
.child
->type
;
2641 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
2642 return get_buffer_size(ctx
, get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
), true);
2643 params
[0] = ctx
->i32zero
;
2644 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
2645 params
[2] = LLVMConstInt(ctx
->i32
, 15, false);
2646 params
[3] = ctx
->i32zero
;
2647 params
[4] = ctx
->i32zero
;
2648 params
[5] = da
? ctx
->i32one
: ctx
->i32zero
;
2649 params
[6] = ctx
->i32zero
;
2650 params
[7] = ctx
->i32zero
;
2651 params
[8] = ctx
->i32zero
;
2652 params
[9] = ctx
->i32zero
;
2654 res
= emit_llvm_intrinsic(ctx
, "llvm.SI.getresinfo.i32", ctx
->v4i32
,
2655 params
, 10, AC_FUNC_ATTR_READNONE
);
2657 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
2658 glsl_sampler_type_is_array(type
)) {
2659 LLVMValueRef two
= LLVMConstInt(ctx
->i32
, 2, false);
2660 LLVMValueRef six
= LLVMConstInt(ctx
->i32
, 6, false);
2661 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->builder
, res
, two
, "");
2662 z
= LLVMBuildSDiv(ctx
->builder
, z
, six
, "");
2663 res
= LLVMBuildInsertElement(ctx
->builder
, res
, z
, two
, "");
2668 static void emit_waitcnt(struct nir_to_llvm_context
*ctx
)
2670 LLVMValueRef args
[1] = {
2671 LLVMConstInt(ctx
->i32
, 0xf70, false),
2673 emit_llvm_intrinsic(ctx
, "llvm.amdgcn.s.waitcnt",
2674 ctx
->voidt
, args
, 1, 0);
2677 static void emit_barrier(struct nir_to_llvm_context
*ctx
)
2680 emit_llvm_intrinsic(ctx
, "llvm.amdgcn.s.barrier",
2681 ctx
->voidt
, NULL
, 0, 0);
2684 static void emit_discard_if(struct nir_to_llvm_context
*ctx
,
2685 nir_intrinsic_instr
*instr
)
2688 ctx
->shader_info
->fs
.can_discard
= true;
2690 cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
2691 get_src(ctx
, instr
->src
[0]),
2694 cond
= LLVMBuildSelect(ctx
->builder
, cond
,
2695 LLVMConstReal(ctx
->f32
, -1.0f
),
2697 emit_llvm_intrinsic(ctx
, "llvm.AMDGPU.kill",
2698 LLVMVoidTypeInContext(ctx
->context
),
2703 visit_load_local_invocation_index(struct nir_to_llvm_context
*ctx
)
2705 LLVMValueRef result
;
2706 LLVMValueRef thread_id
= get_thread_id(ctx
);
2707 result
= LLVMBuildAnd(ctx
->builder
, ctx
->tg_size
,
2708 LLVMConstInt(ctx
->i32
, 0xfc0, false), "");
2710 return LLVMBuildAdd(ctx
->builder
, result
, thread_id
, "");
2713 static LLVMValueRef
visit_var_atomic(struct nir_to_llvm_context
*ctx
,
2714 nir_intrinsic_instr
*instr
)
2716 LLVMValueRef ptr
, result
;
2717 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2718 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
2719 ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
2721 if (instr
->intrinsic
== nir_intrinsic_var_atomic_comp_swap
) {
2722 LLVMValueRef src1
= get_src(ctx
, instr
->src
[1]);
2723 result
= LLVMBuildAtomicCmpXchg(ctx
->builder
,
2725 LLVMAtomicOrderingSequentiallyConsistent
,
2726 LLVMAtomicOrderingSequentiallyConsistent
,
2729 LLVMAtomicRMWBinOp op
;
2730 switch (instr
->intrinsic
) {
2731 case nir_intrinsic_var_atomic_add
:
2732 op
= LLVMAtomicRMWBinOpAdd
;
2734 case nir_intrinsic_var_atomic_umin
:
2735 op
= LLVMAtomicRMWBinOpUMin
;
2737 case nir_intrinsic_var_atomic_umax
:
2738 op
= LLVMAtomicRMWBinOpUMax
;
2740 case nir_intrinsic_var_atomic_imin
:
2741 op
= LLVMAtomicRMWBinOpMin
;
2743 case nir_intrinsic_var_atomic_imax
:
2744 op
= LLVMAtomicRMWBinOpMax
;
2746 case nir_intrinsic_var_atomic_and
:
2747 op
= LLVMAtomicRMWBinOpAnd
;
2749 case nir_intrinsic_var_atomic_or
:
2750 op
= LLVMAtomicRMWBinOpOr
;
2752 case nir_intrinsic_var_atomic_xor
:
2753 op
= LLVMAtomicRMWBinOpXor
;
2755 case nir_intrinsic_var_atomic_exchange
:
2756 op
= LLVMAtomicRMWBinOpXchg
;
2762 result
= LLVMBuildAtomicRMW(ctx
->builder
, op
, ptr
, to_integer(ctx
, src
),
2763 LLVMAtomicOrderingSequentiallyConsistent
,
2769 #define INTERP_CENTER 0
2770 #define INTERP_CENTROID 1
2771 #define INTERP_SAMPLE 2
2773 static LLVMValueRef
lookup_interp_param(struct nir_to_llvm_context
*ctx
,
2774 enum glsl_interp_mode interp
, unsigned location
)
2777 case INTERP_MODE_FLAT
:
2780 case INTERP_MODE_SMOOTH
:
2781 case INTERP_MODE_NONE
:
2782 if (location
== INTERP_CENTER
)
2783 return ctx
->persp_center
;
2784 else if (location
== INTERP_CENTROID
)
2785 return ctx
->persp_centroid
;
2786 else if (location
== INTERP_SAMPLE
)
2787 return ctx
->persp_sample
;
2789 case INTERP_MODE_NOPERSPECTIVE
:
2790 if (location
== INTERP_CENTER
)
2791 return ctx
->linear_center
;
2792 else if (location
== INTERP_CENTROID
)
2793 return ctx
->linear_centroid
;
2794 else if (location
== INTERP_SAMPLE
)
2795 return ctx
->linear_sample
;
2801 static LLVMValueRef
load_sample_position(struct nir_to_llvm_context
*ctx
,
2802 LLVMValueRef sample_id
)
2804 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
2805 LLVMValueRef offset0
= LLVMBuildMul(ctx
->builder
, sample_id
, LLVMConstInt(ctx
->i32
, 8, false), "");
2806 LLVMValueRef offset1
= LLVMBuildAdd(ctx
->builder
, offset0
, LLVMConstInt(ctx
->i32
, 4, false), "");
2807 LLVMValueRef result
[2];
2809 result
[0] = build_indexed_load_const(ctx
, ctx
->sample_positions
, offset0
);
2810 result
[1] = build_indexed_load_const(ctx
, ctx
->sample_positions
, offset1
);
2812 return build_gather_values(ctx
, result
, 2);
2815 static LLVMValueRef
visit_interp(struct nir_to_llvm_context
*ctx
,
2816 nir_intrinsic_instr
*instr
)
2818 LLVMValueRef result
[2];
2819 LLVMValueRef interp_param
, attr_number
;
2822 LLVMValueRef src_c0
, src_c1
;
2823 const char *intr_name
;
2825 int input_index
= instr
->variables
[0]->var
->data
.location
- VARYING_SLOT_VAR0
;
2826 switch (instr
->intrinsic
) {
2827 case nir_intrinsic_interp_var_at_centroid
:
2828 location
= INTERP_CENTROID
;
2830 case nir_intrinsic_interp_var_at_sample
:
2831 case nir_intrinsic_interp_var_at_offset
:
2832 location
= INTERP_SAMPLE
;
2833 src0
= get_src(ctx
, instr
->src
[0]);
2839 if (instr
->intrinsic
== nir_intrinsic_interp_var_at_offset
) {
2840 src_c0
= to_float(ctx
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32zero
, ""));
2841 src_c1
= to_float(ctx
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32one
, ""));
2842 } else if (instr
->intrinsic
== nir_intrinsic_interp_var_at_sample
) {
2843 LLVMValueRef sample_position
;
2844 LLVMValueRef halfval
= LLVMConstReal(ctx
->f32
, 0.5f
);
2846 /* fetch sample ID */
2847 sample_position
= load_sample_position(ctx
, src0
);
2849 src_c0
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->i32zero
, "");
2850 src_c0
= LLVMBuildFSub(ctx
->builder
, src_c0
, halfval
, "");
2851 src_c1
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->i32one
, "");
2852 src_c1
= LLVMBuildFSub(ctx
->builder
, src_c1
, halfval
, "");
2854 interp_param
= lookup_interp_param(ctx
, instr
->variables
[0]->var
->data
.interpolation
, location
);
2855 attr_number
= LLVMConstInt(ctx
->i32
, input_index
, false);
2857 if (location
== INTERP_SAMPLE
) {
2858 LLVMValueRef ij_out
[2];
2859 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
, interp_param
);
2862 * take the I then J parameters, and the DDX/Y for it, and
2863 * calculate the IJ inputs for the interpolator.
2864 * temp1 = ddx * offset/sample.x + I;
2865 * interp_param.I = ddy * offset/sample.y + temp1;
2866 * temp1 = ddx * offset/sample.x + J;
2867 * interp_param.J = ddy * offset/sample.y + temp1;
2869 for (unsigned i
= 0; i
< 2; i
++) {
2870 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->i32
, i
, false);
2871 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->i32
, i
+ 2, false);
2872 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->builder
,
2873 ddxy_out
, ix_ll
, "");
2874 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->builder
,
2875 ddxy_out
, iy_ll
, "");
2876 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->builder
,
2877 interp_param
, ix_ll
, "");
2878 LLVMValueRef temp1
, temp2
;
2880 interp_el
= LLVMBuildBitCast(ctx
->builder
, interp_el
,
2883 temp1
= LLVMBuildFMul(ctx
->builder
, ddx_el
, src_c0
, "");
2884 temp1
= LLVMBuildFAdd(ctx
->builder
, temp1
, interp_el
, "");
2886 temp2
= LLVMBuildFMul(ctx
->builder
, ddy_el
, src_c1
, "");
2887 temp2
= LLVMBuildFAdd(ctx
->builder
, temp2
, temp1
, "");
2889 ij_out
[i
] = LLVMBuildBitCast(ctx
->builder
,
2890 temp2
, ctx
->i32
, "");
2892 interp_param
= build_gather_values(ctx
, ij_out
, 2);
2895 intr_name
= interp_param
? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
2896 for (chan
= 0; chan
< 2; chan
++) {
2897 LLVMValueRef args
[4];
2898 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
2900 args
[0] = llvm_chan
;
2901 args
[1] = attr_number
;
2902 args
[2] = ctx
->prim_mask
;
2903 args
[3] = interp_param
;
2904 result
[chan
] = emit_llvm_intrinsic(ctx
, intr_name
,
2905 ctx
->f32
, args
, args
[3] ? 4 : 3,
2906 AC_FUNC_ATTR_READNONE
);
2908 return build_gather_values(ctx
, result
, 2);
2911 static void visit_intrinsic(struct nir_to_llvm_context
*ctx
,
2912 nir_intrinsic_instr
*instr
)
2914 LLVMValueRef result
= NULL
;
2916 switch (instr
->intrinsic
) {
2917 case nir_intrinsic_load_work_group_id
: {
2918 result
= ctx
->workgroup_ids
;
2921 case nir_intrinsic_load_base_vertex
: {
2922 result
= ctx
->base_vertex
;
2925 case nir_intrinsic_load_vertex_id_zero_base
: {
2926 result
= ctx
->vertex_id
;
2929 case nir_intrinsic_load_local_invocation_id
: {
2930 result
= ctx
->local_invocation_ids
;
2933 case nir_intrinsic_load_base_instance
:
2934 result
= ctx
->start_instance
;
2936 case nir_intrinsic_load_sample_id
:
2937 result
= ctx
->ancillary
;
2939 case nir_intrinsic_load_front_face
:
2940 result
= ctx
->front_face
;
2942 case nir_intrinsic_load_instance_id
:
2943 result
= ctx
->instance_id
;
2944 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
2945 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
2947 case nir_intrinsic_load_num_work_groups
:
2948 result
= ctx
->num_work_groups
;
2950 case nir_intrinsic_load_local_invocation_index
:
2951 result
= visit_load_local_invocation_index(ctx
);
2953 case nir_intrinsic_load_push_constant
:
2954 result
= visit_load_push_constant(ctx
, instr
);
2956 case nir_intrinsic_vulkan_resource_index
:
2957 result
= visit_vulkan_resource_index(ctx
, instr
);
2959 case nir_intrinsic_store_ssbo
:
2960 visit_store_ssbo(ctx
, instr
);
2962 case nir_intrinsic_load_ssbo
:
2963 result
= visit_load_buffer(ctx
, instr
);
2965 case nir_intrinsic_ssbo_atomic_add
:
2966 case nir_intrinsic_ssbo_atomic_imin
:
2967 case nir_intrinsic_ssbo_atomic_umin
:
2968 case nir_intrinsic_ssbo_atomic_imax
:
2969 case nir_intrinsic_ssbo_atomic_umax
:
2970 case nir_intrinsic_ssbo_atomic_and
:
2971 case nir_intrinsic_ssbo_atomic_or
:
2972 case nir_intrinsic_ssbo_atomic_xor
:
2973 case nir_intrinsic_ssbo_atomic_exchange
:
2974 case nir_intrinsic_ssbo_atomic_comp_swap
:
2975 result
= visit_atomic_ssbo(ctx
, instr
);
2977 case nir_intrinsic_load_ubo
:
2978 result
= visit_load_buffer(ctx
, instr
);
2980 case nir_intrinsic_get_buffer_size
:
2981 result
= visit_get_buffer_size(ctx
, instr
);
2983 case nir_intrinsic_load_var
:
2984 result
= visit_load_var(ctx
, instr
);
2986 case nir_intrinsic_store_var
:
2987 visit_store_var(ctx
, instr
);
2989 case nir_intrinsic_image_load
:
2990 result
= visit_image_load(ctx
, instr
);
2992 case nir_intrinsic_image_store
:
2993 visit_image_store(ctx
, instr
);
2995 case nir_intrinsic_image_atomic_add
:
2996 case nir_intrinsic_image_atomic_min
:
2997 case nir_intrinsic_image_atomic_max
:
2998 case nir_intrinsic_image_atomic_and
:
2999 case nir_intrinsic_image_atomic_or
:
3000 case nir_intrinsic_image_atomic_xor
:
3001 case nir_intrinsic_image_atomic_exchange
:
3002 case nir_intrinsic_image_atomic_comp_swap
:
3003 result
= visit_image_atomic(ctx
, instr
);
3005 case nir_intrinsic_image_size
:
3006 result
= visit_image_size(ctx
, instr
);
3008 case nir_intrinsic_discard
:
3009 ctx
->shader_info
->fs
.can_discard
= true;
3010 emit_llvm_intrinsic(ctx
, "llvm.AMDGPU.kilp",
3011 LLVMVoidTypeInContext(ctx
->context
),
3014 case nir_intrinsic_discard_if
:
3015 emit_discard_if(ctx
, instr
);
3017 case nir_intrinsic_memory_barrier
:
3020 case nir_intrinsic_barrier
:
3023 case nir_intrinsic_var_atomic_add
:
3024 case nir_intrinsic_var_atomic_imin
:
3025 case nir_intrinsic_var_atomic_umin
:
3026 case nir_intrinsic_var_atomic_imax
:
3027 case nir_intrinsic_var_atomic_umax
:
3028 case nir_intrinsic_var_atomic_and
:
3029 case nir_intrinsic_var_atomic_or
:
3030 case nir_intrinsic_var_atomic_xor
:
3031 case nir_intrinsic_var_atomic_exchange
:
3032 case nir_intrinsic_var_atomic_comp_swap
:
3033 result
= visit_var_atomic(ctx
, instr
);
3035 case nir_intrinsic_interp_var_at_centroid
:
3036 case nir_intrinsic_interp_var_at_sample
:
3037 case nir_intrinsic_interp_var_at_offset
:
3038 result
= visit_interp(ctx
, instr
);
3041 fprintf(stderr
, "Unknown intrinsic: ");
3042 nir_print_instr(&instr
->instr
, stderr
);
3043 fprintf(stderr
, "\n");
3047 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
3051 static LLVMValueRef
get_sampler_desc(struct nir_to_llvm_context
*ctx
,
3052 nir_deref_var
*deref
,
3053 enum desc_type desc_type
)
3055 unsigned desc_set
= deref
->var
->data
.descriptor_set
;
3056 LLVMValueRef list
= ctx
->descriptor_sets
[desc_set
];
3057 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[desc_set
].layout
;
3058 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ deref
->var
->data
.binding
;
3059 unsigned offset
= binding
->offset
;
3060 unsigned stride
= binding
->size
;
3062 LLVMBuilderRef builder
= ctx
->builder
;
3064 LLVMValueRef indices
[2];
3065 LLVMValueRef index
= NULL
;
3067 assert(deref
->var
->data
.binding
< layout
->binding_count
);
3069 switch (desc_type
) {
3081 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
3092 if (deref
->deref
.child
) {
3093 nir_deref_array
*child
= (nir_deref_array
*)deref
->deref
.child
;
3095 assert(child
->deref_array_type
!= nir_deref_array_type_wildcard
);
3096 offset
+= child
->base_offset
* stride
;
3097 if (child
->deref_array_type
== nir_deref_array_type_indirect
) {
3098 index
= get_src(ctx
, child
->indirect
);
3102 assert(stride
% type_size
== 0);
3105 index
= ctx
->i32zero
;
3107 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, stride
/ type_size
, 0), "");
3108 indices
[0] = ctx
->i32zero
;
3109 indices
[1] = LLVMConstInt(ctx
->i32
, offset
, 0);
3110 list
= LLVMBuildGEP(builder
, list
, indices
, 2, "");
3111 list
= LLVMBuildPointerCast(builder
, list
, const_array(type
, 0), "");
3113 return build_indexed_load_const(ctx
, list
, index
);
3116 static void set_tex_fetch_args(struct nir_to_llvm_context
*ctx
,
3117 struct ac_tex_info
*tinfo
,
3118 nir_tex_instr
*instr
,
3120 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
3121 LLVMValueRef
*param
, unsigned count
,
3125 unsigned is_rect
= 0;
3126 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
3128 if (op
== nir_texop_lod
)
3130 /* Pad to power of two vector */
3131 while (count
< util_next_power_of_two(count
))
3132 param
[count
++] = LLVMGetUndef(ctx
->i32
);
3135 tinfo
->args
[0] = build_gather_values(ctx
, param
, count
);
3137 tinfo
->args
[0] = param
[0];
3139 tinfo
->args
[1] = res_ptr
;
3142 if (op
== nir_texop_txf
||
3143 op
== nir_texop_txf_ms
||
3144 op
== nir_texop_query_levels
||
3145 op
== nir_texop_texture_samples
||
3146 op
== nir_texop_txs
)
3147 tinfo
->dst_type
= ctx
->v4i32
;
3149 tinfo
->dst_type
= ctx
->v4f32
;
3150 tinfo
->args
[num_args
++] = samp_ptr
;
3153 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
&& op
== nir_texop_txf
) {
3154 tinfo
->args
[0] = res_ptr
;
3155 tinfo
->args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
3156 tinfo
->args
[2] = param
[0];
3157 tinfo
->arg_count
= 3;
3161 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, dmask
, 0);
3162 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, is_rect
, 0); /* unorm */
3163 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* r128 */
3164 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, da
? 1 : 0, 0);
3165 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* glc */
3166 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* slc */
3167 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* tfe */
3168 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* lwe */
3170 tinfo
->arg_count
= num_args
;
3173 static void tex_fetch_ptrs(struct nir_to_llvm_context
*ctx
,
3174 nir_tex_instr
*instr
,
3175 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
3176 LLVMValueRef
*fmask_ptr
)
3178 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
3179 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_BUFFER
);
3181 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_IMAGE
);
3184 *samp_ptr
= get_sampler_desc(ctx
, instr
->sampler
, DESC_SAMPLER
);
3186 *samp_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_SAMPLER
);
3188 if (fmask_ptr
&& !instr
->sampler
&& (instr
->op
== nir_texop_txf_ms
||
3189 instr
->op
== nir_texop_samples_identical
))
3190 *fmask_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_FMASK
);
3193 static LLVMValueRef
build_cube_intrinsic(struct nir_to_llvm_context
*ctx
,
3197 LLVMValueRef v
, cube_vec
;
3200 LLVMTypeRef f32
= LLVMTypeOf(in
[0]);
3201 LLVMValueRef out
[4];
3203 out
[0] = emit_llvm_intrinsic(ctx
, "llvm.amdgcn.cubetc",
3204 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
3205 out
[1] = emit_llvm_intrinsic(ctx
, "llvm.amdgcn.cubesc",
3206 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
3207 out
[2] = emit_llvm_intrinsic(ctx
, "llvm.amdgcn.cubema",
3208 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
3209 out
[3] = emit_llvm_intrinsic(ctx
, "llvm.amdgcn.cubeid",
3210 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
3212 return build_gather_values(ctx
, out
, 4);
3218 c
[3] = LLVMGetUndef(LLVMTypeOf(in
[0]));
3219 cube_vec
= build_gather_values(ctx
, c
, 4);
3220 v
= emit_llvm_intrinsic(ctx
, "llvm.AMDGPU.cube", LLVMTypeOf(cube_vec
),
3221 &cube_vec
, 1, AC_FUNC_ATTR_READNONE
);
3226 static void cube_to_2d_coords(struct nir_to_llvm_context
*ctx
,
3227 LLVMValueRef
*in
, LLVMValueRef
*out
)
3229 LLVMValueRef coords
[4];
3230 LLVMValueRef mad_args
[3];
3235 v
= build_cube_intrinsic(ctx
, in
);
3236 for (i
= 0; i
< 4; i
++)
3237 coords
[i
] = LLVMBuildExtractElement(ctx
->builder
, v
,
3238 LLVMConstInt(ctx
->i32
, i
, false), "");
3240 coords
[2] = emit_llvm_intrinsic(ctx
, "llvm.fabs.f32", ctx
->f32
,
3241 &coords
[2], 1, AC_FUNC_ATTR_READNONE
);
3242 coords
[2] = emit_fdiv(ctx
, ctx
->f32one
, coords
[2]);
3244 mad_args
[1] = coords
[2];
3245 mad_args
[2] = LLVMConstReal(ctx
->f32
, 1.5);
3246 mad_args
[0] = coords
[0];
3249 tmp
= LLVMBuildFMul(ctx
->builder
, mad_args
[0], mad_args
[1], "");
3250 coords
[0] = LLVMBuildFAdd(ctx
->builder
, tmp
, mad_args
[2], "");
3252 mad_args
[0] = coords
[1];
3255 tmp
= LLVMBuildFMul(ctx
->builder
, mad_args
[0], mad_args
[1], "");
3256 coords
[1] = LLVMBuildFAdd(ctx
->builder
, tmp
, mad_args
[2], "");
3258 /* apply xyz = yxw swizzle to cooords */
3264 static void emit_prepare_cube_coords(struct nir_to_llvm_context
*ctx
,
3265 LLVMValueRef
*coords_arg
, int num_coords
,
3267 bool is_array
, LLVMValueRef
*derivs_arg
)
3269 LLVMValueRef coords
[4];
3271 cube_to_2d_coords(ctx
, coords_arg
, coords
);
3273 if (is_deriv
&& derivs_arg
) {
3274 LLVMValueRef derivs
[4];
3277 /* Convert cube derivatives to 2D derivatives. */
3278 for (axis
= 0; axis
< 2; axis
++) {
3279 LLVMValueRef shifted_cube_coords
[4], shifted_coords
[4];
3281 /* Shift the cube coordinates by the derivatives to get
3282 * the cube coordinates of the "neighboring pixel".
3284 for (i
= 0; i
< 3; i
++)
3285 shifted_cube_coords
[i
] =
3286 LLVMBuildFAdd(ctx
->builder
, coords_arg
[i
],
3287 derivs_arg
[axis
*3+i
], "");
3288 shifted_cube_coords
[3] = LLVMGetUndef(ctx
->f32
);
3290 /* Project the shifted cube coordinates onto the face. */
3291 cube_to_2d_coords(ctx
, shifted_cube_coords
,
3294 /* Subtract both sets of 2D coordinates to get 2D derivatives.
3295 * This won't work if the shifted coordinates ended up
3296 * in a different face.
3298 for (i
= 0; i
< 2; i
++)
3299 derivs
[axis
* 2 + i
] =
3300 LLVMBuildFSub(ctx
->builder
, shifted_coords
[i
],
3304 memcpy(derivs_arg
, derivs
, sizeof(derivs
));
3308 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
3309 /* coords_arg.w component - array_index for cube arrays */
3310 LLVMValueRef tmp
= LLVMBuildFMul(ctx
->builder
, coords_arg
[3], LLVMConstReal(ctx
->f32
, 8.0), "");
3311 coords
[2] = LLVMBuildFAdd(ctx
->builder
, tmp
, coords
[2], "");
3314 memcpy(coords_arg
, coords
, sizeof(coords
));
3317 static void visit_tex(struct nir_to_llvm_context
*ctx
, nir_tex_instr
*instr
)
3319 LLVMValueRef result
= NULL
;
3320 struct ac_tex_info tinfo
= { 0 };
3321 unsigned dmask
= 0xf;
3322 LLVMValueRef address
[16];
3323 LLVMValueRef coords
[5];
3324 LLVMValueRef coord
= NULL
, lod
= NULL
, comparitor
= NULL
, bias
, offsets
= NULL
;
3325 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
, sample_index
= NULL
;
3326 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
3327 LLVMValueRef derivs
[6];
3328 unsigned chan
, count
= 0;
3329 unsigned const_src
= 0, num_deriv_comp
= 0;
3331 tex_fetch_ptrs(ctx
, instr
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
3333 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3334 switch (instr
->src
[i
].src_type
) {
3335 case nir_tex_src_coord
:
3336 coord
= get_src(ctx
, instr
->src
[i
].src
);
3338 case nir_tex_src_projector
:
3340 case nir_tex_src_comparitor
:
3341 comparitor
= get_src(ctx
, instr
->src
[i
].src
);
3343 case nir_tex_src_offset
:
3344 offsets
= get_src(ctx
, instr
->src
[i
].src
);
3347 case nir_tex_src_bias
:
3348 bias
= get_src(ctx
, instr
->src
[i
].src
);
3350 case nir_tex_src_lod
:
3351 lod
= get_src(ctx
, instr
->src
[i
].src
);
3353 case nir_tex_src_ms_index
:
3354 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
3356 case nir_tex_src_ms_mcs
:
3358 case nir_tex_src_ddx
:
3359 ddx
= get_src(ctx
, instr
->src
[i
].src
);
3360 num_deriv_comp
= instr
->src
[i
].src
.ssa
->num_components
;
3362 case nir_tex_src_ddy
:
3363 ddy
= get_src(ctx
, instr
->src
[i
].src
);
3365 case nir_tex_src_texture_offset
:
3366 case nir_tex_src_sampler_offset
:
3367 case nir_tex_src_plane
:
3373 if (instr
->op
== nir_texop_texture_samples
) {
3374 LLVMValueRef res
, samples
, is_msaa
;
3375 res
= LLVMBuildBitCast(ctx
->builder
, res_ptr
, ctx
->v8i32
, "");
3376 samples
= LLVMBuildExtractElement(ctx
->builder
, res
,
3377 LLVMConstInt(ctx
->i32
, 3, false), "");
3378 is_msaa
= LLVMBuildLShr(ctx
->builder
, samples
,
3379 LLVMConstInt(ctx
->i32
, 28, false), "");
3380 is_msaa
= LLVMBuildAnd(ctx
->builder
, is_msaa
,
3381 LLVMConstInt(ctx
->i32
, 0xe, false), "");
3382 is_msaa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, is_msaa
,
3383 LLVMConstInt(ctx
->i32
, 0xe, false), "");
3385 samples
= LLVMBuildLShr(ctx
->builder
, samples
,
3386 LLVMConstInt(ctx
->i32
, 16, false), "");
3387 samples
= LLVMBuildAnd(ctx
->builder
, samples
,
3388 LLVMConstInt(ctx
->i32
, 0xf, false), "");
3389 samples
= LLVMBuildShl(ctx
->builder
, ctx
->i32one
,
3391 samples
= LLVMBuildSelect(ctx
->builder
, is_msaa
, samples
,
3398 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
3399 coords
[chan
] = llvm_extract_elem(ctx
, coord
, chan
);
3401 if (offsets
&& instr
->op
!= nir_texop_txf
) {
3402 LLVMValueRef offset
[3], pack
;
3403 for (chan
= 0; chan
< 3; ++chan
)
3404 offset
[chan
] = ctx
->i32zero
;
3406 tinfo
.has_offset
= true;
3407 for (chan
= 0; chan
< get_llvm_num_components(offsets
); chan
++) {
3408 offset
[chan
] = llvm_extract_elem(ctx
, offsets
, chan
);
3409 offset
[chan
] = LLVMBuildAnd(ctx
->builder
, offset
[chan
],
3410 LLVMConstInt(ctx
->i32
, 0x3f, false), "");
3412 offset
[chan
] = LLVMBuildShl(ctx
->builder
, offset
[chan
],
3413 LLVMConstInt(ctx
->i32
, chan
* 8, false), "");
3415 pack
= LLVMBuildOr(ctx
->builder
, offset
[0], offset
[1], "");
3416 pack
= LLVMBuildOr(ctx
->builder
, pack
, offset
[2], "");
3417 address
[count
++] = pack
;
3420 /* pack LOD bias value */
3421 if (instr
->op
== nir_texop_txb
&& bias
) {
3422 address
[count
++] = bias
;
3425 /* Pack depth comparison value */
3426 if (instr
->is_shadow
&& comparitor
) {
3427 address
[count
++] = llvm_extract_elem(ctx
, comparitor
, 0);
3430 /* pack derivatives */
3432 switch (instr
->sampler_dim
) {
3433 case GLSL_SAMPLER_DIM_3D
:
3434 case GLSL_SAMPLER_DIM_CUBE
:
3437 case GLSL_SAMPLER_DIM_2D
:
3441 case GLSL_SAMPLER_DIM_1D
:
3446 for (unsigned i
= 0; i
< num_deriv_comp
; i
++) {
3447 derivs
[i
* 2] = to_float(ctx
, llvm_extract_elem(ctx
, ddx
, i
));
3448 derivs
[i
* 2 + 1] = to_float(ctx
, llvm_extract_elem(ctx
, ddy
, i
));
3452 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& coord
) {
3453 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
3454 coords
[chan
] = to_float(ctx
, coords
[chan
]);
3455 if (instr
->coord_components
== 3)
3456 coords
[3] = LLVMGetUndef(ctx
->f32
);
3457 emit_prepare_cube_coords(ctx
, coords
, instr
->coord_components
, instr
->op
== nir_texop_txd
, instr
->is_array
, derivs
);
3463 for (unsigned i
= 0; i
< num_deriv_comp
* 2; i
++)
3464 address
[count
++] = derivs
[i
];
3467 /* Pack texture coordinates */
3469 address
[count
++] = coords
[0];
3470 if (instr
->coord_components
> 1)
3471 address
[count
++] = coords
[1];
3472 if (instr
->coord_components
> 2) {
3473 /* This seems like a bit of a hack - but it passes Vulkan CTS with it */
3474 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_3D
&& instr
->op
!= nir_texop_txf
) {
3475 coords
[2] = to_float(ctx
, coords
[2]);
3476 coords
[2] = emit_llvm_intrinsic(ctx
, "llvm.rint.f32", ctx
->f32
, &coords
[2],
3478 coords
[2] = to_integer(ctx
, coords
[2]);
3480 address
[count
++] = coords
[2];
3485 if ((instr
->op
== nir_texop_txl
|| instr
->op
== nir_texop_txf
) && lod
) {
3486 address
[count
++] = lod
;
3487 } else if (instr
->op
== nir_texop_txf_ms
&& sample_index
) {
3488 address
[count
++] = sample_index
;
3489 } else if(instr
->op
== nir_texop_txs
) {
3491 address
[count
++] = lod
;
3494 for (chan
= 0; chan
< count
; chan
++) {
3495 address
[chan
] = LLVMBuildBitCast(ctx
->builder
,
3496 address
[chan
], ctx
->i32
, "");
3499 if (instr
->op
== nir_texop_samples_identical
) {
3500 LLVMValueRef txf_address
[4];
3501 struct ac_tex_info txf_info
= { 0 };
3502 unsigned txf_count
= count
;
3503 memcpy(txf_address
, address
, sizeof(txf_address
));
3505 if (!instr
->is_array
)
3506 txf_address
[2] = ctx
->i32zero
;
3507 txf_address
[3] = ctx
->i32zero
;
3509 set_tex_fetch_args(ctx
, &txf_info
, instr
, nir_texop_txf
,
3511 txf_address
, txf_count
, 0xf);
3513 result
= build_tex_intrinsic(ctx
, instr
, &txf_info
);
3515 result
= LLVMBuildExtractElement(ctx
->builder
, result
, ctx
->i32zero
, "");
3516 result
= emit_int_cmp(ctx
, LLVMIntEQ
, result
, ctx
->i32zero
);
3520 /* Adjust the sample index according to FMASK.
3522 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3523 * which is the identity mapping. Each nibble says which physical sample
3524 * should be fetched to get that sample.
3526 * For example, 0x11111100 means there are only 2 samples stored and
3527 * the second sample covers 3/4 of the pixel. When reading samples 0
3528 * and 1, return physical sample 0 (determined by the first two 0s
3529 * in FMASK), otherwise return physical sample 1.
3531 * The sample index should be adjusted as follows:
3532 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
3534 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
) {
3535 LLVMValueRef txf_address
[4];
3536 struct ac_tex_info txf_info
= { 0 };
3537 unsigned txf_count
= count
;
3538 memcpy(txf_address
, address
, sizeof(txf_address
));
3540 if (!instr
->is_array
)
3541 txf_address
[2] = ctx
->i32zero
;
3542 txf_address
[3] = ctx
->i32zero
;
3544 set_tex_fetch_args(ctx
, &txf_info
, instr
, nir_texop_txf
,
3546 txf_address
, txf_count
, 0xf);
3548 result
= build_tex_intrinsic(ctx
, instr
, &txf_info
);
3549 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
3550 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
3552 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
3556 unsigned sample_chan
= instr
->is_array
? 3 : 2;
3558 LLVMValueRef sample_index4
=
3559 LLVMBuildMul(ctx
->builder
, address
[sample_chan
], four
, "");
3560 LLVMValueRef shifted_fmask
=
3561 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
3562 LLVMValueRef final_sample
=
3563 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
3565 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3566 * resource descriptor is 0 (invalid),
3568 LLVMValueRef fmask_desc
=
3569 LLVMBuildBitCast(ctx
->builder
, fmask_ptr
,
3572 LLVMValueRef fmask_word1
=
3573 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
3576 LLVMValueRef word1_is_nonzero
=
3577 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3578 fmask_word1
, ctx
->i32zero
, "");
3580 /* Replace the MSAA sample index. */
3581 address
[sample_chan
] =
3582 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
3583 final_sample
, address
[sample_chan
], "");
3586 if (offsets
&& instr
->op
== nir_texop_txf
) {
3587 nir_const_value
*const_offset
=
3588 nir_src_as_const_value(instr
->src
[const_src
].src
);
3590 assert(const_offset
);
3591 if (instr
->coord_components
> 2)
3592 address
[2] = LLVMBuildAdd(ctx
->builder
,
3593 address
[2], LLVMConstInt(ctx
->i32
, const_offset
->i32
[2], false), "");
3594 if (instr
->coord_components
> 1)
3595 address
[1] = LLVMBuildAdd(ctx
->builder
,
3596 address
[1], LLVMConstInt(ctx
->i32
, const_offset
->i32
[1], false), "");
3597 address
[0] = LLVMBuildAdd(ctx
->builder
,
3598 address
[0], LLVMConstInt(ctx
->i32
, const_offset
->i32
[0], false), "");
3602 /* TODO TG4 support */
3603 if (instr
->op
== nir_texop_tg4
) {
3604 if (instr
->is_shadow
)
3607 dmask
= 1 << instr
->component
;
3609 set_tex_fetch_args(ctx
, &tinfo
, instr
, instr
->op
,
3610 res_ptr
, samp_ptr
, address
, count
, dmask
);
3612 result
= build_tex_intrinsic(ctx
, instr
, &tinfo
);
3614 if (instr
->op
== nir_texop_query_levels
)
3615 result
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, 3, false), "");
3616 else if (instr
->op
== nir_texop_txs
&&
3617 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
3619 LLVMValueRef two
= LLVMConstInt(ctx
->i32
, 2, false);
3620 LLVMValueRef six
= LLVMConstInt(ctx
->i32
, 6, false);
3621 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->builder
, result
, two
, "");
3622 z
= LLVMBuildSDiv(ctx
->builder
, z
, six
, "");
3623 result
= LLVMBuildInsertElement(ctx
->builder
, result
, z
, two
, "");
3624 } else if (instr
->dest
.ssa
.num_components
!= 4)
3625 result
= trim_vector(ctx
, result
, instr
->dest
.ssa
.num_components
);
3629 assert(instr
->dest
.is_ssa
);
3630 result
= to_integer(ctx
, result
);
3631 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
3636 static void visit_phi(struct nir_to_llvm_context
*ctx
, nir_phi_instr
*instr
)
3638 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
3639 LLVMValueRef result
= LLVMBuildPhi(ctx
->builder
, type
, "");
3641 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
3642 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
3645 static void visit_post_phi(struct nir_to_llvm_context
*ctx
,
3646 nir_phi_instr
*instr
,
3647 LLVMValueRef llvm_phi
)
3649 nir_foreach_phi_src(src
, instr
) {
3650 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
3651 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
3653 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
3657 static void phi_post_pass(struct nir_to_llvm_context
*ctx
)
3659 struct hash_entry
*entry
;
3660 hash_table_foreach(ctx
->phis
, entry
) {
3661 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
3662 (LLVMValueRef
)entry
->data
);
3667 static void visit_ssa_undef(struct nir_to_llvm_context
*ctx
,
3668 nir_ssa_undef_instr
*instr
)
3670 unsigned num_components
= instr
->def
.num_components
;
3673 if (num_components
== 1)
3674 undef
= LLVMGetUndef(ctx
->i32
);
3676 undef
= LLVMGetUndef(LLVMVectorType(ctx
->i32
, num_components
));
3678 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, undef
);
3681 static void visit_jump(struct nir_to_llvm_context
*ctx
,
3682 nir_jump_instr
*instr
)
3684 switch (instr
->type
) {
3685 case nir_jump_break
:
3686 LLVMBuildBr(ctx
->builder
, ctx
->break_block
);
3687 LLVMClearInsertionPosition(ctx
->builder
);
3689 case nir_jump_continue
:
3690 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
3691 LLVMClearInsertionPosition(ctx
->builder
);
3694 fprintf(stderr
, "Unknown NIR jump instr: ");
3695 nir_print_instr(&instr
->instr
, stderr
);
3696 fprintf(stderr
, "\n");
3701 static void visit_cf_list(struct nir_to_llvm_context
*ctx
,
3702 struct exec_list
*list
);
3704 static void visit_block(struct nir_to_llvm_context
*ctx
, nir_block
*block
)
3706 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->builder
);
3707 nir_foreach_instr(instr
, block
)
3709 switch (instr
->type
) {
3710 case nir_instr_type_alu
:
3711 visit_alu(ctx
, nir_instr_as_alu(instr
));
3713 case nir_instr_type_load_const
:
3714 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
3716 case nir_instr_type_intrinsic
:
3717 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
3719 case nir_instr_type_tex
:
3720 visit_tex(ctx
, nir_instr_as_tex(instr
));
3722 case nir_instr_type_phi
:
3723 visit_phi(ctx
, nir_instr_as_phi(instr
));
3725 case nir_instr_type_ssa_undef
:
3726 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
3728 case nir_instr_type_jump
:
3729 visit_jump(ctx
, nir_instr_as_jump(instr
));
3732 fprintf(stderr
, "Unknown NIR instr type: ");
3733 nir_print_instr(instr
, stderr
);
3734 fprintf(stderr
, "\n");
3739 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
3742 static void visit_if(struct nir_to_llvm_context
*ctx
, nir_if
*if_stmt
)
3744 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
3746 LLVMBasicBlockRef merge_block
=
3747 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
3748 LLVMBasicBlockRef if_block
=
3749 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
3750 LLVMBasicBlockRef else_block
= merge_block
;
3751 if (!exec_list_is_empty(&if_stmt
->else_list
))
3752 else_block
= LLVMAppendBasicBlockInContext(
3753 ctx
->context
, ctx
->main_function
, "");
3755 LLVMValueRef cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, value
,
3756 LLVMConstInt(ctx
->i32
, 0, false), "");
3757 LLVMBuildCondBr(ctx
->builder
, cond
, if_block
, else_block
);
3759 LLVMPositionBuilderAtEnd(ctx
->builder
, if_block
);
3760 visit_cf_list(ctx
, &if_stmt
->then_list
);
3761 if (LLVMGetInsertBlock(ctx
->builder
))
3762 LLVMBuildBr(ctx
->builder
, merge_block
);
3764 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
3765 LLVMPositionBuilderAtEnd(ctx
->builder
, else_block
);
3766 visit_cf_list(ctx
, &if_stmt
->else_list
);
3767 if (LLVMGetInsertBlock(ctx
->builder
))
3768 LLVMBuildBr(ctx
->builder
, merge_block
);
3771 LLVMPositionBuilderAtEnd(ctx
->builder
, merge_block
);
3774 static void visit_loop(struct nir_to_llvm_context
*ctx
, nir_loop
*loop
)
3776 LLVMBasicBlockRef continue_parent
= ctx
->continue_block
;
3777 LLVMBasicBlockRef break_parent
= ctx
->break_block
;
3779 ctx
->continue_block
=
3780 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
3782 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
3784 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
3785 LLVMPositionBuilderAtEnd(ctx
->builder
, ctx
->continue_block
);
3786 visit_cf_list(ctx
, &loop
->body
);
3788 if (LLVMGetInsertBlock(ctx
->builder
))
3789 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
3790 LLVMPositionBuilderAtEnd(ctx
->builder
, ctx
->break_block
);
3792 ctx
->continue_block
= continue_parent
;
3793 ctx
->break_block
= break_parent
;
3796 static void visit_cf_list(struct nir_to_llvm_context
*ctx
,
3797 struct exec_list
*list
)
3799 foreach_list_typed(nir_cf_node
, node
, node
, list
)
3801 switch (node
->type
) {
3802 case nir_cf_node_block
:
3803 visit_block(ctx
, nir_cf_node_as_block(node
));
3806 case nir_cf_node_if
:
3807 visit_if(ctx
, nir_cf_node_as_if(node
));
3810 case nir_cf_node_loop
:
3811 visit_loop(ctx
, nir_cf_node_as_loop(node
));
3821 handle_vs_input_decl(struct nir_to_llvm_context
*ctx
,
3822 struct nir_variable
*variable
)
3824 LLVMValueRef t_list_ptr
= ctx
->vertex_buffers
;
3825 LLVMValueRef t_offset
;
3826 LLVMValueRef t_list
;
3827 LLVMValueRef args
[3];
3829 LLVMValueRef buffer_index
;
3830 int index
= variable
->data
.location
- VERT_ATTRIB_GENERIC0
;
3831 int idx
= variable
->data
.location
;
3832 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, true);
3834 variable
->data
.driver_location
= idx
* 4;
3836 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << index
)) {
3837 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->instance_id
,
3838 ctx
->start_instance
, "");
3839 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
3840 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
3842 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->vertex_id
,
3843 ctx
->base_vertex
, "");
3845 for (unsigned i
= 0; i
< attrib_count
; ++i
, ++idx
) {
3846 t_offset
= LLVMConstInt(ctx
->i32
, index
+ i
, false);
3848 t_list
= build_indexed_load_const(ctx
, t_list_ptr
, t_offset
);
3850 args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
3851 args
[2] = buffer_index
;
3852 input
= emit_llvm_intrinsic(ctx
,
3853 "llvm.SI.vs.load.input", ctx
->v4f32
, args
, 3,
3854 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_NOUNWIND
);
3856 for (unsigned chan
= 0; chan
< 4; chan
++) {
3857 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
3858 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
, chan
)] =
3859 to_integer(ctx
, LLVMBuildExtractElement(ctx
->builder
,
3860 input
, llvm_chan
, ""));
3866 static void interp_fs_input(struct nir_to_llvm_context
*ctx
,
3868 LLVMValueRef interp_param
,
3869 LLVMValueRef prim_mask
,
3870 LLVMValueRef result
[4])
3872 const char *intr_name
;
3873 LLVMValueRef attr_number
;
3876 attr_number
= LLVMConstInt(ctx
->i32
, attr
, false);
3878 /* fs.constant returns the param from the middle vertex, so it's not
3879 * really useful for flat shading. It's meant to be used for custom
3880 * interpolation (but the intrinsic can't fetch from the other two
3883 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
3884 * to do the right thing. The only reason we use fs.constant is that
3885 * fs.interp cannot be used on integers, because they can be equal
3888 intr_name
= interp_param
? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
3890 for (chan
= 0; chan
< 4; chan
++) {
3891 LLVMValueRef args
[4];
3892 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
3894 args
[0] = llvm_chan
;
3895 args
[1] = attr_number
;
3896 args
[2] = prim_mask
;
3897 args
[3] = interp_param
;
3898 result
[chan
] = emit_llvm_intrinsic(ctx
, intr_name
,
3899 ctx
->f32
, args
, args
[3] ? 4 : 3,
3900 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_NOUNWIND
);
3905 handle_fs_input_decl(struct nir_to_llvm_context
*ctx
,
3906 struct nir_variable
*variable
)
3908 int idx
= variable
->data
.location
;
3909 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
3910 LLVMValueRef interp
;
3912 variable
->data
.driver_location
= idx
* 4;
3913 ctx
->input_mask
|= ((1ull << attrib_count
) - 1) << variable
->data
.location
;
3915 if (glsl_get_base_type(glsl_without_array(variable
->type
)) == GLSL_TYPE_FLOAT
)
3916 interp
= lookup_interp_param(ctx
, variable
->data
.interpolation
, INTERP_CENTER
);
3920 for (unsigned i
= 0; i
< attrib_count
; ++i
)
3921 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
+ i
, 0)] = interp
;
3926 handle_shader_input_decl(struct nir_to_llvm_context
*ctx
,
3927 struct nir_variable
*variable
)
3929 switch (ctx
->stage
) {
3930 case MESA_SHADER_VERTEX
:
3931 handle_vs_input_decl(ctx
, variable
);
3933 case MESA_SHADER_FRAGMENT
:
3934 handle_fs_input_decl(ctx
, variable
);
3943 handle_fs_inputs_pre(struct nir_to_llvm_context
*ctx
,
3944 struct nir_shader
*nir
)
3947 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_INPUTS
; ++i
) {
3948 LLVMValueRef interp_param
;
3949 LLVMValueRef
*inputs
= ctx
->inputs
+radeon_llvm_reg_index_soa(i
, 0);
3951 if (!(ctx
->input_mask
& (1ull << i
)))
3954 if (i
>= VARYING_SLOT_VAR0
|| i
== VARYING_SLOT_PNTC
) {
3955 interp_param
= *inputs
;
3956 interp_fs_input(ctx
, index
, interp_param
, ctx
->prim_mask
,
3960 ctx
->shader_info
->fs
.flat_shaded_mask
|= 1u << index
;
3962 } else if (i
== VARYING_SLOT_POS
) {
3963 for(int i
= 0; i
< 3; ++i
)
3964 inputs
[i
] = ctx
->frag_pos
[i
];
3966 inputs
[3] = emit_fdiv(ctx
, ctx
->f32one
, ctx
->frag_pos
[3]);
3969 ctx
->shader_info
->fs
.num_interp
= index
;
3970 if (ctx
->input_mask
& (1 << VARYING_SLOT_PNTC
))
3971 ctx
->shader_info
->fs
.has_pcoord
= true;
3972 ctx
->shader_info
->fs
.input_mask
= ctx
->input_mask
>> VARYING_SLOT_VAR0
;
3976 ac_build_alloca(struct nir_to_llvm_context
*ctx
,
3980 LLVMBuilderRef builder
= ctx
->builder
;
3981 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
3982 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
3983 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
3984 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
3985 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ctx
->context
);
3989 LLVMPositionBuilderBefore(first_builder
, first_instr
);
3991 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
3994 res
= LLVMBuildAlloca(first_builder
, type
, name
);
3995 LLVMBuildStore(builder
, LLVMConstNull(type
), res
);
3997 LLVMDisposeBuilder(first_builder
);
4002 static LLVMValueRef
si_build_alloca_undef(struct nir_to_llvm_context
*ctx
,
4006 LLVMValueRef ptr
= ac_build_alloca(ctx
, type
, name
);
4007 LLVMBuildStore(ctx
->builder
, LLVMGetUndef(type
), ptr
);
4012 handle_shader_output_decl(struct nir_to_llvm_context
*ctx
,
4013 struct nir_variable
*variable
)
4015 int idx
= variable
->data
.location
;
4016 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4018 variable
->data
.driver_location
= idx
* 4;
4020 if (ctx
->stage
== MESA_SHADER_VERTEX
) {
4022 if (idx
== VARYING_SLOT_CLIP_DIST0
||
4023 idx
== VARYING_SLOT_CULL_DIST0
) {
4024 int length
= glsl_get_length(variable
->type
);
4025 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
4026 ctx
->shader_info
->vs
.clip_dist_mask
= (1 << length
) - 1;
4027 ctx
->num_clips
= length
;
4028 } else if (idx
== VARYING_SLOT_CULL_DIST0
) {
4029 ctx
->shader_info
->vs
.cull_dist_mask
= (1 << length
) - 1;
4030 ctx
->num_culls
= length
;
4039 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
4040 for (unsigned chan
= 0; chan
< 4; chan
++) {
4041 ctx
->outputs
[radeon_llvm_reg_index_soa(idx
+ i
, chan
)] =
4042 si_build_alloca_undef(ctx
, ctx
->f32
, "");
4045 ctx
->output_mask
|= ((1ull << attrib_count
) - 1) << variable
->data
.location
;
4049 setup_locals(struct nir_to_llvm_context
*ctx
,
4050 struct nir_function
*func
)
4053 ctx
->num_locals
= 0;
4054 nir_foreach_variable(variable
, &func
->impl
->locals
) {
4055 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4056 variable
->data
.driver_location
= ctx
->num_locals
* 4;
4057 ctx
->num_locals
+= attrib_count
;
4059 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
4063 for (i
= 0; i
< ctx
->num_locals
; i
++) {
4064 for (j
= 0; j
< 4; j
++) {
4065 ctx
->locals
[i
* 4 + j
] =
4066 si_build_alloca_undef(ctx
, ctx
->f32
, "temp");
4072 emit_float_saturate(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
, float lo
, float hi
)
4074 v
= to_float(ctx
, v
);
4075 v
= emit_intrin_2f_param(ctx
, "llvm.maxnum.f32", v
, LLVMConstReal(ctx
->f32
, lo
));
4076 return emit_intrin_2f_param(ctx
, "llvm.minnum.f32", v
, LLVMConstReal(ctx
->f32
, hi
));
4080 static LLVMValueRef
emit_pack_int16(struct nir_to_llvm_context
*ctx
,
4081 LLVMValueRef src0
, LLVMValueRef src1
)
4083 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
4084 LLVMValueRef comp
[2];
4086 comp
[0] = LLVMBuildAnd(ctx
->builder
, src0
, LLVMConstInt(ctx
-> i32
, 65535, 0), "");
4087 comp
[1] = LLVMBuildAnd(ctx
->builder
, src1
, LLVMConstInt(ctx
-> i32
, 65535, 0), "");
4088 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
4089 return LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
4092 /* Initialize arguments for the shader export intrinsic */
4094 si_llvm_init_export_args(struct nir_to_llvm_context
*ctx
,
4095 LLVMValueRef
*values
,
4099 /* Default is 0xf. Adjusted below depending on the format. */
4100 args
[0] = LLVMConstInt(ctx
->i32
, target
!= V_008DFC_SQ_EXP_NULL
? 0xf : 0, false);
4101 /* Specify whether the EXEC mask represents the valid mask */
4102 args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
4104 /* Specify whether this is the last export */
4105 args
[2] = LLVMConstInt(ctx
->i32
, 0, false);
4106 /* Specify the target we are exporting */
4107 args
[3] = LLVMConstInt(ctx
->i32
, target
, false);
4109 args
[4] = LLVMConstInt(ctx
->i32
, 0, false); /* COMPR flag */
4110 args
[5] = LLVMGetUndef(ctx
->f32
);
4111 args
[6] = LLVMGetUndef(ctx
->f32
);
4112 args
[7] = LLVMGetUndef(ctx
->f32
);
4113 args
[8] = LLVMGetUndef(ctx
->f32
);
4118 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&& target
>= V_008DFC_SQ_EXP_MRT
) {
4119 LLVMValueRef val
[4];
4120 unsigned index
= target
- V_008DFC_SQ_EXP_MRT
;
4121 unsigned col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * index
)) & 0xf;
4122 bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> index
) & 1;
4124 switch(col_format
) {
4125 case V_028714_SPI_SHADER_ZERO
:
4126 args
[0] = LLVMConstInt(ctx
->i32
, 0x0, 0);
4127 args
[3] = LLVMConstInt(ctx
->i32
, V_008DFC_SQ_EXP_NULL
, 0);
4130 case V_028714_SPI_SHADER_32_R
:
4131 args
[0] = LLVMConstInt(ctx
->i32
, 0x1, 0);
4132 args
[5] = values
[0];
4135 case V_028714_SPI_SHADER_32_GR
:
4136 args
[0] = LLVMConstInt(ctx
->i32
, 0x3, 0);
4137 args
[5] = values
[0];
4138 args
[6] = values
[1];
4141 case V_028714_SPI_SHADER_32_AR
:
4142 args
[0] = LLVMConstInt(ctx
->i32
, 0x9, 0);
4143 args
[5] = values
[0];
4144 args
[8] = values
[3];
4147 case V_028714_SPI_SHADER_FP16_ABGR
:
4148 args
[4] = ctx
->i32one
;
4150 for (unsigned chan
= 0; chan
< 2; chan
++) {
4151 LLVMValueRef pack_args
[2] = {
4153 values
[2 * chan
+ 1]
4155 LLVMValueRef packed
;
4157 packed
= emit_llvm_intrinsic(ctx
, "llvm.SI.packf16",
4158 ctx
->i32
, pack_args
, 2,
4159 AC_FUNC_ATTR_READNONE
);
4160 args
[chan
+ 5] = packed
;
4164 case V_028714_SPI_SHADER_UNORM16_ABGR
:
4165 for (unsigned chan
= 0; chan
< 4; chan
++) {
4166 val
[chan
] = emit_float_saturate(ctx
, values
[chan
], 0, 1);
4167 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
4168 LLVMConstReal(ctx
->f32
, 65535), "");
4169 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
4170 LLVMConstReal(ctx
->f32
, 0.5), "");
4171 val
[chan
] = LLVMBuildFPToUI(ctx
->builder
, val
[chan
],
4175 args
[4] = ctx
->i32one
;
4176 args
[5] = emit_pack_int16(ctx
, val
[0], val
[1]);
4177 args
[6] = emit_pack_int16(ctx
, val
[2], val
[3]);
4180 case V_028714_SPI_SHADER_SNORM16_ABGR
:
4181 for (unsigned chan
= 0; chan
< 4; chan
++) {
4182 val
[chan
] = emit_float_saturate(ctx
, values
[chan
], -1, 1);
4183 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
4184 LLVMConstReal(ctx
->f32
, 32767), "");
4186 /* If positive, add 0.5, else add -0.5. */
4187 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
4188 LLVMBuildSelect(ctx
->builder
,
4189 LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
,
4190 val
[chan
], ctx
->f32zero
, ""),
4191 LLVMConstReal(ctx
->f32
, 0.5),
4192 LLVMConstReal(ctx
->f32
, -0.5), ""), "");
4193 val
[chan
] = LLVMBuildFPToSI(ctx
->builder
, val
[chan
], ctx
->i32
, "");
4196 args
[4] = ctx
->i32one
;
4197 args
[5] = emit_pack_int16(ctx
, val
[0], val
[1]);
4198 args
[6] = emit_pack_int16(ctx
, val
[2], val
[3]);
4201 case V_028714_SPI_SHADER_UINT16_ABGR
: {
4202 LLVMValueRef max
= LLVMConstInt(ctx
->i32
, is_int8
? 255 : 65535, 0);
4204 for (unsigned chan
= 0; chan
< 4; chan
++) {
4205 val
[chan
] = to_integer(ctx
, values
[chan
]);
4206 val
[chan
] = emit_minmax_int(ctx
, LLVMIntULT
, val
[chan
], max
);
4209 args
[4] = ctx
->i32one
;
4210 args
[5] = emit_pack_int16(ctx
, val
[0], val
[1]);
4211 args
[6] = emit_pack_int16(ctx
, val
[2], val
[3]);
4215 case V_028714_SPI_SHADER_SINT16_ABGR
: {
4216 LLVMValueRef max
= LLVMConstInt(ctx
->i32
, is_int8
? 127 : 32767, 0);
4217 LLVMValueRef min
= LLVMConstInt(ctx
->i32
, is_int8
? -128 : -32768, 0);
4220 for (unsigned chan
= 0; chan
< 4; chan
++) {
4221 val
[chan
] = to_integer(ctx
, values
[chan
]);
4222 val
[chan
] = emit_minmax_int(ctx
, LLVMIntSLT
, val
[chan
], max
);
4223 val
[chan
] = emit_minmax_int(ctx
, LLVMIntSGT
, val
[chan
], min
);
4226 args
[4] = ctx
->i32one
;
4227 args
[5] = emit_pack_int16(ctx
, val
[0], val
[1]);
4228 args
[6] = emit_pack_int16(ctx
, val
[2], val
[3]);
4233 case V_028714_SPI_SHADER_32_ABGR
:
4234 memcpy(&args
[5], values
, sizeof(values
[0]) * 4);
4238 memcpy(&args
[5], values
, sizeof(values
[0]) * 4);
4240 for (unsigned i
= 5; i
< 9; ++i
)
4241 args
[i
] = to_float(ctx
, args
[i
]);
4245 handle_vs_outputs_post(struct nir_to_llvm_context
*ctx
,
4246 struct nir_shader
*nir
)
4248 uint32_t param_count
= 0;
4250 unsigned pos_idx
, num_pos_exports
= 0;
4251 LLVMValueRef args
[9];
4252 LLVMValueRef pos_args
[4][9] = { { 0 } };
4253 LLVMValueRef psize_value
= 0;
4255 const uint64_t clip_mask
= ctx
->output_mask
& ((1ull << VARYING_SLOT_CLIP_DIST0
) |
4256 (1ull << VARYING_SLOT_CLIP_DIST1
) |
4257 (1ull << VARYING_SLOT_CULL_DIST0
) |
4258 (1ull << VARYING_SLOT_CULL_DIST1
));
4261 LLVMValueRef slots
[8];
4264 if (ctx
->shader_info
->vs
.cull_dist_mask
)
4265 ctx
->shader_info
->vs
.cull_dist_mask
<<= ctx
->num_clips
;
4267 i
= VARYING_SLOT_CLIP_DIST0
;
4268 for (j
= 0; j
< ctx
->num_clips
; j
++)
4269 slots
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4270 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4271 i
= VARYING_SLOT_CULL_DIST0
;
4272 for (j
= 0; j
< ctx
->num_culls
; j
++)
4273 slots
[ctx
->num_clips
+ j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4274 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4276 for (i
= ctx
->num_clips
+ ctx
->num_culls
; i
< 8; i
++)
4277 slots
[i
] = LLVMGetUndef(ctx
->f32
);
4279 if (ctx
->num_clips
+ ctx
->num_culls
> 4) {
4280 target
= V_008DFC_SQ_EXP_POS
+ 3;
4281 si_llvm_init_export_args(ctx
, &slots
[4], target
, args
);
4282 memcpy(pos_args
[target
- V_008DFC_SQ_EXP_POS
],
4283 args
, sizeof(args
));
4286 target
= V_008DFC_SQ_EXP_POS
+ 2;
4287 si_llvm_init_export_args(ctx
, &slots
[0], target
, args
);
4288 memcpy(pos_args
[target
- V_008DFC_SQ_EXP_POS
],
4289 args
, sizeof(args
));
4293 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
4294 LLVMValueRef values
[4];
4295 if (!(ctx
->output_mask
& (1ull << i
)))
4298 for (unsigned j
= 0; j
< 4; j
++)
4299 values
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4300 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4302 if (i
== VARYING_SLOT_POS
) {
4303 target
= V_008DFC_SQ_EXP_POS
;
4304 } else if (i
== VARYING_SLOT_CLIP_DIST0
||
4305 i
== VARYING_SLOT_CLIP_DIST1
||
4306 i
== VARYING_SLOT_CULL_DIST0
||
4307 i
== VARYING_SLOT_CULL_DIST1
) {
4309 } else if (i
== VARYING_SLOT_PSIZ
) {
4310 ctx
->shader_info
->vs
.writes_pointsize
= true;
4311 psize_value
= values
[0];
4313 } else if (i
>= VARYING_SLOT_VAR0
) {
4314 ctx
->shader_info
->vs
.export_mask
|= 1u << (i
- VARYING_SLOT_VAR0
);
4315 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
4319 si_llvm_init_export_args(ctx
, values
, target
, args
);
4321 if (target
>= V_008DFC_SQ_EXP_POS
&&
4322 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
4323 memcpy(pos_args
[target
- V_008DFC_SQ_EXP_POS
],
4324 args
, sizeof(args
));
4326 emit_llvm_intrinsic(ctx
,
4328 LLVMVoidTypeInContext(ctx
->context
),
4333 /* We need to add the position output manually if it's missing. */
4334 if (!pos_args
[0][0]) {
4335 pos_args
[0][0] = LLVMConstInt(ctx
->i32
, 0xf, false);
4336 pos_args
[0][1] = ctx
->i32zero
; /* EXEC mask */
4337 pos_args
[0][2] = ctx
->i32zero
; /* last export? */
4338 pos_args
[0][3] = LLVMConstInt(ctx
->i32
, V_008DFC_SQ_EXP_POS
, false);
4339 pos_args
[0][4] = ctx
->i32zero
; /* COMPR flag */
4340 pos_args
[0][5] = ctx
->f32zero
; /* X */
4341 pos_args
[0][6] = ctx
->f32zero
; /* Y */
4342 pos_args
[0][7] = ctx
->f32zero
; /* Z */
4343 pos_args
[0][8] = ctx
->f32one
; /* W */
4346 if (ctx
->shader_info
->vs
.writes_pointsize
== true) {
4347 pos_args
[1][0] = LLVMConstInt(ctx
->i32
, (ctx
->shader_info
->vs
.writes_pointsize
== true), false); /* writemask */
4348 pos_args
[1][1] = ctx
->i32zero
; /* EXEC mask */
4349 pos_args
[1][2] = ctx
->i32zero
; /* last export? */
4350 pos_args
[1][3] = LLVMConstInt(ctx
->i32
, V_008DFC_SQ_EXP_POS
+ 1, false);
4351 pos_args
[1][4] = ctx
->i32zero
; /* COMPR flag */
4352 pos_args
[1][5] = ctx
->f32zero
; /* X */
4353 pos_args
[1][6] = ctx
->f32zero
; /* Y */
4354 pos_args
[1][7] = ctx
->f32zero
; /* Z */
4355 pos_args
[1][8] = ctx
->f32zero
; /* W */
4357 if (ctx
->shader_info
->vs
.writes_pointsize
== true)
4358 pos_args
[1][5] = psize_value
;
4360 for (i
= 0; i
< 4; i
++) {
4366 for (i
= 0; i
< 4; i
++) {
4367 if (!pos_args
[i
][0])
4370 /* Specify the target we are exporting */
4371 pos_args
[i
][3] = LLVMConstInt(ctx
->i32
, V_008DFC_SQ_EXP_POS
+ pos_idx
++, false);
4372 if (pos_idx
== num_pos_exports
)
4373 pos_args
[i
][2] = ctx
->i32one
;
4374 emit_llvm_intrinsic(ctx
,
4376 LLVMVoidTypeInContext(ctx
->context
),
4380 ctx
->shader_info
->vs
.pos_exports
= num_pos_exports
;
4381 ctx
->shader_info
->vs
.param_exports
= param_count
;
4385 si_export_mrt_color(struct nir_to_llvm_context
*ctx
,
4386 LLVMValueRef
*color
, unsigned param
, bool is_last
)
4388 LLVMValueRef args
[9];
4390 si_llvm_init_export_args(ctx
, color
, param
,
4394 args
[1] = ctx
->i32one
; /* whether the EXEC mask is valid */
4395 args
[2] = ctx
->i32one
; /* DONE bit */
4396 } else if (args
[0] == ctx
->i32zero
)
4397 return; /* unnecessary NULL export */
4399 emit_llvm_intrinsic(ctx
, "llvm.SI.export",
4400 ctx
->voidt
, args
, 9, 0);
4404 si_export_mrt_z(struct nir_to_llvm_context
*ctx
,
4405 LLVMValueRef depth
, LLVMValueRef stencil
,
4406 LLVMValueRef samplemask
)
4408 LLVMValueRef args
[9];
4410 args
[1] = ctx
->i32one
; /* whether the EXEC mask is valid */
4411 args
[2] = ctx
->i32one
; /* DONE bit */
4412 /* Specify the target we are exporting */
4413 args
[3] = LLVMConstInt(ctx
->i32
, V_008DFC_SQ_EXP_MRTZ
, false);
4415 args
[4] = ctx
->i32zero
; /* COMP flag */
4416 args
[5] = LLVMGetUndef(ctx
->f32
); /* R, depth */
4417 args
[6] = LLVMGetUndef(ctx
->f32
); /* G, stencil test val[0:7], stencil op val[8:15] */
4418 args
[7] = LLVMGetUndef(ctx
->f32
); /* B, sample mask */
4419 args
[8] = LLVMGetUndef(ctx
->f32
); /* A, alpha to mask */
4432 args
[7] = samplemask
;
4436 /* SI (except OLAND) has a bug that it only looks
4437 * at the X writemask component. */
4438 if (ctx
->options
->chip_class
== SI
&&
4439 ctx
->options
->family
!= CHIP_OLAND
)
4442 args
[0] = LLVMConstInt(ctx
->i32
, mask
, false);
4443 emit_llvm_intrinsic(ctx
, "llvm.SI.export",
4444 ctx
->voidt
, args
, 9, 0);
4448 handle_fs_outputs_post(struct nir_to_llvm_context
*ctx
,
4449 struct nir_shader
*nir
)
4452 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
4454 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
4455 LLVMValueRef values
[4];
4457 if (!(ctx
->output_mask
& (1ull << i
)))
4460 if (i
== FRAG_RESULT_DEPTH
) {
4461 ctx
->shader_info
->fs
.writes_z
= true;
4462 depth
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4463 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
4464 } else if (i
== FRAG_RESULT_STENCIL
) {
4465 ctx
->shader_info
->fs
.writes_stencil
= true;
4466 stencil
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4467 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
4470 for (unsigned j
= 0; j
< 4; j
++)
4471 values
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4472 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4474 if (!ctx
->shader_info
->fs
.writes_z
&& !ctx
->shader_info
->fs
.writes_stencil
)
4475 last
= ctx
->output_mask
<= ((1ull << (i
+ 1)) - 1);
4477 si_export_mrt_color(ctx
, values
, V_008DFC_SQ_EXP_MRT
+ index
, last
);
4482 if (depth
|| stencil
)
4483 si_export_mrt_z(ctx
, depth
, stencil
, samplemask
);
4485 si_export_mrt_color(ctx
, NULL
, V_008DFC_SQ_EXP_NULL
, true);
4487 ctx
->shader_info
->fs
.output_mask
= index
? ((1ull << index
) - 1) : 0;
4491 handle_shader_outputs_post(struct nir_to_llvm_context
*ctx
,
4492 struct nir_shader
*nir
)
4494 switch (ctx
->stage
) {
4495 case MESA_SHADER_VERTEX
:
4496 handle_vs_outputs_post(ctx
, nir
);
4498 case MESA_SHADER_FRAGMENT
:
4499 handle_fs_outputs_post(ctx
, nir
);
4507 handle_shared_compute_var(struct nir_to_llvm_context
*ctx
,
4508 struct nir_variable
*variable
, uint32_t *offset
, int idx
)
4510 unsigned size
= glsl_count_attribute_slots(variable
->type
, false);
4511 variable
->data
.driver_location
= *offset
;
4515 static void ac_llvm_finalize_module(struct nir_to_llvm_context
* ctx
)
4517 LLVMPassManagerRef passmgr
;
4518 /* Create the pass manager */
4519 passmgr
= LLVMCreateFunctionPassManagerForModule(
4522 /* This pass should eliminate all the load and store instructions */
4523 LLVMAddPromoteMemoryToRegisterPass(passmgr
);
4525 /* Add some optimization passes */
4526 LLVMAddScalarReplAggregatesPass(passmgr
);
4527 LLVMAddLICMPass(passmgr
);
4528 LLVMAddAggressiveDCEPass(passmgr
);
4529 LLVMAddCFGSimplificationPass(passmgr
);
4530 LLVMAddInstructionCombiningPass(passmgr
);
4533 LLVMInitializeFunctionPassManager(passmgr
);
4534 LLVMRunFunctionPassManager(passmgr
, ctx
->main_function
);
4535 LLVMFinalizeFunctionPassManager(passmgr
);
4537 LLVMDisposeBuilder(ctx
->builder
);
4538 LLVMDisposePassManager(passmgr
);
4542 LLVMModuleRef
ac_translate_nir_to_llvm(LLVMTargetMachineRef tm
,
4543 struct nir_shader
*nir
,
4544 struct ac_shader_variant_info
*shader_info
,
4545 const struct ac_nir_compiler_options
*options
)
4547 struct nir_to_llvm_context ctx
= {0};
4548 struct nir_function
*func
;
4549 ctx
.options
= options
;
4550 ctx
.shader_info
= shader_info
;
4551 ctx
.context
= LLVMContextCreate();
4552 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
4554 memset(shader_info
, 0, sizeof(*shader_info
));
4556 LLVMSetTarget(ctx
.module
, "amdgcn--");
4559 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
4560 ctx
.stage
= nir
->stage
;
4562 create_function(&ctx
, nir
);
4564 if (nir
->stage
== MESA_SHADER_COMPUTE
) {
4566 nir_foreach_variable(variable
, &nir
->shared
)
4570 uint32_t shared_size
= 0;
4572 LLVMTypeRef i8p
= LLVMPointerType(ctx
.i8
, LOCAL_ADDR_SPACE
);
4573 nir_foreach_variable(variable
, &nir
->shared
) {
4574 handle_shared_compute_var(&ctx
, variable
, &shared_size
, idx
);
4579 var
= LLVMAddGlobalInAddressSpace(ctx
.module
,
4580 LLVMArrayType(ctx
.i8
, shared_size
),
4583 LLVMSetAlignment(var
, 4);
4584 ctx
.shared_memory
= LLVMBuildBitCast(ctx
.builder
, var
, i8p
, "");
4588 nir_foreach_variable(variable
, &nir
->inputs
)
4589 handle_shader_input_decl(&ctx
, variable
);
4591 if (nir
->stage
== MESA_SHADER_FRAGMENT
)
4592 handle_fs_inputs_pre(&ctx
, nir
);
4594 nir_foreach_variable(variable
, &nir
->outputs
)
4595 handle_shader_output_decl(&ctx
, variable
);
4597 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4598 _mesa_key_pointer_equal
);
4599 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4600 _mesa_key_pointer_equal
);
4602 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
4604 setup_locals(&ctx
, func
);
4606 visit_cf_list(&ctx
, &func
->impl
->body
);
4607 phi_post_pass(&ctx
);
4609 handle_shader_outputs_post(&ctx
, nir
);
4610 LLVMBuildRetVoid(ctx
.builder
);
4612 ac_llvm_finalize_module(&ctx
);
4614 ralloc_free(ctx
.defs
);
4615 ralloc_free(ctx
.phis
);
4620 static void ac_diagnostic_handler(LLVMDiagnosticInfoRef di
, void *context
)
4622 unsigned *retval
= (unsigned *)context
;
4623 LLVMDiagnosticSeverity severity
= LLVMGetDiagInfoSeverity(di
);
4624 char *description
= LLVMGetDiagInfoDescription(di
);
4626 if (severity
== LLVMDSError
) {
4628 fprintf(stderr
, "LLVM triggered Diagnostic Handler: %s\n",
4632 LLVMDisposeMessage(description
);
4635 static unsigned ac_llvm_compile(LLVMModuleRef M
,
4636 struct ac_shader_binary
*binary
,
4637 LLVMTargetMachineRef tm
)
4639 unsigned retval
= 0;
4641 LLVMContextRef llvm_ctx
;
4642 LLVMMemoryBufferRef out_buffer
;
4643 unsigned buffer_size
;
4644 const char *buffer_data
;
4647 /* Setup Diagnostic Handler*/
4648 llvm_ctx
= LLVMGetModuleContext(M
);
4650 LLVMContextSetDiagnosticHandler(llvm_ctx
, ac_diagnostic_handler
,
4654 mem_err
= LLVMTargetMachineEmitToMemoryBuffer(tm
, M
, LLVMObjectFile
,
4657 /* Process Errors/Warnings */
4659 fprintf(stderr
, "%s: %s", __FUNCTION__
, err
);
4665 /* Extract Shader Code*/
4666 buffer_size
= LLVMGetBufferSize(out_buffer
);
4667 buffer_data
= LLVMGetBufferStart(out_buffer
);
4669 ac_elf_read(buffer_data
, buffer_size
, binary
);
4672 LLVMDisposeMemoryBuffer(out_buffer
);
4678 void ac_compile_nir_shader(LLVMTargetMachineRef tm
,
4679 struct ac_shader_binary
*binary
,
4680 struct ac_shader_config
*config
,
4681 struct ac_shader_variant_info
*shader_info
,
4682 struct nir_shader
*nir
,
4683 const struct ac_nir_compiler_options
*options
,
4687 LLVMModuleRef llvm_module
= ac_translate_nir_to_llvm(tm
, nir
, shader_info
,
4690 LLVMDumpModule(llvm_module
);
4692 memset(binary
, 0, sizeof(*binary
));
4693 int v
= ac_llvm_compile(llvm_module
, binary
, tm
);
4695 fprintf(stderr
, "compile failed\n");
4699 fprintf(stderr
, "disasm:\n%s\n", binary
->disasm_string
);
4701 ac_shader_binary_read_config(binary
, config
, 0);
4703 LLVMContextRef ctx
= LLVMGetModuleContext(llvm_module
);
4704 LLVMDisposeModule(llvm_module
);
4705 LLVMContextDispose(ctx
);
4707 if (nir
->stage
== MESA_SHADER_FRAGMENT
) {
4708 shader_info
->num_input_vgprs
= 0;
4709 if (G_0286CC_PERSP_SAMPLE_ENA(config
->spi_ps_input_addr
))
4710 shader_info
->num_input_vgprs
+= 2;
4711 if (G_0286CC_PERSP_CENTER_ENA(config
->spi_ps_input_addr
))
4712 shader_info
->num_input_vgprs
+= 2;
4713 if (G_0286CC_PERSP_CENTROID_ENA(config
->spi_ps_input_addr
))
4714 shader_info
->num_input_vgprs
+= 2;
4715 if (G_0286CC_PERSP_PULL_MODEL_ENA(config
->spi_ps_input_addr
))
4716 shader_info
->num_input_vgprs
+= 3;
4717 if (G_0286CC_LINEAR_SAMPLE_ENA(config
->spi_ps_input_addr
))
4718 shader_info
->num_input_vgprs
+= 2;
4719 if (G_0286CC_LINEAR_CENTER_ENA(config
->spi_ps_input_addr
))
4720 shader_info
->num_input_vgprs
+= 2;
4721 if (G_0286CC_LINEAR_CENTROID_ENA(config
->spi_ps_input_addr
))
4722 shader_info
->num_input_vgprs
+= 2;
4723 if (G_0286CC_LINE_STIPPLE_TEX_ENA(config
->spi_ps_input_addr
))
4724 shader_info
->num_input_vgprs
+= 1;
4725 if (G_0286CC_POS_X_FLOAT_ENA(config
->spi_ps_input_addr
))
4726 shader_info
->num_input_vgprs
+= 1;
4727 if (G_0286CC_POS_Y_FLOAT_ENA(config
->spi_ps_input_addr
))
4728 shader_info
->num_input_vgprs
+= 1;
4729 if (G_0286CC_POS_Z_FLOAT_ENA(config
->spi_ps_input_addr
))
4730 shader_info
->num_input_vgprs
+= 1;
4731 if (G_0286CC_POS_W_FLOAT_ENA(config
->spi_ps_input_addr
))
4732 shader_info
->num_input_vgprs
+= 1;
4733 if (G_0286CC_FRONT_FACE_ENA(config
->spi_ps_input_addr
))
4734 shader_info
->num_input_vgprs
+= 1;
4735 if (G_0286CC_ANCILLARY_ENA(config
->spi_ps_input_addr
))
4736 shader_info
->num_input_vgprs
+= 1;
4737 if (G_0286CC_SAMPLE_COVERAGE_ENA(config
->spi_ps_input_addr
))
4738 shader_info
->num_input_vgprs
+= 1;
4739 if (G_0286CC_POS_FIXED_PT_ENA(config
->spi_ps_input_addr
))
4740 shader_info
->num_input_vgprs
+= 1;
4742 config
->num_vgprs
= MAX2(config
->num_vgprs
, shader_info
->num_input_vgprs
);
4744 /* +3 for scratch wave offset and VCC */
4745 config
->num_sgprs
= MAX2(config
->num_sgprs
,
4746 shader_info
->num_input_sgprs
+ 3);
4747 if (nir
->stage
== MESA_SHADER_COMPUTE
) {
4748 for (int i
= 0; i
< 3; ++i
)
4749 shader_info
->cs
.block_size
[i
] = nir
->info
->cs
.local_size
[i
];
4752 if (nir
->stage
== MESA_SHADER_FRAGMENT
)
4753 shader_info
->fs
.early_fragment_test
= nir
->info
->fs
.early_fragment_tests
;