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