ac: add 8-bit support to ac_find_lsb()
[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 "util/u_math.h"
41 #include "sid.h"
42
43 #include "shader_enums.h"
44
45 #define AC_LLVM_INITIAL_CF_DEPTH 4
46
47 /* Data for if/else/endif and bgnloop/endloop control flow structures.
48 */
49 struct ac_llvm_flow {
50 /* Loop exit or next part of if/else/endif. */
51 LLVMBasicBlockRef next_block;
52 LLVMBasicBlockRef loop_entry_block;
53 };
54
55 /* Initialize module-independent parts of the context.
56 *
57 * The caller is responsible for initializing ctx::module and ctx::builder.
58 */
59 void
60 ac_llvm_context_init(struct ac_llvm_context *ctx,
61 enum chip_class chip_class, enum radeon_family family)
62 {
63 LLVMValueRef args[1];
64
65 ctx->context = LLVMContextCreate();
66
67 ctx->chip_class = chip_class;
68 ctx->family = family;
69 ctx->module = NULL;
70 ctx->builder = NULL;
71
72 ctx->voidt = LLVMVoidTypeInContext(ctx->context);
73 ctx->i1 = LLVMInt1TypeInContext(ctx->context);
74 ctx->i8 = LLVMInt8TypeInContext(ctx->context);
75 ctx->i16 = LLVMIntTypeInContext(ctx->context, 16);
76 ctx->i32 = LLVMIntTypeInContext(ctx->context, 32);
77 ctx->i64 = LLVMIntTypeInContext(ctx->context, 64);
78 ctx->intptr = ctx->i32;
79 ctx->f16 = LLVMHalfTypeInContext(ctx->context);
80 ctx->f32 = LLVMFloatTypeInContext(ctx->context);
81 ctx->f64 = LLVMDoubleTypeInContext(ctx->context);
82 ctx->v2i16 = LLVMVectorType(ctx->i16, 2);
83 ctx->v2i32 = LLVMVectorType(ctx->i32, 2);
84 ctx->v3i32 = LLVMVectorType(ctx->i32, 3);
85 ctx->v4i32 = LLVMVectorType(ctx->i32, 4);
86 ctx->v2f32 = LLVMVectorType(ctx->f32, 2);
87 ctx->v4f32 = LLVMVectorType(ctx->f32, 4);
88 ctx->v8i32 = LLVMVectorType(ctx->i32, 8);
89
90 ctx->i8_0 = LLVMConstInt(ctx->i8, 0, false);
91 ctx->i8_1 = LLVMConstInt(ctx->i8, 1, false);
92 ctx->i16_0 = LLVMConstInt(ctx->i16, 0, false);
93 ctx->i16_1 = LLVMConstInt(ctx->i16, 1, false);
94 ctx->i32_0 = LLVMConstInt(ctx->i32, 0, false);
95 ctx->i32_1 = LLVMConstInt(ctx->i32, 1, false);
96 ctx->i64_0 = LLVMConstInt(ctx->i64, 0, false);
97 ctx->i64_1 = LLVMConstInt(ctx->i64, 1, false);
98 ctx->f16_0 = LLVMConstReal(ctx->f16, 0.0);
99 ctx->f16_1 = LLVMConstReal(ctx->f16, 1.0);
100 ctx->f32_0 = LLVMConstReal(ctx->f32, 0.0);
101 ctx->f32_1 = LLVMConstReal(ctx->f32, 1.0);
102 ctx->f64_0 = LLVMConstReal(ctx->f64, 0.0);
103 ctx->f64_1 = LLVMConstReal(ctx->f64, 1.0);
104
105 ctx->i1false = LLVMConstInt(ctx->i1, 0, false);
106 ctx->i1true = LLVMConstInt(ctx->i1, 1, false);
107
108 ctx->range_md_kind = LLVMGetMDKindIDInContext(ctx->context,
109 "range", 5);
110
111 ctx->invariant_load_md_kind = LLVMGetMDKindIDInContext(ctx->context,
112 "invariant.load", 14);
113
114 ctx->fpmath_md_kind = LLVMGetMDKindIDInContext(ctx->context, "fpmath", 6);
115
116 args[0] = LLVMConstReal(ctx->f32, 2.5);
117 ctx->fpmath_md_2p5_ulp = LLVMMDNodeInContext(ctx->context, args, 1);
118
119 ctx->uniform_md_kind = LLVMGetMDKindIDInContext(ctx->context,
120 "amdgpu.uniform", 14);
121
122 ctx->empty_md = LLVMMDNodeInContext(ctx->context, NULL, 0);
123 }
124
125 void
126 ac_llvm_context_dispose(struct ac_llvm_context *ctx)
127 {
128 free(ctx->flow);
129 ctx->flow = NULL;
130 ctx->flow_depth_max = 0;
131 }
132
133 int
134 ac_get_llvm_num_components(LLVMValueRef value)
135 {
136 LLVMTypeRef type = LLVMTypeOf(value);
137 unsigned num_components = LLVMGetTypeKind(type) == LLVMVectorTypeKind
138 ? LLVMGetVectorSize(type)
139 : 1;
140 return num_components;
141 }
142
143 LLVMValueRef
144 ac_llvm_extract_elem(struct ac_llvm_context *ac,
145 LLVMValueRef value,
146 int index)
147 {
148 if (LLVMGetTypeKind(LLVMTypeOf(value)) != LLVMVectorTypeKind) {
149 assert(index == 0);
150 return value;
151 }
152
153 return LLVMBuildExtractElement(ac->builder, value,
154 LLVMConstInt(ac->i32, index, false), "");
155 }
156
157 int
158 ac_get_elem_bits(struct ac_llvm_context *ctx, LLVMTypeRef type)
159 {
160 if (LLVMGetTypeKind(type) == LLVMVectorTypeKind)
161 type = LLVMGetElementType(type);
162
163 if (LLVMGetTypeKind(type) == LLVMIntegerTypeKind)
164 return LLVMGetIntTypeWidth(type);
165
166 if (type == ctx->f16)
167 return 16;
168 if (type == ctx->f32)
169 return 32;
170 if (type == ctx->f64)
171 return 64;
172
173 unreachable("Unhandled type kind in get_elem_bits");
174 }
175
176 unsigned
177 ac_get_type_size(LLVMTypeRef type)
178 {
179 LLVMTypeKind kind = LLVMGetTypeKind(type);
180
181 switch (kind) {
182 case LLVMIntegerTypeKind:
183 return LLVMGetIntTypeWidth(type) / 8;
184 case LLVMHalfTypeKind:
185 return 2;
186 case LLVMFloatTypeKind:
187 return 4;
188 case LLVMDoubleTypeKind:
189 return 8;
190 case LLVMPointerTypeKind:
191 if (LLVMGetPointerAddressSpace(type) == AC_ADDR_SPACE_CONST_32BIT)
192 return 4;
193 return 8;
194 case LLVMVectorTypeKind:
195 return LLVMGetVectorSize(type) *
196 ac_get_type_size(LLVMGetElementType(type));
197 case LLVMArrayTypeKind:
198 return LLVMGetArrayLength(type) *
199 ac_get_type_size(LLVMGetElementType(type));
200 default:
201 assert(0);
202 return 0;
203 }
204 }
205
206 static LLVMTypeRef to_integer_type_scalar(struct ac_llvm_context *ctx, LLVMTypeRef t)
207 {
208 if (t == ctx->i8)
209 return ctx->i8;
210 else if (t == ctx->f16 || t == ctx->i16)
211 return ctx->i16;
212 else if (t == ctx->f32 || t == ctx->i32)
213 return ctx->i32;
214 else if (t == ctx->f64 || t == ctx->i64)
215 return ctx->i64;
216 else
217 unreachable("Unhandled integer size");
218 }
219
220 LLVMTypeRef
221 ac_to_integer_type(struct ac_llvm_context *ctx, LLVMTypeRef t)
222 {
223 if (LLVMGetTypeKind(t) == LLVMVectorTypeKind) {
224 LLVMTypeRef elem_type = LLVMGetElementType(t);
225 return LLVMVectorType(to_integer_type_scalar(ctx, elem_type),
226 LLVMGetVectorSize(t));
227 }
228 if (LLVMGetTypeKind(t) == LLVMPointerTypeKind) {
229 switch (LLVMGetPointerAddressSpace(t)) {
230 case AC_ADDR_SPACE_GLOBAL:
231 return ctx->i64;
232 case AC_ADDR_SPACE_LDS:
233 return ctx->i32;
234 default:
235 unreachable("unhandled address space");
236 }
237 }
238 return to_integer_type_scalar(ctx, t);
239 }
240
241 LLVMValueRef
242 ac_to_integer(struct ac_llvm_context *ctx, LLVMValueRef v)
243 {
244 LLVMTypeRef type = LLVMTypeOf(v);
245 if (LLVMGetTypeKind(type) == LLVMPointerTypeKind) {
246 return LLVMBuildPtrToInt(ctx->builder, v, ac_to_integer_type(ctx, type), "");
247 }
248 return LLVMBuildBitCast(ctx->builder, v, ac_to_integer_type(ctx, type), "");
249 }
250
251 LLVMValueRef
252 ac_to_integer_or_pointer(struct ac_llvm_context *ctx, LLVMValueRef v)
253 {
254 LLVMTypeRef type = LLVMTypeOf(v);
255 if (LLVMGetTypeKind(type) == LLVMPointerTypeKind)
256 return v;
257 return ac_to_integer(ctx, v);
258 }
259
260 static LLVMTypeRef to_float_type_scalar(struct ac_llvm_context *ctx, LLVMTypeRef t)
261 {
262 if (t == ctx->i8)
263 return ctx->i8;
264 else if (t == ctx->i16 || t == ctx->f16)
265 return ctx->f16;
266 else if (t == ctx->i32 || t == ctx->f32)
267 return ctx->f32;
268 else if (t == ctx->i64 || t == ctx->f64)
269 return ctx->f64;
270 else
271 unreachable("Unhandled float size");
272 }
273
274 LLVMTypeRef
275 ac_to_float_type(struct ac_llvm_context *ctx, LLVMTypeRef t)
276 {
277 if (LLVMGetTypeKind(t) == LLVMVectorTypeKind) {
278 LLVMTypeRef elem_type = LLVMGetElementType(t);
279 return LLVMVectorType(to_float_type_scalar(ctx, elem_type),
280 LLVMGetVectorSize(t));
281 }
282 return to_float_type_scalar(ctx, t);
283 }
284
285 LLVMValueRef
286 ac_to_float(struct ac_llvm_context *ctx, LLVMValueRef v)
287 {
288 LLVMTypeRef type = LLVMTypeOf(v);
289 return LLVMBuildBitCast(ctx->builder, v, ac_to_float_type(ctx, type), "");
290 }
291
292
293 LLVMValueRef
294 ac_build_intrinsic(struct ac_llvm_context *ctx, const char *name,
295 LLVMTypeRef return_type, LLVMValueRef *params,
296 unsigned param_count, unsigned attrib_mask)
297 {
298 LLVMValueRef function, call;
299 bool set_callsite_attrs = !(attrib_mask & AC_FUNC_ATTR_LEGACY);
300
301 function = LLVMGetNamedFunction(ctx->module, name);
302 if (!function) {
303 LLVMTypeRef param_types[32], function_type;
304 unsigned i;
305
306 assert(param_count <= 32);
307
308 for (i = 0; i < param_count; ++i) {
309 assert(params[i]);
310 param_types[i] = LLVMTypeOf(params[i]);
311 }
312 function_type =
313 LLVMFunctionType(return_type, param_types, param_count, 0);
314 function = LLVMAddFunction(ctx->module, name, function_type);
315
316 LLVMSetFunctionCallConv(function, LLVMCCallConv);
317 LLVMSetLinkage(function, LLVMExternalLinkage);
318
319 if (!set_callsite_attrs)
320 ac_add_func_attributes(ctx->context, function, attrib_mask);
321 }
322
323 call = LLVMBuildCall(ctx->builder, function, params, param_count, "");
324 if (set_callsite_attrs)
325 ac_add_func_attributes(ctx->context, call, attrib_mask);
326 return call;
327 }
328
329 /**
330 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
331 * intrinsic names).
332 */
333 void ac_build_type_name_for_intr(LLVMTypeRef type, char *buf, unsigned bufsize)
334 {
335 LLVMTypeRef elem_type = type;
336
337 assert(bufsize >= 8);
338
339 if (LLVMGetTypeKind(type) == LLVMVectorTypeKind) {
340 int ret = snprintf(buf, bufsize, "v%u",
341 LLVMGetVectorSize(type));
342 if (ret < 0) {
343 char *type_name = LLVMPrintTypeToString(type);
344 fprintf(stderr, "Error building type name for: %s\n",
345 type_name);
346 return;
347 }
348 elem_type = LLVMGetElementType(type);
349 buf += ret;
350 bufsize -= ret;
351 }
352 switch (LLVMGetTypeKind(elem_type)) {
353 default: break;
354 case LLVMIntegerTypeKind:
355 snprintf(buf, bufsize, "i%d", LLVMGetIntTypeWidth(elem_type));
356 break;
357 case LLVMHalfTypeKind:
358 snprintf(buf, bufsize, "f16");
359 break;
360 case LLVMFloatTypeKind:
361 snprintf(buf, bufsize, "f32");
362 break;
363 case LLVMDoubleTypeKind:
364 snprintf(buf, bufsize, "f64");
365 break;
366 }
367 }
368
369 /**
370 * Helper function that builds an LLVM IR PHI node and immediately adds
371 * incoming edges.
372 */
373 LLVMValueRef
374 ac_build_phi(struct ac_llvm_context *ctx, LLVMTypeRef type,
375 unsigned count_incoming, LLVMValueRef *values,
376 LLVMBasicBlockRef *blocks)
377 {
378 LLVMValueRef phi = LLVMBuildPhi(ctx->builder, type, "");
379 LLVMAddIncoming(phi, values, blocks, count_incoming);
380 return phi;
381 }
382
383 void ac_build_s_barrier(struct ac_llvm_context *ctx)
384 {
385 ac_build_intrinsic(ctx, "llvm.amdgcn.s.barrier", ctx->voidt, NULL,
386 0, AC_FUNC_ATTR_CONVERGENT);
387 }
388
389 /* Prevent optimizations (at least of memory accesses) across the current
390 * point in the program by emitting empty inline assembly that is marked as
391 * having side effects.
392 *
393 * Optionally, a value can be passed through the inline assembly to prevent
394 * LLVM from hoisting calls to ReadNone functions.
395 */
396 void
397 ac_build_optimization_barrier(struct ac_llvm_context *ctx,
398 LLVMValueRef *pvgpr)
399 {
400 static int counter = 0;
401
402 LLVMBuilderRef builder = ctx->builder;
403 char code[16];
404
405 snprintf(code, sizeof(code), "; %d", p_atomic_inc_return(&counter));
406
407 if (!pvgpr) {
408 LLVMTypeRef ftype = LLVMFunctionType(ctx->voidt, NULL, 0, false);
409 LLVMValueRef inlineasm = LLVMConstInlineAsm(ftype, code, "", true, false);
410 LLVMBuildCall(builder, inlineasm, NULL, 0, "");
411 } else {
412 LLVMTypeRef ftype = LLVMFunctionType(ctx->i32, &ctx->i32, 1, false);
413 LLVMValueRef inlineasm = LLVMConstInlineAsm(ftype, code, "=v,0", true, false);
414 LLVMValueRef vgpr = *pvgpr;
415 LLVMTypeRef vgpr_type = LLVMTypeOf(vgpr);
416 unsigned vgpr_size = ac_get_type_size(vgpr_type);
417 LLVMValueRef vgpr0;
418
419 assert(vgpr_size % 4 == 0);
420
421 vgpr = LLVMBuildBitCast(builder, vgpr, LLVMVectorType(ctx->i32, vgpr_size / 4), "");
422 vgpr0 = LLVMBuildExtractElement(builder, vgpr, ctx->i32_0, "");
423 vgpr0 = LLVMBuildCall(builder, inlineasm, &vgpr0, 1, "");
424 vgpr = LLVMBuildInsertElement(builder, vgpr, vgpr0, ctx->i32_0, "");
425 vgpr = LLVMBuildBitCast(builder, vgpr, vgpr_type, "");
426
427 *pvgpr = vgpr;
428 }
429 }
430
431 LLVMValueRef
432 ac_build_shader_clock(struct ac_llvm_context *ctx)
433 {
434 LLVMValueRef tmp = ac_build_intrinsic(ctx, "llvm.readcyclecounter",
435 ctx->i64, NULL, 0, 0);
436 return LLVMBuildBitCast(ctx->builder, tmp, ctx->v2i32, "");
437 }
438
439 LLVMValueRef
440 ac_build_ballot(struct ac_llvm_context *ctx,
441 LLVMValueRef value)
442 {
443 LLVMValueRef args[3] = {
444 value,
445 ctx->i32_0,
446 LLVMConstInt(ctx->i32, LLVMIntNE, 0)
447 };
448
449 /* We currently have no other way to prevent LLVM from lifting the icmp
450 * calls to a dominating basic block.
451 */
452 ac_build_optimization_barrier(ctx, &args[0]);
453
454 args[0] = ac_to_integer(ctx, args[0]);
455
456 return ac_build_intrinsic(ctx,
457 "llvm.amdgcn.icmp.i32",
458 ctx->i64, args, 3,
459 AC_FUNC_ATTR_NOUNWIND |
460 AC_FUNC_ATTR_READNONE |
461 AC_FUNC_ATTR_CONVERGENT);
462 }
463
464 LLVMValueRef
465 ac_build_vote_all(struct ac_llvm_context *ctx, LLVMValueRef value)
466 {
467 LLVMValueRef active_set = ac_build_ballot(ctx, ctx->i32_1);
468 LLVMValueRef vote_set = ac_build_ballot(ctx, value);
469 return LLVMBuildICmp(ctx->builder, LLVMIntEQ, vote_set, active_set, "");
470 }
471
472 LLVMValueRef
473 ac_build_vote_any(struct ac_llvm_context *ctx, LLVMValueRef value)
474 {
475 LLVMValueRef vote_set = ac_build_ballot(ctx, value);
476 return LLVMBuildICmp(ctx->builder, LLVMIntNE, vote_set,
477 LLVMConstInt(ctx->i64, 0, 0), "");
478 }
479
480 LLVMValueRef
481 ac_build_vote_eq(struct ac_llvm_context *ctx, LLVMValueRef value)
482 {
483 LLVMValueRef active_set = ac_build_ballot(ctx, ctx->i32_1);
484 LLVMValueRef vote_set = ac_build_ballot(ctx, value);
485
486 LLVMValueRef all = LLVMBuildICmp(ctx->builder, LLVMIntEQ,
487 vote_set, active_set, "");
488 LLVMValueRef none = LLVMBuildICmp(ctx->builder, LLVMIntEQ,
489 vote_set,
490 LLVMConstInt(ctx->i64, 0, 0), "");
491 return LLVMBuildOr(ctx->builder, all, none, "");
492 }
493
494 LLVMValueRef
495 ac_build_varying_gather_values(struct ac_llvm_context *ctx, LLVMValueRef *values,
496 unsigned value_count, unsigned component)
497 {
498 LLVMValueRef vec = NULL;
499
500 if (value_count == 1) {
501 return values[component];
502 } else if (!value_count)
503 unreachable("value_count is 0");
504
505 for (unsigned i = component; i < value_count + component; i++) {
506 LLVMValueRef value = values[i];
507
508 if (i == component)
509 vec = LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value), value_count));
510 LLVMValueRef index = LLVMConstInt(ctx->i32, i - component, false);
511 vec = LLVMBuildInsertElement(ctx->builder, vec, value, index, "");
512 }
513 return vec;
514 }
515
516 LLVMValueRef
517 ac_build_gather_values_extended(struct ac_llvm_context *ctx,
518 LLVMValueRef *values,
519 unsigned value_count,
520 unsigned value_stride,
521 bool load,
522 bool always_vector)
523 {
524 LLVMBuilderRef builder = ctx->builder;
525 LLVMValueRef vec = NULL;
526 unsigned i;
527
528 if (value_count == 1 && !always_vector) {
529 if (load)
530 return LLVMBuildLoad(builder, values[0], "");
531 return values[0];
532 } else if (!value_count)
533 unreachable("value_count is 0");
534
535 for (i = 0; i < value_count; i++) {
536 LLVMValueRef value = values[i * value_stride];
537 if (load)
538 value = LLVMBuildLoad(builder, value, "");
539
540 if (!i)
541 vec = LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value), value_count));
542 LLVMValueRef index = LLVMConstInt(ctx->i32, i, false);
543 vec = LLVMBuildInsertElement(builder, vec, value, index, "");
544 }
545 return vec;
546 }
547
548 LLVMValueRef
549 ac_build_gather_values(struct ac_llvm_context *ctx,
550 LLVMValueRef *values,
551 unsigned value_count)
552 {
553 return ac_build_gather_values_extended(ctx, values, value_count, 1, false, false);
554 }
555
556 /* Expand a scalar or vector to <dst_channels x type> by filling the remaining
557 * channels with undef. Extract at most src_channels components from the input.
558 */
559 static LLVMValueRef
560 ac_build_expand(struct ac_llvm_context *ctx,
561 LLVMValueRef value,
562 unsigned src_channels,
563 unsigned dst_channels)
564 {
565 LLVMTypeRef elemtype;
566 LLVMValueRef chan[dst_channels];
567
568 if (LLVMGetTypeKind(LLVMTypeOf(value)) == LLVMVectorTypeKind) {
569 unsigned vec_size = LLVMGetVectorSize(LLVMTypeOf(value));
570
571 if (src_channels == dst_channels && vec_size == dst_channels)
572 return value;
573
574 src_channels = MIN2(src_channels, vec_size);
575
576 for (unsigned i = 0; i < src_channels; i++)
577 chan[i] = ac_llvm_extract_elem(ctx, value, i);
578
579 elemtype = LLVMGetElementType(LLVMTypeOf(value));
580 } else {
581 if (src_channels) {
582 assert(src_channels == 1);
583 chan[0] = value;
584 }
585 elemtype = LLVMTypeOf(value);
586 }
587
588 for (unsigned i = src_channels; i < dst_channels; i++)
589 chan[i] = LLVMGetUndef(elemtype);
590
591 return ac_build_gather_values(ctx, chan, dst_channels);
592 }
593
594 /* Expand a scalar or vector to <4 x type> by filling the remaining channels
595 * with undef. Extract at most num_channels components from the input.
596 */
597 LLVMValueRef ac_build_expand_to_vec4(struct ac_llvm_context *ctx,
598 LLVMValueRef value,
599 unsigned num_channels)
600 {
601 return ac_build_expand(ctx, value, num_channels, 4);
602 }
603
604 LLVMValueRef ac_build_round(struct ac_llvm_context *ctx, LLVMValueRef value)
605 {
606 unsigned type_size = ac_get_type_size(LLVMTypeOf(value));
607 const char *name;
608
609 if (type_size == 2)
610 name = "llvm.rint.f16";
611 else if (type_size == 4)
612 name = "llvm.rint.f32";
613 else
614 name = "llvm.rint.f64";
615
616 return ac_build_intrinsic(ctx, name, LLVMTypeOf(value), &value, 1,
617 AC_FUNC_ATTR_READNONE);
618 }
619
620 LLVMValueRef
621 ac_build_fdiv(struct ac_llvm_context *ctx,
622 LLVMValueRef num,
623 LLVMValueRef den)
624 {
625 /* If we do (num / den), LLVM >= 7.0 does:
626 * return num * v_rcp_f32(den * (fabs(den) > 0x1.0p+96f ? 0x1.0p-32f : 1.0f));
627 *
628 * If we do (num * (1 / den)), LLVM does:
629 * return num * v_rcp_f32(den);
630 */
631 LLVMValueRef one = LLVMConstReal(LLVMTypeOf(num), 1.0);
632 LLVMValueRef rcp = LLVMBuildFDiv(ctx->builder, one, den, "");
633 LLVMValueRef ret = LLVMBuildFMul(ctx->builder, num, rcp, "");
634
635 /* Use v_rcp_f32 instead of precise division. */
636 if (!LLVMIsConstant(ret))
637 LLVMSetMetadata(ret, ctx->fpmath_md_kind, ctx->fpmath_md_2p5_ulp);
638 return ret;
639 }
640
641 /* See fast_idiv_by_const.h. */
642 /* Set: increment = util_fast_udiv_info::increment ? multiplier : 0; */
643 LLVMValueRef ac_build_fast_udiv(struct ac_llvm_context *ctx,
644 LLVMValueRef num,
645 LLVMValueRef multiplier,
646 LLVMValueRef pre_shift,
647 LLVMValueRef post_shift,
648 LLVMValueRef increment)
649 {
650 LLVMBuilderRef builder = ctx->builder;
651
652 num = LLVMBuildLShr(builder, num, pre_shift, "");
653 num = LLVMBuildMul(builder,
654 LLVMBuildZExt(builder, num, ctx->i64, ""),
655 LLVMBuildZExt(builder, multiplier, ctx->i64, ""), "");
656 num = LLVMBuildAdd(builder, num,
657 LLVMBuildZExt(builder, increment, ctx->i64, ""), "");
658 num = LLVMBuildLShr(builder, num, LLVMConstInt(ctx->i64, 32, 0), "");
659 num = LLVMBuildTrunc(builder, num, ctx->i32, "");
660 return LLVMBuildLShr(builder, num, post_shift, "");
661 }
662
663 /* See fast_idiv_by_const.h. */
664 /* If num != UINT_MAX, this more efficient version can be used. */
665 /* Set: increment = util_fast_udiv_info::increment; */
666 LLVMValueRef ac_build_fast_udiv_nuw(struct ac_llvm_context *ctx,
667 LLVMValueRef num,
668 LLVMValueRef multiplier,
669 LLVMValueRef pre_shift,
670 LLVMValueRef post_shift,
671 LLVMValueRef increment)
672 {
673 LLVMBuilderRef builder = ctx->builder;
674
675 num = LLVMBuildLShr(builder, num, pre_shift, "");
676 num = LLVMBuildNUWAdd(builder, num, increment, "");
677 num = LLVMBuildMul(builder,
678 LLVMBuildZExt(builder, num, ctx->i64, ""),
679 LLVMBuildZExt(builder, multiplier, ctx->i64, ""), "");
680 num = LLVMBuildLShr(builder, num, LLVMConstInt(ctx->i64, 32, 0), "");
681 num = LLVMBuildTrunc(builder, num, ctx->i32, "");
682 return LLVMBuildLShr(builder, num, post_shift, "");
683 }
684
685 /* See fast_idiv_by_const.h. */
686 /* Both operands must fit in 31 bits and the divisor must not be 1. */
687 LLVMValueRef ac_build_fast_udiv_u31_d_not_one(struct ac_llvm_context *ctx,
688 LLVMValueRef num,
689 LLVMValueRef multiplier,
690 LLVMValueRef post_shift)
691 {
692 LLVMBuilderRef builder = ctx->builder;
693
694 num = LLVMBuildMul(builder,
695 LLVMBuildZExt(builder, num, ctx->i64, ""),
696 LLVMBuildZExt(builder, multiplier, ctx->i64, ""), "");
697 num = LLVMBuildLShr(builder, num, LLVMConstInt(ctx->i64, 32, 0), "");
698 num = LLVMBuildTrunc(builder, num, ctx->i32, "");
699 return LLVMBuildLShr(builder, num, post_shift, "");
700 }
701
702 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
703 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
704 * already multiplied by two. id is the cube face number.
705 */
706 struct cube_selection_coords {
707 LLVMValueRef stc[2];
708 LLVMValueRef ma;
709 LLVMValueRef id;
710 };
711
712 static void
713 build_cube_intrinsic(struct ac_llvm_context *ctx,
714 LLVMValueRef in[3],
715 struct cube_selection_coords *out)
716 {
717 LLVMTypeRef f32 = ctx->f32;
718
719 out->stc[1] = ac_build_intrinsic(ctx, "llvm.amdgcn.cubetc",
720 f32, in, 3, AC_FUNC_ATTR_READNONE);
721 out->stc[0] = ac_build_intrinsic(ctx, "llvm.amdgcn.cubesc",
722 f32, in, 3, AC_FUNC_ATTR_READNONE);
723 out->ma = ac_build_intrinsic(ctx, "llvm.amdgcn.cubema",
724 f32, in, 3, AC_FUNC_ATTR_READNONE);
725 out->id = ac_build_intrinsic(ctx, "llvm.amdgcn.cubeid",
726 f32, in, 3, AC_FUNC_ATTR_READNONE);
727 }
728
729 /**
730 * Build a manual selection sequence for cube face sc/tc coordinates and
731 * major axis vector (multiplied by 2 for consistency) for the given
732 * vec3 \p coords, for the face implied by \p selcoords.
733 *
734 * For the major axis, we always adjust the sign to be in the direction of
735 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
736 * the selcoords major axis.
737 */
738 static void build_cube_select(struct ac_llvm_context *ctx,
739 const struct cube_selection_coords *selcoords,
740 const LLVMValueRef *coords,
741 LLVMValueRef *out_st,
742 LLVMValueRef *out_ma)
743 {
744 LLVMBuilderRef builder = ctx->builder;
745 LLVMTypeRef f32 = LLVMTypeOf(coords[0]);
746 LLVMValueRef is_ma_positive;
747 LLVMValueRef sgn_ma;
748 LLVMValueRef is_ma_z, is_not_ma_z;
749 LLVMValueRef is_ma_y;
750 LLVMValueRef is_ma_x;
751 LLVMValueRef sgn;
752 LLVMValueRef tmp;
753
754 is_ma_positive = LLVMBuildFCmp(builder, LLVMRealUGE,
755 selcoords->ma, LLVMConstReal(f32, 0.0), "");
756 sgn_ma = LLVMBuildSelect(builder, is_ma_positive,
757 LLVMConstReal(f32, 1.0), LLVMConstReal(f32, -1.0), "");
758
759 is_ma_z = LLVMBuildFCmp(builder, LLVMRealUGE, selcoords->id, LLVMConstReal(f32, 4.0), "");
760 is_not_ma_z = LLVMBuildNot(builder, is_ma_z, "");
761 is_ma_y = LLVMBuildAnd(builder, is_not_ma_z,
762 LLVMBuildFCmp(builder, LLVMRealUGE, selcoords->id, LLVMConstReal(f32, 2.0), ""), "");
763 is_ma_x = LLVMBuildAnd(builder, is_not_ma_z, LLVMBuildNot(builder, is_ma_y, ""), "");
764
765 /* Select sc */
766 tmp = LLVMBuildSelect(builder, is_ma_x, coords[2], coords[0], "");
767 sgn = LLVMBuildSelect(builder, is_ma_y, LLVMConstReal(f32, 1.0),
768 LLVMBuildSelect(builder, is_ma_z, sgn_ma,
769 LLVMBuildFNeg(builder, sgn_ma, ""), ""), "");
770 out_st[0] = LLVMBuildFMul(builder, tmp, sgn, "");
771
772 /* Select tc */
773 tmp = LLVMBuildSelect(builder, is_ma_y, coords[2], coords[1], "");
774 sgn = LLVMBuildSelect(builder, is_ma_y, sgn_ma,
775 LLVMConstReal(f32, -1.0), "");
776 out_st[1] = LLVMBuildFMul(builder, tmp, sgn, "");
777
778 /* Select ma */
779 tmp = LLVMBuildSelect(builder, is_ma_z, coords[2],
780 LLVMBuildSelect(builder, is_ma_y, coords[1], coords[0], ""), "");
781 tmp = ac_build_intrinsic(ctx, "llvm.fabs.f32",
782 ctx->f32, &tmp, 1, AC_FUNC_ATTR_READNONE);
783 *out_ma = LLVMBuildFMul(builder, tmp, LLVMConstReal(f32, 2.0), "");
784 }
785
786 void
787 ac_prepare_cube_coords(struct ac_llvm_context *ctx,
788 bool is_deriv, bool is_array, bool is_lod,
789 LLVMValueRef *coords_arg,
790 LLVMValueRef *derivs_arg)
791 {
792
793 LLVMBuilderRef builder = ctx->builder;
794 struct cube_selection_coords selcoords;
795 LLVMValueRef coords[3];
796 LLVMValueRef invma;
797
798 if (is_array && !is_lod) {
799 LLVMValueRef tmp = ac_build_round(ctx, coords_arg[3]);
800
801 /* Section 8.9 (Texture Functions) of the GLSL 4.50 spec says:
802 *
803 * "For Array forms, the array layer used will be
804 *
805 * max(0, min(d−1, floor(layer+0.5)))
806 *
807 * where d is the depth of the texture array and layer
808 * comes from the component indicated in the tables below.
809 * Workaroudn for an issue where the layer is taken from a
810 * helper invocation which happens to fall on a different
811 * layer due to extrapolation."
812 *
813 * VI and earlier attempt to implement this in hardware by
814 * clamping the value of coords[2] = (8 * layer) + face.
815 * Unfortunately, this means that the we end up with the wrong
816 * face when clamping occurs.
817 *
818 * Clamp the layer earlier to work around the issue.
819 */
820 if (ctx->chip_class <= VI) {
821 LLVMValueRef ge0;
822 ge0 = LLVMBuildFCmp(builder, LLVMRealOGE, tmp, ctx->f32_0, "");
823 tmp = LLVMBuildSelect(builder, ge0, tmp, ctx->f32_0, "");
824 }
825
826 coords_arg[3] = tmp;
827 }
828
829 build_cube_intrinsic(ctx, coords_arg, &selcoords);
830
831 invma = ac_build_intrinsic(ctx, "llvm.fabs.f32",
832 ctx->f32, &selcoords.ma, 1, AC_FUNC_ATTR_READNONE);
833 invma = ac_build_fdiv(ctx, LLVMConstReal(ctx->f32, 1.0), invma);
834
835 for (int i = 0; i < 2; ++i)
836 coords[i] = LLVMBuildFMul(builder, selcoords.stc[i], invma, "");
837
838 coords[2] = selcoords.id;
839
840 if (is_deriv && derivs_arg) {
841 LLVMValueRef derivs[4];
842 int axis;
843
844 /* Convert cube derivatives to 2D derivatives. */
845 for (axis = 0; axis < 2; axis++) {
846 LLVMValueRef deriv_st[2];
847 LLVMValueRef deriv_ma;
848
849 /* Transform the derivative alongside the texture
850 * coordinate. Mathematically, the correct formula is
851 * as follows. Assume we're projecting onto the +Z face
852 * and denote by dx/dh the derivative of the (original)
853 * X texture coordinate with respect to horizontal
854 * window coordinates. The projection onto the +Z face
855 * plane is:
856 *
857 * f(x,z) = x/z
858 *
859 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
860 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
861 *
862 * This motivatives the implementation below.
863 *
864 * Whether this actually gives the expected results for
865 * apps that might feed in derivatives obtained via
866 * finite differences is anyone's guess. The OpenGL spec
867 * seems awfully quiet about how textureGrad for cube
868 * maps should be handled.
869 */
870 build_cube_select(ctx, &selcoords, &derivs_arg[axis * 3],
871 deriv_st, &deriv_ma);
872
873 deriv_ma = LLVMBuildFMul(builder, deriv_ma, invma, "");
874
875 for (int i = 0; i < 2; ++i)
876 derivs[axis * 2 + i] =
877 LLVMBuildFSub(builder,
878 LLVMBuildFMul(builder, deriv_st[i], invma, ""),
879 LLVMBuildFMul(builder, deriv_ma, coords[i], ""), "");
880 }
881
882 memcpy(derivs_arg, derivs, sizeof(derivs));
883 }
884
885 /* Shift the texture coordinate. This must be applied after the
886 * derivative calculation.
887 */
888 for (int i = 0; i < 2; ++i)
889 coords[i] = LLVMBuildFAdd(builder, coords[i], LLVMConstReal(ctx->f32, 1.5), "");
890
891 if (is_array) {
892 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
893 /* coords_arg.w component - array_index for cube arrays */
894 coords[2] = ac_build_fmad(ctx, coords_arg[3], LLVMConstReal(ctx->f32, 8.0), coords[2]);
895 }
896
897 memcpy(coords_arg, coords, sizeof(coords));
898 }
899
900
901 LLVMValueRef
902 ac_build_fs_interp(struct ac_llvm_context *ctx,
903 LLVMValueRef llvm_chan,
904 LLVMValueRef attr_number,
905 LLVMValueRef params,
906 LLVMValueRef i,
907 LLVMValueRef j)
908 {
909 LLVMValueRef args[5];
910 LLVMValueRef p1;
911
912 args[0] = i;
913 args[1] = llvm_chan;
914 args[2] = attr_number;
915 args[3] = params;
916
917 p1 = ac_build_intrinsic(ctx, "llvm.amdgcn.interp.p1",
918 ctx->f32, args, 4, AC_FUNC_ATTR_READNONE);
919
920 args[0] = p1;
921 args[1] = j;
922 args[2] = llvm_chan;
923 args[3] = attr_number;
924 args[4] = params;
925
926 return ac_build_intrinsic(ctx, "llvm.amdgcn.interp.p2",
927 ctx->f32, args, 5, AC_FUNC_ATTR_READNONE);
928 }
929
930 LLVMValueRef
931 ac_build_fs_interp_f16(struct ac_llvm_context *ctx,
932 LLVMValueRef llvm_chan,
933 LLVMValueRef attr_number,
934 LLVMValueRef params,
935 LLVMValueRef i,
936 LLVMValueRef j)
937 {
938 LLVMValueRef args[6];
939 LLVMValueRef p1;
940
941 args[0] = i;
942 args[1] = llvm_chan;
943 args[2] = attr_number;
944 args[3] = ctx->i1false;
945 args[4] = params;
946
947 p1 = ac_build_intrinsic(ctx, "llvm.amdgcn.interp.p1.f16",
948 ctx->f32, args, 5, AC_FUNC_ATTR_READNONE);
949
950 args[0] = p1;
951 args[1] = j;
952 args[2] = llvm_chan;
953 args[3] = attr_number;
954 args[4] = ctx->i1false;
955 args[5] = params;
956
957 return ac_build_intrinsic(ctx, "llvm.amdgcn.interp.p2.f16",
958 ctx->f16, args, 6, AC_FUNC_ATTR_READNONE);
959 }
960
961 LLVMValueRef
962 ac_build_fs_interp_mov(struct ac_llvm_context *ctx,
963 LLVMValueRef parameter,
964 LLVMValueRef llvm_chan,
965 LLVMValueRef attr_number,
966 LLVMValueRef params)
967 {
968 LLVMValueRef args[4];
969
970 args[0] = parameter;
971 args[1] = llvm_chan;
972 args[2] = attr_number;
973 args[3] = params;
974
975 return ac_build_intrinsic(ctx, "llvm.amdgcn.interp.mov",
976 ctx->f32, args, 4, AC_FUNC_ATTR_READNONE);
977 }
978
979 LLVMValueRef
980 ac_build_gep_ptr(struct ac_llvm_context *ctx,
981 LLVMValueRef base_ptr,
982 LLVMValueRef index)
983 {
984 return LLVMBuildGEP(ctx->builder, base_ptr, &index, 1, "");
985 }
986
987 LLVMValueRef
988 ac_build_gep0(struct ac_llvm_context *ctx,
989 LLVMValueRef base_ptr,
990 LLVMValueRef index)
991 {
992 LLVMValueRef indices[2] = {
993 ctx->i32_0,
994 index,
995 };
996 return LLVMBuildGEP(ctx->builder, base_ptr, indices, 2, "");
997 }
998
999 LLVMValueRef ac_build_pointer_add(struct ac_llvm_context *ctx, LLVMValueRef ptr,
1000 LLVMValueRef index)
1001 {
1002 return LLVMBuildPointerCast(ctx->builder,
1003 ac_build_gep0(ctx, ptr, index),
1004 LLVMTypeOf(ptr), "");
1005 }
1006
1007 void
1008 ac_build_indexed_store(struct ac_llvm_context *ctx,
1009 LLVMValueRef base_ptr, LLVMValueRef index,
1010 LLVMValueRef value)
1011 {
1012 LLVMBuildStore(ctx->builder, value,
1013 ac_build_gep0(ctx, base_ptr, index));
1014 }
1015
1016 /**
1017 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
1018 * It's equivalent to doing a load from &base_ptr[index].
1019 *
1020 * \param base_ptr Where the array starts.
1021 * \param index The element index into the array.
1022 * \param uniform Whether the base_ptr and index can be assumed to be
1023 * dynamically uniform (i.e. load to an SGPR)
1024 * \param invariant Whether the load is invariant (no other opcodes affect it)
1025 * \param no_unsigned_wraparound
1026 * For all possible re-associations and re-distributions of an expression
1027 * "base_ptr + index * elemsize" into "addr + offset" (excluding GEPs
1028 * without inbounds in base_ptr), this parameter is true if "addr + offset"
1029 * does not result in an unsigned integer wraparound. This is used for
1030 * optimal code generation of 32-bit pointer arithmetic.
1031 *
1032 * For example, a 32-bit immediate offset that causes a 32-bit unsigned
1033 * integer wraparound can't be an imm offset in s_load_dword, because
1034 * the instruction performs "addr + offset" in 64 bits.
1035 *
1036 * Expected usage for bindless textures by chaining GEPs:
1037 * // possible unsigned wraparound, don't use InBounds:
1038 * ptr1 = LLVMBuildGEP(base_ptr, index);
1039 * image = load(ptr1); // becomes "s_load ptr1, 0"
1040 *
1041 * ptr2 = LLVMBuildInBoundsGEP(ptr1, 32 / elemsize);
1042 * sampler = load(ptr2); // becomes "s_load ptr1, 32" thanks to InBounds
1043 */
1044 static LLVMValueRef
1045 ac_build_load_custom(struct ac_llvm_context *ctx, LLVMValueRef base_ptr,
1046 LLVMValueRef index, bool uniform, bool invariant,
1047 bool no_unsigned_wraparound)
1048 {
1049 LLVMValueRef pointer, result;
1050 LLVMValueRef indices[2] = {ctx->i32_0, index};
1051
1052 if (no_unsigned_wraparound &&
1053 LLVMGetPointerAddressSpace(LLVMTypeOf(base_ptr)) == AC_ADDR_SPACE_CONST_32BIT)
1054 pointer = LLVMBuildInBoundsGEP(ctx->builder, base_ptr, indices, 2, "");
1055 else
1056 pointer = LLVMBuildGEP(ctx->builder, base_ptr, indices, 2, "");
1057
1058 if (uniform)
1059 LLVMSetMetadata(pointer, ctx->uniform_md_kind, ctx->empty_md);
1060 result = LLVMBuildLoad(ctx->builder, pointer, "");
1061 if (invariant)
1062 LLVMSetMetadata(result, ctx->invariant_load_md_kind, ctx->empty_md);
1063 return result;
1064 }
1065
1066 LLVMValueRef ac_build_load(struct ac_llvm_context *ctx, LLVMValueRef base_ptr,
1067 LLVMValueRef index)
1068 {
1069 return ac_build_load_custom(ctx, base_ptr, index, false, false, false);
1070 }
1071
1072 LLVMValueRef ac_build_load_invariant(struct ac_llvm_context *ctx,
1073 LLVMValueRef base_ptr, LLVMValueRef index)
1074 {
1075 return ac_build_load_custom(ctx, base_ptr, index, false, true, false);
1076 }
1077
1078 /* This assumes that there is no unsigned integer wraparound during the address
1079 * computation, excluding all GEPs within base_ptr. */
1080 LLVMValueRef ac_build_load_to_sgpr(struct ac_llvm_context *ctx,
1081 LLVMValueRef base_ptr, LLVMValueRef index)
1082 {
1083 return ac_build_load_custom(ctx, base_ptr, index, true, true, true);
1084 }
1085
1086 /* See ac_build_load_custom() documentation. */
1087 LLVMValueRef ac_build_load_to_sgpr_uint_wraparound(struct ac_llvm_context *ctx,
1088 LLVMValueRef base_ptr, LLVMValueRef index)
1089 {
1090 return ac_build_load_custom(ctx, base_ptr, index, true, true, false);
1091 }
1092
1093 static void
1094 ac_build_buffer_store_common(struct ac_llvm_context *ctx,
1095 LLVMValueRef rsrc,
1096 LLVMValueRef data,
1097 LLVMValueRef vindex,
1098 LLVMValueRef voffset,
1099 unsigned num_channels,
1100 bool glc,
1101 bool slc,
1102 bool writeonly_memory,
1103 bool use_format)
1104 {
1105 LLVMValueRef args[] = {
1106 data,
1107 LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, ""),
1108 vindex ? vindex : ctx->i32_0,
1109 voffset,
1110 LLVMConstInt(ctx->i1, glc, 0),
1111 LLVMConstInt(ctx->i1, slc, 0)
1112 };
1113 unsigned func = CLAMP(num_channels, 1, 3) - 1;
1114
1115 const char *type_names[] = {"f32", "v2f32", "v4f32"};
1116 char name[256];
1117
1118 if (use_format) {
1119 snprintf(name, sizeof(name), "llvm.amdgcn.buffer.store.format.%s",
1120 type_names[func]);
1121 } else {
1122 snprintf(name, sizeof(name), "llvm.amdgcn.buffer.store.%s",
1123 type_names[func]);
1124 }
1125
1126 ac_build_intrinsic(ctx, name, ctx->voidt, args, ARRAY_SIZE(args),
1127 ac_get_store_intr_attribs(writeonly_memory));
1128 }
1129
1130 static void
1131 ac_build_llvm8_buffer_store_common(struct ac_llvm_context *ctx,
1132 LLVMValueRef rsrc,
1133 LLVMValueRef data,
1134 LLVMValueRef vindex,
1135 LLVMValueRef voffset,
1136 LLVMValueRef soffset,
1137 unsigned num_channels,
1138 bool glc,
1139 bool slc,
1140 bool writeonly_memory,
1141 bool use_format,
1142 bool structurized)
1143 {
1144 LLVMValueRef args[6];
1145 int idx = 0;
1146 args[idx++] = data;
1147 args[idx++] = LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, "");
1148 if (structurized)
1149 args[idx++] = vindex ? vindex : ctx->i32_0;
1150 args[idx++] = voffset ? voffset : ctx->i32_0;
1151 args[idx++] = soffset ? soffset : ctx->i32_0;
1152 args[idx++] = LLVMConstInt(ctx->i32, (glc ? 1 : 0) + (slc ? 2 : 0), 0);
1153 unsigned func = CLAMP(num_channels, 1, 3) - 1;
1154
1155 const char *type_names[] = {"f32", "v2f32", "v4f32"};
1156 const char *indexing_kind = structurized ? "struct" : "raw";
1157 char name[256];
1158
1159 if (use_format) {
1160 snprintf(name, sizeof(name), "llvm.amdgcn.%s.buffer.store.format.%s",
1161 indexing_kind, type_names[func]);
1162 } else {
1163 snprintf(name, sizeof(name), "llvm.amdgcn.%s.buffer.store.%s",
1164 indexing_kind, type_names[func]);
1165 }
1166
1167 ac_build_intrinsic(ctx, name, ctx->voidt, args, idx,
1168 ac_get_store_intr_attribs(writeonly_memory));
1169 }
1170
1171 void
1172 ac_build_buffer_store_format(struct ac_llvm_context *ctx,
1173 LLVMValueRef rsrc,
1174 LLVMValueRef data,
1175 LLVMValueRef vindex,
1176 LLVMValueRef voffset,
1177 unsigned num_channels,
1178 bool glc,
1179 bool writeonly_memory)
1180 {
1181 if (HAVE_LLVM >= 0x800) {
1182 ac_build_llvm8_buffer_store_common(ctx, rsrc, data, vindex,
1183 voffset, NULL, num_channels,
1184 glc, false, writeonly_memory,
1185 true, true);
1186 } else {
1187 ac_build_buffer_store_common(ctx, rsrc, data, vindex, voffset,
1188 num_channels, glc, false,
1189 writeonly_memory, true);
1190 }
1191 }
1192
1193 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
1194 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
1195 * or v4i32 (num_channels=3,4).
1196 */
1197 void
1198 ac_build_buffer_store_dword(struct ac_llvm_context *ctx,
1199 LLVMValueRef rsrc,
1200 LLVMValueRef vdata,
1201 unsigned num_channels,
1202 LLVMValueRef voffset,
1203 LLVMValueRef soffset,
1204 unsigned inst_offset,
1205 bool glc,
1206 bool slc,
1207 bool writeonly_memory,
1208 bool swizzle_enable_hint)
1209 {
1210 /* Split 3 channel stores, becase LLVM doesn't support 3-channel
1211 * intrinsics. */
1212 if (num_channels == 3) {
1213 LLVMValueRef v[3], v01;
1214
1215 for (int i = 0; i < 3; i++) {
1216 v[i] = LLVMBuildExtractElement(ctx->builder, vdata,
1217 LLVMConstInt(ctx->i32, i, 0), "");
1218 }
1219 v01 = ac_build_gather_values(ctx, v, 2);
1220
1221 ac_build_buffer_store_dword(ctx, rsrc, v01, 2, voffset,
1222 soffset, inst_offset, glc, slc,
1223 writeonly_memory, swizzle_enable_hint);
1224 ac_build_buffer_store_dword(ctx, rsrc, v[2], 1, voffset,
1225 soffset, inst_offset + 8,
1226 glc, slc,
1227 writeonly_memory, swizzle_enable_hint);
1228 return;
1229 }
1230
1231 /* SWIZZLE_ENABLE requires that soffset isn't folded into voffset
1232 * (voffset is swizzled, but soffset isn't swizzled).
1233 * llvm.amdgcn.buffer.store doesn't have a separate soffset parameter.
1234 */
1235 if (!swizzle_enable_hint) {
1236 LLVMValueRef offset = soffset;
1237
1238 if (inst_offset)
1239 offset = LLVMBuildAdd(ctx->builder, offset,
1240 LLVMConstInt(ctx->i32, inst_offset, 0), "");
1241
1242 if (HAVE_LLVM >= 0x800) {
1243 ac_build_llvm8_buffer_store_common(ctx, rsrc,
1244 ac_to_float(ctx, vdata),
1245 ctx->i32_0,
1246 voffset, offset,
1247 num_channels,
1248 glc, slc,
1249 writeonly_memory,
1250 false, false);
1251 } else {
1252 if (voffset)
1253 offset = LLVMBuildAdd(ctx->builder, offset, voffset, "");
1254
1255 ac_build_buffer_store_common(ctx, rsrc,
1256 ac_to_float(ctx, vdata),
1257 ctx->i32_0, offset,
1258 num_channels, glc, slc,
1259 writeonly_memory, false);
1260 }
1261 return;
1262 }
1263
1264 static const unsigned dfmts[] = {
1265 V_008F0C_BUF_DATA_FORMAT_32,
1266 V_008F0C_BUF_DATA_FORMAT_32_32,
1267 V_008F0C_BUF_DATA_FORMAT_32_32_32,
1268 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
1269 };
1270 unsigned dfmt = dfmts[num_channels - 1];
1271 unsigned nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
1272 LLVMValueRef immoffset = LLVMConstInt(ctx->i32, inst_offset, 0);
1273
1274 ac_build_raw_tbuffer_store(ctx, rsrc, vdata, voffset, soffset,
1275 immoffset, num_channels, dfmt, nfmt, glc,
1276 slc, writeonly_memory);
1277 }
1278
1279 static LLVMValueRef
1280 ac_build_buffer_load_common(struct ac_llvm_context *ctx,
1281 LLVMValueRef rsrc,
1282 LLVMValueRef vindex,
1283 LLVMValueRef voffset,
1284 unsigned num_channels,
1285 bool glc,
1286 bool slc,
1287 bool can_speculate,
1288 bool use_format)
1289 {
1290 LLVMValueRef args[] = {
1291 LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, ""),
1292 vindex ? vindex : ctx->i32_0,
1293 voffset,
1294 LLVMConstInt(ctx->i1, glc, 0),
1295 LLVMConstInt(ctx->i1, slc, 0)
1296 };
1297 unsigned func = CLAMP(num_channels, 1, 3) - 1;
1298
1299 LLVMTypeRef types[] = {ctx->f32, ctx->v2f32, ctx->v4f32};
1300 const char *type_names[] = {"f32", "v2f32", "v4f32"};
1301 char name[256];
1302
1303 if (use_format) {
1304 snprintf(name, sizeof(name), "llvm.amdgcn.buffer.load.format.%s",
1305 type_names[func]);
1306 } else {
1307 snprintf(name, sizeof(name), "llvm.amdgcn.buffer.load.%s",
1308 type_names[func]);
1309 }
1310
1311 return ac_build_intrinsic(ctx, name, types[func], args,
1312 ARRAY_SIZE(args),
1313 ac_get_load_intr_attribs(can_speculate));
1314 }
1315
1316 static LLVMValueRef
1317 ac_build_llvm8_buffer_load_common(struct ac_llvm_context *ctx,
1318 LLVMValueRef rsrc,
1319 LLVMValueRef vindex,
1320 LLVMValueRef voffset,
1321 LLVMValueRef soffset,
1322 unsigned num_channels,
1323 bool glc,
1324 bool slc,
1325 bool can_speculate,
1326 bool use_format,
1327 bool structurized)
1328 {
1329 LLVMValueRef args[5];
1330 int idx = 0;
1331 args[idx++] = LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, "");
1332 if (structurized)
1333 args[idx++] = vindex ? vindex : ctx->i32_0;
1334 args[idx++] = voffset ? voffset : ctx->i32_0;
1335 args[idx++] = soffset ? soffset : ctx->i32_0;
1336 args[idx++] = LLVMConstInt(ctx->i32, (glc ? 1 : 0) + (slc ? 2 : 0), 0);
1337 unsigned func = CLAMP(num_channels, 1, 3) - 1;
1338
1339 LLVMTypeRef types[] = {ctx->f32, ctx->v2f32, ctx->v4f32};
1340 const char *type_names[] = {"f32", "v2f32", "v4f32"};
1341 const char *indexing_kind = structurized ? "struct" : "raw";
1342 char name[256];
1343
1344 if (use_format) {
1345 snprintf(name, sizeof(name), "llvm.amdgcn.%s.buffer.load.format.%s",
1346 indexing_kind, type_names[func]);
1347 } else {
1348 snprintf(name, sizeof(name), "llvm.amdgcn.%s.buffer.load.%s",
1349 indexing_kind, type_names[func]);
1350 }
1351
1352 return ac_build_intrinsic(ctx, name, types[func], args,
1353 idx,
1354 ac_get_load_intr_attribs(can_speculate));
1355 }
1356
1357 LLVMValueRef
1358 ac_build_buffer_load(struct ac_llvm_context *ctx,
1359 LLVMValueRef rsrc,
1360 int num_channels,
1361 LLVMValueRef vindex,
1362 LLVMValueRef voffset,
1363 LLVMValueRef soffset,
1364 unsigned inst_offset,
1365 unsigned glc,
1366 unsigned slc,
1367 bool can_speculate,
1368 bool allow_smem)
1369 {
1370 LLVMValueRef offset = LLVMConstInt(ctx->i32, inst_offset, 0);
1371 if (voffset)
1372 offset = LLVMBuildAdd(ctx->builder, offset, voffset, "");
1373 if (soffset)
1374 offset = LLVMBuildAdd(ctx->builder, offset, soffset, "");
1375
1376 if (allow_smem && !slc &&
1377 (!glc || (HAVE_LLVM >= 0x0800 && ctx->chip_class >= VI))) {
1378 assert(vindex == NULL);
1379
1380 LLVMValueRef result[8];
1381
1382 for (int i = 0; i < num_channels; i++) {
1383 if (i) {
1384 offset = LLVMBuildAdd(ctx->builder, offset,
1385 LLVMConstInt(ctx->i32, 4, 0), "");
1386 }
1387 const char *intrname =
1388 HAVE_LLVM >= 0x0800 ? "llvm.amdgcn.s.buffer.load.f32"
1389 : "llvm.SI.load.const.v4i32";
1390 unsigned num_args = HAVE_LLVM >= 0x0800 ? 3 : 2;
1391 LLVMValueRef args[3] = {
1392 rsrc,
1393 offset,
1394 glc ? ctx->i32_1 : ctx->i32_0,
1395 };
1396 result[i] = ac_build_intrinsic(ctx, intrname,
1397 ctx->f32, args, num_args,
1398 AC_FUNC_ATTR_READNONE |
1399 (HAVE_LLVM < 0x0800 ? AC_FUNC_ATTR_LEGACY : 0));
1400 }
1401 if (num_channels == 1)
1402 return result[0];
1403
1404 if (num_channels == 3)
1405 result[num_channels++] = LLVMGetUndef(ctx->f32);
1406 return ac_build_gather_values(ctx, result, num_channels);
1407 }
1408
1409 if (HAVE_LLVM >= 0x0800) {
1410 return ac_build_llvm8_buffer_load_common(ctx, rsrc, vindex,
1411 offset, ctx->i32_0,
1412 num_channels, glc, slc,
1413 can_speculate, false,
1414 false);
1415 }
1416
1417 return ac_build_buffer_load_common(ctx, rsrc, vindex, offset,
1418 num_channels, glc, slc,
1419 can_speculate, false);
1420 }
1421
1422 LLVMValueRef ac_build_buffer_load_format(struct ac_llvm_context *ctx,
1423 LLVMValueRef rsrc,
1424 LLVMValueRef vindex,
1425 LLVMValueRef voffset,
1426 unsigned num_channels,
1427 bool glc,
1428 bool can_speculate)
1429 {
1430 if (HAVE_LLVM >= 0x800) {
1431 return ac_build_llvm8_buffer_load_common(ctx, rsrc, vindex, voffset, ctx->i32_0,
1432 num_channels, glc, false,
1433 can_speculate, true, true);
1434 }
1435 return ac_build_buffer_load_common(ctx, rsrc, vindex, voffset,
1436 num_channels, glc, false,
1437 can_speculate, true);
1438 }
1439
1440 LLVMValueRef ac_build_buffer_load_format_gfx9_safe(struct ac_llvm_context *ctx,
1441 LLVMValueRef rsrc,
1442 LLVMValueRef vindex,
1443 LLVMValueRef voffset,
1444 unsigned num_channels,
1445 bool glc,
1446 bool can_speculate)
1447 {
1448 if (HAVE_LLVM >= 0x800) {
1449 return ac_build_llvm8_buffer_load_common(ctx, rsrc, vindex, voffset, ctx->i32_0,
1450 num_channels, glc, false,
1451 can_speculate, true, true);
1452 }
1453
1454 LLVMValueRef elem_count = LLVMBuildExtractElement(ctx->builder, rsrc, LLVMConstInt(ctx->i32, 2, 0), "");
1455 LLVMValueRef stride = LLVMBuildExtractElement(ctx->builder, rsrc, ctx->i32_1, "");
1456 stride = LLVMBuildLShr(ctx->builder, stride, LLVMConstInt(ctx->i32, 16, 0), "");
1457
1458 LLVMValueRef new_elem_count = LLVMBuildSelect(ctx->builder,
1459 LLVMBuildICmp(ctx->builder, LLVMIntUGT, elem_count, stride, ""),
1460 elem_count, stride, "");
1461
1462 LLVMValueRef new_rsrc = LLVMBuildInsertElement(ctx->builder, rsrc, new_elem_count,
1463 LLVMConstInt(ctx->i32, 2, 0), "");
1464
1465 return ac_build_buffer_load_common(ctx, new_rsrc, vindex, voffset,
1466 num_channels, glc, false,
1467 can_speculate, true);
1468 }
1469
1470 static LLVMValueRef
1471 ac_build_llvm8_tbuffer_load(struct ac_llvm_context *ctx,
1472 LLVMValueRef rsrc,
1473 LLVMValueRef vindex,
1474 LLVMValueRef voffset,
1475 LLVMValueRef soffset,
1476 unsigned num_channels,
1477 unsigned dfmt,
1478 unsigned nfmt,
1479 bool glc,
1480 bool slc,
1481 bool can_speculate,
1482 bool structurized)
1483 {
1484 LLVMValueRef args[6];
1485 int idx = 0;
1486 args[idx++] = LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, "");
1487 if (structurized)
1488 args[idx++] = vindex ? vindex : ctx->i32_0;
1489 args[idx++] = voffset ? voffset : ctx->i32_0;
1490 args[idx++] = soffset ? soffset : ctx->i32_0;
1491 args[idx++] = LLVMConstInt(ctx->i32, dfmt | (nfmt << 4), 0);
1492 args[idx++] = LLVMConstInt(ctx->i32, (glc ? 1 : 0) + (slc ? 2 : 0), 0);
1493 unsigned func = CLAMP(num_channels, 1, 3) - 1;
1494
1495 LLVMTypeRef types[] = {ctx->i32, ctx->v2i32, ctx->v4i32};
1496 const char *type_names[] = {"i32", "v2i32", "v4i32"};
1497 const char *indexing_kind = structurized ? "struct" : "raw";
1498 char name[256];
1499
1500 snprintf(name, sizeof(name), "llvm.amdgcn.%s.tbuffer.load.%s",
1501 indexing_kind, type_names[func]);
1502
1503 return ac_build_intrinsic(ctx, name, types[func], args,
1504 idx,
1505 ac_get_load_intr_attribs(can_speculate));
1506 }
1507
1508 static LLVMValueRef
1509 ac_build_tbuffer_load(struct ac_llvm_context *ctx,
1510 LLVMValueRef rsrc,
1511 LLVMValueRef vindex,
1512 LLVMValueRef voffset,
1513 LLVMValueRef soffset,
1514 LLVMValueRef immoffset,
1515 unsigned num_channels,
1516 unsigned dfmt,
1517 unsigned nfmt,
1518 bool glc,
1519 bool slc,
1520 bool can_speculate,
1521 bool structurized) /* only matters for LLVM 8+ */
1522 {
1523 if (HAVE_LLVM >= 0x800) {
1524 voffset = LLVMBuildAdd(ctx->builder, voffset, immoffset, "");
1525
1526 return ac_build_llvm8_tbuffer_load(ctx, rsrc, vindex, voffset,
1527 soffset, num_channels,
1528 dfmt, nfmt, glc, slc,
1529 can_speculate, structurized);
1530 }
1531
1532 LLVMValueRef args[] = {
1533 rsrc,
1534 vindex ? vindex : ctx->i32_0,
1535 voffset,
1536 soffset,
1537 immoffset,
1538 LLVMConstInt(ctx->i32, dfmt, false),
1539 LLVMConstInt(ctx->i32, nfmt, false),
1540 LLVMConstInt(ctx->i1, glc, false),
1541 LLVMConstInt(ctx->i1, slc, false),
1542 };
1543 unsigned func = CLAMP(num_channels, 1, 3) - 1;
1544 LLVMTypeRef types[] = {ctx->i32, ctx->v2i32, ctx->v4i32};
1545 const char *type_names[] = {"i32", "v2i32", "v4i32"};
1546 char name[256];
1547
1548 snprintf(name, sizeof(name), "llvm.amdgcn.tbuffer.load.%s",
1549 type_names[func]);
1550
1551 return ac_build_intrinsic(ctx, name, types[func], args, 9,
1552 ac_get_load_intr_attribs(can_speculate));
1553 }
1554
1555 LLVMValueRef
1556 ac_build_struct_tbuffer_load(struct ac_llvm_context *ctx,
1557 LLVMValueRef rsrc,
1558 LLVMValueRef vindex,
1559 LLVMValueRef voffset,
1560 LLVMValueRef soffset,
1561 LLVMValueRef immoffset,
1562 unsigned num_channels,
1563 unsigned dfmt,
1564 unsigned nfmt,
1565 bool glc,
1566 bool slc,
1567 bool can_speculate)
1568 {
1569 return ac_build_tbuffer_load(ctx, rsrc, vindex, voffset, soffset,
1570 immoffset, num_channels, dfmt, nfmt, glc,
1571 slc, can_speculate, true);
1572 }
1573
1574 LLVMValueRef
1575 ac_build_raw_tbuffer_load(struct ac_llvm_context *ctx,
1576 LLVMValueRef rsrc,
1577 LLVMValueRef voffset,
1578 LLVMValueRef soffset,
1579 LLVMValueRef immoffset,
1580 unsigned num_channels,
1581 unsigned dfmt,
1582 unsigned nfmt,
1583 bool glc,
1584 bool slc,
1585 bool can_speculate)
1586 {
1587 return ac_build_tbuffer_load(ctx, rsrc, NULL, voffset, soffset,
1588 immoffset, num_channels, dfmt, nfmt, glc,
1589 slc, can_speculate, false);
1590 }
1591
1592 LLVMValueRef
1593 ac_build_tbuffer_load_short(struct ac_llvm_context *ctx,
1594 LLVMValueRef rsrc,
1595 LLVMValueRef voffset,
1596 LLVMValueRef soffset,
1597 LLVMValueRef immoffset,
1598 bool glc)
1599 {
1600 unsigned dfmt = V_008F0C_BUF_DATA_FORMAT_16;
1601 unsigned nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
1602 LLVMValueRef res;
1603
1604 res = ac_build_raw_tbuffer_load(ctx, rsrc, voffset, soffset,
1605 immoffset, 1, dfmt, nfmt, glc, false,
1606 false);
1607
1608 return LLVMBuildTrunc(ctx->builder, res, ctx->i16, "");
1609 }
1610
1611 LLVMValueRef
1612 ac_build_tbuffer_load_byte(struct ac_llvm_context *ctx,
1613 LLVMValueRef rsrc,
1614 LLVMValueRef voffset,
1615 LLVMValueRef soffset,
1616 LLVMValueRef immoffset,
1617 bool glc)
1618 {
1619 unsigned dfmt = V_008F0C_BUF_DATA_FORMAT_8;
1620 unsigned nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
1621 LLVMValueRef res;
1622
1623 res = ac_build_raw_tbuffer_load(ctx, rsrc, voffset, soffset,
1624 immoffset, 1, dfmt, nfmt, glc, false,
1625 false);
1626
1627 return LLVMBuildTrunc(ctx->builder, res, ctx->i8, "");
1628 }
1629 static void
1630 ac_build_llvm8_tbuffer_store(struct ac_llvm_context *ctx,
1631 LLVMValueRef rsrc,
1632 LLVMValueRef vdata,
1633 LLVMValueRef vindex,
1634 LLVMValueRef voffset,
1635 LLVMValueRef soffset,
1636 unsigned num_channels,
1637 unsigned dfmt,
1638 unsigned nfmt,
1639 bool glc,
1640 bool slc,
1641 bool writeonly_memory,
1642 bool structurized)
1643 {
1644 LLVMValueRef args[7];
1645 int idx = 0;
1646 args[idx++] = vdata;
1647 args[idx++] = LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, "");
1648 if (structurized)
1649 args[idx++] = vindex ? vindex : ctx->i32_0;
1650 args[idx++] = voffset ? voffset : ctx->i32_0;
1651 args[idx++] = soffset ? soffset : ctx->i32_0;
1652 args[idx++] = LLVMConstInt(ctx->i32, dfmt | (nfmt << 4), 0);
1653 args[idx++] = LLVMConstInt(ctx->i32, (glc ? 1 : 0) + (slc ? 2 : 0), 0);
1654 unsigned func = CLAMP(num_channels, 1, 3) - 1;
1655
1656 const char *type_names[] = {"i32", "v2i32", "v4i32"};
1657 const char *indexing_kind = structurized ? "struct" : "raw";
1658 char name[256];
1659
1660 snprintf(name, sizeof(name), "llvm.amdgcn.%s.tbuffer.store.%s",
1661 indexing_kind, type_names[func]);
1662
1663 ac_build_intrinsic(ctx, name, ctx->voidt, args, idx,
1664 ac_get_store_intr_attribs(writeonly_memory));
1665 }
1666
1667 static void
1668 ac_build_tbuffer_store(struct ac_llvm_context *ctx,
1669 LLVMValueRef rsrc,
1670 LLVMValueRef vdata,
1671 LLVMValueRef vindex,
1672 LLVMValueRef voffset,
1673 LLVMValueRef soffset,
1674 LLVMValueRef immoffset,
1675 unsigned num_channels,
1676 unsigned dfmt,
1677 unsigned nfmt,
1678 bool glc,
1679 bool slc,
1680 bool writeonly_memory,
1681 bool structurized) /* only matters for LLVM 8+ */
1682 {
1683 if (HAVE_LLVM >= 0x800) {
1684 voffset = LLVMBuildAdd(ctx->builder,
1685 voffset ? voffset : ctx->i32_0,
1686 immoffset, "");
1687
1688 ac_build_llvm8_tbuffer_store(ctx, rsrc, vdata, vindex, voffset,
1689 soffset, num_channels, dfmt, nfmt,
1690 glc, slc, writeonly_memory,
1691 structurized);
1692 } else {
1693 LLVMValueRef params[] = {
1694 vdata,
1695 rsrc,
1696 vindex ? vindex : ctx->i32_0,
1697 voffset ? voffset : ctx->i32_0,
1698 soffset ? soffset : ctx->i32_0,
1699 immoffset,
1700 LLVMConstInt(ctx->i32, dfmt, false),
1701 LLVMConstInt(ctx->i32, nfmt, false),
1702 LLVMConstInt(ctx->i1, glc, false),
1703 LLVMConstInt(ctx->i1, slc, false),
1704 };
1705 unsigned func = CLAMP(num_channels, 1, 3) - 1;
1706 const char *type_names[] = {"i32", "v2i32", "v4i32"};
1707 char name[256];
1708
1709 snprintf(name, sizeof(name), "llvm.amdgcn.tbuffer.store.%s",
1710 type_names[func]);
1711
1712 ac_build_intrinsic(ctx, name, ctx->voidt, params, 10,
1713 ac_get_store_intr_attribs(writeonly_memory));
1714 }
1715 }
1716
1717 void
1718 ac_build_struct_tbuffer_store(struct ac_llvm_context *ctx,
1719 LLVMValueRef rsrc,
1720 LLVMValueRef vdata,
1721 LLVMValueRef vindex,
1722 LLVMValueRef voffset,
1723 LLVMValueRef soffset,
1724 LLVMValueRef immoffset,
1725 unsigned num_channels,
1726 unsigned dfmt,
1727 unsigned nfmt,
1728 bool glc,
1729 bool slc,
1730 bool writeonly_memory)
1731 {
1732 ac_build_tbuffer_store(ctx, rsrc, vdata, vindex, voffset, soffset,
1733 immoffset, num_channels, dfmt, nfmt, glc, slc,
1734 writeonly_memory, true);
1735 }
1736
1737 void
1738 ac_build_raw_tbuffer_store(struct ac_llvm_context *ctx,
1739 LLVMValueRef rsrc,
1740 LLVMValueRef vdata,
1741 LLVMValueRef voffset,
1742 LLVMValueRef soffset,
1743 LLVMValueRef immoffset,
1744 unsigned num_channels,
1745 unsigned dfmt,
1746 unsigned nfmt,
1747 bool glc,
1748 bool slc,
1749 bool writeonly_memory)
1750 {
1751 ac_build_tbuffer_store(ctx, rsrc, vdata, NULL, voffset, soffset,
1752 immoffset, num_channels, dfmt, nfmt, glc, slc,
1753 writeonly_memory, false);
1754 }
1755
1756 void
1757 ac_build_tbuffer_store_short(struct ac_llvm_context *ctx,
1758 LLVMValueRef rsrc,
1759 LLVMValueRef vdata,
1760 LLVMValueRef voffset,
1761 LLVMValueRef soffset,
1762 bool glc,
1763 bool writeonly_memory)
1764 {
1765 unsigned dfmt = V_008F0C_BUF_DATA_FORMAT_16;
1766 unsigned nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
1767
1768 vdata = LLVMBuildBitCast(ctx->builder, vdata, ctx->i16, "");
1769 vdata = LLVMBuildZExt(ctx->builder, vdata, ctx->i32, "");
1770
1771 ac_build_raw_tbuffer_store(ctx, rsrc, vdata, voffset, soffset,
1772 ctx->i32_0, 1, dfmt, nfmt, glc, false,
1773 writeonly_memory);
1774 }
1775
1776 void
1777 ac_build_tbuffer_store_byte(struct ac_llvm_context *ctx,
1778 LLVMValueRef rsrc,
1779 LLVMValueRef vdata,
1780 LLVMValueRef voffset,
1781 LLVMValueRef soffset,
1782 bool glc,
1783 bool writeonly_memory)
1784 {
1785 unsigned dfmt = V_008F0C_BUF_DATA_FORMAT_8;
1786 unsigned nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
1787
1788 vdata = LLVMBuildBitCast(ctx->builder, vdata, ctx->i8, "");
1789 vdata = LLVMBuildZExt(ctx->builder, vdata, ctx->i32, "");
1790
1791 ac_build_raw_tbuffer_store(ctx, rsrc, vdata, voffset, soffset,
1792 ctx->i32_0, 1, dfmt, nfmt, glc, false,
1793 writeonly_memory);
1794 }
1795 /**
1796 * Set range metadata on an instruction. This can only be used on load and
1797 * call instructions. If you know an instruction can only produce the values
1798 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
1799 * \p lo is the minimum value inclusive.
1800 * \p hi is the maximum value exclusive.
1801 */
1802 static void set_range_metadata(struct ac_llvm_context *ctx,
1803 LLVMValueRef value, unsigned lo, unsigned hi)
1804 {
1805 LLVMValueRef range_md, md_args[2];
1806 LLVMTypeRef type = LLVMTypeOf(value);
1807 LLVMContextRef context = LLVMGetTypeContext(type);
1808
1809 md_args[0] = LLVMConstInt(type, lo, false);
1810 md_args[1] = LLVMConstInt(type, hi, false);
1811 range_md = LLVMMDNodeInContext(context, md_args, 2);
1812 LLVMSetMetadata(value, ctx->range_md_kind, range_md);
1813 }
1814
1815 LLVMValueRef
1816 ac_get_thread_id(struct ac_llvm_context *ctx)
1817 {
1818 LLVMValueRef tid;
1819
1820 LLVMValueRef tid_args[2];
1821 tid_args[0] = LLVMConstInt(ctx->i32, 0xffffffff, false);
1822 tid_args[1] = ctx->i32_0;
1823 tid_args[1] = ac_build_intrinsic(ctx,
1824 "llvm.amdgcn.mbcnt.lo", ctx->i32,
1825 tid_args, 2, AC_FUNC_ATTR_READNONE);
1826
1827 tid = ac_build_intrinsic(ctx, "llvm.amdgcn.mbcnt.hi",
1828 ctx->i32, tid_args,
1829 2, AC_FUNC_ATTR_READNONE);
1830 set_range_metadata(ctx, tid, 0, 64);
1831 return tid;
1832 }
1833
1834 /*
1835 * SI implements derivatives using the local data store (LDS)
1836 * All writes to the LDS happen in all executing threads at
1837 * the same time. TID is the Thread ID for the current
1838 * thread and is a value between 0 and 63, representing
1839 * the thread's position in the wavefront.
1840 *
1841 * For the pixel shader threads are grouped into quads of four pixels.
1842 * The TIDs of the pixels of a quad are:
1843 *
1844 * +------+------+
1845 * |4n + 0|4n + 1|
1846 * +------+------+
1847 * |4n + 2|4n + 3|
1848 * +------+------+
1849 *
1850 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
1851 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
1852 * the current pixel's column, and masking with 0xfffffffe yields the TID
1853 * of the left pixel of the current pixel's row.
1854 *
1855 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
1856 * adding 2 yields the TID of the pixel below the top pixel.
1857 */
1858 LLVMValueRef
1859 ac_build_ddxy(struct ac_llvm_context *ctx,
1860 uint32_t mask,
1861 int idx,
1862 LLVMValueRef val)
1863 {
1864 unsigned tl_lanes[4], trbl_lanes[4];
1865 LLVMValueRef tl, trbl;
1866 LLVMValueRef result;
1867
1868 for (unsigned i = 0; i < 4; ++i) {
1869 tl_lanes[i] = i & mask;
1870 trbl_lanes[i] = (i & mask) + idx;
1871 }
1872
1873 tl = ac_build_quad_swizzle(ctx, val,
1874 tl_lanes[0], tl_lanes[1],
1875 tl_lanes[2], tl_lanes[3]);
1876 trbl = ac_build_quad_swizzle(ctx, val,
1877 trbl_lanes[0], trbl_lanes[1],
1878 trbl_lanes[2], trbl_lanes[3]);
1879
1880 tl = LLVMBuildBitCast(ctx->builder, tl, ctx->f32, "");
1881 trbl = LLVMBuildBitCast(ctx->builder, trbl, ctx->f32, "");
1882 result = LLVMBuildFSub(ctx->builder, trbl, tl, "");
1883
1884 result = ac_build_intrinsic(ctx, "llvm.amdgcn.wqm.f32", ctx->f32,
1885 &result, 1, 0);
1886
1887 return result;
1888 }
1889
1890 void
1891 ac_build_sendmsg(struct ac_llvm_context *ctx,
1892 uint32_t msg,
1893 LLVMValueRef wave_id)
1894 {
1895 LLVMValueRef args[2];
1896 args[0] = LLVMConstInt(ctx->i32, msg, false);
1897 args[1] = wave_id;
1898 ac_build_intrinsic(ctx, "llvm.amdgcn.s.sendmsg", ctx->voidt, args, 2, 0);
1899 }
1900
1901 LLVMValueRef
1902 ac_build_imsb(struct ac_llvm_context *ctx,
1903 LLVMValueRef arg,
1904 LLVMTypeRef dst_type)
1905 {
1906 LLVMValueRef msb = ac_build_intrinsic(ctx, "llvm.amdgcn.sffbh.i32",
1907 dst_type, &arg, 1,
1908 AC_FUNC_ATTR_READNONE);
1909
1910 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
1911 * the index from LSB. Invert it by doing "31 - msb". */
1912 msb = LLVMBuildSub(ctx->builder, LLVMConstInt(ctx->i32, 31, false),
1913 msb, "");
1914
1915 LLVMValueRef all_ones = LLVMConstInt(ctx->i32, -1, true);
1916 LLVMValueRef cond = LLVMBuildOr(ctx->builder,
1917 LLVMBuildICmp(ctx->builder, LLVMIntEQ,
1918 arg, ctx->i32_0, ""),
1919 LLVMBuildICmp(ctx->builder, LLVMIntEQ,
1920 arg, all_ones, ""), "");
1921
1922 return LLVMBuildSelect(ctx->builder, cond, all_ones, msb, "");
1923 }
1924
1925 LLVMValueRef
1926 ac_build_umsb(struct ac_llvm_context *ctx,
1927 LLVMValueRef arg,
1928 LLVMTypeRef dst_type)
1929 {
1930 const char *intrin_name;
1931 LLVMTypeRef type;
1932 LLVMValueRef highest_bit;
1933 LLVMValueRef zero;
1934 unsigned bitsize;
1935
1936 bitsize = ac_get_elem_bits(ctx, LLVMTypeOf(arg));
1937 switch (bitsize) {
1938 case 64:
1939 intrin_name = "llvm.ctlz.i64";
1940 type = ctx->i64;
1941 highest_bit = LLVMConstInt(ctx->i64, 63, false);
1942 zero = ctx->i64_0;
1943 break;
1944 case 32:
1945 intrin_name = "llvm.ctlz.i32";
1946 type = ctx->i32;
1947 highest_bit = LLVMConstInt(ctx->i32, 31, false);
1948 zero = ctx->i32_0;
1949 break;
1950 case 16:
1951 intrin_name = "llvm.ctlz.i16";
1952 type = ctx->i16;
1953 highest_bit = LLVMConstInt(ctx->i16, 15, false);
1954 zero = ctx->i16_0;
1955 break;
1956 default:
1957 unreachable(!"invalid bitsize");
1958 break;
1959 }
1960
1961 LLVMValueRef params[2] = {
1962 arg,
1963 ctx->i1true,
1964 };
1965
1966 LLVMValueRef msb = ac_build_intrinsic(ctx, intrin_name, type,
1967 params, 2,
1968 AC_FUNC_ATTR_READNONE);
1969
1970 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
1971 * the index from LSB. Invert it by doing "31 - msb". */
1972 msb = LLVMBuildSub(ctx->builder, highest_bit, msb, "");
1973
1974 if (bitsize == 64) {
1975 msb = LLVMBuildTrunc(ctx->builder, msb, ctx->i32, "");
1976 } else if (bitsize == 16) {
1977 msb = LLVMBuildSExt(ctx->builder, msb, ctx->i32, "");
1978 }
1979
1980 /* check for zero */
1981 return LLVMBuildSelect(ctx->builder,
1982 LLVMBuildICmp(ctx->builder, LLVMIntEQ, arg, zero, ""),
1983 LLVMConstInt(ctx->i32, -1, true), msb, "");
1984 }
1985
1986 LLVMValueRef ac_build_fmin(struct ac_llvm_context *ctx, LLVMValueRef a,
1987 LLVMValueRef b)
1988 {
1989 char name[64];
1990 snprintf(name, sizeof(name), "llvm.minnum.f%d", ac_get_elem_bits(ctx, LLVMTypeOf(a)));
1991 LLVMValueRef args[2] = {a, b};
1992 return ac_build_intrinsic(ctx, name, LLVMTypeOf(a), args, 2,
1993 AC_FUNC_ATTR_READNONE);
1994 }
1995
1996 LLVMValueRef ac_build_fmax(struct ac_llvm_context *ctx, LLVMValueRef a,
1997 LLVMValueRef b)
1998 {
1999 char name[64];
2000 snprintf(name, sizeof(name), "llvm.maxnum.f%d", ac_get_elem_bits(ctx, LLVMTypeOf(a)));
2001 LLVMValueRef args[2] = {a, b};
2002 return ac_build_intrinsic(ctx, name, LLVMTypeOf(a), args, 2,
2003 AC_FUNC_ATTR_READNONE);
2004 }
2005
2006 LLVMValueRef ac_build_imin(struct ac_llvm_context *ctx, LLVMValueRef a,
2007 LLVMValueRef b)
2008 {
2009 LLVMValueRef cmp = LLVMBuildICmp(ctx->builder, LLVMIntSLE, a, b, "");
2010 return LLVMBuildSelect(ctx->builder, cmp, a, b, "");
2011 }
2012
2013 LLVMValueRef ac_build_imax(struct ac_llvm_context *ctx, LLVMValueRef a,
2014 LLVMValueRef b)
2015 {
2016 LLVMValueRef cmp = LLVMBuildICmp(ctx->builder, LLVMIntSGT, a, b, "");
2017 return LLVMBuildSelect(ctx->builder, cmp, a, b, "");
2018 }
2019
2020 LLVMValueRef ac_build_umin(struct ac_llvm_context *ctx, LLVMValueRef a,
2021 LLVMValueRef b)
2022 {
2023 LLVMValueRef cmp = LLVMBuildICmp(ctx->builder, LLVMIntULE, a, b, "");
2024 return LLVMBuildSelect(ctx->builder, cmp, a, b, "");
2025 }
2026
2027 LLVMValueRef ac_build_clamp(struct ac_llvm_context *ctx, LLVMValueRef value)
2028 {
2029 LLVMTypeRef t = LLVMTypeOf(value);
2030 return ac_build_fmin(ctx, ac_build_fmax(ctx, value, LLVMConstReal(t, 0.0)),
2031 LLVMConstReal(t, 1.0));
2032 }
2033
2034 void ac_build_export(struct ac_llvm_context *ctx, struct ac_export_args *a)
2035 {
2036 LLVMValueRef args[9];
2037
2038 args[0] = LLVMConstInt(ctx->i32, a->target, 0);
2039 args[1] = LLVMConstInt(ctx->i32, a->enabled_channels, 0);
2040
2041 if (a->compr) {
2042 LLVMTypeRef i16 = LLVMInt16TypeInContext(ctx->context);
2043 LLVMTypeRef v2i16 = LLVMVectorType(i16, 2);
2044
2045 args[2] = LLVMBuildBitCast(ctx->builder, a->out[0],
2046 v2i16, "");
2047 args[3] = LLVMBuildBitCast(ctx->builder, a->out[1],
2048 v2i16, "");
2049 args[4] = LLVMConstInt(ctx->i1, a->done, 0);
2050 args[5] = LLVMConstInt(ctx->i1, a->valid_mask, 0);
2051
2052 ac_build_intrinsic(ctx, "llvm.amdgcn.exp.compr.v2i16",
2053 ctx->voidt, args, 6, 0);
2054 } else {
2055 args[2] = a->out[0];
2056 args[3] = a->out[1];
2057 args[4] = a->out[2];
2058 args[5] = a->out[3];
2059 args[6] = LLVMConstInt(ctx->i1, a->done, 0);
2060 args[7] = LLVMConstInt(ctx->i1, a->valid_mask, 0);
2061
2062 ac_build_intrinsic(ctx, "llvm.amdgcn.exp.f32",
2063 ctx->voidt, args, 8, 0);
2064 }
2065 }
2066
2067 void ac_build_export_null(struct ac_llvm_context *ctx)
2068 {
2069 struct ac_export_args args;
2070
2071 args.enabled_channels = 0x0; /* enabled channels */
2072 args.valid_mask = 1; /* whether the EXEC mask is valid */
2073 args.done = 1; /* DONE bit */
2074 args.target = V_008DFC_SQ_EXP_NULL;
2075 args.compr = 0; /* COMPR flag (0 = 32-bit export) */
2076 args.out[0] = LLVMGetUndef(ctx->f32); /* R */
2077 args.out[1] = LLVMGetUndef(ctx->f32); /* G */
2078 args.out[2] = LLVMGetUndef(ctx->f32); /* B */
2079 args.out[3] = LLVMGetUndef(ctx->f32); /* A */
2080
2081 ac_build_export(ctx, &args);
2082 }
2083
2084 static unsigned ac_num_coords(enum ac_image_dim dim)
2085 {
2086 switch (dim) {
2087 case ac_image_1d:
2088 return 1;
2089 case ac_image_2d:
2090 case ac_image_1darray:
2091 return 2;
2092 case ac_image_3d:
2093 case ac_image_cube:
2094 case ac_image_2darray:
2095 case ac_image_2dmsaa:
2096 return 3;
2097 case ac_image_2darraymsaa:
2098 return 4;
2099 default:
2100 unreachable("ac_num_coords: bad dim");
2101 }
2102 }
2103
2104 static unsigned ac_num_derivs(enum ac_image_dim dim)
2105 {
2106 switch (dim) {
2107 case ac_image_1d:
2108 case ac_image_1darray:
2109 return 2;
2110 case ac_image_2d:
2111 case ac_image_2darray:
2112 case ac_image_cube:
2113 return 4;
2114 case ac_image_3d:
2115 return 6;
2116 case ac_image_2dmsaa:
2117 case ac_image_2darraymsaa:
2118 default:
2119 unreachable("derivatives not supported");
2120 }
2121 }
2122
2123 static const char *get_atomic_name(enum ac_atomic_op op)
2124 {
2125 switch (op) {
2126 case ac_atomic_swap: return "swap";
2127 case ac_atomic_add: return "add";
2128 case ac_atomic_sub: return "sub";
2129 case ac_atomic_smin: return "smin";
2130 case ac_atomic_umin: return "umin";
2131 case ac_atomic_smax: return "smax";
2132 case ac_atomic_umax: return "umax";
2133 case ac_atomic_and: return "and";
2134 case ac_atomic_or: return "or";
2135 case ac_atomic_xor: return "xor";
2136 }
2137 unreachable("bad atomic op");
2138 }
2139
2140 LLVMValueRef ac_build_image_opcode(struct ac_llvm_context *ctx,
2141 struct ac_image_args *a)
2142 {
2143 const char *overload[3] = { "", "", "" };
2144 unsigned num_overloads = 0;
2145 LLVMValueRef args[18];
2146 unsigned num_args = 0;
2147 enum ac_image_dim dim = a->dim;
2148
2149 assert(!a->lod || a->lod == ctx->i32_0 || a->lod == ctx->f32_0 ||
2150 !a->level_zero);
2151 assert((a->opcode != ac_image_get_resinfo && a->opcode != ac_image_load_mip &&
2152 a->opcode != ac_image_store_mip) ||
2153 a->lod);
2154 assert(a->opcode == ac_image_sample || a->opcode == ac_image_gather4 ||
2155 (!a->compare && !a->offset));
2156 assert((a->opcode == ac_image_sample || a->opcode == ac_image_gather4 ||
2157 a->opcode == ac_image_get_lod) ||
2158 !a->bias);
2159 assert((a->bias ? 1 : 0) +
2160 (a->lod ? 1 : 0) +
2161 (a->level_zero ? 1 : 0) +
2162 (a->derivs[0] ? 1 : 0) <= 1);
2163
2164 if (a->opcode == ac_image_get_lod) {
2165 switch (dim) {
2166 case ac_image_1darray:
2167 dim = ac_image_1d;
2168 break;
2169 case ac_image_2darray:
2170 case ac_image_cube:
2171 dim = ac_image_2d;
2172 break;
2173 default:
2174 break;
2175 }
2176 }
2177
2178 bool sample = a->opcode == ac_image_sample ||
2179 a->opcode == ac_image_gather4 ||
2180 a->opcode == ac_image_get_lod;
2181 bool atomic = a->opcode == ac_image_atomic ||
2182 a->opcode == ac_image_atomic_cmpswap;
2183 LLVMTypeRef coord_type = sample ? ctx->f32 : ctx->i32;
2184
2185 if (atomic || a->opcode == ac_image_store || a->opcode == ac_image_store_mip) {
2186 args[num_args++] = a->data[0];
2187 if (a->opcode == ac_image_atomic_cmpswap)
2188 args[num_args++] = a->data[1];
2189 }
2190
2191 if (!atomic)
2192 args[num_args++] = LLVMConstInt(ctx->i32, a->dmask, false);
2193
2194 if (a->offset)
2195 args[num_args++] = ac_to_integer(ctx, a->offset);
2196 if (a->bias) {
2197 args[num_args++] = ac_to_float(ctx, a->bias);
2198 overload[num_overloads++] = ".f32";
2199 }
2200 if (a->compare)
2201 args[num_args++] = ac_to_float(ctx, a->compare);
2202 if (a->derivs[0]) {
2203 unsigned count = ac_num_derivs(dim);
2204 for (unsigned i = 0; i < count; ++i)
2205 args[num_args++] = ac_to_float(ctx, a->derivs[i]);
2206 overload[num_overloads++] = ".f32";
2207 }
2208 unsigned num_coords =
2209 a->opcode != ac_image_get_resinfo ? ac_num_coords(dim) : 0;
2210 for (unsigned i = 0; i < num_coords; ++i)
2211 args[num_args++] = LLVMBuildBitCast(ctx->builder, a->coords[i], coord_type, "");
2212 if (a->lod)
2213 args[num_args++] = LLVMBuildBitCast(ctx->builder, a->lod, coord_type, "");
2214 overload[num_overloads++] = sample ? ".f32" : ".i32";
2215
2216 args[num_args++] = a->resource;
2217 if (sample) {
2218 args[num_args++] = a->sampler;
2219 args[num_args++] = LLVMConstInt(ctx->i1, a->unorm, false);
2220 }
2221
2222 args[num_args++] = ctx->i32_0; /* texfailctrl */
2223 args[num_args++] = LLVMConstInt(ctx->i32, a->cache_policy, false);
2224
2225 const char *name;
2226 const char *atomic_subop = "";
2227 switch (a->opcode) {
2228 case ac_image_sample: name = "sample"; break;
2229 case ac_image_gather4: name = "gather4"; break;
2230 case ac_image_load: name = "load"; break;
2231 case ac_image_load_mip: name = "load.mip"; break;
2232 case ac_image_store: name = "store"; break;
2233 case ac_image_store_mip: name = "store.mip"; break;
2234 case ac_image_atomic:
2235 name = "atomic.";
2236 atomic_subop = get_atomic_name(a->atomic);
2237 break;
2238 case ac_image_atomic_cmpswap:
2239 name = "atomic.";
2240 atomic_subop = "cmpswap";
2241 break;
2242 case ac_image_get_lod: name = "getlod"; break;
2243 case ac_image_get_resinfo: name = "getresinfo"; break;
2244 default: unreachable("invalid image opcode");
2245 }
2246
2247 const char *dimname;
2248 switch (dim) {
2249 case ac_image_1d: dimname = "1d"; break;
2250 case ac_image_2d: dimname = "2d"; break;
2251 case ac_image_3d: dimname = "3d"; break;
2252 case ac_image_cube: dimname = "cube"; break;
2253 case ac_image_1darray: dimname = "1darray"; break;
2254 case ac_image_2darray: dimname = "2darray"; break;
2255 case ac_image_2dmsaa: dimname = "2dmsaa"; break;
2256 case ac_image_2darraymsaa: dimname = "2darraymsaa"; break;
2257 default: unreachable("invalid dim");
2258 }
2259
2260 bool lod_suffix =
2261 a->lod && (a->opcode == ac_image_sample || a->opcode == ac_image_gather4);
2262 char intr_name[96];
2263 snprintf(intr_name, sizeof(intr_name),
2264 "llvm.amdgcn.image.%s%s" /* base name */
2265 "%s%s%s" /* sample/gather modifiers */
2266 ".%s.%s%s%s%s", /* dimension and type overloads */
2267 name, atomic_subop,
2268 a->compare ? ".c" : "",
2269 a->bias ? ".b" :
2270 lod_suffix ? ".l" :
2271 a->derivs[0] ? ".d" :
2272 a->level_zero ? ".lz" : "",
2273 a->offset ? ".o" : "",
2274 dimname,
2275 atomic ? "i32" : "v4f32",
2276 overload[0], overload[1], overload[2]);
2277
2278 LLVMTypeRef retty;
2279 if (atomic)
2280 retty = ctx->i32;
2281 else if (a->opcode == ac_image_store || a->opcode == ac_image_store_mip)
2282 retty = ctx->voidt;
2283 else
2284 retty = ctx->v4f32;
2285
2286 LLVMValueRef result =
2287 ac_build_intrinsic(ctx, intr_name, retty, args, num_args,
2288 a->attributes);
2289 if (!sample && retty == ctx->v4f32) {
2290 result = LLVMBuildBitCast(ctx->builder, result,
2291 ctx->v4i32, "");
2292 }
2293 return result;
2294 }
2295
2296 LLVMValueRef ac_build_cvt_pkrtz_f16(struct ac_llvm_context *ctx,
2297 LLVMValueRef args[2])
2298 {
2299 LLVMTypeRef v2f16 =
2300 LLVMVectorType(LLVMHalfTypeInContext(ctx->context), 2);
2301
2302 return ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pkrtz", v2f16,
2303 args, 2, AC_FUNC_ATTR_READNONE);
2304 }
2305
2306 LLVMValueRef ac_build_cvt_pknorm_i16(struct ac_llvm_context *ctx,
2307 LLVMValueRef args[2])
2308 {
2309 LLVMValueRef res =
2310 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pknorm.i16",
2311 ctx->v2i16, args, 2,
2312 AC_FUNC_ATTR_READNONE);
2313 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
2314 }
2315
2316 LLVMValueRef ac_build_cvt_pknorm_u16(struct ac_llvm_context *ctx,
2317 LLVMValueRef args[2])
2318 {
2319 LLVMValueRef res =
2320 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pknorm.u16",
2321 ctx->v2i16, args, 2,
2322 AC_FUNC_ATTR_READNONE);
2323 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
2324 }
2325
2326 /* The 8-bit and 10-bit clamping is for HW workarounds. */
2327 LLVMValueRef ac_build_cvt_pk_i16(struct ac_llvm_context *ctx,
2328 LLVMValueRef args[2], unsigned bits, bool hi)
2329 {
2330 assert(bits == 8 || bits == 10 || bits == 16);
2331
2332 LLVMValueRef max_rgb = LLVMConstInt(ctx->i32,
2333 bits == 8 ? 127 : bits == 10 ? 511 : 32767, 0);
2334 LLVMValueRef min_rgb = LLVMConstInt(ctx->i32,
2335 bits == 8 ? -128 : bits == 10 ? -512 : -32768, 0);
2336 LLVMValueRef max_alpha =
2337 bits != 10 ? max_rgb : ctx->i32_1;
2338 LLVMValueRef min_alpha =
2339 bits != 10 ? min_rgb : LLVMConstInt(ctx->i32, -2, 0);
2340
2341 /* Clamp. */
2342 if (bits != 16) {
2343 for (int i = 0; i < 2; i++) {
2344 bool alpha = hi && i == 1;
2345 args[i] = ac_build_imin(ctx, args[i],
2346 alpha ? max_alpha : max_rgb);
2347 args[i] = ac_build_imax(ctx, args[i],
2348 alpha ? min_alpha : min_rgb);
2349 }
2350 }
2351
2352 LLVMValueRef res =
2353 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pk.i16",
2354 ctx->v2i16, args, 2,
2355 AC_FUNC_ATTR_READNONE);
2356 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
2357 }
2358
2359 /* The 8-bit and 10-bit clamping is for HW workarounds. */
2360 LLVMValueRef ac_build_cvt_pk_u16(struct ac_llvm_context *ctx,
2361 LLVMValueRef args[2], unsigned bits, bool hi)
2362 {
2363 assert(bits == 8 || bits == 10 || bits == 16);
2364
2365 LLVMValueRef max_rgb = LLVMConstInt(ctx->i32,
2366 bits == 8 ? 255 : bits == 10 ? 1023 : 65535, 0);
2367 LLVMValueRef max_alpha =
2368 bits != 10 ? max_rgb : LLVMConstInt(ctx->i32, 3, 0);
2369
2370 /* Clamp. */
2371 if (bits != 16) {
2372 for (int i = 0; i < 2; i++) {
2373 bool alpha = hi && i == 1;
2374 args[i] = ac_build_umin(ctx, args[i],
2375 alpha ? max_alpha : max_rgb);
2376 }
2377 }
2378
2379 LLVMValueRef res =
2380 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pk.u16",
2381 ctx->v2i16, args, 2,
2382 AC_FUNC_ATTR_READNONE);
2383 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
2384 }
2385
2386 LLVMValueRef ac_build_wqm_vote(struct ac_llvm_context *ctx, LLVMValueRef i1)
2387 {
2388 return ac_build_intrinsic(ctx, "llvm.amdgcn.wqm.vote", ctx->i1,
2389 &i1, 1, AC_FUNC_ATTR_READNONE);
2390 }
2391
2392 void ac_build_kill_if_false(struct ac_llvm_context *ctx, LLVMValueRef i1)
2393 {
2394 ac_build_intrinsic(ctx, "llvm.amdgcn.kill", ctx->voidt,
2395 &i1, 1, 0);
2396 }
2397
2398 LLVMValueRef ac_build_bfe(struct ac_llvm_context *ctx, LLVMValueRef input,
2399 LLVMValueRef offset, LLVMValueRef width,
2400 bool is_signed)
2401 {
2402 LLVMValueRef args[] = {
2403 input,
2404 offset,
2405 width,
2406 };
2407
2408 return ac_build_intrinsic(ctx,
2409 is_signed ? "llvm.amdgcn.sbfe.i32" :
2410 "llvm.amdgcn.ubfe.i32",
2411 ctx->i32, args, 3,
2412 AC_FUNC_ATTR_READNONE);
2413 }
2414
2415 LLVMValueRef ac_build_imad(struct ac_llvm_context *ctx, LLVMValueRef s0,
2416 LLVMValueRef s1, LLVMValueRef s2)
2417 {
2418 return LLVMBuildAdd(ctx->builder,
2419 LLVMBuildMul(ctx->builder, s0, s1, ""), s2, "");
2420 }
2421
2422 LLVMValueRef ac_build_fmad(struct ac_llvm_context *ctx, LLVMValueRef s0,
2423 LLVMValueRef s1, LLVMValueRef s2)
2424 {
2425 return LLVMBuildFAdd(ctx->builder,
2426 LLVMBuildFMul(ctx->builder, s0, s1, ""), s2, "");
2427 }
2428
2429 void ac_build_waitcnt(struct ac_llvm_context *ctx, unsigned simm16)
2430 {
2431 LLVMValueRef args[1] = {
2432 LLVMConstInt(ctx->i32, simm16, false),
2433 };
2434 ac_build_intrinsic(ctx, "llvm.amdgcn.s.waitcnt",
2435 ctx->voidt, args, 1, 0);
2436 }
2437
2438 LLVMValueRef ac_build_fmed3(struct ac_llvm_context *ctx, LLVMValueRef src0,
2439 LLVMValueRef src1, LLVMValueRef src2,
2440 unsigned bitsize)
2441 {
2442 LLVMTypeRef type;
2443 char *intr;
2444
2445 if (bitsize == 16) {
2446 intr = "llvm.amdgcn.fmed3.f16";
2447 type = ctx->f16;
2448 } else if (bitsize == 32) {
2449 intr = "llvm.amdgcn.fmed3.f32";
2450 type = ctx->f32;
2451 } else {
2452 intr = "llvm.amdgcn.fmed3.f64";
2453 type = ctx->f64;
2454 }
2455
2456 LLVMValueRef params[] = {
2457 src0,
2458 src1,
2459 src2,
2460 };
2461 return ac_build_intrinsic(ctx, intr, type, params, 3,
2462 AC_FUNC_ATTR_READNONE);
2463 }
2464
2465 LLVMValueRef ac_build_fract(struct ac_llvm_context *ctx, LLVMValueRef src0,
2466 unsigned bitsize)
2467 {
2468 LLVMTypeRef type;
2469 char *intr;
2470
2471 if (bitsize == 16) {
2472 intr = "llvm.amdgcn.fract.f16";
2473 type = ctx->f16;
2474 } else if (bitsize == 32) {
2475 intr = "llvm.amdgcn.fract.f32";
2476 type = ctx->f32;
2477 } else {
2478 intr = "llvm.amdgcn.fract.f64";
2479 type = ctx->f64;
2480 }
2481
2482 LLVMValueRef params[] = {
2483 src0,
2484 };
2485 return ac_build_intrinsic(ctx, intr, type, params, 1,
2486 AC_FUNC_ATTR_READNONE);
2487 }
2488
2489 LLVMValueRef ac_build_isign(struct ac_llvm_context *ctx, LLVMValueRef src0,
2490 unsigned bitsize)
2491 {
2492 LLVMTypeRef type = LLVMIntTypeInContext(ctx->context, bitsize);
2493 LLVMValueRef zero = LLVMConstInt(type, 0, false);
2494 LLVMValueRef one = LLVMConstInt(type, 1, false);
2495
2496 LLVMValueRef cmp, val;
2497 cmp = LLVMBuildICmp(ctx->builder, LLVMIntSGT, src0, zero, "");
2498 val = LLVMBuildSelect(ctx->builder, cmp, one, src0, "");
2499 cmp = LLVMBuildICmp(ctx->builder, LLVMIntSGE, val, zero, "");
2500 val = LLVMBuildSelect(ctx->builder, cmp, val, LLVMConstInt(type, -1, true), "");
2501 return val;
2502 }
2503
2504 LLVMValueRef ac_build_fsign(struct ac_llvm_context *ctx, LLVMValueRef src0,
2505 unsigned bitsize)
2506 {
2507 LLVMValueRef cmp, val, zero, one;
2508 LLVMTypeRef type;
2509
2510 if (bitsize == 16) {
2511 type = ctx->f16;
2512 zero = ctx->f16_0;
2513 one = ctx->f16_1;
2514 } else if (bitsize == 32) {
2515 type = ctx->f32;
2516 zero = ctx->f32_0;
2517 one = ctx->f32_1;
2518 } else {
2519 type = ctx->f64;
2520 zero = ctx->f64_0;
2521 one = ctx->f64_1;
2522 }
2523
2524 cmp = LLVMBuildFCmp(ctx->builder, LLVMRealOGT, src0, zero, "");
2525 val = LLVMBuildSelect(ctx->builder, cmp, one, src0, "");
2526 cmp = LLVMBuildFCmp(ctx->builder, LLVMRealOGE, val, zero, "");
2527 val = LLVMBuildSelect(ctx->builder, cmp, val, LLVMConstReal(type, -1.0), "");
2528 return val;
2529 }
2530
2531 LLVMValueRef ac_build_bit_count(struct ac_llvm_context *ctx, LLVMValueRef src0)
2532 {
2533 LLVMValueRef result;
2534 unsigned bitsize;
2535
2536 bitsize = ac_get_elem_bits(ctx, LLVMTypeOf(src0));
2537
2538 switch (bitsize) {
2539 case 64:
2540 result = ac_build_intrinsic(ctx, "llvm.ctpop.i64", ctx->i64,
2541 (LLVMValueRef []) { src0 }, 1,
2542 AC_FUNC_ATTR_READNONE);
2543
2544 result = LLVMBuildTrunc(ctx->builder, result, ctx->i32, "");
2545 break;
2546 case 32:
2547 result = ac_build_intrinsic(ctx, "llvm.ctpop.i32", ctx->i32,
2548 (LLVMValueRef []) { src0 }, 1,
2549 AC_FUNC_ATTR_READNONE);
2550 break;
2551 case 16:
2552 result = ac_build_intrinsic(ctx, "llvm.ctpop.i16", ctx->i16,
2553 (LLVMValueRef []) { src0 }, 1,
2554 AC_FUNC_ATTR_READNONE);
2555
2556 result = LLVMBuildZExt(ctx->builder, result, ctx->i32, "");
2557 break;
2558 case 8:
2559 result = ac_build_intrinsic(ctx, "llvm.ctpop.i8", ctx->i8,
2560 (LLVMValueRef []) { src0 }, 1,
2561 AC_FUNC_ATTR_READNONE);
2562
2563 result = LLVMBuildZExt(ctx->builder, result, ctx->i32, "");
2564 break;
2565 default:
2566 unreachable(!"invalid bitsize");
2567 break;
2568 }
2569
2570 return result;
2571 }
2572
2573 LLVMValueRef ac_build_bitfield_reverse(struct ac_llvm_context *ctx,
2574 LLVMValueRef src0)
2575 {
2576 LLVMValueRef result;
2577 unsigned bitsize;
2578
2579 bitsize = ac_get_elem_bits(ctx, LLVMTypeOf(src0));
2580
2581 switch (bitsize) {
2582 case 32:
2583 result = ac_build_intrinsic(ctx, "llvm.bitreverse.i32", ctx->i32,
2584 (LLVMValueRef []) { src0 }, 1,
2585 AC_FUNC_ATTR_READNONE);
2586 break;
2587 case 16:
2588 result = ac_build_intrinsic(ctx, "llvm.bitreverse.i16", ctx->i16,
2589 (LLVMValueRef []) { src0 }, 1,
2590 AC_FUNC_ATTR_READNONE);
2591
2592 result = LLVMBuildZExt(ctx->builder, result, ctx->i32, "");
2593 break;
2594 default:
2595 unreachable(!"invalid bitsize");
2596 break;
2597 }
2598
2599 return result;
2600 }
2601
2602 #define AC_EXP_TARGET 0
2603 #define AC_EXP_ENABLED_CHANNELS 1
2604 #define AC_EXP_OUT0 2
2605
2606 enum ac_ir_type {
2607 AC_IR_UNDEF,
2608 AC_IR_CONST,
2609 AC_IR_VALUE,
2610 };
2611
2612 struct ac_vs_exp_chan
2613 {
2614 LLVMValueRef value;
2615 float const_float;
2616 enum ac_ir_type type;
2617 };
2618
2619 struct ac_vs_exp_inst {
2620 unsigned offset;
2621 LLVMValueRef inst;
2622 struct ac_vs_exp_chan chan[4];
2623 };
2624
2625 struct ac_vs_exports {
2626 unsigned num;
2627 struct ac_vs_exp_inst exp[VARYING_SLOT_MAX];
2628 };
2629
2630 /* Return true if the PARAM export has been eliminated. */
2631 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset,
2632 uint32_t num_outputs,
2633 struct ac_vs_exp_inst *exp)
2634 {
2635 unsigned i, default_val; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
2636 bool is_zero[4] = {}, is_one[4] = {};
2637
2638 for (i = 0; i < 4; i++) {
2639 /* It's a constant expression. Undef outputs are eliminated too. */
2640 if (exp->chan[i].type == AC_IR_UNDEF) {
2641 is_zero[i] = true;
2642 is_one[i] = true;
2643 } else if (exp->chan[i].type == AC_IR_CONST) {
2644 if (exp->chan[i].const_float == 0)
2645 is_zero[i] = true;
2646 else if (exp->chan[i].const_float == 1)
2647 is_one[i] = true;
2648 else
2649 return false; /* other constant */
2650 } else
2651 return false;
2652 }
2653
2654 /* Only certain combinations of 0 and 1 can be eliminated. */
2655 if (is_zero[0] && is_zero[1] && is_zero[2])
2656 default_val = is_zero[3] ? 0 : 1;
2657 else if (is_one[0] && is_one[1] && is_one[2])
2658 default_val = is_zero[3] ? 2 : 3;
2659 else
2660 return false;
2661
2662 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
2663 LLVMInstructionEraseFromParent(exp->inst);
2664
2665 /* Change OFFSET to DEFAULT_VAL. */
2666 for (i = 0; i < num_outputs; i++) {
2667 if (vs_output_param_offset[i] == exp->offset) {
2668 vs_output_param_offset[i] =
2669 AC_EXP_PARAM_DEFAULT_VAL_0000 + default_val;
2670 break;
2671 }
2672 }
2673 return true;
2674 }
2675
2676 static bool ac_eliminate_duplicated_output(struct ac_llvm_context *ctx,
2677 uint8_t *vs_output_param_offset,
2678 uint32_t num_outputs,
2679 struct ac_vs_exports *processed,
2680 struct ac_vs_exp_inst *exp)
2681 {
2682 unsigned p, copy_back_channels = 0;
2683
2684 /* See if the output is already in the list of processed outputs.
2685 * The LLVMValueRef comparison relies on SSA.
2686 */
2687 for (p = 0; p < processed->num; p++) {
2688 bool different = false;
2689
2690 for (unsigned j = 0; j < 4; j++) {
2691 struct ac_vs_exp_chan *c1 = &processed->exp[p].chan[j];
2692 struct ac_vs_exp_chan *c2 = &exp->chan[j];
2693
2694 /* Treat undef as a match. */
2695 if (c2->type == AC_IR_UNDEF)
2696 continue;
2697
2698 /* If c1 is undef but c2 isn't, we can copy c2 to c1
2699 * and consider the instruction duplicated.
2700 */
2701 if (c1->type == AC_IR_UNDEF) {
2702 copy_back_channels |= 1 << j;
2703 continue;
2704 }
2705
2706 /* Test whether the channels are not equal. */
2707 if (c1->type != c2->type ||
2708 (c1->type == AC_IR_CONST &&
2709 c1->const_float != c2->const_float) ||
2710 (c1->type == AC_IR_VALUE &&
2711 c1->value != c2->value)) {
2712 different = true;
2713 break;
2714 }
2715 }
2716 if (!different)
2717 break;
2718
2719 copy_back_channels = 0;
2720 }
2721 if (p == processed->num)
2722 return false;
2723
2724 /* If a match was found, but the matching export has undef where the new
2725 * one has a normal value, copy the normal value to the undef channel.
2726 */
2727 struct ac_vs_exp_inst *match = &processed->exp[p];
2728
2729 /* Get current enabled channels mask. */
2730 LLVMValueRef arg = LLVMGetOperand(match->inst, AC_EXP_ENABLED_CHANNELS);
2731 unsigned enabled_channels = LLVMConstIntGetZExtValue(arg);
2732
2733 while (copy_back_channels) {
2734 unsigned chan = u_bit_scan(&copy_back_channels);
2735
2736 assert(match->chan[chan].type == AC_IR_UNDEF);
2737 LLVMSetOperand(match->inst, AC_EXP_OUT0 + chan,
2738 exp->chan[chan].value);
2739 match->chan[chan] = exp->chan[chan];
2740
2741 /* Update number of enabled channels because the original mask
2742 * is not always 0xf.
2743 */
2744 enabled_channels |= (1 << chan);
2745 LLVMSetOperand(match->inst, AC_EXP_ENABLED_CHANNELS,
2746 LLVMConstInt(ctx->i32, enabled_channels, 0));
2747 }
2748
2749 /* The PARAM export is duplicated. Kill it. */
2750 LLVMInstructionEraseFromParent(exp->inst);
2751
2752 /* Change OFFSET to the matching export. */
2753 for (unsigned i = 0; i < num_outputs; i++) {
2754 if (vs_output_param_offset[i] == exp->offset) {
2755 vs_output_param_offset[i] = match->offset;
2756 break;
2757 }
2758 }
2759 return true;
2760 }
2761
2762 void ac_optimize_vs_outputs(struct ac_llvm_context *ctx,
2763 LLVMValueRef main_fn,
2764 uint8_t *vs_output_param_offset,
2765 uint32_t num_outputs,
2766 uint8_t *num_param_exports)
2767 {
2768 LLVMBasicBlockRef bb;
2769 bool removed_any = false;
2770 struct ac_vs_exports exports;
2771
2772 exports.num = 0;
2773
2774 /* Process all LLVM instructions. */
2775 bb = LLVMGetFirstBasicBlock(main_fn);
2776 while (bb) {
2777 LLVMValueRef inst = LLVMGetFirstInstruction(bb);
2778
2779 while (inst) {
2780 LLVMValueRef cur = inst;
2781 inst = LLVMGetNextInstruction(inst);
2782 struct ac_vs_exp_inst exp;
2783
2784 if (LLVMGetInstructionOpcode(cur) != LLVMCall)
2785 continue;
2786
2787 LLVMValueRef callee = ac_llvm_get_called_value(cur);
2788
2789 if (!ac_llvm_is_function(callee))
2790 continue;
2791
2792 const char *name = LLVMGetValueName(callee);
2793 unsigned num_args = LLVMCountParams(callee);
2794
2795 /* Check if this is an export instruction. */
2796 if ((num_args != 9 && num_args != 8) ||
2797 (strcmp(name, "llvm.SI.export") &&
2798 strcmp(name, "llvm.amdgcn.exp.f32")))
2799 continue;
2800
2801 LLVMValueRef arg = LLVMGetOperand(cur, AC_EXP_TARGET);
2802 unsigned target = LLVMConstIntGetZExtValue(arg);
2803
2804 if (target < V_008DFC_SQ_EXP_PARAM)
2805 continue;
2806
2807 target -= V_008DFC_SQ_EXP_PARAM;
2808
2809 /* Parse the instruction. */
2810 memset(&exp, 0, sizeof(exp));
2811 exp.offset = target;
2812 exp.inst = cur;
2813
2814 for (unsigned i = 0; i < 4; i++) {
2815 LLVMValueRef v = LLVMGetOperand(cur, AC_EXP_OUT0 + i);
2816
2817 exp.chan[i].value = v;
2818
2819 if (LLVMIsUndef(v)) {
2820 exp.chan[i].type = AC_IR_UNDEF;
2821 } else if (LLVMIsAConstantFP(v)) {
2822 LLVMBool loses_info;
2823 exp.chan[i].type = AC_IR_CONST;
2824 exp.chan[i].const_float =
2825 LLVMConstRealGetDouble(v, &loses_info);
2826 } else {
2827 exp.chan[i].type = AC_IR_VALUE;
2828 }
2829 }
2830
2831 /* Eliminate constant and duplicated PARAM exports. */
2832 if (ac_eliminate_const_output(vs_output_param_offset,
2833 num_outputs, &exp) ||
2834 ac_eliminate_duplicated_output(ctx,
2835 vs_output_param_offset,
2836 num_outputs, &exports,
2837 &exp)) {
2838 removed_any = true;
2839 } else {
2840 exports.exp[exports.num++] = exp;
2841 }
2842 }
2843 bb = LLVMGetNextBasicBlock(bb);
2844 }
2845
2846 /* Remove holes in export memory due to removed PARAM exports.
2847 * This is done by renumbering all PARAM exports.
2848 */
2849 if (removed_any) {
2850 uint8_t old_offset[VARYING_SLOT_MAX];
2851 unsigned out, i;
2852
2853 /* Make a copy of the offsets. We need the old version while
2854 * we are modifying some of them. */
2855 memcpy(old_offset, vs_output_param_offset,
2856 sizeof(old_offset));
2857
2858 for (i = 0; i < exports.num; i++) {
2859 unsigned offset = exports.exp[i].offset;
2860
2861 /* Update vs_output_param_offset. Multiple outputs can
2862 * have the same offset.
2863 */
2864 for (out = 0; out < num_outputs; out++) {
2865 if (old_offset[out] == offset)
2866 vs_output_param_offset[out] = i;
2867 }
2868
2869 /* Change the PARAM offset in the instruction. */
2870 LLVMSetOperand(exports.exp[i].inst, AC_EXP_TARGET,
2871 LLVMConstInt(ctx->i32,
2872 V_008DFC_SQ_EXP_PARAM + i, 0));
2873 }
2874 *num_param_exports = exports.num;
2875 }
2876 }
2877
2878 void ac_init_exec_full_mask(struct ac_llvm_context *ctx)
2879 {
2880 LLVMValueRef full_mask = LLVMConstInt(ctx->i64, ~0ull, 0);
2881 ac_build_intrinsic(ctx,
2882 "llvm.amdgcn.init.exec", ctx->voidt,
2883 &full_mask, 1, AC_FUNC_ATTR_CONVERGENT);
2884 }
2885
2886 void ac_declare_lds_as_pointer(struct ac_llvm_context *ctx)
2887 {
2888 unsigned lds_size = ctx->chip_class >= CIK ? 65536 : 32768;
2889 ctx->lds = LLVMBuildIntToPtr(ctx->builder, ctx->i32_0,
2890 LLVMPointerType(LLVMArrayType(ctx->i32, lds_size / 4), AC_ADDR_SPACE_LDS),
2891 "lds");
2892 }
2893
2894 LLVMValueRef ac_lds_load(struct ac_llvm_context *ctx,
2895 LLVMValueRef dw_addr)
2896 {
2897 return ac_build_load(ctx, ctx->lds, dw_addr);
2898 }
2899
2900 void ac_lds_store(struct ac_llvm_context *ctx,
2901 LLVMValueRef dw_addr,
2902 LLVMValueRef value)
2903 {
2904 value = ac_to_integer(ctx, value);
2905 ac_build_indexed_store(ctx, ctx->lds,
2906 dw_addr, value);
2907 }
2908
2909 LLVMValueRef ac_find_lsb(struct ac_llvm_context *ctx,
2910 LLVMTypeRef dst_type,
2911 LLVMValueRef src0)
2912 {
2913 unsigned src0_bitsize = ac_get_elem_bits(ctx, LLVMTypeOf(src0));
2914 const char *intrin_name;
2915 LLVMTypeRef type;
2916 LLVMValueRef zero;
2917
2918 switch (src0_bitsize) {
2919 case 64:
2920 intrin_name = "llvm.cttz.i64";
2921 type = ctx->i64;
2922 zero = ctx->i64_0;
2923 break;
2924 case 32:
2925 intrin_name = "llvm.cttz.i32";
2926 type = ctx->i32;
2927 zero = ctx->i32_0;
2928 break;
2929 case 16:
2930 intrin_name = "llvm.cttz.i16";
2931 type = ctx->i16;
2932 zero = ctx->i16_0;
2933 break;
2934 case 8:
2935 intrin_name = "llvm.cttz.i8";
2936 type = ctx->i8;
2937 zero = ctx->i8_0;
2938 break;
2939 default:
2940 unreachable(!"invalid bitsize");
2941 }
2942
2943 LLVMValueRef params[2] = {
2944 src0,
2945
2946 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
2947 * add special code to check for x=0. The reason is that
2948 * the LLVM behavior for x=0 is different from what we
2949 * need here. However, LLVM also assumes that ffs(x) is
2950 * in [0, 31], but GLSL expects that ffs(0) = -1, so
2951 * a conditional assignment to handle 0 is still required.
2952 *
2953 * The hardware already implements the correct behavior.
2954 */
2955 ctx->i1true,
2956 };
2957
2958 LLVMValueRef lsb = ac_build_intrinsic(ctx, intrin_name, type,
2959 params, 2,
2960 AC_FUNC_ATTR_READNONE);
2961
2962 if (src0_bitsize == 64) {
2963 lsb = LLVMBuildTrunc(ctx->builder, lsb, ctx->i32, "");
2964 } else if (src0_bitsize < 32) {
2965 lsb = LLVMBuildSExt(ctx->builder, lsb, ctx->i32, "");
2966 }
2967
2968 /* TODO: We need an intrinsic to skip this conditional. */
2969 /* Check for zero: */
2970 return LLVMBuildSelect(ctx->builder, LLVMBuildICmp(ctx->builder,
2971 LLVMIntEQ, src0,
2972 zero, ""),
2973 LLVMConstInt(ctx->i32, -1, 0), lsb, "");
2974 }
2975
2976 LLVMTypeRef ac_array_in_const_addr_space(LLVMTypeRef elem_type)
2977 {
2978 return LLVMPointerType(LLVMArrayType(elem_type, 0),
2979 AC_ADDR_SPACE_CONST);
2980 }
2981
2982 LLVMTypeRef ac_array_in_const32_addr_space(LLVMTypeRef elem_type)
2983 {
2984 return LLVMPointerType(LLVMArrayType(elem_type, 0),
2985 AC_ADDR_SPACE_CONST_32BIT);
2986 }
2987
2988 static struct ac_llvm_flow *
2989 get_current_flow(struct ac_llvm_context *ctx)
2990 {
2991 if (ctx->flow_depth > 0)
2992 return &ctx->flow[ctx->flow_depth - 1];
2993 return NULL;
2994 }
2995
2996 static struct ac_llvm_flow *
2997 get_innermost_loop(struct ac_llvm_context *ctx)
2998 {
2999 for (unsigned i = ctx->flow_depth; i > 0; --i) {
3000 if (ctx->flow[i - 1].loop_entry_block)
3001 return &ctx->flow[i - 1];
3002 }
3003 return NULL;
3004 }
3005
3006 static struct ac_llvm_flow *
3007 push_flow(struct ac_llvm_context *ctx)
3008 {
3009 struct ac_llvm_flow *flow;
3010
3011 if (ctx->flow_depth >= ctx->flow_depth_max) {
3012 unsigned new_max = MAX2(ctx->flow_depth << 1,
3013 AC_LLVM_INITIAL_CF_DEPTH);
3014
3015 ctx->flow = realloc(ctx->flow, new_max * sizeof(*ctx->flow));
3016 ctx->flow_depth_max = new_max;
3017 }
3018
3019 flow = &ctx->flow[ctx->flow_depth];
3020 ctx->flow_depth++;
3021
3022 flow->next_block = NULL;
3023 flow->loop_entry_block = NULL;
3024 return flow;
3025 }
3026
3027 static void set_basicblock_name(LLVMBasicBlockRef bb, const char *base,
3028 int label_id)
3029 {
3030 char buf[32];
3031 snprintf(buf, sizeof(buf), "%s%d", base, label_id);
3032 LLVMSetValueName(LLVMBasicBlockAsValue(bb), buf);
3033 }
3034
3035 /* Append a basic block at the level of the parent flow.
3036 */
3037 static LLVMBasicBlockRef append_basic_block(struct ac_llvm_context *ctx,
3038 const char *name)
3039 {
3040 assert(ctx->flow_depth >= 1);
3041
3042 if (ctx->flow_depth >= 2) {
3043 struct ac_llvm_flow *flow = &ctx->flow[ctx->flow_depth - 2];
3044
3045 return LLVMInsertBasicBlockInContext(ctx->context,
3046 flow->next_block, name);
3047 }
3048
3049 LLVMValueRef main_fn =
3050 LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx->builder));
3051 return LLVMAppendBasicBlockInContext(ctx->context, main_fn, name);
3052 }
3053
3054 /* Emit a branch to the given default target for the current block if
3055 * applicable -- that is, if the current block does not already contain a
3056 * branch from a break or continue.
3057 */
3058 static void emit_default_branch(LLVMBuilderRef builder,
3059 LLVMBasicBlockRef target)
3060 {
3061 if (!LLVMGetBasicBlockTerminator(LLVMGetInsertBlock(builder)))
3062 LLVMBuildBr(builder, target);
3063 }
3064
3065 void ac_build_bgnloop(struct ac_llvm_context *ctx, int label_id)
3066 {
3067 struct ac_llvm_flow *flow = push_flow(ctx);
3068 flow->loop_entry_block = append_basic_block(ctx, "LOOP");
3069 flow->next_block = append_basic_block(ctx, "ENDLOOP");
3070 set_basicblock_name(flow->loop_entry_block, "loop", label_id);
3071 LLVMBuildBr(ctx->builder, flow->loop_entry_block);
3072 LLVMPositionBuilderAtEnd(ctx->builder, flow->loop_entry_block);
3073 }
3074
3075 void ac_build_break(struct ac_llvm_context *ctx)
3076 {
3077 struct ac_llvm_flow *flow = get_innermost_loop(ctx);
3078 LLVMBuildBr(ctx->builder, flow->next_block);
3079 }
3080
3081 void ac_build_continue(struct ac_llvm_context *ctx)
3082 {
3083 struct ac_llvm_flow *flow = get_innermost_loop(ctx);
3084 LLVMBuildBr(ctx->builder, flow->loop_entry_block);
3085 }
3086
3087 void ac_build_else(struct ac_llvm_context *ctx, int label_id)
3088 {
3089 struct ac_llvm_flow *current_branch = get_current_flow(ctx);
3090 LLVMBasicBlockRef endif_block;
3091
3092 assert(!current_branch->loop_entry_block);
3093
3094 endif_block = append_basic_block(ctx, "ENDIF");
3095 emit_default_branch(ctx->builder, endif_block);
3096
3097 LLVMPositionBuilderAtEnd(ctx->builder, current_branch->next_block);
3098 set_basicblock_name(current_branch->next_block, "else", label_id);
3099
3100 current_branch->next_block = endif_block;
3101 }
3102
3103 void ac_build_endif(struct ac_llvm_context *ctx, int label_id)
3104 {
3105 struct ac_llvm_flow *current_branch = get_current_flow(ctx);
3106
3107 assert(!current_branch->loop_entry_block);
3108
3109 emit_default_branch(ctx->builder, current_branch->next_block);
3110 LLVMPositionBuilderAtEnd(ctx->builder, current_branch->next_block);
3111 set_basicblock_name(current_branch->next_block, "endif", label_id);
3112
3113 ctx->flow_depth--;
3114 }
3115
3116 void ac_build_endloop(struct ac_llvm_context *ctx, int label_id)
3117 {
3118 struct ac_llvm_flow *current_loop = get_current_flow(ctx);
3119
3120 assert(current_loop->loop_entry_block);
3121
3122 emit_default_branch(ctx->builder, current_loop->loop_entry_block);
3123
3124 LLVMPositionBuilderAtEnd(ctx->builder, current_loop->next_block);
3125 set_basicblock_name(current_loop->next_block, "endloop", label_id);
3126 ctx->flow_depth--;
3127 }
3128
3129 void ac_build_ifcc(struct ac_llvm_context *ctx, LLVMValueRef cond, int label_id)
3130 {
3131 struct ac_llvm_flow *flow = push_flow(ctx);
3132 LLVMBasicBlockRef if_block;
3133
3134 if_block = append_basic_block(ctx, "IF");
3135 flow->next_block = append_basic_block(ctx, "ELSE");
3136 set_basicblock_name(if_block, "if", label_id);
3137 LLVMBuildCondBr(ctx->builder, cond, if_block, flow->next_block);
3138 LLVMPositionBuilderAtEnd(ctx->builder, if_block);
3139 }
3140
3141 void ac_build_if(struct ac_llvm_context *ctx, LLVMValueRef value,
3142 int label_id)
3143 {
3144 LLVMValueRef cond = LLVMBuildFCmp(ctx->builder, LLVMRealUNE,
3145 value, ctx->f32_0, "");
3146 ac_build_ifcc(ctx, cond, label_id);
3147 }
3148
3149 void ac_build_uif(struct ac_llvm_context *ctx, LLVMValueRef value,
3150 int label_id)
3151 {
3152 LLVMValueRef cond = LLVMBuildICmp(ctx->builder, LLVMIntNE,
3153 ac_to_integer(ctx, value),
3154 ctx->i32_0, "");
3155 ac_build_ifcc(ctx, cond, label_id);
3156 }
3157
3158 LLVMValueRef ac_build_alloca_undef(struct ac_llvm_context *ac, LLVMTypeRef type,
3159 const char *name)
3160 {
3161 LLVMBuilderRef builder = ac->builder;
3162 LLVMBasicBlockRef current_block = LLVMGetInsertBlock(builder);
3163 LLVMValueRef function = LLVMGetBasicBlockParent(current_block);
3164 LLVMBasicBlockRef first_block = LLVMGetEntryBasicBlock(function);
3165 LLVMValueRef first_instr = LLVMGetFirstInstruction(first_block);
3166 LLVMBuilderRef first_builder = LLVMCreateBuilderInContext(ac->context);
3167 LLVMValueRef res;
3168
3169 if (first_instr) {
3170 LLVMPositionBuilderBefore(first_builder, first_instr);
3171 } else {
3172 LLVMPositionBuilderAtEnd(first_builder, first_block);
3173 }
3174
3175 res = LLVMBuildAlloca(first_builder, type, name);
3176 LLVMDisposeBuilder(first_builder);
3177 return res;
3178 }
3179
3180 LLVMValueRef ac_build_alloca(struct ac_llvm_context *ac,
3181 LLVMTypeRef type, const char *name)
3182 {
3183 LLVMValueRef ptr = ac_build_alloca_undef(ac, type, name);
3184 LLVMBuildStore(ac->builder, LLVMConstNull(type), ptr);
3185 return ptr;
3186 }
3187
3188 LLVMValueRef ac_cast_ptr(struct ac_llvm_context *ctx, LLVMValueRef ptr,
3189 LLVMTypeRef type)
3190 {
3191 int addr_space = LLVMGetPointerAddressSpace(LLVMTypeOf(ptr));
3192 return LLVMBuildBitCast(ctx->builder, ptr,
3193 LLVMPointerType(type, addr_space), "");
3194 }
3195
3196 LLVMValueRef ac_trim_vector(struct ac_llvm_context *ctx, LLVMValueRef value,
3197 unsigned count)
3198 {
3199 unsigned num_components = ac_get_llvm_num_components(value);
3200 if (count == num_components)
3201 return value;
3202
3203 LLVMValueRef masks[MAX2(count, 2)];
3204 masks[0] = ctx->i32_0;
3205 masks[1] = ctx->i32_1;
3206 for (unsigned i = 2; i < count; i++)
3207 masks[i] = LLVMConstInt(ctx->i32, i, false);
3208
3209 if (count == 1)
3210 return LLVMBuildExtractElement(ctx->builder, value, masks[0],
3211 "");
3212
3213 LLVMValueRef swizzle = LLVMConstVector(masks, count);
3214 return LLVMBuildShuffleVector(ctx->builder, value, value, swizzle, "");
3215 }
3216
3217 LLVMValueRef ac_unpack_param(struct ac_llvm_context *ctx, LLVMValueRef param,
3218 unsigned rshift, unsigned bitwidth)
3219 {
3220 LLVMValueRef value = param;
3221 if (rshift)
3222 value = LLVMBuildLShr(ctx->builder, value,
3223 LLVMConstInt(ctx->i32, rshift, false), "");
3224
3225 if (rshift + bitwidth < 32) {
3226 unsigned mask = (1 << bitwidth) - 1;
3227 value = LLVMBuildAnd(ctx->builder, value,
3228 LLVMConstInt(ctx->i32, mask, false), "");
3229 }
3230 return value;
3231 }
3232
3233 /* Adjust the sample index according to FMASK.
3234 *
3235 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3236 * which is the identity mapping. Each nibble says which physical sample
3237 * should be fetched to get that sample.
3238 *
3239 * For example, 0x11111100 means there are only 2 samples stored and
3240 * the second sample covers 3/4 of the pixel. When reading samples 0
3241 * and 1, return physical sample 0 (determined by the first two 0s
3242 * in FMASK), otherwise return physical sample 1.
3243 *
3244 * The sample index should be adjusted as follows:
3245 * addr[sample_index] = (fmask >> (addr[sample_index] * 4)) & 0xF;
3246 */
3247 void ac_apply_fmask_to_sample(struct ac_llvm_context *ac, LLVMValueRef fmask,
3248 LLVMValueRef *addr, bool is_array_tex)
3249 {
3250 struct ac_image_args fmask_load = {};
3251 fmask_load.opcode = ac_image_load;
3252 fmask_load.resource = fmask;
3253 fmask_load.dmask = 0xf;
3254 fmask_load.dim = is_array_tex ? ac_image_2darray : ac_image_2d;
3255
3256 fmask_load.coords[0] = addr[0];
3257 fmask_load.coords[1] = addr[1];
3258 if (is_array_tex)
3259 fmask_load.coords[2] = addr[2];
3260
3261 LLVMValueRef fmask_value = ac_build_image_opcode(ac, &fmask_load);
3262 fmask_value = LLVMBuildExtractElement(ac->builder, fmask_value,
3263 ac->i32_0, "");
3264
3265 /* Apply the formula. */
3266 unsigned sample_chan = is_array_tex ? 3 : 2;
3267 LLVMValueRef final_sample;
3268 final_sample = LLVMBuildMul(ac->builder, addr[sample_chan],
3269 LLVMConstInt(ac->i32, 4, 0), "");
3270 final_sample = LLVMBuildLShr(ac->builder, fmask_value, final_sample, "");
3271 /* Mask the sample index by 0x7, because 0x8 means an unknown value
3272 * with EQAA, so those will map to 0. */
3273 final_sample = LLVMBuildAnd(ac->builder, final_sample,
3274 LLVMConstInt(ac->i32, 0x7, 0), "");
3275
3276 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3277 * resource descriptor is 0 (invalid).
3278 */
3279 LLVMValueRef tmp;
3280 tmp = LLVMBuildBitCast(ac->builder, fmask, ac->v8i32, "");
3281 tmp = LLVMBuildExtractElement(ac->builder, tmp, ac->i32_1, "");
3282 tmp = LLVMBuildICmp(ac->builder, LLVMIntNE, tmp, ac->i32_0, "");
3283
3284 /* Replace the MSAA sample index. */
3285 addr[sample_chan] = LLVMBuildSelect(ac->builder, tmp, final_sample,
3286 addr[sample_chan], "");
3287 }
3288
3289 static LLVMValueRef
3290 _ac_build_readlane(struct ac_llvm_context *ctx, LLVMValueRef src, LLVMValueRef lane)
3291 {
3292 ac_build_optimization_barrier(ctx, &src);
3293 return ac_build_intrinsic(ctx,
3294 lane == NULL ? "llvm.amdgcn.readfirstlane" : "llvm.amdgcn.readlane",
3295 LLVMTypeOf(src), (LLVMValueRef []) {
3296 src, lane },
3297 lane == NULL ? 1 : 2,
3298 AC_FUNC_ATTR_READNONE |
3299 AC_FUNC_ATTR_CONVERGENT);
3300 }
3301
3302 /**
3303 * Builds the "llvm.amdgcn.readlane" or "llvm.amdgcn.readfirstlane" intrinsic.
3304 * @param ctx
3305 * @param src
3306 * @param lane - id of the lane or NULL for the first active lane
3307 * @return value of the lane
3308 */
3309 LLVMValueRef
3310 ac_build_readlane(struct ac_llvm_context *ctx, LLVMValueRef src, LLVMValueRef lane)
3311 {
3312 LLVMTypeRef src_type = LLVMTypeOf(src);
3313 src = ac_to_integer(ctx, src);
3314 unsigned bits = LLVMGetIntTypeWidth(LLVMTypeOf(src));
3315 LLVMValueRef ret;
3316
3317 if (bits == 32) {
3318 ret = _ac_build_readlane(ctx, src, lane);
3319 } else {
3320 assert(bits % 32 == 0);
3321 LLVMTypeRef vec_type = LLVMVectorType(ctx->i32, bits / 32);
3322 LLVMValueRef src_vector =
3323 LLVMBuildBitCast(ctx->builder, src, vec_type, "");
3324 ret = LLVMGetUndef(vec_type);
3325 for (unsigned i = 0; i < bits / 32; i++) {
3326 src = LLVMBuildExtractElement(ctx->builder, src_vector,
3327 LLVMConstInt(ctx->i32, i, 0), "");
3328 LLVMValueRef ret_comp = _ac_build_readlane(ctx, src, lane);
3329 ret = LLVMBuildInsertElement(ctx->builder, ret, ret_comp,
3330 LLVMConstInt(ctx->i32, i, 0), "");
3331 }
3332 }
3333 return LLVMBuildBitCast(ctx->builder, ret, src_type, "");
3334 }
3335
3336 LLVMValueRef
3337 ac_build_writelane(struct ac_llvm_context *ctx, LLVMValueRef src, LLVMValueRef value, LLVMValueRef lane)
3338 {
3339 /* TODO: Use the actual instruction when LLVM adds an intrinsic for it.
3340 */
3341 LLVMValueRef pred = LLVMBuildICmp(ctx->builder, LLVMIntEQ, lane,
3342 ac_get_thread_id(ctx), "");
3343 return LLVMBuildSelect(ctx->builder, pred, value, src, "");
3344 }
3345
3346 LLVMValueRef
3347 ac_build_mbcnt(struct ac_llvm_context *ctx, LLVMValueRef mask)
3348 {
3349 LLVMValueRef mask_vec = LLVMBuildBitCast(ctx->builder, mask,
3350 LLVMVectorType(ctx->i32, 2),
3351 "");
3352 LLVMValueRef mask_lo = LLVMBuildExtractElement(ctx->builder, mask_vec,
3353 ctx->i32_0, "");
3354 LLVMValueRef mask_hi = LLVMBuildExtractElement(ctx->builder, mask_vec,
3355 ctx->i32_1, "");
3356 LLVMValueRef val =
3357 ac_build_intrinsic(ctx, "llvm.amdgcn.mbcnt.lo", ctx->i32,
3358 (LLVMValueRef []) { mask_lo, ctx->i32_0 },
3359 2, AC_FUNC_ATTR_READNONE);
3360 val = ac_build_intrinsic(ctx, "llvm.amdgcn.mbcnt.hi", ctx->i32,
3361 (LLVMValueRef []) { mask_hi, val },
3362 2, AC_FUNC_ATTR_READNONE);
3363 return val;
3364 }
3365
3366 enum dpp_ctrl {
3367 _dpp_quad_perm = 0x000,
3368 _dpp_row_sl = 0x100,
3369 _dpp_row_sr = 0x110,
3370 _dpp_row_rr = 0x120,
3371 dpp_wf_sl1 = 0x130,
3372 dpp_wf_rl1 = 0x134,
3373 dpp_wf_sr1 = 0x138,
3374 dpp_wf_rr1 = 0x13C,
3375 dpp_row_mirror = 0x140,
3376 dpp_row_half_mirror = 0x141,
3377 dpp_row_bcast15 = 0x142,
3378 dpp_row_bcast31 = 0x143
3379 };
3380
3381 static inline enum dpp_ctrl
3382 dpp_quad_perm(unsigned lane0, unsigned lane1, unsigned lane2, unsigned lane3)
3383 {
3384 assert(lane0 < 4 && lane1 < 4 && lane2 < 4 && lane3 < 4);
3385 return _dpp_quad_perm | lane0 | (lane1 << 2) | (lane2 << 4) | (lane3 << 6);
3386 }
3387
3388 static inline enum dpp_ctrl
3389 dpp_row_sl(unsigned amount)
3390 {
3391 assert(amount > 0 && amount < 16);
3392 return _dpp_row_sl | amount;
3393 }
3394
3395 static inline enum dpp_ctrl
3396 dpp_row_sr(unsigned amount)
3397 {
3398 assert(amount > 0 && amount < 16);
3399 return _dpp_row_sr | amount;
3400 }
3401
3402 static LLVMValueRef
3403 _ac_build_dpp(struct ac_llvm_context *ctx, LLVMValueRef old, LLVMValueRef src,
3404 enum dpp_ctrl dpp_ctrl, unsigned row_mask, unsigned bank_mask,
3405 bool bound_ctrl)
3406 {
3407 return ac_build_intrinsic(ctx, "llvm.amdgcn.update.dpp.i32",
3408 LLVMTypeOf(old),
3409 (LLVMValueRef[]) {
3410 old, src,
3411 LLVMConstInt(ctx->i32, dpp_ctrl, 0),
3412 LLVMConstInt(ctx->i32, row_mask, 0),
3413 LLVMConstInt(ctx->i32, bank_mask, 0),
3414 LLVMConstInt(ctx->i1, bound_ctrl, 0) },
3415 6, AC_FUNC_ATTR_READNONE | AC_FUNC_ATTR_CONVERGENT);
3416 }
3417
3418 static LLVMValueRef
3419 ac_build_dpp(struct ac_llvm_context *ctx, LLVMValueRef old, LLVMValueRef src,
3420 enum dpp_ctrl dpp_ctrl, unsigned row_mask, unsigned bank_mask,
3421 bool bound_ctrl)
3422 {
3423 LLVMTypeRef src_type = LLVMTypeOf(src);
3424 src = ac_to_integer(ctx, src);
3425 old = ac_to_integer(ctx, old);
3426 unsigned bits = LLVMGetIntTypeWidth(LLVMTypeOf(src));
3427 LLVMValueRef ret;
3428 if (bits == 32) {
3429 ret = _ac_build_dpp(ctx, old, src, dpp_ctrl, row_mask,
3430 bank_mask, bound_ctrl);
3431 } else {
3432 assert(bits % 32 == 0);
3433 LLVMTypeRef vec_type = LLVMVectorType(ctx->i32, bits / 32);
3434 LLVMValueRef src_vector =
3435 LLVMBuildBitCast(ctx->builder, src, vec_type, "");
3436 LLVMValueRef old_vector =
3437 LLVMBuildBitCast(ctx->builder, old, vec_type, "");
3438 ret = LLVMGetUndef(vec_type);
3439 for (unsigned i = 0; i < bits / 32; i++) {
3440 src = LLVMBuildExtractElement(ctx->builder, src_vector,
3441 LLVMConstInt(ctx->i32, i,
3442 0), "");
3443 old = LLVMBuildExtractElement(ctx->builder, old_vector,
3444 LLVMConstInt(ctx->i32, i,
3445 0), "");
3446 LLVMValueRef ret_comp = _ac_build_dpp(ctx, old, src,
3447 dpp_ctrl,
3448 row_mask,
3449 bank_mask,
3450 bound_ctrl);
3451 ret = LLVMBuildInsertElement(ctx->builder, ret,
3452 ret_comp,
3453 LLVMConstInt(ctx->i32, i,
3454 0), "");
3455 }
3456 }
3457 return LLVMBuildBitCast(ctx->builder, ret, src_type, "");
3458 }
3459
3460 static inline unsigned
3461 ds_pattern_bitmode(unsigned and_mask, unsigned or_mask, unsigned xor_mask)
3462 {
3463 assert(and_mask < 32 && or_mask < 32 && xor_mask < 32);
3464 return and_mask | (or_mask << 5) | (xor_mask << 10);
3465 }
3466
3467 static LLVMValueRef
3468 _ac_build_ds_swizzle(struct ac_llvm_context *ctx, LLVMValueRef src, unsigned mask)
3469 {
3470 return ac_build_intrinsic(ctx, "llvm.amdgcn.ds.swizzle",
3471 LLVMTypeOf(src), (LLVMValueRef []) {
3472 src, LLVMConstInt(ctx->i32, mask, 0) },
3473 2, AC_FUNC_ATTR_READNONE | AC_FUNC_ATTR_CONVERGENT);
3474 }
3475
3476 LLVMValueRef
3477 ac_build_ds_swizzle(struct ac_llvm_context *ctx, LLVMValueRef src, unsigned mask)
3478 {
3479 LLVMTypeRef src_type = LLVMTypeOf(src);
3480 src = ac_to_integer(ctx, src);
3481 unsigned bits = LLVMGetIntTypeWidth(LLVMTypeOf(src));
3482 LLVMValueRef ret;
3483 if (bits == 32) {
3484 ret = _ac_build_ds_swizzle(ctx, src, mask);
3485 } else {
3486 assert(bits % 32 == 0);
3487 LLVMTypeRef vec_type = LLVMVectorType(ctx->i32, bits / 32);
3488 LLVMValueRef src_vector =
3489 LLVMBuildBitCast(ctx->builder, src, vec_type, "");
3490 ret = LLVMGetUndef(vec_type);
3491 for (unsigned i = 0; i < bits / 32; i++) {
3492 src = LLVMBuildExtractElement(ctx->builder, src_vector,
3493 LLVMConstInt(ctx->i32, i,
3494 0), "");
3495 LLVMValueRef ret_comp = _ac_build_ds_swizzle(ctx, src,
3496 mask);
3497 ret = LLVMBuildInsertElement(ctx->builder, ret,
3498 ret_comp,
3499 LLVMConstInt(ctx->i32, i,
3500 0), "");
3501 }
3502 }
3503 return LLVMBuildBitCast(ctx->builder, ret, src_type, "");
3504 }
3505
3506 static LLVMValueRef
3507 ac_build_wwm(struct ac_llvm_context *ctx, LLVMValueRef src)
3508 {
3509 char name[32], type[8];
3510 ac_build_type_name_for_intr(LLVMTypeOf(src), type, sizeof(type));
3511 snprintf(name, sizeof(name), "llvm.amdgcn.wwm.%s", type);
3512 return ac_build_intrinsic(ctx, name, LLVMTypeOf(src),
3513 (LLVMValueRef []) { src }, 1,
3514 AC_FUNC_ATTR_READNONE);
3515 }
3516
3517 static LLVMValueRef
3518 ac_build_set_inactive(struct ac_llvm_context *ctx, LLVMValueRef src,
3519 LLVMValueRef inactive)
3520 {
3521 char name[33], type[8];
3522 LLVMTypeRef src_type = LLVMTypeOf(src);
3523 src = ac_to_integer(ctx, src);
3524 inactive = ac_to_integer(ctx, inactive);
3525 ac_build_type_name_for_intr(LLVMTypeOf(src), type, sizeof(type));
3526 snprintf(name, sizeof(name), "llvm.amdgcn.set.inactive.%s", type);
3527 LLVMValueRef ret =
3528 ac_build_intrinsic(ctx, name,
3529 LLVMTypeOf(src), (LLVMValueRef []) {
3530 src, inactive }, 2,
3531 AC_FUNC_ATTR_READNONE |
3532 AC_FUNC_ATTR_CONVERGENT);
3533 return LLVMBuildBitCast(ctx->builder, ret, src_type, "");
3534 }
3535
3536 static LLVMValueRef
3537 get_reduction_identity(struct ac_llvm_context *ctx, nir_op op, unsigned type_size)
3538 {
3539 if (type_size == 4) {
3540 switch (op) {
3541 case nir_op_iadd: return ctx->i32_0;
3542 case nir_op_fadd: return ctx->f32_0;
3543 case nir_op_imul: return ctx->i32_1;
3544 case nir_op_fmul: return ctx->f32_1;
3545 case nir_op_imin: return LLVMConstInt(ctx->i32, INT32_MAX, 0);
3546 case nir_op_umin: return LLVMConstInt(ctx->i32, UINT32_MAX, 0);
3547 case nir_op_fmin: return LLVMConstReal(ctx->f32, INFINITY);
3548 case nir_op_imax: return LLVMConstInt(ctx->i32, INT32_MIN, 0);
3549 case nir_op_umax: return ctx->i32_0;
3550 case nir_op_fmax: return LLVMConstReal(ctx->f32, -INFINITY);
3551 case nir_op_iand: return LLVMConstInt(ctx->i32, -1, 0);
3552 case nir_op_ior: return ctx->i32_0;
3553 case nir_op_ixor: return ctx->i32_0;
3554 default:
3555 unreachable("bad reduction intrinsic");
3556 }
3557 } else { /* type_size == 64bit */
3558 switch (op) {
3559 case nir_op_iadd: return ctx->i64_0;
3560 case nir_op_fadd: return ctx->f64_0;
3561 case nir_op_imul: return ctx->i64_1;
3562 case nir_op_fmul: return ctx->f64_1;
3563 case nir_op_imin: return LLVMConstInt(ctx->i64, INT64_MAX, 0);
3564 case nir_op_umin: return LLVMConstInt(ctx->i64, UINT64_MAX, 0);
3565 case nir_op_fmin: return LLVMConstReal(ctx->f64, INFINITY);
3566 case nir_op_imax: return LLVMConstInt(ctx->i64, INT64_MIN, 0);
3567 case nir_op_umax: return ctx->i64_0;
3568 case nir_op_fmax: return LLVMConstReal(ctx->f64, -INFINITY);
3569 case nir_op_iand: return LLVMConstInt(ctx->i64, -1, 0);
3570 case nir_op_ior: return ctx->i64_0;
3571 case nir_op_ixor: return ctx->i64_0;
3572 default:
3573 unreachable("bad reduction intrinsic");
3574 }
3575 }
3576 }
3577
3578 static LLVMValueRef
3579 ac_build_alu_op(struct ac_llvm_context *ctx, LLVMValueRef lhs, LLVMValueRef rhs, nir_op op)
3580 {
3581 bool _64bit = ac_get_type_size(LLVMTypeOf(lhs)) == 8;
3582 switch (op) {
3583 case nir_op_iadd: return LLVMBuildAdd(ctx->builder, lhs, rhs, "");
3584 case nir_op_fadd: return LLVMBuildFAdd(ctx->builder, lhs, rhs, "");
3585 case nir_op_imul: return LLVMBuildMul(ctx->builder, lhs, rhs, "");
3586 case nir_op_fmul: return LLVMBuildFMul(ctx->builder, lhs, rhs, "");
3587 case nir_op_imin: return LLVMBuildSelect(ctx->builder,
3588 LLVMBuildICmp(ctx->builder, LLVMIntSLT, lhs, rhs, ""),
3589 lhs, rhs, "");
3590 case nir_op_umin: return LLVMBuildSelect(ctx->builder,
3591 LLVMBuildICmp(ctx->builder, LLVMIntULT, lhs, rhs, ""),
3592 lhs, rhs, "");
3593 case nir_op_fmin: return ac_build_intrinsic(ctx,
3594 _64bit ? "llvm.minnum.f64" : "llvm.minnum.f32",
3595 _64bit ? ctx->f64 : ctx->f32,
3596 (LLVMValueRef[]){lhs, rhs}, 2, AC_FUNC_ATTR_READNONE);
3597 case nir_op_imax: return LLVMBuildSelect(ctx->builder,
3598 LLVMBuildICmp(ctx->builder, LLVMIntSGT, lhs, rhs, ""),
3599 lhs, rhs, "");
3600 case nir_op_umax: return LLVMBuildSelect(ctx->builder,
3601 LLVMBuildICmp(ctx->builder, LLVMIntUGT, lhs, rhs, ""),
3602 lhs, rhs, "");
3603 case nir_op_fmax: return ac_build_intrinsic(ctx,
3604 _64bit ? "llvm.maxnum.f64" : "llvm.maxnum.f32",
3605 _64bit ? ctx->f64 : ctx->f32,
3606 (LLVMValueRef[]){lhs, rhs}, 2, AC_FUNC_ATTR_READNONE);
3607 case nir_op_iand: return LLVMBuildAnd(ctx->builder, lhs, rhs, "");
3608 case nir_op_ior: return LLVMBuildOr(ctx->builder, lhs, rhs, "");
3609 case nir_op_ixor: return LLVMBuildXor(ctx->builder, lhs, rhs, "");
3610 default:
3611 unreachable("bad reduction intrinsic");
3612 }
3613 }
3614
3615 /**
3616 * \param maxprefix specifies that the result only needs to be correct for a
3617 * prefix of this many threads
3618 *
3619 * TODO: add inclusive and excluse scan functions for SI chip class.
3620 */
3621 static LLVMValueRef
3622 ac_build_scan(struct ac_llvm_context *ctx, nir_op op, LLVMValueRef src, LLVMValueRef identity,
3623 unsigned maxprefix)
3624 {
3625 LLVMValueRef result, tmp;
3626 result = src;
3627 if (maxprefix <= 1)
3628 return result;
3629 tmp = ac_build_dpp(ctx, identity, src, dpp_row_sr(1), 0xf, 0xf, false);
3630 result = ac_build_alu_op(ctx, result, tmp, op);
3631 if (maxprefix <= 2)
3632 return result;
3633 tmp = ac_build_dpp(ctx, identity, src, dpp_row_sr(2), 0xf, 0xf, false);
3634 result = ac_build_alu_op(ctx, result, tmp, op);
3635 if (maxprefix <= 3)
3636 return result;
3637 tmp = ac_build_dpp(ctx, identity, src, dpp_row_sr(3), 0xf, 0xf, false);
3638 result = ac_build_alu_op(ctx, result, tmp, op);
3639 if (maxprefix <= 4)
3640 return result;
3641 tmp = ac_build_dpp(ctx, identity, result, dpp_row_sr(4), 0xf, 0xe, false);
3642 result = ac_build_alu_op(ctx, result, tmp, op);
3643 if (maxprefix <= 8)
3644 return result;
3645 tmp = ac_build_dpp(ctx, identity, result, dpp_row_sr(8), 0xf, 0xc, false);
3646 result = ac_build_alu_op(ctx, result, tmp, op);
3647 if (maxprefix <= 16)
3648 return result;
3649 tmp = ac_build_dpp(ctx, identity, result, dpp_row_bcast15, 0xa, 0xf, false);
3650 result = ac_build_alu_op(ctx, result, tmp, op);
3651 if (maxprefix <= 32)
3652 return result;
3653 tmp = ac_build_dpp(ctx, identity, result, dpp_row_bcast31, 0xc, 0xf, false);
3654 result = ac_build_alu_op(ctx, result, tmp, op);
3655 return result;
3656 }
3657
3658 LLVMValueRef
3659 ac_build_inclusive_scan(struct ac_llvm_context *ctx, LLVMValueRef src, nir_op op)
3660 {
3661 LLVMValueRef result;
3662
3663 if (LLVMTypeOf(src) == ctx->i1 && op == nir_op_iadd) {
3664 LLVMBuilderRef builder = ctx->builder;
3665 src = LLVMBuildZExt(builder, src, ctx->i32, "");
3666 result = ac_build_ballot(ctx, src);
3667 result = ac_build_mbcnt(ctx, result);
3668 result = LLVMBuildAdd(builder, result, src, "");
3669 return result;
3670 }
3671
3672 ac_build_optimization_barrier(ctx, &src);
3673
3674 LLVMValueRef identity =
3675 get_reduction_identity(ctx, op, ac_get_type_size(LLVMTypeOf(src)));
3676 result = LLVMBuildBitCast(ctx->builder, ac_build_set_inactive(ctx, src, identity),
3677 LLVMTypeOf(identity), "");
3678 result = ac_build_scan(ctx, op, result, identity, 64);
3679
3680 return ac_build_wwm(ctx, result);
3681 }
3682
3683 LLVMValueRef
3684 ac_build_exclusive_scan(struct ac_llvm_context *ctx, LLVMValueRef src, nir_op op)
3685 {
3686 LLVMValueRef result;
3687
3688 if (LLVMTypeOf(src) == ctx->i1 && op == nir_op_iadd) {
3689 LLVMBuilderRef builder = ctx->builder;
3690 src = LLVMBuildZExt(builder, src, ctx->i32, "");
3691 result = ac_build_ballot(ctx, src);
3692 result = ac_build_mbcnt(ctx, result);
3693 return result;
3694 }
3695
3696 ac_build_optimization_barrier(ctx, &src);
3697
3698 LLVMValueRef identity =
3699 get_reduction_identity(ctx, op, ac_get_type_size(LLVMTypeOf(src)));
3700 result = LLVMBuildBitCast(ctx->builder, ac_build_set_inactive(ctx, src, identity),
3701 LLVMTypeOf(identity), "");
3702 result = ac_build_dpp(ctx, identity, result, dpp_wf_sr1, 0xf, 0xf, false);
3703 result = ac_build_scan(ctx, op, result, identity, 64);
3704
3705 return ac_build_wwm(ctx, result);
3706 }
3707
3708 LLVMValueRef
3709 ac_build_reduce(struct ac_llvm_context *ctx, LLVMValueRef src, nir_op op, unsigned cluster_size)
3710 {
3711 if (cluster_size == 1) return src;
3712 ac_build_optimization_barrier(ctx, &src);
3713 LLVMValueRef result, swap;
3714 LLVMValueRef identity = get_reduction_identity(ctx, op,
3715 ac_get_type_size(LLVMTypeOf(src)));
3716 result = LLVMBuildBitCast(ctx->builder,
3717 ac_build_set_inactive(ctx, src, identity),
3718 LLVMTypeOf(identity), "");
3719 swap = ac_build_quad_swizzle(ctx, result, 1, 0, 3, 2);
3720 result = ac_build_alu_op(ctx, result, swap, op);
3721 if (cluster_size == 2) return ac_build_wwm(ctx, result);
3722
3723 swap = ac_build_quad_swizzle(ctx, result, 2, 3, 0, 1);
3724 result = ac_build_alu_op(ctx, result, swap, op);
3725 if (cluster_size == 4) return ac_build_wwm(ctx, result);
3726
3727 if (ctx->chip_class >= VI)
3728 swap = ac_build_dpp(ctx, identity, result, dpp_row_half_mirror, 0xf, 0xf, false);
3729 else
3730 swap = ac_build_ds_swizzle(ctx, result, ds_pattern_bitmode(0x1f, 0, 0x04));
3731 result = ac_build_alu_op(ctx, result, swap, op);
3732 if (cluster_size == 8) return ac_build_wwm(ctx, result);
3733
3734 if (ctx->chip_class >= VI)
3735 swap = ac_build_dpp(ctx, identity, result, dpp_row_mirror, 0xf, 0xf, false);
3736 else
3737 swap = ac_build_ds_swizzle(ctx, result, ds_pattern_bitmode(0x1f, 0, 0x08));
3738 result = ac_build_alu_op(ctx, result, swap, op);
3739 if (cluster_size == 16) return ac_build_wwm(ctx, result);
3740
3741 if (ctx->chip_class >= VI && cluster_size != 32)
3742 swap = ac_build_dpp(ctx, identity, result, dpp_row_bcast15, 0xa, 0xf, false);
3743 else
3744 swap = ac_build_ds_swizzle(ctx, result, ds_pattern_bitmode(0x1f, 0, 0x10));
3745 result = ac_build_alu_op(ctx, result, swap, op);
3746 if (cluster_size == 32) return ac_build_wwm(ctx, result);
3747
3748 if (ctx->chip_class >= VI) {
3749 swap = ac_build_dpp(ctx, identity, result, dpp_row_bcast31, 0xc, 0xf, false);
3750 result = ac_build_alu_op(ctx, result, swap, op);
3751 result = ac_build_readlane(ctx, result, LLVMConstInt(ctx->i32, 63, 0));
3752 return ac_build_wwm(ctx, result);
3753 } else {
3754 swap = ac_build_readlane(ctx, result, ctx->i32_0);
3755 result = ac_build_readlane(ctx, result, LLVMConstInt(ctx->i32, 32, 0));
3756 result = ac_build_alu_op(ctx, result, swap, op);
3757 return ac_build_wwm(ctx, result);
3758 }
3759 }
3760
3761 /**
3762 * "Top half" of a scan that reduces per-wave values across an entire
3763 * workgroup.
3764 *
3765 * The source value must be present in the highest lane of the wave, and the
3766 * highest lane must be live.
3767 */
3768 void
3769 ac_build_wg_wavescan_top(struct ac_llvm_context *ctx, struct ac_wg_scan *ws)
3770 {
3771 if (ws->maxwaves <= 1)
3772 return;
3773
3774 const LLVMValueRef i32_63 = LLVMConstInt(ctx->i32, 63, false);
3775 LLVMBuilderRef builder = ctx->builder;
3776 LLVMValueRef tid = ac_get_thread_id(ctx);
3777 LLVMValueRef tmp;
3778
3779 tmp = LLVMBuildICmp(builder, LLVMIntEQ, tid, i32_63, "");
3780 ac_build_ifcc(ctx, tmp, 1000);
3781 LLVMBuildStore(builder, ws->src, LLVMBuildGEP(builder, ws->scratch, &ws->waveidx, 1, ""));
3782 ac_build_endif(ctx, 1000);
3783 }
3784
3785 /**
3786 * "Bottom half" of a scan that reduces per-wave values across an entire
3787 * workgroup.
3788 *
3789 * The caller must place a barrier between the top and bottom halves.
3790 */
3791 void
3792 ac_build_wg_wavescan_bottom(struct ac_llvm_context *ctx, struct ac_wg_scan *ws)
3793 {
3794 const LLVMTypeRef type = LLVMTypeOf(ws->src);
3795 const LLVMValueRef identity =
3796 get_reduction_identity(ctx, ws->op, ac_get_type_size(type));
3797
3798 if (ws->maxwaves <= 1) {
3799 ws->result_reduce = ws->src;
3800 ws->result_inclusive = ws->src;
3801 ws->result_exclusive = identity;
3802 return;
3803 }
3804 assert(ws->maxwaves <= 32);
3805
3806 LLVMBuilderRef builder = ctx->builder;
3807 LLVMValueRef tid = ac_get_thread_id(ctx);
3808 LLVMBasicBlockRef bbs[2];
3809 LLVMValueRef phivalues_scan[2];
3810 LLVMValueRef tmp, tmp2;
3811
3812 bbs[0] = LLVMGetInsertBlock(builder);
3813 phivalues_scan[0] = LLVMGetUndef(type);
3814
3815 if (ws->enable_reduce)
3816 tmp = LLVMBuildICmp(builder, LLVMIntULT, tid, ws->numwaves, "");
3817 else if (ws->enable_inclusive)
3818 tmp = LLVMBuildICmp(builder, LLVMIntULE, tid, ws->waveidx, "");
3819 else
3820 tmp = LLVMBuildICmp(builder, LLVMIntULT, tid, ws->waveidx, "");
3821 ac_build_ifcc(ctx, tmp, 1001);
3822 {
3823 tmp = LLVMBuildLoad(builder, LLVMBuildGEP(builder, ws->scratch, &tid, 1, ""), "");
3824
3825 ac_build_optimization_barrier(ctx, &tmp);
3826
3827 bbs[1] = LLVMGetInsertBlock(builder);
3828 phivalues_scan[1] = ac_build_scan(ctx, ws->op, tmp, identity, ws->maxwaves);
3829 }
3830 ac_build_endif(ctx, 1001);
3831
3832 const LLVMValueRef scan = ac_build_phi(ctx, type, 2, phivalues_scan, bbs);
3833
3834 if (ws->enable_reduce) {
3835 tmp = LLVMBuildSub(builder, ws->numwaves, ctx->i32_1, "");
3836 ws->result_reduce = ac_build_readlane(ctx, scan, tmp);
3837 }
3838 if (ws->enable_inclusive)
3839 ws->result_inclusive = ac_build_readlane(ctx, scan, ws->waveidx);
3840 if (ws->enable_exclusive) {
3841 tmp = LLVMBuildSub(builder, ws->waveidx, ctx->i32_1, "");
3842 tmp = ac_build_readlane(ctx, scan, tmp);
3843 tmp2 = LLVMBuildICmp(builder, LLVMIntEQ, ws->waveidx, ctx->i32_0, "");
3844 ws->result_exclusive = LLVMBuildSelect(builder, tmp2, identity, tmp, "");
3845 }
3846 }
3847
3848 /**
3849 * Inclusive scan of a per-wave value across an entire workgroup.
3850 *
3851 * This implies an s_barrier instruction.
3852 *
3853 * Unlike ac_build_inclusive_scan, the caller \em must ensure that all threads
3854 * of the workgroup are live. (This requirement cannot easily be relaxed in a
3855 * useful manner because of the barrier in the algorithm.)
3856 */
3857 void
3858 ac_build_wg_wavescan(struct ac_llvm_context *ctx, struct ac_wg_scan *ws)
3859 {
3860 ac_build_wg_wavescan_top(ctx, ws);
3861 ac_build_s_barrier(ctx);
3862 ac_build_wg_wavescan_bottom(ctx, ws);
3863 }
3864
3865 /**
3866 * "Top half" of a scan that reduces per-thread values across an entire
3867 * workgroup.
3868 *
3869 * All lanes must be active when this code runs.
3870 */
3871 void
3872 ac_build_wg_scan_top(struct ac_llvm_context *ctx, struct ac_wg_scan *ws)
3873 {
3874 if (ws->enable_exclusive) {
3875 ws->extra = ac_build_exclusive_scan(ctx, ws->src, ws->op);
3876 if (LLVMTypeOf(ws->src) == ctx->i1 && ws->op == nir_op_iadd)
3877 ws->src = LLVMBuildZExt(ctx->builder, ws->src, ctx->i32, "");
3878 ws->src = ac_build_alu_op(ctx, ws->extra, ws->src, ws->op);
3879 } else {
3880 ws->src = ac_build_inclusive_scan(ctx, ws->src, ws->op);
3881 }
3882
3883 bool enable_inclusive = ws->enable_inclusive;
3884 bool enable_exclusive = ws->enable_exclusive;
3885 ws->enable_inclusive = false;
3886 ws->enable_exclusive = ws->enable_exclusive || enable_inclusive;
3887 ac_build_wg_wavescan_top(ctx, ws);
3888 ws->enable_inclusive = enable_inclusive;
3889 ws->enable_exclusive = enable_exclusive;
3890 }
3891
3892 /**
3893 * "Bottom half" of a scan that reduces per-thread values across an entire
3894 * workgroup.
3895 *
3896 * The caller must place a barrier between the top and bottom halves.
3897 */
3898 void
3899 ac_build_wg_scan_bottom(struct ac_llvm_context *ctx, struct ac_wg_scan *ws)
3900 {
3901 bool enable_inclusive = ws->enable_inclusive;
3902 bool enable_exclusive = ws->enable_exclusive;
3903 ws->enable_inclusive = false;
3904 ws->enable_exclusive = ws->enable_exclusive || enable_inclusive;
3905 ac_build_wg_wavescan_bottom(ctx, ws);
3906 ws->enable_inclusive = enable_inclusive;
3907 ws->enable_exclusive = enable_exclusive;
3908
3909 /* ws->result_reduce is already the correct value */
3910 if (ws->enable_inclusive)
3911 ws->result_inclusive = ac_build_alu_op(ctx, ws->result_exclusive, ws->src, ws->op);
3912 if (ws->enable_exclusive)
3913 ws->result_exclusive = ac_build_alu_op(ctx, ws->result_exclusive, ws->extra, ws->op);
3914 }
3915
3916 /**
3917 * A scan that reduces per-thread values across an entire workgroup.
3918 *
3919 * The caller must ensure that all lanes are active when this code runs
3920 * (WWM is insufficient!), because there is an implied barrier.
3921 */
3922 void
3923 ac_build_wg_scan(struct ac_llvm_context *ctx, struct ac_wg_scan *ws)
3924 {
3925 ac_build_wg_scan_top(ctx, ws);
3926 ac_build_s_barrier(ctx);
3927 ac_build_wg_scan_bottom(ctx, ws);
3928 }
3929
3930 LLVMValueRef
3931 ac_build_quad_swizzle(struct ac_llvm_context *ctx, LLVMValueRef src,
3932 unsigned lane0, unsigned lane1, unsigned lane2, unsigned lane3)
3933 {
3934 unsigned mask = dpp_quad_perm(lane0, lane1, lane2, lane3);
3935 if (ctx->chip_class >= VI) {
3936 return ac_build_dpp(ctx, src, src, mask, 0xf, 0xf, false);
3937 } else {
3938 return ac_build_ds_swizzle(ctx, src, (1 << 15) | mask);
3939 }
3940 }
3941
3942 LLVMValueRef
3943 ac_build_shuffle(struct ac_llvm_context *ctx, LLVMValueRef src, LLVMValueRef index)
3944 {
3945 index = LLVMBuildMul(ctx->builder, index, LLVMConstInt(ctx->i32, 4, 0), "");
3946 return ac_build_intrinsic(ctx,
3947 "llvm.amdgcn.ds.bpermute", ctx->i32,
3948 (LLVMValueRef []) {index, src}, 2,
3949 AC_FUNC_ATTR_READNONE |
3950 AC_FUNC_ATTR_CONVERGENT);
3951 }
3952
3953 LLVMValueRef
3954 ac_build_frexp_exp(struct ac_llvm_context *ctx, LLVMValueRef src0,
3955 unsigned bitsize)
3956 {
3957 LLVMTypeRef type;
3958 char *intr;
3959
3960 if (bitsize == 16) {
3961 intr = "llvm.amdgcn.frexp.exp.i16.f16";
3962 type = ctx->i16;
3963 } else if (bitsize == 32) {
3964 intr = "llvm.amdgcn.frexp.exp.i32.f32";
3965 type = ctx->i32;
3966 } else {
3967 intr = "llvm.amdgcn.frexp.exp.i32.f64";
3968 type = ctx->i32;
3969 }
3970
3971 LLVMValueRef params[] = {
3972 src0,
3973 };
3974 return ac_build_intrinsic(ctx, intr, type, params, 1,
3975 AC_FUNC_ATTR_READNONE);
3976 }
3977 LLVMValueRef
3978 ac_build_frexp_mant(struct ac_llvm_context *ctx, LLVMValueRef src0,
3979 unsigned bitsize)
3980 {
3981 LLVMTypeRef type;
3982 char *intr;
3983
3984 if (bitsize == 16) {
3985 intr = "llvm.amdgcn.frexp.mant.f16";
3986 type = ctx->f16;
3987 } else if (bitsize == 32) {
3988 intr = "llvm.amdgcn.frexp.mant.f32";
3989 type = ctx->f32;
3990 } else {
3991 intr = "llvm.amdgcn.frexp.mant.f64";
3992 type = ctx->f64;
3993 }
3994
3995 LLVMValueRef params[] = {
3996 src0,
3997 };
3998 return ac_build_intrinsic(ctx, intr, type, params, 1,
3999 AC_FUNC_ATTR_READNONE);
4000 }