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