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