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