2 * Copyright 2014 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sub license, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
15 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
17 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
18 * USE OR OTHER DEALINGS IN THE SOFTWARE.
20 * The above copyright notice and this permission notice (including the
21 * next paragraph) shall be included in all copies or substantial portions
25 /* based on pieces from si_pipe.c and radeon_llvm_emit.c */
26 #include "ac_llvm_build.h"
28 #include <llvm-c/Core.h>
30 #include "c11/threads.h"
35 #include "ac_llvm_util.h"
37 #include "util/bitscan.h"
38 #include "util/macros.h"
41 /* Initialize module-independent parts of the context.
43 * The caller is responsible for initializing ctx::module and ctx::builder.
46 ac_llvm_context_init(struct ac_llvm_context
*ctx
, LLVMContextRef context
)
50 ctx
->context
= context
;
54 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->context
);
55 ctx
->i1
= LLVMInt1TypeInContext(ctx
->context
);
56 ctx
->i8
= LLVMInt8TypeInContext(ctx
->context
);
57 ctx
->i32
= LLVMIntTypeInContext(ctx
->context
, 32);
58 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
59 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
60 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
61 ctx
->v16i8
= LLVMVectorType(ctx
->i8
, 16);
63 ctx
->range_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
66 ctx
->invariant_load_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
67 "invariant.load", 14);
69 ctx
->fpmath_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
, "fpmath", 6);
71 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
72 ctx
->fpmath_md_2p5_ulp
= LLVMMDNodeInContext(ctx
->context
, args
, 1);
74 ctx
->uniform_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
75 "amdgpu.uniform", 14);
77 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
81 ac_emit_llvm_intrinsic(struct ac_llvm_context
*ctx
, const char *name
,
82 LLVMTypeRef return_type
, LLVMValueRef
*params
,
83 unsigned param_count
, unsigned attrib_mask
)
85 LLVMValueRef function
, call
;
86 bool set_callsite_attrs
= HAVE_LLVM
>= 0x0400 &&
87 !(attrib_mask
& AC_FUNC_ATTR_LEGACY
);
89 function
= LLVMGetNamedFunction(ctx
->module
, name
);
91 LLVMTypeRef param_types
[32], function_type
;
94 assert(param_count
<= 32);
96 for (i
= 0; i
< param_count
; ++i
) {
98 param_types
[i
] = LLVMTypeOf(params
[i
]);
101 LLVMFunctionType(return_type
, param_types
, param_count
, 0);
102 function
= LLVMAddFunction(ctx
->module
, name
, function_type
);
104 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
105 LLVMSetLinkage(function
, LLVMExternalLinkage
);
107 if (!set_callsite_attrs
)
108 ac_add_func_attributes(ctx
->context
, function
, attrib_mask
);
111 call
= LLVMBuildCall(ctx
->builder
, function
, params
, param_count
, "");
112 if (set_callsite_attrs
)
113 ac_add_func_attributes(ctx
->context
, call
, attrib_mask
);
117 static LLVMValueRef
bitcast_to_float(struct ac_llvm_context
*ctx
,
120 LLVMTypeRef type
= LLVMTypeOf(value
);
121 LLVMTypeRef new_type
;
123 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
124 new_type
= LLVMVectorType(ctx
->f32
, LLVMGetVectorSize(type
));
128 return LLVMBuildBitCast(ctx
->builder
, value
, new_type
, "");
132 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
135 void ac_build_type_name_for_intr(LLVMTypeRef type
, char *buf
, unsigned bufsize
)
137 LLVMTypeRef elem_type
= type
;
139 assert(bufsize
>= 8);
141 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
142 int ret
= snprintf(buf
, bufsize
, "v%u",
143 LLVMGetVectorSize(type
));
145 char *type_name
= LLVMPrintTypeToString(type
);
146 fprintf(stderr
, "Error building type name for: %s\n",
150 elem_type
= LLVMGetElementType(type
);
154 switch (LLVMGetTypeKind(elem_type
)) {
156 case LLVMIntegerTypeKind
:
157 snprintf(buf
, bufsize
, "i%d", LLVMGetIntTypeWidth(elem_type
));
159 case LLVMFloatTypeKind
:
160 snprintf(buf
, bufsize
, "f32");
162 case LLVMDoubleTypeKind
:
163 snprintf(buf
, bufsize
, "f64");
169 ac_build_gather_values_extended(struct ac_llvm_context
*ctx
,
170 LLVMValueRef
*values
,
171 unsigned value_count
,
172 unsigned value_stride
,
175 LLVMBuilderRef builder
= ctx
->builder
;
176 LLVMValueRef vec
= NULL
;
179 if (value_count
== 1) {
181 return LLVMBuildLoad(builder
, values
[0], "");
183 } else if (!value_count
)
184 unreachable("value_count is 0");
186 for (i
= 0; i
< value_count
; i
++) {
187 LLVMValueRef value
= values
[i
* value_stride
];
189 value
= LLVMBuildLoad(builder
, value
, "");
192 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
193 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
194 vec
= LLVMBuildInsertElement(builder
, vec
, value
, index
, "");
200 ac_build_gather_values(struct ac_llvm_context
*ctx
,
201 LLVMValueRef
*values
,
202 unsigned value_count
)
204 return ac_build_gather_values_extended(ctx
, values
, value_count
, 1, false);
208 ac_emit_fdiv(struct ac_llvm_context
*ctx
,
212 LLVMValueRef ret
= LLVMBuildFDiv(ctx
->builder
, num
, den
, "");
214 if (!LLVMIsConstant(ret
))
215 LLVMSetMetadata(ret
, ctx
->fpmath_md_kind
, ctx
->fpmath_md_2p5_ulp
);
219 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
220 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
221 * already multiplied by two. id is the cube face number.
223 struct cube_selection_coords
{
230 build_cube_intrinsic(struct ac_llvm_context
*ctx
,
232 struct cube_selection_coords
*out
)
234 LLVMBuilderRef builder
= ctx
->builder
;
236 if (HAVE_LLVM
>= 0x0309) {
237 LLVMTypeRef f32
= ctx
->f32
;
239 out
->stc
[1] = ac_emit_llvm_intrinsic(ctx
, "llvm.amdgcn.cubetc",
240 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
241 out
->stc
[0] = ac_emit_llvm_intrinsic(ctx
, "llvm.amdgcn.cubesc",
242 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
243 out
->ma
= ac_emit_llvm_intrinsic(ctx
, "llvm.amdgcn.cubema",
244 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
245 out
->id
= ac_emit_llvm_intrinsic(ctx
, "llvm.amdgcn.cubeid",
246 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
248 LLVMValueRef c
[4] = {
252 LLVMGetUndef(LLVMTypeOf(in
[0]))
254 LLVMValueRef vec
= ac_build_gather_values(ctx
, c
, 4);
257 ac_emit_llvm_intrinsic(ctx
, "llvm.AMDGPU.cube",
258 LLVMTypeOf(vec
), &vec
, 1,
259 AC_FUNC_ATTR_READNONE
);
261 out
->stc
[1] = LLVMBuildExtractElement(builder
, tmp
,
262 LLVMConstInt(ctx
->i32
, 0, 0), "");
263 out
->stc
[0] = LLVMBuildExtractElement(builder
, tmp
,
264 LLVMConstInt(ctx
->i32
, 1, 0), "");
265 out
->ma
= LLVMBuildExtractElement(builder
, tmp
,
266 LLVMConstInt(ctx
->i32
, 2, 0), "");
267 out
->id
= LLVMBuildExtractElement(builder
, tmp
,
268 LLVMConstInt(ctx
->i32
, 3, 0), "");
273 * Build a manual selection sequence for cube face sc/tc coordinates and
274 * major axis vector (multiplied by 2 for consistency) for the given
275 * vec3 \p coords, for the face implied by \p selcoords.
277 * For the major axis, we always adjust the sign to be in the direction of
278 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
279 * the selcoords major axis.
281 static void build_cube_select(LLVMBuilderRef builder
,
282 const struct cube_selection_coords
*selcoords
,
283 const LLVMValueRef
*coords
,
284 LLVMValueRef
*out_st
,
285 LLVMValueRef
*out_ma
)
287 LLVMTypeRef f32
= LLVMTypeOf(coords
[0]);
288 LLVMValueRef is_ma_positive
;
290 LLVMValueRef is_ma_z
, is_not_ma_z
;
291 LLVMValueRef is_ma_y
;
292 LLVMValueRef is_ma_x
;
296 is_ma_positive
= LLVMBuildFCmp(builder
, LLVMRealUGE
,
297 selcoords
->ma
, LLVMConstReal(f32
, 0.0), "");
298 sgn_ma
= LLVMBuildSelect(builder
, is_ma_positive
,
299 LLVMConstReal(f32
, 1.0), LLVMConstReal(f32
, -1.0), "");
301 is_ma_z
= LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 4.0), "");
302 is_not_ma_z
= LLVMBuildNot(builder
, is_ma_z
, "");
303 is_ma_y
= LLVMBuildAnd(builder
, is_not_ma_z
,
304 LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 2.0), ""), "");
305 is_ma_x
= LLVMBuildAnd(builder
, is_not_ma_z
, LLVMBuildNot(builder
, is_ma_y
, ""), "");
308 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2], coords
[0], "");
309 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMConstReal(f32
, 1.0),
310 LLVMBuildSelect(builder
, is_ma_x
, sgn_ma
,
311 LLVMBuildFNeg(builder
, sgn_ma
, ""), ""), "");
312 out_st
[0] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
315 tmp
= LLVMBuildSelect(builder
, is_ma_y
, coords
[2], coords
[1], "");
316 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMBuildFNeg(builder
, sgn_ma
, ""),
317 LLVMConstReal(f32
, -1.0), "");
318 out_st
[1] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
321 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2],
322 LLVMBuildSelect(builder
, is_ma_y
, coords
[1], coords
[0], ""), "");
323 sgn
= LLVMBuildSelect(builder
, is_ma_positive
,
324 LLVMConstReal(f32
, 2.0), LLVMConstReal(f32
, -2.0), "");
325 *out_ma
= LLVMBuildFMul(builder
, tmp
, sgn
, "");
329 ac_prepare_cube_coords(struct ac_llvm_context
*ctx
,
330 bool is_deriv
, bool is_array
,
331 LLVMValueRef
*coords_arg
,
332 LLVMValueRef
*derivs_arg
)
335 LLVMBuilderRef builder
= ctx
->builder
;
336 struct cube_selection_coords selcoords
;
337 LLVMValueRef coords
[3];
340 build_cube_intrinsic(ctx
, coords_arg
, &selcoords
);
342 invma
= ac_emit_llvm_intrinsic(ctx
, "llvm.fabs.f32",
343 ctx
->f32
, &selcoords
.ma
, 1, AC_FUNC_ATTR_READNONE
);
344 invma
= ac_emit_fdiv(ctx
, LLVMConstReal(ctx
->f32
, 1.0), invma
);
346 for (int i
= 0; i
< 2; ++i
)
347 coords
[i
] = LLVMBuildFMul(builder
, selcoords
.stc
[i
], invma
, "");
349 coords
[2] = selcoords
.id
;
351 if (is_deriv
&& derivs_arg
) {
352 LLVMValueRef derivs
[4];
355 /* Convert cube derivatives to 2D derivatives. */
356 for (axis
= 0; axis
< 2; axis
++) {
357 LLVMValueRef deriv_st
[2];
358 LLVMValueRef deriv_ma
;
360 /* Transform the derivative alongside the texture
361 * coordinate. Mathematically, the correct formula is
362 * as follows. Assume we're projecting onto the +Z face
363 * and denote by dx/dh the derivative of the (original)
364 * X texture coordinate with respect to horizontal
365 * window coordinates. The projection onto the +Z face
370 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
371 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
373 * This motivatives the implementation below.
375 * Whether this actually gives the expected results for
376 * apps that might feed in derivatives obtained via
377 * finite differences is anyone's guess. The OpenGL spec
378 * seems awfully quiet about how textureGrad for cube
379 * maps should be handled.
381 build_cube_select(builder
, &selcoords
, &derivs_arg
[axis
* 3],
382 deriv_st
, &deriv_ma
);
384 deriv_ma
= LLVMBuildFMul(builder
, deriv_ma
, invma
, "");
386 for (int i
= 0; i
< 2; ++i
)
387 derivs
[axis
* 2 + i
] =
388 LLVMBuildFSub(builder
,
389 LLVMBuildFMul(builder
, deriv_st
[i
], invma
, ""),
390 LLVMBuildFMul(builder
, deriv_ma
, coords
[i
], ""), "");
393 memcpy(derivs_arg
, derivs
, sizeof(derivs
));
396 /* Shift the texture coordinate. This must be applied after the
397 * derivative calculation.
399 for (int i
= 0; i
< 2; ++i
)
400 coords
[i
] = LLVMBuildFAdd(builder
, coords
[i
], LLVMConstReal(ctx
->f32
, 1.5), "");
403 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
404 /* coords_arg.w component - array_index for cube arrays */
405 LLVMValueRef tmp
= LLVMBuildFMul(ctx
->builder
, coords_arg
[3], LLVMConstReal(ctx
->f32
, 8.0), "");
406 coords
[2] = LLVMBuildFAdd(ctx
->builder
, tmp
, coords
[2], "");
409 memcpy(coords_arg
, coords
, sizeof(coords
));
414 ac_build_fs_interp(struct ac_llvm_context
*ctx
,
415 LLVMValueRef llvm_chan
,
416 LLVMValueRef attr_number
,
421 LLVMValueRef args
[5];
424 if (HAVE_LLVM
< 0x0400) {
426 ij
[0] = LLVMBuildBitCast(ctx
->builder
, i
, ctx
->i32
, "");
427 ij
[1] = LLVMBuildBitCast(ctx
->builder
, j
, ctx
->i32
, "");
430 args
[1] = attr_number
;
432 args
[3] = ac_build_gather_values(ctx
, ij
, 2);
433 return ac_emit_llvm_intrinsic(ctx
, "llvm.SI.fs.interp",
435 AC_FUNC_ATTR_READNONE
);
440 args
[2] = attr_number
;
443 p1
= ac_emit_llvm_intrinsic(ctx
, "llvm.amdgcn.interp.p1",
444 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
449 args
[3] = attr_number
;
452 return ac_emit_llvm_intrinsic(ctx
, "llvm.amdgcn.interp.p2",
453 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
457 ac_build_fs_interp_mov(struct ac_llvm_context
*ctx
,
458 LLVMValueRef parameter
,
459 LLVMValueRef llvm_chan
,
460 LLVMValueRef attr_number
,
463 LLVMValueRef args
[4];
464 if (HAVE_LLVM
< 0x0400) {
466 args
[1] = attr_number
;
469 return ac_emit_llvm_intrinsic(ctx
,
470 "llvm.SI.fs.constant",
472 AC_FUNC_ATTR_READNONE
);
477 args
[2] = attr_number
;
480 return ac_emit_llvm_intrinsic(ctx
, "llvm.amdgcn.interp.mov",
481 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
485 ac_build_gep0(struct ac_llvm_context
*ctx
,
486 LLVMValueRef base_ptr
,
489 LLVMValueRef indices
[2] = {
490 LLVMConstInt(ctx
->i32
, 0, 0),
493 return LLVMBuildGEP(ctx
->builder
, base_ptr
,
498 ac_build_indexed_store(struct ac_llvm_context
*ctx
,
499 LLVMValueRef base_ptr
, LLVMValueRef index
,
502 LLVMBuildStore(ctx
->builder
, value
,
503 ac_build_gep0(ctx
, base_ptr
, index
));
507 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
508 * It's equivalent to doing a load from &base_ptr[index].
510 * \param base_ptr Where the array starts.
511 * \param index The element index into the array.
512 * \param uniform Whether the base_ptr and index can be assumed to be
513 * dynamically uniform
516 ac_build_indexed_load(struct ac_llvm_context
*ctx
,
517 LLVMValueRef base_ptr
, LLVMValueRef index
,
520 LLVMValueRef pointer
;
522 pointer
= ac_build_gep0(ctx
, base_ptr
, index
);
524 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
525 return LLVMBuildLoad(ctx
->builder
, pointer
, "");
529 * Do a load from &base_ptr[index], but also add a flag that it's loading
530 * a constant from a dynamically uniform index.
533 ac_build_indexed_load_const(struct ac_llvm_context
*ctx
,
534 LLVMValueRef base_ptr
, LLVMValueRef index
)
536 LLVMValueRef result
= ac_build_indexed_load(ctx
, base_ptr
, index
, true);
537 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
541 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
542 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
543 * or v4i32 (num_channels=3,4).
546 ac_build_buffer_store_dword(struct ac_llvm_context
*ctx
,
549 unsigned num_channels
,
550 LLVMValueRef voffset
,
551 LLVMValueRef soffset
,
552 unsigned inst_offset
,
556 static unsigned dfmt
[] = {
557 V_008F0C_BUF_DATA_FORMAT_32
,
558 V_008F0C_BUF_DATA_FORMAT_32_32
,
559 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
560 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
562 assert(num_channels
>= 1 && num_channels
<= 4);
564 LLVMValueRef args
[] = {
567 LLVMConstInt(ctx
->i32
, num_channels
, 0),
568 voffset
? voffset
: LLVMGetUndef(ctx
->i32
),
570 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
571 LLVMConstInt(ctx
->i32
, dfmt
[num_channels
- 1], 0),
572 LLVMConstInt(ctx
->i32
, V_008F0C_BUF_NUM_FORMAT_UINT
, 0),
573 LLVMConstInt(ctx
->i32
, voffset
!= NULL
, 0),
574 LLVMConstInt(ctx
->i32
, 0, 0), /* idxen */
575 LLVMConstInt(ctx
->i32
, glc
, 0),
576 LLVMConstInt(ctx
->i32
, slc
, 0),
577 LLVMConstInt(ctx
->i32
, 0, 0), /* tfe*/
580 /* The instruction offset field has 12 bits */
581 assert(voffset
|| inst_offset
< (1 << 12));
583 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
584 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
585 const char *types
[] = {"i32", "v2i32", "v4i32"};
587 snprintf(name
, sizeof(name
), "llvm.SI.tbuffer.store.%s", types
[func
]);
589 ac_emit_llvm_intrinsic(ctx
, name
, ctx
->voidt
,
590 args
, ARRAY_SIZE(args
),
591 AC_FUNC_ATTR_LEGACY
);
595 ac_build_buffer_load(struct ac_llvm_context
*ctx
,
599 LLVMValueRef voffset
,
600 LLVMValueRef soffset
,
601 unsigned inst_offset
,
604 bool readonly_memory
)
606 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
608 if (HAVE_LLVM
>= 0x309) {
609 LLVMValueRef args
[] = {
610 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
611 vindex
? vindex
: LLVMConstInt(ctx
->i32
, 0, 0),
612 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
613 LLVMConstInt(ctx
->i1
, glc
, 0),
614 LLVMConstInt(ctx
->i1
, slc
, 0)
617 LLVMTypeRef types
[] = {ctx
->f32
, LLVMVectorType(ctx
->f32
, 2),
619 const char *type_names
[] = {"f32", "v2f32", "v4f32"};
623 args
[2] = LLVMBuildAdd(ctx
->builder
, args
[2], voffset
,
628 args
[2] = LLVMBuildAdd(ctx
->builder
, args
[2], soffset
,
632 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.%s",
635 return ac_emit_llvm_intrinsic(ctx
, name
, types
[func
], args
,
637 /* READNONE means writes can't
638 * affect it, while READONLY means
639 * that writes can affect it. */
641 AC_FUNC_ATTR_READNONE
:
642 AC_FUNC_ATTR_READONLY
);
644 LLVMValueRef args
[] = {
645 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v16i8
, ""),
646 voffset
? voffset
: vindex
,
648 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
649 LLVMConstInt(ctx
->i32
, voffset
? 1 : 0, 0), // offen
650 LLVMConstInt(ctx
->i32
, vindex
? 1 : 0, 0), //idxen
651 LLVMConstInt(ctx
->i32
, glc
, 0),
652 LLVMConstInt(ctx
->i32
, slc
, 0),
653 LLVMConstInt(ctx
->i32
, 0, 0), // TFE
656 LLVMTypeRef types
[] = {ctx
->i32
, LLVMVectorType(ctx
->i32
, 2),
658 const char *type_names
[] = {"i32", "v2i32", "v4i32"};
659 const char *arg_type
= "i32";
662 if (voffset
&& vindex
) {
663 LLVMValueRef vaddr
[] = {vindex
, voffset
};
666 args
[1] = ac_build_gather_values(ctx
, vaddr
, 2);
669 snprintf(name
, sizeof(name
), "llvm.SI.buffer.load.dword.%s.%s",
670 type_names
[func
], arg_type
);
672 return ac_emit_llvm_intrinsic(ctx
, name
, types
[func
], args
,
673 ARRAY_SIZE(args
), AC_FUNC_ATTR_READONLY
);
678 * Set range metadata on an instruction. This can only be used on load and
679 * call instructions. If you know an instruction can only produce the values
680 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
681 * \p lo is the minimum value inclusive.
682 * \p hi is the maximum value exclusive.
684 static void set_range_metadata(struct ac_llvm_context
*ctx
,
685 LLVMValueRef value
, unsigned lo
, unsigned hi
)
687 LLVMValueRef range_md
, md_args
[2];
688 LLVMTypeRef type
= LLVMTypeOf(value
);
689 LLVMContextRef context
= LLVMGetTypeContext(type
);
691 md_args
[0] = LLVMConstInt(type
, lo
, false);
692 md_args
[1] = LLVMConstInt(type
, hi
, false);
693 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
694 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
698 ac_get_thread_id(struct ac_llvm_context
*ctx
)
702 if (HAVE_LLVM
< 0x0308) {
703 tid
= ac_emit_llvm_intrinsic(ctx
, "llvm.SI.tid",
705 NULL
, 0, AC_FUNC_ATTR_READNONE
);
707 LLVMValueRef tid_args
[2];
708 tid_args
[0] = LLVMConstInt(ctx
->i32
, 0xffffffff, false);
709 tid_args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
710 tid_args
[1] = ac_emit_llvm_intrinsic(ctx
,
711 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
712 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
714 tid
= ac_emit_llvm_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi",
716 2, AC_FUNC_ATTR_READNONE
);
718 set_range_metadata(ctx
, tid
, 0, 64);
723 * SI implements derivatives using the local data store (LDS)
724 * All writes to the LDS happen in all executing threads at
725 * the same time. TID is the Thread ID for the current
726 * thread and is a value between 0 and 63, representing
727 * the thread's position in the wavefront.
729 * For the pixel shader threads are grouped into quads of four pixels.
730 * The TIDs of the pixels of a quad are:
738 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
739 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
740 * the current pixel's column, and masking with 0xfffffffe yields the TID
741 * of the left pixel of the current pixel's row.
743 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
744 * adding 2 yields the TID of the pixel below the top pixel.
747 ac_emit_ddxy(struct ac_llvm_context
*ctx
,
748 bool has_ds_bpermute
,
754 LLVMValueRef thread_id
, tl
, trbl
, tl_tid
, trbl_tid
, args
[2];
757 thread_id
= ac_get_thread_id(ctx
);
759 tl_tid
= LLVMBuildAnd(ctx
->builder
, thread_id
,
760 LLVMConstInt(ctx
->i32
, mask
, false), "");
762 trbl_tid
= LLVMBuildAdd(ctx
->builder
, tl_tid
,
763 LLVMConstInt(ctx
->i32
, idx
, false), "");
765 if (has_ds_bpermute
) {
766 args
[0] = LLVMBuildMul(ctx
->builder
, tl_tid
,
767 LLVMConstInt(ctx
->i32
, 4, false), "");
769 tl
= ac_emit_llvm_intrinsic(ctx
,
770 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
771 args
, 2, AC_FUNC_ATTR_READNONE
);
773 args
[0] = LLVMBuildMul(ctx
->builder
, trbl_tid
,
774 LLVMConstInt(ctx
->i32
, 4, false), "");
775 trbl
= ac_emit_llvm_intrinsic(ctx
,
776 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
777 args
, 2, AC_FUNC_ATTR_READNONE
);
779 LLVMValueRef store_ptr
, load_ptr0
, load_ptr1
;
781 store_ptr
= ac_build_gep0(ctx
, lds
, thread_id
);
782 load_ptr0
= ac_build_gep0(ctx
, lds
, tl_tid
);
783 load_ptr1
= ac_build_gep0(ctx
, lds
, trbl_tid
);
785 LLVMBuildStore(ctx
->builder
, val
, store_ptr
);
786 tl
= LLVMBuildLoad(ctx
->builder
, load_ptr0
, "");
787 trbl
= LLVMBuildLoad(ctx
->builder
, load_ptr1
, "");
790 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, ctx
->f32
, "");
791 trbl
= LLVMBuildBitCast(ctx
->builder
, trbl
, ctx
->f32
, "");
792 result
= LLVMBuildFSub(ctx
->builder
, trbl
, tl
, "");
797 ac_emit_sendmsg(struct ac_llvm_context
*ctx
,
799 LLVMValueRef wave_id
)
801 LLVMValueRef args
[2];
802 const char *intr_name
= (HAVE_LLVM
< 0x0400) ? "llvm.SI.sendmsg" : "llvm.amdgcn.s.sendmsg";
803 args
[0] = LLVMConstInt(ctx
->i32
, msg
, false);
805 ac_emit_llvm_intrinsic(ctx
, intr_name
, ctx
->voidt
,
810 ac_emit_imsb(struct ac_llvm_context
*ctx
,
812 LLVMTypeRef dst_type
)
814 const char *intr_name
= (HAVE_LLVM
< 0x0400) ? "llvm.AMDGPU.flbit.i32" :
815 "llvm.amdgcn.sffbh.i32";
816 LLVMValueRef msb
= ac_emit_llvm_intrinsic(ctx
, intr_name
,
818 AC_FUNC_ATTR_READNONE
);
820 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
821 * the index from LSB. Invert it by doing "31 - msb". */
822 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
825 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
826 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
827 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
828 arg
, LLVMConstInt(ctx
->i32
, 0, 0), ""),
829 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
830 arg
, all_ones
, ""), "");
832 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
836 ac_emit_umsb(struct ac_llvm_context
*ctx
,
838 LLVMTypeRef dst_type
)
840 LLVMValueRef args
[2] = {
842 LLVMConstInt(ctx
->i1
, 1, 0),
844 LLVMValueRef msb
= ac_emit_llvm_intrinsic(ctx
, "llvm.ctlz.i32",
845 dst_type
, args
, ARRAY_SIZE(args
),
846 AC_FUNC_ATTR_READNONE
);
848 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
849 * the index from LSB. Invert it by doing "31 - msb". */
850 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
854 return LLVMBuildSelect(ctx
->builder
,
855 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, arg
,
856 LLVMConstInt(ctx
->i32
, 0, 0), ""),
857 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
860 LLVMValueRef
ac_emit_clamp(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
862 if (HAVE_LLVM
>= 0x0500) {
863 LLVMValueRef max
[2] = {
865 LLVMConstReal(ctx
->f32
, 0),
867 LLVMValueRef min
[2] = {
868 LLVMConstReal(ctx
->f32
, 1),
871 min
[1] = ac_emit_llvm_intrinsic(ctx
, "llvm.maxnum.f32",
873 AC_FUNC_ATTR_READNONE
);
874 return ac_emit_llvm_intrinsic(ctx
, "llvm.minnum.f32",
876 AC_FUNC_ATTR_READNONE
);
879 const char *intr
= HAVE_LLVM
>= 0x0308 ? "llvm.AMDGPU.clamp." :
881 LLVMValueRef args
[3] = {
883 LLVMConstReal(ctx
->f32
, 0),
884 LLVMConstReal(ctx
->f32
, 1),
887 return ac_emit_llvm_intrinsic(ctx
, intr
, ctx
->f32
, args
, 3,
888 AC_FUNC_ATTR_READNONE
|
889 AC_FUNC_ATTR_LEGACY
);
892 void ac_emit_export(struct ac_llvm_context
*ctx
, struct ac_export_args
*a
)
894 LLVMValueRef args
[9];
896 if (HAVE_LLVM
>= 0x0500) {
897 args
[0] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
898 args
[1] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
901 LLVMTypeRef i16
= LLVMInt16TypeInContext(ctx
->context
);
902 LLVMTypeRef v2i16
= LLVMVectorType(i16
, 2);
904 args
[2] = LLVMBuildBitCast(ctx
->builder
, a
->out
[0],
906 args
[3] = LLVMBuildBitCast(ctx
->builder
, a
->out
[1],
908 args
[4] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
909 args
[5] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
911 ac_emit_llvm_intrinsic(ctx
, "llvm.amdgcn.exp.compr.v2i16",
912 ctx
->voidt
, args
, 6, 0);
918 args
[6] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
919 args
[7] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
921 ac_emit_llvm_intrinsic(ctx
, "llvm.amdgcn.exp.f32",
922 ctx
->voidt
, args
, 8, 0);
927 args
[0] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
928 args
[1] = LLVMConstInt(ctx
->i32
, a
->valid_mask
, 0);
929 args
[2] = LLVMConstInt(ctx
->i32
, a
->done
, 0);
930 args
[3] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
931 args
[4] = LLVMConstInt(ctx
->i32
, a
->compr
, 0);
932 memcpy(args
+ 5, a
->out
, sizeof(a
->out
[0]) * 4);
934 ac_emit_llvm_intrinsic(ctx
, "llvm.SI.export", ctx
->voidt
, args
, 9,
935 AC_FUNC_ATTR_LEGACY
);
938 LLVMValueRef
ac_emit_image_opcode(struct ac_llvm_context
*ctx
,
939 struct ac_image_args
*a
)
941 LLVMTypeRef dst_type
;
942 LLVMValueRef args
[11];
943 unsigned num_args
= 0;
945 char intr_name
[128], type
[64];
947 if (HAVE_LLVM
>= 0x0400) {
948 bool sample
= a
->opcode
== ac_image_sample
||
949 a
->opcode
== ac_image_gather4
||
950 a
->opcode
== ac_image_get_lod
;
953 args
[num_args
++] = bitcast_to_float(ctx
, a
->addr
);
955 args
[num_args
++] = a
->addr
;
957 args
[num_args
++] = a
->resource
;
959 args
[num_args
++] = a
->sampler
;
960 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, 0);
962 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->unorm
, 0);
963 args
[num_args
++] = LLVMConstInt(ctx
->i1
, 0, 0); /* glc */
964 args
[num_args
++] = LLVMConstInt(ctx
->i1
, 0, 0); /* slc */
965 args
[num_args
++] = LLVMConstInt(ctx
->i1
, 0, 0); /* lwe */
966 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->da
, 0);
969 case ac_image_sample
:
970 name
= "llvm.amdgcn.image.sample";
972 case ac_image_gather4
:
973 name
= "llvm.amdgcn.image.gather4";
976 name
= "llvm.amdgcn.image.load";
978 case ac_image_load_mip
:
979 name
= "llvm.amdgcn.image.load.mip";
981 case ac_image_get_lod
:
982 name
= "llvm.amdgcn.image.getlod";
984 case ac_image_get_resinfo
:
985 name
= "llvm.amdgcn.image.getresinfo";
989 ac_build_type_name_for_intr(LLVMTypeOf(args
[0]), type
,
992 snprintf(intr_name
, sizeof(intr_name
), "%s%s%s%s.v4f32.%s.v8i32",
994 a
->compare
? ".c" : "",
998 a
->level_zero
? ".lz" : "",
999 a
->offset
? ".o" : "",
1002 LLVMValueRef result
=
1003 ac_emit_llvm_intrinsic(ctx
, intr_name
,
1004 ctx
->v4f32
, args
, num_args
,
1005 AC_FUNC_ATTR_READNONE
);
1007 result
= LLVMBuildBitCast(ctx
->builder
, result
,
1013 args
[num_args
++] = a
->addr
;
1014 args
[num_args
++] = a
->resource
;
1016 if (a
->opcode
== ac_image_load
||
1017 a
->opcode
== ac_image_load_mip
||
1018 a
->opcode
== ac_image_get_resinfo
) {
1019 dst_type
= ctx
->v4i32
;
1021 dst_type
= ctx
->v4f32
;
1022 args
[num_args
++] = a
->sampler
;
1025 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, 0);
1026 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->unorm
, 0);
1027 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* r128 */
1028 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->da
, 0);
1029 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* glc */
1030 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* slc */
1031 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* tfe */
1032 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* lwe */
1034 switch (a
->opcode
) {
1035 case ac_image_sample
:
1036 name
= "llvm.SI.image.sample";
1038 case ac_image_gather4
:
1039 name
= "llvm.SI.gather4";
1042 name
= "llvm.SI.image.load";
1044 case ac_image_load_mip
:
1045 name
= "llvm.SI.image.load.mip";
1047 case ac_image_get_lod
:
1048 name
= "llvm.SI.getlod";
1050 case ac_image_get_resinfo
:
1051 name
= "llvm.SI.getresinfo";
1055 ac_build_type_name_for_intr(LLVMTypeOf(a
->addr
), type
, sizeof(type
));
1056 snprintf(intr_name
, sizeof(intr_name
), "%s%s%s%s.%s",
1058 a
->compare
? ".c" : "",
1062 a
->level_zero
? ".lz" : "",
1063 a
->offset
? ".o" : "",
1066 return ac_emit_llvm_intrinsic(ctx
, intr_name
,
1067 dst_type
, args
, num_args
,
1068 AC_FUNC_ATTR_READNONE
|
1069 AC_FUNC_ATTR_LEGACY
);
1072 LLVMValueRef
ac_emit_cvt_pkrtz_f16(struct ac_llvm_context
*ctx
,
1073 LLVMValueRef args
[2])
1075 if (HAVE_LLVM
>= 0x0500) {
1077 LLVMVectorType(LLVMHalfTypeInContext(ctx
->context
), 2);
1079 ac_emit_llvm_intrinsic(ctx
, "llvm.amdgcn.cvt.pkrtz",
1081 AC_FUNC_ATTR_READNONE
);
1082 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1085 return ac_emit_llvm_intrinsic(ctx
, "llvm.SI.packf16", ctx
->i32
, args
, 2,
1086 AC_FUNC_ATTR_READNONE
|
1087 AC_FUNC_ATTR_LEGACY
);
1091 * KILL, AKA discard in GLSL.
1093 * \param value kill if value < 0.0 or value == NULL.
1095 void ac_emit_kill(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
1098 ac_emit_llvm_intrinsic(ctx
, "llvm.AMDGPU.kill", ctx
->voidt
,
1099 &value
, 1, AC_FUNC_ATTR_LEGACY
);
1101 ac_emit_llvm_intrinsic(ctx
, "llvm.AMDGPU.kilp", ctx
->voidt
,
1102 NULL
, 0, AC_FUNC_ATTR_LEGACY
);
1106 LLVMValueRef
ac_emit_bfe(struct ac_llvm_context
*ctx
, LLVMValueRef input
,
1107 LLVMValueRef offset
, LLVMValueRef width
,
1110 LLVMValueRef args
[] = {
1116 if (HAVE_LLVM
>= 0x0500) {
1117 return ac_emit_llvm_intrinsic(ctx
,
1118 is_signed
? "llvm.amdgcn.sbfe.i32" :
1119 "llvm.amdgcn.ubfe.i32",
1121 AC_FUNC_ATTR_READNONE
);
1124 return ac_emit_llvm_intrinsic(ctx
,
1125 is_signed
? "llvm.AMDGPU.bfe.i32" :
1126 "llvm.AMDGPU.bfe.u32",
1128 AC_FUNC_ATTR_READNONE
|
1129 AC_FUNC_ATTR_LEGACY
);