fca2527f28d7a120d308cd88e809ff20083fc02c
[mesa.git] / src / gallium / drivers / radeonsi / si_shader_tgsi_alu.c
1 /*
2 * Copyright 2016 Advanced Micro Devices, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #include "si_shader_internal.h"
26 #include "ac_llvm_util.h"
27
28 void si_llvm_emit_kill(struct ac_shader_abi *abi, LLVMValueRef visible)
29 {
30 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
31 LLVMBuilderRef builder = ctx->ac.builder;
32
33 if (ctx->shader->selector->force_correct_derivs_after_kill) {
34 /* Kill immediately while maintaining WQM. */
35 ac_build_kill_if_false(&ctx->ac,
36 ac_build_wqm_vote(&ctx->ac, visible));
37
38 LLVMValueRef mask = LLVMBuildLoad(builder, ctx->postponed_kill, "");
39 mask = LLVMBuildAnd(builder, mask, visible, "");
40 LLVMBuildStore(builder, mask, ctx->postponed_kill);
41 return;
42 }
43
44 ac_build_kill_if_false(&ctx->ac, visible);
45 }
46
47 static void kil_emit(const struct lp_build_tgsi_action *action,
48 struct lp_build_tgsi_context *bld_base,
49 struct lp_build_emit_data *emit_data)
50 {
51 struct si_shader_context *ctx = si_shader_context(bld_base);
52 LLVMValueRef visible;
53
54 if (emit_data->inst->Instruction.Opcode == TGSI_OPCODE_KILL_IF) {
55 const struct tgsi_full_instruction *inst = emit_data->inst;
56 struct si_shader_context *ctx = si_shader_context(bld_base);
57 LLVMBuilderRef builder = ctx->ac.builder;
58 unsigned i;
59 LLVMValueRef conds[TGSI_NUM_CHANNELS];
60
61 for (i = 0; i < TGSI_NUM_CHANNELS; i++) {
62 LLVMValueRef value = lp_build_emit_fetch(bld_base, inst, 0, i);
63 /* UGE because NaN shouldn't get killed */
64 conds[i] = LLVMBuildFCmp(builder, LLVMRealUGE, value,
65 ctx->ac.f32_0, "");
66 }
67
68 /* And the conditions together */
69 for (i = TGSI_NUM_CHANNELS - 1; i > 0; i--) {
70 conds[i - 1] = LLVMBuildAnd(builder, conds[i], conds[i - 1], "");
71 }
72 visible = conds[0];
73 } else {
74 assert(emit_data->inst->Instruction.Opcode == TGSI_OPCODE_KILL);
75 visible = ctx->i1false;
76 }
77
78 si_llvm_emit_kill(&ctx->abi, visible);
79 }
80
81 static void emit_icmp(const struct lp_build_tgsi_action *action,
82 struct lp_build_tgsi_context *bld_base,
83 struct lp_build_emit_data *emit_data)
84 {
85 unsigned pred;
86 struct si_shader_context *ctx = si_shader_context(bld_base);
87
88 switch (emit_data->inst->Instruction.Opcode) {
89 case TGSI_OPCODE_USEQ:
90 case TGSI_OPCODE_U64SEQ: pred = LLVMIntEQ; break;
91 case TGSI_OPCODE_USNE:
92 case TGSI_OPCODE_U64SNE: pred = LLVMIntNE; break;
93 case TGSI_OPCODE_USGE:
94 case TGSI_OPCODE_U64SGE: pred = LLVMIntUGE; break;
95 case TGSI_OPCODE_USLT:
96 case TGSI_OPCODE_U64SLT: pred = LLVMIntULT; break;
97 case TGSI_OPCODE_ISGE:
98 case TGSI_OPCODE_I64SGE: pred = LLVMIntSGE; break;
99 case TGSI_OPCODE_ISLT:
100 case TGSI_OPCODE_I64SLT: pred = LLVMIntSLT; break;
101 default:
102 assert(!"unknown instruction");
103 pred = 0;
104 break;
105 }
106
107 LLVMValueRef v = LLVMBuildICmp(ctx->ac.builder, pred,
108 emit_data->args[0], emit_data->args[1],"");
109
110 v = LLVMBuildSExtOrBitCast(ctx->ac.builder, v, ctx->i32, "");
111
112 emit_data->output[emit_data->chan] = v;
113 }
114
115 static void emit_ucmp(const struct lp_build_tgsi_action *action,
116 struct lp_build_tgsi_context *bld_base,
117 struct lp_build_emit_data *emit_data)
118 {
119 struct si_shader_context *ctx = si_shader_context(bld_base);
120 LLVMValueRef arg0 = ac_to_integer(&ctx->ac, emit_data->args[0]);
121
122 LLVMValueRef v = LLVMBuildICmp(ctx->ac.builder, LLVMIntNE, arg0,
123 ctx->i32_0, "");
124
125 emit_data->output[emit_data->chan] =
126 LLVMBuildSelect(ctx->ac.builder, v, emit_data->args[1], emit_data->args[2], "");
127 }
128
129 static void emit_cmp(const struct lp_build_tgsi_action *action,
130 struct lp_build_tgsi_context *bld_base,
131 struct lp_build_emit_data *emit_data)
132 {
133 struct si_shader_context *ctx = si_shader_context(bld_base);
134 LLVMValueRef cond, *args = emit_data->args;
135
136 cond = LLVMBuildFCmp(ctx->ac.builder, LLVMRealOLT, args[0],
137 ctx->ac.f32_0, "");
138
139 emit_data->output[emit_data->chan] =
140 LLVMBuildSelect(ctx->ac.builder, cond, args[1], args[2], "");
141 }
142
143 static void emit_set_cond(const struct lp_build_tgsi_action *action,
144 struct lp_build_tgsi_context *bld_base,
145 struct lp_build_emit_data *emit_data)
146 {
147 struct si_shader_context *ctx = si_shader_context(bld_base);
148 LLVMRealPredicate pred;
149 LLVMValueRef cond;
150
151 /* Use ordered for everything but NE (which is usual for
152 * float comparisons)
153 */
154 switch (emit_data->inst->Instruction.Opcode) {
155 case TGSI_OPCODE_SGE: pred = LLVMRealOGE; break;
156 case TGSI_OPCODE_SEQ: pred = LLVMRealOEQ; break;
157 case TGSI_OPCODE_SLE: pred = LLVMRealOLE; break;
158 case TGSI_OPCODE_SLT: pred = LLVMRealOLT; break;
159 case TGSI_OPCODE_SNE: pred = LLVMRealUNE; break;
160 case TGSI_OPCODE_SGT: pred = LLVMRealOGT; break;
161 default: assert(!"unknown instruction"); pred = 0; break;
162 }
163
164 cond = LLVMBuildFCmp(ctx->ac.builder,
165 pred, emit_data->args[0], emit_data->args[1], "");
166
167 emit_data->output[emit_data->chan] = LLVMBuildSelect(ctx->ac.builder,
168 cond, ctx->ac.f32_1, ctx->ac.f32_0, "");
169 }
170
171 static void emit_fcmp(const struct lp_build_tgsi_action *action,
172 struct lp_build_tgsi_context *bld_base,
173 struct lp_build_emit_data *emit_data)
174 {
175 struct si_shader_context *ctx = si_shader_context(bld_base);
176 LLVMRealPredicate pred;
177
178 /* Use ordered for everything but NE (which is usual for
179 * float comparisons)
180 */
181 switch (emit_data->inst->Instruction.Opcode) {
182 case TGSI_OPCODE_FSEQ: pred = LLVMRealOEQ; break;
183 case TGSI_OPCODE_FSGE: pred = LLVMRealOGE; break;
184 case TGSI_OPCODE_FSLT: pred = LLVMRealOLT; break;
185 case TGSI_OPCODE_FSNE: pred = LLVMRealUNE; break;
186 default: assert(!"unknown instruction"); pred = 0; break;
187 }
188
189 LLVMValueRef v = LLVMBuildFCmp(ctx->ac.builder, pred,
190 emit_data->args[0], emit_data->args[1],"");
191
192 v = LLVMBuildSExtOrBitCast(ctx->ac.builder, v, ctx->i32, "");
193
194 emit_data->output[emit_data->chan] = v;
195 }
196
197 static void emit_dcmp(const struct lp_build_tgsi_action *action,
198 struct lp_build_tgsi_context *bld_base,
199 struct lp_build_emit_data *emit_data)
200 {
201 struct si_shader_context *ctx = si_shader_context(bld_base);
202 LLVMRealPredicate pred;
203
204 /* Use ordered for everything but NE (which is usual for
205 * float comparisons)
206 */
207 switch (emit_data->inst->Instruction.Opcode) {
208 case TGSI_OPCODE_DSEQ: pred = LLVMRealOEQ; break;
209 case TGSI_OPCODE_DSGE: pred = LLVMRealOGE; break;
210 case TGSI_OPCODE_DSLT: pred = LLVMRealOLT; break;
211 case TGSI_OPCODE_DSNE: pred = LLVMRealUNE; break;
212 default: assert(!"unknown instruction"); pred = 0; break;
213 }
214
215 LLVMValueRef v = LLVMBuildFCmp(ctx->ac.builder, pred,
216 emit_data->args[0], emit_data->args[1],"");
217
218 v = LLVMBuildSExtOrBitCast(ctx->ac.builder, v, ctx->i32, "");
219
220 emit_data->output[emit_data->chan] = v;
221 }
222
223 static void emit_not(const struct lp_build_tgsi_action *action,
224 struct lp_build_tgsi_context *bld_base,
225 struct lp_build_emit_data *emit_data)
226 {
227 struct si_shader_context *ctx = si_shader_context(bld_base);
228 LLVMValueRef v = ac_to_integer(&ctx->ac, emit_data->args[0]);
229 emit_data->output[emit_data->chan] = LLVMBuildNot(ctx->ac.builder, v, "");
230 }
231
232 static void emit_arl(const struct lp_build_tgsi_action *action,
233 struct lp_build_tgsi_context *bld_base,
234 struct lp_build_emit_data *emit_data)
235 {
236 struct si_shader_context *ctx = si_shader_context(bld_base);
237 LLVMValueRef floor_index =
238 ac_build_intrinsic(&ctx->ac, "llvm.floor.f32", ctx->f32,
239 &emit_data->args[0], 1, AC_FUNC_ATTR_READNONE);
240 emit_data->output[emit_data->chan] = LLVMBuildFPToSI(ctx->ac.builder,
241 floor_index, ctx->i32, "");
242 }
243
244 static void emit_and(const struct lp_build_tgsi_action *action,
245 struct lp_build_tgsi_context *bld_base,
246 struct lp_build_emit_data *emit_data)
247 {
248 struct si_shader_context *ctx = si_shader_context(bld_base);
249 emit_data->output[emit_data->chan] = LLVMBuildAnd(ctx->ac.builder,
250 emit_data->args[0], emit_data->args[1], "");
251 }
252
253 static void emit_or(const struct lp_build_tgsi_action *action,
254 struct lp_build_tgsi_context *bld_base,
255 struct lp_build_emit_data *emit_data)
256 {
257 struct si_shader_context *ctx = si_shader_context(bld_base);
258 emit_data->output[emit_data->chan] = LLVMBuildOr(ctx->ac.builder,
259 emit_data->args[0], emit_data->args[1], "");
260 }
261
262 static void emit_uadd(const struct lp_build_tgsi_action *action,
263 struct lp_build_tgsi_context *bld_base,
264 struct lp_build_emit_data *emit_data)
265 {
266 struct si_shader_context *ctx = si_shader_context(bld_base);
267 emit_data->output[emit_data->chan] = LLVMBuildAdd(ctx->ac.builder,
268 emit_data->args[0], emit_data->args[1], "");
269 }
270
271 static void emit_udiv(const struct lp_build_tgsi_action *action,
272 struct lp_build_tgsi_context *bld_base,
273 struct lp_build_emit_data *emit_data)
274 {
275 struct si_shader_context *ctx = si_shader_context(bld_base);
276 emit_data->output[emit_data->chan] = LLVMBuildUDiv(ctx->ac.builder,
277 emit_data->args[0], emit_data->args[1], "");
278 }
279
280 static void emit_idiv(const struct lp_build_tgsi_action *action,
281 struct lp_build_tgsi_context *bld_base,
282 struct lp_build_emit_data *emit_data)
283 {
284 struct si_shader_context *ctx = si_shader_context(bld_base);
285 emit_data->output[emit_data->chan] = LLVMBuildSDiv(ctx->ac.builder,
286 emit_data->args[0], emit_data->args[1], "");
287 }
288
289 static void emit_mod(const struct lp_build_tgsi_action *action,
290 struct lp_build_tgsi_context *bld_base,
291 struct lp_build_emit_data *emit_data)
292 {
293 struct si_shader_context *ctx = si_shader_context(bld_base);
294 emit_data->output[emit_data->chan] = LLVMBuildSRem(ctx->ac.builder,
295 emit_data->args[0], emit_data->args[1], "");
296 }
297
298 static void emit_umod(const struct lp_build_tgsi_action *action,
299 struct lp_build_tgsi_context *bld_base,
300 struct lp_build_emit_data *emit_data)
301 {
302 struct si_shader_context *ctx = si_shader_context(bld_base);
303 emit_data->output[emit_data->chan] = LLVMBuildURem(ctx->ac.builder,
304 emit_data->args[0], emit_data->args[1], "");
305 }
306
307 static void emit_shl(const struct lp_build_tgsi_action *action,
308 struct lp_build_tgsi_context *bld_base,
309 struct lp_build_emit_data *emit_data)
310 {
311 struct si_shader_context *ctx = si_shader_context(bld_base);
312 emit_data->output[emit_data->chan] = LLVMBuildShl(ctx->ac.builder,
313 emit_data->args[0], emit_data->args[1], "");
314 }
315
316 static void emit_ushr(const struct lp_build_tgsi_action *action,
317 struct lp_build_tgsi_context *bld_base,
318 struct lp_build_emit_data *emit_data)
319 {
320 struct si_shader_context *ctx = si_shader_context(bld_base);
321 emit_data->output[emit_data->chan] = LLVMBuildLShr(ctx->ac.builder,
322 emit_data->args[0], emit_data->args[1], "");
323 }
324 static void emit_ishr(const struct lp_build_tgsi_action *action,
325 struct lp_build_tgsi_context *bld_base,
326 struct lp_build_emit_data *emit_data)
327 {
328 struct si_shader_context *ctx = si_shader_context(bld_base);
329 emit_data->output[emit_data->chan] = LLVMBuildAShr(ctx->ac.builder,
330 emit_data->args[0], emit_data->args[1], "");
331 }
332
333 static void emit_xor(const struct lp_build_tgsi_action *action,
334 struct lp_build_tgsi_context *bld_base,
335 struct lp_build_emit_data *emit_data)
336 {
337 struct si_shader_context *ctx = si_shader_context(bld_base);
338 emit_data->output[emit_data->chan] = LLVMBuildXor(ctx->ac.builder,
339 emit_data->args[0], emit_data->args[1], "");
340 }
341
342 static void emit_ssg(const struct lp_build_tgsi_action *action,
343 struct lp_build_tgsi_context *bld_base,
344 struct lp_build_emit_data *emit_data)
345 {
346 struct si_shader_context *ctx = si_shader_context(bld_base);
347
348 LLVMValueRef val;
349
350 if (emit_data->inst->Instruction.Opcode == TGSI_OPCODE_I64SSG) {
351 val = ac_build_isign(&ctx->ac, emit_data->args[0], 64);
352 } else if (emit_data->inst->Instruction.Opcode == TGSI_OPCODE_ISSG) {
353 val = ac_build_isign(&ctx->ac, emit_data->args[0], 32);
354 } else if (emit_data->inst->Instruction.Opcode == TGSI_OPCODE_DSSG) {
355 val = ac_build_fsign(&ctx->ac, emit_data->args[0], 64);
356 } else {
357 val = ac_build_fsign(&ctx->ac, emit_data->args[0], 32);
358 }
359
360 emit_data->output[emit_data->chan] = val;
361 }
362
363 static void emit_ineg(const struct lp_build_tgsi_action *action,
364 struct lp_build_tgsi_context *bld_base,
365 struct lp_build_emit_data *emit_data)
366 {
367 struct si_shader_context *ctx = si_shader_context(bld_base);
368 emit_data->output[emit_data->chan] = LLVMBuildNeg(ctx->ac.builder,
369 emit_data->args[0], "");
370 }
371
372 static void emit_dneg(const struct lp_build_tgsi_action *action,
373 struct lp_build_tgsi_context *bld_base,
374 struct lp_build_emit_data *emit_data)
375 {
376 struct si_shader_context *ctx = si_shader_context(bld_base);
377 emit_data->output[emit_data->chan] = LLVMBuildFNeg(ctx->ac.builder,
378 emit_data->args[0], "");
379 }
380
381 static void emit_frac(const struct lp_build_tgsi_action *action,
382 struct lp_build_tgsi_context *bld_base,
383 struct lp_build_emit_data *emit_data)
384 {
385 struct si_shader_context *ctx = si_shader_context(bld_base);
386 unsigned bitsize;
387
388 if (emit_data->info->opcode == TGSI_OPCODE_FRC)
389 bitsize = 32;
390 else if (emit_data->info->opcode == TGSI_OPCODE_DFRAC)
391 bitsize = 64;
392 else {
393 assert(0);
394 return;
395 }
396
397 emit_data->output[emit_data->chan] =
398 ac_build_fract(&ctx->ac, emit_data->args[0], bitsize);
399 }
400
401 static void emit_f2i(const struct lp_build_tgsi_action *action,
402 struct lp_build_tgsi_context *bld_base,
403 struct lp_build_emit_data *emit_data)
404 {
405 struct si_shader_context *ctx = si_shader_context(bld_base);
406 emit_data->output[emit_data->chan] = LLVMBuildFPToSI(ctx->ac.builder,
407 emit_data->args[0], ctx->i32, "");
408 }
409
410 static void emit_f2u(const struct lp_build_tgsi_action *action,
411 struct lp_build_tgsi_context *bld_base,
412 struct lp_build_emit_data *emit_data)
413 {
414 struct si_shader_context *ctx = si_shader_context(bld_base);
415 emit_data->output[emit_data->chan] = LLVMBuildFPToUI(ctx->ac.builder,
416 emit_data->args[0], ctx->i32, "");
417 }
418
419 static void emit_i2f(const struct lp_build_tgsi_action *action,
420 struct lp_build_tgsi_context *bld_base,
421 struct lp_build_emit_data *emit_data)
422 {
423 struct si_shader_context *ctx = si_shader_context(bld_base);
424 emit_data->output[emit_data->chan] = LLVMBuildSIToFP(ctx->ac.builder,
425 emit_data->args[0], ctx->f32, "");
426 }
427
428 static void emit_u2f(const struct lp_build_tgsi_action *action,
429 struct lp_build_tgsi_context *bld_base,
430 struct lp_build_emit_data *emit_data)
431 {
432 struct si_shader_context *ctx = si_shader_context(bld_base);
433 emit_data->output[emit_data->chan] = LLVMBuildUIToFP(ctx->ac.builder,
434 emit_data->args[0], ctx->f32, "");
435 }
436
437 static void
438 build_tgsi_intrinsic_nomem(const struct lp_build_tgsi_action *action,
439 struct lp_build_tgsi_context *bld_base,
440 struct lp_build_emit_data *emit_data)
441 {
442 struct si_shader_context *ctx = si_shader_context(bld_base);
443 emit_data->output[emit_data->chan] =
444 ac_build_intrinsic(&ctx->ac, action->intr_name,
445 emit_data->dst_type, emit_data->args,
446 emit_data->arg_count, AC_FUNC_ATTR_READNONE);
447 }
448
449 static void emit_bfi(const struct lp_build_tgsi_action *action,
450 struct lp_build_tgsi_context *bld_base,
451 struct lp_build_emit_data *emit_data)
452 {
453 struct si_shader_context *ctx = si_shader_context(bld_base);
454 LLVMBuilderRef builder = ctx->ac.builder;
455 LLVMValueRef bfi_args[3];
456 LLVMValueRef bfi_sm5;
457 LLVMValueRef cond;
458
459 // Calculate the bitmask: (((1 << src3) - 1) << src2
460 bfi_args[0] = LLVMBuildShl(builder,
461 LLVMBuildSub(builder,
462 LLVMBuildShl(builder,
463 ctx->i32_1,
464 emit_data->args[3], ""),
465 ctx->i32_1, ""),
466 emit_data->args[2], "");
467
468 bfi_args[1] = LLVMBuildShl(builder, emit_data->args[1],
469 emit_data->args[2], "");
470
471 bfi_args[2] = emit_data->args[0];
472
473 /* Calculate:
474 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
475 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
476 */
477 bfi_sm5 =
478 LLVMBuildXor(builder, bfi_args[2],
479 LLVMBuildAnd(builder, bfi_args[0],
480 LLVMBuildXor(builder, bfi_args[1], bfi_args[2],
481 ""), ""), "");
482
483 /* Since shifts of >= 32 bits are undefined in LLVM IR, the backend
484 * uses the convenient V_BFI lowering for the above, which follows SM5
485 * and disagrees with GLSL semantics when bits (src3) is 32.
486 */
487 cond = LLVMBuildICmp(builder, LLVMIntUGE, emit_data->args[3],
488 LLVMConstInt(ctx->i32, 32, 0), "");
489 emit_data->output[emit_data->chan] =
490 LLVMBuildSelect(builder, cond, emit_data->args[1], bfi_sm5, "");
491 }
492
493 static void emit_bfe(const struct lp_build_tgsi_action *action,
494 struct lp_build_tgsi_context *bld_base,
495 struct lp_build_emit_data *emit_data)
496 {
497 struct si_shader_context *ctx = si_shader_context(bld_base);
498
499 if (HAVE_LLVM < 0x0700) {
500 LLVMValueRef bfe_sm5 =
501 ac_build_bfe(&ctx->ac, emit_data->args[0],
502 emit_data->args[1], emit_data->args[2],
503 emit_data->info->opcode == TGSI_OPCODE_IBFE);
504
505 /* Correct for GLSL semantics. */
506 LLVMValueRef cond = LLVMBuildICmp(ctx->ac.builder, LLVMIntUGE, emit_data->args[2],
507 LLVMConstInt(ctx->i32, 32, 0), "");
508 emit_data->output[emit_data->chan] =
509 LLVMBuildSelect(ctx->ac.builder, cond, emit_data->args[0], bfe_sm5, "");
510 } else {
511 /* FIXME: LLVM 7 returns incorrect result when count is 0.
512 * https://bugs.freedesktop.org/show_bug.cgi?id=107276
513 */
514 LLVMValueRef zero = ctx->i32_0;
515 LLVMValueRef bfe_sm5 =
516 ac_build_bfe(&ctx->ac, emit_data->args[0],
517 emit_data->args[1], emit_data->args[2],
518 emit_data->info->opcode == TGSI_OPCODE_IBFE);
519
520 /* Correct for GLSL semantics. */
521 LLVMValueRef cond = LLVMBuildICmp(ctx->ac.builder, LLVMIntUGE, emit_data->args[2],
522 LLVMConstInt(ctx->i32, 32, 0), "");
523 LLVMValueRef cond2 = LLVMBuildICmp(ctx->ac.builder, LLVMIntEQ, emit_data->args[2],
524 zero, "");
525 bfe_sm5 = LLVMBuildSelect(ctx->ac.builder, cond, emit_data->args[0], bfe_sm5, "");
526 emit_data->output[emit_data->chan] =
527 LLVMBuildSelect(ctx->ac.builder, cond2, zero, bfe_sm5, "");
528 }
529 }
530
531 /* this is ffs in C */
532 static void emit_lsb(const struct lp_build_tgsi_action *action,
533 struct lp_build_tgsi_context *bld_base,
534 struct lp_build_emit_data *emit_data)
535 {
536 struct si_shader_context *ctx = si_shader_context(bld_base);
537
538 emit_data->output[emit_data->chan] = ac_find_lsb(&ctx->ac, emit_data->dst_type, emit_data->args[0]);
539 }
540
541 /* Find the last bit set. */
542 static void emit_umsb(const struct lp_build_tgsi_action *action,
543 struct lp_build_tgsi_context *bld_base,
544 struct lp_build_emit_data *emit_data)
545 {
546 struct si_shader_context *ctx = si_shader_context(bld_base);
547
548 emit_data->output[emit_data->chan] =
549 ac_build_umsb(&ctx->ac, emit_data->args[0], emit_data->dst_type);
550 }
551
552 /* Find the last bit opposite of the sign bit. */
553 static void emit_imsb(const struct lp_build_tgsi_action *action,
554 struct lp_build_tgsi_context *bld_base,
555 struct lp_build_emit_data *emit_data)
556 {
557 struct si_shader_context *ctx = si_shader_context(bld_base);
558 emit_data->output[emit_data->chan] =
559 ac_build_imsb(&ctx->ac, emit_data->args[0],
560 emit_data->dst_type);
561 }
562
563 static void emit_iabs(const struct lp_build_tgsi_action *action,
564 struct lp_build_tgsi_context *bld_base,
565 struct lp_build_emit_data *emit_data)
566 {
567 struct si_shader_context *ctx = si_shader_context(bld_base);
568
569 emit_data->output[emit_data->chan] =
570 ac_build_imax(&ctx->ac, emit_data->args[0],
571 LLVMBuildNeg(ctx->ac.builder, emit_data->args[0], ""));
572 }
573
574 static void emit_minmax_int(const struct lp_build_tgsi_action *action,
575 struct lp_build_tgsi_context *bld_base,
576 struct lp_build_emit_data *emit_data)
577 {
578 struct si_shader_context *ctx = si_shader_context(bld_base);
579 LLVMIntPredicate op;
580
581 switch (emit_data->info->opcode) {
582 default:
583 assert(0);
584 case TGSI_OPCODE_IMAX:
585 case TGSI_OPCODE_I64MAX:
586 op = LLVMIntSGT;
587 break;
588 case TGSI_OPCODE_IMIN:
589 case TGSI_OPCODE_I64MIN:
590 op = LLVMIntSLT;
591 break;
592 case TGSI_OPCODE_UMAX:
593 case TGSI_OPCODE_U64MAX:
594 op = LLVMIntUGT;
595 break;
596 case TGSI_OPCODE_UMIN:
597 case TGSI_OPCODE_U64MIN:
598 op = LLVMIntULT;
599 break;
600 }
601
602 emit_data->output[emit_data->chan] =
603 LLVMBuildSelect(ctx->ac.builder,
604 LLVMBuildICmp(ctx->ac.builder, op, emit_data->args[0],
605 emit_data->args[1], ""),
606 emit_data->args[0],
607 emit_data->args[1], "");
608 }
609
610 static void emit_pk2h(const struct lp_build_tgsi_action *action,
611 struct lp_build_tgsi_context *bld_base,
612 struct lp_build_emit_data *emit_data)
613 {
614 struct si_shader_context *ctx = si_shader_context(bld_base);
615 LLVMValueRef v[] = {
616 lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_X),
617 lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_Y),
618 };
619
620
621 /* From the GLSL 4.50 spec:
622 * "The rounding mode cannot be set and is undefined."
623 *
624 * v_cvt_pkrtz_f16 rounds to zero, but it's fastest.
625 */
626 emit_data->output[emit_data->chan] =
627 LLVMBuildBitCast(ctx->ac.builder, ac_build_cvt_pkrtz_f16(&ctx->ac, v),
628 ctx->i32, "");
629 }
630
631 static void emit_up2h(const struct lp_build_tgsi_action *action,
632 struct lp_build_tgsi_context *bld_base,
633 struct lp_build_emit_data *emit_data)
634 {
635 struct si_shader_context *ctx = si_shader_context(bld_base);
636 LLVMTypeRef i16;
637 LLVMValueRef const16, input, val;
638 unsigned i;
639
640 i16 = LLVMInt16TypeInContext(ctx->ac.context);
641 const16 = LLVMConstInt(ctx->i32, 16, 0);
642 input = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_X);
643
644 for (i = 0; i < 2; i++) {
645 val = i == 1 ? LLVMBuildLShr(ctx->ac.builder, input, const16, "") : input;
646 val = LLVMBuildTrunc(ctx->ac.builder, val, i16, "");
647 val = ac_to_float(&ctx->ac, val);
648 emit_data->output[i] = LLVMBuildFPExt(ctx->ac.builder, val, ctx->f32, "");
649 }
650 }
651
652 static void emit_fdiv(const struct lp_build_tgsi_action *action,
653 struct lp_build_tgsi_context *bld_base,
654 struct lp_build_emit_data *emit_data)
655 {
656 struct si_shader_context *ctx = si_shader_context(bld_base);
657
658 emit_data->output[emit_data->chan] =
659 ac_build_fdiv(&ctx->ac, emit_data->args[0], emit_data->args[1]);
660 }
661
662 /* 1/sqrt is translated to rsq for f32 if fp32 denormals are not enabled in
663 * the target machine. f64 needs global unsafe math flags to get rsq. */
664 static void emit_rsq(const struct lp_build_tgsi_action *action,
665 struct lp_build_tgsi_context *bld_base,
666 struct lp_build_emit_data *emit_data)
667 {
668 struct si_shader_context *ctx = si_shader_context(bld_base);
669
670 LLVMValueRef sqrt =
671 ac_build_intrinsic(&ctx->ac, "llvm.sqrt.f32", ctx->f32,
672 &emit_data->args[0], 1, AC_FUNC_ATTR_READNONE);
673
674 emit_data->output[emit_data->chan] =
675 ac_build_fdiv(&ctx->ac, ctx->ac.f32_1, sqrt);
676 }
677
678 static void dfracexp_emit(const struct lp_build_tgsi_action *action,
679 struct lp_build_tgsi_context *bld_base,
680 struct lp_build_emit_data *emit_data)
681 {
682 struct si_shader_context *ctx = si_shader_context(bld_base);
683 LLVMValueRef in = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_X);
684
685 emit_data->output[emit_data->chan] =
686 ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.frexp.mant.f64",
687 ctx->ac.f64, &in, 1, 0);
688 emit_data->output1[emit_data->chan] =
689 ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.frexp.exp.i32.f64",
690 ctx->ac.i32, &in, 1, 0);
691 }
692
693 void si_shader_context_init_alu(struct lp_build_tgsi_context *bld_base)
694 {
695 lp_set_default_actions(bld_base);
696
697 bld_base->op_actions[TGSI_OPCODE_AND].emit = emit_and;
698 bld_base->op_actions[TGSI_OPCODE_ARL].emit = emit_arl;
699 bld_base->op_actions[TGSI_OPCODE_BFI].emit = emit_bfi;
700 bld_base->op_actions[TGSI_OPCODE_BREV].emit = build_tgsi_intrinsic_nomem;
701 bld_base->op_actions[TGSI_OPCODE_BREV].intr_name = "llvm.bitreverse.i32";
702 bld_base->op_actions[TGSI_OPCODE_CEIL].emit = build_tgsi_intrinsic_nomem;
703 bld_base->op_actions[TGSI_OPCODE_CEIL].intr_name = "llvm.ceil.f32";
704 bld_base->op_actions[TGSI_OPCODE_CMP].emit = emit_cmp;
705 bld_base->op_actions[TGSI_OPCODE_COS].emit = build_tgsi_intrinsic_nomem;
706 bld_base->op_actions[TGSI_OPCODE_COS].intr_name = "llvm.cos.f32";
707 bld_base->op_actions[TGSI_OPCODE_DABS].emit = build_tgsi_intrinsic_nomem;
708 bld_base->op_actions[TGSI_OPCODE_DABS].intr_name = "llvm.fabs.f64";
709 bld_base->op_actions[TGSI_OPCODE_DCEIL].emit = build_tgsi_intrinsic_nomem;
710 bld_base->op_actions[TGSI_OPCODE_DCEIL].intr_name = "llvm.ceil.f64";
711 bld_base->op_actions[TGSI_OPCODE_DFLR].emit = build_tgsi_intrinsic_nomem;
712 bld_base->op_actions[TGSI_OPCODE_DFLR].intr_name = "llvm.floor.f64";
713 bld_base->op_actions[TGSI_OPCODE_DFMA].emit = build_tgsi_intrinsic_nomem;
714 bld_base->op_actions[TGSI_OPCODE_DFMA].intr_name = "llvm.fma.f64";
715 bld_base->op_actions[TGSI_OPCODE_DFRAC].emit = emit_frac;
716 bld_base->op_actions[TGSI_OPCODE_DIV].emit = emit_fdiv;
717 bld_base->op_actions[TGSI_OPCODE_DNEG].emit = emit_dneg;
718 bld_base->op_actions[TGSI_OPCODE_DROUND].emit = build_tgsi_intrinsic_nomem;
719 bld_base->op_actions[TGSI_OPCODE_DROUND].intr_name = "llvm.rint.f64";
720 bld_base->op_actions[TGSI_OPCODE_DSEQ].emit = emit_dcmp;
721 bld_base->op_actions[TGSI_OPCODE_DSGE].emit = emit_dcmp;
722 bld_base->op_actions[TGSI_OPCODE_DSLT].emit = emit_dcmp;
723 bld_base->op_actions[TGSI_OPCODE_DSNE].emit = emit_dcmp;
724 bld_base->op_actions[TGSI_OPCODE_DSSG].emit = emit_ssg;
725 bld_base->op_actions[TGSI_OPCODE_DRSQ].emit = build_tgsi_intrinsic_nomem;
726 bld_base->op_actions[TGSI_OPCODE_DRSQ].intr_name = "llvm.amdgcn.rsq.f64";
727 bld_base->op_actions[TGSI_OPCODE_DSQRT].emit = build_tgsi_intrinsic_nomem;
728 bld_base->op_actions[TGSI_OPCODE_DSQRT].intr_name = "llvm.sqrt.f64";
729 bld_base->op_actions[TGSI_OPCODE_DTRUNC].emit = build_tgsi_intrinsic_nomem;
730 bld_base->op_actions[TGSI_OPCODE_DTRUNC].intr_name = "llvm.trunc.f64";
731 bld_base->op_actions[TGSI_OPCODE_DFRACEXP].emit = dfracexp_emit;
732 bld_base->op_actions[TGSI_OPCODE_DLDEXP].emit = build_tgsi_intrinsic_nomem;
733 bld_base->op_actions[TGSI_OPCODE_DLDEXP].intr_name = "llvm.amdgcn.ldexp.f64";
734 bld_base->op_actions[TGSI_OPCODE_EX2].emit = build_tgsi_intrinsic_nomem;
735 bld_base->op_actions[TGSI_OPCODE_EX2].intr_name = "llvm.exp2.f32";
736 bld_base->op_actions[TGSI_OPCODE_FLR].emit = build_tgsi_intrinsic_nomem;
737 bld_base->op_actions[TGSI_OPCODE_FLR].intr_name = "llvm.floor.f32";
738 bld_base->op_actions[TGSI_OPCODE_FMA].emit =
739 bld_base->op_actions[TGSI_OPCODE_MAD].emit;
740 bld_base->op_actions[TGSI_OPCODE_FRC].emit = emit_frac;
741 bld_base->op_actions[TGSI_OPCODE_F2I].emit = emit_f2i;
742 bld_base->op_actions[TGSI_OPCODE_F2U].emit = emit_f2u;
743 bld_base->op_actions[TGSI_OPCODE_FSEQ].emit = emit_fcmp;
744 bld_base->op_actions[TGSI_OPCODE_FSGE].emit = emit_fcmp;
745 bld_base->op_actions[TGSI_OPCODE_FSLT].emit = emit_fcmp;
746 bld_base->op_actions[TGSI_OPCODE_FSNE].emit = emit_fcmp;
747 bld_base->op_actions[TGSI_OPCODE_IABS].emit = emit_iabs;
748 bld_base->op_actions[TGSI_OPCODE_IBFE].emit = emit_bfe;
749 bld_base->op_actions[TGSI_OPCODE_IDIV].emit = emit_idiv;
750 bld_base->op_actions[TGSI_OPCODE_IMAX].emit = emit_minmax_int;
751 bld_base->op_actions[TGSI_OPCODE_IMIN].emit = emit_minmax_int;
752 bld_base->op_actions[TGSI_OPCODE_IMSB].emit = emit_imsb;
753 bld_base->op_actions[TGSI_OPCODE_INEG].emit = emit_ineg;
754 bld_base->op_actions[TGSI_OPCODE_ISHR].emit = emit_ishr;
755 bld_base->op_actions[TGSI_OPCODE_ISGE].emit = emit_icmp;
756 bld_base->op_actions[TGSI_OPCODE_ISLT].emit = emit_icmp;
757 bld_base->op_actions[TGSI_OPCODE_ISSG].emit = emit_ssg;
758 bld_base->op_actions[TGSI_OPCODE_I2F].emit = emit_i2f;
759 bld_base->op_actions[TGSI_OPCODE_KILL_IF].emit = kil_emit;
760 bld_base->op_actions[TGSI_OPCODE_KILL].emit = kil_emit;
761 bld_base->op_actions[TGSI_OPCODE_LDEXP].emit = build_tgsi_intrinsic_nomem;
762 bld_base->op_actions[TGSI_OPCODE_LDEXP].intr_name = "llvm.amdgcn.ldexp.f32";
763 bld_base->op_actions[TGSI_OPCODE_LSB].emit = emit_lsb;
764 bld_base->op_actions[TGSI_OPCODE_LG2].emit = build_tgsi_intrinsic_nomem;
765 bld_base->op_actions[TGSI_OPCODE_LG2].intr_name = "llvm.log2.f32";
766 bld_base->op_actions[TGSI_OPCODE_MAX].emit = build_tgsi_intrinsic_nomem;
767 bld_base->op_actions[TGSI_OPCODE_MAX].intr_name = "llvm.maxnum.f32";
768 bld_base->op_actions[TGSI_OPCODE_MIN].emit = build_tgsi_intrinsic_nomem;
769 bld_base->op_actions[TGSI_OPCODE_MIN].intr_name = "llvm.minnum.f32";
770 bld_base->op_actions[TGSI_OPCODE_MOD].emit = emit_mod;
771 bld_base->op_actions[TGSI_OPCODE_UMSB].emit = emit_umsb;
772 bld_base->op_actions[TGSI_OPCODE_NOT].emit = emit_not;
773 bld_base->op_actions[TGSI_OPCODE_OR].emit = emit_or;
774 bld_base->op_actions[TGSI_OPCODE_PK2H].emit = emit_pk2h;
775 bld_base->op_actions[TGSI_OPCODE_POPC].emit = build_tgsi_intrinsic_nomem;
776 bld_base->op_actions[TGSI_OPCODE_POPC].intr_name = "llvm.ctpop.i32";
777 bld_base->op_actions[TGSI_OPCODE_POW].emit = build_tgsi_intrinsic_nomem;
778 bld_base->op_actions[TGSI_OPCODE_POW].intr_name = "llvm.pow.f32";
779 bld_base->op_actions[TGSI_OPCODE_ROUND].emit = build_tgsi_intrinsic_nomem;
780 bld_base->op_actions[TGSI_OPCODE_ROUND].intr_name = "llvm.rint.f32";
781 bld_base->op_actions[TGSI_OPCODE_RSQ].emit = emit_rsq;
782 bld_base->op_actions[TGSI_OPCODE_SGE].emit = emit_set_cond;
783 bld_base->op_actions[TGSI_OPCODE_SEQ].emit = emit_set_cond;
784 bld_base->op_actions[TGSI_OPCODE_SHL].emit = emit_shl;
785 bld_base->op_actions[TGSI_OPCODE_SLE].emit = emit_set_cond;
786 bld_base->op_actions[TGSI_OPCODE_SLT].emit = emit_set_cond;
787 bld_base->op_actions[TGSI_OPCODE_SNE].emit = emit_set_cond;
788 bld_base->op_actions[TGSI_OPCODE_SGT].emit = emit_set_cond;
789 bld_base->op_actions[TGSI_OPCODE_SIN].emit = build_tgsi_intrinsic_nomem;
790 bld_base->op_actions[TGSI_OPCODE_SIN].intr_name = "llvm.sin.f32";
791 bld_base->op_actions[TGSI_OPCODE_SQRT].emit = build_tgsi_intrinsic_nomem;
792 bld_base->op_actions[TGSI_OPCODE_SQRT].intr_name = "llvm.sqrt.f32";
793 bld_base->op_actions[TGSI_OPCODE_SSG].emit = emit_ssg;
794 bld_base->op_actions[TGSI_OPCODE_TRUNC].emit = build_tgsi_intrinsic_nomem;
795 bld_base->op_actions[TGSI_OPCODE_TRUNC].intr_name = "llvm.trunc.f32";
796 bld_base->op_actions[TGSI_OPCODE_UADD].emit = emit_uadd;
797 bld_base->op_actions[TGSI_OPCODE_UBFE].emit = emit_bfe;
798 bld_base->op_actions[TGSI_OPCODE_UDIV].emit = emit_udiv;
799 bld_base->op_actions[TGSI_OPCODE_UMAX].emit = emit_minmax_int;
800 bld_base->op_actions[TGSI_OPCODE_UMIN].emit = emit_minmax_int;
801 bld_base->op_actions[TGSI_OPCODE_UMOD].emit = emit_umod;
802 bld_base->op_actions[TGSI_OPCODE_USEQ].emit = emit_icmp;
803 bld_base->op_actions[TGSI_OPCODE_USGE].emit = emit_icmp;
804 bld_base->op_actions[TGSI_OPCODE_USHR].emit = emit_ushr;
805 bld_base->op_actions[TGSI_OPCODE_USLT].emit = emit_icmp;
806 bld_base->op_actions[TGSI_OPCODE_USNE].emit = emit_icmp;
807 bld_base->op_actions[TGSI_OPCODE_U2F].emit = emit_u2f;
808 bld_base->op_actions[TGSI_OPCODE_XOR].emit = emit_xor;
809 bld_base->op_actions[TGSI_OPCODE_UCMP].emit = emit_ucmp;
810 bld_base->op_actions[TGSI_OPCODE_UP2H].emit = emit_up2h;
811
812 bld_base->op_actions[TGSI_OPCODE_I64MAX].emit = emit_minmax_int;
813 bld_base->op_actions[TGSI_OPCODE_I64MIN].emit = emit_minmax_int;
814 bld_base->op_actions[TGSI_OPCODE_U64MAX].emit = emit_minmax_int;
815 bld_base->op_actions[TGSI_OPCODE_U64MIN].emit = emit_minmax_int;
816 bld_base->op_actions[TGSI_OPCODE_I64ABS].emit = emit_iabs;
817 bld_base->op_actions[TGSI_OPCODE_I64SSG].emit = emit_ssg;
818 bld_base->op_actions[TGSI_OPCODE_I64NEG].emit = emit_ineg;
819
820 bld_base->op_actions[TGSI_OPCODE_U64SEQ].emit = emit_icmp;
821 bld_base->op_actions[TGSI_OPCODE_U64SNE].emit = emit_icmp;
822 bld_base->op_actions[TGSI_OPCODE_U64SGE].emit = emit_icmp;
823 bld_base->op_actions[TGSI_OPCODE_U64SLT].emit = emit_icmp;
824 bld_base->op_actions[TGSI_OPCODE_I64SGE].emit = emit_icmp;
825 bld_base->op_actions[TGSI_OPCODE_I64SLT].emit = emit_icmp;
826
827 bld_base->op_actions[TGSI_OPCODE_U64ADD].emit = emit_uadd;
828 bld_base->op_actions[TGSI_OPCODE_U64SHL].emit = emit_shl;
829 bld_base->op_actions[TGSI_OPCODE_U64SHR].emit = emit_ushr;
830 bld_base->op_actions[TGSI_OPCODE_I64SHR].emit = emit_ishr;
831
832 bld_base->op_actions[TGSI_OPCODE_U64MOD].emit = emit_umod;
833 bld_base->op_actions[TGSI_OPCODE_I64MOD].emit = emit_mod;
834 bld_base->op_actions[TGSI_OPCODE_U64DIV].emit = emit_udiv;
835 bld_base->op_actions[TGSI_OPCODE_I64DIV].emit = emit_idiv;
836 }