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