823bf34acdbcf9b67fa3ff511698bb5af09eb94a
[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 {
65 LLVMValueRef args[1];
66
67 ctx->context = LLVMContextCreate();
68
69 ctx->chip_class = chip_class;
70 ctx->family = family;
71 ctx->wave_size = wave_size;
72 ctx->module = ac_create_module(wave_size == 32 ? compiler->tm_wave32
73 : compiler->tm,
74 ctx->context);
75 ctx->builder = ac_create_builder(ctx->context, float_mode);
76
77 ctx->voidt = LLVMVoidTypeInContext(ctx->context);
78 ctx->i1 = LLVMInt1TypeInContext(ctx->context);
79 ctx->i8 = LLVMInt8TypeInContext(ctx->context);
80 ctx->i16 = LLVMIntTypeInContext(ctx->context, 16);
81 ctx->i32 = LLVMIntTypeInContext(ctx->context, 32);
82 ctx->i64 = LLVMIntTypeInContext(ctx->context, 64);
83 ctx->intptr = ctx->i32;
84 ctx->f16 = LLVMHalfTypeInContext(ctx->context);
85 ctx->f32 = LLVMFloatTypeInContext(ctx->context);
86 ctx->f64 = LLVMDoubleTypeInContext(ctx->context);
87 ctx->v2i16 = LLVMVectorType(ctx->i16, 2);
88 ctx->v2i32 = LLVMVectorType(ctx->i32, 2);
89 ctx->v3i32 = LLVMVectorType(ctx->i32, 3);
90 ctx->v4i32 = LLVMVectorType(ctx->i32, 4);
91 ctx->v2f32 = LLVMVectorType(ctx->f32, 2);
92 ctx->v3f32 = LLVMVectorType(ctx->f32, 3);
93 ctx->v4f32 = LLVMVectorType(ctx->f32, 4);
94 ctx->v8i32 = LLVMVectorType(ctx->i32, 8);
95 ctx->iN_wavemask = LLVMIntTypeInContext(ctx->context, ctx->wave_size);
96
97 ctx->i8_0 = LLVMConstInt(ctx->i8, 0, false);
98 ctx->i8_1 = LLVMConstInt(ctx->i8, 1, false);
99 ctx->i16_0 = LLVMConstInt(ctx->i16, 0, false);
100 ctx->i16_1 = LLVMConstInt(ctx->i16, 1, false);
101 ctx->i32_0 = LLVMConstInt(ctx->i32, 0, false);
102 ctx->i32_1 = LLVMConstInt(ctx->i32, 1, false);
103 ctx->i64_0 = LLVMConstInt(ctx->i64, 0, false);
104 ctx->i64_1 = LLVMConstInt(ctx->i64, 1, false);
105 ctx->f16_0 = LLVMConstReal(ctx->f16, 0.0);
106 ctx->f16_1 = LLVMConstReal(ctx->f16, 1.0);
107 ctx->f32_0 = LLVMConstReal(ctx->f32, 0.0);
108 ctx->f32_1 = LLVMConstReal(ctx->f32, 1.0);
109 ctx->f64_0 = LLVMConstReal(ctx->f64, 0.0);
110 ctx->f64_1 = LLVMConstReal(ctx->f64, 1.0);
111
112 ctx->i1false = LLVMConstInt(ctx->i1, 0, false);
113 ctx->i1true = LLVMConstInt(ctx->i1, 1, false);
114
115 ctx->range_md_kind = LLVMGetMDKindIDInContext(ctx->context,
116 "range", 5);
117
118 ctx->invariant_load_md_kind = LLVMGetMDKindIDInContext(ctx->context,
119 "invariant.load", 14);
120
121 ctx->fpmath_md_kind = LLVMGetMDKindIDInContext(ctx->context, "fpmath", 6);
122
123 args[0] = LLVMConstReal(ctx->f32, 2.5);
124 ctx->fpmath_md_2p5_ulp = LLVMMDNodeInContext(ctx->context, args, 1);
125
126 ctx->uniform_md_kind = LLVMGetMDKindIDInContext(ctx->context,
127 "amdgpu.uniform", 14);
128
129 ctx->empty_md = LLVMMDNodeInContext(ctx->context, NULL, 0);
130 ctx->flow = calloc(1, sizeof(*ctx->flow));
131 }
132
133 void
134 ac_llvm_context_dispose(struct ac_llvm_context *ctx)
135 {
136 free(ctx->flow->stack);
137 free(ctx->flow);
138 ctx->flow = NULL;
139 }
140
141 int
142 ac_get_llvm_num_components(LLVMValueRef value)
143 {
144 LLVMTypeRef type = LLVMTypeOf(value);
145 unsigned num_components = LLVMGetTypeKind(type) == LLVMVectorTypeKind
146 ? LLVMGetVectorSize(type)
147 : 1;
148 return num_components;
149 }
150
151 LLVMValueRef
152 ac_llvm_extract_elem(struct ac_llvm_context *ac,
153 LLVMValueRef value,
154 int index)
155 {
156 if (LLVMGetTypeKind(LLVMTypeOf(value)) != LLVMVectorTypeKind) {
157 assert(index == 0);
158 return value;
159 }
160
161 return LLVMBuildExtractElement(ac->builder, value,
162 LLVMConstInt(ac->i32, index, false), "");
163 }
164
165 int
166 ac_get_elem_bits(struct ac_llvm_context *ctx, LLVMTypeRef type)
167 {
168 if (LLVMGetTypeKind(type) == LLVMVectorTypeKind)
169 type = LLVMGetElementType(type);
170
171 if (LLVMGetTypeKind(type) == LLVMIntegerTypeKind)
172 return LLVMGetIntTypeWidth(type);
173
174 if (type == ctx->f16)
175 return 16;
176 if (type == ctx->f32)
177 return 32;
178 if (type == ctx->f64)
179 return 64;
180
181 unreachable("Unhandled type kind in get_elem_bits");
182 }
183
184 unsigned
185 ac_get_type_size(LLVMTypeRef type)
186 {
187 LLVMTypeKind kind = LLVMGetTypeKind(type);
188
189 switch (kind) {
190 case LLVMIntegerTypeKind:
191 return LLVMGetIntTypeWidth(type) / 8;
192 case LLVMHalfTypeKind:
193 return 2;
194 case LLVMFloatTypeKind:
195 return 4;
196 case LLVMDoubleTypeKind:
197 return 8;
198 case LLVMPointerTypeKind:
199 if (LLVMGetPointerAddressSpace(type) == AC_ADDR_SPACE_CONST_32BIT)
200 return 4;
201 return 8;
202 case LLVMVectorTypeKind:
203 return LLVMGetVectorSize(type) *
204 ac_get_type_size(LLVMGetElementType(type));
205 case LLVMArrayTypeKind:
206 return LLVMGetArrayLength(type) *
207 ac_get_type_size(LLVMGetElementType(type));
208 default:
209 assert(0);
210 return 0;
211 }
212 }
213
214 static LLVMTypeRef to_integer_type_scalar(struct ac_llvm_context *ctx, LLVMTypeRef t)
215 {
216 if (t == ctx->i8)
217 return ctx->i8;
218 else if (t == ctx->f16 || t == ctx->i16)
219 return ctx->i16;
220 else if (t == ctx->f32 || t == ctx->i32)
221 return ctx->i32;
222 else if (t == ctx->f64 || t == ctx->i64)
223 return ctx->i64;
224 else
225 unreachable("Unhandled integer size");
226 }
227
228 LLVMTypeRef
229 ac_to_integer_type(struct ac_llvm_context *ctx, LLVMTypeRef t)
230 {
231 if (LLVMGetTypeKind(t) == LLVMVectorTypeKind) {
232 LLVMTypeRef elem_type = LLVMGetElementType(t);
233 return LLVMVectorType(to_integer_type_scalar(ctx, elem_type),
234 LLVMGetVectorSize(t));
235 }
236 if (LLVMGetTypeKind(t) == LLVMPointerTypeKind) {
237 switch (LLVMGetPointerAddressSpace(t)) {
238 case AC_ADDR_SPACE_GLOBAL:
239 return ctx->i64;
240 case AC_ADDR_SPACE_LDS:
241 return ctx->i32;
242 default:
243 unreachable("unhandled address space");
244 }
245 }
246 return to_integer_type_scalar(ctx, t);
247 }
248
249 LLVMValueRef
250 ac_to_integer(struct ac_llvm_context *ctx, LLVMValueRef v)
251 {
252 LLVMTypeRef type = LLVMTypeOf(v);
253 if (LLVMGetTypeKind(type) == LLVMPointerTypeKind) {
254 return LLVMBuildPtrToInt(ctx->builder, v, ac_to_integer_type(ctx, type), "");
255 }
256 return LLVMBuildBitCast(ctx->builder, v, ac_to_integer_type(ctx, type), "");
257 }
258
259 LLVMValueRef
260 ac_to_integer_or_pointer(struct ac_llvm_context *ctx, LLVMValueRef v)
261 {
262 LLVMTypeRef type = LLVMTypeOf(v);
263 if (LLVMGetTypeKind(type) == LLVMPointerTypeKind)
264 return v;
265 return ac_to_integer(ctx, v);
266 }
267
268 static LLVMTypeRef to_float_type_scalar(struct ac_llvm_context *ctx, LLVMTypeRef t)
269 {
270 if (t == ctx->i8)
271 return ctx->i8;
272 else if (t == ctx->i16 || t == ctx->f16)
273 return ctx->f16;
274 else if (t == ctx->i32 || t == ctx->f32)
275 return ctx->f32;
276 else if (t == ctx->i64 || t == ctx->f64)
277 return ctx->f64;
278 else
279 unreachable("Unhandled float size");
280 }
281
282 LLVMTypeRef
283 ac_to_float_type(struct ac_llvm_context *ctx, LLVMTypeRef t)
284 {
285 if (LLVMGetTypeKind(t) == LLVMVectorTypeKind) {
286 LLVMTypeRef elem_type = LLVMGetElementType(t);
287 return LLVMVectorType(to_float_type_scalar(ctx, elem_type),
288 LLVMGetVectorSize(t));
289 }
290 return to_float_type_scalar(ctx, t);
291 }
292
293 LLVMValueRef
294 ac_to_float(struct ac_llvm_context *ctx, LLVMValueRef v)
295 {
296 LLVMTypeRef type = LLVMTypeOf(v);
297 return LLVMBuildBitCast(ctx->builder, v, ac_to_float_type(ctx, type), "");
298 }
299
300
301 LLVMValueRef
302 ac_build_intrinsic(struct ac_llvm_context *ctx, const char *name,
303 LLVMTypeRef return_type, LLVMValueRef *params,
304 unsigned param_count, unsigned attrib_mask)
305 {
306 LLVMValueRef function, call;
307 bool set_callsite_attrs = !(attrib_mask & AC_FUNC_ATTR_LEGACY);
308
309 function = LLVMGetNamedFunction(ctx->module, name);
310 if (!function) {
311 LLVMTypeRef param_types[32], function_type;
312 unsigned i;
313
314 assert(param_count <= 32);
315
316 for (i = 0; i < param_count; ++i) {
317 assert(params[i]);
318 param_types[i] = LLVMTypeOf(params[i]);
319 }
320 function_type =
321 LLVMFunctionType(return_type, param_types, param_count, 0);
322 function = LLVMAddFunction(ctx->module, name, function_type);
323
324 LLVMSetFunctionCallConv(function, LLVMCCallConv);
325 LLVMSetLinkage(function, LLVMExternalLinkage);
326
327 if (!set_callsite_attrs)
328 ac_add_func_attributes(ctx->context, function, attrib_mask);
329 }
330
331 call = LLVMBuildCall(ctx->builder, function, params, param_count, "");
332 if (set_callsite_attrs)
333 ac_add_func_attributes(ctx->context, call, attrib_mask);
334 return call;
335 }
336
337 /**
338 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
339 * intrinsic names).
340 */
341 void ac_build_type_name_for_intr(LLVMTypeRef type, char *buf, unsigned bufsize)
342 {
343 LLVMTypeRef elem_type = type;
344
345 assert(bufsize >= 8);
346
347 if (LLVMGetTypeKind(type) == LLVMVectorTypeKind) {
348 int ret = snprintf(buf, bufsize, "v%u",
349 LLVMGetVectorSize(type));
350 if (ret < 0) {
351 char *type_name = LLVMPrintTypeToString(type);
352 fprintf(stderr, "Error building type name for: %s\n",
353 type_name);
354 LLVMDisposeMessage(type_name);
355 return;
356 }
357 elem_type = LLVMGetElementType(type);
358 buf += ret;
359 bufsize -= ret;
360 }
361 switch (LLVMGetTypeKind(elem_type)) {
362 default: break;
363 case LLVMIntegerTypeKind:
364 snprintf(buf, bufsize, "i%d", LLVMGetIntTypeWidth(elem_type));
365 break;
366 case LLVMHalfTypeKind:
367 snprintf(buf, bufsize, "f16");
368 break;
369 case LLVMFloatTypeKind:
370 snprintf(buf, bufsize, "f32");
371 break;
372 case LLVMDoubleTypeKind:
373 snprintf(buf, bufsize, "f64");
374 break;
375 }
376 }
377
378 /**
379 * Helper function that builds an LLVM IR PHI node and immediately adds
380 * incoming edges.
381 */
382 LLVMValueRef
383 ac_build_phi(struct ac_llvm_context *ctx, LLVMTypeRef type,
384 unsigned count_incoming, LLVMValueRef *values,
385 LLVMBasicBlockRef *blocks)
386 {
387 LLVMValueRef phi = LLVMBuildPhi(ctx->builder, type, "");
388 LLVMAddIncoming(phi, values, blocks, count_incoming);
389 return phi;
390 }
391
392 void ac_build_s_barrier(struct ac_llvm_context *ctx)
393 {
394 ac_build_intrinsic(ctx, "llvm.amdgcn.s.barrier", ctx->voidt, NULL,
395 0, AC_FUNC_ATTR_CONVERGENT);
396 }
397
398 /* Prevent optimizations (at least of memory accesses) across the current
399 * point in the program by emitting empty inline assembly that is marked as
400 * having side effects.
401 *
402 * Optionally, a value can be passed through the inline assembly to prevent
403 * LLVM from hoisting calls to ReadNone functions.
404 */
405 void
406 ac_build_optimization_barrier(struct ac_llvm_context *ctx,
407 LLVMValueRef *pvgpr)
408 {
409 static int counter = 0;
410
411 LLVMBuilderRef builder = ctx->builder;
412 char code[16];
413
414 snprintf(code, sizeof(code), "; %d", p_atomic_inc_return(&counter));
415
416 if (!pvgpr) {
417 LLVMTypeRef ftype = LLVMFunctionType(ctx->voidt, NULL, 0, false);
418 LLVMValueRef inlineasm = LLVMConstInlineAsm(ftype, code, "", true, false);
419 LLVMBuildCall(builder, inlineasm, NULL, 0, "");
420 } else {
421 LLVMTypeRef ftype = LLVMFunctionType(ctx->i32, &ctx->i32, 1, false);
422 LLVMValueRef inlineasm = LLVMConstInlineAsm(ftype, code, "=v,0", true, false);
423 LLVMValueRef vgpr = *pvgpr;
424 LLVMTypeRef vgpr_type = LLVMTypeOf(vgpr);
425 unsigned vgpr_size = ac_get_type_size(vgpr_type);
426 LLVMValueRef vgpr0;
427
428 assert(vgpr_size % 4 == 0);
429
430 vgpr = LLVMBuildBitCast(builder, vgpr, LLVMVectorType(ctx->i32, vgpr_size / 4), "");
431 vgpr0 = LLVMBuildExtractElement(builder, vgpr, ctx->i32_0, "");
432 vgpr0 = LLVMBuildCall(builder, inlineasm, &vgpr0, 1, "");
433 vgpr = LLVMBuildInsertElement(builder, vgpr, vgpr0, ctx->i32_0, "");
434 vgpr = LLVMBuildBitCast(builder, vgpr, vgpr_type, "");
435
436 *pvgpr = vgpr;
437 }
438 }
439
440 LLVMValueRef
441 ac_build_shader_clock(struct ac_llvm_context *ctx)
442 {
443 const char *intr = HAVE_LLVM >= 0x0900 && ctx->chip_class >= GFX8 ?
444 "llvm.amdgcn.s.memrealtime" : "llvm.readcyclecounter";
445 LLVMValueRef tmp = ac_build_intrinsic(ctx, intr, ctx->i64, NULL, 0, 0);
446 return LLVMBuildBitCast(ctx->builder, tmp, ctx->v2i32, "");
447 }
448
449 LLVMValueRef
450 ac_build_ballot(struct ac_llvm_context *ctx,
451 LLVMValueRef value)
452 {
453 const char *name;
454
455 if (HAVE_LLVM >= 0x900) {
456 if (ctx->wave_size == 64)
457 name = "llvm.amdgcn.icmp.i64.i32";
458 else
459 name = "llvm.amdgcn.icmp.i32.i32";
460 } else {
461 name = "llvm.amdgcn.icmp.i32";
462 }
463 LLVMValueRef args[3] = {
464 value,
465 ctx->i32_0,
466 LLVMConstInt(ctx->i32, LLVMIntNE, 0)
467 };
468
469 /* We currently have no other way to prevent LLVM from lifting the icmp
470 * calls to a dominating basic block.
471 */
472 ac_build_optimization_barrier(ctx, &args[0]);
473
474 args[0] = ac_to_integer(ctx, args[0]);
475
476 return ac_build_intrinsic(ctx, name, ctx->iN_wavemask, args, 3,
477 AC_FUNC_ATTR_NOUNWIND |
478 AC_FUNC_ATTR_READNONE |
479 AC_FUNC_ATTR_CONVERGENT);
480 }
481
482 LLVMValueRef ac_get_i1_sgpr_mask(struct ac_llvm_context *ctx,
483 LLVMValueRef value)
484 {
485 const char *name = HAVE_LLVM >= 0x900 ? "llvm.amdgcn.icmp.i64.i1" : "llvm.amdgcn.icmp.i1";
486 LLVMValueRef args[3] = {
487 value,
488 ctx->i1false,
489 LLVMConstInt(ctx->i32, LLVMIntNE, 0),
490 };
491
492 assert(HAVE_LLVM >= 0x0800);
493 return ac_build_intrinsic(ctx, name, ctx->i64, args, 3,
494 AC_FUNC_ATTR_NOUNWIND |
495 AC_FUNC_ATTR_READNONE |
496 AC_FUNC_ATTR_CONVERGENT);
497 }
498
499 LLVMValueRef
500 ac_build_vote_all(struct ac_llvm_context *ctx, LLVMValueRef value)
501 {
502 LLVMValueRef active_set = ac_build_ballot(ctx, ctx->i32_1);
503 LLVMValueRef vote_set = ac_build_ballot(ctx, value);
504 return LLVMBuildICmp(ctx->builder, LLVMIntEQ, vote_set, active_set, "");
505 }
506
507 LLVMValueRef
508 ac_build_vote_any(struct ac_llvm_context *ctx, LLVMValueRef value)
509 {
510 LLVMValueRef vote_set = ac_build_ballot(ctx, value);
511 return LLVMBuildICmp(ctx->builder, LLVMIntNE, vote_set,
512 LLVMConstInt(ctx->iN_wavemask, 0, 0), "");
513 }
514
515 LLVMValueRef
516 ac_build_vote_eq(struct ac_llvm_context *ctx, LLVMValueRef value)
517 {
518 LLVMValueRef active_set = ac_build_ballot(ctx, ctx->i32_1);
519 LLVMValueRef vote_set = ac_build_ballot(ctx, value);
520
521 LLVMValueRef all = LLVMBuildICmp(ctx->builder, LLVMIntEQ,
522 vote_set, active_set, "");
523 LLVMValueRef none = LLVMBuildICmp(ctx->builder, LLVMIntEQ,
524 vote_set,
525 LLVMConstInt(ctx->iN_wavemask, 0, 0), "");
526 return LLVMBuildOr(ctx->builder, all, none, "");
527 }
528
529 LLVMValueRef
530 ac_build_varying_gather_values(struct ac_llvm_context *ctx, LLVMValueRef *values,
531 unsigned value_count, unsigned component)
532 {
533 LLVMValueRef vec = NULL;
534
535 if (value_count == 1) {
536 return values[component];
537 } else if (!value_count)
538 unreachable("value_count is 0");
539
540 for (unsigned i = component; i < value_count + component; i++) {
541 LLVMValueRef value = values[i];
542
543 if (i == component)
544 vec = LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value), value_count));
545 LLVMValueRef index = LLVMConstInt(ctx->i32, i - component, false);
546 vec = LLVMBuildInsertElement(ctx->builder, vec, value, index, "");
547 }
548 return vec;
549 }
550
551 LLVMValueRef
552 ac_build_gather_values_extended(struct ac_llvm_context *ctx,
553 LLVMValueRef *values,
554 unsigned value_count,
555 unsigned value_stride,
556 bool load,
557 bool always_vector)
558 {
559 LLVMBuilderRef builder = ctx->builder;
560 LLVMValueRef vec = NULL;
561 unsigned i;
562
563 if (value_count == 1 && !always_vector) {
564 if (load)
565 return LLVMBuildLoad(builder, values[0], "");
566 return values[0];
567 } else if (!value_count)
568 unreachable("value_count is 0");
569
570 for (i = 0; i < value_count; i++) {
571 LLVMValueRef value = values[i * value_stride];
572 if (load)
573 value = LLVMBuildLoad(builder, value, "");
574
575 if (!i)
576 vec = LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value), value_count));
577 LLVMValueRef index = LLVMConstInt(ctx->i32, i, false);
578 vec = LLVMBuildInsertElement(builder, vec, value, index, "");
579 }
580 return vec;
581 }
582
583 LLVMValueRef
584 ac_build_gather_values(struct ac_llvm_context *ctx,
585 LLVMValueRef *values,
586 unsigned value_count)
587 {
588 return ac_build_gather_values_extended(ctx, values, value_count, 1, false, false);
589 }
590
591 /* Expand a scalar or vector to <dst_channels x type> by filling the remaining
592 * channels with undef. Extract at most src_channels components from the input.
593 */
594 static LLVMValueRef
595 ac_build_expand(struct ac_llvm_context *ctx,
596 LLVMValueRef value,
597 unsigned src_channels,
598 unsigned dst_channels)
599 {
600 LLVMTypeRef elemtype;
601 LLVMValueRef chan[dst_channels];
602
603 if (LLVMGetTypeKind(LLVMTypeOf(value)) == LLVMVectorTypeKind) {
604 unsigned vec_size = LLVMGetVectorSize(LLVMTypeOf(value));
605
606 if (src_channels == dst_channels && vec_size == dst_channels)
607 return value;
608
609 src_channels = MIN2(src_channels, vec_size);
610
611 for (unsigned i = 0; i < src_channels; i++)
612 chan[i] = ac_llvm_extract_elem(ctx, value, i);
613
614 elemtype = LLVMGetElementType(LLVMTypeOf(value));
615 } else {
616 if (src_channels) {
617 assert(src_channels == 1);
618 chan[0] = value;
619 }
620 elemtype = LLVMTypeOf(value);
621 }
622
623 for (unsigned i = src_channels; i < dst_channels; i++)
624 chan[i] = LLVMGetUndef(elemtype);
625
626 return ac_build_gather_values(ctx, chan, dst_channels);
627 }
628
629 /* Extract components [start, start + channels) from a vector.
630 */
631 LLVMValueRef
632 ac_extract_components(struct ac_llvm_context *ctx,
633 LLVMValueRef value,
634 unsigned start,
635 unsigned channels)
636 {
637 LLVMValueRef chan[channels];
638
639 for (unsigned i = 0; i < channels; i++)
640 chan[i] = ac_llvm_extract_elem(ctx, value, i + start);
641
642 return ac_build_gather_values(ctx, chan, channels);
643 }
644
645 /* Expand a scalar or vector to <4 x type> by filling the remaining channels
646 * with undef. Extract at most num_channels components from the input.
647 */
648 LLVMValueRef ac_build_expand_to_vec4(struct ac_llvm_context *ctx,
649 LLVMValueRef value,
650 unsigned num_channels)
651 {
652 return ac_build_expand(ctx, value, num_channels, 4);
653 }
654
655 LLVMValueRef ac_build_round(struct ac_llvm_context *ctx, LLVMValueRef value)
656 {
657 unsigned type_size = ac_get_type_size(LLVMTypeOf(value));
658 const char *name;
659
660 if (type_size == 2)
661 name = "llvm.rint.f16";
662 else if (type_size == 4)
663 name = "llvm.rint.f32";
664 else
665 name = "llvm.rint.f64";
666
667 return ac_build_intrinsic(ctx, name, LLVMTypeOf(value), &value, 1,
668 AC_FUNC_ATTR_READNONE);
669 }
670
671 LLVMValueRef
672 ac_build_fdiv(struct ac_llvm_context *ctx,
673 LLVMValueRef num,
674 LLVMValueRef den)
675 {
676 /* If we do (num / den), LLVM >= 7.0 does:
677 * return num * v_rcp_f32(den * (fabs(den) > 0x1.0p+96f ? 0x1.0p-32f : 1.0f));
678 *
679 * If we do (num * (1 / den)), LLVM does:
680 * return num * v_rcp_f32(den);
681 */
682 LLVMValueRef one = LLVMConstReal(LLVMTypeOf(num), 1.0);
683 LLVMValueRef rcp = LLVMBuildFDiv(ctx->builder, one, den, "");
684 LLVMValueRef ret = LLVMBuildFMul(ctx->builder, num, rcp, "");
685
686 /* Use v_rcp_f32 instead of precise division. */
687 if (!LLVMIsConstant(ret))
688 LLVMSetMetadata(ret, ctx->fpmath_md_kind, ctx->fpmath_md_2p5_ulp);
689 return ret;
690 }
691
692 /* See fast_idiv_by_const.h. */
693 /* Set: increment = util_fast_udiv_info::increment ? multiplier : 0; */
694 LLVMValueRef ac_build_fast_udiv(struct ac_llvm_context *ctx,
695 LLVMValueRef num,
696 LLVMValueRef multiplier,
697 LLVMValueRef pre_shift,
698 LLVMValueRef post_shift,
699 LLVMValueRef increment)
700 {
701 LLVMBuilderRef builder = ctx->builder;
702
703 num = LLVMBuildLShr(builder, num, pre_shift, "");
704 num = LLVMBuildMul(builder,
705 LLVMBuildZExt(builder, num, ctx->i64, ""),
706 LLVMBuildZExt(builder, multiplier, ctx->i64, ""), "");
707 num = LLVMBuildAdd(builder, num,
708 LLVMBuildZExt(builder, increment, ctx->i64, ""), "");
709 num = LLVMBuildLShr(builder, num, LLVMConstInt(ctx->i64, 32, 0), "");
710 num = LLVMBuildTrunc(builder, num, ctx->i32, "");
711 return LLVMBuildLShr(builder, num, post_shift, "");
712 }
713
714 /* See fast_idiv_by_const.h. */
715 /* If num != UINT_MAX, this more efficient version can be used. */
716 /* Set: increment = util_fast_udiv_info::increment; */
717 LLVMValueRef ac_build_fast_udiv_nuw(struct ac_llvm_context *ctx,
718 LLVMValueRef num,
719 LLVMValueRef multiplier,
720 LLVMValueRef pre_shift,
721 LLVMValueRef post_shift,
722 LLVMValueRef increment)
723 {
724 LLVMBuilderRef builder = ctx->builder;
725
726 num = LLVMBuildLShr(builder, num, pre_shift, "");
727 num = LLVMBuildNUWAdd(builder, num, increment, "");
728 num = LLVMBuildMul(builder,
729 LLVMBuildZExt(builder, num, ctx->i64, ""),
730 LLVMBuildZExt(builder, multiplier, ctx->i64, ""), "");
731 num = LLVMBuildLShr(builder, num, LLVMConstInt(ctx->i64, 32, 0), "");
732 num = LLVMBuildTrunc(builder, num, ctx->i32, "");
733 return LLVMBuildLShr(builder, num, post_shift, "");
734 }
735
736 /* See fast_idiv_by_const.h. */
737 /* Both operands must fit in 31 bits and the divisor must not be 1. */
738 LLVMValueRef ac_build_fast_udiv_u31_d_not_one(struct ac_llvm_context *ctx,
739 LLVMValueRef num,
740 LLVMValueRef multiplier,
741 LLVMValueRef post_shift)
742 {
743 LLVMBuilderRef builder = ctx->builder;
744
745 num = LLVMBuildMul(builder,
746 LLVMBuildZExt(builder, num, ctx->i64, ""),
747 LLVMBuildZExt(builder, multiplier, ctx->i64, ""), "");
748 num = LLVMBuildLShr(builder, num, LLVMConstInt(ctx->i64, 32, 0), "");
749 num = LLVMBuildTrunc(builder, num, ctx->i32, "");
750 return LLVMBuildLShr(builder, num, post_shift, "");
751 }
752
753 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
754 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
755 * already multiplied by two. id is the cube face number.
756 */
757 struct cube_selection_coords {
758 LLVMValueRef stc[2];
759 LLVMValueRef ma;
760 LLVMValueRef id;
761 };
762
763 static void
764 build_cube_intrinsic(struct ac_llvm_context *ctx,
765 LLVMValueRef in[3],
766 struct cube_selection_coords *out)
767 {
768 LLVMTypeRef f32 = ctx->f32;
769
770 out->stc[1] = ac_build_intrinsic(ctx, "llvm.amdgcn.cubetc",
771 f32, in, 3, AC_FUNC_ATTR_READNONE);
772 out->stc[0] = ac_build_intrinsic(ctx, "llvm.amdgcn.cubesc",
773 f32, in, 3, AC_FUNC_ATTR_READNONE);
774 out->ma = ac_build_intrinsic(ctx, "llvm.amdgcn.cubema",
775 f32, in, 3, AC_FUNC_ATTR_READNONE);
776 out->id = ac_build_intrinsic(ctx, "llvm.amdgcn.cubeid",
777 f32, in, 3, AC_FUNC_ATTR_READNONE);
778 }
779
780 /**
781 * Build a manual selection sequence for cube face sc/tc coordinates and
782 * major axis vector (multiplied by 2 for consistency) for the given
783 * vec3 \p coords, for the face implied by \p selcoords.
784 *
785 * For the major axis, we always adjust the sign to be in the direction of
786 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
787 * the selcoords major axis.
788 */
789 static void build_cube_select(struct ac_llvm_context *ctx,
790 const struct cube_selection_coords *selcoords,
791 const LLVMValueRef *coords,
792 LLVMValueRef *out_st,
793 LLVMValueRef *out_ma)
794 {
795 LLVMBuilderRef builder = ctx->builder;
796 LLVMTypeRef f32 = LLVMTypeOf(coords[0]);
797 LLVMValueRef is_ma_positive;
798 LLVMValueRef sgn_ma;
799 LLVMValueRef is_ma_z, is_not_ma_z;
800 LLVMValueRef is_ma_y;
801 LLVMValueRef is_ma_x;
802 LLVMValueRef sgn;
803 LLVMValueRef tmp;
804
805 is_ma_positive = LLVMBuildFCmp(builder, LLVMRealUGE,
806 selcoords->ma, LLVMConstReal(f32, 0.0), "");
807 sgn_ma = LLVMBuildSelect(builder, is_ma_positive,
808 LLVMConstReal(f32, 1.0), LLVMConstReal(f32, -1.0), "");
809
810 is_ma_z = LLVMBuildFCmp(builder, LLVMRealUGE, selcoords->id, LLVMConstReal(f32, 4.0), "");
811 is_not_ma_z = LLVMBuildNot(builder, is_ma_z, "");
812 is_ma_y = LLVMBuildAnd(builder, is_not_ma_z,
813 LLVMBuildFCmp(builder, LLVMRealUGE, selcoords->id, LLVMConstReal(f32, 2.0), ""), "");
814 is_ma_x = LLVMBuildAnd(builder, is_not_ma_z, LLVMBuildNot(builder, is_ma_y, ""), "");
815
816 /* Select sc */
817 tmp = LLVMBuildSelect(builder, is_ma_x, coords[2], coords[0], "");
818 sgn = LLVMBuildSelect(builder, is_ma_y, LLVMConstReal(f32, 1.0),
819 LLVMBuildSelect(builder, is_ma_z, sgn_ma,
820 LLVMBuildFNeg(builder, sgn_ma, ""), ""), "");
821 out_st[0] = LLVMBuildFMul(builder, tmp, sgn, "");
822
823 /* Select tc */
824 tmp = LLVMBuildSelect(builder, is_ma_y, coords[2], coords[1], "");
825 sgn = LLVMBuildSelect(builder, is_ma_y, sgn_ma,
826 LLVMConstReal(f32, -1.0), "");
827 out_st[1] = LLVMBuildFMul(builder, tmp, sgn, "");
828
829 /* Select ma */
830 tmp = LLVMBuildSelect(builder, is_ma_z, coords[2],
831 LLVMBuildSelect(builder, is_ma_y, coords[1], coords[0], ""), "");
832 tmp = ac_build_intrinsic(ctx, "llvm.fabs.f32",
833 ctx->f32, &tmp, 1, AC_FUNC_ATTR_READNONE);
834 *out_ma = LLVMBuildFMul(builder, tmp, LLVMConstReal(f32, 2.0), "");
835 }
836
837 void
838 ac_prepare_cube_coords(struct ac_llvm_context *ctx,
839 bool is_deriv, bool is_array, bool is_lod,
840 LLVMValueRef *coords_arg,
841 LLVMValueRef *derivs_arg)
842 {
843
844 LLVMBuilderRef builder = ctx->builder;
845 struct cube_selection_coords selcoords;
846 LLVMValueRef coords[3];
847 LLVMValueRef invma;
848
849 if (is_array && !is_lod) {
850 LLVMValueRef tmp = ac_build_round(ctx, coords_arg[3]);
851
852 /* Section 8.9 (Texture Functions) of the GLSL 4.50 spec says:
853 *
854 * "For Array forms, the array layer used will be
855 *
856 * max(0, min(d−1, floor(layer+0.5)))
857 *
858 * where d is the depth of the texture array and layer
859 * comes from the component indicated in the tables below.
860 * Workaroudn for an issue where the layer is taken from a
861 * helper invocation which happens to fall on a different
862 * layer due to extrapolation."
863 *
864 * GFX8 and earlier attempt to implement this in hardware by
865 * clamping the value of coords[2] = (8 * layer) + face.
866 * Unfortunately, this means that the we end up with the wrong
867 * face when clamping occurs.
868 *
869 * Clamp the layer earlier to work around the issue.
870 */
871 if (ctx->chip_class <= GFX8) {
872 LLVMValueRef ge0;
873 ge0 = LLVMBuildFCmp(builder, LLVMRealOGE, tmp, ctx->f32_0, "");
874 tmp = LLVMBuildSelect(builder, ge0, tmp, ctx->f32_0, "");
875 }
876
877 coords_arg[3] = tmp;
878 }
879
880 build_cube_intrinsic(ctx, coords_arg, &selcoords);
881
882 invma = ac_build_intrinsic(ctx, "llvm.fabs.f32",
883 ctx->f32, &selcoords.ma, 1, AC_FUNC_ATTR_READNONE);
884 invma = ac_build_fdiv(ctx, LLVMConstReal(ctx->f32, 1.0), invma);
885
886 for (int i = 0; i < 2; ++i)
887 coords[i] = LLVMBuildFMul(builder, selcoords.stc[i], invma, "");
888
889 coords[2] = selcoords.id;
890
891 if (is_deriv && derivs_arg) {
892 LLVMValueRef derivs[4];
893 int axis;
894
895 /* Convert cube derivatives to 2D derivatives. */
896 for (axis = 0; axis < 2; axis++) {
897 LLVMValueRef deriv_st[2];
898 LLVMValueRef deriv_ma;
899
900 /* Transform the derivative alongside the texture
901 * coordinate. Mathematically, the correct formula is
902 * as follows. Assume we're projecting onto the +Z face
903 * and denote by dx/dh the derivative of the (original)
904 * X texture coordinate with respect to horizontal
905 * window coordinates. The projection onto the +Z face
906 * plane is:
907 *
908 * f(x,z) = x/z
909 *
910 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
911 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
912 *
913 * This motivatives the implementation below.
914 *
915 * Whether this actually gives the expected results for
916 * apps that might feed in derivatives obtained via
917 * finite differences is anyone's guess. The OpenGL spec
918 * seems awfully quiet about how textureGrad for cube
919 * maps should be handled.
920 */
921 build_cube_select(ctx, &selcoords, &derivs_arg[axis * 3],
922 deriv_st, &deriv_ma);
923
924 deriv_ma = LLVMBuildFMul(builder, deriv_ma, invma, "");
925
926 for (int i = 0; i < 2; ++i)
927 derivs[axis * 2 + i] =
928 LLVMBuildFSub(builder,
929 LLVMBuildFMul(builder, deriv_st[i], invma, ""),
930 LLVMBuildFMul(builder, deriv_ma, coords[i], ""), "");
931 }
932
933 memcpy(derivs_arg, derivs, sizeof(derivs));
934 }
935
936 /* Shift the texture coordinate. This must be applied after the
937 * derivative calculation.
938 */
939 for (int i = 0; i < 2; ++i)
940 coords[i] = LLVMBuildFAdd(builder, coords[i], LLVMConstReal(ctx->f32, 1.5), "");
941
942 if (is_array) {
943 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
944 /* coords_arg.w component - array_index for cube arrays */
945 coords[2] = ac_build_fmad(ctx, coords_arg[3], LLVMConstReal(ctx->f32, 8.0), coords[2]);
946 }
947
948 memcpy(coords_arg, coords, sizeof(coords));
949 }
950
951
952 LLVMValueRef
953 ac_build_fs_interp(struct ac_llvm_context *ctx,
954 LLVMValueRef llvm_chan,
955 LLVMValueRef attr_number,
956 LLVMValueRef params,
957 LLVMValueRef i,
958 LLVMValueRef j)
959 {
960 LLVMValueRef args[5];
961 LLVMValueRef p1;
962
963 args[0] = i;
964 args[1] = llvm_chan;
965 args[2] = attr_number;
966 args[3] = params;
967
968 p1 = ac_build_intrinsic(ctx, "llvm.amdgcn.interp.p1",
969 ctx->f32, args, 4, AC_FUNC_ATTR_READNONE);
970
971 args[0] = p1;
972 args[1] = j;
973 args[2] = llvm_chan;
974 args[3] = attr_number;
975 args[4] = params;
976
977 return ac_build_intrinsic(ctx, "llvm.amdgcn.interp.p2",
978 ctx->f32, args, 5, AC_FUNC_ATTR_READNONE);
979 }
980
981 LLVMValueRef
982 ac_build_fs_interp_f16(struct ac_llvm_context *ctx,
983 LLVMValueRef llvm_chan,
984 LLVMValueRef attr_number,
985 LLVMValueRef params,
986 LLVMValueRef i,
987 LLVMValueRef j)
988 {
989 LLVMValueRef args[6];
990 LLVMValueRef p1;
991
992 args[0] = i;
993 args[1] = llvm_chan;
994 args[2] = attr_number;
995 args[3] = ctx->i1false;
996 args[4] = params;
997
998 p1 = ac_build_intrinsic(ctx, "llvm.amdgcn.interp.p1.f16",
999 ctx->f32, args, 5, AC_FUNC_ATTR_READNONE);
1000
1001 args[0] = p1;
1002 args[1] = j;
1003 args[2] = llvm_chan;
1004 args[3] = attr_number;
1005 args[4] = ctx->i1false;
1006 args[5] = params;
1007
1008 return ac_build_intrinsic(ctx, "llvm.amdgcn.interp.p2.f16",
1009 ctx->f16, args, 6, AC_FUNC_ATTR_READNONE);
1010 }
1011
1012 LLVMValueRef
1013 ac_build_fs_interp_mov(struct ac_llvm_context *ctx,
1014 LLVMValueRef parameter,
1015 LLVMValueRef llvm_chan,
1016 LLVMValueRef attr_number,
1017 LLVMValueRef params)
1018 {
1019 LLVMValueRef args[4];
1020
1021 args[0] = parameter;
1022 args[1] = llvm_chan;
1023 args[2] = attr_number;
1024 args[3] = params;
1025
1026 return ac_build_intrinsic(ctx, "llvm.amdgcn.interp.mov",
1027 ctx->f32, args, 4, AC_FUNC_ATTR_READNONE);
1028 }
1029
1030 LLVMValueRef
1031 ac_build_gep_ptr(struct ac_llvm_context *ctx,
1032 LLVMValueRef base_ptr,
1033 LLVMValueRef index)
1034 {
1035 return LLVMBuildGEP(ctx->builder, base_ptr, &index, 1, "");
1036 }
1037
1038 LLVMValueRef
1039 ac_build_gep0(struct ac_llvm_context *ctx,
1040 LLVMValueRef base_ptr,
1041 LLVMValueRef index)
1042 {
1043 LLVMValueRef indices[2] = {
1044 ctx->i32_0,
1045 index,
1046 };
1047 return LLVMBuildGEP(ctx->builder, base_ptr, indices, 2, "");
1048 }
1049
1050 LLVMValueRef ac_build_pointer_add(struct ac_llvm_context *ctx, LLVMValueRef ptr,
1051 LLVMValueRef index)
1052 {
1053 return LLVMBuildPointerCast(ctx->builder,
1054 LLVMBuildGEP(ctx->builder, ptr, &index, 1, ""),
1055 LLVMTypeOf(ptr), "");
1056 }
1057
1058 void
1059 ac_build_indexed_store(struct ac_llvm_context *ctx,
1060 LLVMValueRef base_ptr, LLVMValueRef index,
1061 LLVMValueRef value)
1062 {
1063 LLVMBuildStore(ctx->builder, value,
1064 ac_build_gep0(ctx, base_ptr, index));
1065 }
1066
1067 /**
1068 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
1069 * It's equivalent to doing a load from &base_ptr[index].
1070 *
1071 * \param base_ptr Where the array starts.
1072 * \param index The element index into the array.
1073 * \param uniform Whether the base_ptr and index can be assumed to be
1074 * dynamically uniform (i.e. load to an SGPR)
1075 * \param invariant Whether the load is invariant (no other opcodes affect it)
1076 * \param no_unsigned_wraparound
1077 * For all possible re-associations and re-distributions of an expression
1078 * "base_ptr + index * elemsize" into "addr + offset" (excluding GEPs
1079 * without inbounds in base_ptr), this parameter is true if "addr + offset"
1080 * does not result in an unsigned integer wraparound. This is used for
1081 * optimal code generation of 32-bit pointer arithmetic.
1082 *
1083 * For example, a 32-bit immediate offset that causes a 32-bit unsigned
1084 * integer wraparound can't be an imm offset in s_load_dword, because
1085 * the instruction performs "addr + offset" in 64 bits.
1086 *
1087 * Expected usage for bindless textures by chaining GEPs:
1088 * // possible unsigned wraparound, don't use InBounds:
1089 * ptr1 = LLVMBuildGEP(base_ptr, index);
1090 * image = load(ptr1); // becomes "s_load ptr1, 0"
1091 *
1092 * ptr2 = LLVMBuildInBoundsGEP(ptr1, 32 / elemsize);
1093 * sampler = load(ptr2); // becomes "s_load ptr1, 32" thanks to InBounds
1094 */
1095 static LLVMValueRef
1096 ac_build_load_custom(struct ac_llvm_context *ctx, LLVMValueRef base_ptr,
1097 LLVMValueRef index, bool uniform, bool invariant,
1098 bool no_unsigned_wraparound)
1099 {
1100 LLVMValueRef pointer, result;
1101
1102 if (no_unsigned_wraparound &&
1103 LLVMGetPointerAddressSpace(LLVMTypeOf(base_ptr)) == AC_ADDR_SPACE_CONST_32BIT)
1104 pointer = LLVMBuildInBoundsGEP(ctx->builder, base_ptr, &index, 1, "");
1105 else
1106 pointer = LLVMBuildGEP(ctx->builder, base_ptr, &index, 1, "");
1107
1108 if (uniform)
1109 LLVMSetMetadata(pointer, ctx->uniform_md_kind, ctx->empty_md);
1110 result = LLVMBuildLoad(ctx->builder, pointer, "");
1111 if (invariant)
1112 LLVMSetMetadata(result, ctx->invariant_load_md_kind, ctx->empty_md);
1113 return result;
1114 }
1115
1116 LLVMValueRef ac_build_load(struct ac_llvm_context *ctx, LLVMValueRef base_ptr,
1117 LLVMValueRef index)
1118 {
1119 return ac_build_load_custom(ctx, base_ptr, index, false, false, false);
1120 }
1121
1122 LLVMValueRef ac_build_load_invariant(struct ac_llvm_context *ctx,
1123 LLVMValueRef base_ptr, LLVMValueRef index)
1124 {
1125 return ac_build_load_custom(ctx, base_ptr, index, false, true, false);
1126 }
1127
1128 /* This assumes that there is no unsigned integer wraparound during the address
1129 * computation, excluding all GEPs within base_ptr. */
1130 LLVMValueRef ac_build_load_to_sgpr(struct ac_llvm_context *ctx,
1131 LLVMValueRef base_ptr, LLVMValueRef index)
1132 {
1133 return ac_build_load_custom(ctx, base_ptr, index, true, true, true);
1134 }
1135
1136 /* See ac_build_load_custom() documentation. */
1137 LLVMValueRef ac_build_load_to_sgpr_uint_wraparound(struct ac_llvm_context *ctx,
1138 LLVMValueRef base_ptr, LLVMValueRef index)
1139 {
1140 return ac_build_load_custom(ctx, base_ptr, index, true, true, false);
1141 }
1142
1143 static unsigned get_load_cache_policy(struct ac_llvm_context *ctx,
1144 unsigned cache_policy)
1145 {
1146 return cache_policy |
1147 (ctx->chip_class >= GFX10 && cache_policy & ac_glc ? ac_dlc : 0);
1148 }
1149
1150 static void
1151 ac_build_llvm7_buffer_store_common(struct ac_llvm_context *ctx,
1152 LLVMValueRef rsrc,
1153 LLVMValueRef data,
1154 LLVMValueRef vindex,
1155 LLVMValueRef voffset,
1156 unsigned num_channels,
1157 unsigned cache_policy,
1158 bool use_format)
1159 {
1160 LLVMValueRef args[] = {
1161 data,
1162 LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, ""),
1163 vindex ? vindex : ctx->i32_0,
1164 voffset,
1165 LLVMConstInt(ctx->i1, !!(cache_policy & ac_glc), 0),
1166 LLVMConstInt(ctx->i1, !!(cache_policy & ac_slc), 0)
1167 };
1168 unsigned func = CLAMP(num_channels, 1, 3) - 1;
1169
1170 const char *type_names[] = {"f32", "v2f32", "v4f32"};
1171 char name[256];
1172
1173 if (use_format) {
1174 snprintf(name, sizeof(name), "llvm.amdgcn.buffer.store.format.%s",
1175 type_names[func]);
1176 } else {
1177 snprintf(name, sizeof(name), "llvm.amdgcn.buffer.store.%s",
1178 type_names[func]);
1179 }
1180
1181 ac_build_intrinsic(ctx, name, ctx->voidt, args, ARRAY_SIZE(args),
1182 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY);
1183 }
1184
1185 static void
1186 ac_build_llvm8_buffer_store_common(struct ac_llvm_context *ctx,
1187 LLVMValueRef rsrc,
1188 LLVMValueRef data,
1189 LLVMValueRef vindex,
1190 LLVMValueRef voffset,
1191 LLVMValueRef soffset,
1192 unsigned num_channels,
1193 LLVMTypeRef return_channel_type,
1194 unsigned cache_policy,
1195 bool use_format,
1196 bool structurized)
1197 {
1198 LLVMValueRef args[6];
1199 int idx = 0;
1200 args[idx++] = data;
1201 args[idx++] = LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, "");
1202 if (structurized)
1203 args[idx++] = vindex ? vindex : ctx->i32_0;
1204 args[idx++] = voffset ? voffset : ctx->i32_0;
1205 args[idx++] = soffset ? soffset : ctx->i32_0;
1206 args[idx++] = LLVMConstInt(ctx->i32, cache_policy, 0);
1207 unsigned func = !ac_has_vec3_support(ctx->chip_class, use_format) && num_channels == 3 ? 4 : num_channels;
1208 const char *indexing_kind = structurized ? "struct" : "raw";
1209 char name[256], type_name[8];
1210
1211 LLVMTypeRef type = func > 1 ? LLVMVectorType(return_channel_type, func) : return_channel_type;
1212 ac_build_type_name_for_intr(type, type_name, sizeof(type_name));
1213
1214 if (use_format) {
1215 snprintf(name, sizeof(name), "llvm.amdgcn.%s.buffer.store.format.%s",
1216 indexing_kind, type_name);
1217 } else {
1218 snprintf(name, sizeof(name), "llvm.amdgcn.%s.buffer.store.%s",
1219 indexing_kind, type_name);
1220 }
1221
1222 ac_build_intrinsic(ctx, name, ctx->voidt, args, idx,
1223 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY);
1224 }
1225
1226 void
1227 ac_build_buffer_store_format(struct ac_llvm_context *ctx,
1228 LLVMValueRef rsrc,
1229 LLVMValueRef data,
1230 LLVMValueRef vindex,
1231 LLVMValueRef voffset,
1232 unsigned num_channels,
1233 unsigned cache_policy)
1234 {
1235 if (HAVE_LLVM >= 0x800) {
1236 ac_build_llvm8_buffer_store_common(ctx, rsrc, data, vindex,
1237 voffset, NULL, num_channels,
1238 ctx->f32, cache_policy,
1239 true, true);
1240 } else {
1241 ac_build_llvm7_buffer_store_common(ctx, rsrc, data, vindex, voffset,
1242 num_channels, cache_policy,
1243 true);
1244 }
1245 }
1246
1247 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
1248 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
1249 * or v4i32 (num_channels=3,4).
1250 */
1251 void
1252 ac_build_buffer_store_dword(struct ac_llvm_context *ctx,
1253 LLVMValueRef rsrc,
1254 LLVMValueRef vdata,
1255 unsigned num_channels,
1256 LLVMValueRef voffset,
1257 LLVMValueRef soffset,
1258 unsigned inst_offset,
1259 unsigned cache_policy,
1260 bool swizzle_enable_hint)
1261 {
1262 /* Split 3 channel stores, because only LLVM 9+ support 3-channel
1263 * intrinsics. */
1264 if (num_channels == 3 && !ac_has_vec3_support(ctx->chip_class, false)) {
1265 LLVMValueRef v[3], v01;
1266
1267 for (int i = 0; i < 3; i++) {
1268 v[i] = LLVMBuildExtractElement(ctx->builder, vdata,
1269 LLVMConstInt(ctx->i32, i, 0), "");
1270 }
1271 v01 = ac_build_gather_values(ctx, v, 2);
1272
1273 ac_build_buffer_store_dword(ctx, rsrc, v01, 2, voffset,
1274 soffset, inst_offset, cache_policy,
1275 swizzle_enable_hint);
1276 ac_build_buffer_store_dword(ctx, rsrc, v[2], 1, voffset,
1277 soffset, inst_offset + 8,
1278 cache_policy,
1279 swizzle_enable_hint);
1280 return;
1281 }
1282
1283 /* SWIZZLE_ENABLE requires that soffset isn't folded into voffset
1284 * (voffset is swizzled, but soffset isn't swizzled).
1285 * llvm.amdgcn.buffer.store doesn't have a separate soffset parameter.
1286 */
1287 if (!swizzle_enable_hint) {
1288 LLVMValueRef offset = soffset;
1289
1290 if (inst_offset)
1291 offset = LLVMBuildAdd(ctx->builder, offset,
1292 LLVMConstInt(ctx->i32, inst_offset, 0), "");
1293
1294 if (HAVE_LLVM >= 0x800) {
1295 ac_build_llvm8_buffer_store_common(ctx, rsrc,
1296 ac_to_float(ctx, vdata),
1297 ctx->i32_0,
1298 voffset, offset,
1299 num_channels,
1300 ctx->f32,
1301 cache_policy,
1302 false, false);
1303 } else {
1304 if (voffset)
1305 offset = LLVMBuildAdd(ctx->builder, offset, voffset, "");
1306
1307 ac_build_llvm7_buffer_store_common(ctx, rsrc,
1308 ac_to_float(ctx, vdata),
1309 ctx->i32_0, offset,
1310 num_channels, cache_policy,
1311 false);
1312 }
1313 return;
1314 }
1315
1316 static const unsigned dfmts[] = {
1317 V_008F0C_BUF_DATA_FORMAT_32,
1318 V_008F0C_BUF_DATA_FORMAT_32_32,
1319 V_008F0C_BUF_DATA_FORMAT_32_32_32,
1320 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
1321 };
1322 unsigned dfmt = dfmts[num_channels - 1];
1323 unsigned nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
1324 LLVMValueRef immoffset = LLVMConstInt(ctx->i32, inst_offset, 0);
1325
1326 ac_build_raw_tbuffer_store(ctx, rsrc, vdata, voffset, soffset,
1327 immoffset, num_channels, dfmt, nfmt, cache_policy);
1328 }
1329
1330 static LLVMValueRef
1331 ac_build_llvm7_buffer_load_common(struct ac_llvm_context *ctx,
1332 LLVMValueRef rsrc,
1333 LLVMValueRef vindex,
1334 LLVMValueRef voffset,
1335 unsigned num_channels,
1336 unsigned cache_policy,
1337 bool can_speculate,
1338 bool use_format)
1339 {
1340 LLVMValueRef args[] = {
1341 LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, ""),
1342 vindex ? vindex : ctx->i32_0,
1343 voffset,
1344 LLVMConstInt(ctx->i1, !!(cache_policy & ac_glc), 0),
1345 LLVMConstInt(ctx->i1, !!(cache_policy & ac_slc), 0)
1346 };
1347 unsigned func = CLAMP(num_channels, 1, 3) - 1;
1348
1349 LLVMTypeRef types[] = {ctx->f32, ctx->v2f32, ctx->v4f32};
1350 const char *type_names[] = {"f32", "v2f32", "v4f32"};
1351 char name[256];
1352
1353 if (use_format) {
1354 snprintf(name, sizeof(name), "llvm.amdgcn.buffer.load.format.%s",
1355 type_names[func]);
1356 } else {
1357 snprintf(name, sizeof(name), "llvm.amdgcn.buffer.load.%s",
1358 type_names[func]);
1359 }
1360
1361 return ac_build_intrinsic(ctx, name, types[func], args,
1362 ARRAY_SIZE(args),
1363 ac_get_load_intr_attribs(can_speculate));
1364 }
1365
1366 static LLVMValueRef
1367 ac_build_llvm8_buffer_load_common(struct ac_llvm_context *ctx,
1368 LLVMValueRef rsrc,
1369 LLVMValueRef vindex,
1370 LLVMValueRef voffset,
1371 LLVMValueRef soffset,
1372 unsigned num_channels,
1373 LLVMTypeRef channel_type,
1374 unsigned cache_policy,
1375 bool can_speculate,
1376 bool use_format,
1377 bool structurized)
1378 {
1379 LLVMValueRef args[5];
1380 int idx = 0;
1381 args[idx++] = LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, "");
1382 if (structurized)
1383 args[idx++] = vindex ? vindex : ctx->i32_0;
1384 args[idx++] = voffset ? voffset : ctx->i32_0;
1385 args[idx++] = soffset ? soffset : ctx->i32_0;
1386 args[idx++] = LLVMConstInt(ctx->i32, get_load_cache_policy(ctx, cache_policy), 0);
1387 unsigned func = !ac_has_vec3_support(ctx->chip_class, use_format) && num_channels == 3 ? 4 : num_channels;
1388 const char *indexing_kind = structurized ? "struct" : "raw";
1389 char name[256], type_name[8];
1390
1391 LLVMTypeRef type = func > 1 ? LLVMVectorType(channel_type, func) : channel_type;
1392 ac_build_type_name_for_intr(type, type_name, sizeof(type_name));
1393
1394 if (use_format) {
1395 snprintf(name, sizeof(name), "llvm.amdgcn.%s.buffer.load.format.%s",
1396 indexing_kind, type_name);
1397 } else {
1398 snprintf(name, sizeof(name), "llvm.amdgcn.%s.buffer.load.%s",
1399 indexing_kind, type_name);
1400 }
1401
1402 return ac_build_intrinsic(ctx, name, type, args, idx,
1403 ac_get_load_intr_attribs(can_speculate));
1404 }
1405
1406 LLVMValueRef
1407 ac_build_buffer_load(struct ac_llvm_context *ctx,
1408 LLVMValueRef rsrc,
1409 int num_channels,
1410 LLVMValueRef vindex,
1411 LLVMValueRef voffset,
1412 LLVMValueRef soffset,
1413 unsigned inst_offset,
1414 unsigned cache_policy,
1415 bool can_speculate,
1416 bool allow_smem)
1417 {
1418 LLVMValueRef offset = LLVMConstInt(ctx->i32, inst_offset, 0);
1419 if (voffset)
1420 offset = LLVMBuildAdd(ctx->builder, offset, voffset, "");
1421 if (soffset)
1422 offset = LLVMBuildAdd(ctx->builder, offset, soffset, "");
1423
1424 if (allow_smem && !(cache_policy & ac_slc) &&
1425 (!(cache_policy & ac_glc) || (HAVE_LLVM >= 0x0800 && ctx->chip_class >= GFX8))) {
1426 assert(vindex == NULL);
1427
1428 LLVMValueRef result[8];
1429
1430 for (int i = 0; i < num_channels; i++) {
1431 if (i) {
1432 offset = LLVMBuildAdd(ctx->builder, offset,
1433 LLVMConstInt(ctx->i32, 4, 0), "");
1434 }
1435 const char *intrname =
1436 HAVE_LLVM >= 0x0800 ? "llvm.amdgcn.s.buffer.load.f32"
1437 : "llvm.SI.load.const.v4i32";
1438 unsigned num_args = HAVE_LLVM >= 0x0800 ? 3 : 2;
1439 LLVMValueRef args[3] = {
1440 rsrc,
1441 offset,
1442 LLVMConstInt(ctx->i32, get_load_cache_policy(ctx, cache_policy), 0),
1443 };
1444 result[i] = ac_build_intrinsic(ctx, intrname,
1445 ctx->f32, args, num_args,
1446 AC_FUNC_ATTR_READNONE |
1447 (HAVE_LLVM < 0x0800 ? AC_FUNC_ATTR_LEGACY : 0));
1448 }
1449 if (num_channels == 1)
1450 return result[0];
1451
1452 if (num_channels == 3 && !ac_has_vec3_support(ctx->chip_class, false))
1453 result[num_channels++] = LLVMGetUndef(ctx->f32);
1454 return ac_build_gather_values(ctx, result, num_channels);
1455 }
1456
1457 if (HAVE_LLVM >= 0x0800) {
1458 return ac_build_llvm8_buffer_load_common(ctx, rsrc, vindex,
1459 offset, ctx->i32_0,
1460 num_channels, ctx->f32,
1461 cache_policy,
1462 can_speculate, false,
1463 false);
1464 }
1465
1466 return ac_build_llvm7_buffer_load_common(ctx, rsrc, vindex, offset,
1467 num_channels, cache_policy,
1468 can_speculate, false);
1469 }
1470
1471 LLVMValueRef ac_build_buffer_load_format(struct ac_llvm_context *ctx,
1472 LLVMValueRef rsrc,
1473 LLVMValueRef vindex,
1474 LLVMValueRef voffset,
1475 unsigned num_channels,
1476 unsigned cache_policy,
1477 bool can_speculate)
1478 {
1479 if (HAVE_LLVM >= 0x800) {
1480 return ac_build_llvm8_buffer_load_common(ctx, rsrc, vindex, voffset, ctx->i32_0,
1481 num_channels, ctx->f32,
1482 cache_policy, can_speculate, true, true);
1483 }
1484 return ac_build_llvm7_buffer_load_common(ctx, rsrc, vindex, voffset,
1485 num_channels, cache_policy,
1486 can_speculate, true);
1487 }
1488
1489 LLVMValueRef ac_build_buffer_load_format_gfx9_safe(struct ac_llvm_context *ctx,
1490 LLVMValueRef rsrc,
1491 LLVMValueRef vindex,
1492 LLVMValueRef voffset,
1493 unsigned num_channels,
1494 unsigned cache_policy,
1495 bool can_speculate)
1496 {
1497 if (HAVE_LLVM >= 0x800) {
1498 return ac_build_llvm8_buffer_load_common(ctx, rsrc, vindex, voffset, ctx->i32_0,
1499 num_channels, ctx->f32,
1500 cache_policy, can_speculate, true, true);
1501 }
1502
1503 LLVMValueRef elem_count = LLVMBuildExtractElement(ctx->builder, rsrc, LLVMConstInt(ctx->i32, 2, 0), "");
1504 LLVMValueRef stride = LLVMBuildExtractElement(ctx->builder, rsrc, ctx->i32_1, "");
1505 stride = LLVMBuildLShr(ctx->builder, stride, LLVMConstInt(ctx->i32, 16, 0), "");
1506
1507 LLVMValueRef new_elem_count = LLVMBuildSelect(ctx->builder,
1508 LLVMBuildICmp(ctx->builder, LLVMIntUGT, elem_count, stride, ""),
1509 elem_count, stride, "");
1510
1511 LLVMValueRef new_rsrc = LLVMBuildInsertElement(ctx->builder, rsrc, new_elem_count,
1512 LLVMConstInt(ctx->i32, 2, 0), "");
1513
1514 return ac_build_llvm7_buffer_load_common(ctx, new_rsrc, vindex, voffset,
1515 num_channels, cache_policy,
1516 can_speculate, true);
1517 }
1518
1519 /// Translate a (dfmt, nfmt) pair into a chip-appropriate combined format
1520 /// value for LLVM8+ tbuffer intrinsics.
1521 static unsigned
1522 ac_get_tbuffer_format(struct ac_llvm_context *ctx,
1523 unsigned dfmt, unsigned nfmt)
1524 {
1525 if (ctx->chip_class >= GFX10) {
1526 unsigned format;
1527 switch (dfmt) {
1528 default: unreachable("bad dfmt");
1529 case V_008F0C_BUF_DATA_FORMAT_INVALID: format = V_008F0C_IMG_FORMAT_INVALID; break;
1530 case V_008F0C_BUF_DATA_FORMAT_8: format = V_008F0C_IMG_FORMAT_8_UINT; break;
1531 case V_008F0C_BUF_DATA_FORMAT_8_8: format = V_008F0C_IMG_FORMAT_8_8_UINT; break;
1532 case V_008F0C_BUF_DATA_FORMAT_8_8_8_8: format = V_008F0C_IMG_FORMAT_8_8_8_8_UINT; break;
1533 case V_008F0C_BUF_DATA_FORMAT_16: format = V_008F0C_IMG_FORMAT_16_UINT; break;
1534 case V_008F0C_BUF_DATA_FORMAT_16_16: format = V_008F0C_IMG_FORMAT_16_16_UINT; break;
1535 case V_008F0C_BUF_DATA_FORMAT_16_16_16_16: format = V_008F0C_IMG_FORMAT_16_16_16_16_UINT; break;
1536 case V_008F0C_BUF_DATA_FORMAT_32: format = V_008F0C_IMG_FORMAT_32_UINT; break;
1537 case V_008F0C_BUF_DATA_FORMAT_32_32: format = V_008F0C_IMG_FORMAT_32_32_UINT; break;
1538 case V_008F0C_BUF_DATA_FORMAT_32_32_32: format = V_008F0C_IMG_FORMAT_32_32_32_UINT; break;
1539 case V_008F0C_BUF_DATA_FORMAT_32_32_32_32: format = V_008F0C_IMG_FORMAT_32_32_32_32_UINT; break;
1540 case V_008F0C_BUF_DATA_FORMAT_2_10_10_10: format = V_008F0C_IMG_FORMAT_2_10_10_10_UINT; break;
1541 }
1542
1543 // Use the regularity properties of the combined format enum.
1544 //
1545 // Note: float is incompatible with 8-bit data formats,
1546 // [us]{norm,scaled} are incomparible with 32-bit data formats.
1547 // [us]scaled are not writable.
1548 switch (nfmt) {
1549 case V_008F0C_BUF_NUM_FORMAT_UNORM: format -= 4; break;
1550 case V_008F0C_BUF_NUM_FORMAT_SNORM: format -= 3; break;
1551 case V_008F0C_BUF_NUM_FORMAT_USCALED: format -= 2; break;
1552 case V_008F0C_BUF_NUM_FORMAT_SSCALED: format -= 1; break;
1553 default: unreachable("bad nfmt");
1554 case V_008F0C_BUF_NUM_FORMAT_UINT: break;
1555 case V_008F0C_BUF_NUM_FORMAT_SINT: format += 1; break;
1556 case V_008F0C_BUF_NUM_FORMAT_FLOAT: format += 2; break;
1557 }
1558
1559 return format;
1560 } else {
1561 return dfmt | (nfmt << 4);
1562 }
1563 }
1564
1565 static LLVMValueRef
1566 ac_build_llvm8_tbuffer_load(struct ac_llvm_context *ctx,
1567 LLVMValueRef rsrc,
1568 LLVMValueRef vindex,
1569 LLVMValueRef voffset,
1570 LLVMValueRef soffset,
1571 unsigned num_channels,
1572 unsigned dfmt,
1573 unsigned nfmt,
1574 unsigned cache_policy,
1575 bool can_speculate,
1576 bool structurized)
1577 {
1578 LLVMValueRef args[6];
1579 int idx = 0;
1580 args[idx++] = LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, "");
1581 if (structurized)
1582 args[idx++] = vindex ? vindex : ctx->i32_0;
1583 args[idx++] = voffset ? voffset : ctx->i32_0;
1584 args[idx++] = soffset ? soffset : ctx->i32_0;
1585 args[idx++] = LLVMConstInt(ctx->i32, ac_get_tbuffer_format(ctx, dfmt, nfmt), 0);
1586 args[idx++] = LLVMConstInt(ctx->i32, get_load_cache_policy(ctx, cache_policy), 0);
1587 unsigned func = !ac_has_vec3_support(ctx->chip_class, true) && num_channels == 3 ? 4 : num_channels;
1588 const char *indexing_kind = structurized ? "struct" : "raw";
1589 char name[256], type_name[8];
1590
1591 LLVMTypeRef type = func > 1 ? LLVMVectorType(ctx->i32, func) : ctx->i32;
1592 ac_build_type_name_for_intr(type, type_name, sizeof(type_name));
1593
1594 snprintf(name, sizeof(name), "llvm.amdgcn.%s.tbuffer.load.%s",
1595 indexing_kind, type_name);
1596
1597 return ac_build_intrinsic(ctx, name, type, args, idx,
1598 ac_get_load_intr_attribs(can_speculate));
1599 }
1600
1601 static LLVMValueRef
1602 ac_build_tbuffer_load(struct ac_llvm_context *ctx,
1603 LLVMValueRef rsrc,
1604 LLVMValueRef vindex,
1605 LLVMValueRef voffset,
1606 LLVMValueRef soffset,
1607 LLVMValueRef immoffset,
1608 unsigned num_channels,
1609 unsigned dfmt,
1610 unsigned nfmt,
1611 unsigned cache_policy,
1612 bool can_speculate,
1613 bool structurized) /* only matters for LLVM 8+ */
1614 {
1615 if (HAVE_LLVM >= 0x800) {
1616 voffset = LLVMBuildAdd(ctx->builder, voffset, immoffset, "");
1617
1618 return ac_build_llvm8_tbuffer_load(ctx, rsrc, vindex, voffset,
1619 soffset, num_channels,
1620 dfmt, nfmt, cache_policy,
1621 can_speculate, structurized);
1622 }
1623
1624 LLVMValueRef args[] = {
1625 rsrc,
1626 vindex ? vindex : ctx->i32_0,
1627 voffset,
1628 soffset,
1629 immoffset,
1630 LLVMConstInt(ctx->i32, dfmt, false),
1631 LLVMConstInt(ctx->i32, nfmt, false),
1632 LLVMConstInt(ctx->i1, !!(cache_policy & ac_glc), false),
1633 LLVMConstInt(ctx->i1, !!(cache_policy & ac_slc), false),
1634 };
1635 unsigned func = CLAMP(num_channels, 1, 3) - 1;
1636 LLVMTypeRef types[] = {ctx->i32, ctx->v2i32, ctx->v4i32};
1637 const char *type_names[] = {"i32", "v2i32", "v4i32"};
1638 char name[256];
1639
1640 snprintf(name, sizeof(name), "llvm.amdgcn.tbuffer.load.%s",
1641 type_names[func]);
1642
1643 return ac_build_intrinsic(ctx, name, types[func], args, 9,
1644 ac_get_load_intr_attribs(can_speculate));
1645 }
1646
1647 LLVMValueRef
1648 ac_build_struct_tbuffer_load(struct ac_llvm_context *ctx,
1649 LLVMValueRef rsrc,
1650 LLVMValueRef vindex,
1651 LLVMValueRef voffset,
1652 LLVMValueRef soffset,
1653 LLVMValueRef immoffset,
1654 unsigned num_channels,
1655 unsigned dfmt,
1656 unsigned nfmt,
1657 unsigned cache_policy,
1658 bool can_speculate)
1659 {
1660 return ac_build_tbuffer_load(ctx, rsrc, vindex, voffset, soffset,
1661 immoffset, num_channels, dfmt, nfmt,
1662 cache_policy, can_speculate, true);
1663 }
1664
1665 LLVMValueRef
1666 ac_build_raw_tbuffer_load(struct ac_llvm_context *ctx,
1667 LLVMValueRef rsrc,
1668 LLVMValueRef voffset,
1669 LLVMValueRef soffset,
1670 LLVMValueRef immoffset,
1671 unsigned num_channels,
1672 unsigned dfmt,
1673 unsigned nfmt,
1674 unsigned cache_policy,
1675 bool can_speculate)
1676 {
1677 return ac_build_tbuffer_load(ctx, rsrc, NULL, voffset, soffset,
1678 immoffset, num_channels, dfmt, nfmt,
1679 cache_policy, can_speculate, false);
1680 }
1681
1682 LLVMValueRef
1683 ac_build_tbuffer_load_short(struct ac_llvm_context *ctx,
1684 LLVMValueRef rsrc,
1685 LLVMValueRef voffset,
1686 LLVMValueRef soffset,
1687 LLVMValueRef immoffset,
1688 unsigned cache_policy)
1689 {
1690 LLVMValueRef res;
1691
1692 if (HAVE_LLVM >= 0x900) {
1693 voffset = LLVMBuildAdd(ctx->builder, voffset, immoffset, "");
1694
1695 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1696 res = ac_build_llvm8_buffer_load_common(ctx, rsrc, NULL,
1697 voffset, soffset,
1698 1, ctx->i16, cache_policy,
1699 false, false, false);
1700 } else {
1701 unsigned dfmt = V_008F0C_BUF_DATA_FORMAT_16;
1702 unsigned nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
1703
1704 res = ac_build_raw_tbuffer_load(ctx, rsrc, voffset, soffset,
1705 immoffset, 1, dfmt, nfmt, cache_policy,
1706 false);
1707
1708 res = LLVMBuildTrunc(ctx->builder, res, ctx->i16, "");
1709 }
1710
1711 return res;
1712 }
1713
1714 LLVMValueRef
1715 ac_build_tbuffer_load_byte(struct ac_llvm_context *ctx,
1716 LLVMValueRef rsrc,
1717 LLVMValueRef voffset,
1718 LLVMValueRef soffset,
1719 LLVMValueRef immoffset,
1720 unsigned cache_policy)
1721 {
1722 LLVMValueRef res;
1723
1724 if (HAVE_LLVM >= 0x900) {
1725 voffset = LLVMBuildAdd(ctx->builder, voffset, immoffset, "");
1726
1727 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1728 res = ac_build_llvm8_buffer_load_common(ctx, rsrc, NULL,
1729 voffset, soffset,
1730 1, ctx->i8, cache_policy,
1731 false, false, false);
1732 } else {
1733 unsigned dfmt = V_008F0C_BUF_DATA_FORMAT_8;
1734 unsigned nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
1735
1736 res = ac_build_raw_tbuffer_load(ctx, rsrc, voffset, soffset,
1737 immoffset, 1, dfmt, nfmt, cache_policy,
1738 false);
1739
1740 res = LLVMBuildTrunc(ctx->builder, res, ctx->i8, "");
1741 }
1742
1743 return res;
1744 }
1745
1746 /**
1747 * Convert an 11- or 10-bit unsigned floating point number to an f32.
1748 *
1749 * The input exponent is expected to be biased analogous to IEEE-754, i.e. by
1750 * 2^(exp_bits-1) - 1 (as defined in OpenGL and other graphics APIs).
1751 */
1752 static LLVMValueRef
1753 ac_ufN_to_float(struct ac_llvm_context *ctx, LLVMValueRef src, unsigned exp_bits, unsigned mant_bits)
1754 {
1755 assert(LLVMTypeOf(src) == ctx->i32);
1756
1757 LLVMValueRef tmp;
1758 LLVMValueRef mantissa;
1759 mantissa = LLVMBuildAnd(ctx->builder, src, LLVMConstInt(ctx->i32, (1 << mant_bits) - 1, false), "");
1760
1761 /* Converting normal numbers is just a shift + correcting the exponent bias */
1762 unsigned normal_shift = 23 - mant_bits;
1763 unsigned bias_shift = 127 - ((1 << (exp_bits - 1)) - 1);
1764 LLVMValueRef shifted, normal;
1765
1766 shifted = LLVMBuildShl(ctx->builder, src, LLVMConstInt(ctx->i32, normal_shift, false), "");
1767 normal = LLVMBuildAdd(ctx->builder, shifted, LLVMConstInt(ctx->i32, bias_shift << 23, false), "");
1768
1769 /* Converting nan/inf numbers is the same, but with a different exponent update */
1770 LLVMValueRef naninf;
1771 naninf = LLVMBuildOr(ctx->builder, normal, LLVMConstInt(ctx->i32, 0xff << 23, false), "");
1772
1773 /* Converting denormals is the complex case: determine the leading zeros of the
1774 * mantissa to obtain the correct shift for the mantissa and exponent correction.
1775 */
1776 LLVMValueRef denormal;
1777 LLVMValueRef params[2] = {
1778 mantissa,
1779 ctx->i1true, /* result can be undef when arg is 0 */
1780 };
1781 LLVMValueRef ctlz = ac_build_intrinsic(ctx, "llvm.ctlz.i32", ctx->i32,
1782 params, 2, AC_FUNC_ATTR_READNONE);
1783
1784 /* Shift such that the leading 1 ends up as the LSB of the exponent field. */
1785 tmp = LLVMBuildSub(ctx->builder, ctlz, LLVMConstInt(ctx->i32, 8, false), "");
1786 denormal = LLVMBuildShl(ctx->builder, mantissa, tmp, "");
1787
1788 unsigned denormal_exp = bias_shift + (32 - mant_bits) - 1;
1789 tmp = LLVMBuildSub(ctx->builder, LLVMConstInt(ctx->i32, denormal_exp, false), ctlz, "");
1790 tmp = LLVMBuildShl(ctx->builder, tmp, LLVMConstInt(ctx->i32, 23, false), "");
1791 denormal = LLVMBuildAdd(ctx->builder, denormal, tmp, "");
1792
1793 /* Select the final result. */
1794 LLVMValueRef result;
1795
1796 tmp = LLVMBuildICmp(ctx->builder, LLVMIntUGE, src,
1797 LLVMConstInt(ctx->i32, ((1 << exp_bits) - 1) << mant_bits, false), "");
1798 result = LLVMBuildSelect(ctx->builder, tmp, naninf, normal, "");
1799
1800 tmp = LLVMBuildICmp(ctx->builder, LLVMIntUGE, src,
1801 LLVMConstInt(ctx->i32, 1 << mant_bits, false), "");
1802 result = LLVMBuildSelect(ctx->builder, tmp, result, denormal, "");
1803
1804 tmp = LLVMBuildICmp(ctx->builder, LLVMIntNE, src, ctx->i32_0, "");
1805 result = LLVMBuildSelect(ctx->builder, tmp, result, ctx->i32_0, "");
1806
1807 return ac_to_float(ctx, result);
1808 }
1809
1810 /**
1811 * Generate a fully general open coded buffer format fetch with all required
1812 * fixups suitable for vertex fetch, using non-format buffer loads.
1813 *
1814 * Some combinations of argument values have special interpretations:
1815 * - size = 8 bytes, format = fixed indicates PIPE_FORMAT_R11G11B10_FLOAT
1816 * - size = 8 bytes, format != {float,fixed} indicates a 2_10_10_10 data format
1817 *
1818 * \param log_size log(size of channel in bytes)
1819 * \param num_channels number of channels (1 to 4)
1820 * \param format AC_FETCH_FORMAT_xxx value
1821 * \param reverse whether XYZ channels are reversed
1822 * \param known_aligned whether the source is known to be aligned to hardware's
1823 * effective element size for loading the given format
1824 * (note: this means dword alignment for 8_8_8_8, 16_16, etc.)
1825 * \param rsrc buffer resource descriptor
1826 * \return the resulting vector of floats or integers bitcast to <4 x i32>
1827 */
1828 LLVMValueRef
1829 ac_build_opencoded_load_format(struct ac_llvm_context *ctx,
1830 unsigned log_size,
1831 unsigned num_channels,
1832 unsigned format,
1833 bool reverse,
1834 bool known_aligned,
1835 LLVMValueRef rsrc,
1836 LLVMValueRef vindex,
1837 LLVMValueRef voffset,
1838 LLVMValueRef soffset,
1839 unsigned cache_policy,
1840 bool can_speculate)
1841 {
1842 LLVMValueRef tmp;
1843 unsigned load_log_size = log_size;
1844 unsigned load_num_channels = num_channels;
1845 if (log_size == 3) {
1846 load_log_size = 2;
1847 if (format == AC_FETCH_FORMAT_FLOAT) {
1848 load_num_channels = 2 * num_channels;
1849 } else {
1850 load_num_channels = 1; /* 10_11_11 or 2_10_10_10 */
1851 }
1852 }
1853
1854 int log_recombine = 0;
1855 if (ctx->chip_class == GFX6 && !known_aligned) {
1856 /* Avoid alignment restrictions by loading one byte at a time. */
1857 load_num_channels <<= load_log_size;
1858 log_recombine = load_log_size;
1859 load_log_size = 0;
1860 } else if (load_num_channels == 2 || load_num_channels == 4) {
1861 log_recombine = -util_logbase2(load_num_channels);
1862 load_num_channels = 1;
1863 load_log_size += -log_recombine;
1864 }
1865
1866 assert(load_log_size >= 2 || HAVE_LLVM >= 0x0900);
1867
1868 LLVMValueRef loads[32]; /* up to 32 bytes */
1869 for (unsigned i = 0; i < load_num_channels; ++i) {
1870 tmp = LLVMBuildAdd(ctx->builder, soffset,
1871 LLVMConstInt(ctx->i32, i << load_log_size, false), "");
1872 if (HAVE_LLVM >= 0x0800) {
1873 LLVMTypeRef channel_type = load_log_size == 0 ? ctx->i8 :
1874 load_log_size == 1 ? ctx->i16 : ctx->i32;
1875 unsigned num_channels = 1 << (MAX2(load_log_size, 2) - 2);
1876 loads[i] = ac_build_llvm8_buffer_load_common(
1877 ctx, rsrc, vindex, voffset, tmp,
1878 num_channels, channel_type, cache_policy,
1879 can_speculate, false, true);
1880 } else {
1881 tmp = LLVMBuildAdd(ctx->builder, voffset, tmp, "");
1882 loads[i] = ac_build_llvm7_buffer_load_common(
1883 ctx, rsrc, vindex, tmp,
1884 1 << (load_log_size - 2), cache_policy, can_speculate, false);
1885 }
1886 if (load_log_size >= 2)
1887 loads[i] = ac_to_integer(ctx, loads[i]);
1888 }
1889
1890 if (log_recombine > 0) {
1891 /* Recombine bytes if necessary (GFX6 only) */
1892 LLVMTypeRef dst_type = log_recombine == 2 ? ctx->i32 : ctx->i16;
1893
1894 for (unsigned src = 0, dst = 0; src < load_num_channels; ++dst) {
1895 LLVMValueRef accum = NULL;
1896 for (unsigned i = 0; i < (1 << log_recombine); ++i, ++src) {
1897 tmp = LLVMBuildZExt(ctx->builder, loads[src], dst_type, "");
1898 if (i == 0) {
1899 accum = tmp;
1900 } else {
1901 tmp = LLVMBuildShl(ctx->builder, tmp,
1902 LLVMConstInt(dst_type, 8 * i, false), "");
1903 accum = LLVMBuildOr(ctx->builder, accum, tmp, "");
1904 }
1905 }
1906 loads[dst] = accum;
1907 }
1908 } else if (log_recombine < 0) {
1909 /* Split vectors of dwords */
1910 if (load_log_size > 2) {
1911 assert(load_num_channels == 1);
1912 LLVMValueRef loaded = loads[0];
1913 unsigned log_split = load_log_size - 2;
1914 log_recombine += log_split;
1915 load_num_channels = 1 << log_split;
1916 load_log_size = 2;
1917 for (unsigned i = 0; i < load_num_channels; ++i) {
1918 tmp = LLVMConstInt(ctx->i32, i, false);
1919 loads[i] = LLVMBuildExtractElement(ctx->builder, loaded, tmp, "");
1920 }
1921 }
1922
1923 /* Further split dwords and shorts if required */
1924 if (log_recombine < 0) {
1925 for (unsigned src = load_num_channels,
1926 dst = load_num_channels << -log_recombine;
1927 src > 0; --src) {
1928 unsigned dst_bits = 1 << (3 + load_log_size + log_recombine);
1929 LLVMTypeRef dst_type = LLVMIntTypeInContext(ctx->context, dst_bits);
1930 LLVMValueRef loaded = loads[src - 1];
1931 LLVMTypeRef loaded_type = LLVMTypeOf(loaded);
1932 for (unsigned i = 1 << -log_recombine; i > 0; --i, --dst) {
1933 tmp = LLVMConstInt(loaded_type, dst_bits * (i - 1), false);
1934 tmp = LLVMBuildLShr(ctx->builder, loaded, tmp, "");
1935 loads[dst - 1] = LLVMBuildTrunc(ctx->builder, tmp, dst_type, "");
1936 }
1937 }
1938 }
1939 }
1940
1941 if (log_size == 3) {
1942 if (format == AC_FETCH_FORMAT_FLOAT) {
1943 for (unsigned i = 0; i < num_channels; ++i) {
1944 tmp = ac_build_gather_values(ctx, &loads[2 * i], 2);
1945 loads[i] = LLVMBuildBitCast(ctx->builder, tmp, ctx->f64, "");
1946 }
1947 } else if (format == AC_FETCH_FORMAT_FIXED) {
1948 /* 10_11_11_FLOAT */
1949 LLVMValueRef data = loads[0];
1950 LLVMValueRef i32_2047 = LLVMConstInt(ctx->i32, 2047, false);
1951 LLVMValueRef r = LLVMBuildAnd(ctx->builder, data, i32_2047, "");
1952 tmp = LLVMBuildLShr(ctx->builder, data, LLVMConstInt(ctx->i32, 11, false), "");
1953 LLVMValueRef g = LLVMBuildAnd(ctx->builder, tmp, i32_2047, "");
1954 LLVMValueRef b = LLVMBuildLShr(ctx->builder, data, LLVMConstInt(ctx->i32, 22, false), "");
1955
1956 loads[0] = ac_to_integer(ctx, ac_ufN_to_float(ctx, r, 5, 6));
1957 loads[1] = ac_to_integer(ctx, ac_ufN_to_float(ctx, g, 5, 6));
1958 loads[2] = ac_to_integer(ctx, ac_ufN_to_float(ctx, b, 5, 5));
1959
1960 num_channels = 3;
1961 log_size = 2;
1962 format = AC_FETCH_FORMAT_FLOAT;
1963 } else {
1964 /* 2_10_10_10 data formats */
1965 LLVMValueRef data = loads[0];
1966 LLVMTypeRef i10 = LLVMIntTypeInContext(ctx->context, 10);
1967 LLVMTypeRef i2 = LLVMIntTypeInContext(ctx->context, 2);
1968 loads[0] = LLVMBuildTrunc(ctx->builder, data, i10, "");
1969 tmp = LLVMBuildLShr(ctx->builder, data, LLVMConstInt(ctx->i32, 10, false), "");
1970 loads[1] = LLVMBuildTrunc(ctx->builder, tmp, i10, "");
1971 tmp = LLVMBuildLShr(ctx->builder, data, LLVMConstInt(ctx->i32, 20, false), "");
1972 loads[2] = LLVMBuildTrunc(ctx->builder, tmp, i10, "");
1973 tmp = LLVMBuildLShr(ctx->builder, data, LLVMConstInt(ctx->i32, 30, false), "");
1974 loads[3] = LLVMBuildTrunc(ctx->builder, tmp, i2, "");
1975
1976 num_channels = 4;
1977 }
1978 }
1979
1980 if (format == AC_FETCH_FORMAT_FLOAT) {
1981 if (log_size != 2) {
1982 for (unsigned chan = 0; chan < num_channels; ++chan) {
1983 tmp = ac_to_float(ctx, loads[chan]);
1984 if (log_size == 3)
1985 tmp = LLVMBuildFPTrunc(ctx->builder, tmp, ctx->f32, "");
1986 else if (log_size == 1)
1987 tmp = LLVMBuildFPExt(ctx->builder, tmp, ctx->f32, "");
1988 loads[chan] = ac_to_integer(ctx, tmp);
1989 }
1990 }
1991 } else if (format == AC_FETCH_FORMAT_UINT) {
1992 if (log_size != 2) {
1993 for (unsigned chan = 0; chan < num_channels; ++chan)
1994 loads[chan] = LLVMBuildZExt(ctx->builder, loads[chan], ctx->i32, "");
1995 }
1996 } else if (format == AC_FETCH_FORMAT_SINT) {
1997 if (log_size != 2) {
1998 for (unsigned chan = 0; chan < num_channels; ++chan)
1999 loads[chan] = LLVMBuildSExt(ctx->builder, loads[chan], ctx->i32, "");
2000 }
2001 } else {
2002 bool unsign = format == AC_FETCH_FORMAT_UNORM ||
2003 format == AC_FETCH_FORMAT_USCALED ||
2004 format == AC_FETCH_FORMAT_UINT;
2005
2006 for (unsigned chan = 0; chan < num_channels; ++chan) {
2007 if (unsign) {
2008 tmp = LLVMBuildUIToFP(ctx->builder, loads[chan], ctx->f32, "");
2009 } else {
2010 tmp = LLVMBuildSIToFP(ctx->builder, loads[chan], ctx->f32, "");
2011 }
2012
2013 LLVMValueRef scale = NULL;
2014 if (format == AC_FETCH_FORMAT_FIXED) {
2015 assert(log_size == 2);
2016 scale = LLVMConstReal(ctx->f32, 1.0 / 0x10000);
2017 } else if (format == AC_FETCH_FORMAT_UNORM) {
2018 unsigned bits = LLVMGetIntTypeWidth(LLVMTypeOf(loads[chan]));
2019 scale = LLVMConstReal(ctx->f32, 1.0 / (((uint64_t)1 << bits) - 1));
2020 } else if (format == AC_FETCH_FORMAT_SNORM) {
2021 unsigned bits = LLVMGetIntTypeWidth(LLVMTypeOf(loads[chan]));
2022 scale = LLVMConstReal(ctx->f32, 1.0 / (((uint64_t)1 << (bits - 1)) - 1));
2023 }
2024 if (scale)
2025 tmp = LLVMBuildFMul(ctx->builder, tmp, scale, "");
2026
2027 if (format == AC_FETCH_FORMAT_SNORM) {
2028 /* Clamp to [-1, 1] */
2029 LLVMValueRef neg_one = LLVMConstReal(ctx->f32, -1.0);
2030 LLVMValueRef clamp =
2031 LLVMBuildFCmp(ctx->builder, LLVMRealULT, tmp, neg_one, "");
2032 tmp = LLVMBuildSelect(ctx->builder, clamp, neg_one, tmp, "");
2033 }
2034
2035 loads[chan] = ac_to_integer(ctx, tmp);
2036 }
2037 }
2038
2039 while (num_channels < 4) {
2040 if (format == AC_FETCH_FORMAT_UINT || format == AC_FETCH_FORMAT_SINT) {
2041 loads[num_channels] = num_channels == 3 ? ctx->i32_1 : ctx->i32_0;
2042 } else {
2043 loads[num_channels] = ac_to_integer(ctx, num_channels == 3 ? ctx->f32_1 : ctx->f32_0);
2044 }
2045 num_channels++;
2046 }
2047
2048 if (reverse) {
2049 tmp = loads[0];
2050 loads[0] = loads[2];
2051 loads[2] = tmp;
2052 }
2053
2054 return ac_build_gather_values(ctx, loads, 4);
2055 }
2056
2057 static void
2058 ac_build_llvm8_tbuffer_store(struct ac_llvm_context *ctx,
2059 LLVMValueRef rsrc,
2060 LLVMValueRef vdata,
2061 LLVMValueRef vindex,
2062 LLVMValueRef voffset,
2063 LLVMValueRef soffset,
2064 unsigned num_channels,
2065 unsigned dfmt,
2066 unsigned nfmt,
2067 unsigned cache_policy,
2068 bool structurized)
2069 {
2070 LLVMValueRef args[7];
2071 int idx = 0;
2072 args[idx++] = vdata;
2073 args[idx++] = LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, "");
2074 if (structurized)
2075 args[idx++] = vindex ? vindex : ctx->i32_0;
2076 args[idx++] = voffset ? voffset : ctx->i32_0;
2077 args[idx++] = soffset ? soffset : ctx->i32_0;
2078 args[idx++] = LLVMConstInt(ctx->i32, ac_get_tbuffer_format(ctx, dfmt, nfmt), 0);
2079 args[idx++] = LLVMConstInt(ctx->i32, cache_policy, 0);
2080 unsigned func = !ac_has_vec3_support(ctx->chip_class, true) && num_channels == 3 ? 4 : num_channels;
2081 const char *indexing_kind = structurized ? "struct" : "raw";
2082 char name[256], type_name[8];
2083
2084 LLVMTypeRef type = func > 1 ? LLVMVectorType(ctx->i32, func) : ctx->i32;
2085 ac_build_type_name_for_intr(type, type_name, sizeof(type_name));
2086
2087 snprintf(name, sizeof(name), "llvm.amdgcn.%s.tbuffer.store.%s",
2088 indexing_kind, type_name);
2089
2090 ac_build_intrinsic(ctx, name, ctx->voidt, args, idx,
2091 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY);
2092 }
2093
2094 static void
2095 ac_build_tbuffer_store(struct ac_llvm_context *ctx,
2096 LLVMValueRef rsrc,
2097 LLVMValueRef vdata,
2098 LLVMValueRef vindex,
2099 LLVMValueRef voffset,
2100 LLVMValueRef soffset,
2101 LLVMValueRef immoffset,
2102 unsigned num_channels,
2103 unsigned dfmt,
2104 unsigned nfmt,
2105 unsigned cache_policy,
2106 bool structurized) /* only matters for LLVM 8+ */
2107 {
2108 if (HAVE_LLVM >= 0x800) {
2109 voffset = LLVMBuildAdd(ctx->builder,
2110 voffset ? voffset : ctx->i32_0,
2111 immoffset, "");
2112
2113 ac_build_llvm8_tbuffer_store(ctx, rsrc, vdata, vindex, voffset,
2114 soffset, num_channels, dfmt, nfmt,
2115 cache_policy, structurized);
2116 } else {
2117 LLVMValueRef params[] = {
2118 vdata,
2119 rsrc,
2120 vindex ? vindex : ctx->i32_0,
2121 voffset ? voffset : ctx->i32_0,
2122 soffset ? soffset : ctx->i32_0,
2123 immoffset,
2124 LLVMConstInt(ctx->i32, dfmt, false),
2125 LLVMConstInt(ctx->i32, nfmt, false),
2126 LLVMConstInt(ctx->i1, !!(cache_policy & ac_glc), false),
2127 LLVMConstInt(ctx->i1, !!(cache_policy & ac_slc), false),
2128 };
2129 unsigned func = CLAMP(num_channels, 1, 3) - 1;
2130 const char *type_names[] = {"i32", "v2i32", "v4i32"};
2131 char name[256];
2132
2133 snprintf(name, sizeof(name), "llvm.amdgcn.tbuffer.store.%s",
2134 type_names[func]);
2135
2136 ac_build_intrinsic(ctx, name, ctx->voidt, params, 10,
2137 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY);
2138 }
2139 }
2140
2141 void
2142 ac_build_struct_tbuffer_store(struct ac_llvm_context *ctx,
2143 LLVMValueRef rsrc,
2144 LLVMValueRef vdata,
2145 LLVMValueRef vindex,
2146 LLVMValueRef voffset,
2147 LLVMValueRef soffset,
2148 LLVMValueRef immoffset,
2149 unsigned num_channels,
2150 unsigned dfmt,
2151 unsigned nfmt,
2152 unsigned cache_policy)
2153 {
2154 ac_build_tbuffer_store(ctx, rsrc, vdata, vindex, voffset, soffset,
2155 immoffset, num_channels, dfmt, nfmt, cache_policy,
2156 true);
2157 }
2158
2159 void
2160 ac_build_raw_tbuffer_store(struct ac_llvm_context *ctx,
2161 LLVMValueRef rsrc,
2162 LLVMValueRef vdata,
2163 LLVMValueRef voffset,
2164 LLVMValueRef soffset,
2165 LLVMValueRef immoffset,
2166 unsigned num_channels,
2167 unsigned dfmt,
2168 unsigned nfmt,
2169 unsigned cache_policy)
2170 {
2171 ac_build_tbuffer_store(ctx, rsrc, vdata, NULL, voffset, soffset,
2172 immoffset, num_channels, dfmt, nfmt, cache_policy,
2173 false);
2174 }
2175
2176 void
2177 ac_build_tbuffer_store_short(struct ac_llvm_context *ctx,
2178 LLVMValueRef rsrc,
2179 LLVMValueRef vdata,
2180 LLVMValueRef voffset,
2181 LLVMValueRef soffset,
2182 unsigned cache_policy)
2183 {
2184 vdata = LLVMBuildBitCast(ctx->builder, vdata, ctx->i16, "");
2185
2186 if (HAVE_LLVM >= 0x900) {
2187 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
2188 ac_build_llvm8_buffer_store_common(ctx, rsrc, vdata, NULL,
2189 voffset, soffset, 1,
2190 ctx->i16, cache_policy,
2191 false, false);
2192 } else {
2193 unsigned dfmt = V_008F0C_BUF_DATA_FORMAT_16;
2194 unsigned nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
2195
2196 vdata = LLVMBuildZExt(ctx->builder, vdata, ctx->i32, "");
2197
2198 ac_build_raw_tbuffer_store(ctx, rsrc, vdata, voffset, soffset,
2199 ctx->i32_0, 1, dfmt, nfmt, cache_policy);
2200 }
2201 }
2202
2203 void
2204 ac_build_tbuffer_store_byte(struct ac_llvm_context *ctx,
2205 LLVMValueRef rsrc,
2206 LLVMValueRef vdata,
2207 LLVMValueRef voffset,
2208 LLVMValueRef soffset,
2209 unsigned cache_policy)
2210 {
2211 vdata = LLVMBuildBitCast(ctx->builder, vdata, ctx->i8, "");
2212
2213 if (HAVE_LLVM >= 0x900) {
2214 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
2215 ac_build_llvm8_buffer_store_common(ctx, rsrc, vdata, NULL,
2216 voffset, soffset, 1,
2217 ctx->i8, cache_policy,
2218 false, false);
2219 } else {
2220 unsigned dfmt = V_008F0C_BUF_DATA_FORMAT_8;
2221 unsigned nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
2222
2223 vdata = LLVMBuildZExt(ctx->builder, vdata, ctx->i32, "");
2224
2225 ac_build_raw_tbuffer_store(ctx, rsrc, vdata, voffset, soffset,
2226 ctx->i32_0, 1, dfmt, nfmt, cache_policy);
2227 }
2228 }
2229 /**
2230 * Set range metadata on an instruction. This can only be used on load and
2231 * call instructions. If you know an instruction can only produce the values
2232 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
2233 * \p lo is the minimum value inclusive.
2234 * \p hi is the maximum value exclusive.
2235 */
2236 static void set_range_metadata(struct ac_llvm_context *ctx,
2237 LLVMValueRef value, unsigned lo, unsigned hi)
2238 {
2239 LLVMValueRef range_md, md_args[2];
2240 LLVMTypeRef type = LLVMTypeOf(value);
2241 LLVMContextRef context = LLVMGetTypeContext(type);
2242
2243 md_args[0] = LLVMConstInt(type, lo, false);
2244 md_args[1] = LLVMConstInt(type, hi, false);
2245 range_md = LLVMMDNodeInContext(context, md_args, 2);
2246 LLVMSetMetadata(value, ctx->range_md_kind, range_md);
2247 }
2248
2249 LLVMValueRef
2250 ac_get_thread_id(struct ac_llvm_context *ctx)
2251 {
2252 LLVMValueRef tid;
2253
2254 LLVMValueRef tid_args[2];
2255 tid_args[0] = LLVMConstInt(ctx->i32, 0xffffffff, false);
2256 tid_args[1] = ctx->i32_0;
2257 tid_args[1] = ac_build_intrinsic(ctx,
2258 "llvm.amdgcn.mbcnt.lo", ctx->i32,
2259 tid_args, 2, AC_FUNC_ATTR_READNONE);
2260
2261 if (ctx->wave_size == 32) {
2262 tid = tid_args[1];
2263 } else {
2264 tid = ac_build_intrinsic(ctx, "llvm.amdgcn.mbcnt.hi",
2265 ctx->i32, tid_args,
2266 2, AC_FUNC_ATTR_READNONE);
2267 }
2268 set_range_metadata(ctx, tid, 0, ctx->wave_size);
2269 return tid;
2270 }
2271
2272 /*
2273 * AMD GCN implements derivatives using the local data store (LDS)
2274 * All writes to the LDS happen in all executing threads at
2275 * the same time. TID is the Thread ID for the current
2276 * thread and is a value between 0 and 63, representing
2277 * the thread's position in the wavefront.
2278 *
2279 * For the pixel shader threads are grouped into quads of four pixels.
2280 * The TIDs of the pixels of a quad are:
2281 *
2282 * +------+------+
2283 * |4n + 0|4n + 1|
2284 * +------+------+
2285 * |4n + 2|4n + 3|
2286 * +------+------+
2287 *
2288 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
2289 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
2290 * the current pixel's column, and masking with 0xfffffffe yields the TID
2291 * of the left pixel of the current pixel's row.
2292 *
2293 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
2294 * adding 2 yields the TID of the pixel below the top pixel.
2295 */
2296 LLVMValueRef
2297 ac_build_ddxy(struct ac_llvm_context *ctx,
2298 uint32_t mask,
2299 int idx,
2300 LLVMValueRef val)
2301 {
2302 unsigned tl_lanes[4], trbl_lanes[4];
2303 char name[32], type[8];
2304 LLVMValueRef tl, trbl;
2305 LLVMTypeRef result_type;
2306 LLVMValueRef result;
2307
2308 result_type = ac_to_float_type(ctx, LLVMTypeOf(val));
2309
2310 if (result_type == ctx->f16)
2311 val = LLVMBuildZExt(ctx->builder, val, ctx->i32, "");
2312
2313 for (unsigned i = 0; i < 4; ++i) {
2314 tl_lanes[i] = i & mask;
2315 trbl_lanes[i] = (i & mask) + idx;
2316 }
2317
2318 tl = ac_build_quad_swizzle(ctx, val,
2319 tl_lanes[0], tl_lanes[1],
2320 tl_lanes[2], tl_lanes[3]);
2321 trbl = ac_build_quad_swizzle(ctx, val,
2322 trbl_lanes[0], trbl_lanes[1],
2323 trbl_lanes[2], trbl_lanes[3]);
2324
2325 if (result_type == ctx->f16) {
2326 tl = LLVMBuildTrunc(ctx->builder, tl, ctx->i16, "");
2327 trbl = LLVMBuildTrunc(ctx->builder, trbl, ctx->i16, "");
2328 }
2329
2330 tl = LLVMBuildBitCast(ctx->builder, tl, result_type, "");
2331 trbl = LLVMBuildBitCast(ctx->builder, trbl, result_type, "");
2332 result = LLVMBuildFSub(ctx->builder, trbl, tl, "");
2333
2334 ac_build_type_name_for_intr(result_type, type, sizeof(type));
2335 snprintf(name, sizeof(name), "llvm.amdgcn.wqm.%s", type);
2336
2337 return ac_build_intrinsic(ctx, name, result_type, &result, 1, 0);
2338 }
2339
2340 void
2341 ac_build_sendmsg(struct ac_llvm_context *ctx,
2342 uint32_t msg,
2343 LLVMValueRef wave_id)
2344 {
2345 LLVMValueRef args[2];
2346 args[0] = LLVMConstInt(ctx->i32, msg, false);
2347 args[1] = wave_id;
2348 ac_build_intrinsic(ctx, "llvm.amdgcn.s.sendmsg", ctx->voidt, args, 2, 0);
2349 }
2350
2351 LLVMValueRef
2352 ac_build_imsb(struct ac_llvm_context *ctx,
2353 LLVMValueRef arg,
2354 LLVMTypeRef dst_type)
2355 {
2356 LLVMValueRef msb = ac_build_intrinsic(ctx, "llvm.amdgcn.sffbh.i32",
2357 dst_type, &arg, 1,
2358 AC_FUNC_ATTR_READNONE);
2359
2360 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
2361 * the index from LSB. Invert it by doing "31 - msb". */
2362 msb = LLVMBuildSub(ctx->builder, LLVMConstInt(ctx->i32, 31, false),
2363 msb, "");
2364
2365 LLVMValueRef all_ones = LLVMConstInt(ctx->i32, -1, true);
2366 LLVMValueRef cond = LLVMBuildOr(ctx->builder,
2367 LLVMBuildICmp(ctx->builder, LLVMIntEQ,
2368 arg, ctx->i32_0, ""),
2369 LLVMBuildICmp(ctx->builder, LLVMIntEQ,
2370 arg, all_ones, ""), "");
2371
2372 return LLVMBuildSelect(ctx->builder, cond, all_ones, msb, "");
2373 }
2374
2375 LLVMValueRef
2376 ac_build_umsb(struct ac_llvm_context *ctx,
2377 LLVMValueRef arg,
2378 LLVMTypeRef dst_type)
2379 {
2380 const char *intrin_name;
2381 LLVMTypeRef type;
2382 LLVMValueRef highest_bit;
2383 LLVMValueRef zero;
2384 unsigned bitsize;
2385
2386 bitsize = ac_get_elem_bits(ctx, LLVMTypeOf(arg));
2387 switch (bitsize) {
2388 case 64:
2389 intrin_name = "llvm.ctlz.i64";
2390 type = ctx->i64;
2391 highest_bit = LLVMConstInt(ctx->i64, 63, false);
2392 zero = ctx->i64_0;
2393 break;
2394 case 32:
2395 intrin_name = "llvm.ctlz.i32";
2396 type = ctx->i32;
2397 highest_bit = LLVMConstInt(ctx->i32, 31, false);
2398 zero = ctx->i32_0;
2399 break;
2400 case 16:
2401 intrin_name = "llvm.ctlz.i16";
2402 type = ctx->i16;
2403 highest_bit = LLVMConstInt(ctx->i16, 15, false);
2404 zero = ctx->i16_0;
2405 break;
2406 case 8:
2407 intrin_name = "llvm.ctlz.i8";
2408 type = ctx->i8;
2409 highest_bit = LLVMConstInt(ctx->i8, 7, false);
2410 zero = ctx->i8_0;
2411 break;
2412 default:
2413 unreachable(!"invalid bitsize");
2414 break;
2415 }
2416
2417 LLVMValueRef params[2] = {
2418 arg,
2419 ctx->i1true,
2420 };
2421
2422 LLVMValueRef msb = ac_build_intrinsic(ctx, intrin_name, type,
2423 params, 2,
2424 AC_FUNC_ATTR_READNONE);
2425
2426 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
2427 * the index from LSB. Invert it by doing "31 - msb". */
2428 msb = LLVMBuildSub(ctx->builder, highest_bit, msb, "");
2429
2430 if (bitsize == 64) {
2431 msb = LLVMBuildTrunc(ctx->builder, msb, ctx->i32, "");
2432 } else if (bitsize < 32) {
2433 msb = LLVMBuildSExt(ctx->builder, msb, ctx->i32, "");
2434 }
2435
2436 /* check for zero */
2437 return LLVMBuildSelect(ctx->builder,
2438 LLVMBuildICmp(ctx->builder, LLVMIntEQ, arg, zero, ""),
2439 LLVMConstInt(ctx->i32, -1, true), msb, "");
2440 }
2441
2442 LLVMValueRef ac_build_fmin(struct ac_llvm_context *ctx, LLVMValueRef a,
2443 LLVMValueRef b)
2444 {
2445 char name[64];
2446 snprintf(name, sizeof(name), "llvm.minnum.f%d", ac_get_elem_bits(ctx, LLVMTypeOf(a)));
2447 LLVMValueRef args[2] = {a, b};
2448 return ac_build_intrinsic(ctx, name, LLVMTypeOf(a), args, 2,
2449 AC_FUNC_ATTR_READNONE);
2450 }
2451
2452 LLVMValueRef ac_build_fmax(struct ac_llvm_context *ctx, LLVMValueRef a,
2453 LLVMValueRef b)
2454 {
2455 char name[64];
2456 snprintf(name, sizeof(name), "llvm.maxnum.f%d", ac_get_elem_bits(ctx, LLVMTypeOf(a)));
2457 LLVMValueRef args[2] = {a, b};
2458 return ac_build_intrinsic(ctx, name, LLVMTypeOf(a), args, 2,
2459 AC_FUNC_ATTR_READNONE);
2460 }
2461
2462 LLVMValueRef ac_build_imin(struct ac_llvm_context *ctx, LLVMValueRef a,
2463 LLVMValueRef b)
2464 {
2465 LLVMValueRef cmp = LLVMBuildICmp(ctx->builder, LLVMIntSLE, a, b, "");
2466 return LLVMBuildSelect(ctx->builder, cmp, a, b, "");
2467 }
2468
2469 LLVMValueRef ac_build_imax(struct ac_llvm_context *ctx, LLVMValueRef a,
2470 LLVMValueRef b)
2471 {
2472 LLVMValueRef cmp = LLVMBuildICmp(ctx->builder, LLVMIntSGT, a, b, "");
2473 return LLVMBuildSelect(ctx->builder, cmp, a, b, "");
2474 }
2475
2476 LLVMValueRef ac_build_umin(struct ac_llvm_context *ctx, LLVMValueRef a,
2477 LLVMValueRef b)
2478 {
2479 LLVMValueRef cmp = LLVMBuildICmp(ctx->builder, LLVMIntULE, a, b, "");
2480 return LLVMBuildSelect(ctx->builder, cmp, a, b, "");
2481 }
2482
2483 LLVMValueRef ac_build_umax(struct ac_llvm_context *ctx, LLVMValueRef a,
2484 LLVMValueRef b)
2485 {
2486 LLVMValueRef cmp = LLVMBuildICmp(ctx->builder, LLVMIntUGE, a, b, "");
2487 return LLVMBuildSelect(ctx->builder, cmp, a, b, "");
2488 }
2489
2490 LLVMValueRef ac_build_clamp(struct ac_llvm_context *ctx, LLVMValueRef value)
2491 {
2492 LLVMTypeRef t = LLVMTypeOf(value);
2493 return ac_build_fmin(ctx, ac_build_fmax(ctx, value, LLVMConstReal(t, 0.0)),
2494 LLVMConstReal(t, 1.0));
2495 }
2496
2497 void ac_build_export(struct ac_llvm_context *ctx, struct ac_export_args *a)
2498 {
2499 LLVMValueRef args[9];
2500
2501 args[0] = LLVMConstInt(ctx->i32, a->target, 0);
2502 args[1] = LLVMConstInt(ctx->i32, a->enabled_channels, 0);
2503
2504 if (a->compr) {
2505 LLVMTypeRef i16 = LLVMInt16TypeInContext(ctx->context);
2506 LLVMTypeRef v2i16 = LLVMVectorType(i16, 2);
2507
2508 args[2] = LLVMBuildBitCast(ctx->builder, a->out[0],
2509 v2i16, "");
2510 args[3] = LLVMBuildBitCast(ctx->builder, a->out[1],
2511 v2i16, "");
2512 args[4] = LLVMConstInt(ctx->i1, a->done, 0);
2513 args[5] = LLVMConstInt(ctx->i1, a->valid_mask, 0);
2514
2515 ac_build_intrinsic(ctx, "llvm.amdgcn.exp.compr.v2i16",
2516 ctx->voidt, args, 6, 0);
2517 } else {
2518 args[2] = a->out[0];
2519 args[3] = a->out[1];
2520 args[4] = a->out[2];
2521 args[5] = a->out[3];
2522 args[6] = LLVMConstInt(ctx->i1, a->done, 0);
2523 args[7] = LLVMConstInt(ctx->i1, a->valid_mask, 0);
2524
2525 ac_build_intrinsic(ctx, "llvm.amdgcn.exp.f32",
2526 ctx->voidt, args, 8, 0);
2527 }
2528 }
2529
2530 void ac_build_export_null(struct ac_llvm_context *ctx)
2531 {
2532 struct ac_export_args args;
2533
2534 args.enabled_channels = 0x0; /* enabled channels */
2535 args.valid_mask = 1; /* whether the EXEC mask is valid */
2536 args.done = 1; /* DONE bit */
2537 args.target = V_008DFC_SQ_EXP_NULL;
2538 args.compr = 0; /* COMPR flag (0 = 32-bit export) */
2539 args.out[0] = LLVMGetUndef(ctx->f32); /* R */
2540 args.out[1] = LLVMGetUndef(ctx->f32); /* G */
2541 args.out[2] = LLVMGetUndef(ctx->f32); /* B */
2542 args.out[3] = LLVMGetUndef(ctx->f32); /* A */
2543
2544 ac_build_export(ctx, &args);
2545 }
2546
2547 static unsigned ac_num_coords(enum ac_image_dim dim)
2548 {
2549 switch (dim) {
2550 case ac_image_1d:
2551 return 1;
2552 case ac_image_2d:
2553 case ac_image_1darray:
2554 return 2;
2555 case ac_image_3d:
2556 case ac_image_cube:
2557 case ac_image_2darray:
2558 case ac_image_2dmsaa:
2559 return 3;
2560 case ac_image_2darraymsaa:
2561 return 4;
2562 default:
2563 unreachable("ac_num_coords: bad dim");
2564 }
2565 }
2566
2567 static unsigned ac_num_derivs(enum ac_image_dim dim)
2568 {
2569 switch (dim) {
2570 case ac_image_1d:
2571 case ac_image_1darray:
2572 return 2;
2573 case ac_image_2d:
2574 case ac_image_2darray:
2575 case ac_image_cube:
2576 return 4;
2577 case ac_image_3d:
2578 return 6;
2579 case ac_image_2dmsaa:
2580 case ac_image_2darraymsaa:
2581 default:
2582 unreachable("derivatives not supported");
2583 }
2584 }
2585
2586 static const char *get_atomic_name(enum ac_atomic_op op)
2587 {
2588 switch (op) {
2589 case ac_atomic_swap: return "swap";
2590 case ac_atomic_add: return "add";
2591 case ac_atomic_sub: return "sub";
2592 case ac_atomic_smin: return "smin";
2593 case ac_atomic_umin: return "umin";
2594 case ac_atomic_smax: return "smax";
2595 case ac_atomic_umax: return "umax";
2596 case ac_atomic_and: return "and";
2597 case ac_atomic_or: return "or";
2598 case ac_atomic_xor: return "xor";
2599 case ac_atomic_inc_wrap: return "inc";
2600 case ac_atomic_dec_wrap: return "dec";
2601 }
2602 unreachable("bad atomic op");
2603 }
2604
2605 LLVMValueRef ac_build_image_opcode(struct ac_llvm_context *ctx,
2606 struct ac_image_args *a)
2607 {
2608 const char *overload[3] = { "", "", "" };
2609 unsigned num_overloads = 0;
2610 LLVMValueRef args[18];
2611 unsigned num_args = 0;
2612 enum ac_image_dim dim = a->dim;
2613
2614 assert(!a->lod || a->lod == ctx->i32_0 || a->lod == ctx->f32_0 ||
2615 !a->level_zero);
2616 assert((a->opcode != ac_image_get_resinfo && a->opcode != ac_image_load_mip &&
2617 a->opcode != ac_image_store_mip) ||
2618 a->lod);
2619 assert(a->opcode == ac_image_sample || a->opcode == ac_image_gather4 ||
2620 (!a->compare && !a->offset));
2621 assert((a->opcode == ac_image_sample || a->opcode == ac_image_gather4 ||
2622 a->opcode == ac_image_get_lod) ||
2623 !a->bias);
2624 assert((a->bias ? 1 : 0) +
2625 (a->lod ? 1 : 0) +
2626 (a->level_zero ? 1 : 0) +
2627 (a->derivs[0] ? 1 : 0) <= 1);
2628
2629 if (a->opcode == ac_image_get_lod) {
2630 switch (dim) {
2631 case ac_image_1darray:
2632 dim = ac_image_1d;
2633 break;
2634 case ac_image_2darray:
2635 case ac_image_cube:
2636 dim = ac_image_2d;
2637 break;
2638 default:
2639 break;
2640 }
2641 }
2642
2643 bool sample = a->opcode == ac_image_sample ||
2644 a->opcode == ac_image_gather4 ||
2645 a->opcode == ac_image_get_lod;
2646 bool atomic = a->opcode == ac_image_atomic ||
2647 a->opcode == ac_image_atomic_cmpswap;
2648 bool load = a->opcode == ac_image_sample ||
2649 a->opcode == ac_image_gather4 ||
2650 a->opcode == ac_image_load ||
2651 a->opcode == ac_image_load_mip;
2652 LLVMTypeRef coord_type = sample ? ctx->f32 : ctx->i32;
2653
2654 if (atomic || a->opcode == ac_image_store || a->opcode == ac_image_store_mip) {
2655 args[num_args++] = a->data[0];
2656 if (a->opcode == ac_image_atomic_cmpswap)
2657 args[num_args++] = a->data[1];
2658 }
2659
2660 if (!atomic)
2661 args[num_args++] = LLVMConstInt(ctx->i32, a->dmask, false);
2662
2663 if (a->offset)
2664 args[num_args++] = ac_to_integer(ctx, a->offset);
2665 if (a->bias) {
2666 args[num_args++] = ac_to_float(ctx, a->bias);
2667 overload[num_overloads++] = ".f32";
2668 }
2669 if (a->compare)
2670 args[num_args++] = ac_to_float(ctx, a->compare);
2671 if (a->derivs[0]) {
2672 unsigned count = ac_num_derivs(dim);
2673 for (unsigned i = 0; i < count; ++i)
2674 args[num_args++] = ac_to_float(ctx, a->derivs[i]);
2675 overload[num_overloads++] = ".f32";
2676 }
2677 unsigned num_coords =
2678 a->opcode != ac_image_get_resinfo ? ac_num_coords(dim) : 0;
2679 for (unsigned i = 0; i < num_coords; ++i)
2680 args[num_args++] = LLVMBuildBitCast(ctx->builder, a->coords[i], coord_type, "");
2681 if (a->lod)
2682 args[num_args++] = LLVMBuildBitCast(ctx->builder, a->lod, coord_type, "");
2683 overload[num_overloads++] = sample ? ".f32" : ".i32";
2684
2685 args[num_args++] = a->resource;
2686 if (sample) {
2687 args[num_args++] = a->sampler;
2688 args[num_args++] = LLVMConstInt(ctx->i1, a->unorm, false);
2689 }
2690
2691 args[num_args++] = ctx->i32_0; /* texfailctrl */
2692 args[num_args++] = LLVMConstInt(ctx->i32,
2693 load ? get_load_cache_policy(ctx, a->cache_policy) :
2694 a->cache_policy, false);
2695
2696 const char *name;
2697 const char *atomic_subop = "";
2698 switch (a->opcode) {
2699 case ac_image_sample: name = "sample"; break;
2700 case ac_image_gather4: name = "gather4"; break;
2701 case ac_image_load: name = "load"; break;
2702 case ac_image_load_mip: name = "load.mip"; break;
2703 case ac_image_store: name = "store"; break;
2704 case ac_image_store_mip: name = "store.mip"; break;
2705 case ac_image_atomic:
2706 name = "atomic.";
2707 atomic_subop = get_atomic_name(a->atomic);
2708 break;
2709 case ac_image_atomic_cmpswap:
2710 name = "atomic.";
2711 atomic_subop = "cmpswap";
2712 break;
2713 case ac_image_get_lod: name = "getlod"; break;
2714 case ac_image_get_resinfo: name = "getresinfo"; break;
2715 default: unreachable("invalid image opcode");
2716 }
2717
2718 const char *dimname;
2719 switch (dim) {
2720 case ac_image_1d: dimname = "1d"; break;
2721 case ac_image_2d: dimname = "2d"; break;
2722 case ac_image_3d: dimname = "3d"; break;
2723 case ac_image_cube: dimname = "cube"; break;
2724 case ac_image_1darray: dimname = "1darray"; break;
2725 case ac_image_2darray: dimname = "2darray"; break;
2726 case ac_image_2dmsaa: dimname = "2dmsaa"; break;
2727 case ac_image_2darraymsaa: dimname = "2darraymsaa"; break;
2728 default: unreachable("invalid dim");
2729 }
2730
2731 bool lod_suffix =
2732 a->lod && (a->opcode == ac_image_sample || a->opcode == ac_image_gather4);
2733 char intr_name[96];
2734 snprintf(intr_name, sizeof(intr_name),
2735 "llvm.amdgcn.image.%s%s" /* base name */
2736 "%s%s%s" /* sample/gather modifiers */
2737 ".%s.%s%s%s%s", /* dimension and type overloads */
2738 name, atomic_subop,
2739 a->compare ? ".c" : "",
2740 a->bias ? ".b" :
2741 lod_suffix ? ".l" :
2742 a->derivs[0] ? ".d" :
2743 a->level_zero ? ".lz" : "",
2744 a->offset ? ".o" : "",
2745 dimname,
2746 atomic ? "i32" : "v4f32",
2747 overload[0], overload[1], overload[2]);
2748
2749 LLVMTypeRef retty;
2750 if (atomic)
2751 retty = ctx->i32;
2752 else if (a->opcode == ac_image_store || a->opcode == ac_image_store_mip)
2753 retty = ctx->voidt;
2754 else
2755 retty = ctx->v4f32;
2756
2757 LLVMValueRef result =
2758 ac_build_intrinsic(ctx, intr_name, retty, args, num_args,
2759 a->attributes);
2760 if (!sample && retty == ctx->v4f32) {
2761 result = LLVMBuildBitCast(ctx->builder, result,
2762 ctx->v4i32, "");
2763 }
2764 return result;
2765 }
2766
2767 LLVMValueRef ac_build_cvt_pkrtz_f16(struct ac_llvm_context *ctx,
2768 LLVMValueRef args[2])
2769 {
2770 LLVMTypeRef v2f16 =
2771 LLVMVectorType(LLVMHalfTypeInContext(ctx->context), 2);
2772
2773 return ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pkrtz", v2f16,
2774 args, 2, AC_FUNC_ATTR_READNONE);
2775 }
2776
2777 LLVMValueRef ac_build_cvt_pknorm_i16(struct ac_llvm_context *ctx,
2778 LLVMValueRef args[2])
2779 {
2780 LLVMValueRef res =
2781 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pknorm.i16",
2782 ctx->v2i16, args, 2,
2783 AC_FUNC_ATTR_READNONE);
2784 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
2785 }
2786
2787 LLVMValueRef ac_build_cvt_pknorm_u16(struct ac_llvm_context *ctx,
2788 LLVMValueRef args[2])
2789 {
2790 LLVMValueRef res =
2791 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pknorm.u16",
2792 ctx->v2i16, args, 2,
2793 AC_FUNC_ATTR_READNONE);
2794 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
2795 }
2796
2797 /* The 8-bit and 10-bit clamping is for HW workarounds. */
2798 LLVMValueRef ac_build_cvt_pk_i16(struct ac_llvm_context *ctx,
2799 LLVMValueRef args[2], unsigned bits, bool hi)
2800 {
2801 assert(bits == 8 || bits == 10 || bits == 16);
2802
2803 LLVMValueRef max_rgb = LLVMConstInt(ctx->i32,
2804 bits == 8 ? 127 : bits == 10 ? 511 : 32767, 0);
2805 LLVMValueRef min_rgb = LLVMConstInt(ctx->i32,
2806 bits == 8 ? -128 : bits == 10 ? -512 : -32768, 0);
2807 LLVMValueRef max_alpha =
2808 bits != 10 ? max_rgb : ctx->i32_1;
2809 LLVMValueRef min_alpha =
2810 bits != 10 ? min_rgb : LLVMConstInt(ctx->i32, -2, 0);
2811
2812 /* Clamp. */
2813 if (bits != 16) {
2814 for (int i = 0; i < 2; i++) {
2815 bool alpha = hi && i == 1;
2816 args[i] = ac_build_imin(ctx, args[i],
2817 alpha ? max_alpha : max_rgb);
2818 args[i] = ac_build_imax(ctx, args[i],
2819 alpha ? min_alpha : min_rgb);
2820 }
2821 }
2822
2823 LLVMValueRef res =
2824 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pk.i16",
2825 ctx->v2i16, args, 2,
2826 AC_FUNC_ATTR_READNONE);
2827 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
2828 }
2829
2830 /* The 8-bit and 10-bit clamping is for HW workarounds. */
2831 LLVMValueRef ac_build_cvt_pk_u16(struct ac_llvm_context *ctx,
2832 LLVMValueRef args[2], unsigned bits, bool hi)
2833 {
2834 assert(bits == 8 || bits == 10 || bits == 16);
2835
2836 LLVMValueRef max_rgb = LLVMConstInt(ctx->i32,
2837 bits == 8 ? 255 : bits == 10 ? 1023 : 65535, 0);
2838 LLVMValueRef max_alpha =
2839 bits != 10 ? max_rgb : LLVMConstInt(ctx->i32, 3, 0);
2840
2841 /* Clamp. */
2842 if (bits != 16) {
2843 for (int i = 0; i < 2; i++) {
2844 bool alpha = hi && i == 1;
2845 args[i] = ac_build_umin(ctx, args[i],
2846 alpha ? max_alpha : max_rgb);
2847 }
2848 }
2849
2850 LLVMValueRef res =
2851 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pk.u16",
2852 ctx->v2i16, args, 2,
2853 AC_FUNC_ATTR_READNONE);
2854 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
2855 }
2856
2857 LLVMValueRef ac_build_wqm_vote(struct ac_llvm_context *ctx, LLVMValueRef i1)
2858 {
2859 return ac_build_intrinsic(ctx, "llvm.amdgcn.wqm.vote", ctx->i1,
2860 &i1, 1, AC_FUNC_ATTR_READNONE);
2861 }
2862
2863 void ac_build_kill_if_false(struct ac_llvm_context *ctx, LLVMValueRef i1)
2864 {
2865 ac_build_intrinsic(ctx, "llvm.amdgcn.kill", ctx->voidt,
2866 &i1, 1, 0);
2867 }
2868
2869 LLVMValueRef ac_build_bfe(struct ac_llvm_context *ctx, LLVMValueRef input,
2870 LLVMValueRef offset, LLVMValueRef width,
2871 bool is_signed)
2872 {
2873 LLVMValueRef args[] = {
2874 input,
2875 offset,
2876 width,
2877 };
2878
2879 LLVMValueRef result = ac_build_intrinsic(ctx,
2880 is_signed ? "llvm.amdgcn.sbfe.i32" :
2881 "llvm.amdgcn.ubfe.i32",
2882 ctx->i32, args, 3,
2883 AC_FUNC_ATTR_READNONE);
2884
2885 if (HAVE_LLVM < 0x0800) {
2886 /* FIXME: LLVM 7+ returns incorrect result when count is 0.
2887 * https://bugs.freedesktop.org/show_bug.cgi?id=107276
2888 */
2889 LLVMValueRef zero = ctx->i32_0;
2890 LLVMValueRef icond = LLVMBuildICmp(ctx->builder, LLVMIntEQ, width, zero, "");
2891 result = LLVMBuildSelect(ctx->builder, icond, zero, result, "");
2892 }
2893
2894 return result;
2895 }
2896
2897 LLVMValueRef ac_build_imad(struct ac_llvm_context *ctx, LLVMValueRef s0,
2898 LLVMValueRef s1, LLVMValueRef s2)
2899 {
2900 return LLVMBuildAdd(ctx->builder,
2901 LLVMBuildMul(ctx->builder, s0, s1, ""), s2, "");
2902 }
2903
2904 LLVMValueRef ac_build_fmad(struct ac_llvm_context *ctx, LLVMValueRef s0,
2905 LLVMValueRef s1, LLVMValueRef s2)
2906 {
2907 return LLVMBuildFAdd(ctx->builder,
2908 LLVMBuildFMul(ctx->builder, s0, s1, ""), s2, "");
2909 }
2910
2911 void ac_build_waitcnt(struct ac_llvm_context *ctx, unsigned wait_flags)
2912 {
2913 if (!wait_flags)
2914 return;
2915
2916 unsigned lgkmcnt = 63;
2917 unsigned vmcnt = ctx->chip_class >= GFX9 ? 63 : 15;
2918 unsigned vscnt = 63;
2919
2920 if (wait_flags & AC_WAIT_LGKM)
2921 lgkmcnt = 0;
2922 if (wait_flags & AC_WAIT_VLOAD)
2923 vmcnt = 0;
2924
2925 if (wait_flags & AC_WAIT_VSTORE) {
2926 if (ctx->chip_class >= GFX10)
2927 vscnt = 0;
2928 else
2929 vmcnt = 0;
2930 }
2931
2932 /* There is no intrinsic for vscnt(0), so use a fence. */
2933 if ((wait_flags & AC_WAIT_LGKM &&
2934 wait_flags & AC_WAIT_VLOAD &&
2935 wait_flags & AC_WAIT_VSTORE) ||
2936 vscnt == 0) {
2937 LLVMBuildFence(ctx->builder, LLVMAtomicOrderingRelease, false, "");
2938 return;
2939 }
2940
2941 unsigned simm16 = (lgkmcnt << 8) |
2942 (7 << 4) | /* expcnt */
2943 (vmcnt & 0xf) |
2944 ((vmcnt >> 4) << 14);
2945
2946 LLVMValueRef args[1] = {
2947 LLVMConstInt(ctx->i32, simm16, false),
2948 };
2949 ac_build_intrinsic(ctx, "llvm.amdgcn.s.waitcnt",
2950 ctx->voidt, args, 1, 0);
2951 }
2952
2953 LLVMValueRef ac_build_fmed3(struct ac_llvm_context *ctx, LLVMValueRef src0,
2954 LLVMValueRef src1, LLVMValueRef src2,
2955 unsigned bitsize)
2956 {
2957 LLVMTypeRef type;
2958 char *intr;
2959
2960 if (bitsize == 16) {
2961 intr = "llvm.amdgcn.fmed3.f16";
2962 type = ctx->f16;
2963 } else if (bitsize == 32) {
2964 intr = "llvm.amdgcn.fmed3.f32";
2965 type = ctx->f32;
2966 } else {
2967 intr = "llvm.amdgcn.fmed3.f64";
2968 type = ctx->f64;
2969 }
2970
2971 LLVMValueRef params[] = {
2972 src0,
2973 src1,
2974 src2,
2975 };
2976 return ac_build_intrinsic(ctx, intr, type, params, 3,
2977 AC_FUNC_ATTR_READNONE);
2978 }
2979
2980 LLVMValueRef ac_build_fract(struct ac_llvm_context *ctx, LLVMValueRef src0,
2981 unsigned bitsize)
2982 {
2983 LLVMTypeRef type;
2984 char *intr;
2985
2986 if (bitsize == 16) {
2987 intr = "llvm.amdgcn.fract.f16";
2988 type = ctx->f16;
2989 } else if (bitsize == 32) {
2990 intr = "llvm.amdgcn.fract.f32";
2991 type = ctx->f32;
2992 } else {
2993 intr = "llvm.amdgcn.fract.f64";
2994 type = ctx->f64;
2995 }
2996
2997 LLVMValueRef params[] = {
2998 src0,
2999 };
3000 return ac_build_intrinsic(ctx, intr, type, params, 1,
3001 AC_FUNC_ATTR_READNONE);
3002 }
3003
3004 LLVMValueRef ac_build_isign(struct ac_llvm_context *ctx, LLVMValueRef src0,
3005 unsigned bitsize)
3006 {
3007 LLVMTypeRef type = LLVMIntTypeInContext(ctx->context, bitsize);
3008 LLVMValueRef zero = LLVMConstInt(type, 0, false);
3009 LLVMValueRef one = LLVMConstInt(type, 1, false);
3010
3011 LLVMValueRef cmp, val;
3012 cmp = LLVMBuildICmp(ctx->builder, LLVMIntSGT, src0, zero, "");
3013 val = LLVMBuildSelect(ctx->builder, cmp, one, src0, "");
3014 cmp = LLVMBuildICmp(ctx->builder, LLVMIntSGE, val, zero, "");
3015 val = LLVMBuildSelect(ctx->builder, cmp, val, LLVMConstInt(type, -1, true), "");
3016 return val;
3017 }
3018
3019 LLVMValueRef ac_build_fsign(struct ac_llvm_context *ctx, LLVMValueRef src0,
3020 unsigned bitsize)
3021 {
3022 LLVMValueRef cmp, val, zero, one;
3023 LLVMTypeRef type;
3024
3025 if (bitsize == 16) {
3026 type = ctx->f16;
3027 zero = ctx->f16_0;
3028 one = ctx->f16_1;
3029 } else if (bitsize == 32) {
3030 type = ctx->f32;
3031 zero = ctx->f32_0;
3032 one = ctx->f32_1;
3033 } else {
3034 type = ctx->f64;
3035 zero = ctx->f64_0;
3036 one = ctx->f64_1;
3037 }
3038
3039 cmp = LLVMBuildFCmp(ctx->builder, LLVMRealOGT, src0, zero, "");
3040 val = LLVMBuildSelect(ctx->builder, cmp, one, src0, "");
3041 cmp = LLVMBuildFCmp(ctx->builder, LLVMRealOGE, val, zero, "");
3042 val = LLVMBuildSelect(ctx->builder, cmp, val, LLVMConstReal(type, -1.0), "");
3043 return val;
3044 }
3045
3046 LLVMValueRef ac_build_bit_count(struct ac_llvm_context *ctx, LLVMValueRef src0)
3047 {
3048 LLVMValueRef result;
3049 unsigned bitsize;
3050
3051 bitsize = ac_get_elem_bits(ctx, LLVMTypeOf(src0));
3052
3053 switch (bitsize) {
3054 case 64:
3055 result = ac_build_intrinsic(ctx, "llvm.ctpop.i64", ctx->i64,
3056 (LLVMValueRef []) { src0 }, 1,
3057 AC_FUNC_ATTR_READNONE);
3058
3059 result = LLVMBuildTrunc(ctx->builder, result, ctx->i32, "");
3060 break;
3061 case 32:
3062 result = ac_build_intrinsic(ctx, "llvm.ctpop.i32", ctx->i32,
3063 (LLVMValueRef []) { src0 }, 1,
3064 AC_FUNC_ATTR_READNONE);
3065 break;
3066 case 16:
3067 result = ac_build_intrinsic(ctx, "llvm.ctpop.i16", ctx->i16,
3068 (LLVMValueRef []) { src0 }, 1,
3069 AC_FUNC_ATTR_READNONE);
3070
3071 result = LLVMBuildZExt(ctx->builder, result, ctx->i32, "");
3072 break;
3073 case 8:
3074 result = ac_build_intrinsic(ctx, "llvm.ctpop.i8", ctx->i8,
3075 (LLVMValueRef []) { src0 }, 1,
3076 AC_FUNC_ATTR_READNONE);
3077
3078 result = LLVMBuildZExt(ctx->builder, result, ctx->i32, "");
3079 break;
3080 default:
3081 unreachable(!"invalid bitsize");
3082 break;
3083 }
3084
3085 return result;
3086 }
3087
3088 LLVMValueRef ac_build_bitfield_reverse(struct ac_llvm_context *ctx,
3089 LLVMValueRef src0)
3090 {
3091 LLVMValueRef result;
3092 unsigned bitsize;
3093
3094 bitsize = ac_get_elem_bits(ctx, LLVMTypeOf(src0));
3095
3096 switch (bitsize) {
3097 case 64:
3098 result = ac_build_intrinsic(ctx, "llvm.bitreverse.i64", ctx->i64,
3099 (LLVMValueRef []) { src0 }, 1,
3100 AC_FUNC_ATTR_READNONE);
3101
3102 result = LLVMBuildTrunc(ctx->builder, result, ctx->i32, "");
3103 break;
3104 case 32:
3105 result = ac_build_intrinsic(ctx, "llvm.bitreverse.i32", ctx->i32,
3106 (LLVMValueRef []) { src0 }, 1,
3107 AC_FUNC_ATTR_READNONE);
3108 break;
3109 case 16:
3110 result = ac_build_intrinsic(ctx, "llvm.bitreverse.i16", ctx->i16,
3111 (LLVMValueRef []) { src0 }, 1,
3112 AC_FUNC_ATTR_READNONE);
3113
3114 result = LLVMBuildZExt(ctx->builder, result, ctx->i32, "");
3115 break;
3116 case 8:
3117 result = ac_build_intrinsic(ctx, "llvm.bitreverse.i8", ctx->i8,
3118 (LLVMValueRef []) { src0 }, 1,
3119 AC_FUNC_ATTR_READNONE);
3120
3121 result = LLVMBuildZExt(ctx->builder, result, ctx->i32, "");
3122 break;
3123 default:
3124 unreachable(!"invalid bitsize");
3125 break;
3126 }
3127
3128 return result;
3129 }
3130
3131 #define AC_EXP_TARGET 0
3132 #define AC_EXP_ENABLED_CHANNELS 1
3133 #define AC_EXP_OUT0 2
3134
3135 enum ac_ir_type {
3136 AC_IR_UNDEF,
3137 AC_IR_CONST,
3138 AC_IR_VALUE,
3139 };
3140
3141 struct ac_vs_exp_chan
3142 {
3143 LLVMValueRef value;
3144 float const_float;
3145 enum ac_ir_type type;
3146 };
3147
3148 struct ac_vs_exp_inst {
3149 unsigned offset;
3150 LLVMValueRef inst;
3151 struct ac_vs_exp_chan chan[4];
3152 };
3153
3154 struct ac_vs_exports {
3155 unsigned num;
3156 struct ac_vs_exp_inst exp[VARYING_SLOT_MAX];
3157 };
3158
3159 /* Return true if the PARAM export has been eliminated. */
3160 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset,
3161 uint32_t num_outputs,
3162 struct ac_vs_exp_inst *exp)
3163 {
3164 unsigned i, default_val; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
3165 bool is_zero[4] = {}, is_one[4] = {};
3166
3167 for (i = 0; i < 4; i++) {
3168 /* It's a constant expression. Undef outputs are eliminated too. */
3169 if (exp->chan[i].type == AC_IR_UNDEF) {
3170 is_zero[i] = true;
3171 is_one[i] = true;
3172 } else if (exp->chan[i].type == AC_IR_CONST) {
3173 if (exp->chan[i].const_float == 0)
3174 is_zero[i] = true;
3175 else if (exp->chan[i].const_float == 1)
3176 is_one[i] = true;
3177 else
3178 return false; /* other constant */
3179 } else
3180 return false;
3181 }
3182
3183 /* Only certain combinations of 0 and 1 can be eliminated. */
3184 if (is_zero[0] && is_zero[1] && is_zero[2])
3185 default_val = is_zero[3] ? 0 : 1;
3186 else if (is_one[0] && is_one[1] && is_one[2])
3187 default_val = is_zero[3] ? 2 : 3;
3188 else
3189 return false;
3190
3191 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
3192 LLVMInstructionEraseFromParent(exp->inst);
3193
3194 /* Change OFFSET to DEFAULT_VAL. */
3195 for (i = 0; i < num_outputs; i++) {
3196 if (vs_output_param_offset[i] == exp->offset) {
3197 vs_output_param_offset[i] =
3198 AC_EXP_PARAM_DEFAULT_VAL_0000 + default_val;
3199 break;
3200 }
3201 }
3202 return true;
3203 }
3204
3205 static bool ac_eliminate_duplicated_output(struct ac_llvm_context *ctx,
3206 uint8_t *vs_output_param_offset,
3207 uint32_t num_outputs,
3208 struct ac_vs_exports *processed,
3209 struct ac_vs_exp_inst *exp)
3210 {
3211 unsigned p, copy_back_channels = 0;
3212
3213 /* See if the output is already in the list of processed outputs.
3214 * The LLVMValueRef comparison relies on SSA.
3215 */
3216 for (p = 0; p < processed->num; p++) {
3217 bool different = false;
3218
3219 for (unsigned j = 0; j < 4; j++) {
3220 struct ac_vs_exp_chan *c1 = &processed->exp[p].chan[j];
3221 struct ac_vs_exp_chan *c2 = &exp->chan[j];
3222
3223 /* Treat undef as a match. */
3224 if (c2->type == AC_IR_UNDEF)
3225 continue;
3226
3227 /* If c1 is undef but c2 isn't, we can copy c2 to c1
3228 * and consider the instruction duplicated.
3229 */
3230 if (c1->type == AC_IR_UNDEF) {
3231 copy_back_channels |= 1 << j;
3232 continue;
3233 }
3234
3235 /* Test whether the channels are not equal. */
3236 if (c1->type != c2->type ||
3237 (c1->type == AC_IR_CONST &&
3238 c1->const_float != c2->const_float) ||
3239 (c1->type == AC_IR_VALUE &&
3240 c1->value != c2->value)) {
3241 different = true;
3242 break;
3243 }
3244 }
3245 if (!different)
3246 break;
3247
3248 copy_back_channels = 0;
3249 }
3250 if (p == processed->num)
3251 return false;
3252
3253 /* If a match was found, but the matching export has undef where the new
3254 * one has a normal value, copy the normal value to the undef channel.
3255 */
3256 struct ac_vs_exp_inst *match = &processed->exp[p];
3257
3258 /* Get current enabled channels mask. */
3259 LLVMValueRef arg = LLVMGetOperand(match->inst, AC_EXP_ENABLED_CHANNELS);
3260 unsigned enabled_channels = LLVMConstIntGetZExtValue(arg);
3261
3262 while (copy_back_channels) {
3263 unsigned chan = u_bit_scan(&copy_back_channels);
3264
3265 assert(match->chan[chan].type == AC_IR_UNDEF);
3266 LLVMSetOperand(match->inst, AC_EXP_OUT0 + chan,
3267 exp->chan[chan].value);
3268 match->chan[chan] = exp->chan[chan];
3269
3270 /* Update number of enabled channels because the original mask
3271 * is not always 0xf.
3272 */
3273 enabled_channels |= (1 << chan);
3274 LLVMSetOperand(match->inst, AC_EXP_ENABLED_CHANNELS,
3275 LLVMConstInt(ctx->i32, enabled_channels, 0));
3276 }
3277
3278 /* The PARAM export is duplicated. Kill it. */
3279 LLVMInstructionEraseFromParent(exp->inst);
3280
3281 /* Change OFFSET to the matching export. */
3282 for (unsigned i = 0; i < num_outputs; i++) {
3283 if (vs_output_param_offset[i] == exp->offset) {
3284 vs_output_param_offset[i] = match->offset;
3285 break;
3286 }
3287 }
3288 return true;
3289 }
3290
3291 void ac_optimize_vs_outputs(struct ac_llvm_context *ctx,
3292 LLVMValueRef main_fn,
3293 uint8_t *vs_output_param_offset,
3294 uint32_t num_outputs,
3295 uint8_t *num_param_exports)
3296 {
3297 LLVMBasicBlockRef bb;
3298 bool removed_any = false;
3299 struct ac_vs_exports exports;
3300
3301 exports.num = 0;
3302
3303 /* Process all LLVM instructions. */
3304 bb = LLVMGetFirstBasicBlock(main_fn);
3305 while (bb) {
3306 LLVMValueRef inst = LLVMGetFirstInstruction(bb);
3307
3308 while (inst) {
3309 LLVMValueRef cur = inst;
3310 inst = LLVMGetNextInstruction(inst);
3311 struct ac_vs_exp_inst exp;
3312
3313 if (LLVMGetInstructionOpcode(cur) != LLVMCall)
3314 continue;
3315
3316 LLVMValueRef callee = ac_llvm_get_called_value(cur);
3317
3318 if (!ac_llvm_is_function(callee))
3319 continue;
3320
3321 const char *name = LLVMGetValueName(callee);
3322 unsigned num_args = LLVMCountParams(callee);
3323
3324 /* Check if this is an export instruction. */
3325 if ((num_args != 9 && num_args != 8) ||
3326 (strcmp(name, "llvm.SI.export") &&
3327 strcmp(name, "llvm.amdgcn.exp.f32")))
3328 continue;
3329
3330 LLVMValueRef arg = LLVMGetOperand(cur, AC_EXP_TARGET);
3331 unsigned target = LLVMConstIntGetZExtValue(arg);
3332
3333 if (target < V_008DFC_SQ_EXP_PARAM)
3334 continue;
3335
3336 target -= V_008DFC_SQ_EXP_PARAM;
3337
3338 /* Parse the instruction. */
3339 memset(&exp, 0, sizeof(exp));
3340 exp.offset = target;
3341 exp.inst = cur;
3342
3343 for (unsigned i = 0; i < 4; i++) {
3344 LLVMValueRef v = LLVMGetOperand(cur, AC_EXP_OUT0 + i);
3345
3346 exp.chan[i].value = v;
3347
3348 if (LLVMIsUndef(v)) {
3349 exp.chan[i].type = AC_IR_UNDEF;
3350 } else if (LLVMIsAConstantFP(v)) {
3351 LLVMBool loses_info;
3352 exp.chan[i].type = AC_IR_CONST;
3353 exp.chan[i].const_float =
3354 LLVMConstRealGetDouble(v, &loses_info);
3355 } else {
3356 exp.chan[i].type = AC_IR_VALUE;
3357 }
3358 }
3359
3360 /* Eliminate constant and duplicated PARAM exports. */
3361 if (ac_eliminate_const_output(vs_output_param_offset,
3362 num_outputs, &exp) ||
3363 ac_eliminate_duplicated_output(ctx,
3364 vs_output_param_offset,
3365 num_outputs, &exports,
3366 &exp)) {
3367 removed_any = true;
3368 } else {
3369 exports.exp[exports.num++] = exp;
3370 }
3371 }
3372 bb = LLVMGetNextBasicBlock(bb);
3373 }
3374
3375 /* Remove holes in export memory due to removed PARAM exports.
3376 * This is done by renumbering all PARAM exports.
3377 */
3378 if (removed_any) {
3379 uint8_t old_offset[VARYING_SLOT_MAX];
3380 unsigned out, i;
3381
3382 /* Make a copy of the offsets. We need the old version while
3383 * we are modifying some of them. */
3384 memcpy(old_offset, vs_output_param_offset,
3385 sizeof(old_offset));
3386
3387 for (i = 0; i < exports.num; i++) {
3388 unsigned offset = exports.exp[i].offset;
3389
3390 /* Update vs_output_param_offset. Multiple outputs can
3391 * have the same offset.
3392 */
3393 for (out = 0; out < num_outputs; out++) {
3394 if (old_offset