9b939c148e5bafa3d99171c88e45aee4bb2985ef
[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 lds,
800 LLVMValueRef val)
801 {
802 LLVMValueRef thread_id, tl, trbl, tl_tid, trbl_tid, args[2];
803 LLVMValueRef result;
804
805 thread_id = ac_get_thread_id(ctx);
806
807 tl_tid = LLVMBuildAnd(ctx->builder, thread_id,
808 LLVMConstInt(ctx->i32, mask, false), "");
809
810 trbl_tid = LLVMBuildAdd(ctx->builder, tl_tid,
811 LLVMConstInt(ctx->i32, idx, false), "");
812
813 if (has_ds_bpermute) {
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 LLVMValueRef store_ptr, load_ptr0, load_ptr1;
832
833 store_ptr = ac_build_gep0(ctx, lds, thread_id);
834 load_ptr0 = ac_build_gep0(ctx, lds, tl_tid);
835 load_ptr1 = ac_build_gep0(ctx, lds, trbl_tid);
836
837 LLVMBuildStore(ctx->builder, val, store_ptr);
838 tl = LLVMBuildLoad(ctx->builder, load_ptr0, "");
839 trbl = LLVMBuildLoad(ctx->builder, load_ptr1, "");
840 }
841
842 tl = LLVMBuildBitCast(ctx->builder, tl, ctx->f32, "");
843 trbl = LLVMBuildBitCast(ctx->builder, trbl, ctx->f32, "");
844 result = LLVMBuildFSub(ctx->builder, trbl, tl, "");
845 return result;
846 }
847
848 void
849 ac_build_sendmsg(struct ac_llvm_context *ctx,
850 uint32_t msg,
851 LLVMValueRef wave_id)
852 {
853 LLVMValueRef args[2];
854 const char *intr_name = (HAVE_LLVM < 0x0400) ? "llvm.SI.sendmsg" : "llvm.amdgcn.s.sendmsg";
855 args[0] = LLVMConstInt(ctx->i32, msg, false);
856 args[1] = wave_id;
857 ac_build_intrinsic(ctx, intr_name, ctx->voidt, args, 2, 0);
858 }
859
860 LLVMValueRef
861 ac_build_imsb(struct ac_llvm_context *ctx,
862 LLVMValueRef arg,
863 LLVMTypeRef dst_type)
864 {
865 const char *intr_name = (HAVE_LLVM < 0x0400) ? "llvm.AMDGPU.flbit.i32" :
866 "llvm.amdgcn.sffbh.i32";
867 LLVMValueRef msb = ac_build_intrinsic(ctx, intr_name,
868 dst_type, &arg, 1,
869 AC_FUNC_ATTR_READNONE);
870
871 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
872 * the index from LSB. Invert it by doing "31 - msb". */
873 msb = LLVMBuildSub(ctx->builder, LLVMConstInt(ctx->i32, 31, false),
874 msb, "");
875
876 LLVMValueRef all_ones = LLVMConstInt(ctx->i32, -1, true);
877 LLVMValueRef cond = LLVMBuildOr(ctx->builder,
878 LLVMBuildICmp(ctx->builder, LLVMIntEQ,
879 arg, LLVMConstInt(ctx->i32, 0, 0), ""),
880 LLVMBuildICmp(ctx->builder, LLVMIntEQ,
881 arg, all_ones, ""), "");
882
883 return LLVMBuildSelect(ctx->builder, cond, all_ones, msb, "");
884 }
885
886 LLVMValueRef
887 ac_build_umsb(struct ac_llvm_context *ctx,
888 LLVMValueRef arg,
889 LLVMTypeRef dst_type)
890 {
891 LLVMValueRef args[2] = {
892 arg,
893 LLVMConstInt(ctx->i1, 1, 0),
894 };
895 LLVMValueRef msb = ac_build_intrinsic(ctx, "llvm.ctlz.i32",
896 dst_type, args, ARRAY_SIZE(args),
897 AC_FUNC_ATTR_READNONE);
898
899 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
900 * the index from LSB. Invert it by doing "31 - msb". */
901 msb = LLVMBuildSub(ctx->builder, LLVMConstInt(ctx->i32, 31, false),
902 msb, "");
903
904 /* check for zero */
905 return LLVMBuildSelect(ctx->builder,
906 LLVMBuildICmp(ctx->builder, LLVMIntEQ, arg,
907 LLVMConstInt(ctx->i32, 0, 0), ""),
908 LLVMConstInt(ctx->i32, -1, true), msb, "");
909 }
910
911 LLVMValueRef ac_build_umin(struct ac_llvm_context *ctx, LLVMValueRef a,
912 LLVMValueRef b)
913 {
914 LLVMValueRef cmp = LLVMBuildICmp(ctx->builder, LLVMIntULE, a, b, "");
915 return LLVMBuildSelect(ctx->builder, cmp, a, b, "");
916 }
917
918 LLVMValueRef ac_build_clamp(struct ac_llvm_context *ctx, LLVMValueRef value)
919 {
920 if (HAVE_LLVM >= 0x0500) {
921 LLVMValueRef max[2] = {
922 value,
923 LLVMConstReal(ctx->f32, 0),
924 };
925 LLVMValueRef min[2] = {
926 LLVMConstReal(ctx->f32, 1),
927 };
928
929 min[1] = ac_build_intrinsic(ctx, "llvm.maxnum.f32",
930 ctx->f32, max, 2,
931 AC_FUNC_ATTR_READNONE);
932 return ac_build_intrinsic(ctx, "llvm.minnum.f32",
933 ctx->f32, min, 2,
934 AC_FUNC_ATTR_READNONE);
935 }
936
937 LLVMValueRef args[3] = {
938 value,
939 LLVMConstReal(ctx->f32, 0),
940 LLVMConstReal(ctx->f32, 1),
941 };
942
943 return ac_build_intrinsic(ctx, "llvm.AMDGPU.clamp.", ctx->f32, args, 3,
944 AC_FUNC_ATTR_READNONE |
945 AC_FUNC_ATTR_LEGACY);
946 }
947
948 void ac_build_export(struct ac_llvm_context *ctx, struct ac_export_args *a)
949 {
950 LLVMValueRef args[9];
951
952 if (HAVE_LLVM >= 0x0500) {
953 args[0] = LLVMConstInt(ctx->i32, a->target, 0);
954 args[1] = LLVMConstInt(ctx->i32, a->enabled_channels, 0);
955
956 if (a->compr) {
957 LLVMTypeRef i16 = LLVMInt16TypeInContext(ctx->context);
958 LLVMTypeRef v2i16 = LLVMVectorType(i16, 2);
959
960 args[2] = LLVMBuildBitCast(ctx->builder, a->out[0],
961 v2i16, "");
962 args[3] = LLVMBuildBitCast(ctx->builder, a->out[1],
963 v2i16, "");
964 args[4] = LLVMConstInt(ctx->i1, a->done, 0);
965 args[5] = LLVMConstInt(ctx->i1, a->valid_mask, 0);
966
967 ac_build_intrinsic(ctx, "llvm.amdgcn.exp.compr.v2i16",
968 ctx->voidt, args, 6, 0);
969 } else {
970 args[2] = a->out[0];
971 args[3] = a->out[1];
972 args[4] = a->out[2];
973 args[5] = a->out[3];
974 args[6] = LLVMConstInt(ctx->i1, a->done, 0);
975 args[7] = LLVMConstInt(ctx->i1, a->valid_mask, 0);
976
977 ac_build_intrinsic(ctx, "llvm.amdgcn.exp.f32",
978 ctx->voidt, args, 8, 0);
979 }
980 return;
981 }
982
983 args[0] = LLVMConstInt(ctx->i32, a->enabled_channels, 0);
984 args[1] = LLVMConstInt(ctx->i32, a->valid_mask, 0);
985 args[2] = LLVMConstInt(ctx->i32, a->done, 0);
986 args[3] = LLVMConstInt(ctx->i32, a->target, 0);
987 args[4] = LLVMConstInt(ctx->i32, a->compr, 0);
988 memcpy(args + 5, a->out, sizeof(a->out[0]) * 4);
989
990 ac_build_intrinsic(ctx, "llvm.SI.export", ctx->voidt, args, 9,
991 AC_FUNC_ATTR_LEGACY);
992 }
993
994 LLVMValueRef ac_build_image_opcode(struct ac_llvm_context *ctx,
995 struct ac_image_args *a)
996 {
997 LLVMTypeRef dst_type;
998 LLVMValueRef args[11];
999 unsigned num_args = 0;
1000 const char *name;
1001 char intr_name[128], type[64];
1002
1003 if (HAVE_LLVM >= 0x0400) {
1004 bool sample = a->opcode == ac_image_sample ||
1005 a->opcode == ac_image_gather4 ||
1006 a->opcode == ac_image_get_lod;
1007
1008 if (sample)
1009 args[num_args++] = bitcast_to_float(ctx, a->addr);
1010 else
1011 args[num_args++] = a->addr;
1012
1013 args[num_args++] = a->resource;
1014 if (sample)
1015 args[num_args++] = a->sampler;
1016 args[num_args++] = LLVMConstInt(ctx->i32, a->dmask, 0);
1017 if (sample)
1018 args[num_args++] = LLVMConstInt(ctx->i1, a->unorm, 0);
1019 args[num_args++] = LLVMConstInt(ctx->i1, 0, 0); /* glc */
1020 args[num_args++] = LLVMConstInt(ctx->i1, 0, 0); /* slc */
1021 args[num_args++] = LLVMConstInt(ctx->i1, 0, 0); /* lwe */
1022 args[num_args++] = LLVMConstInt(ctx->i1, a->da, 0);
1023
1024 switch (a->opcode) {
1025 case ac_image_sample:
1026 name = "llvm.amdgcn.image.sample";
1027 break;
1028 case ac_image_gather4:
1029 name = "llvm.amdgcn.image.gather4";
1030 break;
1031 case ac_image_load:
1032 name = "llvm.amdgcn.image.load";
1033 break;
1034 case ac_image_load_mip:
1035 name = "llvm.amdgcn.image.load.mip";
1036 break;
1037 case ac_image_get_lod:
1038 name = "llvm.amdgcn.image.getlod";
1039 break;
1040 case ac_image_get_resinfo:
1041 name = "llvm.amdgcn.image.getresinfo";
1042 break;
1043 default:
1044 unreachable("invalid image opcode");
1045 }
1046
1047 ac_build_type_name_for_intr(LLVMTypeOf(args[0]), type,
1048 sizeof(type));
1049
1050 snprintf(intr_name, sizeof(intr_name), "%s%s%s%s.v4f32.%s.v8i32",
1051 name,
1052 a->compare ? ".c" : "",
1053 a->bias ? ".b" :
1054 a->lod ? ".l" :
1055 a->deriv ? ".d" :
1056 a->level_zero ? ".lz" : "",
1057 a->offset ? ".o" : "",
1058 type);
1059
1060 LLVMValueRef result =
1061 ac_build_intrinsic(ctx, intr_name,
1062 ctx->v4f32, args, num_args,
1063 AC_FUNC_ATTR_READNONE);
1064 if (!sample) {
1065 result = LLVMBuildBitCast(ctx->builder, result,
1066 ctx->v4i32, "");
1067 }
1068 return result;
1069 }
1070
1071 args[num_args++] = a->addr;
1072 args[num_args++] = a->resource;
1073
1074 if (a->opcode == ac_image_load ||
1075 a->opcode == ac_image_load_mip ||
1076 a->opcode == ac_image_get_resinfo) {
1077 dst_type = ctx->v4i32;
1078 } else {
1079 dst_type = ctx->v4f32;
1080 args[num_args++] = a->sampler;
1081 }
1082
1083 args[num_args++] = LLVMConstInt(ctx->i32, a->dmask, 0);
1084 args[num_args++] = LLVMConstInt(ctx->i32, a->unorm, 0);
1085 args[num_args++] = LLVMConstInt(ctx->i32, 0, 0); /* r128 */
1086 args[num_args++] = LLVMConstInt(ctx->i32, a->da, 0);
1087 args[num_args++] = LLVMConstInt(ctx->i32, 0, 0); /* glc */
1088 args[num_args++] = LLVMConstInt(ctx->i32, 0, 0); /* slc */
1089 args[num_args++] = LLVMConstInt(ctx->i32, 0, 0); /* tfe */
1090 args[num_args++] = LLVMConstInt(ctx->i32, 0, 0); /* lwe */
1091
1092 switch (a->opcode) {
1093 case ac_image_sample:
1094 name = "llvm.SI.image.sample";
1095 break;
1096 case ac_image_gather4:
1097 name = "llvm.SI.gather4";
1098 break;
1099 case ac_image_load:
1100 name = "llvm.SI.image.load";
1101 break;
1102 case ac_image_load_mip:
1103 name = "llvm.SI.image.load.mip";
1104 break;
1105 case ac_image_get_lod:
1106 name = "llvm.SI.getlod";
1107 break;
1108 case ac_image_get_resinfo:
1109 name = "llvm.SI.getresinfo";
1110 break;
1111 }
1112
1113 ac_build_type_name_for_intr(LLVMTypeOf(a->addr), type, sizeof(type));
1114 snprintf(intr_name, sizeof(intr_name), "%s%s%s%s.%s",
1115 name,
1116 a->compare ? ".c" : "",
1117 a->bias ? ".b" :
1118 a->lod ? ".l" :
1119 a->deriv ? ".d" :
1120 a->level_zero ? ".lz" : "",
1121 a->offset ? ".o" : "",
1122 type);
1123
1124 return ac_build_intrinsic(ctx, intr_name,
1125 dst_type, args, num_args,
1126 AC_FUNC_ATTR_READNONE |
1127 AC_FUNC_ATTR_LEGACY);
1128 }
1129
1130 LLVMValueRef ac_build_cvt_pkrtz_f16(struct ac_llvm_context *ctx,
1131 LLVMValueRef args[2])
1132 {
1133 if (HAVE_LLVM >= 0x0500) {
1134 LLVMTypeRef v2f16 =
1135 LLVMVectorType(LLVMHalfTypeInContext(ctx->context), 2);
1136 LLVMValueRef res =
1137 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pkrtz",
1138 v2f16, args, 2,
1139 AC_FUNC_ATTR_READNONE);
1140 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
1141 }
1142
1143 return ac_build_intrinsic(ctx, "llvm.SI.packf16", ctx->i32, args, 2,
1144 AC_FUNC_ATTR_READNONE |
1145 AC_FUNC_ATTR_LEGACY);
1146 }
1147
1148 /**
1149 * KILL, AKA discard in GLSL.
1150 *
1151 * \param value kill if value < 0.0 or value == NULL.
1152 */
1153 void ac_build_kill(struct ac_llvm_context *ctx, LLVMValueRef value)
1154 {
1155 if (value) {
1156 ac_build_intrinsic(ctx, "llvm.AMDGPU.kill", ctx->voidt,
1157 &value, 1, AC_FUNC_ATTR_LEGACY);
1158 } else {
1159 ac_build_intrinsic(ctx, "llvm.AMDGPU.kilp", ctx->voidt,
1160 NULL, 0, AC_FUNC_ATTR_LEGACY);
1161 }
1162 }
1163
1164 LLVMValueRef ac_build_bfe(struct ac_llvm_context *ctx, LLVMValueRef input,
1165 LLVMValueRef offset, LLVMValueRef width,
1166 bool is_signed)
1167 {
1168 LLVMValueRef args[] = {
1169 input,
1170 offset,
1171 width,
1172 };
1173
1174 if (HAVE_LLVM >= 0x0500) {
1175 return ac_build_intrinsic(ctx,
1176 is_signed ? "llvm.amdgcn.sbfe.i32" :
1177 "llvm.amdgcn.ubfe.i32",
1178 ctx->i32, args, 3,
1179 AC_FUNC_ATTR_READNONE);
1180 }
1181
1182 return ac_build_intrinsic(ctx,
1183 is_signed ? "llvm.AMDGPU.bfe.i32" :
1184 "llvm.AMDGPU.bfe.u32",
1185 ctx->i32, args, 3,
1186 AC_FUNC_ATTR_READNONE |
1187 AC_FUNC_ATTR_LEGACY);
1188 }
1189
1190 void ac_get_image_intr_name(const char *base_name,
1191 LLVMTypeRef data_type,
1192 LLVMTypeRef coords_type,
1193 LLVMTypeRef rsrc_type,
1194 char *out_name, unsigned out_len)
1195 {
1196 char coords_type_name[8];
1197
1198 ac_build_type_name_for_intr(coords_type, coords_type_name,
1199 sizeof(coords_type_name));
1200
1201 if (HAVE_LLVM <= 0x0309) {
1202 snprintf(out_name, out_len, "%s.%s", base_name, coords_type_name);
1203 } else {
1204 char data_type_name[8];
1205 char rsrc_type_name[8];
1206
1207 ac_build_type_name_for_intr(data_type, data_type_name,
1208 sizeof(data_type_name));
1209 ac_build_type_name_for_intr(rsrc_type, rsrc_type_name,
1210 sizeof(rsrc_type_name));
1211 snprintf(out_name, out_len, "%s.%s.%s.%s", base_name,
1212 data_type_name, coords_type_name, rsrc_type_name);
1213 }
1214 }
1215
1216 #define AC_EXP_TARGET (HAVE_LLVM >= 0x0500 ? 0 : 3)
1217 #define AC_EXP_OUT0 (HAVE_LLVM >= 0x0500 ? 2 : 5)
1218
1219 enum ac_ir_type {
1220 AC_IR_UNDEF,
1221 AC_IR_CONST,
1222 AC_IR_VALUE,
1223 };
1224
1225 struct ac_vs_exp_chan
1226 {
1227 LLVMValueRef value;
1228 float const_float;
1229 enum ac_ir_type type;
1230 };
1231
1232 struct ac_vs_exp_inst {
1233 unsigned offset;
1234 LLVMValueRef inst;
1235 struct ac_vs_exp_chan chan[4];
1236 };
1237
1238 struct ac_vs_exports {
1239 unsigned num;
1240 struct ac_vs_exp_inst exp[VARYING_SLOT_MAX];
1241 };
1242
1243 /* Return true if the PARAM export has been eliminated. */
1244 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset,
1245 uint32_t num_outputs,
1246 struct ac_vs_exp_inst *exp)
1247 {
1248 unsigned i, default_val; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
1249 bool is_zero[4] = {}, is_one[4] = {};
1250
1251 for (i = 0; i < 4; i++) {
1252 /* It's a constant expression. Undef outputs are eliminated too. */
1253 if (exp->chan[i].type == AC_IR_UNDEF) {
1254 is_zero[i] = true;
1255 is_one[i] = true;
1256 } else if (exp->chan[i].type == AC_IR_CONST) {
1257 if (exp->chan[i].const_float == 0)
1258 is_zero[i] = true;
1259 else if (exp->chan[i].const_float == 1)
1260 is_one[i] = true;
1261 else
1262 return false; /* other constant */
1263 } else
1264 return false;
1265 }
1266
1267 /* Only certain combinations of 0 and 1 can be eliminated. */
1268 if (is_zero[0] && is_zero[1] && is_zero[2])
1269 default_val = is_zero[3] ? 0 : 1;
1270 else if (is_one[0] && is_one[1] && is_one[2])
1271 default_val = is_zero[3] ? 2 : 3;
1272 else
1273 return false;
1274
1275 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
1276 LLVMInstructionEraseFromParent(exp->inst);
1277
1278 /* Change OFFSET to DEFAULT_VAL. */
1279 for (i = 0; i < num_outputs; i++) {
1280 if (vs_output_param_offset[i] == exp->offset) {
1281 vs_output_param_offset[i] =
1282 AC_EXP_PARAM_DEFAULT_VAL_0000 + default_val;
1283 break;
1284 }
1285 }
1286 return true;
1287 }
1288
1289 static bool ac_eliminate_duplicated_output(uint8_t *vs_output_param_offset,
1290 uint32_t num_outputs,
1291 struct ac_vs_exports *processed,
1292 struct ac_vs_exp_inst *exp)
1293 {
1294 unsigned p, copy_back_channels = 0;
1295
1296 /* See if the output is already in the list of processed outputs.
1297 * The LLVMValueRef comparison relies on SSA.
1298 */
1299 for (p = 0; p < processed->num; p++) {
1300 bool different = false;
1301
1302 for (unsigned j = 0; j < 4; j++) {
1303 struct ac_vs_exp_chan *c1 = &processed->exp[p].chan[j];
1304 struct ac_vs_exp_chan *c2 = &exp->chan[j];
1305
1306 /* Treat undef as a match. */
1307 if (c2->type == AC_IR_UNDEF)
1308 continue;
1309
1310 /* If c1 is undef but c2 isn't, we can copy c2 to c1
1311 * and consider the instruction duplicated.
1312 */
1313 if (c1->type == AC_IR_UNDEF) {
1314 copy_back_channels |= 1 << j;
1315 continue;
1316 }
1317
1318 /* Test whether the channels are not equal. */
1319 if (c1->type != c2->type ||
1320 (c1->type == AC_IR_CONST &&
1321 c1->const_float != c2->const_float) ||
1322 (c1->type == AC_IR_VALUE &&
1323 c1->value != c2->value)) {
1324 different = true;
1325 break;
1326 }
1327 }
1328 if (!different)
1329 break;
1330
1331 copy_back_channels = 0;
1332 }
1333 if (p == processed->num)
1334 return false;
1335
1336 /* If a match was found, but the matching export has undef where the new
1337 * one has a normal value, copy the normal value to the undef channel.
1338 */
1339 struct ac_vs_exp_inst *match = &processed->exp[p];
1340
1341 while (copy_back_channels) {
1342 unsigned chan = u_bit_scan(&copy_back_channels);
1343
1344 assert(match->chan[chan].type == AC_IR_UNDEF);
1345 LLVMSetOperand(match->inst, AC_EXP_OUT0 + chan,
1346 exp->chan[chan].value);
1347 match->chan[chan] = exp->chan[chan];
1348 }
1349
1350 /* The PARAM export is duplicated. Kill it. */
1351 LLVMInstructionEraseFromParent(exp->inst);
1352
1353 /* Change OFFSET to the matching export. */
1354 for (unsigned i = 0; i < num_outputs; i++) {
1355 if (vs_output_param_offset[i] == exp->offset) {
1356 vs_output_param_offset[i] = match->offset;
1357 break;
1358 }
1359 }
1360 return true;
1361 }
1362
1363 void ac_optimize_vs_outputs(struct ac_llvm_context *ctx,
1364 LLVMValueRef main_fn,
1365 uint8_t *vs_output_param_offset,
1366 uint32_t num_outputs,
1367 uint8_t *num_param_exports)
1368 {
1369 LLVMBasicBlockRef bb;
1370 bool removed_any = false;
1371 struct ac_vs_exports exports;
1372
1373 exports.num = 0;
1374
1375 /* Process all LLVM instructions. */
1376 bb = LLVMGetFirstBasicBlock(main_fn);
1377 while (bb) {
1378 LLVMValueRef inst = LLVMGetFirstInstruction(bb);
1379
1380 while (inst) {
1381 LLVMValueRef cur = inst;
1382 inst = LLVMGetNextInstruction(inst);
1383 struct ac_vs_exp_inst exp;
1384
1385 if (LLVMGetInstructionOpcode(cur) != LLVMCall)
1386 continue;
1387
1388 LLVMValueRef callee = ac_llvm_get_called_value(cur);
1389
1390 if (!ac_llvm_is_function(callee))
1391 continue;
1392
1393 const char *name = LLVMGetValueName(callee);
1394 unsigned num_args = LLVMCountParams(callee);
1395
1396 /* Check if this is an export instruction. */
1397 if ((num_args != 9 && num_args != 8) ||
1398 (strcmp(name, "llvm.SI.export") &&
1399 strcmp(name, "llvm.amdgcn.exp.f32")))
1400 continue;
1401
1402 LLVMValueRef arg = LLVMGetOperand(cur, AC_EXP_TARGET);
1403 unsigned target = LLVMConstIntGetZExtValue(arg);
1404
1405 if (target < V_008DFC_SQ_EXP_PARAM)
1406 continue;
1407
1408 target -= V_008DFC_SQ_EXP_PARAM;
1409
1410 /* Parse the instruction. */
1411 memset(&exp, 0, sizeof(exp));
1412 exp.offset = target;
1413 exp.inst = cur;
1414
1415 for (unsigned i = 0; i < 4; i++) {
1416 LLVMValueRef v = LLVMGetOperand(cur, AC_EXP_OUT0 + i);
1417
1418 exp.chan[i].value = v;
1419
1420 if (LLVMIsUndef(v)) {
1421 exp.chan[i].type = AC_IR_UNDEF;
1422 } else if (LLVMIsAConstantFP(v)) {
1423 LLVMBool loses_info;
1424 exp.chan[i].type = AC_IR_CONST;
1425 exp.chan[i].const_float =
1426 LLVMConstRealGetDouble(v, &loses_info);
1427 } else {
1428 exp.chan[i].type = AC_IR_VALUE;
1429 }
1430 }
1431
1432 /* Eliminate constant and duplicated PARAM exports. */
1433 if (ac_eliminate_const_output(vs_output_param_offset,
1434 num_outputs, &exp) ||
1435 ac_eliminate_duplicated_output(vs_output_param_offset,
1436 num_outputs, &exports,
1437 &exp)) {
1438 removed_any = true;
1439 } else {
1440 exports.exp[exports.num++] = exp;
1441 }
1442 }
1443 bb = LLVMGetNextBasicBlock(bb);
1444 }
1445
1446 /* Remove holes in export memory due to removed PARAM exports.
1447 * This is done by renumbering all PARAM exports.
1448 */
1449 if (removed_any) {
1450 uint8_t old_offset[VARYING_SLOT_MAX];
1451 unsigned out, i;
1452
1453 /* Make a copy of the offsets. We need the old version while
1454 * we are modifying some of them. */
1455 memcpy(old_offset, vs_output_param_offset,
1456 sizeof(old_offset));
1457
1458 for (i = 0; i < exports.num; i++) {
1459 unsigned offset = exports.exp[i].offset;
1460
1461 /* Update vs_output_param_offset. Multiple outputs can
1462 * have the same offset.
1463 */
1464 for (out = 0; out < num_outputs; out++) {
1465 if (old_offset[out] == offset)
1466 vs_output_param_offset[out] = i;
1467 }
1468
1469 /* Change the PARAM offset in the instruction. */
1470 LLVMSetOperand(exports.exp[i].inst, AC_EXP_TARGET,
1471 LLVMConstInt(ctx->i32,
1472 V_008DFC_SQ_EXP_PARAM + i, 0));
1473 }
1474 *num_param_exports = exports.num;
1475 }
1476 }