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