radeon/ac: use ds_swizzle for derivs on si/cik.
[mesa.git] / src / amd / common / ac_llvm_build.c
1 /*
2 * Copyright 2014 Advanced Micro Devices, Inc.
3 *
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:
11 *
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.
19 *
20 * The above copyright notice and this permission notice (including the
21 * next paragraph) shall be included in all copies or substantial portions
22 * of the Software.
23 *
24 */
25 /* based on pieces from si_pipe.c and radeon_llvm_emit.c */
26 #include "ac_llvm_build.h"
27
28 #include <llvm-c/Core.h>
29
30 #include "c11/threads.h"
31
32 #include <assert.h>
33 #include <stdio.h>
34
35 #include "ac_llvm_util.h"
36 #include "ac_exp_param.h"
37 #include "util/bitscan.h"
38 #include "util/macros.h"
39 #include "sid.h"
40
41 #include "shader_enums.h"
42
43 /* Initialize module-independent parts of the context.
44 *
45 * The caller is responsible for initializing ctx::module and ctx::builder.
46 */
47 void
48 ac_llvm_context_init(struct ac_llvm_context *ctx, LLVMContextRef context)
49 {
50 LLVMValueRef args[1];
51
52 ctx->context = context;
53 ctx->module = NULL;
54 ctx->builder = NULL;
55
56 ctx->voidt = LLVMVoidTypeInContext(ctx->context);
57 ctx->i1 = LLVMInt1TypeInContext(ctx->context);
58 ctx->i8 = LLVMInt8TypeInContext(ctx->context);
59 ctx->i16 = LLVMIntTypeInContext(ctx->context, 16);
60 ctx->i32 = LLVMIntTypeInContext(ctx->context, 32);
61 ctx->i64 = LLVMIntTypeInContext(ctx->context, 64);
62 ctx->f16 = LLVMHalfTypeInContext(ctx->context);
63 ctx->f32 = LLVMFloatTypeInContext(ctx->context);
64 ctx->f64 = LLVMDoubleTypeInContext(ctx->context);
65 ctx->v4i32 = LLVMVectorType(ctx->i32, 4);
66 ctx->v4f32 = LLVMVectorType(ctx->f32, 4);
67 ctx->v8i32 = LLVMVectorType(ctx->i32, 8);
68
69 ctx->i32_0 = LLVMConstInt(ctx->i32, 0, false);
70 ctx->i32_1 = LLVMConstInt(ctx->i32, 1, false);
71 ctx->f32_0 = LLVMConstReal(ctx->f32, 0.0);
72 ctx->f32_1 = LLVMConstReal(ctx->f32, 1.0);
73
74 ctx->range_md_kind = LLVMGetMDKindIDInContext(ctx->context,
75 "range", 5);
76
77 ctx->invariant_load_md_kind = LLVMGetMDKindIDInContext(ctx->context,
78 "invariant.load", 14);
79
80 ctx->fpmath_md_kind = LLVMGetMDKindIDInContext(ctx->context, "fpmath", 6);
81
82 args[0] = LLVMConstReal(ctx->f32, 2.5);
83 ctx->fpmath_md_2p5_ulp = LLVMMDNodeInContext(ctx->context, args, 1);
84
85 ctx->uniform_md_kind = LLVMGetMDKindIDInContext(ctx->context,
86 "amdgpu.uniform", 14);
87
88 ctx->empty_md = LLVMMDNodeInContext(ctx->context, NULL, 0);
89 }
90
91 LLVMValueRef
92 ac_build_intrinsic(struct ac_llvm_context *ctx, const char *name,
93 LLVMTypeRef return_type, LLVMValueRef *params,
94 unsigned param_count, unsigned attrib_mask)
95 {
96 LLVMValueRef function, call;
97 bool set_callsite_attrs = HAVE_LLVM >= 0x0400 &&
98 !(attrib_mask & AC_FUNC_ATTR_LEGACY);
99
100 function = LLVMGetNamedFunction(ctx->module, name);
101 if (!function) {
102 LLVMTypeRef param_types[32], function_type;
103 unsigned i;
104
105 assert(param_count <= 32);
106
107 for (i = 0; i < param_count; ++i) {
108 assert(params[i]);
109 param_types[i] = LLVMTypeOf(params[i]);
110 }
111 function_type =
112 LLVMFunctionType(return_type, param_types, param_count, 0);
113 function = LLVMAddFunction(ctx->module, name, function_type);
114
115 LLVMSetFunctionCallConv(function, LLVMCCallConv);
116 LLVMSetLinkage(function, LLVMExternalLinkage);
117
118 if (!set_callsite_attrs)
119 ac_add_func_attributes(ctx->context, function, attrib_mask);
120 }
121
122 call = LLVMBuildCall(ctx->builder, function, params, param_count, "");
123 if (set_callsite_attrs)
124 ac_add_func_attributes(ctx->context, call, attrib_mask);
125 return call;
126 }
127
128 static LLVMValueRef bitcast_to_float(struct ac_llvm_context *ctx,
129 LLVMValueRef value)
130 {
131 LLVMTypeRef type = LLVMTypeOf(value);
132 LLVMTypeRef new_type;
133
134 if (LLVMGetTypeKind(type) == LLVMVectorTypeKind)
135 new_type = LLVMVectorType(ctx->f32, LLVMGetVectorSize(type));
136 else
137 new_type = ctx->f32;
138
139 return LLVMBuildBitCast(ctx->builder, value, new_type, "");
140 }
141
142 /**
143 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
144 * intrinsic names).
145 */
146 void ac_build_type_name_for_intr(LLVMTypeRef type, char *buf, unsigned bufsize)
147 {
148 LLVMTypeRef elem_type = type;
149
150 assert(bufsize >= 8);
151
152 if (LLVMGetTypeKind(type) == LLVMVectorTypeKind) {
153 int ret = snprintf(buf, bufsize, "v%u",
154 LLVMGetVectorSize(type));
155 if (ret < 0) {
156 char *type_name = LLVMPrintTypeToString(type);
157 fprintf(stderr, "Error building type name for: %s\n",
158 type_name);
159 return;
160 }
161 elem_type = LLVMGetElementType(type);
162 buf += ret;
163 bufsize -= ret;
164 }
165 switch (LLVMGetTypeKind(elem_type)) {
166 default: break;
167 case LLVMIntegerTypeKind:
168 snprintf(buf, bufsize, "i%d", LLVMGetIntTypeWidth(elem_type));
169 break;
170 case LLVMFloatTypeKind:
171 snprintf(buf, bufsize, "f32");
172 break;
173 case LLVMDoubleTypeKind:
174 snprintf(buf, bufsize, "f64");
175 break;
176 }
177 }
178
179 LLVMValueRef
180 ac_build_gather_values_extended(struct ac_llvm_context *ctx,
181 LLVMValueRef *values,
182 unsigned value_count,
183 unsigned value_stride,
184 bool load,
185 bool always_vector)
186 {
187 LLVMBuilderRef builder = ctx->builder;
188 LLVMValueRef vec = NULL;
189 unsigned i;
190
191 if (value_count == 1 && !always_vector) {
192 if (load)
193 return LLVMBuildLoad(builder, values[0], "");
194 return values[0];
195 } else if (!value_count)
196 unreachable("value_count is 0");
197
198 for (i = 0; i < value_count; i++) {
199 LLVMValueRef value = values[i * value_stride];
200 if (load)
201 value = LLVMBuildLoad(builder, value, "");
202
203 if (!i)
204 vec = LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value), value_count));
205 LLVMValueRef index = LLVMConstInt(ctx->i32, i, false);
206 vec = LLVMBuildInsertElement(builder, vec, value, index, "");
207 }
208 return vec;
209 }
210
211 LLVMValueRef
212 ac_build_gather_values(struct ac_llvm_context *ctx,
213 LLVMValueRef *values,
214 unsigned value_count)
215 {
216 return ac_build_gather_values_extended(ctx, values, value_count, 1, false, false);
217 }
218
219 LLVMValueRef
220 ac_build_fdiv(struct ac_llvm_context *ctx,
221 LLVMValueRef num,
222 LLVMValueRef den)
223 {
224 LLVMValueRef ret = LLVMBuildFDiv(ctx->builder, num, den, "");
225
226 if (!LLVMIsConstant(ret))
227 LLVMSetMetadata(ret, ctx->fpmath_md_kind, ctx->fpmath_md_2p5_ulp);
228 return ret;
229 }
230
231 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
232 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
233 * already multiplied by two. id is the cube face number.
234 */
235 struct cube_selection_coords {
236 LLVMValueRef stc[2];
237 LLVMValueRef ma;
238 LLVMValueRef id;
239 };
240
241 static void
242 build_cube_intrinsic(struct ac_llvm_context *ctx,
243 LLVMValueRef in[3],
244 struct cube_selection_coords *out)
245 {
246 LLVMTypeRef f32 = ctx->f32;
247
248 out->stc[1] = ac_build_intrinsic(ctx, "llvm.amdgcn.cubetc",
249 f32, in, 3, AC_FUNC_ATTR_READNONE);
250 out->stc[0] = ac_build_intrinsic(ctx, "llvm.amdgcn.cubesc",
251 f32, in, 3, AC_FUNC_ATTR_READNONE);
252 out->ma = ac_build_intrinsic(ctx, "llvm.amdgcn.cubema",
253 f32, in, 3, AC_FUNC_ATTR_READNONE);
254 out->id = ac_build_intrinsic(ctx, "llvm.amdgcn.cubeid",
255 f32, in, 3, AC_FUNC_ATTR_READNONE);
256 }
257
258 /**
259 * Build a manual selection sequence for cube face sc/tc coordinates and
260 * major axis vector (multiplied by 2 for consistency) for the given
261 * vec3 \p coords, for the face implied by \p selcoords.
262 *
263 * For the major axis, we always adjust the sign to be in the direction of
264 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
265 * the selcoords major axis.
266 */
267 static void build_cube_select(LLVMBuilderRef builder,
268 const struct cube_selection_coords *selcoords,
269 const LLVMValueRef *coords,
270 LLVMValueRef *out_st,
271 LLVMValueRef *out_ma)
272 {
273 LLVMTypeRef f32 = LLVMTypeOf(coords[0]);
274 LLVMValueRef is_ma_positive;
275 LLVMValueRef sgn_ma;
276 LLVMValueRef is_ma_z, is_not_ma_z;
277 LLVMValueRef is_ma_y;
278 LLVMValueRef is_ma_x;
279 LLVMValueRef sgn;
280 LLVMValueRef tmp;
281
282 is_ma_positive = LLVMBuildFCmp(builder, LLVMRealUGE,
283 selcoords->ma, LLVMConstReal(f32, 0.0), "");
284 sgn_ma = LLVMBuildSelect(builder, is_ma_positive,
285 LLVMConstReal(f32, 1.0), LLVMConstReal(f32, -1.0), "");
286
287 is_ma_z = LLVMBuildFCmp(builder, LLVMRealUGE, selcoords->id, LLVMConstReal(f32, 4.0), "");
288 is_not_ma_z = LLVMBuildNot(builder, is_ma_z, "");
289 is_ma_y = LLVMBuildAnd(builder, is_not_ma_z,
290 LLVMBuildFCmp(builder, LLVMRealUGE, selcoords->id, LLVMConstReal(f32, 2.0), ""), "");
291 is_ma_x = LLVMBuildAnd(builder, is_not_ma_z, LLVMBuildNot(builder, is_ma_y, ""), "");
292
293 /* Select sc */
294 tmp = LLVMBuildSelect(builder, is_ma_z, coords[2], coords[0], "");
295 sgn = LLVMBuildSelect(builder, is_ma_y, LLVMConstReal(f32, 1.0),
296 LLVMBuildSelect(builder, is_ma_x, sgn_ma,
297 LLVMBuildFNeg(builder, sgn_ma, ""), ""), "");
298 out_st[0] = LLVMBuildFMul(builder, tmp, sgn, "");
299
300 /* Select tc */
301 tmp = LLVMBuildSelect(builder, is_ma_y, coords[2], coords[1], "");
302 sgn = LLVMBuildSelect(builder, is_ma_y, LLVMBuildFNeg(builder, sgn_ma, ""),
303 LLVMConstReal(f32, -1.0), "");
304 out_st[1] = LLVMBuildFMul(builder, tmp, sgn, "");
305
306 /* Select ma */
307 tmp = LLVMBuildSelect(builder, is_ma_z, coords[2],
308 LLVMBuildSelect(builder, is_ma_y, coords[1], coords[0], ""), "");
309 sgn = LLVMBuildSelect(builder, is_ma_positive,
310 LLVMConstReal(f32, 2.0), LLVMConstReal(f32, -2.0), "");
311 *out_ma = LLVMBuildFMul(builder, tmp, sgn, "");
312 }
313
314 void
315 ac_prepare_cube_coords(struct ac_llvm_context *ctx,
316 bool is_deriv, bool is_array,
317 LLVMValueRef *coords_arg,
318 LLVMValueRef *derivs_arg)
319 {
320
321 LLVMBuilderRef builder = ctx->builder;
322 struct cube_selection_coords selcoords;
323 LLVMValueRef coords[3];
324 LLVMValueRef invma;
325
326 build_cube_intrinsic(ctx, coords_arg, &selcoords);
327
328 invma = ac_build_intrinsic(ctx, "llvm.fabs.f32",
329 ctx->f32, &selcoords.ma, 1, AC_FUNC_ATTR_READNONE);
330 invma = ac_build_fdiv(ctx, LLVMConstReal(ctx->f32, 1.0), invma);
331
332 for (int i = 0; i < 2; ++i)
333 coords[i] = LLVMBuildFMul(builder, selcoords.stc[i], invma, "");
334
335 coords[2] = selcoords.id;
336
337 if (is_deriv && derivs_arg) {
338 LLVMValueRef derivs[4];
339 int axis;
340
341 /* Convert cube derivatives to 2D derivatives. */
342 for (axis = 0; axis < 2; axis++) {
343 LLVMValueRef deriv_st[2];
344 LLVMValueRef deriv_ma;
345
346 /* Transform the derivative alongside the texture
347 * coordinate. Mathematically, the correct formula is
348 * as follows. Assume we're projecting onto the +Z face
349 * and denote by dx/dh the derivative of the (original)
350 * X texture coordinate with respect to horizontal
351 * window coordinates. The projection onto the +Z face
352 * plane is:
353 *
354 * f(x,z) = x/z
355 *
356 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
357 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
358 *
359 * This motivatives the implementation below.
360 *
361 * Whether this actually gives the expected results for
362 * apps that might feed in derivatives obtained via
363 * finite differences is anyone's guess. The OpenGL spec
364 * seems awfully quiet about how textureGrad for cube
365 * maps should be handled.
366 */
367 build_cube_select(builder, &selcoords, &derivs_arg[axis * 3],
368 deriv_st, &deriv_ma);
369
370 deriv_ma = LLVMBuildFMul(builder, deriv_ma, invma, "");
371
372 for (int i = 0; i < 2; ++i)
373 derivs[axis * 2 + i] =
374 LLVMBuildFSub(builder,
375 LLVMBuildFMul(builder, deriv_st[i], invma, ""),
376 LLVMBuildFMul(builder, deriv_ma, coords[i], ""), "");
377 }
378
379 memcpy(derivs_arg, derivs, sizeof(derivs));
380 }
381
382 /* Shift the texture coordinate. This must be applied after the
383 * derivative calculation.
384 */
385 for (int i = 0; i < 2; ++i)
386 coords[i] = LLVMBuildFAdd(builder, coords[i], LLVMConstReal(ctx->f32, 1.5), "");
387
388 if (is_array) {
389 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
390 /* coords_arg.w component - array_index for cube arrays */
391 LLVMValueRef tmp = LLVMBuildFMul(ctx->builder, coords_arg[3], LLVMConstReal(ctx->f32, 8.0), "");
392 coords[2] = LLVMBuildFAdd(ctx->builder, tmp, coords[2], "");
393 }
394
395 memcpy(coords_arg, coords, sizeof(coords));
396 }
397
398
399 LLVMValueRef
400 ac_build_fs_interp(struct ac_llvm_context *ctx,
401 LLVMValueRef llvm_chan,
402 LLVMValueRef attr_number,
403 LLVMValueRef params,
404 LLVMValueRef i,
405 LLVMValueRef j)
406 {
407 LLVMValueRef args[5];
408 LLVMValueRef p1;
409
410 if (HAVE_LLVM < 0x0400) {
411 LLVMValueRef ij[2];
412 ij[0] = LLVMBuildBitCast(ctx->builder, i, ctx->i32, "");
413 ij[1] = LLVMBuildBitCast(ctx->builder, j, ctx->i32, "");
414
415 args[0] = llvm_chan;
416 args[1] = attr_number;
417 args[2] = params;
418 args[3] = ac_build_gather_values(ctx, ij, 2);
419 return ac_build_intrinsic(ctx, "llvm.SI.fs.interp",
420 ctx->f32, args, 4,
421 AC_FUNC_ATTR_READNONE);
422 }
423
424 args[0] = i;
425 args[1] = llvm_chan;
426 args[2] = attr_number;
427 args[3] = params;
428
429 p1 = ac_build_intrinsic(ctx, "llvm.amdgcn.interp.p1",
430 ctx->f32, args, 4, AC_FUNC_ATTR_READNONE);
431
432 args[0] = p1;
433 args[1] = j;
434 args[2] = llvm_chan;
435 args[3] = attr_number;
436 args[4] = params;
437
438 return ac_build_intrinsic(ctx, "llvm.amdgcn.interp.p2",
439 ctx->f32, args, 5, AC_FUNC_ATTR_READNONE);
440 }
441
442 LLVMValueRef
443 ac_build_fs_interp_mov(struct ac_llvm_context *ctx,
444 LLVMValueRef parameter,
445 LLVMValueRef llvm_chan,
446 LLVMValueRef attr_number,
447 LLVMValueRef params)
448 {
449 LLVMValueRef args[4];
450 if (HAVE_LLVM < 0x0400) {
451 args[0] = llvm_chan;
452 args[1] = attr_number;
453 args[2] = params;
454
455 return ac_build_intrinsic(ctx,
456 "llvm.SI.fs.constant",
457 ctx->f32, args, 3,
458 AC_FUNC_ATTR_READNONE);
459 }
460
461 args[0] = parameter;
462 args[1] = llvm_chan;
463 args[2] = attr_number;
464 args[3] = params;
465
466 return ac_build_intrinsic(ctx, "llvm.amdgcn.interp.mov",
467 ctx->f32, args, 4, AC_FUNC_ATTR_READNONE);
468 }
469
470 LLVMValueRef
471 ac_build_gep0(struct ac_llvm_context *ctx,
472 LLVMValueRef base_ptr,
473 LLVMValueRef index)
474 {
475 LLVMValueRef indices[2] = {
476 LLVMConstInt(ctx->i32, 0, 0),
477 index,
478 };
479 return LLVMBuildGEP(ctx->builder, base_ptr,
480 indices, 2, "");
481 }
482
483 void
484 ac_build_indexed_store(struct ac_llvm_context *ctx,
485 LLVMValueRef base_ptr, LLVMValueRef index,
486 LLVMValueRef value)
487 {
488 LLVMBuildStore(ctx->builder, value,
489 ac_build_gep0(ctx, base_ptr, index));
490 }
491
492 /**
493 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
494 * It's equivalent to doing a load from &base_ptr[index].
495 *
496 * \param base_ptr Where the array starts.
497 * \param index The element index into the array.
498 * \param uniform Whether the base_ptr and index can be assumed to be
499 * dynamically uniform
500 */
501 LLVMValueRef
502 ac_build_indexed_load(struct ac_llvm_context *ctx,
503 LLVMValueRef base_ptr, LLVMValueRef index,
504 bool uniform)
505 {
506 LLVMValueRef pointer;
507
508 pointer = ac_build_gep0(ctx, base_ptr, index);
509 if (uniform)
510 LLVMSetMetadata(pointer, ctx->uniform_md_kind, ctx->empty_md);
511 return LLVMBuildLoad(ctx->builder, pointer, "");
512 }
513
514 /**
515 * Do a load from &base_ptr[index], but also add a flag that it's loading
516 * a constant from a dynamically uniform index.
517 */
518 LLVMValueRef
519 ac_build_indexed_load_const(struct ac_llvm_context *ctx,
520 LLVMValueRef base_ptr, LLVMValueRef index)
521 {
522 LLVMValueRef result = ac_build_indexed_load(ctx, base_ptr, index, true);
523 LLVMSetMetadata(result, ctx->invariant_load_md_kind, ctx->empty_md);
524 return result;
525 }
526
527 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
528 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
529 * or v4i32 (num_channels=3,4).
530 */
531 void
532 ac_build_buffer_store_dword(struct ac_llvm_context *ctx,
533 LLVMValueRef rsrc,
534 LLVMValueRef vdata,
535 unsigned num_channels,
536 LLVMValueRef voffset,
537 LLVMValueRef soffset,
538 unsigned inst_offset,
539 bool glc,
540 bool slc,
541 bool writeonly_memory,
542 bool has_add_tid)
543 {
544 /* TODO: Fix stores with ADD_TID and remove the "has_add_tid" flag. */
545 if (!has_add_tid) {
546 /* Split 3 channel stores, becase LLVM doesn't support 3-channel
547 * intrinsics. */
548 if (num_channels == 3) {
549 LLVMValueRef v[3], v01;
550
551 for (int i = 0; i < 3; i++) {
552 v[i] = LLVMBuildExtractElement(ctx->builder, vdata,
553 LLVMConstInt(ctx->i32, i, 0), "");
554 }
555 v01 = ac_build_gather_values(ctx, v, 2);
556
557 ac_build_buffer_store_dword(ctx, rsrc, v01, 2, voffset,
558 soffset, inst_offset, glc, slc,
559 writeonly_memory, has_add_tid);
560 ac_build_buffer_store_dword(ctx, rsrc, v[2], 1, voffset,
561 soffset, inst_offset + 8,
562 glc, slc,
563 writeonly_memory, has_add_tid);
564 return;
565 }
566
567 unsigned func = CLAMP(num_channels, 1, 3) - 1;
568 static const char *types[] = {"f32", "v2f32", "v4f32"};
569 char name[256];
570 LLVMValueRef offset = soffset;
571
572 if (inst_offset)
573 offset = LLVMBuildAdd(ctx->builder, offset,
574 LLVMConstInt(ctx->i32, inst_offset, 0), "");
575 if (voffset)
576 offset = LLVMBuildAdd(ctx->builder, offset, voffset, "");
577
578 LLVMValueRef args[] = {
579 bitcast_to_float(ctx, vdata),
580 LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, ""),
581 LLVMConstInt(ctx->i32, 0, 0),
582 offset,
583 LLVMConstInt(ctx->i1, glc, 0),
584 LLVMConstInt(ctx->i1, slc, 0),
585 };
586
587 snprintf(name, sizeof(name), "llvm.amdgcn.buffer.store.%s",
588 types[func]);
589
590 ac_build_intrinsic(ctx, name, ctx->voidt,
591 args, ARRAY_SIZE(args),
592 writeonly_memory ?
593 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY :
594 AC_FUNC_ATTR_WRITEONLY);
595 return;
596 }
597
598 static unsigned dfmt[] = {
599 V_008F0C_BUF_DATA_FORMAT_32,
600 V_008F0C_BUF_DATA_FORMAT_32_32,
601 V_008F0C_BUF_DATA_FORMAT_32_32_32,
602 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
603 };
604 assert(num_channels >= 1 && num_channels <= 4);
605
606 LLVMValueRef args[] = {
607 rsrc,
608 vdata,
609 LLVMConstInt(ctx->i32, num_channels, 0),
610 voffset ? voffset : LLVMGetUndef(ctx->i32),
611 soffset,
612 LLVMConstInt(ctx->i32, inst_offset, 0),
613 LLVMConstInt(ctx->i32, dfmt[num_channels - 1], 0),
614 LLVMConstInt(ctx->i32, V_008F0C_BUF_NUM_FORMAT_UINT, 0),
615 LLVMConstInt(ctx->i32, voffset != NULL, 0),
616 LLVMConstInt(ctx->i32, 0, 0), /* idxen */
617 LLVMConstInt(ctx->i32, glc, 0),
618 LLVMConstInt(ctx->i32, slc, 0),
619 LLVMConstInt(ctx->i32, 0, 0), /* tfe*/
620 };
621
622 /* The instruction offset field has 12 bits */
623 assert(voffset || inst_offset < (1 << 12));
624
625 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
626 unsigned func = CLAMP(num_channels, 1, 3) - 1;
627 const char *types[] = {"i32", "v2i32", "v4i32"};
628 char name[256];
629 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
630
631 ac_build_intrinsic(ctx, name, ctx->voidt,
632 args, ARRAY_SIZE(args),
633 AC_FUNC_ATTR_LEGACY);
634 }
635
636 LLVMValueRef
637 ac_build_buffer_load(struct ac_llvm_context *ctx,
638 LLVMValueRef rsrc,
639 int num_channels,
640 LLVMValueRef vindex,
641 LLVMValueRef voffset,
642 LLVMValueRef soffset,
643 unsigned inst_offset,
644 unsigned glc,
645 unsigned slc,
646 bool can_speculate,
647 bool allow_smem)
648 {
649 LLVMValueRef offset = LLVMConstInt(ctx->i32, inst_offset, 0);
650 if (voffset)
651 offset = LLVMBuildAdd(ctx->builder, offset, voffset, "");
652 if (soffset)
653 offset = LLVMBuildAdd(ctx->builder, offset, soffset, "");
654
655 /* TODO: VI and later generations can use SMEM with GLC=1.*/
656 if (allow_smem && !glc && !slc) {
657 assert(vindex == NULL);
658
659 LLVMValueRef result[4];
660
661 for (int i = 0; i < num_channels; i++) {
662 if (i) {
663 offset = LLVMBuildAdd(ctx->builder, offset,
664 LLVMConstInt(ctx->i32, 4, 0), "");
665 }
666 LLVMValueRef args[2] = {rsrc, offset};
667 result[i] = ac_build_intrinsic(ctx, "llvm.SI.load.const.v4i32",
668 ctx->f32, args, 2,
669 AC_FUNC_ATTR_READNONE |
670 AC_FUNC_ATTR_LEGACY);
671 }
672 if (num_channels == 1)
673 return result[0];
674
675 if (num_channels == 3)
676 result[num_channels++] = LLVMGetUndef(ctx->f32);
677 return ac_build_gather_values(ctx, result, num_channels);
678 }
679
680 unsigned func = CLAMP(num_channels, 1, 3) - 1;
681
682 LLVMValueRef args[] = {
683 LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, ""),
684 vindex ? vindex : LLVMConstInt(ctx->i32, 0, 0),
685 offset,
686 LLVMConstInt(ctx->i1, glc, 0),
687 LLVMConstInt(ctx->i1, slc, 0)
688 };
689
690 LLVMTypeRef types[] = {ctx->f32, LLVMVectorType(ctx->f32, 2),
691 ctx->v4f32};
692 const char *type_names[] = {"f32", "v2f32", "v4f32"};
693 char name[256];
694
695 snprintf(name, sizeof(name), "llvm.amdgcn.buffer.load.%s",
696 type_names[func]);
697
698 return ac_build_intrinsic(ctx, name, types[func], args,
699 ARRAY_SIZE(args),
700 /* READNONE means writes can't affect it, while
701 * READONLY means that writes can affect it. */
702 can_speculate && HAVE_LLVM >= 0x0400 ?
703 AC_FUNC_ATTR_READNONE :
704 AC_FUNC_ATTR_READONLY);
705 }
706
707 LLVMValueRef ac_build_buffer_load_format(struct ac_llvm_context *ctx,
708 LLVMValueRef rsrc,
709 LLVMValueRef vindex,
710 LLVMValueRef voffset,
711 bool can_speculate)
712 {
713 LLVMValueRef args [] = {
714 LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, ""),
715 vindex,
716 voffset,
717 LLVMConstInt(ctx->i1, 0, 0), /* glc */
718 LLVMConstInt(ctx->i1, 0, 0), /* slc */
719 };
720
721 return ac_build_intrinsic(ctx,
722 "llvm.amdgcn.buffer.load.format.v4f32",
723 ctx->v4f32, args, ARRAY_SIZE(args),
724 /* READNONE means writes can't affect it, while
725 * READONLY means that writes can affect it. */
726 can_speculate && HAVE_LLVM >= 0x0400 ?
727 AC_FUNC_ATTR_READNONE :
728 AC_FUNC_ATTR_READONLY);
729 }
730
731 /**
732 * Set range metadata on an instruction. This can only be used on load and
733 * call instructions. If you know an instruction can only produce the values
734 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
735 * \p lo is the minimum value inclusive.
736 * \p hi is the maximum value exclusive.
737 */
738 static void set_range_metadata(struct ac_llvm_context *ctx,
739 LLVMValueRef value, unsigned lo, unsigned hi)
740 {
741 LLVMValueRef range_md, md_args[2];
742 LLVMTypeRef type = LLVMTypeOf(value);
743 LLVMContextRef context = LLVMGetTypeContext(type);
744
745 md_args[0] = LLVMConstInt(type, lo, false);
746 md_args[1] = LLVMConstInt(type, hi, false);
747 range_md = LLVMMDNodeInContext(context, md_args, 2);
748 LLVMSetMetadata(value, ctx->range_md_kind, range_md);
749 }
750
751 LLVMValueRef
752 ac_get_thread_id(struct ac_llvm_context *ctx)
753 {
754 LLVMValueRef tid;
755
756 LLVMValueRef tid_args[2];
757 tid_args[0] = LLVMConstInt(ctx->i32, 0xffffffff, false);
758 tid_args[1] = LLVMConstInt(ctx->i32, 0, false);
759 tid_args[1] = ac_build_intrinsic(ctx,
760 "llvm.amdgcn.mbcnt.lo", ctx->i32,
761 tid_args, 2, AC_FUNC_ATTR_READNONE);
762
763 tid = ac_build_intrinsic(ctx, "llvm.amdgcn.mbcnt.hi",
764 ctx->i32, tid_args,
765 2, AC_FUNC_ATTR_READNONE);
766 set_range_metadata(ctx, tid, 0, 64);
767 return tid;
768 }
769
770 /*
771 * SI implements derivatives using the local data store (LDS)
772 * All writes to the LDS happen in all executing threads at
773 * the same time. TID is the Thread ID for the current
774 * thread and is a value between 0 and 63, representing
775 * the thread's position in the wavefront.
776 *
777 * For the pixel shader threads are grouped into quads of four pixels.
778 * The TIDs of the pixels of a quad are:
779 *
780 * +------+------+
781 * |4n + 0|4n + 1|
782 * +------+------+
783 * |4n + 2|4n + 3|
784 * +------+------+
785 *
786 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
787 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
788 * the current pixel's column, and masking with 0xfffffffe yields the TID
789 * of the left pixel of the current pixel's row.
790 *
791 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
792 * adding 2 yields the TID of the pixel below the top pixel.
793 */
794 LLVMValueRef
795 ac_build_ddxy(struct ac_llvm_context *ctx,
796 bool has_ds_bpermute,
797 uint32_t mask,
798 int idx,
799 LLVMValueRef val)
800 {
801 LLVMValueRef tl, trbl, args[2];
802 LLVMValueRef result;
803
804 if (has_ds_bpermute) {
805 LLVMValueRef thread_id, tl_tid, trbl_tid;
806 thread_id = ac_get_thread_id(ctx);
807
808 tl_tid = LLVMBuildAnd(ctx->builder, thread_id,
809 LLVMConstInt(ctx->i32, mask, false), "");
810
811 trbl_tid = LLVMBuildAdd(ctx->builder, tl_tid,
812 LLVMConstInt(ctx->i32, idx, false), "");
813
814 args[0] = LLVMBuildMul(ctx->builder, tl_tid,
815 LLVMConstInt(ctx->i32, 4, false), "");
816 args[1] = val;
817 tl = ac_build_intrinsic(ctx,
818 "llvm.amdgcn.ds.bpermute", ctx->i32,
819 args, 2,
820 AC_FUNC_ATTR_READNONE |
821 AC_FUNC_ATTR_CONVERGENT);
822
823 args[0] = LLVMBuildMul(ctx->builder, trbl_tid,
824 LLVMConstInt(ctx->i32, 4, false), "");
825 trbl = ac_build_intrinsic(ctx,
826 "llvm.amdgcn.ds.bpermute", ctx->i32,
827 args, 2,
828 AC_FUNC_ATTR_READNONE |
829 AC_FUNC_ATTR_CONVERGENT);
830 } else {
831 uint32_t masks[2];
832
833 switch (mask) {
834 case AC_TID_MASK_TOP_LEFT:
835 masks[0] = 0x8000;
836 if (idx == 1)
837 masks[1] = 0x8055;
838 else
839 masks[1] = 0x80aa;
840
841 break;
842 case AC_TID_MASK_TOP:
843 masks[0] = 0x8044;
844 masks[1] = 0x80ee;
845 break;
846 case AC_TID_MASK_LEFT:
847 masks[0] = 0x80a0;
848 masks[1] = 0x80f5;
849 break;
850 }
851
852 args[0] = val;
853 args[1] = LLVMConstInt(ctx->i32, masks[0], false);
854
855 tl = ac_build_intrinsic(ctx,
856 "llvm.amdgcn.ds.swizzle", ctx->i32,
857 args, 2,
858 AC_FUNC_ATTR_READNONE |
859 AC_FUNC_ATTR_CONVERGENT);
860
861 args[1] = LLVMConstInt(ctx->i32, masks[1], false);
862 trbl = ac_build_intrinsic(ctx,
863 "llvm.amdgcn.ds.swizzle", ctx->i32,
864 args, 2,
865 AC_FUNC_ATTR_READNONE |
866 AC_FUNC_ATTR_CONVERGENT);
867 }
868
869 tl = LLVMBuildBitCast(ctx->builder, tl, ctx->f32, "");
870 trbl = LLVMBuildBitCast(ctx->builder, trbl, ctx->f32, "");
871 result = LLVMBuildFSub(ctx->builder, trbl, tl, "");
872 return result;
873 }
874
875 void
876 ac_build_sendmsg(struct ac_llvm_context *ctx,
877 uint32_t msg,
878 LLVMValueRef wave_id)
879 {
880 LLVMValueRef args[2];
881 const char *intr_name = (HAVE_LLVM < 0x0400) ? "llvm.SI.sendmsg" : "llvm.amdgcn.s.sendmsg";
882 args[0] = LLVMConstInt(ctx->i32, msg, false);
883 args[1] = wave_id;
884 ac_build_intrinsic(ctx, intr_name, ctx->voidt, args, 2, 0);
885 }
886
887 LLVMValueRef
888 ac_build_imsb(struct ac_llvm_context *ctx,
889 LLVMValueRef arg,
890 LLVMTypeRef dst_type)
891 {
892 const char *intr_name = (HAVE_LLVM < 0x0400) ? "llvm.AMDGPU.flbit.i32" :
893 "llvm.amdgcn.sffbh.i32";
894 LLVMValueRef msb = ac_build_intrinsic(ctx, intr_name,
895 dst_type, &arg, 1,
896 AC_FUNC_ATTR_READNONE);
897
898 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
899 * the index from LSB. Invert it by doing "31 - msb". */
900 msb = LLVMBuildSub(ctx->builder, LLVMConstInt(ctx->i32, 31, false),
901 msb, "");
902
903 LLVMValueRef all_ones = LLVMConstInt(ctx->i32, -1, true);
904 LLVMValueRef cond = LLVMBuildOr(ctx->builder,
905 LLVMBuildICmp(ctx->builder, LLVMIntEQ,
906 arg, LLVMConstInt(ctx->i32, 0, 0), ""),
907 LLVMBuildICmp(ctx->builder, LLVMIntEQ,
908 arg, all_ones, ""), "");
909
910 return LLVMBuildSelect(ctx->builder, cond, all_ones, msb, "");
911 }
912
913 LLVMValueRef
914 ac_build_umsb(struct ac_llvm_context *ctx,
915 LLVMValueRef arg,
916 LLVMTypeRef dst_type)
917 {
918 LLVMValueRef args[2] = {
919 arg,
920 LLVMConstInt(ctx->i1, 1, 0),
921 };
922 LLVMValueRef msb = ac_build_intrinsic(ctx, "llvm.ctlz.i32",
923 dst_type, args, ARRAY_SIZE(args),
924 AC_FUNC_ATTR_READNONE);
925
926 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
927 * the index from LSB. Invert it by doing "31 - msb". */
928 msb = LLVMBuildSub(ctx->builder, LLVMConstInt(ctx->i32, 31, false),
929 msb, "");
930
931 /* check for zero */
932 return LLVMBuildSelect(ctx->builder,
933 LLVMBuildICmp(ctx->builder, LLVMIntEQ, arg,
934 LLVMConstInt(ctx->i32, 0, 0), ""),
935 LLVMConstInt(ctx->i32, -1, true), msb, "");
936 }
937
938 LLVMValueRef ac_build_umin(struct ac_llvm_context *ctx, LLVMValueRef a,
939 LLVMValueRef b)
940 {
941 LLVMValueRef cmp = LLVMBuildICmp(ctx->builder, LLVMIntULE, a, b, "");
942 return LLVMBuildSelect(ctx->builder, cmp, a, b, "");
943 }
944
945 LLVMValueRef ac_build_clamp(struct ac_llvm_context *ctx, LLVMValueRef value)
946 {
947 if (HAVE_LLVM >= 0x0500) {
948 LLVMValueRef max[2] = {
949 value,
950 LLVMConstReal(ctx->f32, 0),
951 };
952 LLVMValueRef min[2] = {
953 LLVMConstReal(ctx->f32, 1),
954 };
955
956 min[1] = ac_build_intrinsic(ctx, "llvm.maxnum.f32",
957 ctx->f32, max, 2,
958 AC_FUNC_ATTR_READNONE);
959 return ac_build_intrinsic(ctx, "llvm.minnum.f32",
960 ctx->f32, min, 2,
961 AC_FUNC_ATTR_READNONE);
962 }
963
964 LLVMValueRef args[3] = {
965 value,
966 LLVMConstReal(ctx->f32, 0),
967 LLVMConstReal(ctx->f32, 1),
968 };
969
970 return ac_build_intrinsic(ctx, "llvm.AMDGPU.clamp.", ctx->f32, args, 3,
971 AC_FUNC_ATTR_READNONE |
972 AC_FUNC_ATTR_LEGACY);
973 }
974
975 void ac_build_export(struct ac_llvm_context *ctx, struct ac_export_args *a)
976 {
977 LLVMValueRef args[9];
978
979 if (HAVE_LLVM >= 0x0500) {
980 args[0] = LLVMConstInt(ctx->i32, a->target, 0);
981 args[1] = LLVMConstInt(ctx->i32, a->enabled_channels, 0);
982
983 if (a->compr) {
984 LLVMTypeRef i16 = LLVMInt16TypeInContext(ctx->context);
985 LLVMTypeRef v2i16 = LLVMVectorType(i16, 2);
986
987 args[2] = LLVMBuildBitCast(ctx->builder, a->out[0],
988 v2i16, "");
989 args[3] = LLVMBuildBitCast(ctx->builder, a->out[1],
990 v2i16, "");
991 args[4] = LLVMConstInt(ctx->i1, a->done, 0);
992 args[5] = LLVMConstInt(ctx->i1, a->valid_mask, 0);
993
994 ac_build_intrinsic(ctx, "llvm.amdgcn.exp.compr.v2i16",
995 ctx->voidt, args, 6, 0);
996 } else {
997 args[2] = a->out[0];
998 args[3] = a->out[1];
999 args[4] = a->out[2];
1000 args[5] = a->out[3];
1001 args[6] = LLVMConstInt(ctx->i1, a->done, 0);
1002 args[7] = LLVMConstInt(ctx->i1, a->valid_mask, 0);
1003
1004 ac_build_intrinsic(ctx, "llvm.amdgcn.exp.f32",
1005 ctx->voidt, args, 8, 0);
1006 }
1007 return;
1008 }
1009
1010 args[0] = LLVMConstInt(ctx->i32, a->enabled_channels, 0);
1011 args[1] = LLVMConstInt(ctx->i32, a->valid_mask, 0);
1012 args[2] = LLVMConstInt(ctx->i32, a->done, 0);
1013 args[3] = LLVMConstInt(ctx->i32, a->target, 0);
1014 args[4] = LLVMConstInt(ctx->i32, a->compr, 0);
1015 memcpy(args + 5, a->out, sizeof(a->out[0]) * 4);
1016
1017 ac_build_intrinsic(ctx, "llvm.SI.export", ctx->voidt, args, 9,
1018 AC_FUNC_ATTR_LEGACY);
1019 }
1020
1021 LLVMValueRef ac_build_image_opcode(struct ac_llvm_context *ctx,
1022 struct ac_image_args *a)
1023 {
1024 LLVMTypeRef dst_type;
1025 LLVMValueRef args[11];
1026 unsigned num_args = 0;
1027 const char *name;
1028 char intr_name[128], type[64];
1029
1030 if (HAVE_LLVM >= 0x0400) {
1031 bool sample = a->opcode == ac_image_sample ||
1032 a->opcode == ac_image_gather4 ||
1033 a->opcode == ac_image_get_lod;
1034
1035 if (sample)
1036 args[num_args++] = bitcast_to_float(ctx, a->addr);
1037 else
1038 args[num_args++] = a->addr;
1039
1040 args[num_args++] = a->resource;
1041 if (sample)
1042 args[num_args++] = a->sampler;
1043 args[num_args++] = LLVMConstInt(ctx->i32, a->dmask, 0);
1044 if (sample)
1045 args[num_args++] = LLVMConstInt(ctx->i1, a->unorm, 0);
1046 args[num_args++] = LLVMConstInt(ctx->i1, 0, 0); /* glc */
1047 args[num_args++] = LLVMConstInt(ctx->i1, 0, 0); /* slc */
1048 args[num_args++] = LLVMConstInt(ctx->i1, 0, 0); /* lwe */
1049 args[num_args++] = LLVMConstInt(ctx->i1, a->da, 0);
1050
1051 switch (a->opcode) {
1052 case ac_image_sample:
1053 name = "llvm.amdgcn.image.sample";
1054 break;
1055 case ac_image_gather4:
1056 name = "llvm.amdgcn.image.gather4";
1057 break;
1058 case ac_image_load:
1059 name = "llvm.amdgcn.image.load";
1060 break;
1061 case ac_image_load_mip:
1062 name = "llvm.amdgcn.image.load.mip";
1063 break;
1064 case ac_image_get_lod:
1065 name = "llvm.amdgcn.image.getlod";
1066 break;
1067 case ac_image_get_resinfo:
1068 name = "llvm.amdgcn.image.getresinfo";
1069 break;
1070 default:
1071 unreachable("invalid image opcode");
1072 }
1073
1074 ac_build_type_name_for_intr(LLVMTypeOf(args[0]), type,
1075 sizeof(type));
1076
1077 snprintf(intr_name, sizeof(intr_name), "%s%s%s%s.v4f32.%s.v8i32",
1078 name,
1079 a->compare ? ".c" : "",
1080 a->bias ? ".b" :
1081 a->lod ? ".l" :
1082 a->deriv ? ".d" :
1083 a->level_zero ? ".lz" : "",
1084 a->offset ? ".o" : "",
1085 type);
1086
1087 LLVMValueRef result =
1088 ac_build_intrinsic(ctx, intr_name,
1089 ctx->v4f32, args, num_args,
1090 AC_FUNC_ATTR_READNONE);
1091 if (!sample) {
1092 result = LLVMBuildBitCast(ctx->builder, result,
1093 ctx->v4i32, "");
1094 }
1095 return result;
1096 }
1097
1098 args[num_args++] = a->addr;
1099 args[num_args++] = a->resource;
1100
1101 if (a->opcode == ac_image_load ||
1102 a->opcode == ac_image_load_mip ||
1103 a->opcode == ac_image_get_resinfo) {
1104 dst_type = ctx->v4i32;
1105 } else {
1106 dst_type = ctx->v4f32;
1107 args[num_args++] = a->sampler;
1108 }
1109
1110 args[num_args++] = LLVMConstInt(ctx->i32, a->dmask, 0);
1111 args[num_args++] = LLVMConstInt(ctx->i32, a->unorm, 0);
1112 args[num_args++] = LLVMConstInt(ctx->i32, 0, 0); /* r128 */
1113 args[num_args++] = LLVMConstInt(ctx->i32, a->da, 0);
1114 args[num_args++] = LLVMConstInt(ctx->i32, 0, 0); /* glc */
1115 args[num_args++] = LLVMConstInt(ctx->i32, 0, 0); /* slc */
1116 args[num_args++] = LLVMConstInt(ctx->i32, 0, 0); /* tfe */
1117 args[num_args++] = LLVMConstInt(ctx->i32, 0, 0); /* lwe */
1118
1119 switch (a->opcode) {
1120 case ac_image_sample:
1121 name = "llvm.SI.image.sample";
1122 break;
1123 case ac_image_gather4:
1124 name = "llvm.SI.gather4";
1125 break;
1126 case ac_image_load:
1127 name = "llvm.SI.image.load";
1128 break;
1129 case ac_image_load_mip:
1130 name = "llvm.SI.image.load.mip";
1131 break;
1132 case ac_image_get_lod:
1133 name = "llvm.SI.getlod";
1134 break;
1135 case ac_image_get_resinfo:
1136 name = "llvm.SI.getresinfo";
1137 break;
1138 }
1139
1140 ac_build_type_name_for_intr(LLVMTypeOf(a->addr), type, sizeof(type));
1141 snprintf(intr_name, sizeof(intr_name), "%s%s%s%s.%s",
1142 name,
1143 a->compare ? ".c" : "",
1144 a->bias ? ".b" :
1145 a->lod ? ".l" :
1146 a->deriv ? ".d" :
1147 a->level_zero ? ".lz" : "",
1148 a->offset ? ".o" : "",
1149 type);
1150
1151 return ac_build_intrinsic(ctx, intr_name,
1152 dst_type, args, num_args,
1153 AC_FUNC_ATTR_READNONE |
1154 AC_FUNC_ATTR_LEGACY);
1155 }
1156
1157 LLVMValueRef ac_build_cvt_pkrtz_f16(struct ac_llvm_context *ctx,
1158 LLVMValueRef args[2])
1159 {
1160 if (HAVE_LLVM >= 0x0500) {
1161 LLVMTypeRef v2f16 =
1162 LLVMVectorType(LLVMHalfTypeInContext(ctx->context), 2);
1163 LLVMValueRef res =
1164 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pkrtz",
1165 v2f16, args, 2,
1166 AC_FUNC_ATTR_READNONE);
1167 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
1168 }
1169
1170 return ac_build_intrinsic(ctx, "llvm.SI.packf16", ctx->i32, args, 2,
1171 AC_FUNC_ATTR_READNONE |
1172 AC_FUNC_ATTR_LEGACY);
1173 }
1174
1175 /**
1176 * KILL, AKA discard in GLSL.
1177 *
1178 * \param value kill if value < 0.0 or value == NULL.
1179 */
1180 void ac_build_kill(struct ac_llvm_context *ctx, LLVMValueRef value)
1181 {
1182 if (value) {
1183 ac_build_intrinsic(ctx, "llvm.AMDGPU.kill", ctx->voidt,
1184 &value, 1, AC_FUNC_ATTR_LEGACY);
1185 } else {
1186 ac_build_intrinsic(ctx, "llvm.AMDGPU.kilp", ctx->voidt,
1187 NULL, 0, AC_FUNC_ATTR_LEGACY);
1188 }
1189 }
1190
1191 LLVMValueRef ac_build_bfe(struct ac_llvm_context *ctx, LLVMValueRef input,
1192 LLVMValueRef offset, LLVMValueRef width,
1193 bool is_signed)
1194 {
1195 LLVMValueRef args[] = {
1196 input,
1197 offset,
1198 width,
1199 };
1200
1201 if (HAVE_LLVM >= 0x0500) {
1202 return ac_build_intrinsic(ctx,
1203 is_signed ? "llvm.amdgcn.sbfe.i32" :
1204 "llvm.amdgcn.ubfe.i32",
1205 ctx->i32, args, 3,
1206 AC_FUNC_ATTR_READNONE);
1207 }
1208
1209 return ac_build_intrinsic(ctx,
1210 is_signed ? "llvm.AMDGPU.bfe.i32" :
1211 "llvm.AMDGPU.bfe.u32",
1212 ctx->i32, args, 3,
1213 AC_FUNC_ATTR_READNONE |
1214 AC_FUNC_ATTR_LEGACY);
1215 }
1216
1217 void ac_get_image_intr_name(const char *base_name,
1218 LLVMTypeRef data_type,
1219 LLVMTypeRef coords_type,
1220 LLVMTypeRef rsrc_type,
1221 char *out_name, unsigned out_len)
1222 {
1223 char coords_type_name[8];
1224
1225 ac_build_type_name_for_intr(coords_type, coords_type_name,
1226 sizeof(coords_type_name));
1227
1228 if (HAVE_LLVM <= 0x0309) {
1229 snprintf(out_name, out_len, "%s.%s", base_name, coords_type_name);
1230 } else {
1231 char data_type_name[8];
1232 char rsrc_type_name[8];
1233
1234 ac_build_type_name_for_intr(data_type, data_type_name,
1235 sizeof(data_type_name));
1236 ac_build_type_name_for_intr(rsrc_type, rsrc_type_name,
1237 sizeof(rsrc_type_name));
1238 snprintf(out_name, out_len, "%s.%s.%s.%s", base_name,
1239 data_type_name, coords_type_name, rsrc_type_name);
1240 }
1241 }
1242
1243 #define AC_EXP_TARGET (HAVE_LLVM >= 0x0500 ? 0 : 3)
1244 #define AC_EXP_OUT0 (HAVE_LLVM >= 0x0500 ? 2 : 5)
1245
1246 enum ac_ir_type {
1247 AC_IR_UNDEF,
1248 AC_IR_CONST,
1249 AC_IR_VALUE,
1250 };
1251
1252 struct ac_vs_exp_chan
1253 {
1254 LLVMValueRef value;
1255 float const_float;
1256 enum ac_ir_type type;
1257 };
1258
1259 struct ac_vs_exp_inst {
1260 unsigned offset;
1261 LLVMValueRef inst;
1262 struct ac_vs_exp_chan chan[4];
1263 };
1264
1265 struct ac_vs_exports {
1266 unsigned num;
1267 struct ac_vs_exp_inst exp[VARYING_SLOT_MAX];
1268 };
1269
1270 /* Return true if the PARAM export has been eliminated. */
1271 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset,
1272 uint32_t num_outputs,
1273 struct ac_vs_exp_inst *exp)
1274 {
1275 unsigned i, default_val; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
1276 bool is_zero[4] = {}, is_one[4] = {};
1277
1278 for (i = 0; i < 4; i++) {
1279 /* It's a constant expression. Undef outputs are eliminated too. */
1280 if (exp->chan[i].type == AC_IR_UNDEF) {
1281 is_zero[i] = true;
1282 is_one[i] = true;
1283 } else if (exp->chan[i].type == AC_IR_CONST) {
1284 if (exp->chan[i].const_float == 0)
1285 is_zero[i] = true;
1286 else if (exp->chan[i].const_float == 1)
1287 is_one[i] = true;
1288 else
1289 return false; /* other constant */
1290 } else
1291 return false;
1292 }
1293
1294 /* Only certain combinations of 0 and 1 can be eliminated. */
1295 if (is_zero[0] && is_zero[1] && is_zero[2])
1296 default_val = is_zero[3] ? 0 : 1;
1297 else if (is_one[0] && is_one[1] && is_one[2])
1298 default_val = is_zero[3] ? 2 : 3;
1299 else
1300 return false;
1301
1302 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
1303 LLVMInstructionEraseFromParent(exp->inst);
1304
1305 /* Change OFFSET to DEFAULT_VAL. */
1306 for (i = 0; i < num_outputs; i++) {
1307 if (vs_output_param_offset[i] == exp->offset) {
1308 vs_output_param_offset[i] =
1309 AC_EXP_PARAM_DEFAULT_VAL_0000 + default_val;
1310 break;
1311 }
1312 }
1313 return true;
1314 }
1315
1316 static bool ac_eliminate_duplicated_output(uint8_t *vs_output_param_offset,
1317 uint32_t num_outputs,
1318 struct ac_vs_exports *processed,
1319 struct ac_vs_exp_inst *exp)
1320 {
1321 unsigned p, copy_back_channels = 0;
1322
1323 /* See if the output is already in the list of processed outputs.
1324 * The LLVMValueRef comparison relies on SSA.
1325 */
1326 for (p = 0; p < processed->num; p++) {
1327 bool different = false;
1328
1329 for (unsigned j = 0; j < 4; j++) {
1330 struct ac_vs_exp_chan *c1 = &processed->exp[p].chan[j];
1331 struct ac_vs_exp_chan *c2 = &exp->chan[j];
1332
1333 /* Treat undef as a match. */
1334 if (c2->type == AC_IR_UNDEF)
1335 continue;
1336
1337 /* If c1 is undef but c2 isn't, we can copy c2 to c1
1338 * and consider the instruction duplicated.
1339 */
1340 if (c1->type == AC_IR_UNDEF) {
1341 copy_back_channels |= 1 << j;
1342 continue;
1343 }
1344
1345 /* Test whether the channels are not equal. */
1346 if (c1->type != c2->type ||
1347 (c1->type == AC_IR_CONST &&
1348 c1->const_float != c2->const_float) ||
1349 (c1->type == AC_IR_VALUE &&
1350 c1->value != c2->value)) {
1351 different = true;
1352 break;
1353 }
1354 }
1355 if (!different)
1356 break;
1357
1358 copy_back_channels = 0;
1359 }
1360 if (p == processed->num)
1361 return false;
1362
1363 /* If a match was found, but the matching export has undef where the new
1364 * one has a normal value, copy the normal value to the undef channel.
1365 */
1366 struct ac_vs_exp_inst *match = &processed->exp[p];
1367
1368 while (copy_back_channels) {
1369 unsigned chan = u_bit_scan(&copy_back_channels);
1370
1371 assert(match->chan[chan].type == AC_IR_UNDEF);
1372 LLVMSetOperand(match->inst, AC_EXP_OUT0 + chan,
1373 exp->chan[chan].value);
1374 match->chan[chan] = exp->chan[chan];
1375 }
1376
1377 /* The PARAM export is duplicated. Kill it. */
1378 LLVMInstructionEraseFromParent(exp->inst);
1379
1380 /* Change OFFSET to the matching export. */
1381 for (unsigned i = 0; i < num_outputs; i++) {
1382 if (vs_output_param_offset[i] == exp->offset) {
1383 vs_output_param_offset[i] = match->offset;
1384 break;
1385 }
1386 }
1387 return true;
1388 }
1389
1390 void ac_optimize_vs_outputs(struct ac_llvm_context *ctx,
1391 LLVMValueRef main_fn,
1392 uint8_t *vs_output_param_offset,
1393 uint32_t num_outputs,
1394 uint8_t *num_param_exports)
1395 {
1396 LLVMBasicBlockRef bb;
1397 bool removed_any = false;
1398 struct ac_vs_exports exports;
1399
1400 exports.num = 0;
1401
1402 /* Process all LLVM instructions. */
1403 bb = LLVMGetFirstBasicBlock(main_fn);
1404 while (bb) {
1405 LLVMValueRef inst = LLVMGetFirstInstruction(bb);
1406
1407 while (inst) {
1408 LLVMValueRef cur = inst;
1409 inst = LLVMGetNextInstruction(inst);
1410 struct ac_vs_exp_inst exp;
1411
1412 if (LLVMGetInstructionOpcode(cur) != LLVMCall)
1413 continue;
1414
1415 LLVMValueRef callee = ac_llvm_get_called_value(cur);
1416
1417 if (!ac_llvm_is_function(callee))
1418 continue;
1419
1420 const char *name = LLVMGetValueName(callee);
1421 unsigned num_args = LLVMCountParams(callee);
1422
1423 /* Check if this is an export instruction. */
1424 if ((num_args != 9 && num_args != 8) ||
1425 (strcmp(name, "llvm.SI.export") &&
1426 strcmp(name, "llvm.amdgcn.exp.f32")))
1427 continue;
1428
1429 LLVMValueRef arg = LLVMGetOperand(cur, AC_EXP_TARGET);
1430 unsigned target = LLVMConstIntGetZExtValue(arg);
1431
1432 if (target < V_008DFC_SQ_EXP_PARAM)
1433 continue;
1434
1435 target -= V_008DFC_SQ_EXP_PARAM;
1436
1437 /* Parse the instruction. */
1438 memset(&exp, 0, sizeof(exp));
1439 exp.offset = target;
1440 exp.inst = cur;
1441
1442 for (unsigned i = 0; i < 4; i++) {
1443 LLVMValueRef v = LLVMGetOperand(cur, AC_EXP_OUT0 + i);
1444
1445 exp.chan[i].value = v;
1446
1447 if (LLVMIsUndef(v)) {
1448 exp.chan[i].type = AC_IR_UNDEF;
1449 } else if (LLVMIsAConstantFP(v)) {
1450 LLVMBool loses_info;
1451 exp.chan[i].type = AC_IR_CONST;
1452 exp.chan[i].const_float =
1453 LLVMConstRealGetDouble(v, &loses_info);
1454 } else {
1455 exp.chan[i].type = AC_IR_VALUE;
1456 }
1457 }
1458
1459 /* Eliminate constant and duplicated PARAM exports. */
1460 if (ac_eliminate_const_output(vs_output_param_offset,
1461 num_outputs, &exp) ||
1462 ac_eliminate_duplicated_output(vs_output_param_offset,
1463 num_outputs, &exports,
1464 &exp)) {
1465 removed_any = true;
1466 } else {
1467 exports.exp[exports.num++] = exp;
1468 }
1469 }
1470 bb = LLVMGetNextBasicBlock(bb);
1471 }
1472
1473 /* Remove holes in export memory due to removed PARAM exports.
1474 * This is done by renumbering all PARAM exports.
1475 */
1476 if (removed_any) {
1477 uint8_t old_offset[VARYING_SLOT_MAX];
1478 unsigned out, i;
1479
1480 /* Make a copy of the offsets. We need the old version while
1481 * we are modifying some of them. */
1482 memcpy(old_offset, vs_output_param_offset,
1483 sizeof(old_offset));
1484
1485 for (i = 0; i < exports.num; i++) {
1486 unsigned offset = exports.exp[i].offset;
1487
1488 /* Update vs_output_param_offset. Multiple outputs can
1489 * have the same offset.
1490 */
1491 for (out = 0; out < num_outputs; out++) {
1492 if (old_offset[out] == offset)
1493 vs_output_param_offset[out] = i;
1494 }
1495
1496 /* Change the PARAM offset in the instruction. */
1497 LLVMSetOperand(exports.exp[i].inst, AC_EXP_TARGET,
1498 LLVMConstInt(ctx->i32,
1499 V_008DFC_SQ_EXP_PARAM + i, 0));
1500 }
1501 *num_param_exports = exports.num;
1502 }
1503 }