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