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