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