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