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