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