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