ac: fix ac_build_varying_gather_values() for packed layouts
[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 "util/u_atomic.h"
40 #include "sid.h"
41
42 #include "shader_enums.h"
43
44 /* Initialize module-independent parts of the context.
45 *
46 * The caller is responsible for initializing ctx::module and ctx::builder.
47 */
48 void
49 ac_llvm_context_init(struct ac_llvm_context *ctx, LLVMContextRef context,
50 enum chip_class chip_class, enum radeon_family family)
51 {
52 LLVMValueRef args[1];
53
54 ctx->chip_class = chip_class;
55 ctx->family = family;
56
57 ctx->context = context;
58 ctx->module = NULL;
59 ctx->builder = NULL;
60
61 ctx->voidt = LLVMVoidTypeInContext(ctx->context);
62 ctx->i1 = LLVMInt1TypeInContext(ctx->context);
63 ctx->i8 = LLVMInt8TypeInContext(ctx->context);
64 ctx->i16 = LLVMIntTypeInContext(ctx->context, 16);
65 ctx->i32 = LLVMIntTypeInContext(ctx->context, 32);
66 ctx->i64 = LLVMIntTypeInContext(ctx->context, 64);
67 ctx->f16 = LLVMHalfTypeInContext(ctx->context);
68 ctx->f32 = LLVMFloatTypeInContext(ctx->context);
69 ctx->f64 = LLVMDoubleTypeInContext(ctx->context);
70 ctx->v2i32 = LLVMVectorType(ctx->i32, 2);
71 ctx->v3i32 = LLVMVectorType(ctx->i32, 3);
72 ctx->v4i32 = LLVMVectorType(ctx->i32, 4);
73 ctx->v2f32 = LLVMVectorType(ctx->f32, 2);
74 ctx->v4f32 = LLVMVectorType(ctx->f32, 4);
75 ctx->v8i32 = LLVMVectorType(ctx->i32, 8);
76
77 ctx->i32_0 = LLVMConstInt(ctx->i32, 0, false);
78 ctx->i32_1 = LLVMConstInt(ctx->i32, 1, false);
79 ctx->i64_0 = LLVMConstInt(ctx->i64, 0, false);
80 ctx->i64_1 = LLVMConstInt(ctx->i64, 1, false);
81 ctx->f32_0 = LLVMConstReal(ctx->f32, 0.0);
82 ctx->f32_1 = LLVMConstReal(ctx->f32, 1.0);
83 ctx->f64_0 = LLVMConstReal(ctx->f64, 0.0);
84 ctx->f64_1 = LLVMConstReal(ctx->f64, 1.0);
85
86 ctx->i1false = LLVMConstInt(ctx->i1, 0, false);
87 ctx->i1true = LLVMConstInt(ctx->i1, 1, false);
88
89 ctx->range_md_kind = LLVMGetMDKindIDInContext(ctx->context,
90 "range", 5);
91
92 ctx->invariant_load_md_kind = LLVMGetMDKindIDInContext(ctx->context,
93 "invariant.load", 14);
94
95 ctx->fpmath_md_kind = LLVMGetMDKindIDInContext(ctx->context, "fpmath", 6);
96
97 args[0] = LLVMConstReal(ctx->f32, 2.5);
98 ctx->fpmath_md_2p5_ulp = LLVMMDNodeInContext(ctx->context, args, 1);
99
100 ctx->uniform_md_kind = LLVMGetMDKindIDInContext(ctx->context,
101 "amdgpu.uniform", 14);
102
103 ctx->empty_md = LLVMMDNodeInContext(ctx->context, NULL, 0);
104 }
105
106 int
107 ac_get_llvm_num_components(LLVMValueRef value)
108 {
109 LLVMTypeRef type = LLVMTypeOf(value);
110 unsigned num_components = LLVMGetTypeKind(type) == LLVMVectorTypeKind
111 ? LLVMGetVectorSize(type)
112 : 1;
113 return num_components;
114 }
115
116 LLVMValueRef
117 ac_llvm_extract_elem(struct ac_llvm_context *ac,
118 LLVMValueRef value,
119 int index)
120 {
121 if (LLVMGetTypeKind(LLVMTypeOf(value)) != LLVMVectorTypeKind) {
122 assert(index == 0);
123 return value;
124 }
125
126 return LLVMBuildExtractElement(ac->builder, value,
127 LLVMConstInt(ac->i32, index, false), "");
128 }
129
130 unsigned
131 ac_get_type_size(LLVMTypeRef type)
132 {
133 LLVMTypeKind kind = LLVMGetTypeKind(type);
134
135 switch (kind) {
136 case LLVMIntegerTypeKind:
137 return LLVMGetIntTypeWidth(type) / 8;
138 case LLVMFloatTypeKind:
139 return 4;
140 case LLVMDoubleTypeKind:
141 case LLVMPointerTypeKind:
142 return 8;
143 case LLVMVectorTypeKind:
144 return LLVMGetVectorSize(type) *
145 ac_get_type_size(LLVMGetElementType(type));
146 case LLVMArrayTypeKind:
147 return LLVMGetArrayLength(type) *
148 ac_get_type_size(LLVMGetElementType(type));
149 default:
150 assert(0);
151 return 0;
152 }
153 }
154
155 static LLVMTypeRef to_integer_type_scalar(struct ac_llvm_context *ctx, LLVMTypeRef t)
156 {
157 if (t == ctx->f16 || t == ctx->i16)
158 return ctx->i16;
159 else if (t == ctx->f32 || t == ctx->i32)
160 return ctx->i32;
161 else if (t == ctx->f64 || t == ctx->i64)
162 return ctx->i64;
163 else
164 unreachable("Unhandled integer size");
165 }
166
167 LLVMTypeRef
168 ac_to_integer_type(struct ac_llvm_context *ctx, LLVMTypeRef t)
169 {
170 if (LLVMGetTypeKind(t) == LLVMVectorTypeKind) {
171 LLVMTypeRef elem_type = LLVMGetElementType(t);
172 return LLVMVectorType(to_integer_type_scalar(ctx, elem_type),
173 LLVMGetVectorSize(t));
174 }
175 return to_integer_type_scalar(ctx, t);
176 }
177
178 LLVMValueRef
179 ac_to_integer(struct ac_llvm_context *ctx, LLVMValueRef v)
180 {
181 LLVMTypeRef type = LLVMTypeOf(v);
182 return LLVMBuildBitCast(ctx->builder, v, ac_to_integer_type(ctx, type), "");
183 }
184
185 static LLVMTypeRef to_float_type_scalar(struct ac_llvm_context *ctx, LLVMTypeRef t)
186 {
187 if (t == ctx->i16 || t == ctx->f16)
188 return ctx->f16;
189 else if (t == ctx->i32 || t == ctx->f32)
190 return ctx->f32;
191 else if (t == ctx->i64 || t == ctx->f64)
192 return ctx->f64;
193 else
194 unreachable("Unhandled float size");
195 }
196
197 LLVMTypeRef
198 ac_to_float_type(struct ac_llvm_context *ctx, LLVMTypeRef t)
199 {
200 if (LLVMGetTypeKind(t) == LLVMVectorTypeKind) {
201 LLVMTypeRef elem_type = LLVMGetElementType(t);
202 return LLVMVectorType(to_float_type_scalar(ctx, elem_type),
203 LLVMGetVectorSize(t));
204 }
205 return to_float_type_scalar(ctx, t);
206 }
207
208 LLVMValueRef
209 ac_to_float(struct ac_llvm_context *ctx, LLVMValueRef v)
210 {
211 LLVMTypeRef type = LLVMTypeOf(v);
212 return LLVMBuildBitCast(ctx->builder, v, ac_to_float_type(ctx, type), "");
213 }
214
215
216 LLVMValueRef
217 ac_build_intrinsic(struct ac_llvm_context *ctx, const char *name,
218 LLVMTypeRef return_type, LLVMValueRef *params,
219 unsigned param_count, unsigned attrib_mask)
220 {
221 LLVMValueRef function, call;
222 bool set_callsite_attrs = HAVE_LLVM >= 0x0400 &&
223 !(attrib_mask & AC_FUNC_ATTR_LEGACY);
224
225 function = LLVMGetNamedFunction(ctx->module, name);
226 if (!function) {
227 LLVMTypeRef param_types[32], function_type;
228 unsigned i;
229
230 assert(param_count <= 32);
231
232 for (i = 0; i < param_count; ++i) {
233 assert(params[i]);
234 param_types[i] = LLVMTypeOf(params[i]);
235 }
236 function_type =
237 LLVMFunctionType(return_type, param_types, param_count, 0);
238 function = LLVMAddFunction(ctx->module, name, function_type);
239
240 LLVMSetFunctionCallConv(function, LLVMCCallConv);
241 LLVMSetLinkage(function, LLVMExternalLinkage);
242
243 if (!set_callsite_attrs)
244 ac_add_func_attributes(ctx->context, function, attrib_mask);
245 }
246
247 call = LLVMBuildCall(ctx->builder, function, params, param_count, "");
248 if (set_callsite_attrs)
249 ac_add_func_attributes(ctx->context, call, attrib_mask);
250 return call;
251 }
252
253 /**
254 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
255 * intrinsic names).
256 */
257 void ac_build_type_name_for_intr(LLVMTypeRef type, char *buf, unsigned bufsize)
258 {
259 LLVMTypeRef elem_type = type;
260
261 assert(bufsize >= 8);
262
263 if (LLVMGetTypeKind(type) == LLVMVectorTypeKind) {
264 int ret = snprintf(buf, bufsize, "v%u",
265 LLVMGetVectorSize(type));
266 if (ret < 0) {
267 char *type_name = LLVMPrintTypeToString(type);
268 fprintf(stderr, "Error building type name for: %s\n",
269 type_name);
270 return;
271 }
272 elem_type = LLVMGetElementType(type);
273 buf += ret;
274 bufsize -= ret;
275 }
276 switch (LLVMGetTypeKind(elem_type)) {
277 default: break;
278 case LLVMIntegerTypeKind:
279 snprintf(buf, bufsize, "i%d", LLVMGetIntTypeWidth(elem_type));
280 break;
281 case LLVMFloatTypeKind:
282 snprintf(buf, bufsize, "f32");
283 break;
284 case LLVMDoubleTypeKind:
285 snprintf(buf, bufsize, "f64");
286 break;
287 }
288 }
289
290 /**
291 * Helper function that builds an LLVM IR PHI node and immediately adds
292 * incoming edges.
293 */
294 LLVMValueRef
295 ac_build_phi(struct ac_llvm_context *ctx, LLVMTypeRef type,
296 unsigned count_incoming, LLVMValueRef *values,
297 LLVMBasicBlockRef *blocks)
298 {
299 LLVMValueRef phi = LLVMBuildPhi(ctx->builder, type, "");
300 LLVMAddIncoming(phi, values, blocks, count_incoming);
301 return phi;
302 }
303
304 /* Prevent optimizations (at least of memory accesses) across the current
305 * point in the program by emitting empty inline assembly that is marked as
306 * having side effects.
307 *
308 * Optionally, a value can be passed through the inline assembly to prevent
309 * LLVM from hoisting calls to ReadNone functions.
310 */
311 void
312 ac_build_optimization_barrier(struct ac_llvm_context *ctx,
313 LLVMValueRef *pvgpr)
314 {
315 static int counter = 0;
316
317 LLVMBuilderRef builder = ctx->builder;
318 char code[16];
319
320 snprintf(code, sizeof(code), "; %d", p_atomic_inc_return(&counter));
321
322 if (!pvgpr) {
323 LLVMTypeRef ftype = LLVMFunctionType(ctx->voidt, NULL, 0, false);
324 LLVMValueRef inlineasm = LLVMConstInlineAsm(ftype, code, "", true, false);
325 LLVMBuildCall(builder, inlineasm, NULL, 0, "");
326 } else {
327 LLVMTypeRef ftype = LLVMFunctionType(ctx->i32, &ctx->i32, 1, false);
328 LLVMValueRef inlineasm = LLVMConstInlineAsm(ftype, code, "=v,0", true, false);
329 LLVMValueRef vgpr = *pvgpr;
330 LLVMTypeRef vgpr_type = LLVMTypeOf(vgpr);
331 unsigned vgpr_size = ac_get_type_size(vgpr_type);
332 LLVMValueRef vgpr0;
333
334 assert(vgpr_size % 4 == 0);
335
336 vgpr = LLVMBuildBitCast(builder, vgpr, LLVMVectorType(ctx->i32, vgpr_size / 4), "");
337 vgpr0 = LLVMBuildExtractElement(builder, vgpr, ctx->i32_0, "");
338 vgpr0 = LLVMBuildCall(builder, inlineasm, &vgpr0, 1, "");
339 vgpr = LLVMBuildInsertElement(builder, vgpr, vgpr0, ctx->i32_0, "");
340 vgpr = LLVMBuildBitCast(builder, vgpr, vgpr_type, "");
341
342 *pvgpr = vgpr;
343 }
344 }
345
346 LLVMValueRef
347 ac_build_ballot(struct ac_llvm_context *ctx,
348 LLVMValueRef value)
349 {
350 LLVMValueRef args[3] = {
351 value,
352 ctx->i32_0,
353 LLVMConstInt(ctx->i32, LLVMIntNE, 0)
354 };
355
356 /* We currently have no other way to prevent LLVM from lifting the icmp
357 * calls to a dominating basic block.
358 */
359 ac_build_optimization_barrier(ctx, &args[0]);
360
361 if (LLVMTypeOf(args[0]) != ctx->i32)
362 args[0] = LLVMBuildBitCast(ctx->builder, args[0], ctx->i32, "");
363
364 return ac_build_intrinsic(ctx,
365 "llvm.amdgcn.icmp.i32",
366 ctx->i64, args, 3,
367 AC_FUNC_ATTR_NOUNWIND |
368 AC_FUNC_ATTR_READNONE |
369 AC_FUNC_ATTR_CONVERGENT);
370 }
371
372 LLVMValueRef
373 ac_build_vote_all(struct ac_llvm_context *ctx, LLVMValueRef value)
374 {
375 LLVMValueRef active_set = ac_build_ballot(ctx, ctx->i32_1);
376 LLVMValueRef vote_set = ac_build_ballot(ctx, value);
377 return LLVMBuildICmp(ctx->builder, LLVMIntEQ, vote_set, active_set, "");
378 }
379
380 LLVMValueRef
381 ac_build_vote_any(struct ac_llvm_context *ctx, LLVMValueRef value)
382 {
383 LLVMValueRef vote_set = ac_build_ballot(ctx, value);
384 return LLVMBuildICmp(ctx->builder, LLVMIntNE, vote_set,
385 LLVMConstInt(ctx->i64, 0, 0), "");
386 }
387
388 LLVMValueRef
389 ac_build_vote_eq(struct ac_llvm_context *ctx, LLVMValueRef value)
390 {
391 LLVMValueRef active_set = ac_build_ballot(ctx, ctx->i32_1);
392 LLVMValueRef vote_set = ac_build_ballot(ctx, value);
393
394 LLVMValueRef all = LLVMBuildICmp(ctx->builder, LLVMIntEQ,
395 vote_set, active_set, "");
396 LLVMValueRef none = LLVMBuildICmp(ctx->builder, LLVMIntEQ,
397 vote_set,
398 LLVMConstInt(ctx->i64, 0, 0), "");
399 return LLVMBuildOr(ctx->builder, all, none, "");
400 }
401
402 LLVMValueRef
403 ac_build_varying_gather_values(struct ac_llvm_context *ctx, LLVMValueRef *values,
404 unsigned value_count, unsigned component)
405 {
406 LLVMValueRef vec = NULL;
407
408 if (value_count == 1) {
409 return values[component];
410 } else if (!value_count)
411 unreachable("value_count is 0");
412
413 for (unsigned i = component; i < value_count + component; i++) {
414 LLVMValueRef value = values[i];
415
416 if (i == component)
417 vec = LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value), value_count));
418 LLVMValueRef index = LLVMConstInt(ctx->i32, i - component, false);
419 vec = LLVMBuildInsertElement(ctx->builder, vec, value, index, "");
420 }
421 return vec;
422 }
423
424 LLVMValueRef
425 ac_build_gather_values_extended(struct ac_llvm_context *ctx,
426 LLVMValueRef *values,
427 unsigned value_count,
428 unsigned value_stride,
429 bool load,
430 bool always_vector)
431 {
432 LLVMBuilderRef builder = ctx->builder;
433 LLVMValueRef vec = NULL;
434 unsigned i;
435
436 if (value_count == 1 && !always_vector) {
437 if (load)
438 return LLVMBuildLoad(builder, values[0], "");
439 return values[0];
440 } else if (!value_count)
441 unreachable("value_count is 0");
442
443 for (i = 0; i < value_count; i++) {
444 LLVMValueRef value = values[i * value_stride];
445 if (load)
446 value = LLVMBuildLoad(builder, value, "");
447
448 if (!i)
449 vec = LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value), value_count));
450 LLVMValueRef index = LLVMConstInt(ctx->i32, i, false);
451 vec = LLVMBuildInsertElement(builder, vec, value, index, "");
452 }
453 return vec;
454 }
455
456 LLVMValueRef
457 ac_build_gather_values(struct ac_llvm_context *ctx,
458 LLVMValueRef *values,
459 unsigned value_count)
460 {
461 return ac_build_gather_values_extended(ctx, values, value_count, 1, false, false);
462 }
463
464 LLVMValueRef
465 ac_build_fdiv(struct ac_llvm_context *ctx,
466 LLVMValueRef num,
467 LLVMValueRef den)
468 {
469 LLVMValueRef ret = LLVMBuildFDiv(ctx->builder, num, den, "");
470
471 /* Use v_rcp_f32 instead of precise division. */
472 if (!LLVMIsConstant(ret))
473 LLVMSetMetadata(ret, ctx->fpmath_md_kind, ctx->fpmath_md_2p5_ulp);
474 return ret;
475 }
476
477 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
478 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
479 * already multiplied by two. id is the cube face number.
480 */
481 struct cube_selection_coords {
482 LLVMValueRef stc[2];
483 LLVMValueRef ma;
484 LLVMValueRef id;
485 };
486
487 static void
488 build_cube_intrinsic(struct ac_llvm_context *ctx,
489 LLVMValueRef in[3],
490 struct cube_selection_coords *out)
491 {
492 LLVMTypeRef f32 = ctx->f32;
493
494 out->stc[1] = ac_build_intrinsic(ctx, "llvm.amdgcn.cubetc",
495 f32, in, 3, AC_FUNC_ATTR_READNONE);
496 out->stc[0] = ac_build_intrinsic(ctx, "llvm.amdgcn.cubesc",
497 f32, in, 3, AC_FUNC_ATTR_READNONE);
498 out->ma = ac_build_intrinsic(ctx, "llvm.amdgcn.cubema",
499 f32, in, 3, AC_FUNC_ATTR_READNONE);
500 out->id = ac_build_intrinsic(ctx, "llvm.amdgcn.cubeid",
501 f32, in, 3, AC_FUNC_ATTR_READNONE);
502 }
503
504 /**
505 * Build a manual selection sequence for cube face sc/tc coordinates and
506 * major axis vector (multiplied by 2 for consistency) for the given
507 * vec3 \p coords, for the face implied by \p selcoords.
508 *
509 * For the major axis, we always adjust the sign to be in the direction of
510 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
511 * the selcoords major axis.
512 */
513 static void build_cube_select(struct ac_llvm_context *ctx,
514 const struct cube_selection_coords *selcoords,
515 const LLVMValueRef *coords,
516 LLVMValueRef *out_st,
517 LLVMValueRef *out_ma)
518 {
519 LLVMBuilderRef builder = ctx->builder;
520 LLVMTypeRef f32 = LLVMTypeOf(coords[0]);
521 LLVMValueRef is_ma_positive;
522 LLVMValueRef sgn_ma;
523 LLVMValueRef is_ma_z, is_not_ma_z;
524 LLVMValueRef is_ma_y;
525 LLVMValueRef is_ma_x;
526 LLVMValueRef sgn;
527 LLVMValueRef tmp;
528
529 is_ma_positive = LLVMBuildFCmp(builder, LLVMRealUGE,
530 selcoords->ma, LLVMConstReal(f32, 0.0), "");
531 sgn_ma = LLVMBuildSelect(builder, is_ma_positive,
532 LLVMConstReal(f32, 1.0), LLVMConstReal(f32, -1.0), "");
533
534 is_ma_z = LLVMBuildFCmp(builder, LLVMRealUGE, selcoords->id, LLVMConstReal(f32, 4.0), "");
535 is_not_ma_z = LLVMBuildNot(builder, is_ma_z, "");
536 is_ma_y = LLVMBuildAnd(builder, is_not_ma_z,
537 LLVMBuildFCmp(builder, LLVMRealUGE, selcoords->id, LLVMConstReal(f32, 2.0), ""), "");
538 is_ma_x = LLVMBuildAnd(builder, is_not_ma_z, LLVMBuildNot(builder, is_ma_y, ""), "");
539
540 /* Select sc */
541 tmp = LLVMBuildSelect(builder, is_ma_x, coords[2], coords[0], "");
542 sgn = LLVMBuildSelect(builder, is_ma_y, LLVMConstReal(f32, 1.0),
543 LLVMBuildSelect(builder, is_ma_z, sgn_ma,
544 LLVMBuildFNeg(builder, sgn_ma, ""), ""), "");
545 out_st[0] = LLVMBuildFMul(builder, tmp, sgn, "");
546
547 /* Select tc */
548 tmp = LLVMBuildSelect(builder, is_ma_y, coords[2], coords[1], "");
549 sgn = LLVMBuildSelect(builder, is_ma_y, sgn_ma,
550 LLVMConstReal(f32, -1.0), "");
551 out_st[1] = LLVMBuildFMul(builder, tmp, sgn, "");
552
553 /* Select ma */
554 tmp = LLVMBuildSelect(builder, is_ma_z, coords[2],
555 LLVMBuildSelect(builder, is_ma_y, coords[1], coords[0], ""), "");
556 tmp = ac_build_intrinsic(ctx, "llvm.fabs.f32",
557 ctx->f32, &tmp, 1, AC_FUNC_ATTR_READNONE);
558 *out_ma = LLVMBuildFMul(builder, tmp, LLVMConstReal(f32, 2.0), "");
559 }
560
561 void
562 ac_prepare_cube_coords(struct ac_llvm_context *ctx,
563 bool is_deriv, bool is_array, bool is_lod,
564 LLVMValueRef *coords_arg,
565 LLVMValueRef *derivs_arg)
566 {
567
568 LLVMBuilderRef builder = ctx->builder;
569 struct cube_selection_coords selcoords;
570 LLVMValueRef coords[3];
571 LLVMValueRef invma;
572
573 if (is_array && !is_lod) {
574 LLVMValueRef tmp = coords_arg[3];
575 tmp = ac_build_intrinsic(ctx, "llvm.rint.f32", ctx->f32, &tmp, 1, 0);
576
577 /* Section 8.9 (Texture Functions) of the GLSL 4.50 spec says:
578 *
579 * "For Array forms, the array layer used will be
580 *
581 * max(0, min(d−1, floor(layer+0.5)))
582 *
583 * where d is the depth of the texture array and layer
584 * comes from the component indicated in the tables below.
585 * Workaroudn for an issue where the layer is taken from a
586 * helper invocation which happens to fall on a different
587 * layer due to extrapolation."
588 *
589 * VI and earlier attempt to implement this in hardware by
590 * clamping the value of coords[2] = (8 * layer) + face.
591 * Unfortunately, this means that the we end up with the wrong
592 * face when clamping occurs.
593 *
594 * Clamp the layer earlier to work around the issue.
595 */
596 if (ctx->chip_class <= VI) {
597 LLVMValueRef ge0;
598 ge0 = LLVMBuildFCmp(builder, LLVMRealOGE, tmp, ctx->f32_0, "");
599 tmp = LLVMBuildSelect(builder, ge0, tmp, ctx->f32_0, "");
600 }
601
602 coords_arg[3] = tmp;
603 }
604
605 build_cube_intrinsic(ctx, coords_arg, &selcoords);
606
607 invma = ac_build_intrinsic(ctx, "llvm.fabs.f32",
608 ctx->f32, &selcoords.ma, 1, AC_FUNC_ATTR_READNONE);
609 invma = ac_build_fdiv(ctx, LLVMConstReal(ctx->f32, 1.0), invma);
610
611 for (int i = 0; i < 2; ++i)
612 coords[i] = LLVMBuildFMul(builder, selcoords.stc[i], invma, "");
613
614 coords[2] = selcoords.id;
615
616 if (is_deriv && derivs_arg) {
617 LLVMValueRef derivs[4];
618 int axis;
619
620 /* Convert cube derivatives to 2D derivatives. */
621 for (axis = 0; axis < 2; axis++) {
622 LLVMValueRef deriv_st[2];
623 LLVMValueRef deriv_ma;
624
625 /* Transform the derivative alongside the texture
626 * coordinate. Mathematically, the correct formula is
627 * as follows. Assume we're projecting onto the +Z face
628 * and denote by dx/dh the derivative of the (original)
629 * X texture coordinate with respect to horizontal
630 * window coordinates. The projection onto the +Z face
631 * plane is:
632 *
633 * f(x,z) = x/z
634 *
635 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
636 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
637 *
638 * This motivatives the implementation below.
639 *
640 * Whether this actually gives the expected results for
641 * apps that might feed in derivatives obtained via
642 * finite differences is anyone's guess. The OpenGL spec
643 * seems awfully quiet about how textureGrad for cube
644 * maps should be handled.
645 */
646 build_cube_select(ctx, &selcoords, &derivs_arg[axis * 3],
647 deriv_st, &deriv_ma);
648
649 deriv_ma = LLVMBuildFMul(builder, deriv_ma, invma, "");
650
651 for (int i = 0; i < 2; ++i)
652 derivs[axis * 2 + i] =
653 LLVMBuildFSub(builder,
654 LLVMBuildFMul(builder, deriv_st[i], invma, ""),
655 LLVMBuildFMul(builder, deriv_ma, coords[i], ""), "");
656 }
657
658 memcpy(derivs_arg, derivs, sizeof(derivs));
659 }
660
661 /* Shift the texture coordinate. This must be applied after the
662 * derivative calculation.
663 */
664 for (int i = 0; i < 2; ++i)
665 coords[i] = LLVMBuildFAdd(builder, coords[i], LLVMConstReal(ctx->f32, 1.5), "");
666
667 if (is_array) {
668 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
669 /* coords_arg.w component - array_index for cube arrays */
670 LLVMValueRef tmp = LLVMBuildFMul(ctx->builder, coords_arg[3], LLVMConstReal(ctx->f32, 8.0), "");
671 coords[2] = LLVMBuildFAdd(ctx->builder, tmp, coords[2], "");
672 }
673
674 memcpy(coords_arg, coords, sizeof(coords));
675 }
676
677
678 LLVMValueRef
679 ac_build_fs_interp(struct ac_llvm_context *ctx,
680 LLVMValueRef llvm_chan,
681 LLVMValueRef attr_number,
682 LLVMValueRef params,
683 LLVMValueRef i,
684 LLVMValueRef j)
685 {
686 LLVMValueRef args[5];
687 LLVMValueRef p1;
688
689 if (HAVE_LLVM < 0x0400) {
690 LLVMValueRef ij[2];
691 ij[0] = LLVMBuildBitCast(ctx->builder, i, ctx->i32, "");
692 ij[1] = LLVMBuildBitCast(ctx->builder, j, ctx->i32, "");
693
694 args[0] = llvm_chan;
695 args[1] = attr_number;
696 args[2] = params;
697 args[3] = ac_build_gather_values(ctx, ij, 2);
698 return ac_build_intrinsic(ctx, "llvm.SI.fs.interp",
699 ctx->f32, args, 4,
700 AC_FUNC_ATTR_READNONE);
701 }
702
703 args[0] = i;
704 args[1] = llvm_chan;
705 args[2] = attr_number;
706 args[3] = params;
707
708 p1 = ac_build_intrinsic(ctx, "llvm.amdgcn.interp.p1",
709 ctx->f32, args, 4, AC_FUNC_ATTR_READNONE);
710
711 args[0] = p1;
712 args[1] = j;
713 args[2] = llvm_chan;
714 args[3] = attr_number;
715 args[4] = params;
716
717 return ac_build_intrinsic(ctx, "llvm.amdgcn.interp.p2",
718 ctx->f32, args, 5, AC_FUNC_ATTR_READNONE);
719 }
720
721 LLVMValueRef
722 ac_build_fs_interp_mov(struct ac_llvm_context *ctx,
723 LLVMValueRef parameter,
724 LLVMValueRef llvm_chan,
725 LLVMValueRef attr_number,
726 LLVMValueRef params)
727 {
728 LLVMValueRef args[4];
729 if (HAVE_LLVM < 0x0400) {
730 args[0] = llvm_chan;
731 args[1] = attr_number;
732 args[2] = params;
733
734 return ac_build_intrinsic(ctx,
735 "llvm.SI.fs.constant",
736 ctx->f32, args, 3,
737 AC_FUNC_ATTR_READNONE);
738 }
739
740 args[0] = parameter;
741 args[1] = llvm_chan;
742 args[2] = attr_number;
743 args[3] = params;
744
745 return ac_build_intrinsic(ctx, "llvm.amdgcn.interp.mov",
746 ctx->f32, args, 4, AC_FUNC_ATTR_READNONE);
747 }
748
749 LLVMValueRef
750 ac_build_gep0(struct ac_llvm_context *ctx,
751 LLVMValueRef base_ptr,
752 LLVMValueRef index)
753 {
754 LLVMValueRef indices[2] = {
755 LLVMConstInt(ctx->i32, 0, 0),
756 index,
757 };
758 return LLVMBuildGEP(ctx->builder, base_ptr,
759 indices, 2, "");
760 }
761
762 void
763 ac_build_indexed_store(struct ac_llvm_context *ctx,
764 LLVMValueRef base_ptr, LLVMValueRef index,
765 LLVMValueRef value)
766 {
767 LLVMBuildStore(ctx->builder, value,
768 ac_build_gep0(ctx, base_ptr, index));
769 }
770
771 /**
772 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
773 * It's equivalent to doing a load from &base_ptr[index].
774 *
775 * \param base_ptr Where the array starts.
776 * \param index The element index into the array.
777 * \param uniform Whether the base_ptr and index can be assumed to be
778 * dynamically uniform (i.e. load to an SGPR)
779 * \param invariant Whether the load is invariant (no other opcodes affect it)
780 */
781 static LLVMValueRef
782 ac_build_load_custom(struct ac_llvm_context *ctx, LLVMValueRef base_ptr,
783 LLVMValueRef index, bool uniform, bool invariant)
784 {
785 LLVMValueRef pointer, result;
786
787 pointer = ac_build_gep0(ctx, base_ptr, index);
788 if (uniform)
789 LLVMSetMetadata(pointer, ctx->uniform_md_kind, ctx->empty_md);
790 result = LLVMBuildLoad(ctx->builder, pointer, "");
791 if (invariant)
792 LLVMSetMetadata(result, ctx->invariant_load_md_kind, ctx->empty_md);
793 return result;
794 }
795
796 LLVMValueRef ac_build_load(struct ac_llvm_context *ctx, LLVMValueRef base_ptr,
797 LLVMValueRef index)
798 {
799 return ac_build_load_custom(ctx, base_ptr, index, false, false);
800 }
801
802 LLVMValueRef ac_build_load_invariant(struct ac_llvm_context *ctx,
803 LLVMValueRef base_ptr, LLVMValueRef index)
804 {
805 return ac_build_load_custom(ctx, base_ptr, index, false, true);
806 }
807
808 LLVMValueRef ac_build_load_to_sgpr(struct ac_llvm_context *ctx,
809 LLVMValueRef base_ptr, LLVMValueRef index)
810 {
811 return ac_build_load_custom(ctx, base_ptr, index, true, true);
812 }
813
814 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
815 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
816 * or v4i32 (num_channels=3,4).
817 */
818 void
819 ac_build_buffer_store_dword(struct ac_llvm_context *ctx,
820 LLVMValueRef rsrc,
821 LLVMValueRef vdata,
822 unsigned num_channels,
823 LLVMValueRef voffset,
824 LLVMValueRef soffset,
825 unsigned inst_offset,
826 bool glc,
827 bool slc,
828 bool writeonly_memory,
829 bool swizzle_enable_hint)
830 {
831 /* SWIZZLE_ENABLE requires that soffset isn't folded into voffset
832 * (voffset is swizzled, but soffset isn't swizzled).
833 * llvm.amdgcn.buffer.store doesn't have a separate soffset parameter.
834 */
835 if (!swizzle_enable_hint) {
836 /* Split 3 channel stores, becase LLVM doesn't support 3-channel
837 * intrinsics. */
838 if (num_channels == 3) {
839 LLVMValueRef v[3], v01;
840
841 for (int i = 0; i < 3; i++) {
842 v[i] = LLVMBuildExtractElement(ctx->builder, vdata,
843 LLVMConstInt(ctx->i32, i, 0), "");
844 }
845 v01 = ac_build_gather_values(ctx, v, 2);
846
847 ac_build_buffer_store_dword(ctx, rsrc, v01, 2, voffset,
848 soffset, inst_offset, glc, slc,
849 writeonly_memory, swizzle_enable_hint);
850 ac_build_buffer_store_dword(ctx, rsrc, v[2], 1, voffset,
851 soffset, inst_offset + 8,
852 glc, slc,
853 writeonly_memory, swizzle_enable_hint);
854 return;
855 }
856
857 unsigned func = CLAMP(num_channels, 1, 3) - 1;
858 static const char *types[] = {"f32", "v2f32", "v4f32"};
859 char name[256];
860 LLVMValueRef offset = soffset;
861
862 if (inst_offset)
863 offset = LLVMBuildAdd(ctx->builder, offset,
864 LLVMConstInt(ctx->i32, inst_offset, 0), "");
865 if (voffset)
866 offset = LLVMBuildAdd(ctx->builder, offset, voffset, "");
867
868 LLVMValueRef args[] = {
869 ac_to_float(ctx, vdata),
870 LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, ""),
871 LLVMConstInt(ctx->i32, 0, 0),
872 offset,
873 LLVMConstInt(ctx->i1, glc, 0),
874 LLVMConstInt(ctx->i1, slc, 0),
875 };
876
877 snprintf(name, sizeof(name), "llvm.amdgcn.buffer.store.%s",
878 types[func]);
879
880 ac_build_intrinsic(ctx, name, ctx->voidt,
881 args, ARRAY_SIZE(args),
882 writeonly_memory ?
883 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY :
884 AC_FUNC_ATTR_WRITEONLY);
885 return;
886 }
887
888 static unsigned dfmt[] = {
889 V_008F0C_BUF_DATA_FORMAT_32,
890 V_008F0C_BUF_DATA_FORMAT_32_32,
891 V_008F0C_BUF_DATA_FORMAT_32_32_32,
892 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
893 };
894 assert(num_channels >= 1 && num_channels <= 4);
895
896 LLVMValueRef args[] = {
897 rsrc,
898 vdata,
899 LLVMConstInt(ctx->i32, num_channels, 0),
900 voffset ? voffset : LLVMGetUndef(ctx->i32),
901 soffset,
902 LLVMConstInt(ctx->i32, inst_offset, 0),
903 LLVMConstInt(ctx->i32, dfmt[num_channels - 1], 0),
904 LLVMConstInt(ctx->i32, V_008F0C_BUF_NUM_FORMAT_UINT, 0),
905 LLVMConstInt(ctx->i32, voffset != NULL, 0),
906 LLVMConstInt(ctx->i32, 0, 0), /* idxen */
907 LLVMConstInt(ctx->i32, glc, 0),
908 LLVMConstInt(ctx->i32, slc, 0),
909 LLVMConstInt(ctx->i32, 0, 0), /* tfe*/
910 };
911
912 /* The instruction offset field has 12 bits */
913 assert(voffset || inst_offset < (1 << 12));
914
915 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
916 unsigned func = CLAMP(num_channels, 1, 3) - 1;
917 const char *types[] = {"i32", "v2i32", "v4i32"};
918 char name[256];
919 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
920
921 ac_build_intrinsic(ctx, name, ctx->voidt,
922 args, ARRAY_SIZE(args),
923 AC_FUNC_ATTR_LEGACY);
924 }
925
926 LLVMValueRef
927 ac_build_buffer_load(struct ac_llvm_context *ctx,
928 LLVMValueRef rsrc,
929 int num_channels,
930 LLVMValueRef vindex,
931 LLVMValueRef voffset,
932 LLVMValueRef soffset,
933 unsigned inst_offset,
934 unsigned glc,
935 unsigned slc,
936 bool can_speculate,
937 bool allow_smem)
938 {
939 LLVMValueRef offset = LLVMConstInt(ctx->i32, inst_offset, 0);
940 if (voffset)
941 offset = LLVMBuildAdd(ctx->builder, offset, voffset, "");
942 if (soffset)
943 offset = LLVMBuildAdd(ctx->builder, offset, soffset, "");
944
945 /* TODO: VI and later generations can use SMEM with GLC=1.*/
946 if (allow_smem && !glc && !slc) {
947 assert(vindex == NULL);
948
949 LLVMValueRef result[4];
950
951 for (int i = 0; i < num_channels; i++) {
952 if (i) {
953 offset = LLVMBuildAdd(ctx->builder, offset,
954 LLVMConstInt(ctx->i32, 4, 0), "");
955 }
956 LLVMValueRef args[2] = {rsrc, offset};
957 result[i] = ac_build_intrinsic(ctx, "llvm.SI.load.const.v4i32",
958 ctx->f32, args, 2,
959 AC_FUNC_ATTR_READNONE |
960 AC_FUNC_ATTR_LEGACY);
961 }
962 if (num_channels == 1)
963 return result[0];
964
965 if (num_channels == 3)
966 result[num_channels++] = LLVMGetUndef(ctx->f32);
967 return ac_build_gather_values(ctx, result, num_channels);
968 }
969
970 unsigned func = CLAMP(num_channels, 1, 3) - 1;
971
972 LLVMValueRef args[] = {
973 LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, ""),
974 vindex ? vindex : LLVMConstInt(ctx->i32, 0, 0),
975 offset,
976 LLVMConstInt(ctx->i1, glc, 0),
977 LLVMConstInt(ctx->i1, slc, 0)
978 };
979
980 LLVMTypeRef types[] = {ctx->f32, LLVMVectorType(ctx->f32, 2),
981 ctx->v4f32};
982 const char *type_names[] = {"f32", "v2f32", "v4f32"};
983 char name[256];
984
985 snprintf(name, sizeof(name), "llvm.amdgcn.buffer.load.%s",
986 type_names[func]);
987
988 return ac_build_intrinsic(ctx, name, types[func], args,
989 ARRAY_SIZE(args),
990 ac_get_load_intr_attribs(can_speculate));
991 }
992
993 LLVMValueRef ac_build_buffer_load_format(struct ac_llvm_context *ctx,
994 LLVMValueRef rsrc,
995 LLVMValueRef vindex,
996 LLVMValueRef voffset,
997 bool can_speculate)
998 {
999 LLVMValueRef args [] = {
1000 LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, ""),
1001 vindex,
1002 voffset,
1003 ctx->i1false, /* glc */
1004 ctx->i1false, /* slc */
1005 };
1006
1007 return ac_build_intrinsic(ctx,
1008 "llvm.amdgcn.buffer.load.format.v4f32",
1009 ctx->v4f32, args, ARRAY_SIZE(args),
1010 ac_get_load_intr_attribs(can_speculate));
1011 }
1012
1013 /**
1014 * Set range metadata on an instruction. This can only be used on load and
1015 * call instructions. If you know an instruction can only produce the values
1016 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
1017 * \p lo is the minimum value inclusive.
1018 * \p hi is the maximum value exclusive.
1019 */
1020 static void set_range_metadata(struct ac_llvm_context *ctx,
1021 LLVMValueRef value, unsigned lo, unsigned hi)
1022 {
1023 LLVMValueRef range_md, md_args[2];
1024 LLVMTypeRef type = LLVMTypeOf(value);
1025 LLVMContextRef context = LLVMGetTypeContext(type);
1026
1027 md_args[0] = LLVMConstInt(type, lo, false);
1028 md_args[1] = LLVMConstInt(type, hi, false);
1029 range_md = LLVMMDNodeInContext(context, md_args, 2);
1030 LLVMSetMetadata(value, ctx->range_md_kind, range_md);
1031 }
1032
1033 LLVMValueRef
1034 ac_get_thread_id(struct ac_llvm_context *ctx)
1035 {
1036 LLVMValueRef tid;
1037
1038 LLVMValueRef tid_args[2];
1039 tid_args[0] = LLVMConstInt(ctx->i32, 0xffffffff, false);
1040 tid_args[1] = LLVMConstInt(ctx->i32, 0, false);
1041 tid_args[1] = ac_build_intrinsic(ctx,
1042 "llvm.amdgcn.mbcnt.lo", ctx->i32,
1043 tid_args, 2, AC_FUNC_ATTR_READNONE);
1044
1045 tid = ac_build_intrinsic(ctx, "llvm.amdgcn.mbcnt.hi",
1046 ctx->i32, tid_args,
1047 2, AC_FUNC_ATTR_READNONE);
1048 set_range_metadata(ctx, tid, 0, 64);
1049 return tid;
1050 }
1051
1052 /*
1053 * SI implements derivatives using the local data store (LDS)
1054 * All writes to the LDS happen in all executing threads at
1055 * the same time. TID is the Thread ID for the current
1056 * thread and is a value between 0 and 63, representing
1057 * the thread's position in the wavefront.
1058 *
1059 * For the pixel shader threads are grouped into quads of four pixels.
1060 * The TIDs of the pixels of a quad are:
1061 *
1062 * +------+------+
1063 * |4n + 0|4n + 1|
1064 * +------+------+
1065 * |4n + 2|4n + 3|
1066 * +------+------+
1067 *
1068 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
1069 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
1070 * the current pixel's column, and masking with 0xfffffffe yields the TID
1071 * of the left pixel of the current pixel's row.
1072 *
1073 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
1074 * adding 2 yields the TID of the pixel below the top pixel.
1075 */
1076 LLVMValueRef
1077 ac_build_ddxy(struct ac_llvm_context *ctx,
1078 uint32_t mask,
1079 int idx,
1080 LLVMValueRef val)
1081 {
1082 LLVMValueRef tl, trbl, args[2];
1083 LLVMValueRef result;
1084
1085 if (ctx->chip_class >= VI) {
1086 LLVMValueRef thread_id, tl_tid, trbl_tid;
1087 thread_id = ac_get_thread_id(ctx);
1088
1089 tl_tid = LLVMBuildAnd(ctx->builder, thread_id,
1090 LLVMConstInt(ctx->i32, mask, false), "");
1091
1092 trbl_tid = LLVMBuildAdd(ctx->builder, tl_tid,
1093 LLVMConstInt(ctx->i32, idx, false), "");
1094
1095 args[0] = LLVMBuildMul(ctx->builder, tl_tid,
1096 LLVMConstInt(ctx->i32, 4, false), "");
1097 args[1] = val;
1098 tl = ac_build_intrinsic(ctx,
1099 "llvm.amdgcn.ds.bpermute", ctx->i32,
1100 args, 2,
1101 AC_FUNC_ATTR_READNONE |
1102 AC_FUNC_ATTR_CONVERGENT);
1103
1104 args[0] = LLVMBuildMul(ctx->builder, trbl_tid,
1105 LLVMConstInt(ctx->i32, 4, false), "");
1106 trbl = ac_build_intrinsic(ctx,
1107 "llvm.amdgcn.ds.bpermute", ctx->i32,
1108 args, 2,
1109 AC_FUNC_ATTR_READNONE |
1110 AC_FUNC_ATTR_CONVERGENT);
1111 } else {
1112 uint32_t masks[2] = {};
1113
1114 switch (mask) {
1115 case AC_TID_MASK_TOP_LEFT:
1116 masks[0] = 0x8000;
1117 if (idx == 1)
1118 masks[1] = 0x8055;
1119 else
1120 masks[1] = 0x80aa;
1121
1122 break;
1123 case AC_TID_MASK_TOP:
1124 masks[0] = 0x8044;
1125 masks[1] = 0x80ee;
1126 break;
1127 case AC_TID_MASK_LEFT:
1128 masks[0] = 0x80a0;
1129 masks[1] = 0x80f5;
1130 break;
1131 default:
1132 assert(0);
1133 }
1134
1135 args[0] = val;
1136 args[1] = LLVMConstInt(ctx->i32, masks[0], false);
1137
1138 tl = ac_build_intrinsic(ctx,
1139 "llvm.amdgcn.ds.swizzle", ctx->i32,
1140 args, 2,
1141 AC_FUNC_ATTR_READNONE |
1142 AC_FUNC_ATTR_CONVERGENT);
1143
1144 args[1] = LLVMConstInt(ctx->i32, masks[1], false);
1145 trbl = ac_build_intrinsic(ctx,
1146 "llvm.amdgcn.ds.swizzle", ctx->i32,
1147 args, 2,
1148 AC_FUNC_ATTR_READNONE |
1149 AC_FUNC_ATTR_CONVERGENT);
1150 }
1151
1152 tl = LLVMBuildBitCast(ctx->builder, tl, ctx->f32, "");
1153 trbl = LLVMBuildBitCast(ctx->builder, trbl, ctx->f32, "");
1154 result = LLVMBuildFSub(ctx->builder, trbl, tl, "");
1155 return result;
1156 }
1157
1158 void
1159 ac_build_sendmsg(struct ac_llvm_context *ctx,
1160 uint32_t msg,
1161 LLVMValueRef wave_id)
1162 {
1163 LLVMValueRef args[2];
1164 const char *intr_name = (HAVE_LLVM < 0x0400) ? "llvm.SI.sendmsg" : "llvm.amdgcn.s.sendmsg";
1165 args[0] = LLVMConstInt(ctx->i32, msg, false);
1166 args[1] = wave_id;
1167 ac_build_intrinsic(ctx, intr_name, ctx->voidt, args, 2, 0);
1168 }
1169
1170 LLVMValueRef
1171 ac_build_imsb(struct ac_llvm_context *ctx,
1172 LLVMValueRef arg,
1173 LLVMTypeRef dst_type)
1174 {
1175 const char *intr_name = (HAVE_LLVM < 0x0400) ? "llvm.AMDGPU.flbit.i32" :
1176 "llvm.amdgcn.sffbh.i32";
1177 LLVMValueRef msb = ac_build_intrinsic(ctx, intr_name,
1178 dst_type, &arg, 1,
1179 AC_FUNC_ATTR_READNONE);
1180
1181 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
1182 * the index from LSB. Invert it by doing "31 - msb". */
1183 msb = LLVMBuildSub(ctx->builder, LLVMConstInt(ctx->i32, 31, false),
1184 msb, "");
1185
1186 LLVMValueRef all_ones = LLVMConstInt(ctx->i32, -1, true);
1187 LLVMValueRef cond = LLVMBuildOr(ctx->builder,
1188 LLVMBuildICmp(ctx->builder, LLVMIntEQ,
1189 arg, LLVMConstInt(ctx->i32, 0, 0), ""),
1190 LLVMBuildICmp(ctx->builder, LLVMIntEQ,
1191 arg, all_ones, ""), "");
1192
1193 return LLVMBuildSelect(ctx->builder, cond, all_ones, msb, "");
1194 }
1195
1196 LLVMValueRef
1197 ac_build_umsb(struct ac_llvm_context *ctx,
1198 LLVMValueRef arg,
1199 LLVMTypeRef dst_type)
1200 {
1201 LLVMValueRef args[2] = {
1202 arg,
1203 ctx->i1true,
1204 };
1205 LLVMValueRef msb = ac_build_intrinsic(ctx, "llvm.ctlz.i32",
1206 dst_type, args, ARRAY_SIZE(args),
1207 AC_FUNC_ATTR_READNONE);
1208
1209 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
1210 * the index from LSB. Invert it by doing "31 - msb". */
1211 msb = LLVMBuildSub(ctx->builder, LLVMConstInt(ctx->i32, 31, false),
1212 msb, "");
1213
1214 /* check for zero */
1215 return LLVMBuildSelect(ctx->builder,
1216 LLVMBuildICmp(ctx->builder, LLVMIntEQ, arg,
1217 LLVMConstInt(ctx->i32, 0, 0), ""),
1218 LLVMConstInt(ctx->i32, -1, true), msb, "");
1219 }
1220
1221 LLVMValueRef ac_build_fmin(struct ac_llvm_context *ctx, LLVMValueRef a,
1222 LLVMValueRef b)
1223 {
1224 LLVMValueRef args[2] = {a, b};
1225 return ac_build_intrinsic(ctx, "llvm.minnum.f32", ctx->f32, args, 2,
1226 AC_FUNC_ATTR_READNONE);
1227 }
1228
1229 LLVMValueRef ac_build_fmax(struct ac_llvm_context *ctx, LLVMValueRef a,
1230 LLVMValueRef b)
1231 {
1232 LLVMValueRef args[2] = {a, b};
1233 return ac_build_intrinsic(ctx, "llvm.maxnum.f32", ctx->f32, args, 2,
1234 AC_FUNC_ATTR_READNONE);
1235 }
1236
1237 LLVMValueRef ac_build_umin(struct ac_llvm_context *ctx, LLVMValueRef a,
1238 LLVMValueRef b)
1239 {
1240 LLVMValueRef cmp = LLVMBuildICmp(ctx->builder, LLVMIntULE, a, b, "");
1241 return LLVMBuildSelect(ctx->builder, cmp, a, b, "");
1242 }
1243
1244 LLVMValueRef ac_build_clamp(struct ac_llvm_context *ctx, LLVMValueRef value)
1245 {
1246 if (HAVE_LLVM >= 0x0500) {
1247 return ac_build_fmin(ctx, ac_build_fmax(ctx, value, ctx->f32_0),
1248 ctx->f32_1);
1249 }
1250
1251 LLVMValueRef args[3] = {
1252 value,
1253 LLVMConstReal(ctx->f32, 0),
1254 LLVMConstReal(ctx->f32, 1),
1255 };
1256
1257 return ac_build_intrinsic(ctx, "llvm.AMDGPU.clamp.", ctx->f32, args, 3,
1258 AC_FUNC_ATTR_READNONE |
1259 AC_FUNC_ATTR_LEGACY);
1260 }
1261
1262 void ac_build_export(struct ac_llvm_context *ctx, struct ac_export_args *a)
1263 {
1264 LLVMValueRef args[9];
1265
1266 if (HAVE_LLVM >= 0x0500) {
1267 args[0] = LLVMConstInt(ctx->i32, a->target, 0);
1268 args[1] = LLVMConstInt(ctx->i32, a->enabled_channels, 0);
1269
1270 if (a->compr) {
1271 LLVMTypeRef i16 = LLVMInt16TypeInContext(ctx->context);
1272 LLVMTypeRef v2i16 = LLVMVectorType(i16, 2);
1273
1274 args[2] = LLVMBuildBitCast(ctx->builder, a->out[0],
1275 v2i16, "");
1276 args[3] = LLVMBuildBitCast(ctx->builder, a->out[1],
1277 v2i16, "");
1278 args[4] = LLVMConstInt(ctx->i1, a->done, 0);
1279 args[5] = LLVMConstInt(ctx->i1, a->valid_mask, 0);
1280
1281 ac_build_intrinsic(ctx, "llvm.amdgcn.exp.compr.v2i16",
1282 ctx->voidt, args, 6, 0);
1283 } else {
1284 args[2] = a->out[0];
1285 args[3] = a->out[1];
1286 args[4] = a->out[2];
1287 args[5] = a->out[3];
1288 args[6] = LLVMConstInt(ctx->i1, a->done, 0);
1289 args[7] = LLVMConstInt(ctx->i1, a->valid_mask, 0);
1290
1291 ac_build_intrinsic(ctx, "llvm.amdgcn.exp.f32",
1292 ctx->voidt, args, 8, 0);
1293 }
1294 return;
1295 }
1296
1297 args[0] = LLVMConstInt(ctx->i32, a->enabled_channels, 0);
1298 args[1] = LLVMConstInt(ctx->i32, a->valid_mask, 0);
1299 args[2] = LLVMConstInt(ctx->i32, a->done, 0);
1300 args[3] = LLVMConstInt(ctx->i32, a->target, 0);
1301 args[4] = LLVMConstInt(ctx->i32, a->compr, 0);
1302 memcpy(args + 5, a->out, sizeof(a->out[0]) * 4);
1303
1304 ac_build_intrinsic(ctx, "llvm.SI.export", ctx->voidt, args, 9,
1305 AC_FUNC_ATTR_LEGACY);
1306 }
1307
1308 LLVMValueRef ac_build_image_opcode(struct ac_llvm_context *ctx,
1309 struct ac_image_args *a)
1310 {
1311 LLVMTypeRef dst_type;
1312 LLVMValueRef args[11];
1313 unsigned num_args = 0;
1314 const char *name = NULL;
1315 char intr_name[128], type[64];
1316
1317 if (HAVE_LLVM >= 0x0400) {
1318 bool sample = a->opcode == ac_image_sample ||
1319 a->opcode == ac_image_gather4 ||
1320 a->opcode == ac_image_get_lod;
1321
1322 if (sample)
1323 args[num_args++] = ac_to_float(ctx, a->addr);
1324 else
1325 args[num_args++] = a->addr;
1326
1327 args[num_args++] = a->resource;
1328 if (sample)
1329 args[num_args++] = a->sampler;
1330 args[num_args++] = LLVMConstInt(ctx->i32, a->dmask, 0);
1331 if (sample)
1332 args[num_args++] = LLVMConstInt(ctx->i1, a->unorm, 0);
1333 args[num_args++] = ctx->i1false; /* glc */
1334 args[num_args++] = ctx->i1false; /* slc */
1335 args[num_args++] = ctx->i1false; /* lwe */
1336 args[num_args++] = LLVMConstInt(ctx->i1, a->da, 0);
1337
1338 switch (a->opcode) {
1339 case ac_image_sample:
1340 name = "llvm.amdgcn.image.sample";
1341 break;
1342 case ac_image_gather4:
1343 name = "llvm.amdgcn.image.gather4";
1344 break;
1345 case ac_image_load:
1346 name = "llvm.amdgcn.image.load";
1347 break;
1348 case ac_image_load_mip:
1349 name = "llvm.amdgcn.image.load.mip";
1350 break;
1351 case ac_image_get_lod:
1352 name = "llvm.amdgcn.image.getlod";
1353 break;
1354 case ac_image_get_resinfo:
1355 name = "llvm.amdgcn.image.getresinfo";
1356 break;
1357 default:
1358 unreachable("invalid image opcode");
1359 }
1360
1361 ac_build_type_name_for_intr(LLVMTypeOf(args[0]), type,
1362 sizeof(type));
1363
1364 snprintf(intr_name, sizeof(intr_name), "%s%s%s%s.v4f32.%s.v8i32",
1365 name,
1366 a->compare ? ".c" : "",
1367 a->bias ? ".b" :
1368 a->lod ? ".l" :
1369 a->deriv ? ".d" :
1370 a->level_zero ? ".lz" : "",
1371 a->offset ? ".o" : "",
1372 type);
1373
1374 LLVMValueRef result =
1375 ac_build_intrinsic(ctx, intr_name,
1376 ctx->v4f32, args, num_args,
1377 AC_FUNC_ATTR_READNONE);
1378 if (!sample) {
1379 result = LLVMBuildBitCast(ctx->builder, result,
1380 ctx->v4i32, "");
1381 }
1382 return result;
1383 }
1384
1385 args[num_args++] = a->addr;
1386 args[num_args++] = a->resource;
1387
1388 if (a->opcode == ac_image_load ||
1389 a->opcode == ac_image_load_mip ||
1390 a->opcode == ac_image_get_resinfo) {
1391 dst_type = ctx->v4i32;
1392 } else {
1393 dst_type = ctx->v4f32;
1394 args[num_args++] = a->sampler;
1395 }
1396
1397 args[num_args++] = LLVMConstInt(ctx->i32, a->dmask, 0);
1398 args[num_args++] = LLVMConstInt(ctx->i32, a->unorm, 0);
1399 args[num_args++] = LLVMConstInt(ctx->i32, 0, 0); /* r128 */
1400 args[num_args++] = LLVMConstInt(ctx->i32, a->da, 0);
1401 args[num_args++] = LLVMConstInt(ctx->i32, 0, 0); /* glc */
1402 args[num_args++] = LLVMConstInt(ctx->i32, 0, 0); /* slc */
1403 args[num_args++] = LLVMConstInt(ctx->i32, 0, 0); /* tfe */
1404 args[num_args++] = LLVMConstInt(ctx->i32, 0, 0); /* lwe */
1405
1406 switch (a->opcode) {
1407 case ac_image_sample:
1408 name = "llvm.SI.image.sample";
1409 break;
1410 case ac_image_gather4:
1411 name = "llvm.SI.gather4";
1412 break;
1413 case ac_image_load:
1414 name = "llvm.SI.image.load";
1415 break;
1416 case ac_image_load_mip:
1417 name = "llvm.SI.image.load.mip";
1418 break;
1419 case ac_image_get_lod:
1420 name = "llvm.SI.getlod";
1421 break;
1422 case ac_image_get_resinfo:
1423 name = "llvm.SI.getresinfo";
1424 break;
1425 }
1426
1427 ac_build_type_name_for_intr(LLVMTypeOf(a->addr), type, sizeof(type));
1428 snprintf(intr_name, sizeof(intr_name), "%s%s%s%s.%s",
1429 name,
1430 a->compare ? ".c" : "",
1431 a->bias ? ".b" :
1432 a->lod ? ".l" :
1433 a->deriv ? ".d" :
1434 a->level_zero ? ".lz" : "",
1435 a->offset ? ".o" : "",
1436 type);
1437
1438 return ac_build_intrinsic(ctx, intr_name,
1439 dst_type, args, num_args,
1440 AC_FUNC_ATTR_READNONE |
1441 AC_FUNC_ATTR_LEGACY);
1442 }
1443
1444 LLVMValueRef ac_build_cvt_pkrtz_f16(struct ac_llvm_context *ctx,
1445 LLVMValueRef args[2])
1446 {
1447 if (HAVE_LLVM >= 0x0500) {
1448 LLVMTypeRef v2f16 =
1449 LLVMVectorType(LLVMHalfTypeInContext(ctx->context), 2);
1450 LLVMValueRef res =
1451 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pkrtz",
1452 v2f16, args, 2,
1453 AC_FUNC_ATTR_READNONE);
1454 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
1455 }
1456
1457 return ac_build_intrinsic(ctx, "llvm.SI.packf16", ctx->i32, args, 2,
1458 AC_FUNC_ATTR_READNONE |
1459 AC_FUNC_ATTR_LEGACY);
1460 }
1461
1462 LLVMValueRef ac_build_wqm_vote(struct ac_llvm_context *ctx, LLVMValueRef i1)
1463 {
1464 assert(HAVE_LLVM >= 0x0600);
1465 return ac_build_intrinsic(ctx, "llvm.amdgcn.wqm.vote", ctx->i1,
1466 &i1, 1, AC_FUNC_ATTR_READNONE);
1467 }
1468
1469 void ac_build_kill_if_false(struct ac_llvm_context *ctx, LLVMValueRef i1)
1470 {
1471 if (HAVE_LLVM >= 0x0600) {
1472 ac_build_intrinsic(ctx, "llvm.amdgcn.kill", ctx->voidt,
1473 &i1, 1, 0);
1474 return;
1475 }
1476
1477 LLVMValueRef value = LLVMBuildSelect(ctx->builder, i1,
1478 LLVMConstReal(ctx->f32, 1),
1479 LLVMConstReal(ctx->f32, -1), "");
1480 ac_build_intrinsic(ctx, "llvm.AMDGPU.kill", ctx->voidt,
1481 &value, 1, AC_FUNC_ATTR_LEGACY);
1482 }
1483
1484 LLVMValueRef ac_build_bfe(struct ac_llvm_context *ctx, LLVMValueRef input,
1485 LLVMValueRef offset, LLVMValueRef width,
1486 bool is_signed)
1487 {
1488 LLVMValueRef args[] = {
1489 input,
1490 offset,
1491 width,
1492 };
1493
1494 if (HAVE_LLVM >= 0x0500) {
1495 return ac_build_intrinsic(ctx,
1496 is_signed ? "llvm.amdgcn.sbfe.i32" :
1497 "llvm.amdgcn.ubfe.i32",
1498 ctx->i32, args, 3,
1499 AC_FUNC_ATTR_READNONE);
1500 }
1501
1502 return ac_build_intrinsic(ctx,
1503 is_signed ? "llvm.AMDGPU.bfe.i32" :
1504 "llvm.AMDGPU.bfe.u32",
1505 ctx->i32, args, 3,
1506 AC_FUNC_ATTR_READNONE |
1507 AC_FUNC_ATTR_LEGACY);
1508 }
1509
1510 void ac_build_waitcnt(struct ac_llvm_context *ctx, unsigned simm16)
1511 {
1512 LLVMValueRef args[1] = {
1513 LLVMConstInt(ctx->i32, simm16, false),
1514 };
1515 ac_build_intrinsic(ctx, "llvm.amdgcn.s.waitcnt",
1516 ctx->voidt, args, 1, 0);
1517 }
1518
1519 void ac_get_image_intr_name(const char *base_name,
1520 LLVMTypeRef data_type,
1521 LLVMTypeRef coords_type,
1522 LLVMTypeRef rsrc_type,
1523 char *out_name, unsigned out_len)
1524 {
1525 char coords_type_name[8];
1526
1527 ac_build_type_name_for_intr(coords_type, coords_type_name,
1528 sizeof(coords_type_name));
1529
1530 if (HAVE_LLVM <= 0x0309) {
1531 snprintf(out_name, out_len, "%s.%s", base_name, coords_type_name);
1532 } else {
1533 char data_type_name[8];
1534 char rsrc_type_name[8];
1535
1536 ac_build_type_name_for_intr(data_type, data_type_name,
1537 sizeof(data_type_name));
1538 ac_build_type_name_for_intr(rsrc_type, rsrc_type_name,
1539 sizeof(rsrc_type_name));
1540 snprintf(out_name, out_len, "%s.%s.%s.%s", base_name,
1541 data_type_name, coords_type_name, rsrc_type_name);
1542 }
1543 }
1544
1545 #define AC_EXP_TARGET (HAVE_LLVM >= 0x0500 ? 0 : 3)
1546 #define AC_EXP_OUT0 (HAVE_LLVM >= 0x0500 ? 2 : 5)
1547
1548 enum ac_ir_type {
1549 AC_IR_UNDEF,
1550 AC_IR_CONST,
1551 AC_IR_VALUE,
1552 };
1553
1554 struct ac_vs_exp_chan
1555 {
1556 LLVMValueRef value;
1557 float const_float;
1558 enum ac_ir_type type;
1559 };
1560
1561 struct ac_vs_exp_inst {
1562 unsigned offset;
1563 LLVMValueRef inst;
1564 struct ac_vs_exp_chan chan[4];
1565 };
1566
1567 struct ac_vs_exports {
1568 unsigned num;
1569 struct ac_vs_exp_inst exp[VARYING_SLOT_MAX];
1570 };
1571
1572 /* Return true if the PARAM export has been eliminated. */
1573 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset,
1574 uint32_t num_outputs,
1575 struct ac_vs_exp_inst *exp)
1576 {
1577 unsigned i, default_val; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
1578 bool is_zero[4] = {}, is_one[4] = {};
1579
1580 for (i = 0; i < 4; i++) {
1581 /* It's a constant expression. Undef outputs are eliminated too. */
1582 if (exp->chan[i].type == AC_IR_UNDEF) {
1583 is_zero[i] = true;
1584 is_one[i] = true;
1585 } else if (exp->chan[i].type == AC_IR_CONST) {
1586 if (exp->chan[i].const_float == 0)
1587 is_zero[i] = true;
1588 else if (exp->chan[i].const_float == 1)
1589 is_one[i] = true;
1590 else
1591 return false; /* other constant */
1592 } else
1593 return false;
1594 }
1595
1596 /* Only certain combinations of 0 and 1 can be eliminated. */
1597 if (is_zero[0] && is_zero[1] && is_zero[2])
1598 default_val = is_zero[3] ? 0 : 1;
1599 else if (is_one[0] && is_one[1] && is_one[2])
1600 default_val = is_zero[3] ? 2 : 3;
1601 else
1602 return false;
1603
1604 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
1605 LLVMInstructionEraseFromParent(exp->inst);
1606
1607 /* Change OFFSET to DEFAULT_VAL. */
1608 for (i = 0; i < num_outputs; i++) {
1609 if (vs_output_param_offset[i] == exp->offset) {
1610 vs_output_param_offset[i] =
1611 AC_EXP_PARAM_DEFAULT_VAL_0000 + default_val;
1612 break;
1613 }
1614 }
1615 return true;
1616 }
1617
1618 static bool ac_eliminate_duplicated_output(uint8_t *vs_output_param_offset,
1619 uint32_t num_outputs,
1620 struct ac_vs_exports *processed,
1621 struct ac_vs_exp_inst *exp)
1622 {
1623 unsigned p, copy_back_channels = 0;
1624
1625 /* See if the output is already in the list of processed outputs.
1626 * The LLVMValueRef comparison relies on SSA.
1627 */
1628 for (p = 0; p < processed->num; p++) {
1629 bool different = false;
1630
1631 for (unsigned j = 0; j < 4; j++) {
1632 struct ac_vs_exp_chan *c1 = &processed->exp[p].chan[j];
1633 struct ac_vs_exp_chan *c2 = &exp->chan[j];
1634
1635 /* Treat undef as a match. */
1636 if (c2->type == AC_IR_UNDEF)
1637 continue;
1638
1639 /* If c1 is undef but c2 isn't, we can copy c2 to c1
1640 * and consider the instruction duplicated.
1641 */
1642 if (c1->type == AC_IR_UNDEF) {
1643 copy_back_channels |= 1 << j;
1644 continue;
1645 }
1646
1647 /* Test whether the channels are not equal. */
1648 if (c1->type != c2->type ||
1649 (c1->type == AC_IR_CONST &&
1650 c1->const_float != c2->const_float) ||
1651 (c1->type == AC_IR_VALUE &&
1652 c1->value != c2->value)) {
1653 different = true;
1654 break;
1655 }
1656 }
1657 if (!different)
1658 break;
1659
1660 copy_back_channels = 0;
1661 }
1662 if (p == processed->num)
1663 return false;
1664
1665 /* If a match was found, but the matching export has undef where the new
1666 * one has a normal value, copy the normal value to the undef channel.
1667 */
1668 struct ac_vs_exp_inst *match = &processed->exp[p];
1669
1670 while (copy_back_channels) {
1671 unsigned chan = u_bit_scan(&copy_back_channels);
1672
1673 assert(match->chan[chan].type == AC_IR_UNDEF);
1674 LLVMSetOperand(match->inst, AC_EXP_OUT0 + chan,
1675 exp->chan[chan].value);
1676 match->chan[chan] = exp->chan[chan];
1677 }
1678
1679 /* The PARAM export is duplicated. Kill it. */
1680 LLVMInstructionEraseFromParent(exp->inst);
1681
1682 /* Change OFFSET to the matching export. */
1683 for (unsigned i = 0; i < num_outputs; i++) {
1684 if (vs_output_param_offset[i] == exp->offset) {
1685 vs_output_param_offset[i] = match->offset;
1686 break;
1687 }
1688 }
1689 return true;
1690 }
1691
1692 void ac_optimize_vs_outputs(struct ac_llvm_context *ctx,
1693 LLVMValueRef main_fn,
1694 uint8_t *vs_output_param_offset,
1695 uint32_t num_outputs,
1696 uint8_t *num_param_exports)
1697 {
1698 LLVMBasicBlockRef bb;
1699 bool removed_any = false;
1700 struct ac_vs_exports exports;
1701
1702 exports.num = 0;
1703
1704 /* Process all LLVM instructions. */
1705 bb = LLVMGetFirstBasicBlock(main_fn);
1706 while (bb) {
1707 LLVMValueRef inst = LLVMGetFirstInstruction(bb);
1708
1709 while (inst) {
1710 LLVMValueRef cur = inst;
1711 inst = LLVMGetNextInstruction(inst);
1712 struct ac_vs_exp_inst exp;
1713
1714 if (LLVMGetInstructionOpcode(cur) != LLVMCall)
1715 continue;
1716
1717 LLVMValueRef callee = ac_llvm_get_called_value(cur);
1718
1719 if (!ac_llvm_is_function(callee))
1720 continue;
1721
1722 const char *name = LLVMGetValueName(callee);
1723 unsigned num_args = LLVMCountParams(callee);
1724
1725 /* Check if this is an export instruction. */
1726 if ((num_args != 9 && num_args != 8) ||
1727 (strcmp(name, "llvm.SI.export") &&
1728 strcmp(name, "llvm.amdgcn.exp.f32")))
1729 continue;
1730
1731 LLVMValueRef arg = LLVMGetOperand(cur, AC_EXP_TARGET);
1732 unsigned target = LLVMConstIntGetZExtValue(arg);
1733
1734 if (target < V_008DFC_SQ_EXP_PARAM)
1735 continue;
1736
1737 target -= V_008DFC_SQ_EXP_PARAM;
1738
1739 /* Parse the instruction. */
1740 memset(&exp, 0, sizeof(exp));
1741 exp.offset = target;
1742 exp.inst = cur;
1743
1744 for (unsigned i = 0; i < 4; i++) {
1745 LLVMValueRef v = LLVMGetOperand(cur, AC_EXP_OUT0 + i);
1746
1747 exp.chan[i].value = v;
1748
1749 if (LLVMIsUndef(v)) {
1750 exp.chan[i].type = AC_IR_UNDEF;
1751 } else if (LLVMIsAConstantFP(v)) {
1752 LLVMBool loses_info;
1753 exp.chan[i].type = AC_IR_CONST;
1754 exp.chan[i].const_float =
1755 LLVMConstRealGetDouble(v, &loses_info);
1756 } else {
1757 exp.chan[i].type = AC_IR_VALUE;
1758 }
1759 }
1760
1761 /* Eliminate constant and duplicated PARAM exports. */
1762 if (ac_eliminate_const_output(vs_output_param_offset,
1763 num_outputs, &exp) ||
1764 ac_eliminate_duplicated_output(vs_output_param_offset,
1765 num_outputs, &exports,
1766 &exp)) {
1767 removed_any = true;
1768 } else {
1769 exports.exp[exports.num++] = exp;
1770 }
1771 }
1772 bb = LLVMGetNextBasicBlock(bb);
1773 }
1774
1775 /* Remove holes in export memory due to removed PARAM exports.
1776 * This is done by renumbering all PARAM exports.
1777 */
1778 if (removed_any) {
1779 uint8_t old_offset[VARYING_SLOT_MAX];
1780 unsigned out, i;
1781
1782 /* Make a copy of the offsets. We need the old version while
1783 * we are modifying some of them. */
1784 memcpy(old_offset, vs_output_param_offset,
1785 sizeof(old_offset));
1786
1787 for (i = 0; i < exports.num; i++) {
1788 unsigned offset = exports.exp[i].offset;
1789
1790 /* Update vs_output_param_offset. Multiple outputs can
1791 * have the same offset.
1792 */
1793 for (out = 0; out < num_outputs; out++) {
1794 if (old_offset[out] == offset)
1795 vs_output_param_offset[out] = i;
1796 }
1797
1798 /* Change the PARAM offset in the instruction. */
1799 LLVMSetOperand(exports.exp[i].inst, AC_EXP_TARGET,
1800 LLVMConstInt(ctx->i32,
1801 V_008DFC_SQ_EXP_PARAM + i, 0));
1802 }
1803 *num_param_exports = exports.num;
1804 }
1805 }
1806
1807 void ac_init_exec_full_mask(struct ac_llvm_context *ctx)
1808 {
1809 LLVMValueRef full_mask = LLVMConstInt(ctx->i64, ~0ull, 0);
1810 ac_build_intrinsic(ctx,
1811 "llvm.amdgcn.init.exec", ctx->voidt,
1812 &full_mask, 1, AC_FUNC_ATTR_CONVERGENT);
1813 }
1814
1815 void ac_declare_lds_as_pointer(struct ac_llvm_context *ctx)
1816 {
1817 unsigned lds_size = ctx->chip_class >= CIK ? 65536 : 32768;
1818 ctx->lds = LLVMBuildIntToPtr(ctx->builder, ctx->i32_0,
1819 LLVMPointerType(LLVMArrayType(ctx->i32, lds_size / 4), AC_LOCAL_ADDR_SPACE),
1820 "lds");
1821 }
1822
1823 LLVMValueRef ac_lds_load(struct ac_llvm_context *ctx,
1824 LLVMValueRef dw_addr)
1825 {
1826 return ac_build_load(ctx, ctx->lds, dw_addr);
1827 }
1828
1829 void ac_lds_store(struct ac_llvm_context *ctx,
1830 LLVMValueRef dw_addr,
1831 LLVMValueRef value)
1832 {
1833 value = ac_to_integer(ctx, value);
1834 ac_build_indexed_store(ctx, ctx->lds,
1835 dw_addr, value);
1836 }
1837
1838 LLVMValueRef ac_find_lsb(struct ac_llvm_context *ctx,
1839 LLVMTypeRef dst_type,
1840 LLVMValueRef src0)
1841 {
1842 LLVMValueRef params[2] = {
1843 src0,
1844
1845 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
1846 * add special code to check for x=0. The reason is that
1847 * the LLVM behavior for x=0 is different from what we
1848 * need here. However, LLVM also assumes that ffs(x) is
1849 * in [0, 31], but GLSL expects that ffs(0) = -1, so
1850 * a conditional assignment to handle 0 is still required.
1851 *
1852 * The hardware already implements the correct behavior.
1853 */
1854 LLVMConstInt(ctx->i1, 1, false),
1855 };
1856
1857 LLVMValueRef lsb = ac_build_intrinsic(ctx, "llvm.cttz.i32", ctx->i32,
1858 params, 2,
1859 AC_FUNC_ATTR_READNONE);
1860
1861 /* TODO: We need an intrinsic to skip this conditional. */
1862 /* Check for zero: */
1863 return LLVMBuildSelect(ctx->builder, LLVMBuildICmp(ctx->builder,
1864 LLVMIntEQ, src0,
1865 ctx->i32_0, ""),
1866 LLVMConstInt(ctx->i32, -1, 0), lsb, "");
1867 }