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