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
;
87 function
= LLVMGetNamedFunction(ctx
->module
, name
);
89 LLVMTypeRef param_types
[32], function_type
;
92 assert(param_count
<= 32);
94 for (i
= 0; i
< param_count
; ++i
) {
96 param_types
[i
] = LLVMTypeOf(params
[i
]);
99 LLVMFunctionType(return_type
, param_types
, param_count
, 0);
100 function
= LLVMAddFunction(ctx
->module
, name
, function_type
);
102 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
103 LLVMSetLinkage(function
, LLVMExternalLinkage
);
105 attrib_mask
|= AC_FUNC_ATTR_NOUNWIND
;
106 while (attrib_mask
) {
107 enum ac_func_attr attr
= 1u << u_bit_scan(&attrib_mask
);
108 ac_add_function_attr(function
, -1, attr
);
111 return LLVMBuildCall(ctx
->builder
, function
, params
, param_count
, "");
115 ac_build_gather_values_extended(struct ac_llvm_context
*ctx
,
116 LLVMValueRef
*values
,
117 unsigned value_count
,
118 unsigned value_stride
,
121 LLVMBuilderRef builder
= ctx
->builder
;
126 if (value_count
== 1) {
128 return LLVMBuildLoad(builder
, values
[0], "");
130 } else if (!value_count
)
131 unreachable("value_count is 0");
133 for (i
= 0; i
< value_count
; i
++) {
134 LLVMValueRef value
= values
[i
* value_stride
];
136 value
= LLVMBuildLoad(builder
, value
, "");
139 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
140 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
141 vec
= LLVMBuildInsertElement(builder
, vec
, value
, index
, "");
147 ac_build_gather_values(struct ac_llvm_context
*ctx
,
148 LLVMValueRef
*values
,
149 unsigned value_count
)
151 return ac_build_gather_values_extended(ctx
, values
, value_count
, 1, false);
155 ac_emit_fdiv(struct ac_llvm_context
*ctx
,
159 LLVMValueRef ret
= LLVMBuildFDiv(ctx
->builder
, num
, den
, "");
161 if (!LLVMIsConstant(ret
))
162 LLVMSetMetadata(ret
, ctx
->fpmath_md_kind
, ctx
->fpmath_md_2p5_ulp
);
166 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
167 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
168 * already multiplied by two. id is the cube face number.
170 struct cube_selection_coords
{
177 build_cube_intrinsic(struct ac_llvm_context
*ctx
,
179 struct cube_selection_coords
*out
)
181 LLVMBuilderRef builder
= ctx
->builder
;
183 if (HAVE_LLVM
>= 0x0309) {
184 LLVMTypeRef f32
= ctx
->f32
;
186 out
->stc
[1] = ac_emit_llvm_intrinsic(ctx
, "llvm.amdgcn.cubetc",
187 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
188 out
->stc
[0] = ac_emit_llvm_intrinsic(ctx
, "llvm.amdgcn.cubesc",
189 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
190 out
->ma
= ac_emit_llvm_intrinsic(ctx
, "llvm.amdgcn.cubema",
191 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
192 out
->id
= ac_emit_llvm_intrinsic(ctx
, "llvm.amdgcn.cubeid",
193 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
195 LLVMValueRef c
[4] = {
199 LLVMGetUndef(LLVMTypeOf(in
[0]))
201 LLVMValueRef vec
= ac_build_gather_values(ctx
, c
, 4);
204 ac_emit_llvm_intrinsic(ctx
, "llvm.AMDGPU.cube",
205 LLVMTypeOf(vec
), &vec
, 1,
206 AC_FUNC_ATTR_READNONE
);
208 out
->stc
[1] = LLVMBuildExtractElement(builder
, tmp
,
209 LLVMConstInt(ctx
->i32
, 0, 0), "");
210 out
->stc
[0] = LLVMBuildExtractElement(builder
, tmp
,
211 LLVMConstInt(ctx
->i32
, 1, 0), "");
212 out
->ma
= LLVMBuildExtractElement(builder
, tmp
,
213 LLVMConstInt(ctx
->i32
, 2, 0), "");
214 out
->id
= LLVMBuildExtractElement(builder
, tmp
,
215 LLVMConstInt(ctx
->i32
, 3, 0), "");
220 * Build a manual selection sequence for cube face sc/tc coordinates and
221 * major axis vector (multiplied by 2 for consistency) for the given
222 * vec3 \p coords, for the face implied by \p selcoords.
224 * For the major axis, we always adjust the sign to be in the direction of
225 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
226 * the selcoords major axis.
228 static void build_cube_select(LLVMBuilderRef builder
,
229 const struct cube_selection_coords
*selcoords
,
230 const LLVMValueRef
*coords
,
231 LLVMValueRef
*out_st
,
232 LLVMValueRef
*out_ma
)
234 LLVMTypeRef f32
= LLVMTypeOf(coords
[0]);
235 LLVMValueRef is_ma_positive
;
237 LLVMValueRef is_ma_z
, is_not_ma_z
;
238 LLVMValueRef is_ma_y
;
239 LLVMValueRef is_ma_x
;
243 is_ma_positive
= LLVMBuildFCmp(builder
, LLVMRealUGE
,
244 selcoords
->ma
, LLVMConstReal(f32
, 0.0), "");
245 sgn_ma
= LLVMBuildSelect(builder
, is_ma_positive
,
246 LLVMConstReal(f32
, 1.0), LLVMConstReal(f32
, -1.0), "");
248 is_ma_z
= LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 4.0), "");
249 is_not_ma_z
= LLVMBuildNot(builder
, is_ma_z
, "");
250 is_ma_y
= LLVMBuildAnd(builder
, is_not_ma_z
,
251 LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 2.0), ""), "");
252 is_ma_x
= LLVMBuildAnd(builder
, is_not_ma_z
, LLVMBuildNot(builder
, is_ma_y
, ""), "");
255 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2], coords
[0], "");
256 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMConstReal(f32
, 1.0),
257 LLVMBuildSelect(builder
, is_ma_x
, sgn_ma
,
258 LLVMBuildFNeg(builder
, sgn_ma
, ""), ""), "");
259 out_st
[0] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
262 tmp
= LLVMBuildSelect(builder
, is_ma_y
, coords
[2], coords
[1], "");
263 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMBuildFNeg(builder
, sgn_ma
, ""),
264 LLVMConstReal(f32
, -1.0), "");
265 out_st
[1] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
268 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2],
269 LLVMBuildSelect(builder
, is_ma_y
, coords
[1], coords
[0], ""), "");
270 sgn
= LLVMBuildSelect(builder
, is_ma_positive
,
271 LLVMConstReal(f32
, 2.0), LLVMConstReal(f32
, -2.0), "");
272 *out_ma
= LLVMBuildFMul(builder
, tmp
, sgn
, "");
276 ac_prepare_cube_coords(struct ac_llvm_context
*ctx
,
277 bool is_deriv
, bool is_array
,
278 LLVMValueRef
*coords_arg
,
279 LLVMValueRef
*derivs_arg
)
282 LLVMBuilderRef builder
= ctx
->builder
;
283 struct cube_selection_coords selcoords
;
284 LLVMValueRef coords
[3];
287 build_cube_intrinsic(ctx
, coords_arg
, &selcoords
);
289 invma
= ac_emit_llvm_intrinsic(ctx
, "llvm.fabs.f32",
290 ctx
->f32
, &selcoords
.ma
, 1, AC_FUNC_ATTR_READNONE
);
291 invma
= ac_emit_fdiv(ctx
, LLVMConstReal(ctx
->f32
, 1.0), invma
);
293 for (int i
= 0; i
< 2; ++i
)
294 coords
[i
] = LLVMBuildFMul(builder
, selcoords
.stc
[i
], invma
, "");
296 coords
[2] = selcoords
.id
;
298 if (is_deriv
&& derivs_arg
) {
299 LLVMValueRef derivs
[4];
302 /* Convert cube derivatives to 2D derivatives. */
303 for (axis
= 0; axis
< 2; axis
++) {
304 LLVMValueRef deriv_st
[2];
305 LLVMValueRef deriv_ma
;
307 /* Transform the derivative alongside the texture
308 * coordinate. Mathematically, the correct formula is
309 * as follows. Assume we're projecting onto the +Z face
310 * and denote by dx/dh the derivative of the (original)
311 * X texture coordinate with respect to horizontal
312 * window coordinates. The projection onto the +Z face
317 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
318 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
320 * This motivatives the implementation below.
322 * Whether this actually gives the expected results for
323 * apps that might feed in derivatives obtained via
324 * finite differences is anyone's guess. The OpenGL spec
325 * seems awfully quiet about how textureGrad for cube
326 * maps should be handled.
328 build_cube_select(builder
, &selcoords
, &derivs_arg
[axis
* 3],
329 deriv_st
, &deriv_ma
);
331 deriv_ma
= LLVMBuildFMul(builder
, deriv_ma
, invma
, "");
333 for (int i
= 0; i
< 2; ++i
)
334 derivs
[axis
* 2 + i
] =
335 LLVMBuildFSub(builder
,
336 LLVMBuildFMul(builder
, deriv_st
[i
], invma
, ""),
337 LLVMBuildFMul(builder
, deriv_ma
, coords
[i
], ""), "");
340 memcpy(derivs_arg
, derivs
, sizeof(derivs
));
343 /* Shift the texture coordinate. This must be applied after the
344 * derivative calculation.
346 for (int i
= 0; i
< 2; ++i
)
347 coords
[i
] = LLVMBuildFAdd(builder
, coords
[i
], LLVMConstReal(ctx
->f32
, 1.5), "");
350 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
351 /* coords_arg.w component - array_index for cube arrays */
352 LLVMValueRef tmp
= LLVMBuildFMul(ctx
->builder
, coords_arg
[3], LLVMConstReal(ctx
->f32
, 8.0), "");
353 coords
[2] = LLVMBuildFAdd(ctx
->builder
, tmp
, coords
[2], "");
356 memcpy(coords_arg
, coords
, sizeof(coords
));
361 ac_build_fs_interp(struct ac_llvm_context
*ctx
,
362 LLVMValueRef llvm_chan
,
363 LLVMValueRef attr_number
,
368 LLVMValueRef args
[5];
371 if (HAVE_LLVM
< 0x0400) {
373 ij
[0] = LLVMBuildBitCast(ctx
->builder
, i
, ctx
->i32
, "");
374 ij
[1] = LLVMBuildBitCast(ctx
->builder
, j
, ctx
->i32
, "");
377 args
[1] = attr_number
;
379 args
[3] = ac_build_gather_values(ctx
, ij
, 2);
380 return ac_emit_llvm_intrinsic(ctx
, "llvm.SI.fs.interp",
382 AC_FUNC_ATTR_READNONE
);
387 args
[2] = attr_number
;
390 p1
= ac_emit_llvm_intrinsic(ctx
, "llvm.amdgcn.interp.p1",
391 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
396 args
[3] = attr_number
;
399 return ac_emit_llvm_intrinsic(ctx
, "llvm.amdgcn.interp.p2",
400 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
404 ac_build_fs_interp_mov(struct ac_llvm_context
*ctx
,
405 LLVMValueRef parameter
,
406 LLVMValueRef llvm_chan
,
407 LLVMValueRef attr_number
,
410 LLVMValueRef args
[4];
411 if (HAVE_LLVM
< 0x0400) {
413 args
[1] = attr_number
;
416 return ac_emit_llvm_intrinsic(ctx
,
417 "llvm.SI.fs.constant",
419 AC_FUNC_ATTR_READNONE
);
424 args
[2] = attr_number
;
427 return ac_emit_llvm_intrinsic(ctx
, "llvm.amdgcn.interp.mov",
428 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
432 ac_build_gep0(struct ac_llvm_context
*ctx
,
433 LLVMValueRef base_ptr
,
436 LLVMValueRef indices
[2] = {
437 LLVMConstInt(ctx
->i32
, 0, 0),
440 return LLVMBuildGEP(ctx
->builder
, base_ptr
,
445 ac_build_indexed_store(struct ac_llvm_context
*ctx
,
446 LLVMValueRef base_ptr
, LLVMValueRef index
,
449 LLVMBuildStore(ctx
->builder
, value
,
450 ac_build_gep0(ctx
, base_ptr
, index
));
454 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
455 * It's equivalent to doing a load from &base_ptr[index].
457 * \param base_ptr Where the array starts.
458 * \param index The element index into the array.
459 * \param uniform Whether the base_ptr and index can be assumed to be
460 * dynamically uniform
463 ac_build_indexed_load(struct ac_llvm_context
*ctx
,
464 LLVMValueRef base_ptr
, LLVMValueRef index
,
467 LLVMValueRef pointer
;
469 pointer
= ac_build_gep0(ctx
, base_ptr
, index
);
471 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
472 return LLVMBuildLoad(ctx
->builder
, pointer
, "");
476 * Do a load from &base_ptr[index], but also add a flag that it's loading
477 * a constant from a dynamically uniform index.
480 ac_build_indexed_load_const(struct ac_llvm_context
*ctx
,
481 LLVMValueRef base_ptr
, LLVMValueRef index
)
483 LLVMValueRef result
= ac_build_indexed_load(ctx
, base_ptr
, index
, true);
484 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
488 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
489 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
490 * or v4i32 (num_channels=3,4).
493 ac_build_tbuffer_store(struct ac_llvm_context
*ctx
,
496 unsigned num_channels
,
498 LLVMValueRef soffset
,
499 unsigned inst_offset
,
508 LLVMValueRef args
[] = {
511 LLVMConstInt(ctx
->i32
, num_channels
, 0),
514 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
515 LLVMConstInt(ctx
->i32
, dfmt
, 0),
516 LLVMConstInt(ctx
->i32
, nfmt
, 0),
517 LLVMConstInt(ctx
->i32
, offen
, 0),
518 LLVMConstInt(ctx
->i32
, idxen
, 0),
519 LLVMConstInt(ctx
->i32
, glc
, 0),
520 LLVMConstInt(ctx
->i32
, slc
, 0),
521 LLVMConstInt(ctx
->i32
, tfe
, 0)
524 /* The instruction offset field has 12 bits */
525 assert(offen
|| inst_offset
< (1 << 12));
527 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
528 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
529 const char *types
[] = {"i32", "v2i32", "v4i32"};
531 snprintf(name
, sizeof(name
), "llvm.SI.tbuffer.store.%s", types
[func
]);
533 ac_emit_llvm_intrinsic(ctx
, name
, ctx
->voidt
,
534 args
, ARRAY_SIZE(args
), 0);
538 ac_build_tbuffer_store_dwords(struct ac_llvm_context
*ctx
,
541 unsigned num_channels
,
543 LLVMValueRef soffset
,
544 unsigned inst_offset
)
546 static unsigned dfmt
[] = {
547 V_008F0C_BUF_DATA_FORMAT_32
,
548 V_008F0C_BUF_DATA_FORMAT_32_32
,
549 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
550 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
552 assert(num_channels
>= 1 && num_channels
<= 4);
554 ac_build_tbuffer_store(ctx
, rsrc
, vdata
, num_channels
, vaddr
, soffset
,
555 inst_offset
, dfmt
[num_channels
- 1],
556 V_008F0C_BUF_NUM_FORMAT_UINT
, 1, 0, 1, 1, 0);
560 ac_build_buffer_load(struct ac_llvm_context
*ctx
,
564 LLVMValueRef voffset
,
565 LLVMValueRef soffset
,
566 unsigned inst_offset
,
570 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
572 if (HAVE_LLVM
>= 0x309) {
573 LLVMValueRef args
[] = {
574 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
575 vindex
? vindex
: LLVMConstInt(ctx
->i32
, 0, 0),
576 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
577 LLVMConstInt(ctx
->i1
, glc
, 0),
578 LLVMConstInt(ctx
->i1
, slc
, 0)
581 LLVMTypeRef types
[] = {ctx
->f32
, LLVMVectorType(ctx
->f32
, 2),
583 const char *type_names
[] = {"f32", "v2f32", "v4f32"};
587 args
[2] = LLVMBuildAdd(ctx
->builder
, args
[2], voffset
,
592 args
[2] = LLVMBuildAdd(ctx
->builder
, args
[2], soffset
,
596 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.%s",
599 return ac_emit_llvm_intrinsic(ctx
, name
, types
[func
], args
,
600 ARRAY_SIZE(args
), AC_FUNC_ATTR_READONLY
);
602 LLVMValueRef args
[] = {
603 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v16i8
, ""),
604 voffset
? voffset
: vindex
,
606 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
607 LLVMConstInt(ctx
->i32
, voffset
? 1 : 0, 0), // offen
608 LLVMConstInt(ctx
->i32
, vindex
? 1 : 0, 0), //idxen
609 LLVMConstInt(ctx
->i32
, glc
, 0),
610 LLVMConstInt(ctx
->i32
, slc
, 0),
611 LLVMConstInt(ctx
->i32
, 0, 0), // TFE
614 LLVMTypeRef types
[] = {ctx
->i32
, LLVMVectorType(ctx
->i32
, 2),
616 const char *type_names
[] = {"i32", "v2i32", "v4i32"};
617 const char *arg_type
= "i32";
620 if (voffset
&& vindex
) {
621 LLVMValueRef vaddr
[] = {vindex
, voffset
};
624 args
[1] = ac_build_gather_values(ctx
, vaddr
, 2);
627 snprintf(name
, sizeof(name
), "llvm.SI.buffer.load.dword.%s.%s",
628 type_names
[func
], arg_type
);
630 return ac_emit_llvm_intrinsic(ctx
, name
, types
[func
], args
,
631 ARRAY_SIZE(args
), AC_FUNC_ATTR_READONLY
);
636 * Set range metadata on an instruction. This can only be used on load and
637 * call instructions. If you know an instruction can only produce the values
638 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
639 * \p lo is the minimum value inclusive.
640 * \p hi is the maximum value exclusive.
642 static void set_range_metadata(struct ac_llvm_context
*ctx
,
643 LLVMValueRef value
, unsigned lo
, unsigned hi
)
645 LLVMValueRef range_md
, md_args
[2];
646 LLVMTypeRef type
= LLVMTypeOf(value
);
647 LLVMContextRef context
= LLVMGetTypeContext(type
);
649 md_args
[0] = LLVMConstInt(type
, lo
, false);
650 md_args
[1] = LLVMConstInt(type
, hi
, false);
651 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
652 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
656 ac_get_thread_id(struct ac_llvm_context
*ctx
)
660 if (HAVE_LLVM
< 0x0308) {
661 tid
= ac_emit_llvm_intrinsic(ctx
, "llvm.SI.tid",
663 NULL
, 0, AC_FUNC_ATTR_READNONE
);
665 LLVMValueRef tid_args
[2];
666 tid_args
[0] = LLVMConstInt(ctx
->i32
, 0xffffffff, false);
667 tid_args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
668 tid_args
[1] = ac_emit_llvm_intrinsic(ctx
,
669 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
670 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
672 tid
= ac_emit_llvm_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi",
674 2, AC_FUNC_ATTR_READNONE
);
676 set_range_metadata(ctx
, tid
, 0, 64);
681 * SI implements derivatives using the local data store (LDS)
682 * All writes to the LDS happen in all executing threads at
683 * the same time. TID is the Thread ID for the current
684 * thread and is a value between 0 and 63, representing
685 * the thread's position in the wavefront.
687 * For the pixel shader threads are grouped into quads of four pixels.
688 * The TIDs of the pixels of a quad are:
696 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
697 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
698 * the current pixel's column, and masking with 0xfffffffe yields the TID
699 * of the left pixel of the current pixel's row.
701 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
702 * adding 2 yields the TID of the pixel below the top pixel.
705 ac_emit_ddxy(struct ac_llvm_context
*ctx
,
706 bool has_ds_bpermute
,
712 LLVMValueRef thread_id
, tl
, trbl
, tl_tid
, trbl_tid
, args
[2];
715 thread_id
= ac_get_thread_id(ctx
);
717 tl_tid
= LLVMBuildAnd(ctx
->builder
, thread_id
,
718 LLVMConstInt(ctx
->i32
, mask
, false), "");
720 trbl_tid
= LLVMBuildAdd(ctx
->builder
, tl_tid
,
721 LLVMConstInt(ctx
->i32
, idx
, false), "");
723 if (has_ds_bpermute
) {
724 args
[0] = LLVMBuildMul(ctx
->builder
, tl_tid
,
725 LLVMConstInt(ctx
->i32
, 4, false), "");
727 tl
= ac_emit_llvm_intrinsic(ctx
,
728 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
729 args
, 2, AC_FUNC_ATTR_READNONE
);
731 args
[0] = LLVMBuildMul(ctx
->builder
, trbl_tid
,
732 LLVMConstInt(ctx
->i32
, 4, false), "");
733 trbl
= ac_emit_llvm_intrinsic(ctx
,
734 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
735 args
, 2, AC_FUNC_ATTR_READNONE
);
737 LLVMValueRef store_ptr
, load_ptr0
, load_ptr1
;
739 store_ptr
= ac_build_gep0(ctx
, lds
, thread_id
);
740 load_ptr0
= ac_build_gep0(ctx
, lds
, tl_tid
);
741 load_ptr1
= ac_build_gep0(ctx
, lds
, trbl_tid
);
743 LLVMBuildStore(ctx
->builder
, val
, store_ptr
);
744 tl
= LLVMBuildLoad(ctx
->builder
, load_ptr0
, "");
745 trbl
= LLVMBuildLoad(ctx
->builder
, load_ptr1
, "");
748 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, ctx
->f32
, "");
749 trbl
= LLVMBuildBitCast(ctx
->builder
, trbl
, ctx
->f32
, "");
750 result
= LLVMBuildFSub(ctx
->builder
, trbl
, tl
, "");
755 ac_emit_sendmsg(struct ac_llvm_context
*ctx
,
757 LLVMValueRef wave_id
)
759 LLVMValueRef args
[2];
760 const char *intr_name
= (HAVE_LLVM
< 0x0400) ? "llvm.SI.sendmsg" : "llvm.amdgcn.s.sendmsg";
761 args
[0] = LLVMConstInt(ctx
->i32
, msg
, false);
763 ac_emit_llvm_intrinsic(ctx
, intr_name
, ctx
->voidt
,
768 ac_emit_imsb(struct ac_llvm_context
*ctx
,
770 LLVMTypeRef dst_type
)
772 const char *intr_name
= (HAVE_LLVM
< 0x0400) ? "llvm.AMDGPU.flbit.i32" :
773 "llvm.amdgcn.sffbh.i32";
774 LLVMValueRef msb
= ac_emit_llvm_intrinsic(ctx
, intr_name
,
776 AC_FUNC_ATTR_READNONE
);
778 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
779 * the index from LSB. Invert it by doing "31 - msb". */
780 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
783 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
784 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
785 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
786 arg
, LLVMConstInt(ctx
->i32
, 0, 0), ""),
787 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
788 arg
, all_ones
, ""), "");
790 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
794 ac_emit_umsb(struct ac_llvm_context
*ctx
,
796 LLVMTypeRef dst_type
)
798 LLVMValueRef args
[2] = {
800 LLVMConstInt(ctx
->i1
, 1, 0),
802 LLVMValueRef msb
= ac_emit_llvm_intrinsic(ctx
, "llvm.ctlz.i32",
803 dst_type
, args
, ARRAY_SIZE(args
),
804 AC_FUNC_ATTR_READNONE
);
806 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
807 * the index from LSB. Invert it by doing "31 - msb". */
808 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
812 return LLVMBuildSelect(ctx
->builder
,
813 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, arg
,
814 LLVMConstInt(ctx
->i32
, 0, 0), ""),
815 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
818 LLVMValueRef
ac_emit_clamp(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
820 if (HAVE_LLVM
>= 0x0500) {
821 LLVMValueRef max
[2] = {
823 LLVMConstReal(ctx
->f32
, 0),
825 LLVMValueRef min
[2] = {
826 LLVMConstReal(ctx
->f32
, 1),
829 min
[1] = ac_emit_llvm_intrinsic(ctx
, "llvm.maxnum.f32",
831 AC_FUNC_ATTR_READNONE
);
832 return ac_emit_llvm_intrinsic(ctx
, "llvm.minnum.f32",
834 AC_FUNC_ATTR_READNONE
);
837 const char *intr
= HAVE_LLVM
>= 0x0308 ? "llvm.AMDGPU.clamp." :
839 LLVMValueRef args
[3] = {
841 LLVMConstReal(ctx
->f32
, 0),
842 LLVMConstReal(ctx
->f32
, 1),
845 return ac_emit_llvm_intrinsic(ctx
, intr
, ctx
->f32
, args
, 3,
846 AC_FUNC_ATTR_READNONE
);