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 emit_llvm_intrinsic(struct nir_to_llvm_context
*ctx
, const char *name
,
144 LLVMTypeRef return_type
, LLVMValueRef
*params
,
145 unsigned param_count
, LLVMAttribute attribs
);
146 static LLVMValueRef
get_sampler_desc(struct nir_to_llvm_context
*ctx
,
147 nir_deref_var
*deref
,
148 enum desc_type desc_type
);
149 static unsigned radeon_llvm_reg_index_soa(unsigned index
, unsigned chan
)
151 return (index
* 4) + chan
;
154 static unsigned llvm_get_type_size(LLVMTypeRef type
)
156 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
159 case LLVMIntegerTypeKind
:
160 return LLVMGetIntTypeWidth(type
) / 8;
161 case LLVMFloatTypeKind
:
163 case LLVMPointerTypeKind
:
165 case LLVMVectorTypeKind
:
166 return LLVMGetVectorSize(type
) *
167 llvm_get_type_size(LLVMGetElementType(type
));
174 static void set_llvm_calling_convention(LLVMValueRef func
,
175 gl_shader_stage stage
)
177 enum radeon_llvm_calling_convention calling_conv
;
180 case MESA_SHADER_VERTEX
:
181 case MESA_SHADER_TESS_CTRL
:
182 case MESA_SHADER_TESS_EVAL
:
183 calling_conv
= RADEON_LLVM_AMDGPU_VS
;
185 case MESA_SHADER_GEOMETRY
:
186 calling_conv
= RADEON_LLVM_AMDGPU_GS
;
188 case MESA_SHADER_FRAGMENT
:
189 calling_conv
= RADEON_LLVM_AMDGPU_PS
;
191 case MESA_SHADER_COMPUTE
:
192 calling_conv
= RADEON_LLVM_AMDGPU_CS
;
195 unreachable("Unhandle shader type");
198 LLVMSetFunctionCallConv(func
, calling_conv
);
202 create_llvm_function(LLVMContextRef ctx
, LLVMModuleRef module
,
203 LLVMBuilderRef builder
, LLVMTypeRef
*return_types
,
204 unsigned num_return_elems
, LLVMTypeRef
*param_types
,
205 unsigned param_count
, unsigned array_params
,
206 unsigned sgpr_params
, bool unsafe_math
)
208 LLVMTypeRef main_function_type
, ret_type
;
209 LLVMBasicBlockRef main_function_body
;
211 if (num_return_elems
)
212 ret_type
= LLVMStructTypeInContext(ctx
, return_types
,
213 num_return_elems
, true);
215 ret_type
= LLVMVoidTypeInContext(ctx
);
217 /* Setup the function */
219 LLVMFunctionType(ret_type
, param_types
, param_count
, 0);
220 LLVMValueRef main_function
=
221 LLVMAddFunction(module
, "main", main_function_type
);
223 LLVMAppendBasicBlockInContext(ctx
, main_function
, "main_body");
224 LLVMPositionBuilderAtEnd(builder
, main_function_body
);
226 LLVMSetFunctionCallConv(main_function
, RADEON_LLVM_AMDGPU_CS
);
227 for (unsigned i
= 0; i
< sgpr_params
; ++i
) {
228 LLVMValueRef P
= LLVMGetParam(main_function
, i
);
230 if (i
< array_params
) {
231 LLVMAddAttribute(P
, LLVMByValAttribute
);
232 ac_add_attr_dereferenceable(P
, UINT64_MAX
);
235 LLVMAddAttribute(P
, LLVMInRegAttribute
);
239 /* These were copied from some LLVM test. */
240 LLVMAddTargetDependentFunctionAttr(main_function
,
241 "less-precise-fpmad",
243 LLVMAddTargetDependentFunctionAttr(main_function
,
246 LLVMAddTargetDependentFunctionAttr(main_function
,
249 LLVMAddTargetDependentFunctionAttr(main_function
,
253 return main_function
;
256 static LLVMTypeRef
const_array(LLVMTypeRef elem_type
, int num_elements
)
258 return LLVMPointerType(LLVMArrayType(elem_type
, num_elements
),
262 static LLVMValueRef
get_shared_memory_ptr(struct nir_to_llvm_context
*ctx
,
270 offset
= LLVMConstInt(ctx
->i32
, idx
, false);
272 ptr
= ctx
->shared_memory
;
273 ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &offset
, 1, "");
274 addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
275 ptr
= LLVMBuildBitCast(ctx
->builder
, ptr
, LLVMPointerType(type
, addr_space
), "");
279 static LLVMValueRef
to_integer(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
)
281 LLVMTypeRef type
= LLVMTypeOf(v
);
282 if (type
== ctx
->f32
) {
283 return LLVMBuildBitCast(ctx
->builder
, v
, ctx
->i32
, "");
284 } else if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
285 LLVMTypeRef elem_type
= LLVMGetElementType(type
);
286 if (elem_type
== ctx
->f32
) {
287 LLVMTypeRef nt
= LLVMVectorType(ctx
->i32
, LLVMGetVectorSize(type
));
288 return LLVMBuildBitCast(ctx
->builder
, v
, nt
, "");
294 static LLVMValueRef
to_float(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
)
296 LLVMTypeRef type
= LLVMTypeOf(v
);
297 if (type
== ctx
->i32
) {
298 return LLVMBuildBitCast(ctx
->builder
, v
, ctx
->f32
, "");
299 } else if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
300 LLVMTypeRef elem_type
= LLVMGetElementType(type
);
301 if (elem_type
== ctx
->i32
) {
302 LLVMTypeRef nt
= LLVMVectorType(ctx
->f32
, LLVMGetVectorSize(type
));
303 return LLVMBuildBitCast(ctx
->builder
, v
, nt
, "");
309 static LLVMValueRef
build_indexed_load(struct nir_to_llvm_context
*ctx
,
310 LLVMValueRef base_ptr
, LLVMValueRef index
,
313 LLVMValueRef pointer
;
314 LLVMValueRef indices
[] = {ctx
->i32zero
, index
};
316 pointer
= LLVMBuildGEP(ctx
->builder
, base_ptr
, indices
, 2, "");
318 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
319 return LLVMBuildLoad(ctx
->builder
, pointer
, "");
322 static LLVMValueRef
build_indexed_load_const(struct nir_to_llvm_context
*ctx
,
323 LLVMValueRef base_ptr
, LLVMValueRef index
)
325 LLVMValueRef result
= build_indexed_load(ctx
, base_ptr
, index
, true);
326 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
330 static void create_function(struct nir_to_llvm_context
*ctx
,
331 struct nir_shader
*nir
)
333 LLVMTypeRef arg_types
[23];
334 unsigned arg_idx
= 0;
335 unsigned array_count
= 0;
336 unsigned sgpr_count
= 0, user_sgpr_count
;
339 /* 1 for each descriptor set */
340 for (unsigned i
= 0; i
< 4; ++i
)
341 arg_types
[arg_idx
++] = const_array(ctx
->i8
, 1024 * 1024);
343 /* 1 for push constants and dynamic descriptors */
344 arg_types
[arg_idx
++] = const_array(ctx
->i8
, 1024 * 1024);
346 array_count
= arg_idx
;
347 switch (nir
->stage
) {
348 case MESA_SHADER_COMPUTE
:
349 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3); /* grid size */
350 user_sgpr_count
= arg_idx
;
351 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3);
352 arg_types
[arg_idx
++] = ctx
->i32
;
353 sgpr_count
= arg_idx
;
355 arg_types
[arg_idx
++] = LLVMVectorType(ctx
->i32
, 3);
357 case MESA_SHADER_VERTEX
:
358 arg_types
[arg_idx
++] = const_array(ctx
->v16i8
, 16); /* vertex buffers */
359 arg_types
[arg_idx
++] = ctx
->i32
; // base vertex
360 arg_types
[arg_idx
++] = ctx
->i32
; // start instance
361 user_sgpr_count
= sgpr_count
= arg_idx
;
362 arg_types
[arg_idx
++] = ctx
->i32
; // vertex id
363 arg_types
[arg_idx
++] = ctx
->i32
; // rel auto id
364 arg_types
[arg_idx
++] = ctx
->i32
; // vs prim id
365 arg_types
[arg_idx
++] = ctx
->i32
; // instance id
367 case MESA_SHADER_FRAGMENT
:
368 arg_types
[arg_idx
++] = const_array(ctx
->f32
, 32); /* sample positions */
369 user_sgpr_count
= arg_idx
;
370 arg_types
[arg_idx
++] = ctx
->i32
; /* prim mask */
371 sgpr_count
= arg_idx
;
372 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp sample */
373 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp center */
374 arg_types
[arg_idx
++] = ctx
->v2i32
; /* persp centroid */
375 arg_types
[arg_idx
++] = ctx
->v3i32
; /* persp pull model */
376 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear sample */
377 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear center */
378 arg_types
[arg_idx
++] = ctx
->v2i32
; /* linear centroid */
379 arg_types
[arg_idx
++] = ctx
->f32
; /* line stipple tex */
380 arg_types
[arg_idx
++] = ctx
->f32
; /* pos x float */
381 arg_types
[arg_idx
++] = ctx
->f32
; /* pos y float */
382 arg_types
[arg_idx
++] = ctx
->f32
; /* pos z float */
383 arg_types
[arg_idx
++] = ctx
->f32
; /* pos w float */
384 arg_types
[arg_idx
++] = ctx
->i32
; /* front face */
385 arg_types
[arg_idx
++] = ctx
->i32
; /* ancillary */
386 arg_types
[arg_idx
++] = ctx
->f32
; /* sample coverage */
387 arg_types
[arg_idx
++] = ctx
->i32
; /* fixed pt */
390 unreachable("Shader stage not implemented");
393 ctx
->main_function
= create_llvm_function(
394 ctx
->context
, ctx
->module
, ctx
->builder
, NULL
, 0, arg_types
,
395 arg_idx
, array_count
, sgpr_count
, ctx
->options
->unsafe_math
);
396 set_llvm_calling_convention(ctx
->main_function
, nir
->stage
);
399 ctx
->shader_info
->num_input_sgprs
= 0;
400 ctx
->shader_info
->num_input_vgprs
= 0;
402 for (i
= 0; i
< user_sgpr_count
; i
++)
403 ctx
->shader_info
->num_user_sgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
405 ctx
->shader_info
->num_input_sgprs
= ctx
->shader_info
->num_user_sgprs
;
406 for (; i
< sgpr_count
; i
++)
407 ctx
->shader_info
->num_input_sgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
409 if (nir
->stage
!= MESA_SHADER_FRAGMENT
)
410 for (; i
< arg_idx
; ++i
)
411 ctx
->shader_info
->num_input_vgprs
+= llvm_get_type_size(arg_types
[i
]) / 4;
414 for (unsigned i
= 0; i
< 4; ++i
)
415 ctx
->descriptor_sets
[i
] =
416 LLVMGetParam(ctx
->main_function
, arg_idx
++);
418 ctx
->push_constants
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
420 switch (nir
->stage
) {
421 case MESA_SHADER_COMPUTE
:
422 ctx
->num_work_groups
=
423 LLVMGetParam(ctx
->main_function
, arg_idx
++);
425 LLVMGetParam(ctx
->main_function
, arg_idx
++);
427 LLVMGetParam(ctx
->main_function
, arg_idx
++);
428 ctx
->local_invocation_ids
=
429 LLVMGetParam(ctx
->main_function
, arg_idx
++);
431 case MESA_SHADER_VERTEX
:
432 ctx
->vertex_buffers
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
433 ctx
->base_vertex
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
434 ctx
->start_instance
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
435 ctx
->vertex_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
436 ctx
->rel_auto_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
437 ctx
->vs_prim_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
438 ctx
->instance_id
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
440 case MESA_SHADER_FRAGMENT
:
441 ctx
->sample_positions
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
442 ctx
->prim_mask
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
443 ctx
->persp_sample
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
444 ctx
->persp_center
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
445 ctx
->persp_centroid
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
447 ctx
->linear_sample
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
448 ctx
->linear_center
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
449 ctx
->linear_centroid
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
450 arg_idx
++; /* line stipple */
451 ctx
->frag_pos
[0] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
452 ctx
->frag_pos
[1] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
453 ctx
->frag_pos
[2] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
454 ctx
->frag_pos
[3] = LLVMGetParam(ctx
->main_function
, arg_idx
++);
455 ctx
->front_face
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
456 ctx
->ancillary
= LLVMGetParam(ctx
->main_function
, arg_idx
++);
459 unreachable("Shader stage not implemented");
463 static void setup_types(struct nir_to_llvm_context
*ctx
)
465 LLVMValueRef args
[4];
467 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->context
);
468 ctx
->i1
= LLVMIntTypeInContext(ctx
->context
, 1);
469 ctx
->i8
= LLVMIntTypeInContext(ctx
->context
, 8);
470 ctx
->i16
= LLVMIntTypeInContext(ctx
->context
, 16);
471 ctx
->i32
= LLVMIntTypeInContext(ctx
->context
, 32);
472 ctx
->i64
= LLVMIntTypeInContext(ctx
->context
, 64);
473 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
474 ctx
->v3i32
= LLVMVectorType(ctx
->i32
, 3);
475 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
476 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
477 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
478 ctx
->f16
= LLVMHalfTypeInContext(ctx
->context
);
479 ctx
->v2f32
= LLVMVectorType(ctx
->f32
, 2);
480 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
481 ctx
->v16i8
= LLVMVectorType(ctx
->i8
, 16);
483 ctx
->i32zero
= LLVMConstInt(ctx
->i32
, 0, false);
484 ctx
->i32one
= LLVMConstInt(ctx
->i32
, 1, false);
485 ctx
->f32zero
= LLVMConstReal(ctx
->f32
, 0.0);
486 ctx
->f32one
= LLVMConstReal(ctx
->f32
, 1.0);
488 args
[0] = ctx
->f32zero
;
489 args
[1] = ctx
->f32zero
;
490 args
[2] = ctx
->f32zero
;
491 args
[3] = ctx
->f32one
;
492 ctx
->v4f32empty
= LLVMConstVector(args
, 4);
494 ctx
->range_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
496 ctx
->invariant_load_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
497 "invariant.load", 14);
498 ctx
->uniform_md_kind
=
499 LLVMGetMDKindIDInContext(ctx
->context
, "amdgpu.uniform", 14);
500 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
502 ctx
->fpmath_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
, "fpmath", 6);
504 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
505 ctx
->fpmath_md_2p5_ulp
= LLVMMDNodeInContext(ctx
->context
, args
, 1);
508 static int get_llvm_num_components(LLVMValueRef value
)
510 LLVMTypeRef type
= LLVMTypeOf(value
);
511 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
512 ? LLVMGetVectorSize(type
)
514 return num_components
;
517 static LLVMValueRef
llvm_extract_elem(struct nir_to_llvm_context
*ctx
,
521 int count
= get_llvm_num_components(value
);
523 assert(index
< count
);
527 return LLVMBuildExtractElement(ctx
->builder
, value
,
528 LLVMConstInt(ctx
->i32
, index
, false), "");
531 static LLVMValueRef
trim_vector(struct nir_to_llvm_context
*ctx
,
532 LLVMValueRef value
, unsigned count
)
534 unsigned num_components
= get_llvm_num_components(value
);
535 if (count
== num_components
)
538 LLVMValueRef masks
[] = {
539 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
540 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false)};
543 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
546 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
547 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
551 build_gather_values_extended(struct nir_to_llvm_context
*ctx
,
552 LLVMValueRef
*values
,
553 unsigned value_count
,
554 unsigned value_stride
,
557 LLVMBuilderRef builder
= ctx
->builder
;
562 if (value_count
== 1) {
564 return LLVMBuildLoad(builder
, values
[0], "");
568 for (i
= 0; i
< value_count
; i
++) {
569 LLVMValueRef value
= values
[i
* value_stride
];
571 value
= LLVMBuildLoad(builder
, value
, "");
574 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
575 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
576 vec
= LLVMBuildInsertElement(builder
, vec
, value
, index
, "");
583 build_store_values_extended(struct nir_to_llvm_context
*ctx
,
584 LLVMValueRef
*values
,
585 unsigned value_count
,
586 unsigned value_stride
,
589 LLVMBuilderRef builder
= ctx
->builder
;
592 if (value_count
== 1) {
593 LLVMBuildStore(builder
, vec
, values
[0]);
597 for (i
= 0; i
< value_count
; i
++) {
598 LLVMValueRef ptr
= values
[i
* value_stride
];
599 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
600 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
601 LLVMBuildStore(builder
, value
, ptr
);
606 build_gather_values(struct nir_to_llvm_context
*ctx
,
607 LLVMValueRef
*values
,
608 unsigned value_count
)
610 return build_gather_values_extended(ctx
, values
, value_count
, 1, false);
613 static LLVMTypeRef
get_def_type(struct nir_to_llvm_context
*ctx
,
616 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->context
, def
->bit_size
);
617 if (def
->num_components
> 1) {
618 type
= LLVMVectorType(type
, def
->num_components
);
623 static LLVMValueRef
get_src(struct nir_to_llvm_context
*ctx
, nir_src src
)
626 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->defs
, src
.ssa
);
627 return (LLVMValueRef
)entry
->data
;
631 static LLVMBasicBlockRef
get_block(struct nir_to_llvm_context
*ctx
,
634 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->defs
, b
);
635 return (LLVMBasicBlockRef
)entry
->data
;
638 static LLVMValueRef
get_alu_src(struct nir_to_llvm_context
*ctx
,
640 unsigned num_components
)
642 LLVMValueRef value
= get_src(ctx
, src
.src
);
643 bool need_swizzle
= false;
646 LLVMTypeRef type
= LLVMTypeOf(value
);
647 unsigned src_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
648 ? LLVMGetVectorSize(type
)
651 for (unsigned i
= 0; i
< num_components
; ++i
) {
652 assert(src
.swizzle
[i
] < src_components
);
653 if (src
.swizzle
[i
] != i
)
657 if (need_swizzle
|| num_components
!= src_components
) {
658 LLVMValueRef masks
[] = {
659 LLVMConstInt(ctx
->i32
, src
.swizzle
[0], false),
660 LLVMConstInt(ctx
->i32
, src
.swizzle
[1], false),
661 LLVMConstInt(ctx
->i32
, src
.swizzle
[2], false),
662 LLVMConstInt(ctx
->i32
, src
.swizzle
[3], false)};
664 if (src_components
> 1 && num_components
== 1) {
665 value
= LLVMBuildExtractElement(ctx
->builder
, value
,
667 } else if (src_components
== 1 && num_components
> 1) {
668 LLVMValueRef values
[] = {value
, value
, value
, value
};
669 value
= build_gather_values(ctx
, values
, num_components
);
671 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
672 value
= LLVMBuildShuffleVector(ctx
->builder
, value
, value
,
681 static LLVMValueRef
emit_int_cmp(struct nir_to_llvm_context
*ctx
,
682 LLVMIntPredicate pred
, LLVMValueRef src0
,
685 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
686 return LLVMBuildSelect(ctx
->builder
, result
,
687 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
688 LLVMConstInt(ctx
->i32
, 0, false), "");
691 static LLVMValueRef
emit_float_cmp(struct nir_to_llvm_context
*ctx
,
692 LLVMRealPredicate pred
, LLVMValueRef src0
,
696 src0
= to_float(ctx
, src0
);
697 src1
= to_float(ctx
, src1
);
698 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
699 return LLVMBuildSelect(ctx
->builder
, result
,
700 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
701 LLVMConstInt(ctx
->i32
, 0, false), "");
704 static LLVMValueRef
emit_intrin_1f_param(struct nir_to_llvm_context
*ctx
,
708 LLVMValueRef params
[] = {
711 return emit_llvm_intrinsic(ctx
, intrin
, ctx
->f32
, params
, 1, LLVMReadNoneAttribute
);
714 static LLVMValueRef
emit_intrin_2f_param(struct nir_to_llvm_context
*ctx
,
716 LLVMValueRef src0
, LLVMValueRef src1
)
718 LLVMValueRef params
[] = {
722 return emit_llvm_intrinsic(ctx
, intrin
, ctx
->f32
, params
, 2, LLVMReadNoneAttribute
);
725 static LLVMValueRef
emit_intrin_3f_param(struct nir_to_llvm_context
*ctx
,
727 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
729 LLVMValueRef params
[] = {
734 return emit_llvm_intrinsic(ctx
, intrin
, ctx
->f32
, params
, 3, LLVMReadNoneAttribute
);
737 static LLVMValueRef
emit_bcsel(struct nir_to_llvm_context
*ctx
,
738 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
740 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
742 return LLVMBuildSelect(ctx
->builder
, v
, src1
, src2
, "");
745 static LLVMValueRef
emit_find_lsb(struct nir_to_llvm_context
*ctx
,
748 LLVMValueRef params
[2] = {
751 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
752 * add special code to check for x=0. The reason is that
753 * the LLVM behavior for x=0 is different from what we
756 * The hardware already implements the correct behavior.
758 LLVMConstInt(ctx
->i32
, 1, false),
760 return emit_llvm_intrinsic(ctx
, "llvm.cttz.i32", ctx
->i32
, params
, 2, LLVMReadNoneAttribute
);
763 static LLVMValueRef
emit_ifind_msb(struct nir_to_llvm_context
*ctx
,
766 LLVMValueRef msb
= emit_llvm_intrinsic(ctx
, "llvm.AMDGPU.flbit.i32",
768 LLVMReadNoneAttribute
);
770 /* The HW returns the last bit index from MSB, but NIR wants
771 * the index from LSB. Invert it by doing "31 - msb". */
772 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
775 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
776 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
777 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
778 src0
, ctx
->i32zero
, ""),
779 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
780 src0
, all_ones
, ""), "");
782 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
785 static LLVMValueRef
emit_ufind_msb(struct nir_to_llvm_context
*ctx
,
788 LLVMValueRef args
[2] = {
792 LLVMValueRef msb
= emit_llvm_intrinsic(ctx
, "llvm.ctlz.i32",
793 ctx
->i32
, args
, ARRAY_SIZE(args
),
794 LLVMReadNoneAttribute
);
796 /* The HW returns the last bit index from MSB, but NIR wants
797 * the index from LSB. Invert it by doing "31 - msb". */
798 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
801 return LLVMBuildSelect(ctx
->builder
,
802 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src0
,
804 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
807 static LLVMValueRef
emit_minmax_int(struct nir_to_llvm_context
*ctx
,
808 LLVMIntPredicate pred
,
809 LLVMValueRef src0
, LLVMValueRef src1
)
811 return LLVMBuildSelect(ctx
->builder
,
812 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
817 static LLVMValueRef
emit_iabs(struct nir_to_llvm_context
*ctx
,
820 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
821 LLVMBuildNeg(ctx
->builder
, src0
, ""));
824 static LLVMValueRef
emit_fsign(struct nir_to_llvm_context
*ctx
,
827 LLVMValueRef cmp
, val
;
829 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, ctx
->f32zero
, "");
830 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->f32one
, src0
, "");
831 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, ctx
->f32zero
, "");
832 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(ctx
->f32
, -1.0), "");
836 static LLVMValueRef
emit_isign(struct nir_to_llvm_context
*ctx
,
839 LLVMValueRef cmp
, val
;
841 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, ctx
->i32zero
, "");
842 val
= LLVMBuildSelect(ctx
->builder
, cmp
, ctx
->i32one
, src0
, "");
843 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, ctx
->i32zero
, "");
844 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(ctx
->i32
, -1, true), "");
848 static LLVMValueRef
emit_ffract(struct nir_to_llvm_context
*ctx
,
851 const char *intr
= "llvm.floor.f32";
852 LLVMValueRef fsrc0
= to_float(ctx
, src0
);
853 LLVMValueRef params
[] = {
856 LLVMValueRef floor
= emit_llvm_intrinsic(ctx
, intr
,
858 LLVMReadNoneAttribute
);
859 return LLVMBuildFSub(ctx
->builder
, fsrc0
, floor
, "");
862 static LLVMValueRef
emit_uint_carry(struct nir_to_llvm_context
*ctx
,
864 LLVMValueRef src0
, LLVMValueRef src1
)
866 LLVMTypeRef ret_type
;
867 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
869 LLVMValueRef params
[] = { src0
, src1
};
870 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
873 res
= emit_llvm_intrinsic(ctx
, intrin
, ret_type
,
874 params
, 2, LLVMReadNoneAttribute
);
876 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
877 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
881 static LLVMValueRef
emit_b2f(struct nir_to_llvm_context
*ctx
,
884 return LLVMBuildAnd(ctx
->builder
, src0
, LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""), "");
887 static LLVMValueRef
emit_umul_high(struct nir_to_llvm_context
*ctx
,
888 LLVMValueRef src0
, LLVMValueRef src1
)
890 LLVMValueRef dst64
, result
;
891 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
892 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
894 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
895 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
896 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
900 static LLVMValueRef
emit_imul_high(struct nir_to_llvm_context
*ctx
,
901 LLVMValueRef src0
, LLVMValueRef src1
)
903 LLVMValueRef dst64
, result
;
904 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
905 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
907 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
908 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
909 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
913 static LLVMValueRef
emit_bitfield_extract(struct nir_to_llvm_context
*ctx
,
915 LLVMValueRef srcs
[3])
918 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
919 result
= emit_llvm_intrinsic(ctx
, intrin
, ctx
->i32
, srcs
, 3, LLVMReadNoneAttribute
);
921 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
925 static LLVMValueRef
emit_bitfield_insert(struct nir_to_llvm_context
*ctx
,
926 LLVMValueRef src0
, LLVMValueRef src1
,
927 LLVMValueRef src2
, LLVMValueRef src3
)
929 LLVMValueRef bfi_args
[3], result
;
931 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
932 LLVMBuildSub(ctx
->builder
,
933 LLVMBuildShl(ctx
->builder
,
938 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
941 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
944 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
945 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
947 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
948 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
949 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
951 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
955 static LLVMValueRef
emit_pack_half_2x16(struct nir_to_llvm_context
*ctx
,
958 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
960 LLVMValueRef comp
[2];
962 src0
= to_float(ctx
, src0
);
963 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32zero
, "");
964 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32one
, "");
965 for (i
= 0; i
< 2; i
++) {
966 comp
[i
] = LLVMBuildFPTrunc(ctx
->builder
, comp
[i
], ctx
->f16
, "");
967 comp
[i
] = LLVMBuildBitCast(ctx
->builder
, comp
[i
], ctx
->i16
, "");
968 comp
[i
] = LLVMBuildZExt(ctx
->builder
, comp
[i
], ctx
->i32
, "");
971 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
972 comp
[0] = LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
977 static LLVMValueRef
emit_unpack_half_2x16(struct nir_to_llvm_context
*ctx
,
980 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
981 LLVMValueRef temps
[2], result
, val
;
984 for (i
= 0; i
< 2; i
++) {
985 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
986 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
987 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
988 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
991 result
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), temps
[0],
993 result
= LLVMBuildInsertElement(ctx
->builder
, result
, temps
[1],
999 * Set range metadata on an instruction. This can only be used on load and
1000 * call instructions. If you know an instruction can only produce the values
1001 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
1002 * \p lo is the minimum value inclusive.
1003 * \p hi is the maximum value exclusive.
1005 static void set_range_metadata(struct nir_to_llvm_context
*ctx
,
1006 LLVMValueRef value
, unsigned lo
, unsigned hi
)
1008 LLVMValueRef range_md
, md_args
[2];
1009 LLVMTypeRef type
= LLVMTypeOf(value
);
1010 LLVMContextRef context
= LLVMGetTypeContext(type
);
1012 md_args
[0] = LLVMConstInt(type
, lo
, false);
1013 md_args
[1] = LLVMConstInt(type
, hi
, false);
1014 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
1015 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
1018 static LLVMValueRef
get_thread_id(struct nir_to_llvm_context
*ctx
)
1021 LLVMValueRef tid_args
[2];
1022 tid_args
[0] = LLVMConstInt(ctx
->i32
, 0xffffffff, false);
1023 tid_args
[1] = ctx
->i32zero
;
1024 tid_args
[1] = emit_llvm_intrinsic(ctx
,
1025 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
1026 tid_args
, 2, LLVMReadNoneAttribute
);
1028 tid
= emit_llvm_intrinsic(ctx
,
1029 "llvm.amdgcn.mbcnt.hi", ctx
->i32
,
1030 tid_args
, 2, LLVMReadNoneAttribute
);
1031 set_range_metadata(ctx
, tid
, 0, 64);
1036 * SI implements derivatives using the local data store (LDS)
1037 * All writes to the LDS happen in all executing threads at
1038 * the same time. TID is the Thread ID for the current
1039 * thread and is a value between 0 and 63, representing
1040 * the thread's position in the wavefront.
1042 * For the pixel shader threads are grouped into quads of four pixels.
1043 * The TIDs of the pixels of a quad are:
1051 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
1052 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
1053 * the current pixel's column, and masking with 0xfffffffe yields the TID
1054 * of the left pixel of the current pixel's row.
1056 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
1057 * adding 2 yields the TID of the pixel below the top pixel.
1059 /* masks for thread ID. */
1060 #define TID_MASK_TOP_LEFT 0xfffffffc
1061 #define TID_MASK_TOP 0xfffffffd
1062 #define TID_MASK_LEFT 0xfffffffe
1063 static LLVMValueRef
emit_ddxy(struct nir_to_llvm_context
*ctx
,
1064 nir_alu_instr
*instr
,
1067 LLVMValueRef indices
[2];
1068 LLVMValueRef store_ptr
, load_ptr0
, load_ptr1
;
1069 LLVMValueRef tl
, trbl
, result
;
1070 LLVMValueRef tl_tid
, trbl_tid
;
1071 LLVMValueRef args
[2];
1074 ctx
->has_ddxy
= true;
1076 ctx
->lds
= LLVMAddGlobalInAddressSpace(ctx
->module
,
1077 LLVMArrayType(ctx
->i32
, 64),
1078 "ddxy_lds", LOCAL_ADDR_SPACE
);
1080 indices
[0] = ctx
->i32zero
;
1081 indices
[1] = get_thread_id(ctx
);
1082 store_ptr
= LLVMBuildGEP(ctx
->builder
, ctx
->lds
,
1085 if (instr
->op
== nir_op_fddx_fine
|| instr
->op
== nir_op_fddx
)
1086 mask
= TID_MASK_LEFT
;
1087 else if (instr
->op
== nir_op_fddy_fine
|| instr
->op
== nir_op_fddy
)
1088 mask
= TID_MASK_TOP
;
1090 mask
= TID_MASK_TOP_LEFT
;
1092 tl_tid
= LLVMBuildAnd(ctx
->builder
, indices
[1],
1093 LLVMConstInt(ctx
->i32
, mask
, false), "");
1094 indices
[1] = tl_tid
;
1095 load_ptr0
= LLVMBuildGEP(ctx
->builder
, ctx
->lds
,
1098 /* for DDX we want to next X pixel, DDY next Y pixel. */
1099 if (instr
->op
== nir_op_fddx_fine
||
1100 instr
->op
== nir_op_fddx_coarse
||
1101 instr
->op
== nir_op_fddx
)
1106 trbl_tid
= LLVMBuildAdd(ctx
->builder
, indices
[1],
1107 LLVMConstInt(ctx
->i32
, idx
, false), "");
1108 indices
[1] = trbl_tid
;
1109 load_ptr1
= LLVMBuildGEP(ctx
->builder
, ctx
->lds
,
1112 if (ctx
->options
->family
>= CHIP_TONGA
) {
1113 args
[0] = LLVMBuildMul(ctx
->builder
, tl_tid
,
1114 LLVMConstInt(ctx
->i32
, 4, false), "");
1116 tl
= emit_llvm_intrinsic(ctx
, "llvm.amdgcn.ds.bpermute",
1118 LLVMReadNoneAttribute
);
1120 args
[0] = LLVMBuildMul(ctx
->builder
, trbl_tid
,
1121 LLVMConstInt(ctx
->i32
, 4, false), "");
1122 trbl
= emit_llvm_intrinsic(ctx
, "llvm.amdgcn.ds.bpermute",
1124 LLVMReadNoneAttribute
);
1126 LLVMBuildStore(ctx
->builder
, src0
, store_ptr
);
1128 tl
= LLVMBuildLoad(ctx
->builder
, load_ptr0
, "");
1129 trbl
= LLVMBuildLoad(ctx
->builder
, load_ptr1
, "");
1131 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, ctx
->f32
, "");
1132 trbl
= LLVMBuildBitCast(ctx
->builder
, trbl
, ctx
->f32
, "");
1133 result
= LLVMBuildFSub(ctx
->builder
, trbl
, tl
, "");
1138 * this takes an I,J coordinate pair,
1139 * and works out the X and Y derivatives.
1140 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
1142 static LLVMValueRef
emit_ddxy_interp(
1143 struct nir_to_llvm_context
*ctx
,
1144 LLVMValueRef interp_ij
)
1146 LLVMValueRef indices
[2];
1147 LLVMValueRef store_ptr
, load_ptr_x
, load_ptr_y
, load_ptr_ddx
, load_ptr_ddy
, temp
, temp2
;
1148 LLVMValueRef tl
, tr
, bl
, result
[4];
1152 ctx
->lds
= LLVMAddGlobalInAddressSpace(ctx
->module
,
1153 LLVMArrayType(ctx
->i32
, 64),
1154 "ddxy_lds", LOCAL_ADDR_SPACE
);
1156 indices
[0] = ctx
->i32zero
;
1157 indices
[1] = get_thread_id(ctx
);
1158 store_ptr
= LLVMBuildGEP(ctx
->builder
, ctx
->lds
,
1161 temp
= LLVMBuildAnd(ctx
->builder
, indices
[1],
1162 LLVMConstInt(ctx
->i32
, TID_MASK_LEFT
, false), "");
1164 temp2
= LLVMBuildAnd(ctx
->builder
, indices
[1],
1165 LLVMConstInt(ctx
->i32
, TID_MASK_TOP
, false), "");
1168 load_ptr_x
= LLVMBuildGEP(ctx
->builder
, ctx
->lds
,
1172 load_ptr_y
= LLVMBuildGEP(ctx
->builder
, ctx
->lds
,
1175 indices
[1] = LLVMBuildAdd(ctx
->builder
, temp
,
1176 LLVMConstInt(ctx
->i32
, 1, false), "");
1177 load_ptr_ddx
= LLVMBuildGEP(ctx
->builder
, ctx
->lds
,
1180 indices
[1] = LLVMBuildAdd(ctx
->builder
, temp2
,
1181 LLVMConstInt(ctx
->i32
, 2, false), "");
1182 load_ptr_ddy
= LLVMBuildGEP(ctx
->builder
, ctx
->lds
,
1185 for (c
= 0; c
< 2; ++c
) {
1186 LLVMValueRef store_val
;
1187 LLVMValueRef c_ll
= LLVMConstInt(ctx
->i32
, c
, false);
1189 store_val
= LLVMBuildExtractElement(ctx
->builder
,
1190 interp_ij
, c_ll
, "");
1191 LLVMBuildStore(ctx
->builder
,
1195 tl
= LLVMBuildLoad(ctx
->builder
, load_ptr_x
, "");
1196 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, ctx
->f32
, "");
1198 tr
= LLVMBuildLoad(ctx
->builder
, load_ptr_ddx
, "");
1199 tr
= LLVMBuildBitCast(ctx
->builder
, tr
, ctx
->f32
, "");
1201 result
[c
] = LLVMBuildFSub(ctx
->builder
, tr
, tl
, "");
1203 tl
= LLVMBuildLoad(ctx
->builder
, load_ptr_y
, "");
1204 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, ctx
->f32
, "");
1206 bl
= LLVMBuildLoad(ctx
->builder
, load_ptr_ddy
, "");
1207 bl
= LLVMBuildBitCast(ctx
->builder
, bl
, ctx
->f32
, "");
1209 result
[c
+ 2] = LLVMBuildFSub(ctx
->builder
, bl
, tl
, "");
1212 return build_gather_values(ctx
, result
, 4);
1215 static LLVMValueRef
emit_fdiv(struct nir_to_llvm_context
*ctx
,
1219 LLVMValueRef ret
= LLVMBuildFDiv(ctx
->builder
, num
, den
, "");
1221 if (!LLVMIsConstant(ret
))
1222 LLVMSetMetadata(ret
, ctx
->fpmath_md_kind
, ctx
->fpmath_md_2p5_ulp
);
1226 static void visit_alu(struct nir_to_llvm_context
*ctx
, nir_alu_instr
*instr
)
1228 LLVMValueRef src
[4], result
= NULL
;
1229 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
1230 unsigned src_components
;
1232 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
1233 switch (instr
->op
) {
1239 case nir_op_pack_half_2x16
:
1242 case nir_op_unpack_half_2x16
:
1246 src_components
= num_components
;
1249 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1250 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
1252 switch (instr
->op
) {
1258 src
[0] = to_float(ctx
, src
[0]);
1259 result
= LLVMBuildFNeg(ctx
->builder
, src
[0], "");
1262 result
= LLVMBuildNeg(ctx
->builder
, src
[0], "");
1265 result
= LLVMBuildNot(ctx
->builder
, src
[0], "");
1268 result
= LLVMBuildAdd(ctx
->builder
, src
[0], src
[1], "");
1271 src
[0] = to_float(ctx
, src
[0]);
1272 src
[1] = to_float(ctx
, src
[1]);
1273 result
= LLVMBuildFAdd(ctx
->builder
, src
[0], src
[1], "");
1276 src
[0] = to_float(ctx
, src
[0]);
1277 src
[1] = to_float(ctx
, src
[1]);
1278 result
= LLVMBuildFSub(ctx
->builder
, src
[0], src
[1], "");
1281 result
= LLVMBuildSub(ctx
->builder
, src
[0], src
[1], "");
1284 result
= LLVMBuildMul(ctx
->builder
, src
[0], src
[1], "");
1287 result
= LLVMBuildSRem(ctx
->builder
, src
[0], src
[1], "");
1290 result
= LLVMBuildURem(ctx
->builder
, src
[0], src
[1], "");
1293 src
[0] = to_float(ctx
, src
[0]);
1294 src
[1] = to_float(ctx
, src
[1]);
1295 result
= emit_fdiv(ctx
, src
[0], src
[1]);
1296 result
= emit_intrin_1f_param(ctx
, "llvm.floor.f32", result
);
1297 result
= LLVMBuildFMul(ctx
->builder
, src
[1] , result
, "");
1298 result
= LLVMBuildFSub(ctx
->builder
, src
[0], result
, "");
1301 src
[0] = to_float(ctx
, src
[0]);
1302 src
[1] = to_float(ctx
, src
[1]);
1303 result
= LLVMBuildFRem(ctx
->builder
, src
[0], src
[1], "");
1306 result
= LLVMBuildSDiv(ctx
->builder
, src
[0], src
[1], "");
1309 result
= LLVMBuildUDiv(ctx
->builder
, src
[0], src
[1], "");
1312 src
[0] = to_float(ctx
, src
[0]);
1313 src
[1] = to_float(ctx
, src
[1]);
1314 result
= LLVMBuildFMul(ctx
->builder
, src
[0], src
[1], "");
1317 src
[0] = to_float(ctx
, src
[0]);
1318 src
[1] = to_float(ctx
, src
[1]);
1319 result
= emit_fdiv(ctx
, src
[0], src
[1]);
1322 src
[0] = to_float(ctx
, src
[0]);
1323 result
= emit_fdiv(ctx
, ctx
->f32one
, src
[0]);
1326 result
= LLVMBuildAnd(ctx
->builder
, src
[0], src
[1], "");
1329 result
= LLVMBuildOr(ctx
->builder
, src
[0], src
[1], "");
1332 result
= LLVMBuildXor(ctx
->builder
, src
[0], src
[1], "");
1335 result
= LLVMBuildShl(ctx
->builder
, src
[0], src
[1], "");
1338 result
= LLVMBuildAShr(ctx
->builder
, src
[0], src
[1], "");
1341 result
= LLVMBuildLShr(ctx
->builder
, src
[0], src
[1], "");
1344 result
= emit_int_cmp(ctx
, LLVMIntSLT
, src
[0], src
[1]);
1347 result
= emit_int_cmp(ctx
, LLVMIntNE
, src
[0], src
[1]);
1350 result
= emit_int_cmp(ctx
, LLVMIntEQ
, src
[0], src
[1]);
1353 result
= emit_int_cmp(ctx
, LLVMIntSGE
, src
[0], src
[1]);
1356 result
= emit_int_cmp(ctx
, LLVMIntULT
, src
[0], src
[1]);
1359 result
= emit_int_cmp(ctx
, LLVMIntUGE
, src
[0], src
[1]);
1362 result
= emit_float_cmp(ctx
, LLVMRealUEQ
, src
[0], src
[1]);
1365 result
= emit_float_cmp(ctx
, LLVMRealUNE
, src
[0], src
[1]);
1368 result
= emit_float_cmp(ctx
, LLVMRealULT
, src
[0], src
[1]);
1371 result
= emit_float_cmp(ctx
, LLVMRealUGE
, src
[0], src
[1]);
1374 result
= emit_intrin_1f_param(ctx
, "llvm.fabs.f32", src
[0]);
1377 result
= emit_iabs(ctx
, src
[0]);
1380 result
= emit_minmax_int(ctx
, LLVMIntSGT
, src
[0], src
[1]);
1383 result
= emit_minmax_int(ctx
, LLVMIntSLT
, src
[0], src
[1]);
1386 result
= emit_minmax_int(ctx
, LLVMIntUGT
, src
[0], src
[1]);
1389 result
= emit_minmax_int(ctx
, LLVMIntULT
, src
[0], src
[1]);
1392 result
= emit_isign(ctx
, src
[0]);
1395 src
[0] = to_float(ctx
, src
[0]);
1396 result
= emit_fsign(ctx
, src
[0]);
1399 result
= emit_intrin_1f_param(ctx
, "llvm.floor.f32", src
[0]);
1402 result
= emit_intrin_1f_param(ctx
, "llvm.trunc.f32", src
[0]);
1405 result
= emit_intrin_1f_param(ctx
, "llvm.ceil.f32", src
[0]);
1407 case nir_op_fround_even
:
1408 result
= emit_intrin_1f_param(ctx
, "llvm.rint.f32", src
[0]);
1411 result
= emit_ffract(ctx
, src
[0]);
1414 result
= emit_intrin_1f_param(ctx
, "llvm.sin.f32", src
[0]);
1417 result
= emit_intrin_1f_param(ctx
, "llvm.cos.f32", src
[0]);
1420 result
= emit_intrin_1f_param(ctx
, "llvm.sqrt.f32", src
[0]);
1423 result
= emit_intrin_1f_param(ctx
, "llvm.exp2.f32", src
[0]);
1426 result
= emit_intrin_1f_param(ctx
, "llvm.log2.f32", src
[0]);
1429 result
= emit_intrin_1f_param(ctx
, "llvm.sqrt.f32", src
[0]);
1430 result
= emit_fdiv(ctx
, ctx
->f32one
, result
);
1433 result
= emit_intrin_2f_param(ctx
, "llvm.pow.f32", src
[0], src
[1]);
1436 result
= emit_intrin_2f_param(ctx
, "llvm.maxnum.f32", src
[0], src
[1]);
1439 result
= emit_intrin_2f_param(ctx
, "llvm.minnum.f32", src
[0], src
[1]);
1442 result
= emit_intrin_3f_param(ctx
, "llvm.fma.f32", src
[0], src
[1], src
[2]);
1444 case nir_op_ibitfield_extract
:
1445 result
= emit_bitfield_extract(ctx
, "llvm.AMDGPU.bfe.i32", src
);
1447 case nir_op_ubitfield_extract
:
1448 result
= emit_bitfield_extract(ctx
, "llvm.AMDGPU.bfe.u32", src
);
1450 case nir_op_bitfield_insert
:
1451 result
= emit_bitfield_insert(ctx
, src
[0], src
[1], src
[2], src
[3]);
1453 case nir_op_bitfield_reverse
:
1454 result
= emit_llvm_intrinsic(ctx
, "llvm.bitreverse.i32", ctx
->i32
, src
, 1, LLVMReadNoneAttribute
);
1456 case nir_op_bit_count
:
1457 result
= emit_llvm_intrinsic(ctx
, "llvm.ctpop.i32", ctx
->i32
, src
, 1, LLVMReadNoneAttribute
);
1462 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1463 src
[i
] = to_integer(ctx
, src
[i
]);
1464 result
= build_gather_values(ctx
, src
, num_components
);
1467 src
[0] = to_float(ctx
, src
[0]);
1468 result
= LLVMBuildFPToSI(ctx
->builder
, src
[0], ctx
->i32
, "");
1471 src
[0] = to_float(ctx
, src
[0]);
1472 result
= LLVMBuildFPToUI(ctx
->builder
, src
[0], ctx
->i32
, "");
1475 result
= LLVMBuildSIToFP(ctx
->builder
, src
[0], ctx
->f32
, "");
1478 result
= LLVMBuildUIToFP(ctx
->builder
, src
[0], ctx
->f32
, "");
1481 result
= emit_bcsel(ctx
, src
[0], src
[1], src
[2]);
1483 case nir_op_find_lsb
:
1484 result
= emit_find_lsb(ctx
, src
[0]);
1486 case nir_op_ufind_msb
:
1487 result
= emit_ufind_msb(ctx
, src
[0]);
1489 case nir_op_ifind_msb
:
1490 result
= emit_ifind_msb(ctx
, src
[0]);
1492 case nir_op_uadd_carry
:
1493 result
= emit_uint_carry(ctx
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
1495 case nir_op_usub_borrow
:
1496 result
= emit_uint_carry(ctx
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
1499 result
= emit_b2f(ctx
, src
[0]);
1501 case nir_op_fquantize2f16
:
1502 src
[0] = to_float(ctx
, src
[0]);
1503 result
= LLVMBuildFPTrunc(ctx
->builder
, src
[0], ctx
->f16
, "");
1504 /* need to convert back up to f32 */
1505 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
1507 case nir_op_umul_high
:
1508 result
= emit_umul_high(ctx
, src
[0], src
[1]);
1510 case nir_op_imul_high
:
1511 result
= emit_imul_high(ctx
, src
[0], src
[1]);
1513 case nir_op_pack_half_2x16
:
1514 result
= emit_pack_half_2x16(ctx
, src
[0]);
1516 case nir_op_unpack_half_2x16
:
1517 result
= emit_unpack_half_2x16(ctx
, src
[0]);
1521 case nir_op_fddx_fine
:
1522 case nir_op_fddy_fine
:
1523 case nir_op_fddx_coarse
:
1524 case nir_op_fddy_coarse
:
1525 result
= emit_ddxy(ctx
, instr
, src
[0]);
1528 fprintf(stderr
, "Unknown NIR alu instr: ");
1529 nir_print_instr(&instr
->instr
, stderr
);
1530 fprintf(stderr
, "\n");
1535 assert(instr
->dest
.dest
.is_ssa
);
1536 result
= to_integer(ctx
, result
);
1537 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.dest
.ssa
,
1542 static void visit_load_const(struct nir_to_llvm_context
*ctx
,
1543 nir_load_const_instr
*instr
)
1545 LLVMValueRef values
[4], value
= NULL
;
1546 LLVMTypeRef element_type
=
1547 LLVMIntTypeInContext(ctx
->context
, instr
->def
.bit_size
);
1549 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1550 switch (instr
->def
.bit_size
) {
1552 values
[i
] = LLVMConstInt(element_type
,
1553 instr
->value
.u32
[i
], false);
1556 values
[i
] = LLVMConstInt(element_type
,
1557 instr
->value
.u64
[i
], false);
1561 "unsupported nir load_const bit_size: %d\n",
1562 instr
->def
.bit_size
);
1566 if (instr
->def
.num_components
> 1) {
1567 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1571 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, value
);
1574 static LLVMValueRef
cast_ptr(struct nir_to_llvm_context
*ctx
, LLVMValueRef ptr
,
1577 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
1578 return LLVMBuildBitCast(ctx
->builder
, ptr
,
1579 LLVMPointerType(type
, addr_space
), "");
1583 emit_llvm_intrinsic(struct nir_to_llvm_context
*ctx
, const char *name
,
1584 LLVMTypeRef return_type
, LLVMValueRef
*params
,
1585 unsigned param_count
, LLVMAttribute attribs
)
1587 LLVMValueRef function
;
1589 function
= LLVMGetNamedFunction(ctx
->module
, name
);
1591 LLVMTypeRef param_types
[32], function_type
;
1594 assert(param_count
<= 32);
1596 for (i
= 0; i
< param_count
; ++i
) {
1598 param_types
[i
] = LLVMTypeOf(params
[i
]);
1601 LLVMFunctionType(return_type
, param_types
, param_count
, 0);
1602 function
= LLVMAddFunction(ctx
->module
, name
, function_type
);
1604 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
1605 LLVMSetLinkage(function
, LLVMExternalLinkage
);
1607 LLVMAddFunctionAttr(function
, attribs
| LLVMNoUnwindAttribute
);
1609 return LLVMBuildCall(ctx
->builder
, function
, params
, param_count
, "");
1613 get_buffer_size(struct nir_to_llvm_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1616 LLVMBuildExtractElement(ctx
->builder
, descriptor
,
1617 LLVMConstInt(ctx
->i32
, 2, false), "");
1620 if (ctx
->options
->chip_class
>= VI
&& in_elements
) {
1621 /* On VI, the descriptor contains the size in bytes,
1622 * but TXQ must return the size in elements.
1623 * The stride is always non-zero for resources using TXQ.
1625 LLVMValueRef stride
=
1626 LLVMBuildExtractElement(ctx
->builder
, descriptor
,
1627 LLVMConstInt(ctx
->i32
, 1, false), "");
1628 stride
= LLVMBuildLShr(ctx
->builder
, stride
,
1629 LLVMConstInt(ctx
->i32
, 16, false), "");
1630 stride
= LLVMBuildAnd(ctx
->builder
, stride
,
1631 LLVMConstInt(ctx
->i32
, 0x3fff, false), "");
1633 size
= LLVMBuildUDiv(ctx
->builder
, size
, stride
, "");
1639 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
1642 static void build_int_type_name(
1644 char *buf
, unsigned bufsize
)
1646 assert(bufsize
>= 6);
1648 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
1649 snprintf(buf
, bufsize
, "v%ui32",
1650 LLVMGetVectorSize(type
));
1655 static LLVMValueRef
radv_lower_gather4_integer(struct nir_to_llvm_context
*ctx
,
1656 struct ac_tex_info
*tinfo
,
1657 nir_tex_instr
*instr
,
1658 const char *intr_name
,
1659 unsigned coord_vgpr_index
)
1661 LLVMValueRef coord
= tinfo
->args
[0];
1662 LLVMValueRef half_texel
[2];
1667 LLVMValueRef txq_args
[10];
1668 int txq_arg_count
= 0;
1670 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
1671 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, false);
1672 txq_args
[txq_arg_count
++] = tinfo
->args
[1];
1673 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0xf, 0); /* dmask */
1674 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* unorm */
1675 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* r128 */
1676 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, da
? 1 : 0, 0);
1677 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* glc */
1678 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* slc */
1679 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* tfe */
1680 txq_args
[txq_arg_count
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* lwe */
1681 size
= emit_llvm_intrinsic(ctx
, "llvm.SI.getresinfo.i32", ctx
->v4i32
,
1682 txq_args
, txq_arg_count
,
1683 LLVMReadNoneAttribute
);
1685 for (c
= 0; c
< 2; c
++) {
1686 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
1688 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1689 half_texel
[c
] = emit_fdiv(ctx
, ctx
->f32one
, half_texel
[c
]);
1690 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1691 LLVMConstReal(ctx
->f32
, -0.5), "");
1695 for (c
= 0; c
< 2; c
++) {
1697 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, coord_vgpr_index
+ c
, 0);
1698 tmp
= LLVMBuildExtractElement(ctx
->builder
, coord
, index
, "");
1699 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1700 tmp
= LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
1701 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1702 coord
= LLVMBuildInsertElement(ctx
->builder
, coord
, tmp
, index
, "");
1705 tinfo
->args
[0] = coord
;
1706 return emit_llvm_intrinsic(ctx
, intr_name
, tinfo
->dst_type
, tinfo
->args
, tinfo
->arg_count
,
1707 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1711 static LLVMValueRef
build_tex_intrinsic(struct nir_to_llvm_context
*ctx
,
1712 nir_tex_instr
*instr
,
1713 struct ac_tex_info
*tinfo
)
1715 const char *name
= "llvm.SI.image.sample";
1716 const char *infix
= "";
1717 char intr_name
[127];
1719 bool is_shadow
= instr
->is_shadow
;
1720 bool has_offset
= tinfo
->has_offset
;
1721 switch (instr
->op
) {
1723 case nir_texop_txf_ms
:
1724 case nir_texop_samples_identical
:
1725 name
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
? "llvm.SI.image.load" :
1726 instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
? "llvm.SI.vs.load.input" :
1727 "llvm.SI.image.load.mip";
1738 name
= "llvm.SI.getresinfo";
1740 case nir_texop_query_levels
:
1741 name
= "llvm.SI.getresinfo";
1744 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
1751 name
= "llvm.SI.gather4";
1755 name
= "llvm.SI.getlod";
1763 build_int_type_name(LLVMTypeOf(tinfo
->args
[0]), type
, sizeof(type
));
1764 sprintf(intr_name
, "%s%s%s%s.%s", name
, is_shadow
? ".c" : "", infix
,
1765 has_offset
? ".o" : "", type
);
1767 if (instr
->op
== nir_texop_tg4
) {
1768 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
1769 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
1770 return radv_lower_gather4_integer(ctx
, tinfo
, instr
, intr_name
,
1771 (int)has_offset
+ (int)is_shadow
);
1774 return emit_llvm_intrinsic(ctx
, intr_name
, tinfo
->dst_type
, tinfo
->args
, tinfo
->arg_count
,
1775 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1779 static LLVMValueRef
visit_vulkan_resource_index(struct nir_to_llvm_context
*ctx
,
1780 nir_intrinsic_instr
*instr
)
1782 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
1783 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
1784 unsigned binding
= nir_intrinsic_binding(instr
);
1785 LLVMValueRef desc_ptr
= ctx
->descriptor_sets
[desc_set
];
1786 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[desc_set
].layout
;
1787 unsigned base_offset
= layout
->binding
[binding
].offset
;
1788 LLVMValueRef offset
, stride
;
1790 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
1791 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
1792 desc_ptr
= ctx
->push_constants
;
1793 base_offset
= ctx
->options
->layout
->push_constant_size
;
1794 base_offset
+= 16 * layout
->binding
[binding
].dynamic_offset_offset
;
1795 stride
= LLVMConstInt(ctx
->i32
, 16, false);
1797 stride
= LLVMConstInt(ctx
->i32
, layout
->binding
[binding
].size
, false);
1799 offset
= LLVMConstInt(ctx
->i32
, base_offset
, false);
1800 index
= LLVMBuildMul(ctx
->builder
, index
, stride
, "");
1801 offset
= LLVMBuildAdd(ctx
->builder
, offset
, index
, "");
1803 LLVMValueRef indices
[] = {ctx
->i32zero
, offset
};
1804 desc_ptr
= LLVMBuildGEP(ctx
->builder
, desc_ptr
, indices
, 2, "");
1805 desc_ptr
= cast_ptr(ctx
, desc_ptr
, ctx
->v4i32
);
1806 LLVMSetMetadata(desc_ptr
, ctx
->uniform_md_kind
, ctx
->empty_md
);
1808 return LLVMBuildLoad(ctx
->builder
, desc_ptr
, "");
1811 static LLVMValueRef
visit_load_push_constant(struct nir_to_llvm_context
*ctx
,
1812 nir_intrinsic_instr
*instr
)
1816 LLVMValueRef indices
[] = {ctx
->i32zero
, get_src(ctx
, instr
->src
[0])};
1817 ptr
= LLVMBuildGEP(ctx
->builder
, ctx
->push_constants
, indices
, 2, "");
1818 ptr
= cast_ptr(ctx
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
1820 return LLVMBuildLoad(ctx
->builder
, ptr
, "");
1823 static LLVMValueRef
visit_get_buffer_size(struct nir_to_llvm_context
*ctx
,
1824 nir_intrinsic_instr
*instr
)
1826 LLVMValueRef desc
= get_src(ctx
, instr
->src
[0]);
1828 return get_buffer_size(ctx
, desc
, false);
1830 static void visit_store_ssbo(struct nir_to_llvm_context
*ctx
,
1831 nir_intrinsic_instr
*instr
)
1833 const char *store_name
;
1834 LLVMTypeRef data_type
= ctx
->f32
;
1835 unsigned writemask
= nir_intrinsic_write_mask(instr
);
1836 LLVMValueRef base_data
, base_offset
;
1837 LLVMValueRef params
[6];
1839 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
1840 ctx
->shader_info
->fs
.writes_memory
= true;
1842 params
[1] = get_src(ctx
, instr
->src
[1]);
1843 params
[2] = LLVMConstInt(ctx
->i32
, 0, false); /* vindex */
1844 params
[4] = LLVMConstInt(ctx
->i1
, 0, false); /* glc */
1845 params
[5] = LLVMConstInt(ctx
->i1
, 0, false); /* slc */
1847 if (instr
->num_components
> 1)
1848 data_type
= LLVMVectorType(ctx
->f32
, instr
->num_components
);
1850 base_data
= to_float(ctx
, get_src(ctx
, instr
->src
[0]));
1851 base_data
= trim_vector(ctx
, base_data
, instr
->num_components
);
1852 base_data
= LLVMBuildBitCast(ctx
->builder
, base_data
,
1854 base_offset
= get_src(ctx
, instr
->src
[2]); /* voffset */
1858 LLVMValueRef offset
;
1860 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
1862 /* Due to an LLVM limitation, split 3-element writes
1863 * into a 2-element and a 1-element write. */
1865 writemask
|= 1 << (start
+ 2);
1870 store_name
= "llvm.amdgcn.buffer.store.v4f32";
1872 } else if (count
== 2) {
1873 tmp
= LLVMBuildExtractElement(ctx
->builder
,
1874 base_data
, LLVMConstInt(ctx
->i32
, start
, false), "");
1875 data
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), tmp
,
1878 tmp
= LLVMBuildExtractElement(ctx
->builder
,
1879 base_data
, LLVMConstInt(ctx
->i32
, start
+ 1, false), "");
1880 data
= LLVMBuildInsertElement(ctx
->builder
, data
, tmp
,
1882 store_name
= "llvm.amdgcn.buffer.store.v2f32";
1886 if (get_llvm_num_components(base_data
) > 1)
1887 data
= LLVMBuildExtractElement(ctx
->builder
, base_data
,
1888 LLVMConstInt(ctx
->i32
, start
, false), "");
1891 store_name
= "llvm.amdgcn.buffer.store.f32";
1894 offset
= base_offset
;
1896 offset
= LLVMBuildAdd(ctx
->builder
, offset
, LLVMConstInt(ctx
->i32
, start
* 4, false), "");
1900 emit_llvm_intrinsic(ctx
, store_name
,
1901 LLVMVoidTypeInContext(ctx
->context
), params
, 6, 0);
1905 static LLVMValueRef
visit_atomic_ssbo(struct nir_to_llvm_context
*ctx
,
1906 nir_intrinsic_instr
*instr
)
1909 LLVMValueRef params
[5];
1911 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
1912 ctx
->shader_info
->fs
.writes_memory
= true;
1914 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
1915 params
[arg_count
++] = get_src(ctx
, instr
->src
[3]);
1917 params
[arg_count
++] = get_src(ctx
, instr
->src
[2]);
1918 params
[arg_count
++] = get_src(ctx
, instr
->src
[0]);
1919 params
[arg_count
++] = LLVMConstInt(ctx
->i32
, 0, false); /* vindex */
1920 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1921 params
[arg_count
++] = LLVMConstInt(ctx
->i1
, 0, false); /* slc */
1923 switch (instr
->intrinsic
) {
1924 case nir_intrinsic_ssbo_atomic_add
:
1925 name
= "llvm.amdgcn.buffer.atomic.add";
1927 case nir_intrinsic_ssbo_atomic_imin
:
1928 name
= "llvm.amdgcn.buffer.atomic.smin";
1930 case nir_intrinsic_ssbo_atomic_umin
:
1931 name
= "llvm.amdgcn.buffer.atomic.umin";
1933 case nir_intrinsic_ssbo_atomic_imax
:
1934 name
= "llvm.amdgcn.buffer.atomic.smax";
1936 case nir_intrinsic_ssbo_atomic_umax
:
1937 name
= "llvm.amdgcn.buffer.atomic.umax";
1939 case nir_intrinsic_ssbo_atomic_and
:
1940 name
= "llvm.amdgcn.buffer.atomic.and";
1942 case nir_intrinsic_ssbo_atomic_or
:
1943 name
= "llvm.amdgcn.buffer.atomic.or";
1945 case nir_intrinsic_ssbo_atomic_xor
:
1946 name
= "llvm.amdgcn.buffer.atomic.xor";
1948 case nir_intrinsic_ssbo_atomic_exchange
:
1949 name
= "llvm.amdgcn.buffer.atomic.swap";
1951 case nir_intrinsic_ssbo_atomic_comp_swap
:
1952 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
1958 return emit_llvm_intrinsic(ctx
, name
, ctx
->i32
, params
, arg_count
, 0);
1961 static LLVMValueRef
visit_load_buffer(struct nir_to_llvm_context
*ctx
,
1962 nir_intrinsic_instr
*instr
)
1964 const char *load_name
;
1965 LLVMTypeRef data_type
= ctx
->f32
;
1966 if (instr
->num_components
== 3)
1967 data_type
= LLVMVectorType(ctx
->f32
, 4);
1968 else if (instr
->num_components
> 1)
1969 data_type
= LLVMVectorType(ctx
->f32
, instr
->num_components
);
1971 if (instr
->num_components
== 4 || instr
->num_components
== 3)
1972 load_name
= "llvm.amdgcn.buffer.load.v4f32";
1973 else if (instr
->num_components
== 2)
1974 load_name
= "llvm.amdgcn.buffer.load.v2f32";
1975 else if (instr
->num_components
== 1)
1976 load_name
= "llvm.amdgcn.buffer.load.f32";
1980 LLVMValueRef params
[] = {
1981 get_src(ctx
, instr
->src
[0]),
1982 LLVMConstInt(ctx
->i32
, 0, false),
1983 get_src(ctx
, instr
->src
[1]),
1984 LLVMConstInt(ctx
->i1
, 0, false),
1985 LLVMConstInt(ctx
->i1
, 0, false),
1989 emit_llvm_intrinsic(ctx
, load_name
, data_type
, params
, 5, 0);
1991 if (instr
->num_components
== 3)
1992 ret
= trim_vector(ctx
, ret
, 3);
1994 return LLVMBuildBitCast(ctx
->builder
, ret
,
1995 get_def_type(ctx
, &instr
->dest
.ssa
), "");
1999 radv_get_deref_offset(struct nir_to_llvm_context
*ctx
, nir_deref
*tail
,
2000 bool vs_in
, unsigned *const_out
, LLVMValueRef
*indir_out
)
2002 unsigned const_offset
= 0;
2003 LLVMValueRef offset
= NULL
;
2006 while (tail
->child
!= NULL
) {
2007 const struct glsl_type
*parent_type
= tail
->type
;
2010 if (tail
->deref_type
== nir_deref_type_array
) {
2011 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
2012 LLVMValueRef index
, stride
, local_offset
;
2013 unsigned size
= glsl_count_attribute_slots(tail
->type
, vs_in
);
2015 const_offset
+= size
* deref_array
->base_offset
;
2016 if (deref_array
->deref_array_type
== nir_deref_array_type_direct
)
2019 assert(deref_array
->deref_array_type
== nir_deref_array_type_indirect
);
2020 index
= get_src(ctx
, deref_array
->indirect
);
2021 stride
= LLVMConstInt(ctx
->i32
, size
, 0);
2022 local_offset
= LLVMBuildMul(ctx
->builder
, stride
, index
, "");
2025 offset
= LLVMBuildAdd(ctx
->builder
, offset
, local_offset
, "");
2027 offset
= local_offset
;
2028 } else if (tail
->deref_type
== nir_deref_type_struct
) {
2029 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
2031 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
2032 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
2033 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
2036 unreachable("unsupported deref type");
2040 if (const_offset
&& offset
)
2041 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
2042 LLVMConstInt(ctx
->i32
, const_offset
, 0),
2045 *const_out
= const_offset
;
2046 *indir_out
= offset
;
2049 static LLVMValueRef
visit_load_var(struct nir_to_llvm_context
*ctx
,
2050 nir_intrinsic_instr
*instr
)
2052 LLVMValueRef values
[4];
2053 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2054 int ve
= instr
->dest
.ssa
.num_components
;
2055 LLVMValueRef indir_index
;
2056 unsigned const_index
;
2057 switch (instr
->variables
[0]->var
->data
.mode
) {
2058 case nir_var_shader_in
:
2059 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
,
2060 ctx
->stage
== MESA_SHADER_VERTEX
,
2061 &const_index
, &indir_index
);
2062 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2064 unsigned count
= glsl_count_attribute_slots(
2065 instr
->variables
[0]->var
->type
,
2066 ctx
->stage
== MESA_SHADER_VERTEX
);
2067 LLVMValueRef tmp_vec
= build_gather_values_extended(
2068 ctx
, ctx
->inputs
+ idx
+ chan
, count
,
2071 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2075 values
[chan
] = ctx
->inputs
[idx
+ chan
+ const_index
* 4];
2077 return to_integer(ctx
, build_gather_values(ctx
, values
, ve
));
2080 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2081 &const_index
, &indir_index
);
2082 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2084 unsigned count
= glsl_count_attribute_slots(
2085 instr
->variables
[0]->var
->type
, false);
2086 LLVMValueRef tmp_vec
= build_gather_values_extended(
2087 ctx
, ctx
->locals
+ idx
+ chan
, count
,
2090 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2094 values
[chan
] = LLVMBuildLoad(ctx
->builder
, ctx
->locals
[idx
+ chan
+ const_index
* 4], "");
2097 return to_integer(ctx
, build_gather_values(ctx
, values
, ve
));
2098 case nir_var_shader_out
:
2099 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2100 &const_index
, &indir_index
);
2101 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2103 unsigned count
= glsl_count_attribute_slots(
2104 instr
->variables
[0]->var
->type
, false);
2105 LLVMValueRef tmp_vec
= build_gather_values_extended(
2106 ctx
, ctx
->outputs
+ idx
+ chan
, count
,
2109 values
[chan
] = LLVMBuildExtractElement(ctx
->builder
,
2113 values
[chan
] = LLVMBuildLoad(ctx
->builder
,
2114 ctx
->outputs
[idx
+ chan
+ const_index
* 4],
2118 return to_integer(ctx
, build_gather_values(ctx
, values
, ve
));
2119 case nir_var_shared
: {
2120 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2121 &const_index
, &indir_index
);
2122 LLVMValueRef ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
2123 LLVMValueRef derived_ptr
;
2124 LLVMValueRef index
= ctx
->i32zero
;
2126 index
= LLVMBuildAdd(ctx
->builder
, index
, indir_index
, "");
2127 derived_ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, "");
2129 return to_integer(ctx
, LLVMBuildLoad(ctx
->builder
, derived_ptr
, ""));
2139 visit_store_var(struct nir_to_llvm_context
*ctx
,
2140 nir_intrinsic_instr
*instr
)
2142 LLVMValueRef temp_ptr
, value
;
2143 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2144 LLVMValueRef src
= to_float(ctx
, get_src(ctx
, instr
->src
[0]));
2145 int writemask
= instr
->const_index
[0];
2146 LLVMValueRef indir_index
;
2147 unsigned const_index
;
2148 switch (instr
->variables
[0]->var
->data
.mode
) {
2149 case nir_var_shader_out
:
2150 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2151 &const_index
, &indir_index
);
2152 for (unsigned chan
= 0; chan
< 4; chan
++) {
2154 if (!(writemask
& (1 << chan
)))
2156 if (get_llvm_num_components(src
) == 1)
2159 value
= LLVMBuildExtractElement(ctx
->builder
, src
,
2160 LLVMConstInt(ctx
->i32
,
2164 if (instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CLIP_DIST0
||
2165 instr
->variables
[0]->var
->data
.location
== VARYING_SLOT_CULL_DIST0
)
2168 unsigned count
= glsl_count_attribute_slots(
2169 instr
->variables
[0]->var
->type
, false);
2170 LLVMValueRef tmp_vec
= build_gather_values_extended(
2171 ctx
, ctx
->outputs
+ idx
+ chan
, count
,
2174 if (get_llvm_num_components(tmp_vec
) > 1) {
2175 tmp_vec
= LLVMBuildInsertElement(ctx
->builder
, tmp_vec
,
2176 value
, indir_index
, "");
2179 build_store_values_extended(ctx
, ctx
->outputs
+ idx
+ chan
,
2180 count
, stride
, tmp_vec
);
2183 temp_ptr
= ctx
->outputs
[idx
+ chan
+ const_index
* stride
];
2185 LLVMBuildStore(ctx
->builder
, value
, temp_ptr
);
2190 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2191 &const_index
, &indir_index
);
2192 for (unsigned chan
= 0; chan
< 4; chan
++) {
2193 if (!(writemask
& (1 << chan
)))
2196 if (get_llvm_num_components(src
) == 1)
2199 value
= LLVMBuildExtractElement(ctx
->builder
, src
,
2200 LLVMConstInt(ctx
->i32
, chan
, false), "");
2202 unsigned count
= glsl_count_attribute_slots(
2203 instr
->variables
[0]->var
->type
, false);
2204 LLVMValueRef tmp_vec
= build_gather_values_extended(
2205 ctx
, ctx
->locals
+ idx
+ chan
, count
,
2208 tmp_vec
= LLVMBuildInsertElement(ctx
->builder
, tmp_vec
,
2209 value
, indir_index
, "");
2210 build_store_values_extended(ctx
, ctx
->locals
+ idx
+ chan
,
2213 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
2215 LLVMBuildStore(ctx
->builder
, value
, temp_ptr
);
2219 case nir_var_shared
: {
2221 radv_get_deref_offset(ctx
, &instr
->variables
[0]->deref
, false,
2222 &const_index
, &indir_index
);
2224 ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
2225 LLVMValueRef index
= ctx
->i32zero
;
2226 LLVMValueRef derived_ptr
;
2229 index
= LLVMBuildAdd(ctx
->builder
, index
, indir_index
, "");
2230 derived_ptr
= LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, "");
2231 LLVMBuildStore(ctx
->builder
,
2232 to_integer(ctx
, src
), derived_ptr
);
2240 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
2243 case GLSL_SAMPLER_DIM_BUF
:
2245 case GLSL_SAMPLER_DIM_1D
:
2246 return array
? 2 : 1;
2247 case GLSL_SAMPLER_DIM_2D
:
2248 return array
? 3 : 2;
2249 case GLSL_SAMPLER_DIM_3D
:
2250 case GLSL_SAMPLER_DIM_CUBE
:
2252 case GLSL_SAMPLER_DIM_RECT
:
2253 case GLSL_SAMPLER_DIM_SUBPASS
:
2261 static LLVMValueRef
get_image_coords(struct nir_to_llvm_context
*ctx
,
2262 nir_intrinsic_instr
*instr
, bool add_frag_pos
)
2264 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
2265 if(instr
->variables
[0]->deref
.child
)
2266 type
= instr
->variables
[0]->deref
.child
->type
;
2268 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
2269 LLVMValueRef coords
[4];
2270 LLVMValueRef masks
[] = {
2271 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
2272 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false),
2276 count
= image_type_to_components_count(glsl_get_sampler_dim(type
),
2277 glsl_sampler_type_is_array(type
));
2280 if (instr
->src
[0].ssa
->num_components
)
2281 res
= LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[0], "");
2286 for (chan
= 0; chan
< count
; ++chan
) {
2287 coords
[chan
] = LLVMBuildExtractElement(ctx
->builder
, src0
, masks
[chan
], "");
2291 for (chan
= 0; chan
< count
; ++chan
)
2292 coords
[chan
] = LLVMBuildAdd(ctx
->builder
, coords
[chan
], LLVMBuildFPToUI(ctx
->builder
, ctx
->frag_pos
[chan
], ctx
->i32
, ""), "");
2295 coords
[3] = LLVMGetUndef(ctx
->i32
);
2298 res
= build_gather_values(ctx
, coords
, count
);
2303 static void build_type_name_for_intr(
2305 char *buf
, unsigned bufsize
)
2307 LLVMTypeRef elem_type
= type
;
2309 assert(bufsize
>= 8);
2311 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
2312 int ret
= snprintf(buf
, bufsize
, "v%u",
2313 LLVMGetVectorSize(type
));
2315 char *type_name
= LLVMPrintTypeToString(type
);
2316 fprintf(stderr
, "Error building type name for: %s\n",
2320 elem_type
= LLVMGetElementType(type
);
2324 switch (LLVMGetTypeKind(elem_type
)) {
2326 case LLVMIntegerTypeKind
:
2327 snprintf(buf
, bufsize
, "i%d", LLVMGetIntTypeWidth(elem_type
));
2329 case LLVMFloatTypeKind
:
2330 snprintf(buf
, bufsize
, "f32");
2332 case LLVMDoubleTypeKind
:
2333 snprintf(buf
, bufsize
, "f64");
2338 static void get_image_intr_name(const char *base_name
,
2339 LLVMTypeRef data_type
,
2340 LLVMTypeRef coords_type
,
2341 LLVMTypeRef rsrc_type
,
2342 char *out_name
, unsigned out_len
)
2344 char coords_type_name
[8];
2346 build_type_name_for_intr(coords_type
, coords_type_name
,
2347 sizeof(coords_type_name
));
2349 if (HAVE_LLVM
<= 0x0309) {
2350 snprintf(out_name
, out_len
, "%s.%s", base_name
, coords_type_name
);
2352 char data_type_name
[8];
2353 char rsrc_type_name
[8];
2355 build_type_name_for_intr(data_type
, data_type_name
,
2356 sizeof(data_type_name
));
2357 build_type_name_for_intr(rsrc_type
, rsrc_type_name
,
2358 sizeof(rsrc_type_name
));
2359 snprintf(out_name
, out_len
, "%s.%s.%s.%s", base_name
,
2360 data_type_name
, coords_type_name
, rsrc_type_name
);
2364 static LLVMValueRef
visit_image_load(struct nir_to_llvm_context
*ctx
,
2365 nir_intrinsic_instr
*instr
)
2367 LLVMValueRef params
[7];
2369 char intrinsic_name
[64];
2370 const nir_variable
*var
= instr
->variables
[0]->var
;
2371 const struct glsl_type
*type
= var
->type
;
2372 if(instr
->variables
[0]->deref
.child
)
2373 type
= instr
->variables
[0]->deref
.child
->type
;
2375 type
= glsl_without_array(type
);
2376 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2377 params
[0] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
2378 params
[1] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
2379 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
2380 params
[2] = LLVMConstInt(ctx
->i32
, 0, false); /* voffset */
2381 params
[3] = LLVMConstInt(ctx
->i1
, 0, false); /* glc */
2382 params
[4] = LLVMConstInt(ctx
->i1
, 0, false); /* slc */
2383 res
= emit_llvm_intrinsic(ctx
, "llvm.amdgcn.buffer.load.format.v4f32", ctx
->v4f32
,
2386 res
= trim_vector(ctx
, res
, instr
->dest
.ssa
.num_components
);
2387 res
= to_integer(ctx
, res
);
2389 bool is_da
= glsl_sampler_type_is_array(type
) ||
2390 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
2391 bool add_frag_pos
= glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_SUBPASS
;
2392 LLVMValueRef da
= is_da
? ctx
->i32one
: ctx
->i32zero
;
2393 LLVMValueRef glc
= LLVMConstInt(ctx
->i1
, 0, false);
2394 LLVMValueRef slc
= LLVMConstInt(ctx
->i1
, 0, false);
2396 params
[0] = get_image_coords(ctx
, instr
, add_frag_pos
);
2397 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
2398 params
[2] = LLVMConstInt(ctx
->i32
, 15, false); /* dmask */
2399 if (HAVE_LLVM
<= 0x0309) {
2400 params
[3] = LLVMConstInt(ctx
->i1
, 0, false); /* r128 */
2405 LLVMValueRef lwe
= LLVMConstInt(ctx
->i1
, 0, false);
2412 get_image_intr_name("llvm.amdgcn.image.load",
2413 ctx
->v4f32
, /* vdata */
2414 LLVMTypeOf(params
[0]), /* coords */
2415 LLVMTypeOf(params
[1]), /* rsrc */
2416 intrinsic_name
, sizeof(intrinsic_name
));
2418 res
= emit_llvm_intrinsic(ctx
, intrinsic_name
, ctx
->v4f32
,
2419 params
, 7, LLVMReadOnlyAttribute
);
2421 return to_integer(ctx
, res
);
2424 static void visit_image_store(struct nir_to_llvm_context
*ctx
,
2425 nir_intrinsic_instr
*instr
)
2427 LLVMValueRef params
[8];
2428 char intrinsic_name
[64];
2429 const nir_variable
*var
= instr
->variables
[0]->var
;
2430 LLVMValueRef i1false
= LLVMConstInt(ctx
->i1
, 0, 0);
2431 LLVMValueRef i1true
= LLVMConstInt(ctx
->i1
, 1, 0);
2432 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2434 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
2435 ctx
->shader_info
->fs
.writes_memory
= true;
2437 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2438 params
[0] = to_float(ctx
, get_src(ctx
, instr
->src
[2])); /* data */
2439 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
2440 params
[2] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
2441 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
2442 params
[3] = LLVMConstInt(ctx
->i32
, 0, false); /* voffset */
2443 params
[4] = i1false
; /* glc */
2444 params
[5] = i1false
; /* slc */
2445 emit_llvm_intrinsic(ctx
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->voidt
,
2448 bool is_da
= glsl_sampler_type_is_array(type
) ||
2449 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
2450 LLVMValueRef da
= is_da
? i1true
: i1false
;
2451 LLVMValueRef glc
= i1false
;
2452 LLVMValueRef slc
= i1false
;
2454 params
[0] = to_float(ctx
, get_src(ctx
, instr
->src
[2]));
2455 params
[1] = get_image_coords(ctx
, instr
, false); /* coords */
2456 params
[2] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
2457 params
[3] = LLVMConstInt(ctx
->i32
, 15, false); /* dmask */
2458 if (HAVE_LLVM
<= 0x0309) {
2459 params
[4] = i1false
; /* r128 */
2464 LLVMValueRef lwe
= i1false
;
2471 get_image_intr_name("llvm.amdgcn.image.store",
2472 LLVMTypeOf(params
[0]), /* vdata */
2473 LLVMTypeOf(params
[1]), /* coords */
2474 LLVMTypeOf(params
[2]), /* rsrc */
2475 intrinsic_name
, sizeof(intrinsic_name
));
2477 emit_llvm_intrinsic(ctx
, intrinsic_name
, ctx
->voidt
,
2483 static LLVMValueRef
visit_image_atomic(struct nir_to_llvm_context
*ctx
,
2484 nir_intrinsic_instr
*instr
)
2486 LLVMValueRef params
[6];
2487 int param_count
= 0;
2488 const nir_variable
*var
= instr
->variables
[0]->var
;
2489 LLVMValueRef i1false
= LLVMConstInt(ctx
->i1
, 0, 0);
2490 LLVMValueRef i1true
= LLVMConstInt(ctx
->i1
, 1, 0);
2491 const char *base_name
= "llvm.amdgcn.image.atomic";
2492 const char *atomic_name
;
2493 LLVMValueRef coords
;
2494 char intrinsic_name
[32], coords_type
[8];
2495 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2497 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
2498 ctx
->shader_info
->fs
.writes_memory
= true;
2500 params
[param_count
++] = get_src(ctx
, instr
->src
[2]);
2501 if (instr
->intrinsic
== nir_intrinsic_image_atomic_comp_swap
)
2502 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
2504 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2505 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
);
2506 coords
= params
[param_count
++] = LLVMBuildExtractElement(ctx
->builder
, get_src(ctx
, instr
->src
[0]),
2507 LLVMConstInt(ctx
->i32
, 0, false), ""); /* vindex */
2508 params
[param_count
++] = ctx
->i32zero
; /* voffset */
2509 params
[param_count
++] = i1false
; /* glc */
2510 params
[param_count
++] = i1false
; /* slc */
2512 bool da
= glsl_sampler_type_is_array(type
) ||
2513 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
;
2515 coords
= params
[param_count
++] = get_image_coords(ctx
, instr
, false);
2516 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
2517 params
[param_count
++] = i1false
; /* r128 */
2518 params
[param_count
++] = da
? i1true
: i1false
; /* da */
2519 params
[param_count
++] = i1false
; /* slc */
2522 switch (instr
->intrinsic
) {
2523 case nir_intrinsic_image_atomic_add
:
2524 atomic_name
= "add";
2526 case nir_intrinsic_image_atomic_min
:
2527 atomic_name
= "smin";
2529 case nir_intrinsic_image_atomic_max
:
2530 atomic_name
= "smax";
2532 case nir_intrinsic_image_atomic_and
:
2533 atomic_name
= "and";
2535 case nir_intrinsic_image_atomic_or
:
2538 case nir_intrinsic_image_atomic_xor
:
2539 atomic_name
= "xor";
2541 case nir_intrinsic_image_atomic_exchange
:
2542 atomic_name
= "swap";
2544 case nir_intrinsic_image_atomic_comp_swap
:
2545 atomic_name
= "cmpswap";
2550 build_int_type_name(LLVMTypeOf(coords
),
2551 coords_type
, sizeof(coords_type
));
2553 snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2554 "%s.%s.%s", base_name
, atomic_name
, coords_type
);
2555 return emit_llvm_intrinsic(ctx
, intrinsic_name
, ctx
->i32
, params
, param_count
, 0);
2558 static LLVMValueRef
visit_image_size(struct nir_to_llvm_context
*ctx
,
2559 nir_intrinsic_instr
*instr
)
2562 LLVMValueRef params
[10];
2563 const nir_variable
*var
= instr
->variables
[0]->var
;
2564 const struct glsl_type
*type
= instr
->variables
[0]->var
->type
;
2565 bool da
= glsl_sampler_type_is_array(var
->type
) ||
2566 glsl_get_sampler_dim(var
->type
) == GLSL_SAMPLER_DIM_CUBE
;
2567 if(instr
->variables
[0]->deref
.child
)
2568 type
= instr
->variables
[0]->deref
.child
->type
;
2570 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
2571 return get_buffer_size(ctx
, get_sampler_desc(ctx
, instr
->variables
[0], DESC_BUFFER
), true);
2572 params
[0] = ctx
->i32zero
;
2573 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], DESC_IMAGE
);
2574 params
[2] = LLVMConstInt(ctx
->i32
, 15, false);
2575 params
[3] = ctx
->i32zero
;
2576 params
[4] = ctx
->i32zero
;
2577 params
[5] = da
? ctx
->i32one
: ctx
->i32zero
;
2578 params
[6] = ctx
->i32zero
;
2579 params
[7] = ctx
->i32zero
;
2580 params
[8] = ctx
->i32zero
;
2581 params
[9] = ctx
->i32zero
;
2583 res
= emit_llvm_intrinsic(ctx
, "llvm.SI.getresinfo.i32", ctx
->v4i32
,
2584 params
, 10, LLVMReadNoneAttribute
);
2586 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
2587 glsl_sampler_type_is_array(type
)) {
2588 LLVMValueRef two
= LLVMConstInt(ctx
->i32
, 2, false);
2589 LLVMValueRef six
= LLVMConstInt(ctx
->i32
, 6, false);
2590 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->builder
, res
, two
, "");
2591 z
= LLVMBuildSDiv(ctx
->builder
, z
, six
, "");
2592 res
= LLVMBuildInsertElement(ctx
->builder
, res
, z
, two
, "");
2597 static void emit_waitcnt(struct nir_to_llvm_context
*ctx
)
2599 LLVMValueRef args
[1] = {
2600 LLVMConstInt(ctx
->i32
, 0xf70, false),
2602 emit_llvm_intrinsic(ctx
, "llvm.amdgcn.s.waitcnt",
2603 ctx
->voidt
, args
, 1, 0);
2606 static void emit_barrier(struct nir_to_llvm_context
*ctx
)
2609 emit_llvm_intrinsic(ctx
, "llvm.amdgcn.s.barrier",
2610 ctx
->voidt
, NULL
, 0, 0);
2614 visit_load_local_invocation_index(struct nir_to_llvm_context
*ctx
)
2616 LLVMValueRef result
;
2617 LLVMValueRef thread_id
= get_thread_id(ctx
);
2618 result
= LLVMBuildAnd(ctx
->builder
, ctx
->tg_size
,
2619 LLVMConstInt(ctx
->i32
, 0xfc0, false), "");
2621 return LLVMBuildAdd(ctx
->builder
, result
, thread_id
, "");
2624 static LLVMValueRef
visit_var_atomic(struct nir_to_llvm_context
*ctx
,
2625 nir_intrinsic_instr
*instr
)
2627 LLVMValueRef ptr
, result
;
2628 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
2629 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
2630 ptr
= get_shared_memory_ptr(ctx
, idx
, ctx
->i32
);
2632 if (instr
->intrinsic
== nir_intrinsic_var_atomic_comp_swap
) {
2633 LLVMValueRef src1
= get_src(ctx
, instr
->src
[1]);
2634 result
= LLVMBuildAtomicCmpXchg(ctx
->builder
,
2636 LLVMAtomicOrderingSequentiallyConsistent
,
2637 LLVMAtomicOrderingSequentiallyConsistent
,
2640 LLVMAtomicRMWBinOp op
;
2641 switch (instr
->intrinsic
) {
2642 case nir_intrinsic_var_atomic_add
:
2643 op
= LLVMAtomicRMWBinOpAdd
;
2645 case nir_intrinsic_var_atomic_umin
:
2646 op
= LLVMAtomicRMWBinOpUMin
;
2648 case nir_intrinsic_var_atomic_umax
:
2649 op
= LLVMAtomicRMWBinOpUMax
;
2651 case nir_intrinsic_var_atomic_imin
:
2652 op
= LLVMAtomicRMWBinOpMin
;
2654 case nir_intrinsic_var_atomic_imax
:
2655 op
= LLVMAtomicRMWBinOpMax
;
2657 case nir_intrinsic_var_atomic_and
:
2658 op
= LLVMAtomicRMWBinOpAnd
;
2660 case nir_intrinsic_var_atomic_or
:
2661 op
= LLVMAtomicRMWBinOpOr
;
2663 case nir_intrinsic_var_atomic_xor
:
2664 op
= LLVMAtomicRMWBinOpXor
;
2666 case nir_intrinsic_var_atomic_exchange
:
2667 op
= LLVMAtomicRMWBinOpXchg
;
2673 result
= LLVMBuildAtomicRMW(ctx
->builder
, op
, ptr
, to_integer(ctx
, src
),
2674 LLVMAtomicOrderingSequentiallyConsistent
,
2680 #define INTERP_CENTER 0
2681 #define INTERP_CENTROID 1
2682 #define INTERP_SAMPLE 2
2684 static LLVMValueRef
lookup_interp_param(struct nir_to_llvm_context
*ctx
,
2685 enum glsl_interp_mode interp
, unsigned location
)
2688 case INTERP_MODE_FLAT
:
2691 case INTERP_MODE_SMOOTH
:
2692 case INTERP_MODE_NONE
:
2693 if (location
== INTERP_CENTER
)
2694 return ctx
->persp_center
;
2695 else if (location
== INTERP_CENTROID
)
2696 return ctx
->persp_centroid
;
2697 else if (location
== INTERP_SAMPLE
)
2698 return ctx
->persp_sample
;
2700 case INTERP_MODE_NOPERSPECTIVE
:
2701 if (location
== INTERP_CENTER
)
2702 return ctx
->linear_center
;
2703 else if (location
== INTERP_CENTROID
)
2704 return ctx
->linear_centroid
;
2705 else if (location
== INTERP_SAMPLE
)
2706 return ctx
->linear_sample
;
2712 static LLVMValueRef
load_sample_position(struct nir_to_llvm_context
*ctx
,
2713 LLVMValueRef sample_id
)
2715 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
2716 LLVMValueRef offset0
= LLVMBuildMul(ctx
->builder
, sample_id
, LLVMConstInt(ctx
->i32
, 8, false), "");
2717 LLVMValueRef offset1
= LLVMBuildAdd(ctx
->builder
, offset0
, LLVMConstInt(ctx
->i32
, 4, false), "");
2718 LLVMValueRef result
[2];
2720 result
[0] = build_indexed_load_const(ctx
, ctx
->sample_positions
, offset0
);
2721 result
[1] = build_indexed_load_const(ctx
, ctx
->sample_positions
, offset1
);
2723 return build_gather_values(ctx
, result
, 2);
2726 static LLVMValueRef
visit_interp(struct nir_to_llvm_context
*ctx
,
2727 nir_intrinsic_instr
*instr
)
2729 LLVMValueRef result
[2];
2730 LLVMValueRef interp_param
, attr_number
;
2733 LLVMValueRef src_c0
, src_c1
;
2734 const char *intr_name
;
2736 int input_index
= instr
->variables
[0]->var
->data
.location
- VARYING_SLOT_VAR0
;
2737 switch (instr
->intrinsic
) {
2738 case nir_intrinsic_interp_var_at_centroid
:
2739 location
= INTERP_CENTROID
;
2741 case nir_intrinsic_interp_var_at_sample
:
2742 case nir_intrinsic_interp_var_at_offset
:
2743 location
= INTERP_SAMPLE
;
2744 src0
= get_src(ctx
, instr
->src
[0]);
2750 if (instr
->intrinsic
== nir_intrinsic_interp_var_at_offset
) {
2751 src_c0
= to_float(ctx
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32zero
, ""));
2752 src_c1
= to_float(ctx
, LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32one
, ""));
2753 } else if (instr
->intrinsic
== nir_intrinsic_interp_var_at_sample
) {
2754 LLVMValueRef sample_position
;
2755 LLVMValueRef halfval
= LLVMConstReal(ctx
->f32
, 0.5f
);
2757 /* fetch sample ID */
2758 sample_position
= load_sample_position(ctx
, src0
);
2760 src_c0
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->i32zero
, "");
2761 src_c0
= LLVMBuildFSub(ctx
->builder
, src_c0
, halfval
, "");
2762 src_c1
= LLVMBuildExtractElement(ctx
->builder
, sample_position
, ctx
->i32one
, "");
2763 src_c1
= LLVMBuildFSub(ctx
->builder
, src_c1
, halfval
, "");
2765 interp_param
= lookup_interp_param(ctx
, instr
->variables
[0]->var
->data
.interpolation
, location
);
2766 attr_number
= LLVMConstInt(ctx
->i32
, input_index
, false);
2768 if (location
== INTERP_SAMPLE
) {
2769 LLVMValueRef ij_out
[2];
2770 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
, interp_param
);
2773 * take the I then J parameters, and the DDX/Y for it, and
2774 * calculate the IJ inputs for the interpolator.
2775 * temp1 = ddx * offset/sample.x + I;
2776 * interp_param.I = ddy * offset/sample.y + temp1;
2777 * temp1 = ddx * offset/sample.x + J;
2778 * interp_param.J = ddy * offset/sample.y + temp1;
2780 for (unsigned i
= 0; i
< 2; i
++) {
2781 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->i32
, i
, false);
2782 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->i32
, i
+ 2, false);
2783 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->builder
,
2784 ddxy_out
, ix_ll
, "");
2785 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->builder
,
2786 ddxy_out
, iy_ll
, "");
2787 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->builder
,
2788 interp_param
, ix_ll
, "");
2789 LLVMValueRef temp1
, temp2
;
2791 interp_el
= LLVMBuildBitCast(ctx
->builder
, interp_el
,
2794 temp1
= LLVMBuildFMul(ctx
->builder
, ddx_el
, src_c0
, "");
2795 temp1
= LLVMBuildFAdd(ctx
->builder
, temp1
, interp_el
, "");
2797 temp2
= LLVMBuildFMul(ctx
->builder
, ddy_el
, src_c1
, "");
2798 temp2
= LLVMBuildFAdd(ctx
->builder
, temp2
, temp1
, "");
2800 ij_out
[i
] = LLVMBuildBitCast(ctx
->builder
,
2801 temp2
, ctx
->i32
, "");
2803 interp_param
= build_gather_values(ctx
, ij_out
, 2);
2806 intr_name
= interp_param
? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
2807 for (chan
= 0; chan
< 2; chan
++) {
2808 LLVMValueRef args
[4];
2809 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
2811 args
[0] = llvm_chan
;
2812 args
[1] = attr_number
;
2813 args
[2] = ctx
->prim_mask
;
2814 args
[3] = interp_param
;
2815 result
[chan
] = emit_llvm_intrinsic(ctx
, intr_name
,
2816 ctx
->f32
, args
, args
[3] ? 4 : 3,
2817 LLVMReadNoneAttribute
);
2819 return build_gather_values(ctx
, result
, 2);
2822 static void visit_intrinsic(struct nir_to_llvm_context
*ctx
,
2823 nir_intrinsic_instr
*instr
)
2825 LLVMValueRef result
= NULL
;
2827 switch (instr
->intrinsic
) {
2828 case nir_intrinsic_load_work_group_id
: {
2829 result
= ctx
->workgroup_ids
;
2832 case nir_intrinsic_load_base_vertex
: {
2833 result
= ctx
->base_vertex
;
2836 case nir_intrinsic_load_vertex_id_zero_base
: {
2837 result
= ctx
->vertex_id
;
2840 case nir_intrinsic_load_local_invocation_id
: {
2841 result
= ctx
->local_invocation_ids
;
2844 case nir_intrinsic_load_base_instance
:
2845 result
= ctx
->start_instance
;
2847 case nir_intrinsic_load_sample_id
:
2848 result
= ctx
->ancillary
;
2850 case nir_intrinsic_load_front_face
:
2851 result
= ctx
->front_face
;
2853 case nir_intrinsic_load_instance_id
:
2854 result
= ctx
->instance_id
;
2855 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
2856 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
2858 case nir_intrinsic_load_num_work_groups
:
2859 result
= ctx
->num_work_groups
;
2861 case nir_intrinsic_load_local_invocation_index
:
2862 result
= visit_load_local_invocation_index(ctx
);
2864 case nir_intrinsic_load_push_constant
:
2865 result
= visit_load_push_constant(ctx
, instr
);
2867 case nir_intrinsic_vulkan_resource_index
:
2868 result
= visit_vulkan_resource_index(ctx
, instr
);
2870 case nir_intrinsic_store_ssbo
:
2871 visit_store_ssbo(ctx
, instr
);
2873 case nir_intrinsic_load_ssbo
:
2874 result
= visit_load_buffer(ctx
, instr
);
2876 case nir_intrinsic_ssbo_atomic_add
:
2877 case nir_intrinsic_ssbo_atomic_imin
:
2878 case nir_intrinsic_ssbo_atomic_umin
:
2879 case nir_intrinsic_ssbo_atomic_imax
:
2880 case nir_intrinsic_ssbo_atomic_umax
:
2881 case nir_intrinsic_ssbo_atomic_and
:
2882 case nir_intrinsic_ssbo_atomic_or
:
2883 case nir_intrinsic_ssbo_atomic_xor
:
2884 case nir_intrinsic_ssbo_atomic_exchange
:
2885 case nir_intrinsic_ssbo_atomic_comp_swap
:
2886 result
= visit_atomic_ssbo(ctx
, instr
);
2888 case nir_intrinsic_load_ubo
:
2889 result
= visit_load_buffer(ctx
, instr
);
2891 case nir_intrinsic_get_buffer_size
:
2892 result
= visit_get_buffer_size(ctx
, instr
);
2894 case nir_intrinsic_load_var
:
2895 result
= visit_load_var(ctx
, instr
);
2897 case nir_intrinsic_store_var
:
2898 visit_store_var(ctx
, instr
);
2900 case nir_intrinsic_image_load
:
2901 result
= visit_image_load(ctx
, instr
);
2903 case nir_intrinsic_image_store
:
2904 visit_image_store(ctx
, instr
);
2906 case nir_intrinsic_image_atomic_add
:
2907 case nir_intrinsic_image_atomic_min
:
2908 case nir_intrinsic_image_atomic_max
:
2909 case nir_intrinsic_image_atomic_and
:
2910 case nir_intrinsic_image_atomic_or
:
2911 case nir_intrinsic_image_atomic_xor
:
2912 case nir_intrinsic_image_atomic_exchange
:
2913 case nir_intrinsic_image_atomic_comp_swap
:
2914 result
= visit_image_atomic(ctx
, instr
);
2916 case nir_intrinsic_image_size
:
2917 result
= visit_image_size(ctx
, instr
);
2919 case nir_intrinsic_discard
:
2920 ctx
->shader_info
->fs
.can_discard
= true;
2921 emit_llvm_intrinsic(ctx
, "llvm.AMDGPU.kilp",
2922 LLVMVoidTypeInContext(ctx
->context
),
2925 case nir_intrinsic_memory_barrier
:
2928 case nir_intrinsic_barrier
:
2931 case nir_intrinsic_var_atomic_add
:
2932 case nir_intrinsic_var_atomic_imin
:
2933 case nir_intrinsic_var_atomic_umin
:
2934 case nir_intrinsic_var_atomic_imax
:
2935 case nir_intrinsic_var_atomic_umax
:
2936 case nir_intrinsic_var_atomic_and
:
2937 case nir_intrinsic_var_atomic_or
:
2938 case nir_intrinsic_var_atomic_xor
:
2939 case nir_intrinsic_var_atomic_exchange
:
2940 case nir_intrinsic_var_atomic_comp_swap
:
2941 result
= visit_var_atomic(ctx
, instr
);
2943 case nir_intrinsic_interp_var_at_centroid
:
2944 case nir_intrinsic_interp_var_at_sample
:
2945 case nir_intrinsic_interp_var_at_offset
:
2946 result
= visit_interp(ctx
, instr
);
2949 fprintf(stderr
, "Unknown intrinsic: ");
2950 nir_print_instr(&instr
->instr
, stderr
);
2951 fprintf(stderr
, "\n");
2955 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
2959 static LLVMValueRef
get_sampler_desc(struct nir_to_llvm_context
*ctx
,
2960 nir_deref_var
*deref
,
2961 enum desc_type desc_type
)
2963 unsigned desc_set
= deref
->var
->data
.descriptor_set
;
2964 LLVMValueRef list
= ctx
->descriptor_sets
[desc_set
];
2965 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[desc_set
].layout
;
2966 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ deref
->var
->data
.binding
;
2967 unsigned offset
= binding
->offset
;
2968 unsigned stride
= binding
->size
;
2970 LLVMBuilderRef builder
= ctx
->builder
;
2972 LLVMValueRef indices
[2];
2973 LLVMValueRef index
= NULL
;
2975 assert(deref
->var
->data
.binding
< layout
->binding_count
);
2977 switch (desc_type
) {
2989 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
3000 if (deref
->deref
.child
) {
3001 nir_deref_array
*child
= (nir_deref_array
*)deref
->deref
.child
;
3003 assert(child
->deref_array_type
!= nir_deref_array_type_wildcard
);
3004 offset
+= child
->base_offset
* stride
;
3005 if (child
->deref_array_type
== nir_deref_array_type_indirect
) {
3006 index
= get_src(ctx
, child
->indirect
);
3010 assert(stride
% type_size
== 0);
3013 index
= ctx
->i32zero
;
3015 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, stride
/ type_size
, 0), "");
3016 indices
[0] = ctx
->i32zero
;
3017 indices
[1] = LLVMConstInt(ctx
->i32
, offset
, 0);
3018 list
= LLVMBuildGEP(builder
, list
, indices
, 2, "");
3019 list
= LLVMBuildPointerCast(builder
, list
, const_array(type
, 0), "");
3021 return build_indexed_load_const(ctx
, list
, index
);
3024 static void set_tex_fetch_args(struct nir_to_llvm_context
*ctx
,
3025 struct ac_tex_info
*tinfo
,
3026 nir_tex_instr
*instr
,
3028 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
3029 LLVMValueRef
*param
, unsigned count
,
3033 unsigned is_rect
= 0;
3034 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
3036 if (op
== nir_texop_lod
)
3038 /* Pad to power of two vector */
3039 while (count
< util_next_power_of_two(count
))
3040 param
[count
++] = LLVMGetUndef(ctx
->i32
);
3043 tinfo
->args
[0] = build_gather_values(ctx
, param
, count
);
3045 tinfo
->args
[0] = param
[0];
3047 tinfo
->args
[1] = res_ptr
;
3050 if (op
== nir_texop_txf
||
3051 op
== nir_texop_txf_ms
||
3052 op
== nir_texop_query_levels
||
3053 op
== nir_texop_texture_samples
||
3054 op
== nir_texop_txs
)
3055 tinfo
->dst_type
= ctx
->v4i32
;
3057 tinfo
->dst_type
= ctx
->v4f32
;
3058 tinfo
->args
[num_args
++] = samp_ptr
;
3061 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
&& op
== nir_texop_txf
) {
3062 tinfo
->args
[0] = res_ptr
;
3063 tinfo
->args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
3064 tinfo
->args
[2] = param
[0];
3065 tinfo
->arg_count
= 3;
3069 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, dmask
, 0);
3070 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, is_rect
, 0); /* unorm */
3071 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* r128 */
3072 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, da
? 1 : 0, 0);
3073 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* glc */
3074 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* slc */
3075 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* tfe */
3076 tinfo
->args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* lwe */
3078 tinfo
->arg_count
= num_args
;
3081 static void tex_fetch_ptrs(struct nir_to_llvm_context
*ctx
,
3082 nir_tex_instr
*instr
,
3083 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
3084 LLVMValueRef
*fmask_ptr
)
3086 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
3087 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_BUFFER
);
3089 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_IMAGE
);
3092 *samp_ptr
= get_sampler_desc(ctx
, instr
->sampler
, DESC_SAMPLER
);
3094 *samp_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_SAMPLER
);
3096 if (fmask_ptr
&& !instr
->sampler
&& (instr
->op
== nir_texop_txf_ms
||
3097 instr
->op
== nir_texop_samples_identical
))
3098 *fmask_ptr
= get_sampler_desc(ctx
, instr
->texture
, DESC_FMASK
);
3101 static LLVMValueRef
build_cube_intrinsic(struct nir_to_llvm_context
*ctx
,
3105 LLVMValueRef v
, cube_vec
;
3108 LLVMTypeRef f32
= LLVMTypeOf(in
[0]);
3109 LLVMValueRef out
[4];
3111 out
[0] = emit_llvm_intrinsic(ctx
, "llvm.amdgcn.cubetc",
3112 f32
, in
, 3, LLVMReadNoneAttribute
);
3113 out
[1] = emit_llvm_intrinsic(ctx
, "llvm.amdgcn.cubesc",
3114 f32
, in
, 3, LLVMReadNoneAttribute
);
3115 out
[2] = emit_llvm_intrinsic(ctx
, "llvm.amdgcn.cubema",
3116 f32
, in
, 3, LLVMReadNoneAttribute
);
3117 out
[3] = emit_llvm_intrinsic(ctx
, "llvm.amdgcn.cubeid",
3118 f32
, in
, 3, LLVMReadNoneAttribute
);
3120 return build_gather_values(ctx
, out
, 4);
3126 c
[3] = LLVMGetUndef(LLVMTypeOf(in
[0]));
3127 cube_vec
= build_gather_values(ctx
, c
, 4);
3128 v
= emit_llvm_intrinsic(ctx
, "llvm.AMDGPU.cube", LLVMTypeOf(cube_vec
),
3129 &cube_vec
, 1, LLVMReadNoneAttribute
);
3134 static void cube_to_2d_coords(struct nir_to_llvm_context
*ctx
,
3135 LLVMValueRef
*in
, LLVMValueRef
*out
)
3137 LLVMValueRef coords
[4];
3138 LLVMValueRef mad_args
[3];
3143 v
= build_cube_intrinsic(ctx
, in
);
3144 for (i
= 0; i
< 4; i
++)
3145 coords
[i
] = LLVMBuildExtractElement(ctx
->builder
, v
,
3146 LLVMConstInt(ctx
->i32
, i
, false), "");
3148 coords
[2] = emit_llvm_intrinsic(ctx
, "llvm.fabs.f32", ctx
->f32
,
3149 &coords
[2], 1, LLVMReadNoneAttribute
);
3150 coords
[2] = emit_fdiv(ctx
, ctx
->f32one
, coords
[2]);
3152 mad_args
[1] = coords
[2];
3153 mad_args
[2] = LLVMConstReal(ctx
->f32
, 1.5);
3154 mad_args
[0] = coords
[0];
3157 tmp
= LLVMBuildFMul(ctx
->builder
, mad_args
[0], mad_args
[1], "");
3158 coords
[0] = LLVMBuildFAdd(ctx
->builder
, tmp
, mad_args
[2], "");
3160 mad_args
[0] = coords
[1];
3163 tmp
= LLVMBuildFMul(ctx
->builder
, mad_args
[0], mad_args
[1], "");
3164 coords
[1] = LLVMBuildFAdd(ctx
->builder
, tmp
, mad_args
[2], "");
3166 /* apply xyz = yxw swizzle to cooords */
3172 static void emit_prepare_cube_coords(struct nir_to_llvm_context
*ctx
,
3173 LLVMValueRef
*coords_arg
, int num_coords
,
3175 bool is_array
, LLVMValueRef
*derivs_arg
)
3177 LLVMValueRef coords
[4];
3179 cube_to_2d_coords(ctx
, coords_arg
, coords
);
3181 if (is_deriv
&& derivs_arg
) {
3182 LLVMValueRef derivs
[4];
3185 /* Convert cube derivatives to 2D derivatives. */
3186 for (axis
= 0; axis
< 2; axis
++) {
3187 LLVMValueRef shifted_cube_coords
[4], shifted_coords
[4];
3189 /* Shift the cube coordinates by the derivatives to get
3190 * the cube coordinates of the "neighboring pixel".
3192 for (i
= 0; i
< 3; i
++)
3193 shifted_cube_coords
[i
] =
3194 LLVMBuildFAdd(ctx
->builder
, coords_arg
[i
],
3195 derivs_arg
[axis
*3+i
], "");
3196 shifted_cube_coords
[3] = LLVMGetUndef(ctx
->f32
);
3198 /* Project the shifted cube coordinates onto the face. */
3199 cube_to_2d_coords(ctx
, shifted_cube_coords
,
3202 /* Subtract both sets of 2D coordinates to get 2D derivatives.
3203 * This won't work if the shifted coordinates ended up
3204 * in a different face.
3206 for (i
= 0; i
< 2; i
++)
3207 derivs
[axis
* 2 + i
] =
3208 LLVMBuildFSub(ctx
->builder
, shifted_coords
[i
],
3212 memcpy(derivs_arg
, derivs
, sizeof(derivs
));
3216 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
3217 /* coords_arg.w component - array_index for cube arrays */
3218 LLVMValueRef tmp
= LLVMBuildFMul(ctx
->builder
, coords_arg
[3], LLVMConstReal(ctx
->f32
, 8.0), "");
3219 coords
[2] = LLVMBuildFAdd(ctx
->builder
, tmp
, coords
[2], "");
3222 memcpy(coords_arg
, coords
, sizeof(coords
));
3225 static void visit_tex(struct nir_to_llvm_context
*ctx
, nir_tex_instr
*instr
)
3227 LLVMValueRef result
= NULL
;
3228 struct ac_tex_info tinfo
= { 0 };
3229 unsigned dmask
= 0xf;
3230 LLVMValueRef address
[16];
3231 LLVMValueRef coords
[5];
3232 LLVMValueRef coord
= NULL
, lod
= NULL
, comparitor
= NULL
, bias
, offsets
= NULL
;
3233 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
, sample_index
= NULL
;
3234 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
3235 LLVMValueRef derivs
[6];
3236 unsigned chan
, count
= 0;
3237 unsigned const_src
= 0, num_deriv_comp
= 0;
3239 tex_fetch_ptrs(ctx
, instr
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
3241 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3242 switch (instr
->src
[i
].src_type
) {
3243 case nir_tex_src_coord
:
3244 coord
= get_src(ctx
, instr
->src
[i
].src
);
3246 case nir_tex_src_projector
:
3248 case nir_tex_src_comparitor
:
3249 comparitor
= get_src(ctx
, instr
->src
[i
].src
);
3251 case nir_tex_src_offset
:
3252 offsets
= get_src(ctx
, instr
->src
[i
].src
);
3255 case nir_tex_src_bias
:
3256 bias
= get_src(ctx
, instr
->src
[i
].src
);
3258 case nir_tex_src_lod
:
3259 lod
= get_src(ctx
, instr
->src
[i
].src
);
3261 case nir_tex_src_ms_index
:
3262 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
3264 case nir_tex_src_ms_mcs
:
3266 case nir_tex_src_ddx
:
3267 ddx
= get_src(ctx
, instr
->src
[i
].src
);
3268 num_deriv_comp
= instr
->src
[i
].src
.ssa
->num_components
;
3270 case nir_tex_src_ddy
:
3271 ddy
= get_src(ctx
, instr
->src
[i
].src
);
3273 case nir_tex_src_texture_offset
:
3274 case nir_tex_src_sampler_offset
:
3275 case nir_tex_src_plane
:
3281 if (instr
->op
== nir_texop_texture_samples
) {
3282 LLVMValueRef res
, samples
;
3283 res
= LLVMBuildBitCast(ctx
->builder
, res_ptr
, ctx
->v8i32
, "");
3284 samples
= LLVMBuildExtractElement(ctx
->builder
, res
,
3285 LLVMConstInt(ctx
->i32
, 3, false), "");
3286 samples
= LLVMBuildLShr(ctx
->builder
, samples
,
3287 LLVMConstInt(ctx
->i32
, 16, false), "");
3288 samples
= LLVMBuildAnd(ctx
->builder
, samples
,
3289 LLVMConstInt(ctx
->i32
, 0xf, false), "");
3290 samples
= LLVMBuildShl(ctx
->builder
, ctx
->i32one
,
3298 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
3299 coords
[chan
] = llvm_extract_elem(ctx
, coord
, chan
);
3301 if (offsets
&& instr
->op
!= nir_texop_txf
) {
3302 LLVMValueRef offset
[3], pack
;
3303 for (chan
= 0; chan
< 3; ++chan
)
3304 offset
[chan
] = ctx
->i32zero
;
3306 tinfo
.has_offset
= true;
3307 for (chan
= 0; chan
< get_llvm_num_components(offsets
); chan
++) {
3308 offset
[chan
] = llvm_extract_elem(ctx
, offsets
, chan
);
3309 offset
[chan
] = LLVMBuildAnd(ctx
->builder
, offset
[chan
],
3310 LLVMConstInt(ctx
->i32
, 0x3f, false), "");
3312 offset
[chan
] = LLVMBuildShl(ctx
->builder
, offset
[chan
],
3313 LLVMConstInt(ctx
->i32
, chan
* 8, false), "");
3315 pack
= LLVMBuildOr(ctx
->builder
, offset
[0], offset
[1], "");
3316 pack
= LLVMBuildOr(ctx
->builder
, pack
, offset
[2], "");
3317 address
[count
++] = pack
;
3320 /* pack LOD bias value */
3321 if (instr
->op
== nir_texop_txb
&& bias
) {
3322 address
[count
++] = bias
;
3325 /* Pack depth comparison value */
3326 if (instr
->is_shadow
&& comparitor
) {
3327 address
[count
++] = llvm_extract_elem(ctx
, comparitor
, 0);
3330 /* pack derivatives */
3332 switch (instr
->sampler_dim
) {
3333 case GLSL_SAMPLER_DIM_3D
:
3334 case GLSL_SAMPLER_DIM_CUBE
:
3337 case GLSL_SAMPLER_DIM_2D
:
3341 case GLSL_SAMPLER_DIM_1D
:
3346 for (unsigned i
= 0; i
< num_deriv_comp
; i
++) {
3347 derivs
[i
* 2] = to_float(ctx
, llvm_extract_elem(ctx
, ddx
, i
));
3348 derivs
[i
* 2 + 1] = to_float(ctx
, llvm_extract_elem(ctx
, ddy
, i
));
3352 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& coord
) {
3353 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
3354 coords
[chan
] = to_float(ctx
, coords
[chan
]);
3355 if (instr
->coord_components
== 3)
3356 coords
[3] = LLVMGetUndef(ctx
->f32
);
3357 emit_prepare_cube_coords(ctx
, coords
, instr
->coord_components
, instr
->op
== nir_texop_txd
, instr
->is_array
, derivs
);
3363 for (unsigned i
= 0; i
< num_deriv_comp
* 2; i
++)
3364 address
[count
++] = derivs
[i
];
3367 /* Pack texture coordinates */
3369 address
[count
++] = coords
[0];
3370 if (instr
->coord_components
> 1)
3371 address
[count
++] = coords
[1];
3372 if (instr
->coord_components
> 2) {
3373 /* This seems like a bit of a hack - but it passes Vulkan CTS with it */
3374 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_3D
&& instr
->op
!= nir_texop_txf
) {
3375 coords
[2] = to_float(ctx
, coords
[2]);
3376 coords
[2] = emit_llvm_intrinsic(ctx
, "llvm.rint.f32", ctx
->f32
, &coords
[2],
3378 coords
[2] = to_integer(ctx
, coords
[2]);
3380 address
[count
++] = coords
[2];
3385 if ((instr
->op
== nir_texop_txl
|| instr
->op
== nir_texop_txf
) && lod
) {
3386 address
[count
++] = lod
;
3387 } else if (instr
->op
== nir_texop_txf_ms
&& sample_index
) {
3388 address
[count
++] = sample_index
;
3389 } else if(instr
->op
== nir_texop_txs
) {
3391 address
[count
++] = lod
;
3394 for (chan
= 0; chan
< count
; chan
++) {
3395 address
[chan
] = LLVMBuildBitCast(ctx
->builder
,
3396 address
[chan
], ctx
->i32
, "");
3399 if (instr
->op
== nir_texop_samples_identical
) {
3400 LLVMValueRef txf_address
[4];
3401 struct ac_tex_info txf_info
= { 0 };
3402 unsigned txf_count
= count
;
3403 memcpy(txf_address
, address
, sizeof(txf_address
));
3405 if (!instr
->is_array
)
3406 txf_address
[2] = ctx
->i32zero
;
3407 txf_address
[3] = ctx
->i32zero
;
3409 set_tex_fetch_args(ctx
, &txf_info
, instr
, nir_texop_txf
,
3411 txf_address
, txf_count
, 0xf);
3413 result
= build_tex_intrinsic(ctx
, instr
, &txf_info
);
3415 result
= LLVMBuildExtractElement(ctx
->builder
, result
, ctx
->i32zero
, "");
3416 result
= emit_int_cmp(ctx
, LLVMIntEQ
, result
, ctx
->i32zero
);
3420 /* Adjust the sample index according to FMASK.
3422 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3423 * which is the identity mapping. Each nibble says which physical sample
3424 * should be fetched to get that sample.
3426 * For example, 0x11111100 means there are only 2 samples stored and
3427 * the second sample covers 3/4 of the pixel. When reading samples 0
3428 * and 1, return physical sample 0 (determined by the first two 0s
3429 * in FMASK), otherwise return physical sample 1.
3431 * The sample index should be adjusted as follows:
3432 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
3434 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
) {
3435 LLVMValueRef txf_address
[4];
3436 struct ac_tex_info txf_info
= { 0 };
3437 unsigned txf_count
= count
;
3438 memcpy(txf_address
, address
, sizeof(txf_address
));
3440 if (!instr
->is_array
)
3441 txf_address
[2] = ctx
->i32zero
;
3442 txf_address
[3] = ctx
->i32zero
;
3444 set_tex_fetch_args(ctx
, &txf_info
, instr
, nir_texop_txf
,
3446 txf_address
, txf_count
, 0xf);
3448 result
= build_tex_intrinsic(ctx
, instr
, &txf_info
);
3449 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
3450 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
3452 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
3456 unsigned sample_chan
= instr
->is_array
? 3 : 2;
3458 LLVMValueRef sample_index4
=
3459 LLVMBuildMul(ctx
->builder
, address
[sample_chan
], four
, "");
3460 LLVMValueRef shifted_fmask
=
3461 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
3462 LLVMValueRef final_sample
=
3463 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
3465 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3466 * resource descriptor is 0 (invalid),
3468 LLVMValueRef fmask_desc
=
3469 LLVMBuildBitCast(ctx
->builder
, fmask_ptr
,
3472 LLVMValueRef fmask_word1
=
3473 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
3476 LLVMValueRef word1_is_nonzero
=
3477 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3478 fmask_word1
, ctx
->i32zero
, "");
3480 /* Replace the MSAA sample index. */
3481 address
[sample_chan
] =
3482 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
3483 final_sample
, address
[sample_chan
], "");
3486 if (offsets
&& instr
->op
== nir_texop_txf
) {
3487 nir_const_value
*const_offset
=
3488 nir_src_as_const_value(instr
->src
[const_src
].src
);
3490 assert(const_offset
);
3491 if (instr
->coord_components
> 2)
3492 address
[2] = LLVMBuildAdd(ctx
->builder
,
3493 address
[2], LLVMConstInt(ctx
->i32
, const_offset
->i32
[2], false), "");
3494 if (instr
->coord_components
> 1)
3495 address
[1] = LLVMBuildAdd(ctx
->builder
,
3496 address
[1], LLVMConstInt(ctx
->i32
, const_offset
->i32
[1], false), "");
3497 address
[0] = LLVMBuildAdd(ctx
->builder
,
3498 address
[0], LLVMConstInt(ctx
->i32
, const_offset
->i32
[0], false), "");
3502 /* TODO TG4 support */
3503 if (instr
->op
== nir_texop_tg4
) {
3504 if (instr
->is_shadow
)
3507 dmask
= 1 << instr
->component
;
3509 set_tex_fetch_args(ctx
, &tinfo
, instr
, instr
->op
,
3510 res_ptr
, samp_ptr
, address
, count
, dmask
);
3512 result
= build_tex_intrinsic(ctx
, instr
, &tinfo
);
3514 if (instr
->op
== nir_texop_query_levels
)
3515 result
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, 3, false), "");
3516 else if (instr
->op
== nir_texop_txs
&&
3517 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
3519 LLVMValueRef two
= LLVMConstInt(ctx
->i32
, 2, false);
3520 LLVMValueRef six
= LLVMConstInt(ctx
->i32
, 6, false);
3521 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->builder
, result
, two
, "");
3522 z
= LLVMBuildSDiv(ctx
->builder
, z
, six
, "");
3523 result
= LLVMBuildInsertElement(ctx
->builder
, result
, z
, two
, "");
3524 } else if (instr
->dest
.ssa
.num_components
!= 4)
3525 result
= trim_vector(ctx
, result
, instr
->dest
.ssa
.num_components
);
3529 assert(instr
->dest
.is_ssa
);
3530 result
= to_integer(ctx
, result
);
3531 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
3536 static void visit_phi(struct nir_to_llvm_context
*ctx
, nir_phi_instr
*instr
)
3538 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
3539 LLVMValueRef result
= LLVMBuildPhi(ctx
->builder
, type
, "");
3541 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
3542 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
3545 static void visit_post_phi(struct nir_to_llvm_context
*ctx
,
3546 nir_phi_instr
*instr
,
3547 LLVMValueRef llvm_phi
)
3549 nir_foreach_phi_src(src
, instr
) {
3550 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
3551 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
3553 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
3557 static void phi_post_pass(struct nir_to_llvm_context
*ctx
)
3559 struct hash_entry
*entry
;
3560 hash_table_foreach(ctx
->phis
, entry
) {
3561 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
3562 (LLVMValueRef
)entry
->data
);
3567 static void visit_ssa_undef(struct nir_to_llvm_context
*ctx
,
3568 nir_ssa_undef_instr
*instr
)
3570 unsigned num_components
= instr
->def
.num_components
;
3573 if (num_components
== 1)
3574 undef
= LLVMGetUndef(ctx
->i32
);
3576 undef
= LLVMGetUndef(LLVMVectorType(ctx
->i32
, num_components
));
3578 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, undef
);
3581 static void visit_jump(struct nir_to_llvm_context
*ctx
,
3582 nir_jump_instr
*instr
)
3584 switch (instr
->type
) {
3585 case nir_jump_break
:
3586 LLVMBuildBr(ctx
->builder
, ctx
->break_block
);
3587 LLVMClearInsertionPosition(ctx
->builder
);
3589 case nir_jump_continue
:
3590 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
3591 LLVMClearInsertionPosition(ctx
->builder
);
3594 fprintf(stderr
, "Unknown NIR jump instr: ");
3595 nir_print_instr(&instr
->instr
, stderr
);
3596 fprintf(stderr
, "\n");
3601 static void visit_cf_list(struct nir_to_llvm_context
*ctx
,
3602 struct exec_list
*list
);
3604 static void visit_block(struct nir_to_llvm_context
*ctx
, nir_block
*block
)
3606 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->builder
);
3607 nir_foreach_instr(instr
, block
)
3609 switch (instr
->type
) {
3610 case nir_instr_type_alu
:
3611 visit_alu(ctx
, nir_instr_as_alu(instr
));
3613 case nir_instr_type_load_const
:
3614 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
3616 case nir_instr_type_intrinsic
:
3617 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
3619 case nir_instr_type_tex
:
3620 visit_tex(ctx
, nir_instr_as_tex(instr
));
3622 case nir_instr_type_phi
:
3623 visit_phi(ctx
, nir_instr_as_phi(instr
));
3625 case nir_instr_type_ssa_undef
:
3626 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
3628 case nir_instr_type_jump
:
3629 visit_jump(ctx
, nir_instr_as_jump(instr
));
3632 fprintf(stderr
, "Unknown NIR instr type: ");
3633 nir_print_instr(instr
, stderr
);
3634 fprintf(stderr
, "\n");
3639 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
3642 static void visit_if(struct nir_to_llvm_context
*ctx
, nir_if
*if_stmt
)
3644 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
3646 LLVMBasicBlockRef merge_block
=
3647 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
3648 LLVMBasicBlockRef if_block
=
3649 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
3650 LLVMBasicBlockRef else_block
= merge_block
;
3651 if (!exec_list_is_empty(&if_stmt
->else_list
))
3652 else_block
= LLVMAppendBasicBlockInContext(
3653 ctx
->context
, ctx
->main_function
, "");
3655 LLVMValueRef cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, value
,
3656 LLVMConstInt(ctx
->i32
, 0, false), "");
3657 LLVMBuildCondBr(ctx
->builder
, cond
, if_block
, else_block
);
3659 LLVMPositionBuilderAtEnd(ctx
->builder
, if_block
);
3660 visit_cf_list(ctx
, &if_stmt
->then_list
);
3661 if (LLVMGetInsertBlock(ctx
->builder
))
3662 LLVMBuildBr(ctx
->builder
, merge_block
);
3664 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
3665 LLVMPositionBuilderAtEnd(ctx
->builder
, else_block
);
3666 visit_cf_list(ctx
, &if_stmt
->else_list
);
3667 if (LLVMGetInsertBlock(ctx
->builder
))
3668 LLVMBuildBr(ctx
->builder
, merge_block
);
3671 LLVMPositionBuilderAtEnd(ctx
->builder
, merge_block
);
3674 static void visit_loop(struct nir_to_llvm_context
*ctx
, nir_loop
*loop
)
3676 LLVMBasicBlockRef continue_parent
= ctx
->continue_block
;
3677 LLVMBasicBlockRef break_parent
= ctx
->break_block
;
3679 ctx
->continue_block
=
3680 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
3682 LLVMAppendBasicBlockInContext(ctx
->context
, ctx
->main_function
, "");
3684 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
3685 LLVMPositionBuilderAtEnd(ctx
->builder
, ctx
->continue_block
);
3686 visit_cf_list(ctx
, &loop
->body
);
3688 if (LLVMGetInsertBlock(ctx
->builder
))
3689 LLVMBuildBr(ctx
->builder
, ctx
->continue_block
);
3690 LLVMPositionBuilderAtEnd(ctx
->builder
, ctx
->break_block
);
3692 ctx
->continue_block
= continue_parent
;
3693 ctx
->break_block
= break_parent
;
3696 static void visit_cf_list(struct nir_to_llvm_context
*ctx
,
3697 struct exec_list
*list
)
3699 foreach_list_typed(nir_cf_node
, node
, node
, list
)
3701 switch (node
->type
) {
3702 case nir_cf_node_block
:
3703 visit_block(ctx
, nir_cf_node_as_block(node
));
3706 case nir_cf_node_if
:
3707 visit_if(ctx
, nir_cf_node_as_if(node
));
3710 case nir_cf_node_loop
:
3711 visit_loop(ctx
, nir_cf_node_as_loop(node
));
3721 handle_vs_input_decl(struct nir_to_llvm_context
*ctx
,
3722 struct nir_variable
*variable
)
3724 LLVMValueRef t_list_ptr
= ctx
->vertex_buffers
;
3725 LLVMValueRef t_offset
;
3726 LLVMValueRef t_list
;
3727 LLVMValueRef args
[3];
3729 LLVMValueRef buffer_index
;
3730 int index
= variable
->data
.location
- VERT_ATTRIB_GENERIC0
;
3731 int idx
= variable
->data
.location
;
3732 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, true);
3734 variable
->data
.driver_location
= idx
* 4;
3736 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << index
)) {
3737 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->instance_id
,
3738 ctx
->start_instance
, "");
3739 ctx
->shader_info
->vs
.vgpr_comp_cnt
= MAX2(3,
3740 ctx
->shader_info
->vs
.vgpr_comp_cnt
);
3742 buffer_index
= LLVMBuildAdd(ctx
->builder
, ctx
->vertex_id
,
3743 ctx
->base_vertex
, "");
3745 for (unsigned i
= 0; i
< attrib_count
; ++i
, ++idx
) {
3746 t_offset
= LLVMConstInt(ctx
->i32
, index
+ i
, false);
3748 t_list
= build_indexed_load_const(ctx
, t_list_ptr
, t_offset
);
3750 args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
3751 args
[2] = buffer_index
;
3752 input
= emit_llvm_intrinsic(ctx
,
3753 "llvm.SI.vs.load.input", ctx
->v4f32
, args
, 3,
3754 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
3756 for (unsigned chan
= 0; chan
< 4; chan
++) {
3757 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
3758 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
, chan
)] =
3759 to_integer(ctx
, LLVMBuildExtractElement(ctx
->builder
,
3760 input
, llvm_chan
, ""));
3766 static void interp_fs_input(struct nir_to_llvm_context
*ctx
,
3768 LLVMValueRef interp_param
,
3769 LLVMValueRef prim_mask
,
3770 LLVMValueRef result
[4])
3772 const char *intr_name
;
3773 LLVMValueRef attr_number
;
3776 attr_number
= LLVMConstInt(ctx
->i32
, attr
, false);
3778 /* fs.constant returns the param from the middle vertex, so it's not
3779 * really useful for flat shading. It's meant to be used for custom
3780 * interpolation (but the intrinsic can't fetch from the other two
3783 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
3784 * to do the right thing. The only reason we use fs.constant is that
3785 * fs.interp cannot be used on integers, because they can be equal
3788 intr_name
= interp_param
? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
3790 for (chan
= 0; chan
< 4; chan
++) {
3791 LLVMValueRef args
[4];
3792 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, false);
3794 args
[0] = llvm_chan
;
3795 args
[1] = attr_number
;
3796 args
[2] = prim_mask
;
3797 args
[3] = interp_param
;
3798 result
[chan
] = emit_llvm_intrinsic(ctx
, intr_name
,
3799 ctx
->f32
, args
, args
[3] ? 4 : 3,
3800 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
3805 handle_fs_input_decl(struct nir_to_llvm_context
*ctx
,
3806 struct nir_variable
*variable
)
3808 int idx
= variable
->data
.location
;
3809 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
3810 LLVMValueRef interp
;
3812 variable
->data
.driver_location
= idx
* 4;
3813 ctx
->input_mask
|= ((1ull << attrib_count
) - 1) << variable
->data
.location
;
3815 if (glsl_get_base_type(glsl_without_array(variable
->type
)) == GLSL_TYPE_FLOAT
)
3816 interp
= lookup_interp_param(ctx
, variable
->data
.interpolation
, INTERP_CENTER
);
3820 for (unsigned i
= 0; i
< attrib_count
; ++i
)
3821 ctx
->inputs
[radeon_llvm_reg_index_soa(idx
+ i
, 0)] = interp
;
3826 handle_shader_input_decl(struct nir_to_llvm_context
*ctx
,
3827 struct nir_variable
*variable
)
3829 switch (ctx
->stage
) {
3830 case MESA_SHADER_VERTEX
:
3831 handle_vs_input_decl(ctx
, variable
);
3833 case MESA_SHADER_FRAGMENT
:
3834 handle_fs_input_decl(ctx
, variable
);
3843 handle_fs_inputs_pre(struct nir_to_llvm_context
*ctx
,
3844 struct nir_shader
*nir
)
3847 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_INPUTS
; ++i
) {
3848 LLVMValueRef interp_param
;
3849 LLVMValueRef
*inputs
= ctx
->inputs
+radeon_llvm_reg_index_soa(i
, 0);
3851 if (!(ctx
->input_mask
& (1ull << i
)))
3854 if (i
>= VARYING_SLOT_VAR0
|| i
== VARYING_SLOT_PNTC
) {
3855 interp_param
= *inputs
;
3856 interp_fs_input(ctx
, index
, interp_param
, ctx
->prim_mask
,
3860 ctx
->shader_info
->fs
.flat_shaded_mask
|= 1u << index
;
3862 } else if (i
== VARYING_SLOT_POS
) {
3863 for(int i
= 0; i
< 3; ++i
)
3864 inputs
[i
] = ctx
->frag_pos
[i
];
3866 inputs
[3] = emit_fdiv(ctx
, ctx
->f32one
, ctx
->frag_pos
[3]);
3869 ctx
->shader_info
->fs
.num_interp
= index
;
3870 if (ctx
->input_mask
& (1 << VARYING_SLOT_PNTC
))
3871 ctx
->shader_info
->fs
.has_pcoord
= true;
3872 ctx
->shader_info
->fs
.input_mask
= ctx
->input_mask
>> VARYING_SLOT_VAR0
;
3876 ac_build_alloca(struct nir_to_llvm_context
*ctx
,
3880 LLVMBuilderRef builder
= ctx
->builder
;
3881 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
3882 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
3883 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
3884 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
3885 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ctx
->context
);
3889 LLVMPositionBuilderBefore(first_builder
, first_instr
);
3891 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
3894 res
= LLVMBuildAlloca(first_builder
, type
, name
);
3895 LLVMBuildStore(builder
, LLVMConstNull(type
), res
);
3897 LLVMDisposeBuilder(first_builder
);
3902 static LLVMValueRef
si_build_alloca_undef(struct nir_to_llvm_context
*ctx
,
3906 LLVMValueRef ptr
= ac_build_alloca(ctx
, type
, name
);
3907 LLVMBuildStore(ctx
->builder
, LLVMGetUndef(type
), ptr
);
3912 handle_shader_output_decl(struct nir_to_llvm_context
*ctx
,
3913 struct nir_variable
*variable
)
3915 int idx
= variable
->data
.location
;
3916 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
3918 variable
->data
.driver_location
= idx
* 4;
3920 if (ctx
->stage
== MESA_SHADER_VERTEX
) {
3922 if (idx
== VARYING_SLOT_CLIP_DIST0
||
3923 idx
== VARYING_SLOT_CULL_DIST0
) {
3924 int length
= glsl_get_length(variable
->type
);
3925 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
3926 ctx
->shader_info
->vs
.clip_dist_mask
= (1 << length
) - 1;
3927 ctx
->num_clips
= length
;
3928 } else if (idx
== VARYING_SLOT_CULL_DIST0
) {
3929 ctx
->shader_info
->vs
.cull_dist_mask
= (1 << length
) - 1;
3930 ctx
->num_culls
= length
;
3939 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
3940 for (unsigned chan
= 0; chan
< 4; chan
++) {
3941 ctx
->outputs
[radeon_llvm_reg_index_soa(idx
+ i
, chan
)] =
3942 si_build_alloca_undef(ctx
, ctx
->f32
, "");
3945 ctx
->output_mask
|= ((1ull << attrib_count
) - 1) << variable
->data
.location
;
3949 setup_locals(struct nir_to_llvm_context
*ctx
,
3950 struct nir_function
*func
)
3953 ctx
->num_locals
= 0;
3954 nir_foreach_variable(variable
, &func
->impl
->locals
) {
3955 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
3956 variable
->data
.driver_location
= ctx
->num_locals
* 4;
3957 ctx
->num_locals
+= attrib_count
;
3959 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
3963 for (i
= 0; i
< ctx
->num_locals
; i
++) {
3964 for (j
= 0; j
< 4; j
++) {
3965 ctx
->locals
[i
* 4 + j
] =
3966 si_build_alloca_undef(ctx
, ctx
->f32
, "temp");
3972 emit_float_saturate(struct nir_to_llvm_context
*ctx
, LLVMValueRef v
, float lo
, float hi
)
3974 v
= to_float(ctx
, v
);
3975 v
= emit_intrin_2f_param(ctx
, "llvm.maxnum.f32", v
, LLVMConstReal(ctx
->f32
, lo
));
3976 return emit_intrin_2f_param(ctx
, "llvm.minnum.f32", v
, LLVMConstReal(ctx
->f32
, hi
));
3980 static LLVMValueRef
emit_pack_int16(struct nir_to_llvm_context
*ctx
,
3981 LLVMValueRef src0
, LLVMValueRef src1
)
3983 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
3984 LLVMValueRef comp
[2];
3986 comp
[0] = LLVMBuildAnd(ctx
->builder
, src0
, LLVMConstInt(ctx
-> i32
, 65535, 0), "");
3987 comp
[1] = LLVMBuildAnd(ctx
->builder
, src1
, LLVMConstInt(ctx
-> i32
, 65535, 0), "");
3988 comp
[1] = LLVMBuildShl(ctx
->builder
, comp
[1], const16
, "");
3989 return LLVMBuildOr(ctx
->builder
, comp
[0], comp
[1], "");
3992 /* Initialize arguments for the shader export intrinsic */
3994 si_llvm_init_export_args(struct nir_to_llvm_context
*ctx
,
3995 LLVMValueRef
*values
,
3999 /* Default is 0xf. Adjusted below depending on the format. */
4000 args
[0] = LLVMConstInt(ctx
->i32
, target
!= V_008DFC_SQ_EXP_NULL
? 0xf : 0, false);
4001 /* Specify whether the EXEC mask represents the valid mask */
4002 args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
4004 /* Specify whether this is the last export */
4005 args
[2] = LLVMConstInt(ctx
->i32
, 0, false);
4006 /* Specify the target we are exporting */
4007 args
[3] = LLVMConstInt(ctx
->i32
, target
, false);
4009 args
[4] = LLVMConstInt(ctx
->i32
, 0, false); /* COMPR flag */
4010 args
[5] = LLVMGetUndef(ctx
->f32
);
4011 args
[6] = LLVMGetUndef(ctx
->f32
);
4012 args
[7] = LLVMGetUndef(ctx
->f32
);
4013 args
[8] = LLVMGetUndef(ctx
->f32
);
4018 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&& target
>= V_008DFC_SQ_EXP_MRT
) {
4019 LLVMValueRef val
[4];
4020 unsigned index
= target
- V_008DFC_SQ_EXP_MRT
;
4021 unsigned col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * index
)) & 0xf;
4022 bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> index
) & 1;
4024 switch(col_format
) {
4025 case V_028714_SPI_SHADER_ZERO
:
4026 args
[0] = LLVMConstInt(ctx
->i32
, 0x0, 0);
4027 args
[3] = LLVMConstInt(ctx
->i32
, V_008DFC_SQ_EXP_NULL
, 0);
4030 case V_028714_SPI_SHADER_32_R
:
4031 args
[0] = LLVMConstInt(ctx
->i32
, 0x1, 0);
4032 args
[5] = values
[0];
4035 case V_028714_SPI_SHADER_32_GR
:
4036 args
[0] = LLVMConstInt(ctx
->i32
, 0x3, 0);
4037 args
[5] = values
[0];
4038 args
[6] = values
[1];
4041 case V_028714_SPI_SHADER_32_AR
:
4042 args
[0] = LLVMConstInt(ctx
->i32
, 0x9, 0);
4043 args
[5] = values
[0];
4044 args
[8] = values
[3];
4047 case V_028714_SPI_SHADER_FP16_ABGR
:
4048 args
[4] = ctx
->i32one
;
4050 for (unsigned chan
= 0; chan
< 2; chan
++) {
4051 LLVMValueRef pack_args
[2] = {
4053 values
[2 * chan
+ 1]
4055 LLVMValueRef packed
;
4057 packed
= emit_llvm_intrinsic(ctx
, "llvm.SI.packf16",
4058 ctx
->i32
, pack_args
, 2,
4059 LLVMReadNoneAttribute
);
4060 args
[chan
+ 5] = packed
;
4064 case V_028714_SPI_SHADER_UNORM16_ABGR
:
4065 for (unsigned chan
= 0; chan
< 4; chan
++) {
4066 val
[chan
] = emit_float_saturate(ctx
, values
[chan
], 0, 1);
4067 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
4068 LLVMConstReal(ctx
->f32
, 65535), "");
4069 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
4070 LLVMConstReal(ctx
->f32
, 0.5), "");
4071 val
[chan
] = LLVMBuildFPToUI(ctx
->builder
, val
[chan
],
4075 args
[4] = ctx
->i32one
;
4076 args
[5] = emit_pack_int16(ctx
, val
[0], val
[1]);
4077 args
[6] = emit_pack_int16(ctx
, val
[2], val
[3]);
4080 case V_028714_SPI_SHADER_SNORM16_ABGR
:
4081 for (unsigned chan
= 0; chan
< 4; chan
++) {
4082 val
[chan
] = emit_float_saturate(ctx
, values
[chan
], -1, 1);
4083 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
4084 LLVMConstReal(ctx
->f32
, 32767), "");
4086 /* If positive, add 0.5, else add -0.5. */
4087 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
4088 LLVMBuildSelect(ctx
->builder
,
4089 LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
,
4090 val
[chan
], ctx
->f32zero
, ""),
4091 LLVMConstReal(ctx
->f32
, 0.5),
4092 LLVMConstReal(ctx
->f32
, -0.5), ""), "");
4093 val
[chan
] = LLVMBuildFPToSI(ctx
->builder
, val
[chan
], ctx
->i32
, "");
4096 args
[4] = ctx
->i32one
;
4097 args
[5] = emit_pack_int16(ctx
, val
[0], val
[1]);
4098 args
[6] = emit_pack_int16(ctx
, val
[2], val
[3]);
4101 case V_028714_SPI_SHADER_UINT16_ABGR
: {
4102 LLVMValueRef max
= LLVMConstInt(ctx
->i32
, is_int8
? 255 : 65535, 0);
4104 for (unsigned chan
= 0; chan
< 4; chan
++) {
4105 val
[chan
] = to_integer(ctx
, values
[chan
]);
4106 val
[chan
] = emit_minmax_int(ctx
, LLVMIntULT
, val
[chan
], max
);
4109 args
[4] = ctx
->i32one
;
4110 args
[5] = emit_pack_int16(ctx
, val
[0], val
[1]);
4111 args
[6] = emit_pack_int16(ctx
, val
[2], val
[3]);
4115 case V_028714_SPI_SHADER_SINT16_ABGR
: {
4116 LLVMValueRef max
= LLVMConstInt(ctx
->i32
, is_int8
? 127 : 32767, 0);
4117 LLVMValueRef min
= LLVMConstInt(ctx
->i32
, is_int8
? -128 : -32768, 0);
4120 for (unsigned chan
= 0; chan
< 4; chan
++) {
4121 val
[chan
] = to_integer(ctx
, values
[chan
]);
4122 val
[chan
] = emit_minmax_int(ctx
, LLVMIntSLT
, val
[chan
], max
);
4123 val
[chan
] = emit_minmax_int(ctx
, LLVMIntSGT
, val
[chan
], min
);
4126 args
[4] = ctx
->i32one
;
4127 args
[5] = emit_pack_int16(ctx
, val
[0], val
[1]);
4128 args
[6] = emit_pack_int16(ctx
, val
[2], val
[3]);
4133 case V_028714_SPI_SHADER_32_ABGR
:
4134 memcpy(&args
[5], values
, sizeof(values
[0]) * 4);
4138 memcpy(&args
[5], values
, sizeof(values
[0]) * 4);
4140 for (unsigned i
= 5; i
< 9; ++i
)
4141 args
[i
] = to_float(ctx
, args
[i
]);
4145 handle_vs_outputs_post(struct nir_to_llvm_context
*ctx
,
4146 struct nir_shader
*nir
)
4148 uint32_t param_count
= 0;
4150 unsigned pos_idx
, num_pos_exports
= 0;
4151 LLVMValueRef args
[9];
4152 LLVMValueRef pos_args
[4][9] = { { 0 } };
4153 LLVMValueRef psize_value
= 0;
4155 const uint64_t clip_mask
= ctx
->output_mask
& ((1ull << VARYING_SLOT_CLIP_DIST0
) |
4156 (1ull << VARYING_SLOT_CLIP_DIST1
) |
4157 (1ull << VARYING_SLOT_CULL_DIST0
) |
4158 (1ull << VARYING_SLOT_CULL_DIST1
));
4161 LLVMValueRef slots
[8];
4164 if (ctx
->shader_info
->vs
.cull_dist_mask
)
4165 ctx
->shader_info
->vs
.cull_dist_mask
<<= ctx
->num_clips
;
4167 i
= VARYING_SLOT_CLIP_DIST0
;
4168 for (j
= 0; j
< ctx
->num_clips
; j
++)
4169 slots
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4170 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4171 i
= VARYING_SLOT_CULL_DIST0
;
4172 for (j
= 0; j
< ctx
->num_culls
; j
++)
4173 slots
[ctx
->num_clips
+ j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4174 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4176 for (i
= ctx
->num_clips
+ ctx
->num_culls
; i
< 8; i
++)
4177 slots
[i
] = LLVMGetUndef(ctx
->f32
);
4179 if (ctx
->num_clips
+ ctx
->num_culls
> 4) {
4180 target
= V_008DFC_SQ_EXP_POS
+ 3;
4181 si_llvm_init_export_args(ctx
, &slots
[4], target
, args
);
4182 memcpy(pos_args
[target
- V_008DFC_SQ_EXP_POS
],
4183 args
, sizeof(args
));
4186 target
= V_008DFC_SQ_EXP_POS
+ 2;
4187 si_llvm_init_export_args(ctx
, &slots
[0], target
, args
);
4188 memcpy(pos_args
[target
- V_008DFC_SQ_EXP_POS
],
4189 args
, sizeof(args
));
4193 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
4194 LLVMValueRef values
[4];
4195 if (!(ctx
->output_mask
& (1ull << i
)))
4198 for (unsigned j
= 0; j
< 4; j
++)
4199 values
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4200 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4202 if (i
== VARYING_SLOT_POS
) {
4203 target
= V_008DFC_SQ_EXP_POS
;
4204 } else if (i
== VARYING_SLOT_CLIP_DIST0
||
4205 i
== VARYING_SLOT_CLIP_DIST1
||
4206 i
== VARYING_SLOT_CULL_DIST0
||
4207 i
== VARYING_SLOT_CULL_DIST1
) {
4209 } else if (i
== VARYING_SLOT_PSIZ
) {
4210 ctx
->shader_info
->vs
.writes_pointsize
= true;
4211 psize_value
= values
[0];
4213 } else if (i
>= VARYING_SLOT_VAR0
) {
4214 ctx
->shader_info
->vs
.export_mask
|= 1u << (i
- VARYING_SLOT_VAR0
);
4215 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
4219 si_llvm_init_export_args(ctx
, values
, target
, args
);
4221 if (target
>= V_008DFC_SQ_EXP_POS
&&
4222 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
4223 memcpy(pos_args
[target
- V_008DFC_SQ_EXP_POS
],
4224 args
, sizeof(args
));
4226 emit_llvm_intrinsic(ctx
,
4228 LLVMVoidTypeInContext(ctx
->context
),
4233 /* We need to add the position output manually if it's missing. */
4234 if (!pos_args
[0][0]) {
4235 pos_args
[0][0] = LLVMConstInt(ctx
->i32
, 0xf, false);
4236 pos_args
[0][1] = ctx
->i32zero
; /* EXEC mask */
4237 pos_args
[0][2] = ctx
->i32zero
; /* last export? */
4238 pos_args
[0][3] = LLVMConstInt(ctx
->i32
, V_008DFC_SQ_EXP_POS
, false);
4239 pos_args
[0][4] = ctx
->i32zero
; /* COMPR flag */
4240 pos_args
[0][5] = ctx
->f32zero
; /* X */
4241 pos_args
[0][6] = ctx
->f32zero
; /* Y */
4242 pos_args
[0][7] = ctx
->f32zero
; /* Z */
4243 pos_args
[0][8] = ctx
->f32one
; /* W */
4246 if (ctx
->shader_info
->vs
.writes_pointsize
== true) {
4247 pos_args
[1][0] = LLVMConstInt(ctx
->i32
, (ctx
->shader_info
->vs
.writes_pointsize
== true), false); /* writemask */
4248 pos_args
[1][1] = ctx
->i32zero
; /* EXEC mask */
4249 pos_args
[1][2] = ctx
->i32zero
; /* last export? */
4250 pos_args
[1][3] = LLVMConstInt(ctx
->i32
, V_008DFC_SQ_EXP_POS
+ 1, false);
4251 pos_args
[1][4] = ctx
->i32zero
; /* COMPR flag */
4252 pos_args
[1][5] = ctx
->f32zero
; /* X */
4253 pos_args
[1][6] = ctx
->f32zero
; /* Y */
4254 pos_args
[1][7] = ctx
->f32zero
; /* Z */
4255 pos_args
[1][8] = ctx
->f32zero
; /* W */
4257 if (ctx
->shader_info
->vs
.writes_pointsize
== true)
4258 pos_args
[1][5] = psize_value
;
4260 for (i
= 0; i
< 4; i
++) {
4266 for (i
= 0; i
< 4; i
++) {
4267 if (!pos_args
[i
][0])
4270 /* Specify the target we are exporting */
4271 pos_args
[i
][3] = LLVMConstInt(ctx
->i32
, V_008DFC_SQ_EXP_POS
+ pos_idx
++, false);
4272 if (pos_idx
== num_pos_exports
)
4273 pos_args
[i
][2] = ctx
->i32one
;
4274 emit_llvm_intrinsic(ctx
,
4276 LLVMVoidTypeInContext(ctx
->context
),
4280 ctx
->shader_info
->vs
.pos_exports
= num_pos_exports
;
4281 ctx
->shader_info
->vs
.param_exports
= param_count
;
4285 si_export_mrt_color(struct nir_to_llvm_context
*ctx
,
4286 LLVMValueRef
*color
, unsigned param
, bool is_last
)
4288 LLVMValueRef args
[9];
4290 si_llvm_init_export_args(ctx
, color
, param
,
4294 args
[1] = ctx
->i32one
; /* whether the EXEC mask is valid */
4295 args
[2] = ctx
->i32one
; /* DONE bit */
4296 } else if (args
[0] == ctx
->i32zero
)
4297 return; /* unnecessary NULL export */
4299 emit_llvm_intrinsic(ctx
, "llvm.SI.export",
4300 ctx
->voidt
, args
, 9, 0);
4304 si_export_mrt_z(struct nir_to_llvm_context
*ctx
,
4305 LLVMValueRef depth
, LLVMValueRef stencil
,
4306 LLVMValueRef samplemask
)
4308 LLVMValueRef args
[9];
4310 args
[1] = ctx
->i32one
; /* whether the EXEC mask is valid */
4311 args
[2] = ctx
->i32one
; /* DONE bit */
4312 /* Specify the target we are exporting */
4313 args
[3] = LLVMConstInt(ctx
->i32
, V_008DFC_SQ_EXP_MRTZ
, false);
4315 args
[4] = ctx
->i32zero
; /* COMP flag */
4316 args
[5] = LLVMGetUndef(ctx
->f32
); /* R, depth */
4317 args
[6] = LLVMGetUndef(ctx
->f32
); /* G, stencil test val[0:7], stencil op val[8:15] */
4318 args
[7] = LLVMGetUndef(ctx
->f32
); /* B, sample mask */
4319 args
[8] = LLVMGetUndef(ctx
->f32
); /* A, alpha to mask */
4332 args
[7] = samplemask
;
4336 /* SI (except OLAND) has a bug that it only looks
4337 * at the X writemask component. */
4338 if (ctx
->options
->chip_class
== SI
&&
4339 ctx
->options
->family
!= CHIP_OLAND
)
4342 args
[0] = LLVMConstInt(ctx
->i32
, mask
, false);
4343 emit_llvm_intrinsic(ctx
, "llvm.SI.export",
4344 ctx
->voidt
, args
, 9, 0);
4348 handle_fs_outputs_post(struct nir_to_llvm_context
*ctx
,
4349 struct nir_shader
*nir
)
4352 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
4354 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_OUTPUTS
; ++i
) {
4355 LLVMValueRef values
[4];
4357 if (!(ctx
->output_mask
& (1ull << i
)))
4360 last
= ctx
->output_mask
<= ((1ull << (i
+ 1)) - 1);
4362 if (i
== FRAG_RESULT_DEPTH
) {
4363 ctx
->shader_info
->fs
.writes_z
= true;
4364 depth
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4365 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
4366 } else if (i
== FRAG_RESULT_STENCIL
) {
4367 ctx
->shader_info
->fs
.writes_stencil
= true;
4368 stencil
= to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4369 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, 0)], ""));
4371 for (unsigned j
= 0; j
< 4; j
++)
4372 values
[j
] = to_float(ctx
, LLVMBuildLoad(ctx
->builder
,
4373 ctx
->outputs
[radeon_llvm_reg_index_soa(i
, j
)], ""));
4375 si_export_mrt_color(ctx
, values
, V_008DFC_SQ_EXP_MRT
+ index
, last
);
4380 if (depth
|| stencil
)
4381 si_export_mrt_z(ctx
, depth
, stencil
, samplemask
);
4383 si_export_mrt_color(ctx
, NULL
, V_008DFC_SQ_EXP_NULL
, true);
4385 ctx
->shader_info
->fs
.output_mask
= index
? ((1ull << index
) - 1) : 0;
4389 handle_shader_outputs_post(struct nir_to_llvm_context
*ctx
,
4390 struct nir_shader
*nir
)
4392 switch (ctx
->stage
) {
4393 case MESA_SHADER_VERTEX
:
4394 handle_vs_outputs_post(ctx
, nir
);
4396 case MESA_SHADER_FRAGMENT
:
4397 handle_fs_outputs_post(ctx
, nir
);
4405 handle_shared_compute_var(struct nir_to_llvm_context
*ctx
,
4406 struct nir_variable
*variable
, uint32_t *offset
, int idx
)
4408 unsigned size
= glsl_count_attribute_slots(variable
->type
, false);
4409 variable
->data
.driver_location
= *offset
;
4413 static void ac_llvm_finalize_module(struct nir_to_llvm_context
* ctx
)
4415 LLVMPassManagerRef passmgr
;
4416 /* Create the pass manager */
4417 passmgr
= LLVMCreateFunctionPassManagerForModule(
4420 /* This pass should eliminate all the load and store instructions */
4421 LLVMAddPromoteMemoryToRegisterPass(passmgr
);
4423 /* Add some optimization passes */
4424 LLVMAddScalarReplAggregatesPass(passmgr
);
4425 LLVMAddLICMPass(passmgr
);
4426 LLVMAddAggressiveDCEPass(passmgr
);
4427 LLVMAddCFGSimplificationPass(passmgr
);
4428 LLVMAddInstructionCombiningPass(passmgr
);
4431 LLVMInitializeFunctionPassManager(passmgr
);
4432 LLVMRunFunctionPassManager(passmgr
, ctx
->main_function
);
4433 LLVMFinalizeFunctionPassManager(passmgr
);
4435 LLVMDisposeBuilder(ctx
->builder
);
4436 LLVMDisposePassManager(passmgr
);
4440 LLVMModuleRef
ac_translate_nir_to_llvm(LLVMTargetMachineRef tm
,
4441 struct nir_shader
*nir
,
4442 struct ac_shader_variant_info
*shader_info
,
4443 const struct ac_nir_compiler_options
*options
)
4445 struct nir_to_llvm_context ctx
= {0};
4446 struct nir_function
*func
;
4447 ctx
.options
= options
;
4448 ctx
.shader_info
= shader_info
;
4449 ctx
.context
= LLVMContextCreate();
4450 ctx
.module
= LLVMModuleCreateWithNameInContext("shader", ctx
.context
);
4452 memset(shader_info
, 0, sizeof(*shader_info
));
4454 LLVMSetTarget(ctx
.module
, "amdgcn--");
4457 ctx
.builder
= LLVMCreateBuilderInContext(ctx
.context
);
4458 ctx
.stage
= nir
->stage
;
4460 create_function(&ctx
, nir
);
4462 if (nir
->stage
== MESA_SHADER_COMPUTE
) {
4464 nir_foreach_variable(variable
, &nir
->shared
)
4468 uint32_t shared_size
= 0;
4470 LLVMTypeRef i8p
= LLVMPointerType(ctx
.i8
, LOCAL_ADDR_SPACE
);
4471 nir_foreach_variable(variable
, &nir
->shared
) {
4472 handle_shared_compute_var(&ctx
, variable
, &shared_size
, idx
);
4477 var
= LLVMAddGlobalInAddressSpace(ctx
.module
,
4478 LLVMArrayType(ctx
.i8
, shared_size
),
4481 LLVMSetAlignment(var
, 4);
4482 ctx
.shared_memory
= LLVMBuildBitCast(ctx
.builder
, var
, i8p
, "");
4486 nir_foreach_variable(variable
, &nir
->inputs
)
4487 handle_shader_input_decl(&ctx
, variable
);
4489 if (nir
->stage
== MESA_SHADER_FRAGMENT
)
4490 handle_fs_inputs_pre(&ctx
, nir
);
4492 nir_foreach_variable(variable
, &nir
->outputs
)
4493 handle_shader_output_decl(&ctx
, variable
);
4495 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4496 _mesa_key_pointer_equal
);
4497 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4498 _mesa_key_pointer_equal
);
4500 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
4502 setup_locals(&ctx
, func
);
4504 visit_cf_list(&ctx
, &func
->impl
->body
);
4505 phi_post_pass(&ctx
);
4507 handle_shader_outputs_post(&ctx
, nir
);
4508 LLVMBuildRetVoid(ctx
.builder
);
4510 ac_llvm_finalize_module(&ctx
);
4512 ralloc_free(ctx
.defs
);
4513 ralloc_free(ctx
.phis
);
4518 static void ac_diagnostic_handler(LLVMDiagnosticInfoRef di
, void *context
)
4520 unsigned *retval
= (unsigned *)context
;
4521 LLVMDiagnosticSeverity severity
= LLVMGetDiagInfoSeverity(di
);
4522 char *description
= LLVMGetDiagInfoDescription(di
);
4524 if (severity
== LLVMDSError
) {
4526 fprintf(stderr
, "LLVM triggered Diagnostic Handler: %s\n",
4530 LLVMDisposeMessage(description
);
4533 static unsigned ac_llvm_compile(LLVMModuleRef M
,
4534 struct ac_shader_binary
*binary
,
4535 LLVMTargetMachineRef tm
)
4537 unsigned retval
= 0;
4539 LLVMContextRef llvm_ctx
;
4540 LLVMMemoryBufferRef out_buffer
;
4541 unsigned buffer_size
;
4542 const char *buffer_data
;
4545 /* Setup Diagnostic Handler*/
4546 llvm_ctx
= LLVMGetModuleContext(M
);
4548 LLVMContextSetDiagnosticHandler(llvm_ctx
, ac_diagnostic_handler
,
4552 mem_err
= LLVMTargetMachineEmitToMemoryBuffer(tm
, M
, LLVMObjectFile
,
4555 /* Process Errors/Warnings */
4557 fprintf(stderr
, "%s: %s", __FUNCTION__
, err
);
4563 /* Extract Shader Code*/
4564 buffer_size
= LLVMGetBufferSize(out_buffer
);
4565 buffer_data
= LLVMGetBufferStart(out_buffer
);
4567 ac_elf_read(buffer_data
, buffer_size
, binary
);
4570 LLVMDisposeMemoryBuffer(out_buffer
);
4576 void ac_compile_nir_shader(LLVMTargetMachineRef tm
,
4577 struct ac_shader_binary
*binary
,
4578 struct ac_shader_config
*config
,
4579 struct ac_shader_variant_info
*shader_info
,
4580 struct nir_shader
*nir
,
4581 const struct ac_nir_compiler_options
*options
,
4585 LLVMModuleRef llvm_module
= ac_translate_nir_to_llvm(tm
, nir
, shader_info
,
4588 LLVMDumpModule(llvm_module
);
4590 memset(binary
, 0, sizeof(*binary
));
4591 int v
= ac_llvm_compile(llvm_module
, binary
, tm
);
4593 fprintf(stderr
, "compile failed\n");
4597 fprintf(stderr
, "disasm:\n%s\n", binary
->disasm_string
);
4599 ac_shader_binary_read_config(binary
, config
, 0);
4601 LLVMContextRef ctx
= LLVMGetModuleContext(llvm_module
);
4602 LLVMDisposeModule(llvm_module
);
4603 LLVMContextDispose(ctx
);
4605 if (nir
->stage
== MESA_SHADER_FRAGMENT
) {
4606 shader_info
->num_input_vgprs
= 0;
4607 if (G_0286CC_PERSP_SAMPLE_ENA(config
->spi_ps_input_addr
))
4608 shader_info
->num_input_vgprs
+= 2;
4609 if (G_0286CC_PERSP_CENTER_ENA(config
->spi_ps_input_addr
))
4610 shader_info
->num_input_vgprs
+= 2;
4611 if (G_0286CC_PERSP_CENTROID_ENA(config
->spi_ps_input_addr
))
4612 shader_info
->num_input_vgprs
+= 2;
4613 if (G_0286CC_PERSP_PULL_MODEL_ENA(config
->spi_ps_input_addr
))
4614 shader_info
->num_input_vgprs
+= 3;
4615 if (G_0286CC_LINEAR_SAMPLE_ENA(config
->spi_ps_input_addr
))
4616 shader_info
->num_input_vgprs
+= 2;
4617 if (G_0286CC_LINEAR_CENTER_ENA(config
->spi_ps_input_addr
))
4618 shader_info
->num_input_vgprs
+= 2;
4619 if (G_0286CC_LINEAR_CENTROID_ENA(config
->spi_ps_input_addr
))
4620 shader_info
->num_input_vgprs
+= 2;
4621 if (G_0286CC_LINE_STIPPLE_TEX_ENA(config
->spi_ps_input_addr
))
4622 shader_info
->num_input_vgprs
+= 1;
4623 if (G_0286CC_POS_X_FLOAT_ENA(config
->spi_ps_input_addr
))
4624 shader_info
->num_input_vgprs
+= 1;
4625 if (G_0286CC_POS_Y_FLOAT_ENA(config
->spi_ps_input_addr
))
4626 shader_info
->num_input_vgprs
+= 1;
4627 if (G_0286CC_POS_Z_FLOAT_ENA(config
->spi_ps_input_addr
))
4628 shader_info
->num_input_vgprs
+= 1;
4629 if (G_0286CC_POS_W_FLOAT_ENA(config
->spi_ps_input_addr
))
4630 shader_info
->num_input_vgprs
+= 1;
4631 if (G_0286CC_FRONT_FACE_ENA(config
->spi_ps_input_addr
))
4632 shader_info
->num_input_vgprs
+= 1;
4633 if (G_0286CC_ANCILLARY_ENA(config
->spi_ps_input_addr
))
4634 shader_info
->num_input_vgprs
+= 1;
4635 if (G_0286CC_SAMPLE_COVERAGE_ENA(config
->spi_ps_input_addr
))
4636 shader_info
->num_input_vgprs
+= 1;
4637 if (G_0286CC_POS_FIXED_PT_ENA(config
->spi_ps_input_addr
))
4638 shader_info
->num_input_vgprs
+= 1;
4640 config
->num_vgprs
= MAX2(config
->num_vgprs
, shader_info
->num_input_vgprs
);
4642 /* +3 for scratch wave offset and VCC */
4643 config
->num_sgprs
= MAX2(config
->num_sgprs
,
4644 shader_info
->num_input_sgprs
+ 3);
4645 if (nir
->stage
== MESA_SHADER_COMPUTE
) {
4646 for (int i
= 0; i
< 3; ++i
)
4647 shader_info
->cs
.block_size
[i
] = nir
->info
->cs
.local_size
[i
];
4650 if (nir
->stage
== MESA_SHADER_FRAGMENT
)
4651 shader_info
->fs
.early_fragment_test
= nir
->info
->fs
.early_fragment_tests
;