amd/common: lower bitfield_extract to ubfe/ibfe.
[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 use_format)
1120 {
1121 LLVMValueRef args[] = {
1122 data,
1123 LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, ""),
1124 vindex ? vindex : ctx->i32_0,
1125 voffset,
1126 LLVMConstInt(ctx->i1, glc, 0),
1127 LLVMConstInt(ctx->i1, slc, 0)
1128 };
1129 unsigned func = CLAMP(num_channels, 1, 3) - 1;
1130
1131 const char *type_names[] = {"f32", "v2f32", "v4f32"};
1132 char name[256];
1133
1134 if (use_format) {
1135 snprintf(name, sizeof(name), "llvm.amdgcn.buffer.store.format.%s",
1136 type_names[func]);
1137 } else {
1138 snprintf(name, sizeof(name), "llvm.amdgcn.buffer.store.%s",
1139 type_names[func]);
1140 }
1141
1142 ac_build_intrinsic(ctx, name, ctx->voidt, args, ARRAY_SIZE(args),
1143 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY);
1144 }
1145
1146 static void
1147 ac_build_llvm8_buffer_store_common(struct ac_llvm_context *ctx,
1148 LLVMValueRef rsrc,
1149 LLVMValueRef data,
1150 LLVMValueRef vindex,
1151 LLVMValueRef voffset,
1152 LLVMValueRef soffset,
1153 unsigned num_channels,
1154 LLVMTypeRef return_channel_type,
1155 bool glc,
1156 bool slc,
1157 bool use_format,
1158 bool structurized)
1159 {
1160 LLVMValueRef args[6];
1161 int idx = 0;
1162 args[idx++] = data;
1163 args[idx++] = LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, "");
1164 if (structurized)
1165 args[idx++] = vindex ? vindex : ctx->i32_0;
1166 args[idx++] = voffset ? voffset : ctx->i32_0;
1167 args[idx++] = soffset ? soffset : ctx->i32_0;
1168 args[idx++] = LLVMConstInt(ctx->i32, (glc ? 1 : 0) + (slc ? 2 : 0), 0);
1169 unsigned func = !ac_has_vec3_support(ctx->chip_class, use_format) && num_channels == 3 ? 4 : num_channels;
1170 const char *indexing_kind = structurized ? "struct" : "raw";
1171 char name[256], type_name[8];
1172
1173 LLVMTypeRef type = func > 1 ? LLVMVectorType(return_channel_type, func) : return_channel_type;
1174 ac_build_type_name_for_intr(type, type_name, sizeof(type_name));
1175
1176 if (use_format) {
1177 snprintf(name, sizeof(name), "llvm.amdgcn.%s.buffer.store.format.%s",
1178 indexing_kind, type_name);
1179 } else {
1180 snprintf(name, sizeof(name), "llvm.amdgcn.%s.buffer.store.%s",
1181 indexing_kind, type_name);
1182 }
1183
1184 ac_build_intrinsic(ctx, name, ctx->voidt, args, idx,
1185 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY);
1186 }
1187
1188 void
1189 ac_build_buffer_store_format(struct ac_llvm_context *ctx,
1190 LLVMValueRef rsrc,
1191 LLVMValueRef data,
1192 LLVMValueRef vindex,
1193 LLVMValueRef voffset,
1194 unsigned num_channels,
1195 bool glc,
1196 bool slc)
1197 {
1198 if (HAVE_LLVM >= 0x800) {
1199 ac_build_llvm8_buffer_store_common(ctx, rsrc, data, vindex,
1200 voffset, NULL, num_channels,
1201 ctx->f32, glc, slc,
1202 true, true);
1203 } else {
1204 ac_build_llvm7_buffer_store_common(ctx, rsrc, data, vindex, voffset,
1205 num_channels, glc, slc,
1206 true);
1207 }
1208 }
1209
1210 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
1211 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
1212 * or v4i32 (num_channels=3,4).
1213 */
1214 void
1215 ac_build_buffer_store_dword(struct ac_llvm_context *ctx,
1216 LLVMValueRef rsrc,
1217 LLVMValueRef vdata,
1218 unsigned num_channels,
1219 LLVMValueRef voffset,
1220 LLVMValueRef soffset,
1221 unsigned inst_offset,
1222 bool glc,
1223 bool slc,
1224 bool swizzle_enable_hint)
1225 {
1226 /* Split 3 channel stores, because only LLVM 9+ support 3-channel
1227 * intrinsics. */
1228 if (num_channels == 3 && !ac_has_vec3_support(ctx->chip_class, false)) {
1229 LLVMValueRef v[3], v01;
1230
1231 for (int i = 0; i < 3; i++) {
1232 v[i] = LLVMBuildExtractElement(ctx->builder, vdata,
1233 LLVMConstInt(ctx->i32, i, 0), "");
1234 }
1235 v01 = ac_build_gather_values(ctx, v, 2);
1236
1237 ac_build_buffer_store_dword(ctx, rsrc, v01, 2, voffset,
1238 soffset, inst_offset, glc, slc,
1239 swizzle_enable_hint);
1240 ac_build_buffer_store_dword(ctx, rsrc, v[2], 1, voffset,
1241 soffset, inst_offset + 8,
1242 glc, slc,
1243 swizzle_enable_hint);
1244 return;
1245 }
1246
1247 /* SWIZZLE_ENABLE requires that soffset isn't folded into voffset
1248 * (voffset is swizzled, but soffset isn't swizzled).
1249 * llvm.amdgcn.buffer.store doesn't have a separate soffset parameter.
1250 */
1251 if (!swizzle_enable_hint) {
1252 LLVMValueRef offset = soffset;
1253
1254 if (inst_offset)
1255 offset = LLVMBuildAdd(ctx->builder, offset,
1256 LLVMConstInt(ctx->i32, inst_offset, 0), "");
1257
1258 if (HAVE_LLVM >= 0x800) {
1259 ac_build_llvm8_buffer_store_common(ctx, rsrc,
1260 ac_to_float(ctx, vdata),
1261 ctx->i32_0,
1262 voffset, offset,
1263 num_channels,
1264 ctx->f32,
1265 glc, slc,
1266 false, false);
1267 } else {
1268 if (voffset)
1269 offset = LLVMBuildAdd(ctx->builder, offset, voffset, "");
1270
1271 ac_build_llvm7_buffer_store_common(ctx, rsrc,
1272 ac_to_float(ctx, vdata),
1273 ctx->i32_0, offset,
1274 num_channels, glc, slc,
1275 false);
1276 }
1277 return;
1278 }
1279
1280 static const unsigned dfmts[] = {
1281 V_008F0C_BUF_DATA_FORMAT_32,
1282 V_008F0C_BUF_DATA_FORMAT_32_32,
1283 V_008F0C_BUF_DATA_FORMAT_32_32_32,
1284 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
1285 };
1286 unsigned dfmt = dfmts[num_channels - 1];
1287 unsigned nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
1288 LLVMValueRef immoffset = LLVMConstInt(ctx->i32, inst_offset, 0);
1289
1290 ac_build_raw_tbuffer_store(ctx, rsrc, vdata, voffset, soffset,
1291 immoffset, num_channels, dfmt, nfmt, glc,
1292 slc);
1293 }
1294
1295 static LLVMValueRef
1296 ac_build_llvm7_buffer_load_common(struct ac_llvm_context *ctx,
1297 LLVMValueRef rsrc,
1298 LLVMValueRef vindex,
1299 LLVMValueRef voffset,
1300 unsigned num_channels,
1301 bool glc,
1302 bool slc,
1303 bool can_speculate,
1304 bool use_format)
1305 {
1306 LLVMValueRef args[] = {
1307 LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, ""),
1308 vindex ? vindex : ctx->i32_0,
1309 voffset,
1310 LLVMConstInt(ctx->i1, glc, 0),
1311 LLVMConstInt(ctx->i1, slc, 0)
1312 };
1313 unsigned func = CLAMP(num_channels, 1, 3) - 1;
1314
1315 LLVMTypeRef types[] = {ctx->f32, ctx->v2f32, ctx->v4f32};
1316 const char *type_names[] = {"f32", "v2f32", "v4f32"};
1317 char name[256];
1318
1319 if (use_format) {
1320 snprintf(name, sizeof(name), "llvm.amdgcn.buffer.load.format.%s",
1321 type_names[func]);
1322 } else {
1323 snprintf(name, sizeof(name), "llvm.amdgcn.buffer.load.%s",
1324 type_names[func]);
1325 }
1326
1327 return ac_build_intrinsic(ctx, name, types[func], args,
1328 ARRAY_SIZE(args),
1329 ac_get_load_intr_attribs(can_speculate));
1330 }
1331
1332 static LLVMValueRef
1333 ac_build_llvm8_buffer_load_common(struct ac_llvm_context *ctx,
1334 LLVMValueRef rsrc,
1335 LLVMValueRef vindex,
1336 LLVMValueRef voffset,
1337 LLVMValueRef soffset,
1338 unsigned num_channels,
1339 LLVMTypeRef channel_type,
1340 bool glc,
1341 bool slc,
1342 bool can_speculate,
1343 bool use_format,
1344 bool structurized)
1345 {
1346 LLVMValueRef args[5];
1347 int idx = 0;
1348 args[idx++] = LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, "");
1349 if (structurized)
1350 args[idx++] = vindex ? vindex : ctx->i32_0;
1351 args[idx++] = voffset ? voffset : ctx->i32_0;
1352 args[idx++] = soffset ? soffset : ctx->i32_0;
1353 args[idx++] = LLVMConstInt(ctx->i32, (glc ? 1 : 0) + (slc ? 2 : 0), 0);
1354 unsigned func = !ac_has_vec3_support(ctx->chip_class, use_format) && num_channels == 3 ? 4 : num_channels;
1355 const char *indexing_kind = structurized ? "struct" : "raw";
1356 char name[256], type_name[8];
1357
1358 LLVMTypeRef type = func > 1 ? LLVMVectorType(channel_type, func) : channel_type;
1359 ac_build_type_name_for_intr(type, type_name, sizeof(type_name));
1360
1361 if (use_format) {
1362 snprintf(name, sizeof(name), "llvm.amdgcn.%s.buffer.load.format.%s",
1363 indexing_kind, type_name);
1364 } else {
1365 snprintf(name, sizeof(name), "llvm.amdgcn.%s.buffer.load.%s",
1366 indexing_kind, type_name);
1367 }
1368
1369 return ac_build_intrinsic(ctx, name, type, args, idx,
1370 ac_get_load_intr_attribs(can_speculate));
1371 }
1372
1373 LLVMValueRef
1374 ac_build_buffer_load(struct ac_llvm_context *ctx,
1375 LLVMValueRef rsrc,
1376 int num_channels,
1377 LLVMValueRef vindex,
1378 LLVMValueRef voffset,
1379 LLVMValueRef soffset,
1380 unsigned inst_offset,
1381 unsigned glc,
1382 unsigned slc,
1383 bool can_speculate,
1384 bool allow_smem)
1385 {
1386 LLVMValueRef offset = LLVMConstInt(ctx->i32, inst_offset, 0);
1387 if (voffset)
1388 offset = LLVMBuildAdd(ctx->builder, offset, voffset, "");
1389 if (soffset)
1390 offset = LLVMBuildAdd(ctx->builder, offset, soffset, "");
1391
1392 if (allow_smem && !slc &&
1393 (!glc || (HAVE_LLVM >= 0x0800 && ctx->chip_class >= GFX8))) {
1394 assert(vindex == NULL);
1395
1396 LLVMValueRef result[8];
1397
1398 for (int i = 0; i < num_channels; i++) {
1399 if (i) {
1400 offset = LLVMBuildAdd(ctx->builder, offset,
1401 LLVMConstInt(ctx->i32, 4, 0), "");
1402 }
1403 const char *intrname =
1404 HAVE_LLVM >= 0x0800 ? "llvm.amdgcn.s.buffer.load.f32"
1405 : "llvm.SI.load.const.v4i32";
1406 unsigned num_args = HAVE_LLVM >= 0x0800 ? 3 : 2;
1407 LLVMValueRef args[3] = {
1408 rsrc,
1409 offset,
1410 glc ? ctx->i32_1 : ctx->i32_0,
1411 };
1412 result[i] = ac_build_intrinsic(ctx, intrname,
1413 ctx->f32, args, num_args,
1414 AC_FUNC_ATTR_READNONE |
1415 (HAVE_LLVM < 0x0800 ? AC_FUNC_ATTR_LEGACY : 0));
1416 }
1417 if (num_channels == 1)
1418 return result[0];
1419
1420 if (num_channels == 3 && !ac_has_vec3_support(ctx->chip_class, false))
1421 result[num_channels++] = LLVMGetUndef(ctx->f32);
1422 return ac_build_gather_values(ctx, result, num_channels);
1423 }
1424
1425 if (HAVE_LLVM >= 0x0800) {
1426 return ac_build_llvm8_buffer_load_common(ctx, rsrc, vindex,
1427 offset, ctx->i32_0,
1428 num_channels, ctx->f32,
1429 glc, slc,
1430 can_speculate, false,
1431 false);
1432 }
1433
1434 return ac_build_llvm7_buffer_load_common(ctx, rsrc, vindex, offset,
1435 num_channels, glc, slc,
1436 can_speculate, false);
1437 }
1438
1439 LLVMValueRef ac_build_buffer_load_format(struct ac_llvm_context *ctx,
1440 LLVMValueRef rsrc,
1441 LLVMValueRef vindex,
1442 LLVMValueRef voffset,
1443 unsigned num_channels,
1444 bool glc,
1445 bool can_speculate)
1446 {
1447 if (HAVE_LLVM >= 0x800) {
1448 return ac_build_llvm8_buffer_load_common(ctx, rsrc, vindex, voffset, ctx->i32_0,
1449 num_channels, ctx->f32,
1450 glc, false,
1451 can_speculate, true, true);
1452 }
1453 return ac_build_llvm7_buffer_load_common(ctx, rsrc, vindex, voffset,
1454 num_channels, glc, false,
1455 can_speculate, true);
1456 }
1457
1458 LLVMValueRef ac_build_buffer_load_format_gfx9_safe(struct ac_llvm_context *ctx,
1459 LLVMValueRef rsrc,
1460 LLVMValueRef vindex,
1461 LLVMValueRef voffset,
1462 unsigned num_channels,
1463 bool glc,
1464 bool can_speculate)
1465 {
1466 if (HAVE_LLVM >= 0x800) {
1467 return ac_build_llvm8_buffer_load_common(ctx, rsrc, vindex, voffset, ctx->i32_0,
1468 num_channels, ctx->f32,
1469 glc, false,
1470 can_speculate, true, true);
1471 }
1472
1473 LLVMValueRef elem_count = LLVMBuildExtractElement(ctx->builder, rsrc, LLVMConstInt(ctx->i32, 2, 0), "");
1474 LLVMValueRef stride = LLVMBuildExtractElement(ctx->builder, rsrc, ctx->i32_1, "");
1475 stride = LLVMBuildLShr(ctx->builder, stride, LLVMConstInt(ctx->i32, 16, 0), "");
1476
1477 LLVMValueRef new_elem_count = LLVMBuildSelect(ctx->builder,
1478 LLVMBuildICmp(ctx->builder, LLVMIntUGT, elem_count, stride, ""),
1479 elem_count, stride, "");
1480
1481 LLVMValueRef new_rsrc = LLVMBuildInsertElement(ctx->builder, rsrc, new_elem_count,
1482 LLVMConstInt(ctx->i32, 2, 0), "");
1483
1484 return ac_build_llvm7_buffer_load_common(ctx, new_rsrc, vindex, voffset,
1485 num_channels, glc, false,
1486 can_speculate, true);
1487 }
1488
1489 static LLVMValueRef
1490 ac_build_llvm8_tbuffer_load(struct ac_llvm_context *ctx,
1491 LLVMValueRef rsrc,
1492 LLVMValueRef vindex,
1493 LLVMValueRef voffset,
1494 LLVMValueRef soffset,
1495 unsigned num_channels,
1496 unsigned dfmt,
1497 unsigned nfmt,
1498 bool glc,
1499 bool slc,
1500 bool can_speculate,
1501 bool structurized)
1502 {
1503 LLVMValueRef args[6];
1504 int idx = 0;
1505 args[idx++] = LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, "");
1506 if (structurized)
1507 args[idx++] = vindex ? vindex : ctx->i32_0;
1508 args[idx++] = voffset ? voffset : ctx->i32_0;
1509 args[idx++] = soffset ? soffset : ctx->i32_0;
1510 args[idx++] = LLVMConstInt(ctx->i32, dfmt | (nfmt << 4), 0);
1511 args[idx++] = LLVMConstInt(ctx->i32, (glc ? 1 : 0) + (slc ? 2 : 0), 0);
1512 unsigned func = !ac_has_vec3_support(ctx->chip_class, true) && num_channels == 3 ? 4 : num_channels;
1513 const char *indexing_kind = structurized ? "struct" : "raw";
1514 char name[256], type_name[8];
1515
1516 LLVMTypeRef type = func > 1 ? LLVMVectorType(ctx->i32, func) : ctx->i32;
1517 ac_build_type_name_for_intr(type, type_name, sizeof(type_name));
1518
1519 snprintf(name, sizeof(name), "llvm.amdgcn.%s.tbuffer.load.%s",
1520 indexing_kind, type_name);
1521
1522 return ac_build_intrinsic(ctx, name, type, args, idx,
1523 ac_get_load_intr_attribs(can_speculate));
1524 }
1525
1526 static LLVMValueRef
1527 ac_build_tbuffer_load(struct ac_llvm_context *ctx,
1528 LLVMValueRef rsrc,
1529 LLVMValueRef vindex,
1530 LLVMValueRef voffset,
1531 LLVMValueRef soffset,
1532 LLVMValueRef immoffset,
1533 unsigned num_channels,
1534 unsigned dfmt,
1535 unsigned nfmt,
1536 bool glc,
1537 bool slc,
1538 bool can_speculate,
1539 bool structurized) /* only matters for LLVM 8+ */
1540 {
1541 if (HAVE_LLVM >= 0x800) {
1542 voffset = LLVMBuildAdd(ctx->builder, voffset, immoffset, "");
1543
1544 return ac_build_llvm8_tbuffer_load(ctx, rsrc, vindex, voffset,
1545 soffset, num_channels,
1546 dfmt, nfmt, glc, slc,
1547 can_speculate, structurized);
1548 }
1549
1550 LLVMValueRef args[] = {
1551 rsrc,
1552 vindex ? vindex : ctx->i32_0,
1553 voffset,
1554 soffset,
1555 immoffset,
1556 LLVMConstInt(ctx->i32, dfmt, false),
1557 LLVMConstInt(ctx->i32, nfmt, false),
1558 LLVMConstInt(ctx->i1, glc, false),
1559 LLVMConstInt(ctx->i1, slc, false),
1560 };
1561 unsigned func = CLAMP(num_channels, 1, 3) - 1;
1562 LLVMTypeRef types[] = {ctx->i32, ctx->v2i32, ctx->v4i32};
1563 const char *type_names[] = {"i32", "v2i32", "v4i32"};
1564 char name[256];
1565
1566 snprintf(name, sizeof(name), "llvm.amdgcn.tbuffer.load.%s",
1567 type_names[func]);
1568
1569 return ac_build_intrinsic(ctx, name, types[func], args, 9,
1570 ac_get_load_intr_attribs(can_speculate));
1571 }
1572
1573 LLVMValueRef
1574 ac_build_struct_tbuffer_load(struct ac_llvm_context *ctx,
1575 LLVMValueRef rsrc,
1576 LLVMValueRef vindex,
1577 LLVMValueRef voffset,
1578 LLVMValueRef soffset,
1579 LLVMValueRef immoffset,
1580 unsigned num_channels,
1581 unsigned dfmt,
1582 unsigned nfmt,
1583 bool glc,
1584 bool slc,
1585 bool can_speculate)
1586 {
1587 return ac_build_tbuffer_load(ctx, rsrc, vindex, voffset, soffset,
1588 immoffset, num_channels, dfmt, nfmt, glc,
1589 slc, can_speculate, true);
1590 }
1591
1592 LLVMValueRef
1593 ac_build_raw_tbuffer_load(struct ac_llvm_context *ctx,
1594 LLVMValueRef rsrc,
1595 LLVMValueRef voffset,
1596 LLVMValueRef soffset,
1597 LLVMValueRef immoffset,
1598 unsigned num_channels,
1599 unsigned dfmt,
1600 unsigned nfmt,
1601 bool glc,
1602 bool slc,
1603 bool can_speculate)
1604 {
1605 return ac_build_tbuffer_load(ctx, rsrc, NULL, voffset, soffset,
1606 immoffset, num_channels, dfmt, nfmt, glc,
1607 slc, can_speculate, false);
1608 }
1609
1610 LLVMValueRef
1611 ac_build_tbuffer_load_short(struct ac_llvm_context *ctx,
1612 LLVMValueRef rsrc,
1613 LLVMValueRef voffset,
1614 LLVMValueRef soffset,
1615 LLVMValueRef immoffset,
1616 bool glc)
1617 {
1618 LLVMValueRef res;
1619
1620 if (HAVE_LLVM >= 0x900) {
1621 voffset = LLVMBuildAdd(ctx->builder, voffset, immoffset, "");
1622
1623 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1624 res = ac_build_llvm8_buffer_load_common(ctx, rsrc, NULL,
1625 voffset, soffset,
1626 1, ctx->i16, glc, false,
1627 false, false, false);
1628 } else {
1629 unsigned dfmt = V_008F0C_BUF_DATA_FORMAT_16;
1630 unsigned nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
1631
1632 res = ac_build_raw_tbuffer_load(ctx, rsrc, voffset, soffset,
1633 immoffset, 1, dfmt, nfmt, glc, false,
1634 false);
1635
1636 res = LLVMBuildTrunc(ctx->builder, res, ctx->i16, "");
1637 }
1638
1639 return res;
1640 }
1641
1642 LLVMValueRef
1643 ac_build_tbuffer_load_byte(struct ac_llvm_context *ctx,
1644 LLVMValueRef rsrc,
1645 LLVMValueRef voffset,
1646 LLVMValueRef soffset,
1647 LLVMValueRef immoffset,
1648 bool glc)
1649 {
1650 LLVMValueRef res;
1651
1652 if (HAVE_LLVM >= 0x900) {
1653 voffset = LLVMBuildAdd(ctx->builder, voffset, immoffset, "");
1654
1655 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1656 res = ac_build_llvm8_buffer_load_common(ctx, rsrc, NULL,
1657 voffset, soffset,
1658 1, ctx->i8, glc, false,
1659 false, false, false);
1660 } else {
1661 unsigned dfmt = V_008F0C_BUF_DATA_FORMAT_8;
1662 unsigned nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
1663
1664 res = ac_build_raw_tbuffer_load(ctx, rsrc, voffset, soffset,
1665 immoffset, 1, dfmt, nfmt, glc, false,
1666 false);
1667
1668 res = LLVMBuildTrunc(ctx->builder, res, ctx->i8, "");
1669 }
1670
1671 return res;
1672 }
1673
1674 /**
1675 * Convert an 11- or 10-bit unsigned floating point number to an f32.
1676 *
1677 * The input exponent is expected to be biased analogous to IEEE-754, i.e. by
1678 * 2^(exp_bits-1) - 1 (as defined in OpenGL and other graphics APIs).
1679 */
1680 static LLVMValueRef
1681 ac_ufN_to_float(struct ac_llvm_context *ctx, LLVMValueRef src, unsigned exp_bits, unsigned mant_bits)
1682 {
1683 assert(LLVMTypeOf(src) == ctx->i32);
1684
1685 LLVMValueRef tmp;
1686 LLVMValueRef mantissa;
1687 mantissa = LLVMBuildAnd(ctx->builder, src, LLVMConstInt(ctx->i32, (1 << mant_bits) - 1, false), "");
1688
1689 /* Converting normal numbers is just a shift + correcting the exponent bias */
1690 unsigned normal_shift = 23 - mant_bits;
1691 unsigned bias_shift = 127 - ((1 << (exp_bits - 1)) - 1);
1692 LLVMValueRef shifted, normal;
1693
1694 shifted = LLVMBuildShl(ctx->builder, src, LLVMConstInt(ctx->i32, normal_shift, false), "");
1695 normal = LLVMBuildAdd(ctx->builder, shifted, LLVMConstInt(ctx->i32, bias_shift << 23, false), "");
1696
1697 /* Converting nan/inf numbers is the same, but with a different exponent update */
1698 LLVMValueRef naninf;
1699 naninf = LLVMBuildOr(ctx->builder, normal, LLVMConstInt(ctx->i32, 0xff << 23, false), "");
1700
1701 /* Converting denormals is the complex case: determine the leading zeros of the
1702 * mantissa to obtain the correct shift for the mantissa and exponent correction.
1703 */
1704 LLVMValueRef denormal;
1705 LLVMValueRef params[2] = {
1706 mantissa,
1707 ctx->i1true, /* result can be undef when arg is 0 */
1708 };
1709 LLVMValueRef ctlz = ac_build_intrinsic(ctx, "llvm.ctlz.i32", ctx->i32,
1710 params, 2, AC_FUNC_ATTR_READNONE);
1711
1712 /* Shift such that the leading 1 ends up as the LSB of the exponent field. */
1713 tmp = LLVMBuildSub(ctx->builder, ctlz, LLVMConstInt(ctx->i32, 8, false), "");
1714 denormal = LLVMBuildShl(ctx->builder, mantissa, tmp, "");
1715
1716 unsigned denormal_exp = bias_shift + (32 - mant_bits) - 1;
1717 tmp = LLVMBuildSub(ctx->builder, LLVMConstInt(ctx->i32, denormal_exp, false), ctlz, "");
1718 tmp = LLVMBuildShl(ctx->builder, tmp, LLVMConstInt(ctx->i32, 23, false), "");
1719 denormal = LLVMBuildAdd(ctx->builder, denormal, tmp, "");
1720
1721 /* Select the final result. */
1722 LLVMValueRef result;
1723
1724 tmp = LLVMBuildICmp(ctx->builder, LLVMIntUGE, src,
1725 LLVMConstInt(ctx->i32, ((1 << exp_bits) - 1) << mant_bits, false), "");
1726 result = LLVMBuildSelect(ctx->builder, tmp, naninf, normal, "");
1727
1728 tmp = LLVMBuildICmp(ctx->builder, LLVMIntUGE, src,
1729 LLVMConstInt(ctx->i32, 1 << mant_bits, false), "");
1730 result = LLVMBuildSelect(ctx->builder, tmp, result, denormal, "");
1731
1732 tmp = LLVMBuildICmp(ctx->builder, LLVMIntNE, src, ctx->i32_0, "");
1733 result = LLVMBuildSelect(ctx->builder, tmp, result, ctx->i32_0, "");
1734
1735 return ac_to_float(ctx, result);
1736 }
1737
1738 /**
1739 * Generate a fully general open coded buffer format fetch with all required
1740 * fixups suitable for vertex fetch, using non-format buffer loads.
1741 *
1742 * Some combinations of argument values have special interpretations:
1743 * - size = 8 bytes, format = fixed indicates PIPE_FORMAT_R11G11B10_FLOAT
1744 * - size = 8 bytes, format != {float,fixed} indicates a 2_10_10_10 data format
1745 *
1746 * \param log_size log(size of channel in bytes)
1747 * \param num_channels number of channels (1 to 4)
1748 * \param format AC_FETCH_FORMAT_xxx value
1749 * \param reverse whether XYZ channels are reversed
1750 * \param known_aligned whether the source is known to be aligned to hardware's
1751 * effective element size for loading the given format
1752 * (note: this means dword alignment for 8_8_8_8, 16_16, etc.)
1753 * \param rsrc buffer resource descriptor
1754 * \return the resulting vector of floats or integers bitcast to <4 x i32>
1755 */
1756 LLVMValueRef
1757 ac_build_opencoded_load_format(struct ac_llvm_context *ctx,
1758 unsigned log_size,
1759 unsigned num_channels,
1760 unsigned format,
1761 bool reverse,
1762 bool known_aligned,
1763 LLVMValueRef rsrc,
1764 LLVMValueRef vindex,
1765 LLVMValueRef voffset,
1766 LLVMValueRef soffset,
1767 bool glc,
1768 bool slc,
1769 bool can_speculate)
1770 {
1771 LLVMValueRef tmp;
1772 unsigned load_log_size = log_size;
1773 unsigned load_num_channels = num_channels;
1774 if (log_size == 3) {
1775 load_log_size = 2;
1776 if (format == AC_FETCH_FORMAT_FLOAT) {
1777 load_num_channels = 2 * num_channels;
1778 } else {
1779 load_num_channels = 1; /* 10_11_11 or 2_10_10_10 */
1780 }
1781 }
1782
1783 int log_recombine = 0;
1784 if (ctx->chip_class == GFX6 && !known_aligned) {
1785 /* Avoid alignment restrictions by loading one byte at a time. */
1786 load_num_channels <<= load_log_size;
1787 log_recombine = load_log_size;
1788 load_log_size = 0;
1789 } else if (load_num_channels == 2 || load_num_channels == 4) {
1790 log_recombine = -util_logbase2(load_num_channels);
1791 load_num_channels = 1;
1792 load_log_size += -log_recombine;
1793 }
1794
1795 assert(load_log_size >= 2 || HAVE_LLVM >= 0x0900);
1796
1797 LLVMValueRef loads[32]; /* up to 32 bytes */
1798 for (unsigned i = 0; i < load_num_channels; ++i) {
1799 tmp = LLVMBuildAdd(ctx->builder, soffset,
1800 LLVMConstInt(ctx->i32, i << load_log_size, false), "");
1801 if (HAVE_LLVM >= 0x0800) {
1802 LLVMTypeRef channel_type = load_log_size == 0 ? ctx->i8 :
1803 load_log_size == 1 ? ctx->i16 : ctx->i32;
1804 unsigned num_channels = 1 << (MAX2(load_log_size, 2) - 2);
1805 loads[i] = ac_build_llvm8_buffer_load_common(
1806 ctx, rsrc, vindex, voffset, tmp,
1807 num_channels, channel_type, glc, slc,
1808 can_speculate, false, true);
1809 } else {
1810 tmp = LLVMBuildAdd(ctx->builder, voffset, tmp, "");
1811 loads[i] = ac_build_llvm7_buffer_load_common(
1812 ctx, rsrc, vindex, tmp,
1813 1 << (load_log_size - 2), glc, slc, can_speculate, false);
1814 }
1815 if (load_log_size >= 2)
1816 loads[i] = ac_to_integer(ctx, loads[i]);
1817 }
1818
1819 if (log_recombine > 0) {
1820 /* Recombine bytes if necessary (GFX6 only) */
1821 LLVMTypeRef dst_type = log_recombine == 2 ? ctx->i32 : ctx->i16;
1822
1823 for (unsigned src = 0, dst = 0; src < load_num_channels; ++dst) {
1824 LLVMValueRef accum = NULL;
1825 for (unsigned i = 0; i < (1 << log_recombine); ++i, ++src) {
1826 tmp = LLVMBuildZExt(ctx->builder, loads[src], dst_type, "");
1827 if (i == 0) {
1828 accum = tmp;
1829 } else {
1830 tmp = LLVMBuildShl(ctx->builder, tmp,
1831 LLVMConstInt(dst_type, 8 * i, false), "");
1832 accum = LLVMBuildOr(ctx->builder, accum, tmp, "");
1833 }
1834 }
1835 loads[dst] = accum;
1836 }
1837 } else if (log_recombine < 0) {
1838 /* Split vectors of dwords */
1839 if (load_log_size > 2) {
1840 assert(load_num_channels == 1);
1841 LLVMValueRef loaded = loads[0];
1842 unsigned log_split = load_log_size - 2;
1843 log_recombine += log_split;
1844 load_num_channels = 1 << log_split;
1845 load_log_size = 2;
1846 for (unsigned i = 0; i < load_num_channels; ++i) {
1847 tmp = LLVMConstInt(ctx->i32, i, false);
1848 loads[i] = LLVMBuildExtractElement(ctx->builder, loaded, tmp, "");
1849 }
1850 }
1851
1852 /* Further split dwords and shorts if required */
1853 if (log_recombine < 0) {
1854 for (unsigned src = load_num_channels,
1855 dst = load_num_channels << -log_recombine;
1856 src > 0; --src) {
1857 unsigned dst_bits = 1 << (3 + load_log_size + log_recombine);
1858 LLVMTypeRef dst_type = LLVMIntTypeInContext(ctx->context, dst_bits);
1859 LLVMValueRef loaded = loads[src - 1];
1860 LLVMTypeRef loaded_type = LLVMTypeOf(loaded);
1861 for (unsigned i = 1 << -log_recombine; i > 0; --i, --dst) {
1862 tmp = LLVMConstInt(loaded_type, dst_bits * (i - 1), false);
1863 tmp = LLVMBuildLShr(ctx->builder, loaded, tmp, "");
1864 loads[dst - 1] = LLVMBuildTrunc(ctx->builder, tmp, dst_type, "");
1865 }
1866 }
1867 }
1868 }
1869
1870 if (log_size == 3) {
1871 if (format == AC_FETCH_FORMAT_FLOAT) {
1872 for (unsigned i = 0; i < num_channels; ++i) {
1873 tmp = ac_build_gather_values(ctx, &loads[2 * i], 2);
1874 loads[i] = LLVMBuildBitCast(ctx->builder, tmp, ctx->f64, "");
1875 }
1876 } else if (format == AC_FETCH_FORMAT_FIXED) {
1877 /* 10_11_11_FLOAT */
1878 LLVMValueRef data = loads[0];
1879 LLVMValueRef i32_2047 = LLVMConstInt(ctx->i32, 2047, false);
1880 LLVMValueRef r = LLVMBuildAnd(ctx->builder, data, i32_2047, "");
1881 tmp = LLVMBuildLShr(ctx->builder, data, LLVMConstInt(ctx->i32, 11, false), "");
1882 LLVMValueRef g = LLVMBuildAnd(ctx->builder, tmp, i32_2047, "");
1883 LLVMValueRef b = LLVMBuildLShr(ctx->builder, data, LLVMConstInt(ctx->i32, 22, false), "");
1884
1885 loads[0] = ac_to_integer(ctx, ac_ufN_to_float(ctx, r, 5, 6));
1886 loads[1] = ac_to_integer(ctx, ac_ufN_to_float(ctx, g, 5, 6));
1887 loads[2] = ac_to_integer(ctx, ac_ufN_to_float(ctx, b, 5, 5));
1888
1889 num_channels = 3;
1890 log_size = 2;
1891 format = AC_FETCH_FORMAT_FLOAT;
1892 } else {
1893 /* 2_10_10_10 data formats */
1894 LLVMValueRef data = loads[0];
1895 LLVMTypeRef i10 = LLVMIntTypeInContext(ctx->context, 10);
1896 LLVMTypeRef i2 = LLVMIntTypeInContext(ctx->context, 2);
1897 loads[0] = LLVMBuildTrunc(ctx->builder, data, i10, "");
1898 tmp = LLVMBuildLShr(ctx->builder, data, LLVMConstInt(ctx->i32, 10, false), "");
1899 loads[1] = LLVMBuildTrunc(ctx->builder, tmp, i10, "");
1900 tmp = LLVMBuildLShr(ctx->builder, data, LLVMConstInt(ctx->i32, 20, false), "");
1901 loads[2] = LLVMBuildTrunc(ctx->builder, tmp, i10, "");
1902 tmp = LLVMBuildLShr(ctx->builder, data, LLVMConstInt(ctx->i32, 30, false), "");
1903 loads[3] = LLVMBuildTrunc(ctx->builder, tmp, i2, "");
1904
1905 num_channels = 4;
1906 }
1907 }
1908
1909 if (format == AC_FETCH_FORMAT_FLOAT) {
1910 if (log_size != 2) {
1911 for (unsigned chan = 0; chan < num_channels; ++chan) {
1912 tmp = ac_to_float(ctx, loads[chan]);
1913 if (log_size == 3)
1914 tmp = LLVMBuildFPTrunc(ctx->builder, tmp, ctx->f32, "");
1915 else if (log_size == 1)
1916 tmp = LLVMBuildFPExt(ctx->builder, tmp, ctx->f32, "");
1917 loads[chan] = ac_to_integer(ctx, tmp);
1918 }
1919 }
1920 } else if (format == AC_FETCH_FORMAT_UINT) {
1921 if (log_size != 2) {
1922 for (unsigned chan = 0; chan < num_channels; ++chan)
1923 loads[chan] = LLVMBuildZExt(ctx->builder, loads[chan], ctx->i32, "");
1924 }
1925 } else if (format == AC_FETCH_FORMAT_SINT) {
1926 if (log_size != 2) {
1927 for (unsigned chan = 0; chan < num_channels; ++chan)
1928 loads[chan] = LLVMBuildSExt(ctx->builder, loads[chan], ctx->i32, "");
1929 }
1930 } else {
1931 bool unsign = format == AC_FETCH_FORMAT_UNORM ||
1932 format == AC_FETCH_FORMAT_USCALED ||
1933 format == AC_FETCH_FORMAT_UINT;
1934
1935 for (unsigned chan = 0; chan < num_channels; ++chan) {
1936 if (unsign) {
1937 tmp = LLVMBuildUIToFP(ctx->builder, loads[chan], ctx->f32, "");
1938 } else {
1939 tmp = LLVMBuildSIToFP(ctx->builder, loads[chan], ctx->f32, "");
1940 }
1941
1942 LLVMValueRef scale = NULL;
1943 if (format == AC_FETCH_FORMAT_FIXED) {
1944 assert(log_size == 2);
1945 scale = LLVMConstReal(ctx->f32, 1.0 / 0x10000);
1946 } else if (format == AC_FETCH_FORMAT_UNORM) {
1947 unsigned bits = LLVMGetIntTypeWidth(LLVMTypeOf(loads[chan]));
1948 scale = LLVMConstReal(ctx->f32, 1.0 / (((uint64_t)1 << bits) - 1));
1949 } else if (format == AC_FETCH_FORMAT_SNORM) {
1950 unsigned bits = LLVMGetIntTypeWidth(LLVMTypeOf(loads[chan]));
1951 scale = LLVMConstReal(ctx->f32, 1.0 / (((uint64_t)1 << (bits - 1)) - 1));
1952 }
1953 if (scale)
1954 tmp = LLVMBuildFMul(ctx->builder, tmp, scale, "");
1955
1956 if (format == AC_FETCH_FORMAT_SNORM) {
1957 /* Clamp to [-1, 1] */
1958 LLVMValueRef neg_one = LLVMConstReal(ctx->f32, -1.0);
1959 LLVMValueRef clamp =
1960 LLVMBuildFCmp(ctx->builder, LLVMRealULT, tmp, neg_one, "");
1961 tmp = LLVMBuildSelect(ctx->builder, clamp, neg_one, tmp, "");
1962 }
1963
1964 loads[chan] = ac_to_integer(ctx, tmp);
1965 }
1966 }
1967
1968 while (num_channels < 4) {
1969 if (format == AC_FETCH_FORMAT_UINT || format == AC_FETCH_FORMAT_SINT) {
1970 loads[num_channels] = num_channels == 3 ? ctx->i32_1 : ctx->i32_0;
1971 } else {
1972 loads[num_channels] = ac_to_integer(ctx, num_channels == 3 ? ctx->f32_1 : ctx->f32_0);
1973 }
1974 num_channels++;
1975 }
1976
1977 if (reverse) {
1978 tmp = loads[0];
1979 loads[0] = loads[2];
1980 loads[2] = tmp;
1981 }
1982
1983 return ac_build_gather_values(ctx, loads, 4);
1984 }
1985
1986 static void
1987 ac_build_llvm8_tbuffer_store(struct ac_llvm_context *ctx,
1988 LLVMValueRef rsrc,
1989 LLVMValueRef vdata,
1990 LLVMValueRef vindex,
1991 LLVMValueRef voffset,
1992 LLVMValueRef soffset,
1993 unsigned num_channels,
1994 unsigned dfmt,
1995 unsigned nfmt,
1996 bool glc,
1997 bool slc,
1998 bool structurized)
1999 {
2000 LLVMValueRef args[7];
2001 int idx = 0;
2002 args[idx++] = vdata;
2003 args[idx++] = LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, "");
2004 if (structurized)
2005 args[idx++] = vindex ? vindex : ctx->i32_0;
2006 args[idx++] = voffset ? voffset : ctx->i32_0;
2007 args[idx++] = soffset ? soffset : ctx->i32_0;
2008 args[idx++] = LLVMConstInt(ctx->i32, dfmt | (nfmt << 4), 0);
2009 args[idx++] = LLVMConstInt(ctx->i32, (glc ? 1 : 0) + (slc ? 2 : 0), 0);
2010 unsigned func = !ac_has_vec3_support(ctx->chip_class, true) && num_channels == 3 ? 4 : num_channels;
2011 const char *indexing_kind = structurized ? "struct" : "raw";
2012 char name[256], type_name[8];
2013
2014 LLVMTypeRef type = func > 1 ? LLVMVectorType(ctx->i32, func) : ctx->i32;
2015 ac_build_type_name_for_intr(type, type_name, sizeof(type_name));
2016
2017 snprintf(name, sizeof(name), "llvm.amdgcn.%s.tbuffer.store.%s",
2018 indexing_kind, type_name);
2019
2020 ac_build_intrinsic(ctx, name, ctx->voidt, args, idx,
2021 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY);
2022 }
2023
2024 static void
2025 ac_build_tbuffer_store(struct ac_llvm_context *ctx,
2026 LLVMValueRef rsrc,
2027 LLVMValueRef vdata,
2028 LLVMValueRef vindex,
2029 LLVMValueRef voffset,
2030 LLVMValueRef soffset,
2031 LLVMValueRef immoffset,
2032 unsigned num_channels,
2033 unsigned dfmt,
2034 unsigned nfmt,
2035 bool glc,
2036 bool slc,
2037 bool structurized) /* only matters for LLVM 8+ */
2038 {
2039 if (HAVE_LLVM >= 0x800) {
2040 voffset = LLVMBuildAdd(ctx->builder,
2041 voffset ? voffset : ctx->i32_0,
2042 immoffset, "");
2043
2044 ac_build_llvm8_tbuffer_store(ctx, rsrc, vdata, vindex, voffset,
2045 soffset, num_channels, dfmt, nfmt,
2046 glc, slc, structurized);
2047 } else {
2048 LLVMValueRef params[] = {
2049 vdata,
2050 rsrc,
2051 vindex ? vindex : ctx->i32_0,
2052 voffset ? voffset : ctx->i32_0,
2053 soffset ? soffset : ctx->i32_0,
2054 immoffset,
2055 LLVMConstInt(ctx->i32, dfmt, false),
2056 LLVMConstInt(ctx->i32, nfmt, false),
2057 LLVMConstInt(ctx->i1, glc, false),
2058 LLVMConstInt(ctx->i1, slc, false),
2059 };
2060 unsigned func = CLAMP(num_channels, 1, 3) - 1;
2061 const char *type_names[] = {"i32", "v2i32", "v4i32"};
2062 char name[256];
2063
2064 snprintf(name, sizeof(name), "llvm.amdgcn.tbuffer.store.%s",
2065 type_names[func]);
2066
2067 ac_build_intrinsic(ctx, name, ctx->voidt, params, 10,
2068 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY);
2069 }
2070 }
2071
2072 void
2073 ac_build_struct_tbuffer_store(struct ac_llvm_context *ctx,
2074 LLVMValueRef rsrc,
2075 LLVMValueRef vdata,
2076 LLVMValueRef vindex,
2077 LLVMValueRef voffset,
2078 LLVMValueRef soffset,
2079 LLVMValueRef immoffset,
2080 unsigned num_channels,
2081 unsigned dfmt,
2082 unsigned nfmt,
2083 bool glc,
2084 bool slc)
2085 {
2086 ac_build_tbuffer_store(ctx, rsrc, vdata, vindex, voffset, soffset,
2087 immoffset, num_channels, dfmt, nfmt, glc, slc,
2088 true);
2089 }
2090
2091 void
2092 ac_build_raw_tbuffer_store(struct ac_llvm_context *ctx,
2093 LLVMValueRef rsrc,
2094 LLVMValueRef vdata,
2095 LLVMValueRef voffset,
2096 LLVMValueRef soffset,
2097 LLVMValueRef immoffset,
2098 unsigned num_channels,
2099 unsigned dfmt,
2100 unsigned nfmt,
2101 bool glc,
2102 bool slc)
2103 {
2104 ac_build_tbuffer_store(ctx, rsrc, vdata, NULL, voffset, soffset,
2105 immoffset, num_channels, dfmt, nfmt, glc, slc,
2106 false);
2107 }
2108
2109 void
2110 ac_build_tbuffer_store_short(struct ac_llvm_context *ctx,
2111 LLVMValueRef rsrc,
2112 LLVMValueRef vdata,
2113 LLVMValueRef voffset,
2114 LLVMValueRef soffset,
2115 bool glc)
2116 {
2117 vdata = LLVMBuildBitCast(ctx->builder, vdata, ctx->i16, "");
2118
2119 if (HAVE_LLVM >= 0x900) {
2120 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
2121 ac_build_llvm8_buffer_store_common(ctx, rsrc, vdata, NULL,
2122 voffset, soffset, 1,
2123 ctx->i16, glc, false,
2124 false, false);
2125 } else {
2126 unsigned dfmt = V_008F0C_BUF_DATA_FORMAT_16;
2127 unsigned nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
2128
2129 vdata = LLVMBuildZExt(ctx->builder, vdata, ctx->i32, "");
2130
2131 ac_build_raw_tbuffer_store(ctx, rsrc, vdata, voffset, soffset,
2132 ctx->i32_0, 1, dfmt, nfmt, glc,
2133 false);
2134 }
2135 }
2136
2137 void
2138 ac_build_tbuffer_store_byte(struct ac_llvm_context *ctx,
2139 LLVMValueRef rsrc,
2140 LLVMValueRef vdata,
2141 LLVMValueRef voffset,
2142 LLVMValueRef soffset,
2143 bool glc)
2144 {
2145 vdata = LLVMBuildBitCast(ctx->builder, vdata, ctx->i8, "");
2146
2147 if (HAVE_LLVM >= 0x900) {
2148 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
2149 ac_build_llvm8_buffer_store_common(ctx, rsrc, vdata, NULL,
2150 voffset, soffset, 1,
2151 ctx->i8, glc, false,
2152 false, false);
2153 } else {
2154 unsigned dfmt = V_008F0C_BUF_DATA_FORMAT_8;
2155 unsigned nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
2156
2157 vdata = LLVMBuildZExt(ctx->builder, vdata, ctx->i32, "");
2158
2159 ac_build_raw_tbuffer_store(ctx, rsrc, vdata, voffset, soffset,
2160 ctx->i32_0, 1, dfmt, nfmt, glc, false);
2161 }
2162 }
2163 /**
2164 * Set range metadata on an instruction. This can only be used on load and
2165 * call instructions. If you know an instruction can only produce the values
2166 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
2167 * \p lo is the minimum value inclusive.
2168 * \p hi is the maximum value exclusive.
2169 */
2170 static void set_range_metadata(struct ac_llvm_context *ctx,
2171 LLVMValueRef value, unsigned lo, unsigned hi)
2172 {
2173 LLVMValueRef range_md, md_args[2];
2174 LLVMTypeRef type = LLVMTypeOf(value);
2175 LLVMContextRef context = LLVMGetTypeContext(type);
2176
2177 md_args[0] = LLVMConstInt(type, lo, false);
2178 md_args[1] = LLVMConstInt(type, hi, false);
2179 range_md = LLVMMDNodeInContext(context, md_args, 2);
2180 LLVMSetMetadata(value, ctx->range_md_kind, range_md);
2181 }
2182
2183 LLVMValueRef
2184 ac_get_thread_id(struct ac_llvm_context *ctx)
2185 {
2186 LLVMValueRef tid;
2187
2188 LLVMValueRef tid_args[2];
2189 tid_args[0] = LLVMConstInt(ctx->i32, 0xffffffff, false);
2190 tid_args[1] = ctx->i32_0;
2191 tid_args[1] = ac_build_intrinsic(ctx,
2192 "llvm.amdgcn.mbcnt.lo", ctx->i32,
2193 tid_args, 2, AC_FUNC_ATTR_READNONE);
2194
2195 tid = ac_build_intrinsic(ctx, "llvm.amdgcn.mbcnt.hi",
2196 ctx->i32, tid_args,
2197 2, AC_FUNC_ATTR_READNONE);
2198 set_range_metadata(ctx, tid, 0, 64);
2199 return tid;
2200 }
2201
2202 /*
2203 * AMD GCN implements derivatives using the local data store (LDS)
2204 * All writes to the LDS happen in all executing threads at
2205 * the same time. TID is the Thread ID for the current
2206 * thread and is a value between 0 and 63, representing
2207 * the thread's position in the wavefront.
2208 *
2209 * For the pixel shader threads are grouped into quads of four pixels.
2210 * The TIDs of the pixels of a quad are:
2211 *
2212 * +------+------+
2213 * |4n + 0|4n + 1|
2214 * +------+------+
2215 * |4n + 2|4n + 3|
2216 * +------+------+
2217 *
2218 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
2219 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
2220 * the current pixel's column, and masking with 0xfffffffe yields the TID
2221 * of the left pixel of the current pixel's row.
2222 *
2223 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
2224 * adding 2 yields the TID of the pixel below the top pixel.
2225 */
2226 LLVMValueRef
2227 ac_build_ddxy(struct ac_llvm_context *ctx,
2228 uint32_t mask,
2229 int idx,
2230 LLVMValueRef val)
2231 {
2232 unsigned tl_lanes[4], trbl_lanes[4];
2233 char name[32], type[8];
2234 LLVMValueRef tl, trbl;
2235 LLVMTypeRef result_type;
2236 LLVMValueRef result;
2237
2238 result_type = ac_to_float_type(ctx, LLVMTypeOf(val));
2239
2240 if (result_type == ctx->f16)
2241 val = LLVMBuildZExt(ctx->builder, val, ctx->i32, "");
2242
2243 for (unsigned i = 0; i < 4; ++i) {
2244 tl_lanes[i] = i & mask;
2245 trbl_lanes[i] = (i & mask) + idx;
2246 }
2247
2248 tl = ac_build_quad_swizzle(ctx, val,
2249 tl_lanes[0], tl_lanes[1],
2250 tl_lanes[2], tl_lanes[3]);
2251 trbl = ac_build_quad_swizzle(ctx, val,
2252 trbl_lanes[0], trbl_lanes[1],
2253 trbl_lanes[2], trbl_lanes[3]);
2254
2255 if (result_type == ctx->f16) {
2256 tl = LLVMBuildTrunc(ctx->builder, tl, ctx->i16, "");
2257 trbl = LLVMBuildTrunc(ctx->builder, trbl, ctx->i16, "");
2258 }
2259
2260 tl = LLVMBuildBitCast(ctx->builder, tl, result_type, "");
2261 trbl = LLVMBuildBitCast(ctx->builder, trbl, result_type, "");
2262 result = LLVMBuildFSub(ctx->builder, trbl, tl, "");
2263
2264 ac_build_type_name_for_intr(result_type, type, sizeof(type));
2265 snprintf(name, sizeof(name), "llvm.amdgcn.wqm.%s", type);
2266
2267 return ac_build_intrinsic(ctx, name, result_type, &result, 1, 0);
2268 }
2269
2270 void
2271 ac_build_sendmsg(struct ac_llvm_context *ctx,
2272 uint32_t msg,
2273 LLVMValueRef wave_id)
2274 {
2275 LLVMValueRef args[2];
2276 args[0] = LLVMConstInt(ctx->i32, msg, false);
2277 args[1] = wave_id;
2278 ac_build_intrinsic(ctx, "llvm.amdgcn.s.sendmsg", ctx->voidt, args, 2, 0);
2279 }
2280
2281 LLVMValueRef
2282 ac_build_imsb(struct ac_llvm_context *ctx,
2283 LLVMValueRef arg,
2284 LLVMTypeRef dst_type)
2285 {
2286 LLVMValueRef msb = ac_build_intrinsic(ctx, "llvm.amdgcn.sffbh.i32",
2287 dst_type, &arg, 1,
2288 AC_FUNC_ATTR_READNONE);
2289
2290 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
2291 * the index from LSB. Invert it by doing "31 - msb". */
2292 msb = LLVMBuildSub(ctx->builder, LLVMConstInt(ctx->i32, 31, false),
2293 msb, "");
2294
2295 LLVMValueRef all_ones = LLVMConstInt(ctx->i32, -1, true);
2296 LLVMValueRef cond = LLVMBuildOr(ctx->builder,
2297 LLVMBuildICmp(ctx->builder, LLVMIntEQ,
2298 arg, ctx->i32_0, ""),
2299 LLVMBuildICmp(ctx->builder, LLVMIntEQ,
2300 arg, all_ones, ""), "");
2301
2302 return LLVMBuildSelect(ctx->builder, cond, all_ones, msb, "");
2303 }
2304
2305 LLVMValueRef
2306 ac_build_umsb(struct ac_llvm_context *ctx,
2307 LLVMValueRef arg,
2308 LLVMTypeRef dst_type)
2309 {
2310 const char *intrin_name;
2311 LLVMTypeRef type;
2312 LLVMValueRef highest_bit;
2313 LLVMValueRef zero;
2314 unsigned bitsize;
2315
2316 bitsize = ac_get_elem_bits(ctx, LLVMTypeOf(arg));
2317 switch (bitsize) {
2318 case 64:
2319 intrin_name = "llvm.ctlz.i64";
2320 type = ctx->i64;
2321 highest_bit = LLVMConstInt(ctx->i64, 63, false);
2322 zero = ctx->i64_0;
2323 break;
2324 case 32:
2325 intrin_name = "llvm.ctlz.i32";
2326 type = ctx->i32;
2327 highest_bit = LLVMConstInt(ctx->i32, 31, false);
2328 zero = ctx->i32_0;
2329 break;
2330 case 16:
2331 intrin_name = "llvm.ctlz.i16";
2332 type = ctx->i16;
2333 highest_bit = LLVMConstInt(ctx->i16, 15, false);
2334 zero = ctx->i16_0;
2335 break;
2336 case 8:
2337 intrin_name = "llvm.ctlz.i8";
2338 type = ctx->i8;
2339 highest_bit = LLVMConstInt(ctx->i8, 7, false);
2340 zero = ctx->i8_0;
2341 break;
2342 default:
2343 unreachable(!"invalid bitsize");
2344 break;
2345 }
2346
2347 LLVMValueRef params[2] = {
2348 arg,
2349 ctx->i1true,
2350 };
2351
2352 LLVMValueRef msb = ac_build_intrinsic(ctx, intrin_name, type,
2353 params, 2,
2354 AC_FUNC_ATTR_READNONE);
2355
2356 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
2357 * the index from LSB. Invert it by doing "31 - msb". */
2358 msb = LLVMBuildSub(ctx->builder, highest_bit, msb, "");
2359
2360 if (bitsize == 64) {
2361 msb = LLVMBuildTrunc(ctx->builder, msb, ctx->i32, "");
2362 } else if (bitsize < 32) {
2363 msb = LLVMBuildSExt(ctx->builder, msb, ctx->i32, "");
2364 }
2365
2366 /* check for zero */
2367 return LLVMBuildSelect(ctx->builder,
2368 LLVMBuildICmp(ctx->builder, LLVMIntEQ, arg, zero, ""),
2369 LLVMConstInt(ctx->i32, -1, true), msb, "");
2370 }
2371
2372 LLVMValueRef ac_build_fmin(struct ac_llvm_context *ctx, LLVMValueRef a,
2373 LLVMValueRef b)
2374 {
2375 char name[64];
2376 snprintf(name, sizeof(name), "llvm.minnum.f%d", ac_get_elem_bits(ctx, LLVMTypeOf(a)));
2377 LLVMValueRef args[2] = {a, b};
2378 return ac_build_intrinsic(ctx, name, LLVMTypeOf(a), args, 2,
2379 AC_FUNC_ATTR_READNONE);
2380 }
2381
2382 LLVMValueRef ac_build_fmax(struct ac_llvm_context *ctx, LLVMValueRef a,
2383 LLVMValueRef b)
2384 {
2385 char name[64];
2386 snprintf(name, sizeof(name), "llvm.maxnum.f%d", ac_get_elem_bits(ctx, LLVMTypeOf(a)));
2387 LLVMValueRef args[2] = {a, b};
2388 return ac_build_intrinsic(ctx, name, LLVMTypeOf(a), args, 2,
2389 AC_FUNC_ATTR_READNONE);
2390 }
2391
2392 LLVMValueRef ac_build_imin(struct ac_llvm_context *ctx, LLVMValueRef a,
2393 LLVMValueRef b)
2394 {
2395 LLVMValueRef cmp = LLVMBuildICmp(ctx->builder, LLVMIntSLE, a, b, "");
2396 return LLVMBuildSelect(ctx->builder, cmp, a, b, "");
2397 }
2398
2399 LLVMValueRef ac_build_imax(struct ac_llvm_context *ctx, LLVMValueRef a,
2400 LLVMValueRef b)
2401 {
2402 LLVMValueRef cmp = LLVMBuildICmp(ctx->builder, LLVMIntSGT, a, b, "");
2403 return LLVMBuildSelect(ctx->builder, cmp, a, b, "");
2404 }
2405
2406 LLVMValueRef ac_build_umin(struct ac_llvm_context *ctx, LLVMValueRef a,
2407 LLVMValueRef b)
2408 {
2409 LLVMValueRef cmp = LLVMBuildICmp(ctx->builder, LLVMIntULE, a, b, "");
2410 return LLVMBuildSelect(ctx->builder, cmp, a, b, "");
2411 }
2412
2413 LLVMValueRef ac_build_umax(struct ac_llvm_context *ctx, LLVMValueRef a,
2414 LLVMValueRef b)
2415 {
2416 LLVMValueRef cmp = LLVMBuildICmp(ctx->builder, LLVMIntUGE, a, b, "");
2417 return LLVMBuildSelect(ctx->builder, cmp, a, b, "");
2418 }
2419
2420 LLVMValueRef ac_build_clamp(struct ac_llvm_context *ctx, LLVMValueRef value)
2421 {
2422 LLVMTypeRef t = LLVMTypeOf(value);
2423 return ac_build_fmin(ctx, ac_build_fmax(ctx, value, LLVMConstReal(t, 0.0)),
2424 LLVMConstReal(t, 1.0));
2425 }
2426
2427 void ac_build_export(struct ac_llvm_context *ctx, struct ac_export_args *a)
2428 {
2429 LLVMValueRef args[9];
2430
2431 args[0] = LLVMConstInt(ctx->i32, a->target, 0);
2432 args[1] = LLVMConstInt(ctx->i32, a->enabled_channels, 0);
2433
2434 if (a->compr) {
2435 LLVMTypeRef i16 = LLVMInt16TypeInContext(ctx->context);
2436 LLVMTypeRef v2i16 = LLVMVectorType(i16, 2);
2437
2438 args[2] = LLVMBuildBitCast(ctx->builder, a->out[0],
2439 v2i16, "");
2440 args[3] = LLVMBuildBitCast(ctx->builder, a->out[1],
2441 v2i16, "");
2442 args[4] = LLVMConstInt(ctx->i1, a->done, 0);
2443 args[5] = LLVMConstInt(ctx->i1, a->valid_mask, 0);
2444
2445 ac_build_intrinsic(ctx, "llvm.amdgcn.exp.compr.v2i16",
2446 ctx->voidt, args, 6, 0);
2447 } else {
2448 args[2] = a->out[0];
2449 args[3] = a->out[1];
2450 args[4] = a->out[2];
2451 args[5] = a->out[3];
2452 args[6] = LLVMConstInt(ctx->i1, a->done, 0);
2453 args[7] = LLVMConstInt(ctx->i1, a->valid_mask, 0);
2454
2455 ac_build_intrinsic(ctx, "llvm.amdgcn.exp.f32",
2456 ctx->voidt, args, 8, 0);
2457 }
2458 }
2459
2460 void ac_build_export_null(struct ac_llvm_context *ctx)
2461 {
2462 struct ac_export_args args;
2463
2464 args.enabled_channels = 0x0; /* enabled channels */
2465 args.valid_mask = 1; /* whether the EXEC mask is valid */
2466 args.done = 1; /* DONE bit */
2467 args.target = V_008DFC_SQ_EXP_NULL;
2468 args.compr = 0; /* COMPR flag (0 = 32-bit export) */
2469 args.out[0] = LLVMGetUndef(ctx->f32); /* R */
2470 args.out[1] = LLVMGetUndef(ctx->f32); /* G */
2471 args.out[2] = LLVMGetUndef(ctx->f32); /* B */
2472 args.out[3] = LLVMGetUndef(ctx->f32); /* A */
2473
2474 ac_build_export(ctx, &args);
2475 }
2476
2477 static unsigned ac_num_coords(enum ac_image_dim dim)
2478 {
2479 switch (dim) {
2480 case ac_image_1d:
2481 return 1;
2482 case ac_image_2d:
2483 case ac_image_1darray:
2484 return 2;
2485 case ac_image_3d:
2486 case ac_image_cube:
2487 case ac_image_2darray:
2488 case ac_image_2dmsaa:
2489 return 3;
2490 case ac_image_2darraymsaa:
2491 return 4;
2492 default:
2493 unreachable("ac_num_coords: bad dim");
2494 }
2495 }
2496
2497 static unsigned ac_num_derivs(enum ac_image_dim dim)
2498 {
2499 switch (dim) {
2500 case ac_image_1d:
2501 case ac_image_1darray:
2502 return 2;
2503 case ac_image_2d:
2504 case ac_image_2darray:
2505 case ac_image_cube:
2506 return 4;
2507 case ac_image_3d:
2508 return 6;
2509 case ac_image_2dmsaa:
2510 case ac_image_2darraymsaa:
2511 default:
2512 unreachable("derivatives not supported");
2513 }
2514 }
2515
2516 static const char *get_atomic_name(enum ac_atomic_op op)
2517 {
2518 switch (op) {
2519 case ac_atomic_swap: return "swap";
2520 case ac_atomic_add: return "add";
2521 case ac_atomic_sub: return "sub";
2522 case ac_atomic_smin: return "smin";
2523 case ac_atomic_umin: return "umin";
2524 case ac_atomic_smax: return "smax";
2525 case ac_atomic_umax: return "umax";
2526 case ac_atomic_and: return "and";
2527 case ac_atomic_or: return "or";
2528 case ac_atomic_xor: return "xor";
2529 }
2530 unreachable("bad atomic op");
2531 }
2532
2533 LLVMValueRef ac_build_image_opcode(struct ac_llvm_context *ctx,
2534 struct ac_image_args *a)
2535 {
2536 const char *overload[3] = { "", "", "" };
2537 unsigned num_overloads = 0;
2538 LLVMValueRef args[18];
2539 unsigned num_args = 0;
2540 enum ac_image_dim dim = a->dim;
2541
2542 assert(!a->lod || a->lod == ctx->i32_0 || a->lod == ctx->f32_0 ||
2543 !a->level_zero);
2544 assert((a->opcode != ac_image_get_resinfo && a->opcode != ac_image_load_mip &&
2545 a->opcode != ac_image_store_mip) ||
2546 a->lod);
2547 assert(a->opcode == ac_image_sample || a->opcode == ac_image_gather4 ||
2548 (!a->compare && !a->offset));
2549 assert((a->opcode == ac_image_sample || a->opcode == ac_image_gather4 ||
2550 a->opcode == ac_image_get_lod) ||
2551 !a->bias);
2552 assert((a->bias ? 1 : 0) +
2553 (a->lod ? 1 : 0) +
2554 (a->level_zero ? 1 : 0) +
2555 (a->derivs[0] ? 1 : 0) <= 1);
2556
2557 if (a->opcode == ac_image_get_lod) {
2558 switch (dim) {
2559 case ac_image_1darray:
2560 dim = ac_image_1d;
2561 break;
2562 case ac_image_2darray:
2563 case ac_image_cube:
2564 dim = ac_image_2d;
2565 break;
2566 default:
2567 break;
2568 }
2569 }
2570
2571 bool sample = a->opcode == ac_image_sample ||
2572 a->opcode == ac_image_gather4 ||
2573 a->opcode == ac_image_get_lod;
2574 bool atomic = a->opcode == ac_image_atomic ||
2575 a->opcode == ac_image_atomic_cmpswap;
2576 LLVMTypeRef coord_type = sample ? ctx->f32 : ctx->i32;
2577
2578 if (atomic || a->opcode == ac_image_store || a->opcode == ac_image_store_mip) {
2579 args[num_args++] = a->data[0];
2580 if (a->opcode == ac_image_atomic_cmpswap)
2581 args[num_args++] = a->data[1];
2582 }
2583
2584 if (!atomic)
2585 args[num_args++] = LLVMConstInt(ctx->i32, a->dmask, false);
2586
2587 if (a->offset)
2588 args[num_args++] = ac_to_integer(ctx, a->offset);
2589 if (a->bias) {
2590 args[num_args++] = ac_to_float(ctx, a->bias);
2591 overload[num_overloads++] = ".f32";
2592 }
2593 if (a->compare)
2594 args[num_args++] = ac_to_float(ctx, a->compare);
2595 if (a->derivs[0]) {
2596 unsigned count = ac_num_derivs(dim);
2597 for (unsigned i = 0; i < count; ++i)
2598 args[num_args++] = ac_to_float(ctx, a->derivs[i]);
2599 overload[num_overloads++] = ".f32";
2600 }
2601 unsigned num_coords =
2602 a->opcode != ac_image_get_resinfo ? ac_num_coords(dim) : 0;
2603 for (unsigned i = 0; i < num_coords; ++i)
2604 args[num_args++] = LLVMBuildBitCast(ctx->builder, a->coords[i], coord_type, "");
2605 if (a->lod)
2606 args[num_args++] = LLVMBuildBitCast(ctx->builder, a->lod, coord_type, "");
2607 overload[num_overloads++] = sample ? ".f32" : ".i32";
2608
2609 args[num_args++] = a->resource;
2610 if (sample) {
2611 args[num_args++] = a->sampler;
2612 args[num_args++] = LLVMConstInt(ctx->i1, a->unorm, false);
2613 }
2614
2615 args[num_args++] = ctx->i32_0; /* texfailctrl */
2616 args[num_args++] = LLVMConstInt(ctx->i32, a->cache_policy, false);
2617
2618 const char *name;
2619 const char *atomic_subop = "";
2620 switch (a->opcode) {
2621 case ac_image_sample: name = "sample"; break;
2622 case ac_image_gather4: name = "gather4"; break;
2623 case ac_image_load: name = "load"; break;
2624 case ac_image_load_mip: name = "load.mip"; break;
2625 case ac_image_store: name = "store"; break;
2626 case ac_image_store_mip: name = "store.mip"; break;
2627 case ac_image_atomic:
2628 name = "atomic.";
2629 atomic_subop = get_atomic_name(a->atomic);
2630 break;
2631 case ac_image_atomic_cmpswap:
2632 name = "atomic.";
2633 atomic_subop = "cmpswap";
2634 break;
2635 case ac_image_get_lod: name = "getlod"; break;
2636 case ac_image_get_resinfo: name = "getresinfo"; break;
2637 default: unreachable("invalid image opcode");
2638 }
2639
2640 const char *dimname;
2641 switch (dim) {
2642 case ac_image_1d: dimname = "1d"; break;
2643 case ac_image_2d: dimname = "2d"; break;
2644 case ac_image_3d: dimname = "3d"; break;
2645 case ac_image_cube: dimname = "cube"; break;
2646 case ac_image_1darray: dimname = "1darray"; break;
2647 case ac_image_2darray: dimname = "2darray"; break;
2648 case ac_image_2dmsaa: dimname = "2dmsaa"; break;
2649 case ac_image_2darraymsaa: dimname = "2darraymsaa"; break;
2650 default: unreachable("invalid dim");
2651 }
2652
2653 bool lod_suffix =
2654 a->lod && (a->opcode == ac_image_sample || a->opcode == ac_image_gather4);
2655 char intr_name[96];
2656 snprintf(intr_name, sizeof(intr_name),
2657 "llvm.amdgcn.image.%s%s" /* base name */
2658 "%s%s%s" /* sample/gather modifiers */
2659 ".%s.%s%s%s%s", /* dimension and type overloads */
2660 name, atomic_subop,
2661 a->compare ? ".c" : "",
2662 a->bias ? ".b" :
2663 lod_suffix ? ".l" :
2664 a->derivs[0] ? ".d" :
2665 a->level_zero ? ".lz" : "",
2666 a->offset ? ".o" : "",
2667 dimname,
2668 atomic ? "i32" : "v4f32",
2669 overload[0], overload[1], overload[2]);
2670
2671 LLVMTypeRef retty;
2672 if (atomic)
2673 retty = ctx->i32;
2674 else if (a->opcode == ac_image_store || a->opcode == ac_image_store_mip)
2675 retty = ctx->voidt;
2676 else
2677 retty = ctx->v4f32;
2678
2679 LLVMValueRef result =
2680 ac_build_intrinsic(ctx, intr_name, retty, args, num_args,
2681 a->attributes);
2682 if (!sample && retty == ctx->v4f32) {
2683 result = LLVMBuildBitCast(ctx->builder, result,
2684 ctx->v4i32, "");
2685 }
2686 return result;
2687 }
2688
2689 LLVMValueRef ac_build_cvt_pkrtz_f16(struct ac_llvm_context *ctx,
2690 LLVMValueRef args[2])
2691 {
2692 LLVMTypeRef v2f16 =
2693 LLVMVectorType(LLVMHalfTypeInContext(ctx->context), 2);
2694
2695 return ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pkrtz", v2f16,
2696 args, 2, AC_FUNC_ATTR_READNONE);
2697 }
2698
2699 LLVMValueRef ac_build_cvt_pknorm_i16(struct ac_llvm_context *ctx,
2700 LLVMValueRef args[2])
2701 {
2702 LLVMValueRef res =
2703 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pknorm.i16",
2704 ctx->v2i16, args, 2,
2705 AC_FUNC_ATTR_READNONE);
2706 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
2707 }
2708
2709 LLVMValueRef ac_build_cvt_pknorm_u16(struct ac_llvm_context *ctx,
2710 LLVMValueRef args[2])
2711 {
2712 LLVMValueRef res =
2713 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pknorm.u16",
2714 ctx->v2i16, args, 2,
2715 AC_FUNC_ATTR_READNONE);
2716 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
2717 }
2718
2719 /* The 8-bit and 10-bit clamping is for HW workarounds. */
2720 LLVMValueRef ac_build_cvt_pk_i16(struct ac_llvm_context *ctx,
2721 LLVMValueRef args[2], unsigned bits, bool hi)
2722 {
2723 assert(bits == 8 || bits == 10 || bits == 16);
2724
2725 LLVMValueRef max_rgb = LLVMConstInt(ctx->i32,
2726 bits == 8 ? 127 : bits == 10 ? 511 : 32767, 0);
2727 LLVMValueRef min_rgb = LLVMConstInt(ctx->i32,
2728 bits == 8 ? -128 : bits == 10 ? -512 : -32768, 0);
2729 LLVMValueRef max_alpha =
2730 bits != 10 ? max_rgb : ctx->i32_1;
2731 LLVMValueRef min_alpha =
2732 bits != 10 ? min_rgb : LLVMConstInt(ctx->i32, -2, 0);
2733
2734 /* Clamp. */
2735 if (bits != 16) {
2736 for (int i = 0; i < 2; i++) {
2737 bool alpha = hi && i == 1;
2738 args[i] = ac_build_imin(ctx, args[i],
2739 alpha ? max_alpha : max_rgb);
2740 args[i] = ac_build_imax(ctx, args[i],
2741 alpha ? min_alpha : min_rgb);
2742 }
2743 }
2744
2745 LLVMValueRef res =
2746 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pk.i16",
2747 ctx->v2i16, args, 2,
2748 AC_FUNC_ATTR_READNONE);
2749 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
2750 }
2751
2752 /* The 8-bit and 10-bit clamping is for HW workarounds. */
2753 LLVMValueRef ac_build_cvt_pk_u16(struct ac_llvm_context *ctx,
2754 LLVMValueRef args[2], unsigned bits, bool hi)
2755 {
2756 assert(bits == 8 || bits == 10 || bits == 16);
2757
2758 LLVMValueRef max_rgb = LLVMConstInt(ctx->i32,
2759 bits == 8 ? 255 : bits == 10 ? 1023 : 65535, 0);
2760 LLVMValueRef max_alpha =
2761 bits != 10 ? max_rgb : LLVMConstInt(ctx->i32, 3, 0);
2762
2763 /* Clamp. */
2764 if (bits != 16) {
2765 for (int i = 0; i < 2; i++) {
2766 bool alpha = hi && i == 1;
2767 args[i] = ac_build_umin(ctx, args[i],
2768 alpha ? max_alpha : max_rgb);
2769 }
2770 }
2771
2772 LLVMValueRef res =
2773 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pk.u16",
2774 ctx->v2i16, args, 2,
2775 AC_FUNC_ATTR_READNONE);
2776 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
2777 }
2778
2779 LLVMValueRef ac_build_wqm_vote(struct ac_llvm_context *ctx, LLVMValueRef i1)
2780 {
2781 return ac_build_intrinsic(ctx, "llvm.amdgcn.wqm.vote", ctx->i1,
2782 &i1, 1, AC_FUNC_ATTR_READNONE);
2783 }
2784
2785 void ac_build_kill_if_false(struct ac_llvm_context *ctx, LLVMValueRef i1)
2786 {
2787 ac_build_intrinsic(ctx, "llvm.amdgcn.kill", ctx->voidt,
2788 &i1, 1, 0);
2789 }
2790
2791 LLVMValueRef ac_build_bfe(struct ac_llvm_context *ctx, LLVMValueRef input,
2792 LLVMValueRef offset, LLVMValueRef width,
2793 bool is_signed)
2794 {
2795 LLVMValueRef args[] = {
2796 input,
2797 offset,
2798 width,
2799 };
2800
2801 LLVMValueRef result = ac_build_intrinsic(ctx,
2802 is_signed ? "llvm.amdgcn.sbfe.i32" :
2803 "llvm.amdgcn.ubfe.i32",
2804 ctx->i32, args, 3,
2805 AC_FUNC_ATTR_READNONE);
2806
2807 if (HAVE_LLVM < 0x0800) {
2808 /* FIXME: LLVM 7+ returns incorrect result when count is 0.
2809 * https://bugs.freedesktop.org/show_bug.cgi?id=107276
2810 */
2811 LLVMValueRef zero = ctx->i32_0;
2812 LLVMValueRef icond = LLVMBuildICmp(ctx->builder, LLVMIntEQ, width, zero, "");
2813 result = LLVMBuildSelect(ctx->builder, icond, zero, result, "");
2814 }
2815
2816 return result;
2817 }
2818
2819 LLVMValueRef ac_build_imad(struct ac_llvm_context *ctx, LLVMValueRef s0,
2820 LLVMValueRef s1, LLVMValueRef s2)
2821 {
2822 return LLVMBuildAdd(ctx->builder,
2823 LLVMBuildMul(ctx->builder, s0, s1, ""), s2, "");
2824 }
2825
2826 LLVMValueRef ac_build_fmad(struct ac_llvm_context *ctx, LLVMValueRef s0,
2827 LLVMValueRef s1, LLVMValueRef s2)
2828 {
2829 return LLVMBuildFAdd(ctx->builder,
2830 LLVMBuildFMul(ctx->builder, s0, s1, ""), s2, "");
2831 }
2832
2833 void ac_build_waitcnt(struct ac_llvm_context *ctx, unsigned simm16)
2834 {
2835 LLVMValueRef args[1] = {
2836 LLVMConstInt(ctx->i32, simm16, false),
2837 };
2838 ac_build_intrinsic(ctx, "llvm.amdgcn.s.waitcnt",
2839 ctx->voidt, args, 1, 0);
2840 }
2841
2842 LLVMValueRef ac_build_fmed3(struct ac_llvm_context *ctx, LLVMValueRef src0,
2843 LLVMValueRef src1, LLVMValueRef src2,
2844 unsigned bitsize)
2845 {
2846 LLVMTypeRef type;
2847 char *intr;
2848
2849 if (bitsize == 16) {
2850 intr = "llvm.amdgcn.fmed3.f16";
2851 type = ctx->f16;
2852 } else if (bitsize == 32) {
2853 intr = "llvm.amdgcn.fmed3.f32";
2854 type = ctx->f32;
2855 } else {
2856 intr = "llvm.amdgcn.fmed3.f64";
2857 type = ctx->f64;
2858 }
2859
2860 LLVMValueRef params[] = {
2861 src0,
2862 src1,
2863 src2,
2864 };
2865 return ac_build_intrinsic(ctx, intr, type, params, 3,
2866 AC_FUNC_ATTR_READNONE);
2867 }
2868
2869 LLVMValueRef ac_build_fract(struct ac_llvm_context *ctx, LLVMValueRef src0,
2870 unsigned bitsize)
2871 {
2872 LLVMTypeRef type;
2873 char *intr;
2874
2875 if (bitsize == 16) {
2876 intr = "llvm.amdgcn.fract.f16";
2877 type = ctx->f16;
2878 } else if (bitsize == 32) {
2879 intr = "llvm.amdgcn.fract.f32";
2880 type = ctx->f32;
2881 } else {
2882 intr = "llvm.amdgcn.fract.f64";
2883 type = ctx->f64;
2884 }
2885
2886 LLVMValueRef params[] = {
2887 src0,
2888 };
2889 return ac_build_intrinsic(ctx, intr, type, params, 1,
2890 AC_FUNC_ATTR_READNONE);
2891 }
2892
2893 LLVMValueRef ac_build_isign(struct ac_llvm_context *ctx, LLVMValueRef src0,
2894 unsigned bitsize)
2895 {
2896 LLVMTypeRef type = LLVMIntTypeInContext(ctx->context, bitsize);
2897 LLVMValueRef zero = LLVMConstInt(type, 0, false);
2898 LLVMValueRef one = LLVMConstInt(type, 1, false);
2899
2900 LLVMValueRef cmp, val;
2901 cmp = LLVMBuildICmp(ctx->builder, LLVMIntSGT, src0, zero, "");
2902 val = LLVMBuildSelect(ctx->builder, cmp, one, src0, "");
2903 cmp = LLVMBuildICmp(ctx->builder, LLVMIntSGE, val, zero, "");
2904 val = LLVMBuildSelect(ctx->builder, cmp, val, LLVMConstInt(type, -1, true), "");
2905 return val;
2906 }
2907
2908 LLVMValueRef ac_build_fsign(struct ac_llvm_context *ctx, LLVMValueRef src0,
2909 unsigned bitsize)
2910 {
2911 LLVMValueRef cmp, val, zero, one;
2912 LLVMTypeRef type;
2913
2914 if (bitsize == 16) {
2915 type = ctx->f16;
2916 zero = ctx->f16_0;
2917 one = ctx->f16_1;
2918 } else if (bitsize == 32) {
2919 type = ctx->f32;
2920 zero = ctx->f32_0;
2921 one = ctx->f32_1;
2922 } else {
2923 type = ctx->f64;
2924 zero = ctx->f64_0;
2925 one = ctx->f64_1;
2926 }
2927
2928 cmp = LLVMBuildFCmp(ctx->builder, LLVMRealOGT, src0, zero, "");
2929 val = LLVMBuildSelect(ctx->builder, cmp, one, src0, "");
2930 cmp = LLVMBuildFCmp(ctx->builder, LLVMRealOGE, val, zero, "");
2931 val = LLVMBuildSelect(ctx->builder, cmp, val, LLVMConstReal(type, -1.0), "");
2932 return val;
2933 }
2934
2935 LLVMValueRef ac_build_bit_count(struct ac_llvm_context *ctx, LLVMValueRef src0)
2936 {
2937 LLVMValueRef result;
2938 unsigned bitsize;
2939
2940 bitsize = ac_get_elem_bits(ctx, LLVMTypeOf(src0));
2941
2942 switch (bitsize) {
2943 case 64:
2944 result = ac_build_intrinsic(ctx, "llvm.ctpop.i64", ctx->i64,
2945 (LLVMValueRef []) { src0 }, 1,
2946 AC_FUNC_ATTR_READNONE);
2947
2948 result = LLVMBuildTrunc(ctx->builder, result, ctx->i32, "");
2949 break;
2950 case 32:
2951 result = ac_build_intrinsic(ctx, "llvm.ctpop.i32", ctx->i32,
2952 (LLVMValueRef []) { src0 }, 1,
2953 AC_FUNC_ATTR_READNONE);
2954 break;
2955 case 16:
2956 result = ac_build_intrinsic(ctx, "llvm.ctpop.i16", ctx->i16,
2957 (LLVMValueRef []) { src0 }, 1,
2958 AC_FUNC_ATTR_READNONE);
2959
2960 result = LLVMBuildZExt(ctx->builder, result, ctx->i32, "");
2961 break;
2962 case 8:
2963 result = ac_build_intrinsic(ctx, "llvm.ctpop.i8", ctx->i8,
2964 (LLVMValueRef []) { src0 }, 1,
2965 AC_FUNC_ATTR_READNONE);
2966
2967 result = LLVMBuildZExt(ctx->builder, result, ctx->i32, "");
2968 break;
2969 default:
2970 unreachable(!"invalid bitsize");
2971 break;
2972 }
2973
2974 return result;
2975 }
2976
2977 LLVMValueRef ac_build_bitfield_reverse(struct ac_llvm_context *ctx,
2978 LLVMValueRef src0)
2979 {
2980 LLVMValueRef result;
2981 unsigned bitsize;
2982
2983 bitsize = ac_get_elem_bits(ctx, LLVMTypeOf(src0));
2984
2985 switch (bitsize) {
2986 case 64:
2987 result = ac_build_intrinsic(ctx, "llvm.bitreverse.i64", ctx->i64,
2988 (LLVMValueRef []) { src0 }, 1,
2989 AC_FUNC_ATTR_READNONE);
2990
2991 result = LLVMBuildTrunc(ctx->builder, result, ctx->i32, "");
2992 break;
2993 case 32:
2994 result = ac_build_intrinsic(ctx, "llvm.bitreverse.i32", ctx->i32,
2995 (LLVMValueRef []) { src0 }, 1,
2996 AC_FUNC_ATTR_READNONE);
2997 break;
2998 case 16:
2999 result = ac_build_intrinsic(ctx, "llvm.bitreverse.i16", ctx->i16,
3000 (LLVMValueRef []) { src0 }, 1,
3001 AC_FUNC_ATTR_READNONE);
3002
3003 result = LLVMBuildZExt(ctx->builder, result, ctx->i32, "");
3004 break;
3005 case 8:
3006 result = ac_build_intrinsic(ctx, "llvm.bitreverse.i8", ctx->i8,
3007 (LLVMValueRef []) { src0 }, 1,
3008 AC_FUNC_ATTR_READNONE);
3009
3010 result = LLVMBuildZExt(ctx->builder, result, ctx->i32, "");
3011 break;
3012 default:
3013 unreachable(!"invalid bitsize");
3014 break;
3015 }
3016
3017 return result;
3018 }
3019
3020 #define AC_EXP_TARGET 0
3021 #define AC_EXP_ENABLED_CHANNELS 1
3022 #define AC_EXP_OUT0 2
3023
3024 enum ac_ir_type {
3025 AC_IR_UNDEF,
3026 AC_IR_CONST,
3027 AC_IR_VALUE,
3028 };
3029
3030 struct ac_vs_exp_chan
3031 {
3032 LLVMValueRef value;
3033 float const_float;
3034 enum ac_ir_type type;
3035 };
3036
3037 struct ac_vs_exp_inst {
3038 unsigned offset;
3039 LLVMValueRef inst;
3040 struct ac_vs_exp_chan chan[4];
3041 };
3042
3043 struct ac_vs_exports {
3044 unsigned num;
3045 struct ac_vs_exp_inst exp[VARYING_SLOT_MAX];
3046 };
3047
3048 /* Return true if the PARAM export has been eliminated. */
3049 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset,
3050 uint32_t num_outputs,
3051 struct ac_vs_exp_inst *exp)
3052 {
3053 unsigned i, default_val; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
3054 bool is_zero[4] = {}, is_one[4] = {};
3055
3056 for (i = 0; i < 4; i++) {
3057 /* It's a constant expression. Undef outputs are eliminated too. */
3058 if (exp->chan[i].type == AC_IR_UNDEF) {
3059 is_zero[i] = true;
3060 is_one[i] = true;
3061 } else if (exp->chan[i].type == AC_IR_CONST) {
3062 if (exp->chan[i].const_float == 0)
3063 is_zero[i] = true;
3064 else if (exp->chan[i].const_float == 1)
3065 is_one[i] = true;
3066 else
3067 return false; /* other constant */
3068 } else
3069 return false;
3070 }
3071
3072 /* Only certain combinations of 0 and 1 can be eliminated. */
3073 if (is_zero[0] && is_zero[1] && is_zero[2])
3074 default_val = is_zero[3] ? 0 : 1;
3075 else if (is_one[0] && is_one[1] && is_one[2])
3076 default_val = is_zero[3] ? 2 : 3;
3077 else
3078 return false;
3079
3080 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
3081 LLVMInstructionEraseFromParent(exp->inst);
3082
3083 /* Change OFFSET to DEFAULT_VAL. */
3084 for (i = 0; i < num_outputs; i++) {
3085 if (vs_output_param_offset[i] == exp->offset) {
3086 vs_output_param_offset[i] =
3087 AC_EXP_PARAM_DEFAULT_VAL_0000 + default_val;
3088 break;
3089 }
3090 }
3091 return true;
3092 }
3093
3094 static bool ac_eliminate_duplicated_output(struct ac_llvm_context *ctx,
3095 uint8_t *vs_output_param_offset,
3096 uint32_t num_outputs,
3097 struct ac_vs_exports *processed,
3098 struct ac_vs_exp_inst *exp)
3099 {
3100 unsigned p, copy_back_channels = 0;
3101
3102 /* See if the output is already in the list of processed outputs.
3103 * The LLVMValueRef comparison relies on SSA.
3104 */
3105 for (p = 0; p < processed->num; p++) {
3106 bool different = false;
3107
3108 for (unsigned j = 0; j < 4; j++) {
3109 struct ac_vs_exp_chan *c1 = &processed->exp[p].chan[j];
3110 struct ac_vs_exp_chan *c2 = &exp->chan[j];
3111
3112 /* Treat undef as a match. */
3113 if (c2->type == AC_IR_UNDEF)
3114 continue;
3115
3116 /* If c1 is undef but c2 isn't, we can copy c2 to c1
3117 * and consider the instruction duplicated.
3118 */
3119 if (c1->type == AC_IR_UNDEF) {
3120 copy_back_channels |= 1 << j;
3121 continue;
3122 }
3123
3124 /* Test whether the channels are not equal. */
3125 if (c1->type != c2->type ||
3126 (c1->type == AC_IR_CONST &&
3127 c1->const_float != c2->const_float) ||
3128 (c1->type == AC_IR_VALUE &&
3129 c1->value != c2->value)) {
3130 different = true;
3131 break;
3132 }
3133 }
3134 if (!different)
3135 break;
3136
3137 copy_back_channels = 0;
3138 }
3139 if (p == processed->num)
3140 return false;
3141
3142 /* If a match was found, but the matching export has undef where the new
3143 * one has a normal value, copy the normal value to the undef channel.
3144 */
3145 struct ac_vs_exp_inst *match = &processed->exp[p];
3146
3147 /* Get current enabled channels mask. */
3148 LLVMValueRef arg = LLVMGetOperand(match->inst, AC_EXP_ENABLED_CHANNELS);
3149 unsigned enabled_channels = LLVMConstIntGetZExtValue(arg);
3150
3151 while (copy_back_channels) {
3152 unsigned chan = u_bit_scan(&copy_back_channels);
3153
3154 assert(match->chan[chan].type == AC_IR_UNDEF);
3155 LLVMSetOperand(match->inst, AC_EXP_OUT0 + chan,
3156 exp->chan[chan].value);
3157 match->chan[chan] = exp->chan[chan];
3158
3159 /* Update number of enabled channels because the original mask
3160 * is not always 0xf.
3161 */
3162 enabled_channels |= (1 << chan);
3163 LLVMSetOperand(match->inst, AC_EXP_ENABLED_CHANNELS,
3164 LLVMConstInt(ctx->i32, enabled_channels, 0));
3165 }
3166
3167 /* The PARAM export is duplicated. Kill it. */
3168 LLVMInstructionEraseFromParent(exp->inst);
3169
3170 /* Change OFFSET to the matching export. */
3171 for (unsigned i = 0; i < num_outputs; i++) {
3172 if (vs_output_param_offset[i] == exp->offset) {
3173 vs_output_param_offset[i] = match->offset;
3174 break;
3175 }
3176 }
3177 return true;
3178 }
3179
3180 void ac_optimize_vs_outputs(struct ac_llvm_context *ctx,
3181 LLVMValueRef main_fn,
3182 uint8_t *vs_output_param_offset,
3183 uint32_t num_outputs,
3184 uint8_t *num_param_exports)
3185 {
3186 LLVMBasicBlockRef bb;
3187 bool removed_any = false;
3188 struct ac_vs_exports exports;
3189
3190 exports.num = 0;
3191
3192 /* Process all LLVM instructions. */
3193 bb = LLVMGetFirstBasicBlock(main_fn);
3194 while (bb) {
3195 LLVMValueRef inst = LLVMGetFirstInstruction(bb);
3196
3197 while (inst) {
3198 LLVMValueRef cur = inst;
3199 inst = LLVMGetNextInstruction(inst);
3200 struct ac_vs_exp_inst exp;
3201
3202 if (LLVMGetInstructionOpcode(cur) != LLVMCall)
3203 continue;
3204
3205 LLVMValueRef callee = ac_llvm_get_called_value(cur);
3206
3207 if (!ac_llvm_is_function(callee))
3208 continue;
3209
3210 const char *name = LLVMGetValueName(callee);
3211 unsigned num_args = LLVMCountParams(callee);
3212
3213 /* Check if this is an export instruction. */
3214 if ((num_args != 9 && num_args != 8) ||
3215 (strcmp(name, "llvm.SI.export") &&
3216 strcmp(name, "llvm.amdgcn.exp.f32")))
3217 continue;
3218
3219 LLVMValueRef arg = LLVMGetOperand(cur, AC_EXP_TARGET);
3220 unsigned target = LLVMConstIntGetZExtValue(arg);
3221
3222 if (target < V_008DFC_SQ_EXP_PARAM)
3223 continue;
3224
3225 target -= V_008DFC_SQ_EXP_PARAM;
3226
3227 /* Parse the instruction. */
3228 memset(&exp, 0, sizeof(exp));
3229 exp.offset = target;
3230 exp.inst = cur;
3231
3232 for (unsigned i = 0; i < 4; i++) {
3233 LLVMValueRef v = LLVMGetOperand(cur, AC_EXP_OUT0 + i);
3234
3235 exp.chan[i].value = v;
3236
3237 if (LLVMIsUndef(v)) {
3238 exp.chan[i].type = AC_IR_UNDEF;
3239 } else if (LLVMIsAConstantFP(v)) {
3240 LLVMBool loses_info;
3241 exp.chan[i].type = AC_IR_CONST;
3242 exp.chan[i].const_float =
3243 LLVMConstRealGetDouble(v, &loses_info);
3244 } else {
3245 exp.chan[i].type = AC_IR_VALUE;
3246 }
3247 }
3248
3249 /* Eliminate constant and duplicated PARAM exports. */
3250 if (ac_eliminate_const_output(vs_output_param_offset,
3251 num_outputs, &exp) ||
3252 ac_eliminate_duplicated_output(ctx,
3253 vs_output_param_offset,
3254 num_outputs, &exports,
3255 &exp)) {
3256 removed_any = true;
3257 } else {
3258 exports.exp[exports.num++] = exp;
3259 }
3260 }
3261 bb = LLVMGetNextBasicBlock(bb);
3262 }
3263
3264 /* Remove holes in export memory due to removed PARAM exports.
3265 * This is done by renumbering all PARAM exports.
3266 */
3267 if (removed_any) {
3268 uint8_t old_offset[VARYING_SLOT_MAX];
3269 unsigned out, i;
3270
3271 /* Make a copy of the offsets. We need the old version while
3272 * we are modifying some of them. */
3273 memcpy(old_offset, vs_output_param_offset,
3274 sizeof(old_offset));
3275
3276 for (i = 0; i < exports.num; i++) {
3277 unsigned offset = exports.exp[i].offset;
3278
3279 /* Update vs_output_param_offset. Multiple outputs can
3280 * have the same offset.
3281 */
3282 for (out = 0; out < num_outputs; out++) {
3283 if (old_offset[out] == offset)
3284 vs_output_param_offset[out] = i;
3285 }
3286
3287 /* Change the PARAM offset in the instruction. */
3288 LLVMSetOperand(exports.exp[i].inst, AC_EXP_TARGET,
3289 LLVMConstInt(ctx->i32,
3290 V_008DFC_SQ_EXP_PARAM + i, 0));
3291 }
3292 *num_param_exports = exports.num;
3293 }
3294 }
3295
3296 void ac_init_exec_full_mask(struct ac_llvm_context *ctx)
3297 {
3298 LLVMValueRef full_mask = LLVMConstInt(ctx->i64, ~0ull, 0);
3299 ac_build_intrinsic(ctx,
3300 "llvm.amdgcn.init.exec", ctx->voidt,
3301 &full_mask, 1, AC_FUNC_ATTR_CONVERGENT);
3302 }
3303
3304 void ac_declare_lds_as_pointer(struct ac_llvm_context *ctx)
3305 {
3306 unsigned lds_size = ctx->chip_class >= GFX7 ? 65536 : 32768;
3307 ctx->lds = LLVMBuildIntToPtr(ctx->builder, ctx->i32_0,
3308 LLVMPointerType(LLVMArrayType(ctx->i32, lds_size / 4), AC_ADDR_SPACE_LDS),
3309 "lds");
3310 }
3311
3312 LLVMValueRef ac_lds_load(struct ac_llvm_context *ctx,
3313 LLVMValueRef dw_addr)
3314 {
3315 return LLVMBuildLoad(ctx->builder, ac_build_gep0(ctx, ctx->lds, dw_addr), "");
3316 }
3317
3318 void ac_lds_store(struct ac_llvm_context *ctx,
3319 LLVMValueRef dw_addr,
3320 LLVMValueRef value)
3321 {
3322 value = ac_to_integer(ctx, value);
3323 ac_build_indexed_store(ctx, ctx->lds,
3324 dw_addr, value);
3325 }
3326
3327 LLVMValueRef ac_find_lsb(struct ac_llvm_context *ctx,
3328 LLVMTypeRef dst_type,
3329 LLVMValueRef src0)
3330 {
3331 unsigned src0_bitsize = ac_get_elem_bits(ctx, LLVMTypeOf(src0));
3332 const char *intrin_name;
3333 LLVMTypeRef type;
3334 LLVMValueRef zero;
3335
3336 switch (src0_bitsize) {
3337 case 64:
3338 intrin_name = "llvm.cttz.i64";
3339 type = ctx->i64;
3340 zero = ctx->i64_0;
3341 break;
3342 case 32:
3343 intrin_name = "llvm.cttz.i32";
3344 type = ctx->i32;
3345 zero = ctx->i32_0;
3346 break;
3347 case 16:
3348 intrin_name = "llvm.cttz.i16";
3349 type = ctx->i16;
3350 zero = ctx->i16_0;
3351 break;
3352 case 8:
3353 intrin_name = "llvm.cttz.i8";
3354 type = ctx->i8;
3355 zero = ctx->i8_0;
3356 break;
3357 default:
3358 unreachable(!"invalid bitsize");
3359 }
3360
3361 LLVMValueRef params[2] = {
3362 src0,
3363
3364 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
3365 * add special code to check for x=0. The reason is that
3366 * the LLVM behavior for x=0 is different from what we
3367 * need here. However, LLVM also assumes that ffs(x) is
3368 * in [0, 31], but GLSL expects that ffs(0) = -1, so
3369 * a conditional assignment to handle 0 is still required.
3370 *
3371 * The hardware already implements the correct behavior.
3372 */
3373 ctx->i1true,
3374 };
3375
3376 LLVMValueRef lsb = ac_build_intrinsic(ctx, intrin_name, type,
3377 params, 2,
3378 AC_FUNC_ATTR_READNONE);
3379
3380 if (src0_bitsize == 64) {
3381 lsb = LLVMBuildTrunc(ctx->builder, lsb, ctx->i32, "");
3382 } else if (src0_bitsize < 32) {
3383 lsb = LLVMBuildSExt(ctx->builder, lsb, ctx->i32, "");
3384 }
3385
3386 /* TODO: We need an intrinsic to skip this conditional. */
3387 /* Check for zero: */
3388 return LLVMBuildSelect(ctx->builder, LLVMBuildICmp(ctx->builder,
3389 LLVMIntEQ, src0,
3390 zero, ""),
3391 LLVMConstInt(ctx->i32, -1, 0), lsb, "");
3392 }
3393
3394 LLVMTypeRef ac_array_in_const_addr_space(LLVMTypeRef elem_type)
3395 {
3396 return LLVMPointerType(elem_type, AC_ADDR_SPACE_CONST);
3397 }
3398
3399 LLVMTypeRef ac_array_in_const32_addr_space(LLVMTypeRef elem_type)
3400 {
3401 return LLVMPointerType(elem_type, AC_ADDR_SPACE_CONST_32BIT);
3402 }
3403
3404 static struct ac_llvm_flow *
3405 get_current_flow(struct ac_llvm_context *ctx)
3406 {
3407 if (ctx->flow_depth > 0)
3408 return &ctx->flow[ctx->flow_depth - 1];
3409 return NULL;
3410 }
3411
3412 static struct ac_llvm_flow *
3413 get_innermost_loop(struct ac_llvm_context *ctx)
3414 {
3415 for (unsigned i = ctx->flow_depth; i > 0; --i) {
3416 if (ctx->flow[i - 1].loop_entry_block)
3417 return &ctx->flow[i - 1];
3418 }
3419 return NULL;
3420 }
3421
3422 static struct ac_llvm_flow *
3423 push_flow(struct ac_llvm_context *ctx)
3424 {
3425 struct ac_llvm_flow *flow;
3426
3427 if (ctx->flow_depth >= ctx->flow_depth_max) {
3428 unsigned new_max = MAX2(ctx->flow_depth << 1,
3429 AC_LLVM_INITIAL_CF_DEPTH);
3430
3431 ctx->flow = realloc(ctx->flow, new_max * sizeof(*ctx->flow));
3432 ctx->flow_depth_max = new_max;
3433 }
3434
3435 flow = &ctx->flow[ctx->flow_depth];
3436 ctx->flow_depth++;
3437
3438 flow->next_block = NULL;
3439 flow->loop_entry_block = NULL;
3440 return flow;
3441 }
3442
3443 static void set_basicblock_name(LLVMBasicBlockRef bb, const char *base,
3444 int label_id)
3445 {
3446 char buf[32];
3447 snprintf(buf, sizeof(buf), "%s%d", base, label_id);
3448 LLVMSetValueName(LLVMBasicBlockAsValue(bb), buf);
3449 }
3450
3451 /* Append a basic block at the level of the parent flow.
3452 */
3453 static LLVMBasicBlockRef append_basic_block(struct ac_llvm_context *ctx,
3454 const char *name)
3455 {
3456 assert(ctx->flow_depth >= 1);
3457
3458 if (ctx->flow_depth >= 2) {
3459 struct ac_llvm_flow *flow = &ctx->flow[ctx->flow_depth - 2];
3460
3461 return LLVMInsertBasicBlockInContext(ctx->context,
3462 flow->next_block, name);
3463 }
3464
3465 LLVMValueRef main_fn =
3466 LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx->builder));
3467 return LLVMAppendBasicBlockInContext(ctx->context, main_fn, name);
3468 }
3469
3470 /* Emit a branch to the given default target for the current block if
3471 * applicable -- that is, if the current block does not already contain a
3472 * branch from a break or continue.
3473 */
3474 static void emit_default_branch(LLVMBuilderRef builder,
3475 LLVMBasicBlockRef target)
3476 {
3477 if (!LLVMGetBasicBlockTerminator(LLVMGetInsertBlock(builder)))
3478 LLVMBuildBr(builder, target);
3479 }
3480
3481 void ac_build_bgnloop(struct ac_llvm_context *ctx, int label_id)
3482 {
3483 struct ac_llvm_flow *flow = push_flow(ctx);
3484 flow->loop_entry_block = append_basic_block(ctx, "LOOP");
3485 flow->next_block = append_basic_block(ctx, "ENDLOOP");
3486 set_basicblock_name(flow->loop_entry_block, "loop", label_id);
3487 LLVMBuildBr(ctx->builder, flow->loop_entry_block);
3488 LLVMPositionBuilderAtEnd(ctx->builder, flow->loop_entry_block);
3489 }
3490
3491 void ac_build_break(struct ac_llvm_context *ctx)
3492 {
3493 struct ac_llvm_flow *flow = get_innermost_loop(ctx);
3494 LLVMBuildBr(ctx->builder, flow->next_block);
3495 }
3496
3497 void ac_build_continue(struct ac_llvm_context *ctx)
3498 {
3499 struct ac_llvm_flow *flow = get_innermost_loop(ctx);
3500 LLVMBuildBr(ctx->builder, flow->loop_entry_block);
3501 }
3502
3503 void ac_build_else(struct ac_llvm_context *ctx, int label_id)
3504 {
3505 struct ac_llvm_flow *current_branch = get_current_flow(ctx);
3506 LLVMBasicBlockRef endif_block;
3507
3508 assert(!current_branch->loop_entry_block);
3509
3510 endif_block = append_basic_block(ctx, "ENDIF");
3511 emit_default_branch(ctx->builder, endif_block);
3512
3513 LLVMPositionBuilderAtEnd(ctx->builder, current_branch->next_block);
3514 set_basicblock_name(current_branch->next_block, "else", label_id);
3515
3516 current_branch->next_block = endif_block;
3517 }
3518
3519 void ac_build_endif(struct ac_llvm_context *ctx, int label_id)
3520 {
3521 struct ac_llvm_flow *current_branch = get_current_flow(ctx);
3522
3523 assert(!current_branch->loop_entry_block);
3524
3525 emit_default_branch(ctx->builder, current_branch->next_block);
3526 LLVMPositionBuilderAtEnd(ctx->builder, current_branch->next_block);
3527 set_basicblock_name(current_branch->next_block, "endif", label_id);
3528
3529 ctx->flow_depth--;
3530 }
3531
3532 void ac_build_endloop(struct ac_llvm_context *ctx, int label_id)
3533 {
3534 struct ac_llvm_flow *current_loop = get_current_flow(ctx);
3535
3536 assert(current_loop->loop_entry_block);
3537
3538 emit_default_branch(ctx->builder, current_loop->loop_entry_block);
3539
3540 LLVMPositionBuilderAtEnd(ctx->builder, current_loop->next_block);
3541 set_basicblock_name(current_loop->next_block, "endloop", label_id);
3542 ctx->flow_depth--;
3543 }
3544
3545 void ac_build_ifcc(struct ac_llvm_context *ctx, LLVMValueRef cond, int label_id)
3546 {
3547 struct ac_llvm_flow *flow = push_flow(ctx);
3548 LLVMBasicBlockRef if_block;
3549
3550 if_block = append_basic_block(ctx, "IF");
3551 flow->next_block = append_basic_block(ctx, "ELSE");
3552 set_basicblock_name(if_block, "if", label_id);
3553 LLVMBuildCondBr(ctx->builder, cond, if_block, flow->next_block);
3554 LLVMPositionBuilderAtEnd(ctx->builder, if_block);
3555 }
3556
3557 void ac_build_if(struct ac_llvm_context *ctx, LLVMValueRef value,
3558 int label_id)
3559 {
3560 LLVMValueRef cond = LLVMBuildFCmp(ctx->builder, LLVMRealUNE,
3561 value, ctx->f32_0, "");
3562 ac_build_ifcc(ctx, cond, label_id);
3563 }
3564
3565 void ac_build_uif(struct ac_llvm_context *ctx, LLVMValueRef value,
3566 int label_id)
3567 {
3568 LLVMValueRef cond = LLVMBuildICmp(ctx->builder, LLVMIntNE,
3569 ac_to_integer(ctx, value),
3570 ctx->i32_0, "");
3571 ac_build_ifcc(ctx, cond, label_id);
3572 }
3573
3574 LLVMValueRef ac_build_alloca_undef(struct ac_llvm_context *ac, LLVMTypeRef type,
3575 const char *name)
3576 {
3577 LLVMBuilderRef builder = ac->builder;
3578 LLVMBasicBlockRef current_block = LLVMGetInsertBlock(builder);
3579 LLVMValueRef function = LLVMGetBasicBlockParent(current_block);
3580 LLVMBasicBlockRef first_block = LLVMGetEntryBasicBlock(function);
3581 LLVMValueRef first_instr = LLVMGetFirstInstruction(first_block);
3582 LLVMBuilderRef first_builder = LLVMCreateBuilderInContext(ac->context);
3583 LLVMValueRef res;
3584
3585 if (first_instr) {
3586 LLVMPositionBuilderBefore(first_builder, first_instr);
3587 } else {
3588 LLVMPositionBuilderAtEnd(first_builder, first_block);
3589 }
3590
3591 res = LLVMBuildAlloca(first_builder, type, name);
3592 LLVMDisposeBuilder(first_builder);
3593 return res;
3594 }
3595
3596 LLVMValueRef ac_build_alloca(struct ac_llvm_context *ac,
3597 LLVMTypeRef type, const char *name)
3598 {
3599 LLVMValueRef ptr = ac_build_alloca_undef(ac, type, name);
3600 LLVMBuildStore(ac->builder, LLVMConstNull(type), ptr);
3601 return ptr;
3602 }
3603
3604 LLVMValueRef ac_cast_ptr(struct ac_llvm_context *ctx, LLVMValueRef ptr,
3605 LLVMTypeRef type)
3606 {
3607 int addr_space = LLVMGetPointerAddressSpace(LLVMTypeOf(ptr));
3608 return LLVMBuildBitCast(ctx->builder, ptr,
3609 LLVMPointerType(type, addr_space), "");
3610 }
3611
3612 LLVMValueRef ac_trim_vector(struct ac_llvm_context *ctx, LLVMValueRef value,
3613 unsigned count)
3614 {
3615 unsigned num_components = ac_get_llvm_num_components(value);
3616 if (count == num_components)
3617 return value;
3618
3619 LLVMValueRef masks[MAX2(count, 2)];
3620 masks[0] = ctx->i32_0;
3621 masks[1] = ctx->i32_1;
3622 for (unsigned i = 2; i < count; i++)
3623 masks[i] = LLVMConstInt(ctx->i32, i, false);
3624
3625 if (count == 1)
3626 return LLVMBuildExtractElement(ctx->builder, value, masks[0],
3627 "");
3628
3629 LLVMValueRef swizzle = LLVMConstVector(masks, count);
3630 return LLVMBuildShuffleVector(ctx->builder, value, value, swizzle, "");
3631 }
3632
3633 LLVMValueRef ac_unpack_param(struct ac_llvm_context *ctx, LLVMValueRef param,
3634 unsigned rshift, unsigned bitwidth)
3635 {
3636 LLVMValueRef value = param;
3637 if (rshift)
3638 value = LLVMBuildLShr(ctx->builder, value,
3639 LLVMConstInt(ctx->i32, rshift, false), "");
3640
3641 if (rshift + bitwidth < 32) {
3642 unsigned mask = (1 << bitwidth) - 1;
3643 value = LLVMBuildAnd(ctx->builder, value,
3644 LLVMConstInt(ctx->i32, mask, false), "");
3645 }
3646 return value;
3647 }
3648
3649 /* Adjust the sample index according to FMASK.
3650 *
3651 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3652 * which is the identity mapping. Each nibble says which physical sample
3653 * should be fetched to get that sample.
3654 *
3655 * For example, 0x11111100 means there are only 2 samples stored and
3656 * the second sample covers 3/4 of the pixel. When reading samples 0
3657 * and 1, return physical sample 0 (determined by the first two 0s
3658 * in FMASK), otherwise return physical sample 1.
3659 *
3660 * The sample index should be adjusted as follows:
3661 * addr[sample_index] = (fmask >> (addr[sample_index] * 4)) & 0xF;
3662 */
3663 void ac_apply_fmask_to_sample(struct ac_llvm_context *ac, LLVMValueRef fmask,
3664 LLVMValueRef *addr, bool is_array_tex)
3665 {
3666 struct ac_image_args fmask_load = {};
3667 fmask_load.opcode = ac_image_load;
3668 fmask_load.resource = fmask;
3669 fmask_load.dmask = 0xf;
3670 fmask_load.dim = is_array_tex ? ac_image_2darray : ac_image_2d;
3671 fmask_load.attributes = AC_FUNC_ATTR_READNONE;
3672
3673 fmask_load.coords[0] = addr[0];
3674 fmask_load.coords[1] = addr[1];
3675 if (is_array_tex)
3676 fmask_load.coords[2] = addr[2];
3677
3678 LLVMValueRef fmask_value = ac_build_image_opcode(ac, &fmask_load);
3679 fmask_value = LLVMBuildExtractElement(ac->builder, fmask_value,
3680 ac->i32_0, "");
3681
3682 /* Apply the formula. */
3683 unsigned sample_chan = is_array_tex ? 3 : 2;
3684 LLVMValueRef final_sample;
3685 final_sample = LLVMBuildMul(ac->builder, addr[sample_chan],
3686 LLVMConstInt(ac->i32, 4, 0), "");
3687 final_sample = LLVMBuildLShr(ac->builder, fmask_value, final_sample, "");
3688 /* Mask the sample index by 0x7, because 0x8 means an unknown value
3689 * with EQAA, so those will map to 0. */
3690 final_sample = LLVMBuildAnd(ac->builder, final_sample,
3691 LLVMConstInt(ac->i32, 0x7, 0), "");
3692
3693 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3694 * resource descriptor is 0 (invalid).
3695 */
3696 LLVMValueRef tmp;
3697 tmp = LLVMBuildBitCast(ac->builder, fmask, ac->v8i32, "");
3698 tmp = LLVMBuildExtractElement(ac->builder, tmp, ac->i32_1, "");
3699 tmp = LLVMBuildICmp(ac->builder, LLVMIntNE, tmp, ac->i32_0, "");
3700
3701 /* Replace the MSAA sample index. */
3702 addr[sample_chan] = LLVMBuildSelect(ac->builder, tmp, final_sample,
3703 addr[sample_chan], "");
3704 }
3705
3706 static LLVMValueRef
3707 _ac_build_readlane(struct ac_llvm_context *ctx, LLVMValueRef src, LLVMValueRef lane)
3708 {
3709 ac_build_optimization_barrier(ctx, &src);
3710 return ac_build_intrinsic(ctx,
3711 lane == NULL ? "llvm.amdgcn.readfirstlane" : "llvm.amdgcn.readlane",
3712 LLVMTypeOf(src), (LLVMValueRef []) {
3713 src, lane },
3714 lane == NULL ? 1 : 2,
3715 AC_FUNC_ATTR_READNONE |
3716 AC_FUNC_ATTR_CONVERGENT);
3717 }
3718
3719 /**
3720 * Builds the "llvm.amdgcn.readlane" or "llvm.amdgcn.readfirstlane" intrinsic.
3721 * @param ctx
3722 * @param src
3723 * @param lane - id of the lane or NULL for the first active lane
3724 * @return value of the lane
3725 */
3726 LLVMValueRef
3727 ac_build_readlane(struct ac_llvm_context *ctx, LLVMValueRef src, LLVMValueRef lane)
3728 {
3729 LLVMTypeRef src_type = LLVMTypeOf(src);
3730 src = ac_to_integer(ctx, src);
3731 unsigned bits = LLVMGetIntTypeWidth(LLVMTypeOf(src));
3732 LLVMValueRef ret;
3733
3734 if (bits == 32) {
3735 ret = _ac_build_readlane(ctx, src, lane);
3736 } else {
3737 assert(bits % 32 == 0);
3738 LLVMTypeRef vec_type = LLVMVectorType(ctx->i32, bits / 32);
3739 LLVMValueRef src_vector =
3740 LLVMBuildBitCast(ctx->builder, src, vec_type, "");
3741 ret = LLVMGetUndef(vec_type);
3742 for (unsigned i = 0; i < bits / 32; i++) {
3743 src = LLVMBuildExtractElement(ctx->builder, src_vector,
3744 LLVMConstInt(ctx->i32, i, 0), "");
3745 LLVMValueRef ret_comp = _ac_build_readlane(ctx, src, lane);
3746 ret = LLVMBuildInsertElement(ctx->builder, ret, ret_comp,
3747 LLVMConstInt(ctx->i32, i, 0), "");
3748 }
3749 }
3750 return LLVMBuildBitCast(ctx->builder, ret, src_type, "");
3751 }
3752
3753 LLVMValueRef
3754 ac_build_writelane(struct ac_llvm_context *ctx, LLVMValueRef src, LLVMValueRef value, LLVMValueRef lane)
3755 {
3756 /* TODO: Use the actual instruction when LLVM adds an intrinsic for it.
3757 */
3758 LLVMValueRef pred = LLVMBuildICmp(ctx->builder, LLVMIntEQ, lane,
3759 ac_get_thread_id(ctx), "");
3760 return LLVMBuildSelect(ctx->builder, pred, value, src, "");
3761 }
3762
3763 LLVMValueRef
3764 ac_build_mbcnt(struct ac_llvm_context *ctx, LLVMValueRef mask)
3765 {
3766 LLVMValueRef mask_vec = LLVMBuildBitCast(ctx->builder, mask,
3767 LLVMVectorType(ctx->i32, 2),
3768 "");
3769 LLVMValueRef mask_lo = LLVMBuildExtractElement(ctx->builder, mask_vec,
3770 ctx->i32_0, "");
3771 LLVMValueRef mask_hi = LLVMBuildExtractElement(ctx->builder, mask_vec,
3772 ctx->i32_1, "");
3773 LLVMValueRef val =
3774 ac_build_intrinsic(ctx, "llvm.amdgcn.mbcnt.lo", ctx->i32,
3775 (LLVMValueRef []) { mask_lo, ctx->i32_0 },
3776 2, AC_FUNC_ATTR_READNONE);
3777 val = ac_build_intrinsic(ctx, "llvm.amdgcn.mbcnt.hi", ctx->i32,
3778 (LLVMValueRef []) { mask_hi, val },
3779 2, AC_FUNC_ATTR_READNONE);
3780 return val;
3781 }
3782
3783 enum dpp_ctrl {
3784 _dpp_quad_perm = 0x000,
3785 _dpp_row_sl = 0x100,
3786 _dpp_row_sr = 0x110,
3787 _dpp_row_rr = 0x120,
3788 dpp_wf_sl1 = 0x130,
3789 dpp_wf_rl1 = 0x134,
3790 dpp_wf_sr1 = 0x138,
3791 dpp_wf_rr1 = 0x13C,
3792 dpp_row_mirror = 0x140,
3793 dpp_row_half_mirror = 0x141,
3794 dpp_row_bcast15 = 0x142,
3795 dpp_row_bcast31 = 0x143
3796 };
3797
3798 static inline enum dpp_ctrl
3799 dpp_quad_perm(unsigned lane0, unsigned lane1, unsigned lane2, unsigned lane3)
3800 {
3801 assert(lane0 < 4 && lane1 < 4 && lane2 < 4 && lane3 < 4);
3802 return _dpp_quad_perm | lane0 | (lane1 << 2) | (lane2 << 4) | (lane3 << 6);
3803 }
3804
3805 static inline enum dpp_ctrl
3806 dpp_row_sl(unsigned amount)
3807 {
3808 assert(amount > 0 && amount < 16);
3809 return _dpp_row_sl | amount;
3810 }
3811
3812 static inline enum dpp_ctrl
3813 dpp_row_sr(unsigned amount)
3814 {
3815 assert(amount > 0 && amount < 16);
3816 return _dpp_row_sr | amount;
3817 }
3818
3819 static LLVMValueRef
3820 _ac_build_dpp(struct ac_llvm_context *ctx, LLVMValueRef old, LLVMValueRef src,
3821 enum dpp_ctrl dpp_ctrl, unsigned row_mask, unsigned bank_mask,
3822 bool bound_ctrl)
3823 {
3824 return ac_build_intrinsic(ctx, "llvm.amdgcn.update.dpp.i32",
3825 LLVMTypeOf(old),
3826 (LLVMValueRef[]) {
3827 old, src,
3828 LLVMConstInt(ctx->i32, dpp_ctrl, 0),
3829 LLVMConstInt(ctx->i32, row_mask, 0),
3830 LLVMConstInt(ctx->i32, bank_mask, 0),
3831 LLVMConstInt(ctx->i1, bound_ctrl, 0) },
3832 6, AC_FUNC_ATTR_READNONE | AC_FUNC_ATTR_CONVERGENT);
3833 }
3834
3835 static LLVMValueRef
3836 ac_build_dpp(struct ac_llvm_context *ctx, LLVMValueRef old, LLVMValueRef src,
3837 enum dpp_ctrl dpp_ctrl, unsigned row_mask, unsigned bank_mask,
3838 bool bound_ctrl)
3839 {
3840 LLVMTypeRef src_type = LLVMTypeOf(src);
3841 src = ac_to_integer(ctx, src);
3842 old = ac_to_integer(ctx, old);
3843 unsigned bits = LLVMGetIntTypeWidth(LLVMTypeOf(src));
3844 LLVMValueRef ret;
3845 if (bits == 32) {
3846 ret = _ac_build_dpp(ctx, old, src, dpp_ctrl, row_mask,
3847 bank_mask, bound_ctrl);
3848 } else {
3849 assert(bits % 32 == 0);
3850 LLVMTypeRef vec_type = LLVMVectorType(ctx->i32, bits / 32);
3851 LLVMValueRef src_vector =
3852 LLVMBuildBitCast(ctx->builder, src, vec_type, "");
3853 LLVMValueRef old_vector =
3854 LLVMBuildBitCast(ctx->builder, old, vec_type, "");
3855 ret = LLVMGetUndef(vec_type);
3856 for (unsigned i = 0; i < bits / 32; i++) {
3857 src = LLVMBuildExtractElement(ctx->builder, src_vector,
3858 LLVMConstInt(ctx->i32, i,
3859 0), "");
3860 old = LLVMBuildExtractElement(ctx->builder, old_vector,
3861 LLVMConstInt(ctx->i32, i,
3862 0), "");
3863 LLVMValueRef ret_comp = _ac_build_dpp(ctx, old, src,
3864 dpp_ctrl,
3865 row_mask,
3866 bank_mask,
3867 bound_ctrl);
3868 ret = LLVMBuildInsertElement(ctx->builder, ret,
3869 ret_comp,
3870 LLVMConstInt(ctx->i32, i,
3871 0), "");
3872 }
3873 }
3874 return LLVMBuildBitCast(ctx->builder, ret, src_type, "");
3875 }
3876
3877 static inline unsigned
3878 ds_pattern_bitmode(unsigned and_mask, unsigned or_mask, unsigned xor_mask)
3879 {
3880 assert(and_mask < 32 && or_mask < 32 && xor_mask < 32);
3881 return and_mask | (or_mask << 5) | (xor_mask << 10);
3882 }
3883
3884 static LLVMValueRef
3885 _ac_build_ds_swizzle(struct ac_llvm_context *ctx, LLVMValueRef src, unsigned mask)
3886 {
3887 return ac_build_intrinsic(ctx, "llvm.amdgcn.ds.swizzle",
3888 LLVMTypeOf(src), (LLVMValueRef []) {
3889 src, LLVMConstInt(ctx->i32, mask, 0) },
3890 2, AC_FUNC_ATTR_READNONE | AC_FUNC_ATTR_CONVERGENT);
3891 }
3892
3893 LLVMValueRef
3894 ac_build_ds_swizzle(struct ac_llvm_context *ctx, LLVMValueRef src, unsigned mask)
3895 {
3896 LLVMTypeRef src_type = LLVMTypeOf(src);
3897 src = ac_to_integer(ctx, src);
3898 unsigned bits = LLVMGetIntTypeWidth(LLVMTypeOf(src));
3899 LLVMValueRef ret;
3900 if (bits == 32) {
3901 ret = _ac_build_ds_swizzle(ctx, src, mask);
3902 } else {
3903 assert(bits % 32 == 0);
3904 LLVMTypeRef vec_type = LLVMVectorType(ctx->i32, bits / 32);
3905 LLVMValueRef src_vector =
3906 LLVMBuildBitCast(ctx->builder, src, vec_type, "");
3907 ret = LLVMGetUndef(vec_type);
3908 for (unsigned i = 0; i < bits / 32; i++) {
3909 src = LLVMBuildExtractElement(ctx->builder, src_vector,
3910 LLVMConstInt(ctx->i32, i,
3911 0), "");
3912 LLVMValueRef ret_comp = _ac_build_ds_swizzle(ctx, src,
3913 mask);
3914 ret = LLVMBuildInsertElement(ctx->builder, ret,
3915 ret_comp,
3916 LLVMConstInt(ctx->i32, i,
3917 0), "");
3918 }
3919 }
3920 return LLVMBuildBitCast(ctx->builder, ret, src_type, "");
3921 }
3922
3923 static LLVMValueRef
3924 ac_build_wwm(struct ac_llvm_context *ctx, LLVMValueRef src)
3925 {
3926 char name[32], type[8];
3927 ac_build_type_name_for_intr(LLVMTypeOf(src), type, sizeof(type));
3928 snprintf(name, sizeof(name), "llvm.amdgcn.wwm.%s", type);
3929 return ac_build_intrinsic(ctx, name, LLVMTypeOf(src),
3930 (LLVMValueRef []) { src }, 1,
3931 AC_FUNC_ATTR_READNONE);
3932 }
3933
3934 static LLVMValueRef
3935 ac_build_set_inactive(struct ac_llvm_context *ctx, LLVMValueRef src,
3936 LLVMValueRef inactive)
3937 {
3938 char name[33], type[8];
3939 LLVMTypeRef src_type = LLVMTypeOf(src);
3940 src = ac_to_integer(ctx, src);
3941 inactive = ac_to_integer(ctx, inactive);
3942 ac_build_type_name_for_intr(LLVMTypeOf(src), type, sizeof(type));
3943 snprintf(name, sizeof(name), "llvm.amdgcn.set.inactive.%s", type);
3944 LLVMValueRef ret =
3945 ac_build_intrinsic(ctx, name,
3946 LLVMTypeOf(src), (LLVMValueRef []) {
3947 src, inactive }, 2,
3948 AC_FUNC_ATTR_READNONE |
3949 AC_FUNC_ATTR_CONVERGENT);
3950 return LLVMBuildBitCast(ctx->builder, ret, src_type, "");
3951 }
3952
3953 static LLVMValueRef
3954 get_reduction_identity(struct ac_llvm_context *ctx, nir_op op, unsigned type_size)
3955 {
3956 if (type_size == 4) {
3957 switch (op) {
3958 case nir_op_iadd: return ctx->i32_0;
3959 case nir_op_fadd: return ctx->f32_0;
3960 case nir_op_imul: return ctx->i32_1;
3961 case nir_op_fmul: return ctx->f32_1;
3962 case nir_op_imin: return LLVMConstInt(ctx->i32, INT32_MAX, 0);
3963 case nir_op_umin: return LLVMConstInt(ctx->i32, UINT32_MAX, 0);
3964 case nir_op_fmin: return LLVMConstReal(ctx->f32, INFINITY);
3965 case nir_op_imax: return LLVMConstInt(ctx->i32, INT32_MIN, 0);
3966 case nir_op_umax: return ctx->i32_0;
3967 case nir_op_fmax: return LLVMConstReal(ctx->f32, -INFINITY);
3968 case nir_op_iand: return LLVMConstInt(ctx->i32, -1, 0);
3969 case nir_op_ior: return ctx->i32_0;
3970 case nir_op_ixor: return ctx->i32_0;
3971 default:
3972 unreachable("bad reduction intrinsic");
3973 }
3974 } else { /* type_size == 64bit */
3975 switch (op) {
3976 case nir_op_iadd: return ctx->i64_0;
3977 case nir_op_fadd: return ctx->f64_0;
3978 case nir_op_imul: return ctx->i64_1;
3979 case nir_op_fmul: return ctx->f64_1;
3980 case nir_op_imin: return LLVMConstInt(ctx->i64, INT64_MAX, 0);
3981 case nir_op_umin: return LLVMConstInt(ctx->i64, UINT64_MAX, 0);
3982 case nir_op_fmin: return LLVMConstReal(ctx->f64, INFINITY);
3983 case nir_op_imax: return LLVMConstInt(ctx->i64, INT64_MIN, 0);
3984 case nir_op_umax: return ctx->i64_0;
3985 case nir_op_fmax: return LLVMConstReal(ctx->f64, -INFINITY);
3986 case nir_op_iand: return LLVMConstInt(ctx->i64, -1, 0);
3987 case nir_op_ior: return ctx->i64_0;
3988 case nir_op_ixor: return ctx->i64_0;
3989 default:
3990 unreachable("bad reduction intrinsic");
3991 }
3992 }
3993 }
3994
3995 static LLVMValueRef
3996 ac_build_alu_op(struct ac_llvm_context *ctx, LLVMValueRef lhs, LLVMValueRef rhs, nir_op op)
3997 {
3998 bool _64bit = ac_get_type_size(LLVMTypeOf(lhs)) == 8;
3999 switch (op) {
4000 case nir_op_iadd: return LLVMBuildAdd(ctx->builder, lhs, rhs, "");
4001 case nir_op_fadd: return LLVMBuildFAdd(ctx->builder, lhs, rhs, "");
4002 case nir_op_imul: return LLVMBuildMul(ctx->builder, lhs, rhs, "");
4003 case nir_op_fmul: return LLVMBuildFMul(ctx->builder, lhs, rhs, "");
4004 case nir_op_imin: return LLVMBuildSelect(ctx->builder,
4005 LLVMBuildICmp(ctx->builder, LLVMIntSLT, lhs, rhs, ""),
4006 lhs, rhs, "");
4007 case nir_op_umin: return LLVMBuildSelect(ctx->builder,
4008 LLVMBuildICmp(ctx->builder, LLVMIntULT, lhs, rhs, ""),
4009 lhs, rhs, "");
4010 case nir_op_fmin: return ac_build_intrinsic(ctx,
4011 _64bit ? "llvm.minnum.f64" : "llvm.minnum.f32",
4012 _64bit ? ctx->f64 : ctx->f32,
4013 (LLVMValueRef[]){lhs, rhs}, 2, AC_FUNC_ATTR_READNONE);
4014 case nir_op_imax: return LLVMBuildSelect(ctx->builder,
4015 LLVMBuildICmp(ctx->builder, LLVMIntSGT, lhs, rhs, ""),
4016 lhs, rhs, "");
4017 case nir_op_umax: return LLVMBuildSelect(ctx->builder,
4018 LLVMBuildICmp(ctx->builder, LLVMIntUGT, lhs, rhs, ""),
4019 lhs, rhs, "");
4020 case nir_op_fmax: return ac_build_intrinsic(ctx,
4021 _64bit ? "llvm.maxnum.f64" : "llvm.maxnum.f32",
4022 _64bit ? ctx->f64 : ctx->f32,
4023 (LLVMValueRef[]){lhs, rhs}, 2, AC_FUNC_ATTR_READNONE);
4024 case nir_op_iand: return LLVMBuildAnd(ctx->builder, lhs, rhs, "");
4025 case nir_op_ior: return LLVMBuildOr(ctx->builder, lhs, rhs, "");
4026 case nir_op_ixor: return LLVMBuildXor(ctx->builder, lhs, rhs, "");
4027 default:
4028 unreachable("bad reduction intrinsic");
4029 }
4030 }
4031
4032 /**
4033 * \param maxprefix specifies that the result only needs to be correct for a
4034 * prefix of this many threads
4035 *
4036 * TODO: add inclusive and excluse scan functions for GFX6.
4037 */
4038 static LLVMValueRef
4039 ac_build_scan(struct ac_llvm_context *ctx, nir_op op, LLVMValueRef src, LLVMValueRef identity,
4040 unsigned maxprefix)
4041 {
4042 LLVMValueRef result, tmp;
4043 result = src;
4044 if (maxprefix <= 1)
4045 return result;
4046 tmp = ac_build_dpp(ctx, identity, src, dpp_row_sr(1), 0xf, 0xf, false);
4047 result = ac_build_alu_op(ctx, result, tmp, op);
4048 if (maxprefix <= 2)
4049 return result;
4050 tmp = ac_build_dpp(ctx, identity, src, dpp_row_sr(2), 0xf, 0xf, false);
4051 result = ac_build_alu_op(ctx, result, tmp, op);
4052 if (maxprefix <= 3)
4053 return result;
4054 tmp = ac_build_dpp(ctx, identity, src, dpp_row_sr(3), 0xf, 0xf, false);
4055 result = ac_build_alu_op(ctx, result, tmp, op);
4056 if (maxprefix <= 4)
4057 return result;
4058 tmp = ac_build_dpp(ctx, identity, result, dpp_row_sr(4), 0xf, 0xe, false);
4059 result = ac_build_alu_op(ctx, result, tmp, op);
4060 if (maxprefix <= 8)
4061 return result;
4062 tmp = ac_build_dpp(ctx, identity, result, dpp_row_sr(8), 0xf, 0xc, false);
4063 result = ac_build_alu_op(ctx, result, tmp, op);
4064 if (maxprefix <= 16)
4065 return result;
4066 tmp = ac_build_dpp(ctx, identity, result, dpp_row_bcast15, 0xa, 0xf, false);
4067 result = ac_build_alu_op(ctx, result, tmp, op);
4068 if (maxprefix <= 32)
4069 return result;
4070 tmp = ac_build_dpp(ctx, identity, result, dpp_row_bcast31, 0xc, 0xf, false);
4071 result = ac_build_alu_op(ctx, result, tmp, op);
4072 return result;
4073 }
4074
4075 LLVMValueRef
4076 ac_build_inclusive_scan(struct ac_llvm_context *ctx, LLVMValueRef src, nir_op op)
4077 {
4078 LLVMValueRef result;
4079
4080 if (LLVMTypeOf(src) == ctx->i1 && op == nir_op_iadd) {
4081 LLVMBuilderRef builder = ctx->builder;
4082 src = LLVMBuildZExt(builder, src, ctx->i32, "");
4083 result = ac_build_ballot(ctx, src);
4084 result = ac_build_mbcnt(ctx, result);
4085 result = LLVMBuildAdd(builder, result, src, "");
4086 return result;
4087 }
4088
4089 ac_build_optimization_barrier(ctx, &src);
4090
4091 LLVMValueRef identity =
4092 get_reduction_identity(ctx, op, ac_get_type_size(LLVMTypeOf(src)));
4093 result = LLVMBuildBitCast(ctx->builder, ac_build_set_inactive(ctx, src, identity),
4094 LLVMTypeOf(identity), "");
4095 result = ac_build_scan(ctx, op, result, identity, 64);
4096
4097 return ac_build_wwm(ctx, result);
4098 }
4099
4100 LLVMValueRef
4101 ac_build_exclusive_scan(struct ac_llvm_context *ctx, LLVMValueRef src, nir_op op)
4102 {
4103 LLVMValueRef result;
4104
4105 if (LLVMTypeOf(src) == ctx->i1 && op == nir_op_iadd) {
4106 LLVMBuilderRef builder = ctx->builder;
4107 src = LLVMBuildZExt(builder, src, ctx->i32, "");
4108 result = ac_build_ballot(ctx, src);
4109 result = ac_build_mbcnt(ctx, result);
4110 return result;
4111 }
4112
4113 ac_build_optimization_barrier(ctx, &src);
4114
4115 LLVMValueRef identity =
4116 get_reduction_identity(ctx, op, ac_get_type_size(LLVMTypeOf(src)));
4117 result = LLVMBuildBitCast(ctx->builder, ac_build_set_inactive(ctx, src, identity),
4118 LLVMTypeOf(identity), "");
4119 result = ac_build_dpp(ctx, identity, result, dpp_wf_sr1, 0xf, 0xf, false);
4120 result = ac_build_scan(ctx, op, result, identity, 64);
4121
4122 return ac_build_wwm(ctx, result);
4123 }
4124
4125 LLVMValueRef
4126 ac_build_reduce(struct ac_llvm_context *ctx, LLVMValueRef src, nir_op op, unsigned cluster_size)
4127 {
4128 if (cluster_size == 1) return src;
4129 ac_build_optimization_barrier(ctx, &src);
4130 LLVMValueRef result, swap;
4131 LLVMValueRef identity = get_reduction_identity(ctx, op,
4132 ac_get_type_size(LLVMTypeOf(src)));
4133 result = LLVMBuildBitCast(ctx->builder,
4134 ac_build_set_inactive(ctx, src, identity),
4135 LLVMTypeOf(identity), "");
4136 swap = ac_build_quad_swizzle(ctx, result, 1, 0, 3, 2);
4137 result = ac_build_alu_op(ctx, result, swap, op);
4138 if (cluster_size == 2) return ac_build_wwm(ctx, result);
4139
4140 swap = ac_build_quad_swizzle(ctx, result, 2, 3, 0, 1);
4141 result = ac_build_alu_op(ctx, result, swap, op);
4142 if (cluster_size == 4) return ac_build_wwm(ctx, result);
4143
4144 if (ctx->chip_class >= GFX8)
4145 swap = ac_build_dpp(ctx, identity, result, dpp_row_half_mirror, 0xf, 0xf, false);
4146 else
4147 swap = ac_build_ds_swizzle(ctx, result, ds_pattern_bitmode(0x1f, 0, 0x04));
4148 result = ac_build_alu_op(ctx, result, swap, op);
4149 if (cluster_size == 8) return ac_build_wwm(ctx, result);
4150
4151 if (ctx->chip_class >= GFX8)
4152 swap = ac_build_dpp(ctx, identity, result, dpp_row_mirror, 0xf, 0xf, false);
4153 else
4154 swap = ac_build_ds_swizzle(ctx, result, ds_pattern_bitmode(0x1f, 0, 0x08));
4155 result = ac_build_alu_op(ctx, result, swap, op);
4156 if (cluster_size == 16) return ac_build_wwm(ctx, result);
4157
4158 if (ctx->chip_class >= GFX8 && cluster_size != 32)
4159 swap = ac_build_dpp(ctx, identity, result, dpp_row_bcast15, 0xa, 0xf, false);
4160 else
4161 swap = ac_build_ds_swizzle(ctx, result, ds_pattern_bitmode(0x1f, 0, 0x10));
4162 result = ac_build_alu_op(ctx, result, swap, op);
4163 if (cluster_size == 32) return ac_build_wwm(ctx, result);
4164
4165 if (ctx->chip_class >= GFX8) {
4166 swap = ac_build_dpp(ctx, identity, result, dpp_row_bcast31, 0xc, 0xf, false);
4167 result = ac_build_alu_op(ctx, result, swap, op);
4168 result = ac_build_readlane(ctx, result, LLVMConstInt(ctx->i32, 63, 0));
4169 return ac_build_wwm(ctx, result);
4170 } else {
4171 swap = ac_build_readlane(ctx, result, ctx->i32_0);
4172 result = ac_build_readlane(ctx, result, LLVMConstInt(ctx->i32, 32, 0));
4173 result = ac_build_alu_op(ctx, result, swap, op);
4174 return ac_build_wwm(ctx, result);
4175 }
4176 }
4177
4178 /**
4179 * "Top half" of a scan that reduces per-wave values across an entire
4180 * workgroup.
4181 *
4182 * The source value must be present in the highest lane of the wave, and the
4183 * highest lane must be live.
4184 */
4185 void
4186 ac_build_wg_wavescan_top(struct ac_llvm_context *ctx, struct ac_wg_scan *ws)
4187 {
4188 if (ws->maxwaves <= 1)
4189 return;
4190
4191 const LLVMValueRef i32_63 = LLVMConstInt(ctx->i32, 63, false);
4192 LLVMBuilderRef builder = ctx->builder;
4193 LLVMValueRef tid = ac_get_thread_id(ctx);
4194 LLVMValueRef tmp;
4195
4196 tmp = LLVMBuildICmp(builder, LLVMIntEQ, tid, i32_63, "");
4197 ac_build_ifcc(ctx, tmp, 1000);
4198 LLVMBuildStore(builder, ws->src, LLVMBuildGEP(builder, ws->scratch, &ws->waveidx, 1, ""));
4199 ac_build_endif(ctx, 1000);
4200 }
4201
4202 /**
4203 * "Bottom half" of a scan that reduces per-wave values across an entire
4204 * workgroup.
4205 *
4206 * The caller must place a barrier between the top and bottom halves.
4207 */
4208 void
4209 ac_build_wg_wavescan_bottom(struct ac_llvm_context *ctx, struct ac_wg_scan *ws)
4210 {
4211 const LLVMTypeRef type = LLVMTypeOf(ws->src);
4212 const LLVMValueRef identity =
4213 get_reduction_identity(ctx, ws->op, ac_get_type_size(type));
4214
4215 if (ws->maxwaves <= 1) {
4216 ws->result_reduce = ws->src;
4217 ws->result_inclusive = ws->src;
4218 ws->result_exclusive = identity;
4219 return;
4220 }
4221 assert(ws->maxwaves <= 32);
4222
4223 LLVMBuilderRef builder = ctx->builder;
4224 LLVMValueRef tid = ac_get_thread_id(ctx);
4225 LLVMBasicBlockRef bbs[2];
4226 LLVMValueRef phivalues_scan[2];
4227 LLVMValueRef tmp, tmp2;
4228
4229 bbs[0] = LLVMGetInsertBlock(builder);
4230 phivalues_scan[0] = LLVMGetUndef(type);
4231
4232 if (ws->enable_reduce)
4233 tmp = LLVMBuildICmp(builder, LLVMIntULT, tid, ws->numwaves, "");
4234 else if (ws->enable_inclusive)
4235 tmp = LLVMBuildICmp(builder, LLVMIntULE, tid, ws->waveidx, "");
4236 else
4237 tmp = LLVMBuildICmp(builder, LLVMIntULT, tid, ws->waveidx, "");
4238 ac_build_ifcc(ctx, tmp, 1001);
4239 {
4240 tmp = LLVMBuildLoad(builder, LLVMBuildGEP(builder, ws->scratch, &tid, 1, ""), "");
4241
4242 ac_build_optimization_barrier(ctx, &tmp);
4243
4244 bbs[1] = LLVMGetInsertBlock(builder);
4245 phivalues_scan[1] = ac_build_scan(ctx, ws->op, tmp, identity, ws->maxwaves);
4246 }
4247 ac_build_endif(ctx, 1001);
4248
4249 const LLVMValueRef scan = ac_build_phi(ctx, type, 2, phivalues_scan, bbs);
4250
4251 if (ws->enable_reduce) {
4252 tmp = LLVMBuildSub(builder, ws->numwaves, ctx->i32_1, "");
4253 ws->result_reduce = ac_build_readlane(ctx, scan, tmp);
4254 }
4255 if (ws->enable_inclusive)
4256 ws->result_inclusive = ac_build_readlane(ctx, scan, ws->waveidx);
4257 if (ws->enable_exclusive) {
4258 tmp = LLVMBuildSub(builder, ws->waveidx, ctx->i32_1, "");
4259 tmp = ac_build_readlane(ctx, scan, tmp);
4260 tmp2 = LLVMBuildICmp(builder, LLVMIntEQ, ws->waveidx, ctx->i32_0, "");
4261 ws->result_exclusive = LLVMBuildSelect(builder, tmp2, identity, tmp, "");
4262 }
4263 }
4264
4265 /**
4266 * Inclusive scan of a per-wave value across an entire workgroup.
4267 *
4268 * This implies an s_barrier instruction.
4269 *
4270 * Unlike ac_build_inclusive_scan, the caller \em must ensure that all threads
4271 * of the workgroup are live. (This requirement cannot easily be relaxed in a
4272 * useful manner because of the barrier in the algorithm.)
4273 */
4274 void
4275 ac_build_wg_wavescan(struct ac_llvm_context *ctx, struct ac_wg_scan *ws)
4276 {
4277 ac_build_wg_wavescan_top(ctx, ws);
4278 ac_build_s_barrier(ctx);
4279 ac_build_wg_wavescan_bottom(ctx, ws);
4280 }
4281
4282 /**
4283 * "Top half" of a scan that reduces per-thread values across an entire
4284 * workgroup.
4285 *
4286 * All lanes must be active when this code runs.
4287 */
4288 void
4289 ac_build_wg_scan_top(struct ac_llvm_context *ctx, struct ac_wg_scan *ws)
4290 {
4291 if (ws->enable_exclusive) {
4292 ws->extra = ac_build_exclusive_scan(ctx, ws->src, ws->op);
4293 if (LLVMTypeOf(ws->src) == ctx->i1 && ws->op == nir_op_iadd)
4294 ws->src = LLVMBuildZExt(ctx->builder, ws->src, ctx->i32, "");
4295 ws->src = ac_build_alu_op(ctx, ws->extra, ws->src, ws->op);
4296 } else {
4297 ws->src = ac_build_inclusive_scan(ctx, ws->src, ws->op);
4298 }
4299
4300 bool enable_inclusive = ws->enable_inclusive;
4301 bool enable_exclusive = ws->enable_exclusive;
4302 ws->enable_inclusive = false;
4303 ws->enable_exclusive = ws->enable_exclusive || enable_inclusive;
4304 ac_build_wg_wavescan_top(ctx, ws);
4305 ws->enable_inclusive = enable_inclusive;
4306 ws->enable_exclusive = enable_exclusive;
4307 }
4308
4309 /**
4310 * "Bottom half" of a scan that reduces per-thread values across an entire
4311 * workgroup.
4312 *
4313 * The caller must place a barrier between the top and bottom halves.
4314 */
4315 void
4316 ac_build_wg_scan_bottom(struct ac_llvm_context *ctx, struct ac_wg_scan *ws)
4317 {
4318 bool enable_inclusive = ws->enable_inclusive;
4319 bool enable_exclusive = ws->enable_exclusive;
4320 ws->enable_inclusive = false;
4321 ws->enable_exclusive = ws->enable_exclusive || enable_inclusive;
4322 ac_build_wg_wavescan_bottom(ctx, ws);
4323 ws->enable_inclusive = enable_inclusive;
4324 ws->enable_exclusive = enable_exclusive;
4325
4326 /* ws->result_reduce is already the correct value */
4327 if (ws->enable_inclusive)
4328 ws->result_inclusive = ac_build_alu_op(ctx, ws->result_inclusive, ws->src, ws->op);
4329 if (ws->enable_exclusive)
4330 ws->result_exclusive = ac_build_alu_op(ctx, ws->result_exclusive, ws->extra, ws->op);
4331 }
4332
4333 /**
4334 * A scan that reduces per-thread values across an entire workgroup.
4335 *
4336 * The caller must ensure that all lanes are active when this code runs
4337 * (WWM is insufficient!), because there is an implied barrier.
4338 */
4339 void
4340 ac_build_wg_scan(struct ac_llvm_context *ctx, struct ac_wg_scan *ws)
4341 {
4342 ac_build_wg_scan_top(ctx, ws);
4343 ac_build_s_barrier(ctx);
4344 ac_build_wg_scan_bottom(ctx, ws);
4345 }
4346
4347 LLVMValueRef
4348 ac_build_quad_swizzle(struct ac_llvm_context *ctx, LLVMValueRef src,
4349 unsigned lane0, unsigned lane1, unsigned lane2, unsigned lane3)
4350 {
4351 unsigned mask = dpp_quad_perm(lane0, lane1, lane2, lane3);
4352 if (ctx->chip_class >= GFX8) {
4353 return ac_build_dpp(ctx, src, src, mask, 0xf, 0xf, false);
4354 } else {
4355 return ac_build_ds_swizzle(ctx, src, (1 << 15) | mask);
4356 }
4357 }
4358
4359 LLVMValueRef
4360 ac_build_shuffle(struct ac_llvm_context *ctx, LLVMValueRef src, LLVMValueRef index)
4361 {
4362 index = LLVMBuildMul(ctx->builder, index, LLVMConstInt(ctx->i32, 4, 0), "");
4363 return ac_build_intrinsic(ctx,
4364 "llvm.amdgcn.ds.bpermute", ctx->i32,
4365 (LLVMValueRef []) {index, src}, 2,
4366 AC_FUNC_ATTR_READNONE |
4367 AC_FUNC_ATTR_CONVERGENT);
4368 }
4369
4370 LLVMValueRef
4371 ac_build_frexp_exp(struct ac_llvm_context *ctx, LLVMValueRef src0,
4372 unsigned bitsize)
4373 {
4374 LLVMTypeRef type;
4375 char *intr;
4376
4377 if (bitsize == 16) {
4378 intr = "llvm.amdgcn.frexp.exp.i16.f16";
4379 type = ctx->i16;
4380 } else if (bitsize == 32) {
4381 intr = "llvm.amdgcn.frexp.exp.i32.f32";
4382 type = ctx->i32;
4383 } else {
4384 intr = "llvm.amdgcn.frexp.exp.i32.f64";
4385 type = ctx->i32;
4386 }
4387
4388 LLVMValueRef params[] = {
4389 src0,
4390 };
4391 return ac_build_intrinsic(ctx, intr, type, params, 1,
4392 AC_FUNC_ATTR_READNONE);
4393 }
4394 LLVMValueRef
4395 ac_build_frexp_mant(struct ac_llvm_context *ctx, LLVMValueRef src0,
4396 unsigned bitsize)
4397 {
4398 LLVMTypeRef type;
4399 char *intr;
4400
4401 if (bitsize == 16) {
4402 intr = "llvm.amdgcn.frexp.mant.f16";
4403 type = ctx->f16;
4404 } else if (bitsize == 32) {
4405 intr = "llvm.amdgcn.frexp.mant.f32";
4406 type = ctx->f32;
4407 } else {
4408 intr = "llvm.amdgcn.frexp.mant.f64";
4409 type = ctx->f64;
4410 }
4411
4412 LLVMValueRef params[] = {
4413 src0,
4414 };
4415 return ac_build_intrinsic(ctx, intr, type, params, 1,
4416 AC_FUNC_ATTR_READNONE);
4417 }
4418
4419 /*
4420 * this takes an I,J coordinate pair,
4421 * and works out the X and Y derivatives.
4422 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
4423 */
4424 LLVMValueRef
4425 ac_build_ddxy_interp(struct ac_llvm_context *ctx, LLVMValueRef interp_ij)
4426 {
4427 LLVMValueRef result[4], a;
4428 unsigned i;
4429
4430 for (i = 0; i < 2; i++) {
4431 a = LLVMBuildExtractElement(ctx->builder, interp_ij,
4432 LLVMConstInt(ctx->i32, i, false), "");
4433 result[i] = ac_build_ddxy(ctx, AC_TID_MASK_TOP_LEFT, 1, a);
4434 result[2+i] = ac_build_ddxy(ctx, AC_TID_MASK_TOP_LEFT, 2, a);
4435 }
4436 return ac_build_gather_values(ctx, result, 4);
4437 }
4438
4439 LLVMValueRef
4440 ac_build_load_helper_invocation(struct ac_llvm_context *ctx)
4441 {
4442 LLVMValueRef result = ac_build_intrinsic(ctx, "llvm.amdgcn.ps.live",
4443 ctx->i1, NULL, 0,
4444 AC_FUNC_ATTR_READNONE);
4445 result = LLVMBuildNot(ctx->builder, result, "");
4446 return LLVMBuildSExt(ctx->builder, result, ctx->i32, "");
4447 }