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