blob: 949f181aace05c0517b2f4d0a8fa0b2316a14fca [file] [log] [blame]
Dave Airlie13a28ff2017-02-03 10:05:00 +10001/*
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"
Dave Airliee2659172017-04-25 23:33:29 +010036#include "ac_exp_param.h"
Dave Airlie13a28ff2017-02-03 10:05:00 +100037#include "util/bitscan.h"
38#include "util/macros.h"
Connor Abbottac27fa72017-06-05 14:16:43 -070039#include "util/u_atomic.h"
Dave Airlie13a28ff2017-02-03 10:05:00 +100040#include "sid.h"
41
Dave Airliee2659172017-04-25 23:33:29 +010042#include "shader_enums.h"
43
Dave Airlie13a28ff2017-02-03 10:05:00 +100044/* Initialize module-independent parts of the context.
45 *
46 * The caller is responsible for initializing ctx::module and ctx::builder.
47 */
48void
Nicolai Hähnle3db86d82017-09-13 14:36:23 +020049ac_llvm_context_init(struct ac_llvm_context *ctx, LLVMContextRef context,
50 enum chip_class chip_class)
Dave Airlie13a28ff2017-02-03 10:05:00 +100051{
52 LLVMValueRef args[1];
53
Nicolai Hähnle3db86d82017-09-13 14:36:23 +020054 ctx->chip_class = chip_class;
55
Dave Airlie13a28ff2017-02-03 10:05:00 +100056 ctx->context = context;
57 ctx->module = NULL;
58 ctx->builder = NULL;
59
60 ctx->voidt = LLVMVoidTypeInContext(ctx->context);
61 ctx->i1 = LLVMInt1TypeInContext(ctx->context);
62 ctx->i8 = LLVMInt8TypeInContext(ctx->context);
Nicolai Hähnle7bf8c942017-03-30 14:10:26 +020063 ctx->i16 = LLVMIntTypeInContext(ctx->context, 16);
Dave Airlie13a28ff2017-02-03 10:05:00 +100064 ctx->i32 = LLVMIntTypeInContext(ctx->context, 32);
Nicolai Hähnle7bf8c942017-03-30 14:10:26 +020065 ctx->i64 = LLVMIntTypeInContext(ctx->context, 64);
66 ctx->f16 = LLVMHalfTypeInContext(ctx->context);
Dave Airlie13a28ff2017-02-03 10:05:00 +100067 ctx->f32 = LLVMFloatTypeInContext(ctx->context);
Nicolai Hähnle7bf8c942017-03-30 14:10:26 +020068 ctx->f64 = LLVMDoubleTypeInContext(ctx->context);
Dave Airlie13a28ff2017-02-03 10:05:00 +100069 ctx->v4i32 = LLVMVectorType(ctx->i32, 4);
70 ctx->v4f32 = LLVMVectorType(ctx->f32, 4);
Nicolai Hähnleedfd3be2017-06-08 20:04:28 +020071 ctx->v8i32 = LLVMVectorType(ctx->i32, 8);
Dave Airlie13a28ff2017-02-03 10:05:00 +100072
Nicolai Hähnle331a5742017-05-18 22:02:48 +020073 ctx->i32_0 = LLVMConstInt(ctx->i32, 0, false);
74 ctx->i32_1 = LLVMConstInt(ctx->i32, 1, false);
75 ctx->f32_0 = LLVMConstReal(ctx->f32, 0.0);
76 ctx->f32_1 = LLVMConstReal(ctx->f32, 1.0);
77
Dave Airlie13a28ff2017-02-03 10:05:00 +100078 ctx->range_md_kind = LLVMGetMDKindIDInContext(ctx->context,
79 "range", 5);
80
81 ctx->invariant_load_md_kind = LLVMGetMDKindIDInContext(ctx->context,
82 "invariant.load", 14);
83
84 ctx->fpmath_md_kind = LLVMGetMDKindIDInContext(ctx->context, "fpmath", 6);
85
86 args[0] = LLVMConstReal(ctx->f32, 2.5);
87 ctx->fpmath_md_2p5_ulp = LLVMMDNodeInContext(ctx->context, args, 1);
88
89 ctx->uniform_md_kind = LLVMGetMDKindIDInContext(ctx->context,
90 "amdgpu.uniform", 14);
91
92 ctx->empty_md = LLVMMDNodeInContext(ctx->context, NULL, 0);
93}
94
Connor Abbottc181d4f2017-06-05 14:37:01 -070095unsigned
96ac_get_type_size(LLVMTypeRef type)
97{
98 LLVMTypeKind kind = LLVMGetTypeKind(type);
99
100 switch (kind) {
101 case LLVMIntegerTypeKind:
102 return LLVMGetIntTypeWidth(type) / 8;
103 case LLVMFloatTypeKind:
104 return 4;
Connor Abbottfafa2992017-07-18 20:44:47 -0700105 case LLVMDoubleTypeKind:
Connor Abbottc181d4f2017-06-05 14:37:01 -0700106 case LLVMPointerTypeKind:
107 return 8;
108 case LLVMVectorTypeKind:
109 return LLVMGetVectorSize(type) *
110 ac_get_type_size(LLVMGetElementType(type));
111 case LLVMArrayTypeKind:
112 return LLVMGetArrayLength(type) *
113 ac_get_type_size(LLVMGetElementType(type));
114 default:
115 assert(0);
116 return 0;
117 }
118}
119
Connor Abbott50967cd2017-07-18 17:32:10 -0700120static LLVMTypeRef to_integer_type_scalar(struct ac_llvm_context *ctx, LLVMTypeRef t)
121{
122 if (t == ctx->f16 || t == ctx->i16)
123 return ctx->i16;
124 else if (t == ctx->f32 || t == ctx->i32)
125 return ctx->i32;
126 else if (t == ctx->f64 || t == ctx->i64)
127 return ctx->i64;
128 else
129 unreachable("Unhandled integer size");
130}
131
132LLVMTypeRef
133ac_to_integer_type(struct ac_llvm_context *ctx, LLVMTypeRef t)
134{
135 if (LLVMGetTypeKind(t) == LLVMVectorTypeKind) {
136 LLVMTypeRef elem_type = LLVMGetElementType(t);
137 return LLVMVectorType(to_integer_type_scalar(ctx, elem_type),
138 LLVMGetVectorSize(t));
139 }
140 return to_integer_type_scalar(ctx, t);
141}
142
143LLVMValueRef
144ac_to_integer(struct ac_llvm_context *ctx, LLVMValueRef v)
145{
146 LLVMTypeRef type = LLVMTypeOf(v);
147 return LLVMBuildBitCast(ctx->builder, v, ac_to_integer_type(ctx, type), "");
148}
149
150static LLVMTypeRef to_float_type_scalar(struct ac_llvm_context *ctx, LLVMTypeRef t)
151{
152 if (t == ctx->i16 || t == ctx->f16)
153 return ctx->f16;
154 else if (t == ctx->i32 || t == ctx->f32)
155 return ctx->f32;
156 else if (t == ctx->i64 || t == ctx->f64)
157 return ctx->f64;
158 else
159 unreachable("Unhandled float size");
160}
161
162LLVMTypeRef
163ac_to_float_type(struct ac_llvm_context *ctx, LLVMTypeRef t)
164{
165 if (LLVMGetTypeKind(t) == LLVMVectorTypeKind) {
166 LLVMTypeRef elem_type = LLVMGetElementType(t);
167 return LLVMVectorType(to_float_type_scalar(ctx, elem_type),
168 LLVMGetVectorSize(t));
169 }
170 return to_float_type_scalar(ctx, t);
171}
172
173LLVMValueRef
174ac_to_float(struct ac_llvm_context *ctx, LLVMValueRef v)
175{
176 LLVMTypeRef type = LLVMTypeOf(v);
177 return LLVMBuildBitCast(ctx->builder, v, ac_to_float_type(ctx, type), "");
178}
179
180
Dave Airlie13a28ff2017-02-03 10:05:00 +1000181LLVMValueRef
Marek Olšák7f1446a2017-02-26 00:41:37 +0100182ac_build_intrinsic(struct ac_llvm_context *ctx, const char *name,
183 LLVMTypeRef return_type, LLVMValueRef *params,
184 unsigned param_count, unsigned attrib_mask)
Dave Airlie13a28ff2017-02-03 10:05:00 +1000185{
Marek Olšák940da362017-02-22 02:29:12 +0100186 LLVMValueRef function, call;
187 bool set_callsite_attrs = HAVE_LLVM >= 0x0400 &&
188 !(attrib_mask & AC_FUNC_ATTR_LEGACY);
Dave Airlie13a28ff2017-02-03 10:05:00 +1000189
190 function = LLVMGetNamedFunction(ctx->module, name);
191 if (!function) {
192 LLVMTypeRef param_types[32], function_type;
193 unsigned i;
194
195 assert(param_count <= 32);
196
197 for (i = 0; i < param_count; ++i) {
198 assert(params[i]);
199 param_types[i] = LLVMTypeOf(params[i]);
200 }
201 function_type =
202 LLVMFunctionType(return_type, param_types, param_count, 0);
203 function = LLVMAddFunction(ctx->module, name, function_type);
204
205 LLVMSetFunctionCallConv(function, LLVMCCallConv);
206 LLVMSetLinkage(function, LLVMExternalLinkage);
207
Marek Olšák940da362017-02-22 02:29:12 +0100208 if (!set_callsite_attrs)
209 ac_add_func_attributes(ctx->context, function, attrib_mask);
Dave Airlie13a28ff2017-02-03 10:05:00 +1000210 }
Marek Olšák940da362017-02-22 02:29:12 +0100211
212 call = LLVMBuildCall(ctx->builder, function, params, param_count, "");
213 if (set_callsite_attrs)
214 ac_add_func_attributes(ctx->context, call, attrib_mask);
215 return call;
Dave Airlie13a28ff2017-02-03 10:05:00 +1000216}
217
Marek Olšák9af03312017-02-23 22:58:49 +0100218/**
219 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
220 * intrinsic names).
221 */
222void ac_build_type_name_for_intr(LLVMTypeRef type, char *buf, unsigned bufsize)
223{
224 LLVMTypeRef elem_type = type;
225
226 assert(bufsize >= 8);
227
228 if (LLVMGetTypeKind(type) == LLVMVectorTypeKind) {
229 int ret = snprintf(buf, bufsize, "v%u",
230 LLVMGetVectorSize(type));
231 if (ret < 0) {
232 char *type_name = LLVMPrintTypeToString(type);
233 fprintf(stderr, "Error building type name for: %s\n",
234 type_name);
235 return;
236 }
237 elem_type = LLVMGetElementType(type);
238 buf += ret;
239 bufsize -= ret;
240 }
241 switch (LLVMGetTypeKind(elem_type)) {
242 default: break;
243 case LLVMIntegerTypeKind:
244 snprintf(buf, bufsize, "i%d", LLVMGetIntTypeWidth(elem_type));
245 break;
246 case LLVMFloatTypeKind:
247 snprintf(buf, bufsize, "f32");
248 break;
249 case LLVMDoubleTypeKind:
250 snprintf(buf, bufsize, "f64");
251 break;
252 }
253}
254
Nicolai Hähnle052b9742017-09-29 11:17:03 +0200255/**
256 * Helper function that builds an LLVM IR PHI node and immediately adds
257 * incoming edges.
258 */
259LLVMValueRef
260ac_build_phi(struct ac_llvm_context *ctx, LLVMTypeRef type,
261 unsigned count_incoming, LLVMValueRef *values,
262 LLVMBasicBlockRef *blocks)
263{
264 LLVMValueRef phi = LLVMBuildPhi(ctx->builder, type, "");
265 LLVMAddIncoming(phi, values, blocks, count_incoming);
266 return phi;
267}
268
Connor Abbottac27fa72017-06-05 14:16:43 -0700269/* Prevent optimizations (at least of memory accesses) across the current
270 * point in the program by emitting empty inline assembly that is marked as
271 * having side effects.
272 *
273 * Optionally, a value can be passed through the inline assembly to prevent
274 * LLVM from hoisting calls to ReadNone functions.
275 */
276void
277ac_build_optimization_barrier(struct ac_llvm_context *ctx,
278 LLVMValueRef *pvgpr)
279{
280 static int counter = 0;
281
282 LLVMBuilderRef builder = ctx->builder;
283 char code[16];
284
285 snprintf(code, sizeof(code), "; %d", p_atomic_inc_return(&counter));
286
287 if (!pvgpr) {
288 LLVMTypeRef ftype = LLVMFunctionType(ctx->voidt, NULL, 0, false);
289 LLVMValueRef inlineasm = LLVMConstInlineAsm(ftype, code, "", true, false);
290 LLVMBuildCall(builder, inlineasm, NULL, 0, "");
291 } else {
292 LLVMTypeRef ftype = LLVMFunctionType(ctx->i32, &ctx->i32, 1, false);
293 LLVMValueRef inlineasm = LLVMConstInlineAsm(ftype, code, "=v,0", true, false);
294 LLVMValueRef vgpr = *pvgpr;
295 LLVMTypeRef vgpr_type = LLVMTypeOf(vgpr);
296 unsigned vgpr_size = ac_get_type_size(vgpr_type);
297 LLVMValueRef vgpr0;
298
299 assert(vgpr_size % 4 == 0);
300
301 vgpr = LLVMBuildBitCast(builder, vgpr, LLVMVectorType(ctx->i32, vgpr_size / 4), "");
302 vgpr0 = LLVMBuildExtractElement(builder, vgpr, ctx->i32_0, "");
303 vgpr0 = LLVMBuildCall(builder, inlineasm, &vgpr0, 1, "");
304 vgpr = LLVMBuildInsertElement(builder, vgpr, vgpr0, ctx->i32_0, "");
305 vgpr = LLVMBuildBitCast(builder, vgpr, vgpr_type, "");
306
307 *pvgpr = vgpr;
308 }
309}
310
Dave Airlie13a28ff2017-02-03 10:05:00 +1000311LLVMValueRef
Connor Abbottbd73b892017-06-05 15:20:04 -0700312ac_build_ballot(struct ac_llvm_context *ctx,
313 LLVMValueRef value)
314{
315 LLVMValueRef args[3] = {
316 value,
317 ctx->i32_0,
318 LLVMConstInt(ctx->i32, LLVMIntNE, 0)
319 };
320
321 /* We currently have no other way to prevent LLVM from lifting the icmp
322 * calls to a dominating basic block.
323 */
324 ac_build_optimization_barrier(ctx, &args[0]);
325
326 if (LLVMTypeOf(args[0]) != ctx->i32)
327 args[0] = LLVMBuildBitCast(ctx->builder, args[0], ctx->i32, "");
328
329 return ac_build_intrinsic(ctx,
330 "llvm.amdgcn.icmp.i32",
331 ctx->i64, args, 3,
332 AC_FUNC_ATTR_NOUNWIND |
333 AC_FUNC_ATTR_READNONE |
334 AC_FUNC_ATTR_CONVERGENT);
335}
336
337LLVMValueRef
Connor Abbottb8a51c82017-06-06 16:40:26 -0700338ac_build_vote_all(struct ac_llvm_context *ctx, LLVMValueRef value)
339{
340 LLVMValueRef active_set = ac_build_ballot(ctx, ctx->i32_1);
341 LLVMValueRef vote_set = ac_build_ballot(ctx, value);
342 return LLVMBuildICmp(ctx->builder, LLVMIntEQ, vote_set, active_set, "");
343}
344
345LLVMValueRef
346ac_build_vote_any(struct ac_llvm_context *ctx, LLVMValueRef value)
347{
348 LLVMValueRef vote_set = ac_build_ballot(ctx, value);
349 return LLVMBuildICmp(ctx->builder, LLVMIntNE, vote_set,
350 LLVMConstInt(ctx->i64, 0, 0), "");
351}
352
353LLVMValueRef
354ac_build_vote_eq(struct ac_llvm_context *ctx, LLVMValueRef value)
355{
356 LLVMValueRef active_set = ac_build_ballot(ctx, ctx->i32_1);
357 LLVMValueRef vote_set = ac_build_ballot(ctx, value);
358
359 LLVMValueRef all = LLVMBuildICmp(ctx->builder, LLVMIntEQ,
360 vote_set, active_set, "");
361 LLVMValueRef none = LLVMBuildICmp(ctx->builder, LLVMIntEQ,
362 vote_set,
363 LLVMConstInt(ctx->i64, 0, 0), "");
364 return LLVMBuildOr(ctx->builder, all, none, "");
365}
366
367LLVMValueRef
Dave Airlie13a28ff2017-02-03 10:05:00 +1000368ac_build_gather_values_extended(struct ac_llvm_context *ctx,
369 LLVMValueRef *values,
370 unsigned value_count,
371 unsigned value_stride,
Nicolai Hähnleac2ab5a2017-06-25 13:04:51 +0200372 bool load,
373 bool always_vector)
Dave Airlie13a28ff2017-02-03 10:05:00 +1000374{
375 LLVMBuilderRef builder = ctx->builder;
Marek Olšákc7878b02017-02-23 01:34:27 +0100376 LLVMValueRef vec = NULL;
Dave Airlie13a28ff2017-02-03 10:05:00 +1000377 unsigned i;
378
Nicolai Hähnleac2ab5a2017-06-25 13:04:51 +0200379 if (value_count == 1 && !always_vector) {
Dave Airlie13a28ff2017-02-03 10:05:00 +1000380 if (load)
381 return LLVMBuildLoad(builder, values[0], "");
382 return values[0];
383 } else if (!value_count)
384 unreachable("value_count is 0");
385
386 for (i = 0; i < value_count; i++) {
387 LLVMValueRef value = values[i * value_stride];
388 if (load)
389 value = LLVMBuildLoad(builder, value, "");
390
391 if (!i)
392 vec = LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value), value_count));
393 LLVMValueRef index = LLVMConstInt(ctx->i32, i, false);
394 vec = LLVMBuildInsertElement(builder, vec, value, index, "");
395 }
396 return vec;
397}
398
399LLVMValueRef
400ac_build_gather_values(struct ac_llvm_context *ctx,
401 LLVMValueRef *values,
402 unsigned value_count)
403{
Nicolai Hähnleac2ab5a2017-06-25 13:04:51 +0200404 return ac_build_gather_values_extended(ctx, values, value_count, 1, false, false);
Dave Airlie13a28ff2017-02-03 10:05:00 +1000405}
406
407LLVMValueRef
Marek Olšák7f1446a2017-02-26 00:41:37 +0100408ac_build_fdiv(struct ac_llvm_context *ctx,
409 LLVMValueRef num,
410 LLVMValueRef den)
Dave Airlie13a28ff2017-02-03 10:05:00 +1000411{
412 LLVMValueRef ret = LLVMBuildFDiv(ctx->builder, num, den, "");
413
414 if (!LLVMIsConstant(ret))
415 LLVMSetMetadata(ret, ctx->fpmath_md_kind, ctx->fpmath_md_2p5_ulp);
416 return ret;
417}
418
419/* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
420 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
421 * already multiplied by two. id is the cube face number.
422 */
423struct cube_selection_coords {
424 LLVMValueRef stc[2];
425 LLVMValueRef ma;
426 LLVMValueRef id;
427};
428
429static void
430build_cube_intrinsic(struct ac_llvm_context *ctx,
431 LLVMValueRef in[3],
432 struct cube_selection_coords *out)
433{
Marek Olšák12beef02017-04-25 02:18:10 +0200434 LLVMTypeRef f32 = ctx->f32;
Dave Airlie13a28ff2017-02-03 10:05:00 +1000435
Marek Olšák12beef02017-04-25 02:18:10 +0200436 out->stc[1] = ac_build_intrinsic(ctx, "llvm.amdgcn.cubetc",
437 f32, in, 3, AC_FUNC_ATTR_READNONE);
438 out->stc[0] = ac_build_intrinsic(ctx, "llvm.amdgcn.cubesc",
439 f32, in, 3, AC_FUNC_ATTR_READNONE);
440 out->ma = ac_build_intrinsic(ctx, "llvm.amdgcn.cubema",
441 f32, in, 3, AC_FUNC_ATTR_READNONE);
442 out->id = ac_build_intrinsic(ctx, "llvm.amdgcn.cubeid",
443 f32, in, 3, AC_FUNC_ATTR_READNONE);
Dave Airlie13a28ff2017-02-03 10:05:00 +1000444}
445
446/**
447 * Build a manual selection sequence for cube face sc/tc coordinates and
448 * major axis vector (multiplied by 2 for consistency) for the given
449 * vec3 \p coords, for the face implied by \p selcoords.
450 *
451 * For the major axis, we always adjust the sign to be in the direction of
452 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
453 * the selcoords major axis.
454 */
Nicolai Hähnlea6ea4c12017-09-22 19:14:16 +0200455static void build_cube_select(struct ac_llvm_context *ctx,
Dave Airlie13a28ff2017-02-03 10:05:00 +1000456 const struct cube_selection_coords *selcoords,
457 const LLVMValueRef *coords,
458 LLVMValueRef *out_st,
459 LLVMValueRef *out_ma)
460{
Nicolai Hähnlea6ea4c12017-09-22 19:14:16 +0200461 LLVMBuilderRef builder = ctx->builder;
Dave Airlie13a28ff2017-02-03 10:05:00 +1000462 LLVMTypeRef f32 = LLVMTypeOf(coords[0]);
463 LLVMValueRef is_ma_positive;
464 LLVMValueRef sgn_ma;
465 LLVMValueRef is_ma_z, is_not_ma_z;
466 LLVMValueRef is_ma_y;
467 LLVMValueRef is_ma_x;
468 LLVMValueRef sgn;
469 LLVMValueRef tmp;
470
471 is_ma_positive = LLVMBuildFCmp(builder, LLVMRealUGE,
472 selcoords->ma, LLVMConstReal(f32, 0.0), "");
473 sgn_ma = LLVMBuildSelect(builder, is_ma_positive,
474 LLVMConstReal(f32, 1.0), LLVMConstReal(f32, -1.0), "");
475
476 is_ma_z = LLVMBuildFCmp(builder, LLVMRealUGE, selcoords->id, LLVMConstReal(f32, 4.0), "");
477 is_not_ma_z = LLVMBuildNot(builder, is_ma_z, "");
478 is_ma_y = LLVMBuildAnd(builder, is_not_ma_z,
479 LLVMBuildFCmp(builder, LLVMRealUGE, selcoords->id, LLVMConstReal(f32, 2.0), ""), "");
480 is_ma_x = LLVMBuildAnd(builder, is_not_ma_z, LLVMBuildNot(builder, is_ma_y, ""), "");
481
482 /* Select sc */
Nicolai Hähnle5be5c1e2017-09-22 19:05:52 +0200483 tmp = LLVMBuildSelect(builder, is_ma_x, coords[2], coords[0], "");
Dave Airlie13a28ff2017-02-03 10:05:00 +1000484 sgn = LLVMBuildSelect(builder, is_ma_y, LLVMConstReal(f32, 1.0),
Nicolai Hähnle5be5c1e2017-09-22 19:05:52 +0200485 LLVMBuildSelect(builder, is_ma_z, sgn_ma,
Dave Airlie13a28ff2017-02-03 10:05:00 +1000486 LLVMBuildFNeg(builder, sgn_ma, ""), ""), "");
487 out_st[0] = LLVMBuildFMul(builder, tmp, sgn, "");
488
489 /* Select tc */
490 tmp = LLVMBuildSelect(builder, is_ma_y, coords[2], coords[1], "");
Nicolai Hähnle5be5c1e2017-09-22 19:05:52 +0200491 sgn = LLVMBuildSelect(builder, is_ma_y, sgn_ma,
Dave Airlie13a28ff2017-02-03 10:05:00 +1000492 LLVMConstReal(f32, -1.0), "");
493 out_st[1] = LLVMBuildFMul(builder, tmp, sgn, "");
494
495 /* Select ma */
496 tmp = LLVMBuildSelect(builder, is_ma_z, coords[2],
497 LLVMBuildSelect(builder, is_ma_y, coords[1], coords[0], ""), "");
Nicolai Hähnlea6ea4c12017-09-22 19:14:16 +0200498 tmp = ac_build_intrinsic(ctx, "llvm.fabs.f32",
499 ctx->f32, &tmp, 1, AC_FUNC_ATTR_READNONE);
500 *out_ma = LLVMBuildFMul(builder, tmp, LLVMConstReal(f32, 2.0), "");
Dave Airlie13a28ff2017-02-03 10:05:00 +1000501}
502
503void
504ac_prepare_cube_coords(struct ac_llvm_context *ctx,
Nicolai Hähnlee0af3be2017-09-13 10:47:02 +0200505 bool is_deriv, bool is_array, bool is_lod,
Dave Airlie13a28ff2017-02-03 10:05:00 +1000506 LLVMValueRef *coords_arg,
507 LLVMValueRef *derivs_arg)
508{
509
510 LLVMBuilderRef builder = ctx->builder;
511 struct cube_selection_coords selcoords;
512 LLVMValueRef coords[3];
513 LLVMValueRef invma;
514
Nicolai Hähnlee0af3be2017-09-13 10:47:02 +0200515 if (is_array && !is_lod) {
Nicolai Hähnle94736d32017-09-13 15:33:23 +0200516 LLVMValueRef tmp = coords_arg[3];
517 tmp = ac_build_intrinsic(ctx, "llvm.rint.f32", ctx->f32, &tmp, 1, 0);
518
519 /* Section 8.9 (Texture Functions) of the GLSL 4.50 spec says:
520 *
521 * "For Array forms, the array layer used will be
522 *
523 * max(0, min(d−1, floor(layer+0.5)))
524 *
525 * where d is the depth of the texture array and layer
526 * comes from the component indicated in the tables below.
527 * Workaroudn for an issue where the layer is taken from a
528 * helper invocation which happens to fall on a different
529 * layer due to extrapolation."
530 *
531 * VI and earlier attempt to implement this in hardware by
532 * clamping the value of coords[2] = (8 * layer) + face.
533 * Unfortunately, this means that the we end up with the wrong
534 * face when clamping occurs.
535 *
536 * Clamp the layer earlier to work around the issue.
537 */
538 if (ctx->chip_class <= VI) {
539 LLVMValueRef ge0;
540 ge0 = LLVMBuildFCmp(builder, LLVMRealOGE, tmp, ctx->f32_0, "");
541 tmp = LLVMBuildSelect(builder, ge0, tmp, ctx->f32_0, "");
542 }
543
544 coords_arg[3] = tmp;
Nicolai Hähnlee0af3be2017-09-13 10:47:02 +0200545 }
546
Dave Airlie13a28ff2017-02-03 10:05:00 +1000547 build_cube_intrinsic(ctx, coords_arg, &selcoords);
548
Marek Olšák7f1446a2017-02-26 00:41:37 +0100549 invma = ac_build_intrinsic(ctx, "llvm.fabs.f32",
Dave Airlie13a28ff2017-02-03 10:05:00 +1000550 ctx->f32, &selcoords.ma, 1, AC_FUNC_ATTR_READNONE);
Marek Olšák7f1446a2017-02-26 00:41:37 +0100551 invma = ac_build_fdiv(ctx, LLVMConstReal(ctx->f32, 1.0), invma);
Dave Airlie13a28ff2017-02-03 10:05:00 +1000552
553 for (int i = 0; i < 2; ++i)
554 coords[i] = LLVMBuildFMul(builder, selcoords.stc[i], invma, "");
555
556 coords[2] = selcoords.id;
557
558 if (is_deriv && derivs_arg) {
559 LLVMValueRef derivs[4];
560 int axis;
561
562 /* Convert cube derivatives to 2D derivatives. */
563 for (axis = 0; axis < 2; axis++) {
564 LLVMValueRef deriv_st[2];
565 LLVMValueRef deriv_ma;
566
567 /* Transform the derivative alongside the texture
568 * coordinate. Mathematically, the correct formula is
569 * as follows. Assume we're projecting onto the +Z face
570 * and denote by dx/dh the derivative of the (original)
571 * X texture coordinate with respect to horizontal
572 * window coordinates. The projection onto the +Z face
573 * plane is:
574 *
575 * f(x,z) = x/z
576 *
577 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
578 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
579 *
580 * This motivatives the implementation below.
581 *
582 * Whether this actually gives the expected results for
583 * apps that might feed in derivatives obtained via
584 * finite differences is anyone's guess. The OpenGL spec
585 * seems awfully quiet about how textureGrad for cube
586 * maps should be handled.
587 */
Nicolai Hähnlea6ea4c12017-09-22 19:14:16 +0200588 build_cube_select(ctx, &selcoords, &derivs_arg[axis * 3],
Dave Airlie13a28ff2017-02-03 10:05:00 +1000589 deriv_st, &deriv_ma);
590
591 deriv_ma = LLVMBuildFMul(builder, deriv_ma, invma, "");
592
593 for (int i = 0; i < 2; ++i)
594 derivs[axis * 2 + i] =
595 LLVMBuildFSub(builder,
596 LLVMBuildFMul(builder, deriv_st[i], invma, ""),
597 LLVMBuildFMul(builder, deriv_ma, coords[i], ""), "");
598 }
599
600 memcpy(derivs_arg, derivs, sizeof(derivs));
601 }
602
603 /* Shift the texture coordinate. This must be applied after the
604 * derivative calculation.
605 */
606 for (int i = 0; i < 2; ++i)
607 coords[i] = LLVMBuildFAdd(builder, coords[i], LLVMConstReal(ctx->f32, 1.5), "");
608
609 if (is_array) {
610 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
611 /* coords_arg.w component - array_index for cube arrays */
612 LLVMValueRef tmp = LLVMBuildFMul(ctx->builder, coords_arg[3], LLVMConstReal(ctx->f32, 8.0), "");
613 coords[2] = LLVMBuildFAdd(ctx->builder, tmp, coords[2], "");
614 }
615
616 memcpy(coords_arg, coords, sizeof(coords));
617}
618
619
620LLVMValueRef
621ac_build_fs_interp(struct ac_llvm_context *ctx,
622 LLVMValueRef llvm_chan,
623 LLVMValueRef attr_number,
624 LLVMValueRef params,
625 LLVMValueRef i,
626 LLVMValueRef j)
627{
628 LLVMValueRef args[5];
629 LLVMValueRef p1;
630
631 if (HAVE_LLVM < 0x0400) {
632 LLVMValueRef ij[2];
633 ij[0] = LLVMBuildBitCast(ctx->builder, i, ctx->i32, "");
634 ij[1] = LLVMBuildBitCast(ctx->builder, j, ctx->i32, "");
635
636 args[0] = llvm_chan;
637 args[1] = attr_number;
638 args[2] = params;
639 args[3] = ac_build_gather_values(ctx, ij, 2);
Marek Olšák7f1446a2017-02-26 00:41:37 +0100640 return ac_build_intrinsic(ctx, "llvm.SI.fs.interp",
641 ctx->f32, args, 4,
642 AC_FUNC_ATTR_READNONE);
Dave Airlie13a28ff2017-02-03 10:05:00 +1000643 }
644
645 args[0] = i;
646 args[1] = llvm_chan;
647 args[2] = attr_number;
648 args[3] = params;
649
Marek Olšák7f1446a2017-02-26 00:41:37 +0100650 p1 = ac_build_intrinsic(ctx, "llvm.amdgcn.interp.p1",
651 ctx->f32, args, 4, AC_FUNC_ATTR_READNONE);
Dave Airlie13a28ff2017-02-03 10:05:00 +1000652
653 args[0] = p1;
654 args[1] = j;
655 args[2] = llvm_chan;
656 args[3] = attr_number;
657 args[4] = params;
658
Marek Olšák7f1446a2017-02-26 00:41:37 +0100659 return ac_build_intrinsic(ctx, "llvm.amdgcn.interp.p2",
660 ctx->f32, args, 5, AC_FUNC_ATTR_READNONE);
Dave Airlie13a28ff2017-02-03 10:05:00 +1000661}
662
663LLVMValueRef
664ac_build_fs_interp_mov(struct ac_llvm_context *ctx,
665 LLVMValueRef parameter,
666 LLVMValueRef llvm_chan,
667 LLVMValueRef attr_number,
668 LLVMValueRef params)
669{
670 LLVMValueRef args[4];
671 if (HAVE_LLVM < 0x0400) {
672 args[0] = llvm_chan;
673 args[1] = attr_number;
674 args[2] = params;
675
Marek Olšák7f1446a2017-02-26 00:41:37 +0100676 return ac_build_intrinsic(ctx,
677 "llvm.SI.fs.constant",
678 ctx->f32, args, 3,
679 AC_FUNC_ATTR_READNONE);
Dave Airlie13a28ff2017-02-03 10:05:00 +1000680 }
681
682 args[0] = parameter;
683 args[1] = llvm_chan;
684 args[2] = attr_number;
685 args[3] = params;
686
Marek Olšák7f1446a2017-02-26 00:41:37 +0100687 return ac_build_intrinsic(ctx, "llvm.amdgcn.interp.mov",
688 ctx->f32, args, 4, AC_FUNC_ATTR_READNONE);
Dave Airlie13a28ff2017-02-03 10:05:00 +1000689}
690
691LLVMValueRef
692ac_build_gep0(struct ac_llvm_context *ctx,
693 LLVMValueRef base_ptr,
694 LLVMValueRef index)
695{
696 LLVMValueRef indices[2] = {
697 LLVMConstInt(ctx->i32, 0, 0),
698 index,
699 };
700 return LLVMBuildGEP(ctx->builder, base_ptr,
701 indices, 2, "");
702}
703
704void
705ac_build_indexed_store(struct ac_llvm_context *ctx,
706 LLVMValueRef base_ptr, LLVMValueRef index,
707 LLVMValueRef value)
708{
709 LLVMBuildStore(ctx->builder, value,
710 ac_build_gep0(ctx, base_ptr, index));
711}
712
713/**
714 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
715 * It's equivalent to doing a load from &base_ptr[index].
716 *
717 * \param base_ptr Where the array starts.
718 * \param index The element index into the array.
719 * \param uniform Whether the base_ptr and index can be assumed to be
Marek Olšák854593b2017-10-08 20:05:44 +0200720 * dynamically uniform (i.e. load to an SGPR)
721 * \param invariant Whether the load is invariant (no other opcodes affect it)
Dave Airlie13a28ff2017-02-03 10:05:00 +1000722 */
Marek Olšák854593b2017-10-08 20:05:44 +0200723static LLVMValueRef
724ac_build_load_custom(struct ac_llvm_context *ctx, LLVMValueRef base_ptr,
725 LLVMValueRef index, bool uniform, bool invariant)
Dave Airlie13a28ff2017-02-03 10:05:00 +1000726{
Marek Olšák854593b2017-10-08 20:05:44 +0200727 LLVMValueRef pointer, result;
Dave Airlie13a28ff2017-02-03 10:05:00 +1000728
729 pointer = ac_build_gep0(ctx, base_ptr, index);
730 if (uniform)
731 LLVMSetMetadata(pointer, ctx->uniform_md_kind, ctx->empty_md);
Marek Olšák854593b2017-10-08 20:05:44 +0200732 result = LLVMBuildLoad(ctx->builder, pointer, "");
733 if (invariant)
734 LLVMSetMetadata(result, ctx->invariant_load_md_kind, ctx->empty_md);
735 return result;
Dave Airlie13a28ff2017-02-03 10:05:00 +1000736}
737
Marek Olšák854593b2017-10-08 20:05:44 +0200738LLVMValueRef ac_build_load(struct ac_llvm_context *ctx, LLVMValueRef base_ptr,
739 LLVMValueRef index)
Dave Airlie13a28ff2017-02-03 10:05:00 +1000740{
Marek Olšák854593b2017-10-08 20:05:44 +0200741 return ac_build_load_custom(ctx, base_ptr, index, false, false);
742}
743
744LLVMValueRef ac_build_load_invariant(struct ac_llvm_context *ctx,
745 LLVMValueRef base_ptr, LLVMValueRef index)
746{
747 return ac_build_load_custom(ctx, base_ptr, index, false, true);
748}
749
750LLVMValueRef ac_build_load_to_sgpr(struct ac_llvm_context *ctx,
751 LLVMValueRef base_ptr, LLVMValueRef index)
752{
753 return ac_build_load_custom(ctx, base_ptr, index, true, true);
Dave Airlie13a28ff2017-02-03 10:05:00 +1000754}
755
756/* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
757 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
758 * or v4i32 (num_channels=3,4).
759 */
760void
Marek Olšák27439df2017-02-24 01:20:35 +0100761ac_build_buffer_store_dword(struct ac_llvm_context *ctx,
762 LLVMValueRef rsrc,
763 LLVMValueRef vdata,
764 unsigned num_channels,
Marek Olšák8cfdbba2017-02-24 20:23:23 +0100765 LLVMValueRef voffset,
Marek Olšák27439df2017-02-24 01:20:35 +0100766 LLVMValueRef soffset,
767 unsigned inst_offset,
Marek Olšák27439df2017-02-24 01:20:35 +0100768 bool glc,
Marek Olšák97e21cf2017-02-24 02:09:47 +0100769 bool slc,
770 bool writeonly_memory,
Marek Olšákbcd3e762017-09-30 15:36:18 +0200771 bool swizzle_enable_hint)
Dave Airlie13a28ff2017-02-03 10:05:00 +1000772{
Marek Olšákbcd3e762017-09-30 15:36:18 +0200773 /* SWIZZLE_ENABLE requires that soffset isn't folded into voffset
774 * (voffset is swizzled, but soffset isn't swizzled).
775 * llvm.amdgcn.buffer.store doesn't have a separate soffset parameter.
776 */
777 if (!swizzle_enable_hint) {
Marek Olšák97e21cf2017-02-24 02:09:47 +0100778 /* Split 3 channel stores, becase LLVM doesn't support 3-channel
779 * intrinsics. */
780 if (num_channels == 3) {
781 LLVMValueRef v[3], v01;
782
783 for (int i = 0; i < 3; i++) {
784 v[i] = LLVMBuildExtractElement(ctx->builder, vdata,
785 LLVMConstInt(ctx->i32, i, 0), "");
786 }
787 v01 = ac_build_gather_values(ctx, v, 2);
788
789 ac_build_buffer_store_dword(ctx, rsrc, v01, 2, voffset,
790 soffset, inst_offset, glc, slc,
Marek Olšákbcd3e762017-09-30 15:36:18 +0200791 writeonly_memory, swizzle_enable_hint);
Marek Olšák97e21cf2017-02-24 02:09:47 +0100792 ac_build_buffer_store_dword(ctx, rsrc, v[2], 1, voffset,
793 soffset, inst_offset + 8,
794 glc, slc,
Marek Olšákbcd3e762017-09-30 15:36:18 +0200795 writeonly_memory, swizzle_enable_hint);
Marek Olšák97e21cf2017-02-24 02:09:47 +0100796 return;
797 }
798
799 unsigned func = CLAMP(num_channels, 1, 3) - 1;
800 static const char *types[] = {"f32", "v2f32", "v4f32"};
801 char name[256];
802 LLVMValueRef offset = soffset;
803
804 if (inst_offset)
805 offset = LLVMBuildAdd(ctx->builder, offset,
806 LLVMConstInt(ctx->i32, inst_offset, 0), "");
807 if (voffset)
808 offset = LLVMBuildAdd(ctx->builder, offset, voffset, "");
809
810 LLVMValueRef args[] = {
Connor Abbottb909d272017-07-18 17:35:35 -0700811 ac_to_float(ctx, vdata),
Marek Olšák97e21cf2017-02-24 02:09:47 +0100812 LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, ""),
813 LLVMConstInt(ctx->i32, 0, 0),
814 offset,
815 LLVMConstInt(ctx->i1, glc, 0),
816 LLVMConstInt(ctx->i1, slc, 0),
817 };
818
819 snprintf(name, sizeof(name), "llvm.amdgcn.buffer.store.%s",
820 types[func]);
821
Marek Olšák7f1446a2017-02-26 00:41:37 +0100822 ac_build_intrinsic(ctx, name, ctx->voidt,
823 args, ARRAY_SIZE(args),
824 writeonly_memory ?
825 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY :
826 AC_FUNC_ATTR_WRITEONLY);
Marek Olšák97e21cf2017-02-24 02:09:47 +0100827 return;
828 }
829
Marek Olšák27439df2017-02-24 01:20:35 +0100830 static unsigned dfmt[] = {
831 V_008F0C_BUF_DATA_FORMAT_32,
832 V_008F0C_BUF_DATA_FORMAT_32_32,
833 V_008F0C_BUF_DATA_FORMAT_32_32_32,
834 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
835 };
836 assert(num_channels >= 1 && num_channels <= 4);
837
Dave Airlie13a28ff2017-02-03 10:05:00 +1000838 LLVMValueRef args[] = {
839 rsrc,
840 vdata,
841 LLVMConstInt(ctx->i32, num_channels, 0),
Marek Olšák8cfdbba2017-02-24 20:23:23 +0100842 voffset ? voffset : LLVMGetUndef(ctx->i32),
Dave Airlie13a28ff2017-02-03 10:05:00 +1000843 soffset,
844 LLVMConstInt(ctx->i32, inst_offset, 0),
Marek Olšák27439df2017-02-24 01:20:35 +0100845 LLVMConstInt(ctx->i32, dfmt[num_channels - 1], 0),
846 LLVMConstInt(ctx->i32, V_008F0C_BUF_NUM_FORMAT_UINT, 0),
Marek Olšák8cfdbba2017-02-24 20:23:23 +0100847 LLVMConstInt(ctx->i32, voffset != NULL, 0),
Marek Olšák27439df2017-02-24 01:20:35 +0100848 LLVMConstInt(ctx->i32, 0, 0), /* idxen */
Dave Airlie13a28ff2017-02-03 10:05:00 +1000849 LLVMConstInt(ctx->i32, glc, 0),
850 LLVMConstInt(ctx->i32, slc, 0),
Marek Olšák27439df2017-02-24 01:20:35 +0100851 LLVMConstInt(ctx->i32, 0, 0), /* tfe*/
Dave Airlie13a28ff2017-02-03 10:05:00 +1000852 };
853
854 /* The instruction offset field has 12 bits */
Marek Olšák8cfdbba2017-02-24 20:23:23 +0100855 assert(voffset || inst_offset < (1 << 12));
Dave Airlie13a28ff2017-02-03 10:05:00 +1000856
857 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
858 unsigned func = CLAMP(num_channels, 1, 3) - 1;
859 const char *types[] = {"i32", "v2i32", "v4i32"};
860 char name[256];
861 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
862
Marek Olšák7f1446a2017-02-26 00:41:37 +0100863 ac_build_intrinsic(ctx, name, ctx->voidt,
864 args, ARRAY_SIZE(args),
865 AC_FUNC_ATTR_LEGACY);
Dave Airlie13a28ff2017-02-03 10:05:00 +1000866}
867
Dave Airlie13a28ff2017-02-03 10:05:00 +1000868LLVMValueRef
869ac_build_buffer_load(struct ac_llvm_context *ctx,
870 LLVMValueRef rsrc,
871 int num_channels,
872 LLVMValueRef vindex,
873 LLVMValueRef voffset,
874 LLVMValueRef soffset,
875 unsigned inst_offset,
876 unsigned glc,
Marek Olšáke729dc72017-02-24 17:16:28 +0100877 unsigned slc,
Marek Olšáke019ea82017-05-19 15:02:34 +0200878 bool can_speculate,
879 bool allow_smem)
Dave Airlie13a28ff2017-02-03 10:05:00 +1000880{
Marek Olšáke019ea82017-05-19 15:02:34 +0200881 LLVMValueRef offset = LLVMConstInt(ctx->i32, inst_offset, 0);
882 if (voffset)
883 offset = LLVMBuildAdd(ctx->builder, offset, voffset, "");
884 if (soffset)
885 offset = LLVMBuildAdd(ctx->builder, offset, soffset, "");
886
887 /* TODO: VI and later generations can use SMEM with GLC=1.*/
888 if (allow_smem && !glc && !slc) {
889 assert(vindex == NULL);
890
891 LLVMValueRef result[4];
892
893 for (int i = 0; i < num_channels; i++) {
894 if (i) {
895 offset = LLVMBuildAdd(ctx->builder, offset,
896 LLVMConstInt(ctx->i32, 4, 0), "");
897 }
898 LLVMValueRef args[2] = {rsrc, offset};
899 result[i] = ac_build_intrinsic(ctx, "llvm.SI.load.const.v4i32",
900 ctx->f32, args, 2,
901 AC_FUNC_ATTR_READNONE |
902 AC_FUNC_ATTR_LEGACY);
903 }
904 if (num_channels == 1)
905 return result[0];
906
907 if (num_channels == 3)
908 result[num_channels++] = LLVMGetUndef(ctx->f32);
909 return ac_build_gather_values(ctx, result, num_channels);
910 }
911
Dave Airlie13a28ff2017-02-03 10:05:00 +1000912 unsigned func = CLAMP(num_channels, 1, 3) - 1;
913
Marek Olšák12beef02017-04-25 02:18:10 +0200914 LLVMValueRef args[] = {
915 LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, ""),
916 vindex ? vindex : LLVMConstInt(ctx->i32, 0, 0),
Marek Olšáke019ea82017-05-19 15:02:34 +0200917 offset,
Marek Olšák12beef02017-04-25 02:18:10 +0200918 LLVMConstInt(ctx->i1, glc, 0),
919 LLVMConstInt(ctx->i1, slc, 0)
920 };
Dave Airlie13a28ff2017-02-03 10:05:00 +1000921
Marek Olšák12beef02017-04-25 02:18:10 +0200922 LLVMTypeRef types[] = {ctx->f32, LLVMVectorType(ctx->f32, 2),
923 ctx->v4f32};
924 const char *type_names[] = {"f32", "v2f32", "v4f32"};
925 char name[256];
Dave Airlie13a28ff2017-02-03 10:05:00 +1000926
Marek Olšák12beef02017-04-25 02:18:10 +0200927 snprintf(name, sizeof(name), "llvm.amdgcn.buffer.load.%s",
928 type_names[func]);
929
930 return ac_build_intrinsic(ctx, name, types[func], args,
931 ARRAY_SIZE(args),
932 /* READNONE means writes can't affect it, while
933 * READONLY means that writes can affect it. */
Marek Olšáke1942c92017-05-25 16:13:54 +0200934 can_speculate && HAVE_LLVM >= 0x0400 ?
Marek Olšák12beef02017-04-25 02:18:10 +0200935 AC_FUNC_ATTR_READNONE :
936 AC_FUNC_ATTR_READONLY);
Dave Airlie13a28ff2017-02-03 10:05:00 +1000937}
938
Marek Olšák94811dc2017-02-25 23:40:52 +0100939LLVMValueRef ac_build_buffer_load_format(struct ac_llvm_context *ctx,
940 LLVMValueRef rsrc,
941 LLVMValueRef vindex,
942 LLVMValueRef voffset,
Marek Olšáke1942c92017-05-25 16:13:54 +0200943 bool can_speculate)
Marek Olšák94811dc2017-02-25 23:40:52 +0100944{
Marek Olšák12beef02017-04-25 02:18:10 +0200945 LLVMValueRef args [] = {
946 LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, ""),
Marek Olšák94811dc2017-02-25 23:40:52 +0100947 vindex,
Marek Olšák12beef02017-04-25 02:18:10 +0200948 voffset,
949 LLVMConstInt(ctx->i1, 0, 0), /* glc */
950 LLVMConstInt(ctx->i1, 0, 0), /* slc */
Marek Olšák94811dc2017-02-25 23:40:52 +0100951 };
Marek Olšák12beef02017-04-25 02:18:10 +0200952
953 return ac_build_intrinsic(ctx,
954 "llvm.amdgcn.buffer.load.format.v4f32",
955 ctx->v4f32, args, ARRAY_SIZE(args),
956 /* READNONE means writes can't affect it, while
957 * READONLY means that writes can affect it. */
Marek Olšáke1942c92017-05-25 16:13:54 +0200958 can_speculate && HAVE_LLVM >= 0x0400 ?
Marek Olšák12beef02017-04-25 02:18:10 +0200959 AC_FUNC_ATTR_READNONE :
960 AC_FUNC_ATTR_READONLY);
Marek Olšák94811dc2017-02-25 23:40:52 +0100961}
962
Dave Airlie13a28ff2017-02-03 10:05:00 +1000963/**
964 * Set range metadata on an instruction. This can only be used on load and
965 * call instructions. If you know an instruction can only produce the values
966 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
967 * \p lo is the minimum value inclusive.
968 * \p hi is the maximum value exclusive.
969 */
970static void set_range_metadata(struct ac_llvm_context *ctx,
971 LLVMValueRef value, unsigned lo, unsigned hi)
972{
973 LLVMValueRef range_md, md_args[2];
974 LLVMTypeRef type = LLVMTypeOf(value);
975 LLVMContextRef context = LLVMGetTypeContext(type);
976
977 md_args[0] = LLVMConstInt(type, lo, false);
978 md_args[1] = LLVMConstInt(type, hi, false);
979 range_md = LLVMMDNodeInContext(context, md_args, 2);
980 LLVMSetMetadata(value, ctx->range_md_kind, range_md);
981}
982
983LLVMValueRef
984ac_get_thread_id(struct ac_llvm_context *ctx)
985{
986 LLVMValueRef tid;
987
Marek Olšák7e1faa72017-03-05 00:15:31 +0100988 LLVMValueRef tid_args[2];
989 tid_args[0] = LLVMConstInt(ctx->i32, 0xffffffff, false);
990 tid_args[1] = LLVMConstInt(ctx->i32, 0, false);
991 tid_args[1] = ac_build_intrinsic(ctx,
992 "llvm.amdgcn.mbcnt.lo", ctx->i32,
993 tid_args, 2, AC_FUNC_ATTR_READNONE);
Dave Airlie13a28ff2017-02-03 10:05:00 +1000994
Marek Olšák7e1faa72017-03-05 00:15:31 +0100995 tid = ac_build_intrinsic(ctx, "llvm.amdgcn.mbcnt.hi",
996 ctx->i32, tid_args,
997 2, AC_FUNC_ATTR_READNONE);
Dave Airlie13a28ff2017-02-03 10:05:00 +1000998 set_range_metadata(ctx, tid, 0, 64);
999 return tid;
1000}
1001
1002/*
1003 * SI implements derivatives using the local data store (LDS)
1004 * All writes to the LDS happen in all executing threads at
1005 * the same time. TID is the Thread ID for the current
1006 * thread and is a value between 0 and 63, representing
1007 * the thread's position in the wavefront.
1008 *
1009 * For the pixel shader threads are grouped into quads of four pixels.
1010 * The TIDs of the pixels of a quad are:
1011 *
1012 * +------+------+
1013 * |4n + 0|4n + 1|
1014 * +------+------+
1015 * |4n + 2|4n + 3|
1016 * +------+------+
1017 *
1018 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
1019 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
1020 * the current pixel's column, and masking with 0xfffffffe yields the TID
1021 * of the left pixel of the current pixel's row.
1022 *
1023 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
1024 * adding 2 yields the TID of the pixel below the top pixel.
1025 */
1026LLVMValueRef
Marek Olšák7f1446a2017-02-26 00:41:37 +01001027ac_build_ddxy(struct ac_llvm_context *ctx,
Marek Olšák7f1446a2017-02-26 00:41:37 +01001028 uint32_t mask,
1029 int idx,
Marek Olšák7f1446a2017-02-26 00:41:37 +01001030 LLVMValueRef val)
Dave Airlie13a28ff2017-02-03 10:05:00 +10001031{
Dave Airliecb6f16d2017-08-01 05:10:49 +01001032 LLVMValueRef tl, trbl, args[2];
Dave Airlie13a28ff2017-02-03 10:05:00 +10001033 LLVMValueRef result;
1034
Nicolai Hähnle67724522017-09-13 14:38:17 +02001035 if (ctx->chip_class >= VI) {
Dave Airliecb6f16d2017-08-01 05:10:49 +01001036 LLVMValueRef thread_id, tl_tid, trbl_tid;
1037 thread_id = ac_get_thread_id(ctx);
1038
1039 tl_tid = LLVMBuildAnd(ctx->builder, thread_id,
1040 LLVMConstInt(ctx->i32, mask, false), "");
1041
1042 trbl_tid = LLVMBuildAdd(ctx->builder, tl_tid,
1043 LLVMConstInt(ctx->i32, idx, false), "");
1044
Dave Airlie13a28ff2017-02-03 10:05:00 +10001045 args[0] = LLVMBuildMul(ctx->builder, tl_tid,
1046 LLVMConstInt(ctx->i32, 4, false), "");
1047 args[1] = val;
Marek Olšák7f1446a2017-02-26 00:41:37 +01001048 tl = ac_build_intrinsic(ctx,
1049 "llvm.amdgcn.ds.bpermute", ctx->i32,
Marek Olšákd5d74fe2017-02-26 19:00:44 +01001050 args, 2,
1051 AC_FUNC_ATTR_READNONE |
1052 AC_FUNC_ATTR_CONVERGENT);
Dave Airlie13a28ff2017-02-03 10:05:00 +10001053
1054 args[0] = LLVMBuildMul(ctx->builder, trbl_tid,
1055 LLVMConstInt(ctx->i32, 4, false), "");
Marek Olšák7f1446a2017-02-26 00:41:37 +01001056 trbl = ac_build_intrinsic(ctx,
1057 "llvm.amdgcn.ds.bpermute", ctx->i32,
Marek Olšákd5d74fe2017-02-26 19:00:44 +01001058 args, 2,
1059 AC_FUNC_ATTR_READNONE |
1060 AC_FUNC_ATTR_CONVERGENT);
Dave Airlie13a28ff2017-02-03 10:05:00 +10001061 } else {
Marek Olšák94d800b2017-10-04 16:59:40 +02001062 uint32_t masks[2] = {};
Dave Airlie13a28ff2017-02-03 10:05:00 +10001063
Dave Airliecb6f16d2017-08-01 05:10:49 +01001064 switch (mask) {
1065 case AC_TID_MASK_TOP_LEFT:
1066 masks[0] = 0x8000;
1067 if (idx == 1)
1068 masks[1] = 0x8055;
1069 else
1070 masks[1] = 0x80aa;
Dave Airlie13a28ff2017-02-03 10:05:00 +10001071
Dave Airliecb6f16d2017-08-01 05:10:49 +01001072 break;
1073 case AC_TID_MASK_TOP:
1074 masks[0] = 0x8044;
1075 masks[1] = 0x80ee;
1076 break;
1077 case AC_TID_MASK_LEFT:
1078 masks[0] = 0x80a0;
1079 masks[1] = 0x80f5;
1080 break;
Marek Olšák94d800b2017-10-04 16:59:40 +02001081 default:
1082 assert(0);
Dave Airliecb6f16d2017-08-01 05:10:49 +01001083 }
1084
1085 args[0] = val;
1086 args[1] = LLVMConstInt(ctx->i32, masks[0], false);
1087
1088 tl = ac_build_intrinsic(ctx,
1089 "llvm.amdgcn.ds.swizzle", ctx->i32,
1090 args, 2,
1091 AC_FUNC_ATTR_READNONE |
1092 AC_FUNC_ATTR_CONVERGENT);
1093
1094 args[1] = LLVMConstInt(ctx->i32, masks[1], false);
1095 trbl = ac_build_intrinsic(ctx,
1096 "llvm.amdgcn.ds.swizzle", ctx->i32,
1097 args, 2,
1098 AC_FUNC_ATTR_READNONE |
1099 AC_FUNC_ATTR_CONVERGENT);
Dave Airlie13a28ff2017-02-03 10:05:00 +10001100 }
1101
1102 tl = LLVMBuildBitCast(ctx->builder, tl, ctx->f32, "");
1103 trbl = LLVMBuildBitCast(ctx->builder, trbl, ctx->f32, "");
1104 result = LLVMBuildFSub(ctx->builder, trbl, tl, "");
1105 return result;
1106}
Dave Airlief32955b2017-02-13 22:08:30 +00001107
1108void
Marek Olšák7f1446a2017-02-26 00:41:37 +01001109ac_build_sendmsg(struct ac_llvm_context *ctx,
1110 uint32_t msg,
1111 LLVMValueRef wave_id)
Dave Airlief32955b2017-02-13 22:08:30 +00001112{
1113 LLVMValueRef args[2];
1114 const char *intr_name = (HAVE_LLVM < 0x0400) ? "llvm.SI.sendmsg" : "llvm.amdgcn.s.sendmsg";
1115 args[0] = LLVMConstInt(ctx->i32, msg, false);
1116 args[1] = wave_id;
Marek Olšák7f1446a2017-02-26 00:41:37 +01001117 ac_build_intrinsic(ctx, intr_name, ctx->voidt, args, 2, 0);
Dave Airlief32955b2017-02-13 22:08:30 +00001118}
Dave Airliecae1ff12017-02-16 03:42:56 +00001119
1120LLVMValueRef
Marek Olšák7f1446a2017-02-26 00:41:37 +01001121ac_build_imsb(struct ac_llvm_context *ctx,
1122 LLVMValueRef arg,
1123 LLVMTypeRef dst_type)
Dave Airliecae1ff12017-02-16 03:42:56 +00001124{
Marek Olšákedd23e02017-02-17 14:26:07 +01001125 const char *intr_name = (HAVE_LLVM < 0x0400) ? "llvm.AMDGPU.flbit.i32" :
1126 "llvm.amdgcn.sffbh.i32";
Marek Olšák7f1446a2017-02-26 00:41:37 +01001127 LLVMValueRef msb = ac_build_intrinsic(ctx, intr_name,
1128 dst_type, &arg, 1,
1129 AC_FUNC_ATTR_READNONE);
Dave Airliecae1ff12017-02-16 03:42:56 +00001130
1131 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
1132 * the index from LSB. Invert it by doing "31 - msb". */
1133 msb = LLVMBuildSub(ctx->builder, LLVMConstInt(ctx->i32, 31, false),
1134 msb, "");
1135
1136 LLVMValueRef all_ones = LLVMConstInt(ctx->i32, -1, true);
1137 LLVMValueRef cond = LLVMBuildOr(ctx->builder,
1138 LLVMBuildICmp(ctx->builder, LLVMIntEQ,
1139 arg, LLVMConstInt(ctx->i32, 0, 0), ""),
1140 LLVMBuildICmp(ctx->builder, LLVMIntEQ,
1141 arg, all_ones, ""), "");
1142
1143 return LLVMBuildSelect(ctx->builder, cond, all_ones, msb, "");
1144}
Dave Airlie0ec66b92017-02-16 03:53:27 +00001145
1146LLVMValueRef
Marek Olšák7f1446a2017-02-26 00:41:37 +01001147ac_build_umsb(struct ac_llvm_context *ctx,
1148 LLVMValueRef arg,
1149 LLVMTypeRef dst_type)
Dave Airlie0ec66b92017-02-16 03:53:27 +00001150{
1151 LLVMValueRef args[2] = {
1152 arg,
Marek Olšákedd23e02017-02-17 14:26:07 +01001153 LLVMConstInt(ctx->i1, 1, 0),
Dave Airlie0ec66b92017-02-16 03:53:27 +00001154 };
Marek Olšák7f1446a2017-02-26 00:41:37 +01001155 LLVMValueRef msb = ac_build_intrinsic(ctx, "llvm.ctlz.i32",
1156 dst_type, args, ARRAY_SIZE(args),
1157 AC_FUNC_ATTR_READNONE);
Dave Airlie0ec66b92017-02-16 03:53:27 +00001158
1159 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
1160 * the index from LSB. Invert it by doing "31 - msb". */
1161 msb = LLVMBuildSub(ctx->builder, LLVMConstInt(ctx->i32, 31, false),
1162 msb, "");
1163
1164 /* check for zero */
1165 return LLVMBuildSelect(ctx->builder,
1166 LLVMBuildICmp(ctx->builder, LLVMIntEQ, arg,
1167 LLVMConstInt(ctx->i32, 0, 0), ""),
1168 LLVMConstInt(ctx->i32, -1, true), msb, "");
1169}
Marek Olšák660b55e2017-02-16 22:41:16 +01001170
Nicolai Hähnlea69afb62017-06-25 17:56:37 +02001171LLVMValueRef ac_build_umin(struct ac_llvm_context *ctx, LLVMValueRef a,
1172 LLVMValueRef b)
1173{
1174 LLVMValueRef cmp = LLVMBuildICmp(ctx->builder, LLVMIntULE, a, b, "");
1175 return LLVMBuildSelect(ctx->builder, cmp, a, b, "");
1176}
1177
Marek Olšák7f1446a2017-02-26 00:41:37 +01001178LLVMValueRef ac_build_clamp(struct ac_llvm_context *ctx, LLVMValueRef value)
Marek Olšák660b55e2017-02-16 22:41:16 +01001179{
Marek Olšák675ef9c2017-02-16 22:52:53 +01001180 if (HAVE_LLVM >= 0x0500) {
1181 LLVMValueRef max[2] = {
1182 value,
1183 LLVMConstReal(ctx->f32, 0),
1184 };
1185 LLVMValueRef min[2] = {
1186 LLVMConstReal(ctx->f32, 1),
1187 };
1188
Marek Olšák7f1446a2017-02-26 00:41:37 +01001189 min[1] = ac_build_intrinsic(ctx, "llvm.maxnum.f32",
1190 ctx->f32, max, 2,
1191 AC_FUNC_ATTR_READNONE);
1192 return ac_build_intrinsic(ctx, "llvm.minnum.f32",
1193 ctx->f32, min, 2,
1194 AC_FUNC_ATTR_READNONE);
Marek Olšák675ef9c2017-02-16 22:52:53 +01001195 }
1196
Marek Olšák660b55e2017-02-16 22:41:16 +01001197 LLVMValueRef args[3] = {
1198 value,
1199 LLVMConstReal(ctx->f32, 0),
1200 LLVMConstReal(ctx->f32, 1),
1201 };
1202
Marek Olšák7e1faa72017-03-05 00:15:31 +01001203 return ac_build_intrinsic(ctx, "llvm.AMDGPU.clamp.", ctx->f32, args, 3,
Marek Olšák7f1446a2017-02-26 00:41:37 +01001204 AC_FUNC_ATTR_READNONE |
1205 AC_FUNC_ATTR_LEGACY);
Marek Olšák660b55e2017-02-16 22:41:16 +01001206}
Marek Olšák369f4a82017-02-23 02:06:40 +01001207
Marek Olšák7f1446a2017-02-26 00:41:37 +01001208void ac_build_export(struct ac_llvm_context *ctx, struct ac_export_args *a)
Marek Olšák369f4a82017-02-23 02:06:40 +01001209{
1210 LLVMValueRef args[9];
1211
Marek Olšák2b3ebe32017-02-23 02:15:54 +01001212 if (HAVE_LLVM >= 0x0500) {
1213 args[0] = LLVMConstInt(ctx->i32, a->target, 0);
1214 args[1] = LLVMConstInt(ctx->i32, a->enabled_channels, 0);
1215
1216 if (a->compr) {
1217 LLVMTypeRef i16 = LLVMInt16TypeInContext(ctx->context);
1218 LLVMTypeRef v2i16 = LLVMVectorType(i16, 2);
1219
1220 args[2] = LLVMBuildBitCast(ctx->builder, a->out[0],
1221 v2i16, "");
1222 args[3] = LLVMBuildBitCast(ctx->builder, a->out[1],
1223 v2i16, "");
1224 args[4] = LLVMConstInt(ctx->i1, a->done, 0);
1225 args[5] = LLVMConstInt(ctx->i1, a->valid_mask, 0);
1226
Marek Olšák7f1446a2017-02-26 00:41:37 +01001227 ac_build_intrinsic(ctx, "llvm.amdgcn.exp.compr.v2i16",
1228 ctx->voidt, args, 6, 0);
Marek Olšák2b3ebe32017-02-23 02:15:54 +01001229 } else {
1230 args[2] = a->out[0];
1231 args[3] = a->out[1];
1232 args[4] = a->out[2];
1233 args[5] = a->out[3];
1234 args[6] = LLVMConstInt(ctx->i1, a->done, 0);
1235 args[7] = LLVMConstInt(ctx->i1, a->valid_mask, 0);
1236
Marek Olšák7f1446a2017-02-26 00:41:37 +01001237 ac_build_intrinsic(ctx, "llvm.amdgcn.exp.f32",
1238 ctx->voidt, args, 8, 0);
Marek Olšák2b3ebe32017-02-23 02:15:54 +01001239 }
1240 return;
1241 }
1242
Marek Olšák369f4a82017-02-23 02:06:40 +01001243 args[0] = LLVMConstInt(ctx->i32, a->enabled_channels, 0);
1244 args[1] = LLVMConstInt(ctx->i32, a->valid_mask, 0);
1245 args[2] = LLVMConstInt(ctx->i32, a->done, 0);
1246 args[3] = LLVMConstInt(ctx->i32, a->target, 0);
1247 args[4] = LLVMConstInt(ctx->i32, a->compr, 0);
1248 memcpy(args + 5, a->out, sizeof(a->out[0]) * 4);
1249
Marek Olšák7f1446a2017-02-26 00:41:37 +01001250 ac_build_intrinsic(ctx, "llvm.SI.export", ctx->voidt, args, 9,
1251 AC_FUNC_ATTR_LEGACY);
Marek Olšák369f4a82017-02-23 02:06:40 +01001252}
Marek Olšákad18d7f2017-02-23 23:00:19 +01001253
Marek Olšák7f1446a2017-02-26 00:41:37 +01001254LLVMValueRef ac_build_image_opcode(struct ac_llvm_context *ctx,
1255 struct ac_image_args *a)
Marek Olšákad18d7f2017-02-23 23:00:19 +01001256{
1257 LLVMTypeRef dst_type;
1258 LLVMValueRef args[11];
1259 unsigned num_args = 0;
1260 const char *name;
1261 char intr_name[128], type[64];
1262
Marek Olšák4b2e5b92017-02-23 23:37:59 +01001263 if (HAVE_LLVM >= 0x0400) {
1264 bool sample = a->opcode == ac_image_sample ||
1265 a->opcode == ac_image_gather4 ||
1266 a->opcode == ac_image_get_lod;
1267
1268 if (sample)
Connor Abbottb909d272017-07-18 17:35:35 -07001269 args[num_args++] = ac_to_float(ctx, a->addr);
Marek Olšák4b2e5b92017-02-23 23:37:59 +01001270 else
1271 args[num_args++] = a->addr;
1272
1273 args[num_args++] = a->resource;
1274 if (sample)
1275 args[num_args++] = a->sampler;
1276 args[num_args++] = LLVMConstInt(ctx->i32, a->dmask, 0);
1277 if (sample)
1278 args[num_args++] = LLVMConstInt(ctx->i1, a->unorm, 0);
1279 args[num_args++] = LLVMConstInt(ctx->i1, 0, 0); /* glc */
1280 args[num_args++] = LLVMConstInt(ctx->i1, 0, 0); /* slc */
1281 args[num_args++] = LLVMConstInt(ctx->i1, 0, 0); /* lwe */
1282 args[num_args++] = LLVMConstInt(ctx->i1, a->da, 0);
1283
1284 switch (a->opcode) {
1285 case ac_image_sample:
1286 name = "llvm.amdgcn.image.sample";
1287 break;
1288 case ac_image_gather4:
1289 name = "llvm.amdgcn.image.gather4";
1290 break;
1291 case ac_image_load:
1292 name = "llvm.amdgcn.image.load";
1293 break;
1294 case ac_image_load_mip:
1295 name = "llvm.amdgcn.image.load.mip";
1296 break;
1297 case ac_image_get_lod:
1298 name = "llvm.amdgcn.image.getlod";
1299 break;
1300 case ac_image_get_resinfo:
1301 name = "llvm.amdgcn.image.getresinfo";
1302 break;
Samuel Pitoiseta1c37ff2017-04-07 18:44:16 +02001303 default:
1304 unreachable("invalid image opcode");
Marek Olšák4b2e5b92017-02-23 23:37:59 +01001305 }
1306
1307 ac_build_type_name_for_intr(LLVMTypeOf(args[0]), type,
1308 sizeof(type));
1309
1310 snprintf(intr_name, sizeof(intr_name), "%s%s%s%s.v4f32.%s.v8i32",
1311 name,
1312 a->compare ? ".c" : "",
1313 a->bias ? ".b" :
1314 a->lod ? ".l" :
1315 a->deriv ? ".d" :
1316 a->level_zero ? ".lz" : "",
1317 a->offset ? ".o" : "",
1318 type);
1319
1320 LLVMValueRef result =
Marek Olšák7f1446a2017-02-26 00:41:37 +01001321 ac_build_intrinsic(ctx, intr_name,
1322 ctx->v4f32, args, num_args,
1323 AC_FUNC_ATTR_READNONE);
Marek Olšák4b2e5b92017-02-23 23:37:59 +01001324 if (!sample) {
1325 result = LLVMBuildBitCast(ctx->builder, result,
1326 ctx->v4i32, "");
1327 }
1328 return result;
1329 }
1330
Marek Olšákad18d7f2017-02-23 23:00:19 +01001331 args[num_args++] = a->addr;
1332 args[num_args++] = a->resource;
1333
1334 if (a->opcode == ac_image_load ||
1335 a->opcode == ac_image_load_mip ||
1336 a->opcode == ac_image_get_resinfo) {
1337 dst_type = ctx->v4i32;
1338 } else {
1339 dst_type = ctx->v4f32;
1340 args[num_args++] = a->sampler;
1341 }
1342
1343 args[num_args++] = LLVMConstInt(ctx->i32, a->dmask, 0);
1344 args[num_args++] = LLVMConstInt(ctx->i32, a->unorm, 0);
1345 args[num_args++] = LLVMConstInt(ctx->i32, 0, 0); /* r128 */
1346 args[num_args++] = LLVMConstInt(ctx->i32, a->da, 0);
1347 args[num_args++] = LLVMConstInt(ctx->i32, 0, 0); /* glc */
1348 args[num_args++] = LLVMConstInt(ctx->i32, 0, 0); /* slc */
1349 args[num_args++] = LLVMConstInt(ctx->i32, 0, 0); /* tfe */
1350 args[num_args++] = LLVMConstInt(ctx->i32, 0, 0); /* lwe */
1351
1352 switch (a->opcode) {
1353 case ac_image_sample:
1354 name = "llvm.SI.image.sample";
1355 break;
1356 case ac_image_gather4:
1357 name = "llvm.SI.gather4";
1358 break;
1359 case ac_image_load:
1360 name = "llvm.SI.image.load";
1361 break;
1362 case ac_image_load_mip:
1363 name = "llvm.SI.image.load.mip";
1364 break;
1365 case ac_image_get_lod:
1366 name = "llvm.SI.getlod";
1367 break;
1368 case ac_image_get_resinfo:
1369 name = "llvm.SI.getresinfo";
1370 break;
1371 }
1372
1373 ac_build_type_name_for_intr(LLVMTypeOf(a->addr), type, sizeof(type));
1374 snprintf(intr_name, sizeof(intr_name), "%s%s%s%s.%s",
1375 name,
1376 a->compare ? ".c" : "",
1377 a->bias ? ".b" :
1378 a->lod ? ".l" :
1379 a->deriv ? ".d" :
1380 a->level_zero ? ".lz" : "",
1381 a->offset ? ".o" : "",
1382 type);
1383
Marek Olšák7f1446a2017-02-26 00:41:37 +01001384 return ac_build_intrinsic(ctx, intr_name,
1385 dst_type, args, num_args,
1386 AC_FUNC_ATTR_READNONE |
1387 AC_FUNC_ATTR_LEGACY);
Marek Olšákad18d7f2017-02-23 23:00:19 +01001388}
Marek Olšák653ac0b2017-02-24 16:38:25 +01001389
Marek Olšák7f1446a2017-02-26 00:41:37 +01001390LLVMValueRef ac_build_cvt_pkrtz_f16(struct ac_llvm_context *ctx,
1391 LLVMValueRef args[2])
Marek Olšák653ac0b2017-02-24 16:38:25 +01001392{
1393 if (HAVE_LLVM >= 0x0500) {
1394 LLVMTypeRef v2f16 =
1395 LLVMVectorType(LLVMHalfTypeInContext(ctx->context), 2);
1396 LLVMValueRef res =
Marek Olšák7f1446a2017-02-26 00:41:37 +01001397 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pkrtz",
1398 v2f16, args, 2,
1399 AC_FUNC_ATTR_READNONE);
Marek Olšák653ac0b2017-02-24 16:38:25 +01001400 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
1401 }
1402
Marek Olšák7f1446a2017-02-26 00:41:37 +01001403 return ac_build_intrinsic(ctx, "llvm.SI.packf16", ctx->i32, args, 2,
1404 AC_FUNC_ATTR_READNONE |
1405 AC_FUNC_ATTR_LEGACY);
Marek Olšák653ac0b2017-02-24 16:38:25 +01001406}
Marek Olšák9c095922017-02-24 22:44:18 +01001407
1408/**
1409 * KILL, AKA discard in GLSL.
1410 *
1411 * \param value kill if value < 0.0 or value == NULL.
1412 */
Marek Olšák7f1446a2017-02-26 00:41:37 +01001413void ac_build_kill(struct ac_llvm_context *ctx, LLVMValueRef value)
Marek Olšák9c095922017-02-24 22:44:18 +01001414{
1415 if (value) {
Marek Olšák7f1446a2017-02-26 00:41:37 +01001416 ac_build_intrinsic(ctx, "llvm.AMDGPU.kill", ctx->voidt,
1417 &value, 1, AC_FUNC_ATTR_LEGACY);
Marek Olšák9c095922017-02-24 22:44:18 +01001418 } else {
Marek Olšák7f1446a2017-02-26 00:41:37 +01001419 ac_build_intrinsic(ctx, "llvm.AMDGPU.kilp", ctx->voidt,
1420 NULL, 0, AC_FUNC_ATTR_LEGACY);
Marek Olšák9c095922017-02-24 22:44:18 +01001421 }
1422}
Marek Olšákd4324dd2017-02-24 23:06:31 +01001423
Marek Olšák7f1446a2017-02-26 00:41:37 +01001424LLVMValueRef ac_build_bfe(struct ac_llvm_context *ctx, LLVMValueRef input,
1425 LLVMValueRef offset, LLVMValueRef width,
1426 bool is_signed)
Marek Olšákd4324dd2017-02-24 23:06:31 +01001427{
1428 LLVMValueRef args[] = {
1429 input,
1430 offset,
1431 width,
1432 };
1433
1434 if (HAVE_LLVM >= 0x0500) {
Marek Olšák7f1446a2017-02-26 00:41:37 +01001435 return ac_build_intrinsic(ctx,
1436 is_signed ? "llvm.amdgcn.sbfe.i32" :
1437 "llvm.amdgcn.ubfe.i32",
1438 ctx->i32, args, 3,
1439 AC_FUNC_ATTR_READNONE);
Marek Olšákd4324dd2017-02-24 23:06:31 +01001440 }
1441
Marek Olšák7f1446a2017-02-26 00:41:37 +01001442 return ac_build_intrinsic(ctx,
1443 is_signed ? "llvm.AMDGPU.bfe.i32" :
1444 "llvm.AMDGPU.bfe.u32",
1445 ctx->i32, args, 3,
1446 AC_FUNC_ATTR_READNONE |
1447 AC_FUNC_ATTR_LEGACY);
Marek Olšákd4324dd2017-02-24 23:06:31 +01001448}
Dave Airlie10ae83a2017-03-06 08:37:22 +10001449
1450void ac_get_image_intr_name(const char *base_name,
1451 LLVMTypeRef data_type,
1452 LLVMTypeRef coords_type,
1453 LLVMTypeRef rsrc_type,
1454 char *out_name, unsigned out_len)
1455{
1456 char coords_type_name[8];
1457
1458 ac_build_type_name_for_intr(coords_type, coords_type_name,
1459 sizeof(coords_type_name));
1460
1461 if (HAVE_LLVM <= 0x0309) {
1462 snprintf(out_name, out_len, "%s.%s", base_name, coords_type_name);
1463 } else {
1464 char data_type_name[8];
1465 char rsrc_type_name[8];
1466
1467 ac_build_type_name_for_intr(data_type, data_type_name,
1468 sizeof(data_type_name));
1469 ac_build_type_name_for_intr(rsrc_type, rsrc_type_name,
1470 sizeof(rsrc_type_name));
1471 snprintf(out_name, out_len, "%s.%s.%s.%s", base_name,
1472 data_type_name, coords_type_name, rsrc_type_name);
1473 }
1474}
Dave Airliee2659172017-04-25 23:33:29 +01001475
1476#define AC_EXP_TARGET (HAVE_LLVM >= 0x0500 ? 0 : 3)
1477#define AC_EXP_OUT0 (HAVE_LLVM >= 0x0500 ? 2 : 5)
1478
Marek Olšákfaa37472017-04-29 23:47:08 +02001479enum ac_ir_type {
1480 AC_IR_UNDEF,
1481 AC_IR_CONST,
1482 AC_IR_VALUE,
1483};
1484
1485struct ac_vs_exp_chan
1486{
1487 LLVMValueRef value;
1488 float const_float;
1489 enum ac_ir_type type;
1490};
1491
1492struct ac_vs_exp_inst {
1493 unsigned offset;
1494 LLVMValueRef inst;
1495 struct ac_vs_exp_chan chan[4];
1496};
1497
1498struct ac_vs_exports {
1499 unsigned num;
1500 struct ac_vs_exp_inst exp[VARYING_SLOT_MAX];
1501};
1502
Dave Airliee2659172017-04-25 23:33:29 +01001503/* Return true if the PARAM export has been eliminated. */
1504static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset,
1505 uint32_t num_outputs,
Marek Olšákfaa37472017-04-29 23:47:08 +02001506 struct ac_vs_exp_inst *exp)
Dave Airliee2659172017-04-25 23:33:29 +01001507{
1508 unsigned i, default_val; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
1509 bool is_zero[4] = {}, is_one[4] = {};
1510
1511 for (i = 0; i < 4; i++) {
Dave Airliee2659172017-04-25 23:33:29 +01001512 /* It's a constant expression. Undef outputs are eliminated too. */
Marek Olšákfaa37472017-04-29 23:47:08 +02001513 if (exp->chan[i].type == AC_IR_UNDEF) {
Dave Airliee2659172017-04-25 23:33:29 +01001514 is_zero[i] = true;
1515 is_one[i] = true;
Marek Olšákfaa37472017-04-29 23:47:08 +02001516 } else if (exp->chan[i].type == AC_IR_CONST) {
1517 if (exp->chan[i].const_float == 0)
Dave Airliee2659172017-04-25 23:33:29 +01001518 is_zero[i] = true;
Marek Olšákfaa37472017-04-29 23:47:08 +02001519 else if (exp->chan[i].const_float == 1)
Dave Airliee2659172017-04-25 23:33:29 +01001520 is_one[i] = true;
1521 else
1522 return false; /* other constant */
1523 } else
1524 return false;
1525 }
1526
1527 /* Only certain combinations of 0 and 1 can be eliminated. */
1528 if (is_zero[0] && is_zero[1] && is_zero[2])
1529 default_val = is_zero[3] ? 0 : 1;
1530 else if (is_one[0] && is_one[1] && is_one[2])
1531 default_val = is_zero[3] ? 2 : 3;
1532 else
1533 return false;
1534
1535 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
Marek Olšákfaa37472017-04-29 23:47:08 +02001536 LLVMInstructionEraseFromParent(exp->inst);
Dave Airliee2659172017-04-25 23:33:29 +01001537
1538 /* Change OFFSET to DEFAULT_VAL. */
1539 for (i = 0; i < num_outputs; i++) {
Marek Olšákfaa37472017-04-29 23:47:08 +02001540 if (vs_output_param_offset[i] == exp->offset) {
Dave Airliee2659172017-04-25 23:33:29 +01001541 vs_output_param_offset[i] =
1542 AC_EXP_PARAM_DEFAULT_VAL_0000 + default_val;
1543 break;
1544 }
1545 }
1546 return true;
1547}
1548
Marek Olšákb0871542017-04-29 23:56:03 +02001549static bool ac_eliminate_duplicated_output(uint8_t *vs_output_param_offset,
1550 uint32_t num_outputs,
1551 struct ac_vs_exports *processed,
1552 struct ac_vs_exp_inst *exp)
1553{
1554 unsigned p, copy_back_channels = 0;
1555
1556 /* See if the output is already in the list of processed outputs.
1557 * The LLVMValueRef comparison relies on SSA.
1558 */
1559 for (p = 0; p < processed->num; p++) {
1560 bool different = false;
1561
1562 for (unsigned j = 0; j < 4; j++) {
1563 struct ac_vs_exp_chan *c1 = &processed->exp[p].chan[j];
1564 struct ac_vs_exp_chan *c2 = &exp->chan[j];
1565
1566 /* Treat undef as a match. */
1567 if (c2->type == AC_IR_UNDEF)
1568 continue;
1569
1570 /* If c1 is undef but c2 isn't, we can copy c2 to c1
1571 * and consider the instruction duplicated.
1572 */
1573 if (c1->type == AC_IR_UNDEF) {
1574 copy_back_channels |= 1 << j;
1575 continue;
1576 }
1577
1578 /* Test whether the channels are not equal. */
1579 if (c1->type != c2->type ||
1580 (c1->type == AC_IR_CONST &&
1581 c1->const_float != c2->const_float) ||
1582 (c1->type == AC_IR_VALUE &&
1583 c1->value != c2->value)) {
1584 different = true;
1585 break;
1586 }
1587 }
1588 if (!different)
1589 break;
1590
1591 copy_back_channels = 0;
1592 }
1593 if (p == processed->num)
1594 return false;
1595
1596 /* If a match was found, but the matching export has undef where the new
1597 * one has a normal value, copy the normal value to the undef channel.
1598 */
1599 struct ac_vs_exp_inst *match = &processed->exp[p];
1600
1601 while (copy_back_channels) {
1602 unsigned chan = u_bit_scan(&copy_back_channels);
1603
1604 assert(match->chan[chan].type == AC_IR_UNDEF);
1605 LLVMSetOperand(match->inst, AC_EXP_OUT0 + chan,
1606 exp->chan[chan].value);
1607 match->chan[chan] = exp->chan[chan];
1608 }
1609
1610 /* The PARAM export is duplicated. Kill it. */
1611 LLVMInstructionEraseFromParent(exp->inst);
1612
1613 /* Change OFFSET to the matching export. */
1614 for (unsigned i = 0; i < num_outputs; i++) {
1615 if (vs_output_param_offset[i] == exp->offset) {
1616 vs_output_param_offset[i] = match->offset;
1617 break;
1618 }
1619 }
1620 return true;
1621}
1622
Marek Olšák7647e902017-04-29 23:53:08 +02001623void ac_optimize_vs_outputs(struct ac_llvm_context *ctx,
1624 LLVMValueRef main_fn,
1625 uint8_t *vs_output_param_offset,
1626 uint32_t num_outputs,
1627 uint8_t *num_param_exports)
Dave Airliee2659172017-04-25 23:33:29 +01001628{
1629 LLVMBasicBlockRef bb;
1630 bool removed_any = false;
1631 struct ac_vs_exports exports;
1632
Dave Airliee2659172017-04-25 23:33:29 +01001633 exports.num = 0;
1634
1635 /* Process all LLVM instructions. */
1636 bb = LLVMGetFirstBasicBlock(main_fn);
1637 while (bb) {
1638 LLVMValueRef inst = LLVMGetFirstInstruction(bb);
1639
1640 while (inst) {
1641 LLVMValueRef cur = inst;
1642 inst = LLVMGetNextInstruction(inst);
Marek Olšákfaa37472017-04-29 23:47:08 +02001643 struct ac_vs_exp_inst exp;
Dave Airliee2659172017-04-25 23:33:29 +01001644
1645 if (LLVMGetInstructionOpcode(cur) != LLVMCall)
1646 continue;
1647
1648 LLVMValueRef callee = ac_llvm_get_called_value(cur);
1649
1650 if (!ac_llvm_is_function(callee))
1651 continue;
1652
1653 const char *name = LLVMGetValueName(callee);
1654 unsigned num_args = LLVMCountParams(callee);
1655
1656 /* Check if this is an export instruction. */
1657 if ((num_args != 9 && num_args != 8) ||
1658 (strcmp(name, "llvm.SI.export") &&
1659 strcmp(name, "llvm.amdgcn.exp.f32")))
1660 continue;
1661
1662 LLVMValueRef arg = LLVMGetOperand(cur, AC_EXP_TARGET);
1663 unsigned target = LLVMConstIntGetZExtValue(arg);
1664
1665 if (target < V_008DFC_SQ_EXP_PARAM)
1666 continue;
1667
1668 target -= V_008DFC_SQ_EXP_PARAM;
1669
Marek Olšákfaa37472017-04-29 23:47:08 +02001670 /* Parse the instruction. */
1671 memset(&exp, 0, sizeof(exp));
1672 exp.offset = target;
1673 exp.inst = cur;
1674
1675 for (unsigned i = 0; i < 4; i++) {
1676 LLVMValueRef v = LLVMGetOperand(cur, AC_EXP_OUT0 + i);
1677
1678 exp.chan[i].value = v;
1679
1680 if (LLVMIsUndef(v)) {
1681 exp.chan[i].type = AC_IR_UNDEF;
1682 } else if (LLVMIsAConstantFP(v)) {
1683 LLVMBool loses_info;
1684 exp.chan[i].type = AC_IR_CONST;
1685 exp.chan[i].const_float =
1686 LLVMConstRealGetDouble(v, &loses_info);
1687 } else {
1688 exp.chan[i].type = AC_IR_VALUE;
1689 }
1690 }
1691
Marek Olšákb0871542017-04-29 23:56:03 +02001692 /* Eliminate constant and duplicated PARAM exports. */
Dave Airliee2659172017-04-25 23:33:29 +01001693 if (ac_eliminate_const_output(vs_output_param_offset,
Marek Olšákb0871542017-04-29 23:56:03 +02001694 num_outputs, &exp) ||
1695 ac_eliminate_duplicated_output(vs_output_param_offset,
1696 num_outputs, &exports,
1697 &exp)) {
Dave Airliee2659172017-04-25 23:33:29 +01001698 removed_any = true;
1699 } else {
Marek Olšákfaa37472017-04-29 23:47:08 +02001700 exports.exp[exports.num++] = exp;
Dave Airliee2659172017-04-25 23:33:29 +01001701 }
1702 }
1703 bb = LLVMGetNextBasicBlock(bb);
1704 }
1705
1706 /* Remove holes in export memory due to removed PARAM exports.
1707 * This is done by renumbering all PARAM exports.
1708 */
1709 if (removed_any) {
Marek Olšák34bc4702017-05-08 16:37:26 +02001710 uint8_t old_offset[VARYING_SLOT_MAX];
Dave Airliee2659172017-04-25 23:33:29 +01001711 unsigned out, i;
1712
1713 /* Make a copy of the offsets. We need the old version while
1714 * we are modifying some of them. */
Marek Olšák34bc4702017-05-08 16:37:26 +02001715 memcpy(old_offset, vs_output_param_offset,
1716 sizeof(old_offset));
Dave Airliee2659172017-04-25 23:33:29 +01001717
1718 for (i = 0; i < exports.num; i++) {
Marek Olšákfaa37472017-04-29 23:47:08 +02001719 unsigned offset = exports.exp[i].offset;
Dave Airliee2659172017-04-25 23:33:29 +01001720
Marek Olšák34bc4702017-05-08 16:37:26 +02001721 /* Update vs_output_param_offset. Multiple outputs can
1722 * have the same offset.
1723 */
Dave Airliee2659172017-04-25 23:33:29 +01001724 for (out = 0; out < num_outputs; out++) {
Marek Olšák34bc4702017-05-08 16:37:26 +02001725 if (old_offset[out] == offset)
1726 vs_output_param_offset[out] = i;
Dave Airliee2659172017-04-25 23:33:29 +01001727 }
Marek Olšák34bc4702017-05-08 16:37:26 +02001728
1729 /* Change the PARAM offset in the instruction. */
1730 LLVMSetOperand(exports.exp[i].inst, AC_EXP_TARGET,
1731 LLVMConstInt(ctx->i32,
1732 V_008DFC_SQ_EXP_PARAM + i, 0));
Dave Airliee2659172017-04-25 23:33:29 +01001733 }
Marek Olšák34bc4702017-05-08 16:37:26 +02001734 *num_param_exports = exports.num;
Dave Airliee2659172017-04-25 23:33:29 +01001735 }
1736}