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