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