2 * Copyright 2011 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * Authors: Tom Stellard <thomas.stellard@amd.com>
26 #include "radeon_llvm.h"
28 #include "gallivm/lp_bld_const.h"
29 #include "gallivm/lp_bld_gather.h"
30 #include "gallivm/lp_bld_flow.h"
31 #include "gallivm/lp_bld_init.h"
32 #include "gallivm/lp_bld_intr.h"
33 #include "gallivm/lp_bld_swizzle.h"
34 #include "tgsi/tgsi_info.h"
35 #include "tgsi/tgsi_parse.h"
36 #include "util/u_math.h"
37 #include "util/u_memory.h"
38 #include "util/u_debug.h"
40 #include <llvm-c/Core.h>
41 #include <llvm-c/Transforms/Scalar.h>
43 static struct radeon_llvm_loop
* get_current_loop(struct radeon_llvm_context
* ctx
)
45 return ctx
->loop_depth
> 0 ? ctx
->loop
+ (ctx
->loop_depth
- 1) : NULL
;
48 static struct radeon_llvm_branch
* get_current_branch(
49 struct radeon_llvm_context
* ctx
)
51 return ctx
->branch_depth
> 0 ?
52 ctx
->branch
+ (ctx
->branch_depth
- 1) : NULL
;
55 unsigned radeon_llvm_reg_index_soa(unsigned index
, unsigned chan
)
57 return (index
* 4) + chan
;
60 static LLVMValueRef
emit_swizzle(
61 struct lp_build_tgsi_context
* bld_base
,
68 LLVMValueRef swizzles
[4];
70 LLVMInt32TypeInContext(bld_base
->base
.gallivm
->context
);
72 swizzles
[0] = LLVMConstInt(i32t
, swizzle_x
, 0);
73 swizzles
[1] = LLVMConstInt(i32t
, swizzle_y
, 0);
74 swizzles
[2] = LLVMConstInt(i32t
, swizzle_z
, 0);
75 swizzles
[3] = LLVMConstInt(i32t
, swizzle_w
, 0);
77 return LLVMBuildShuffleVector(bld_base
->base
.gallivm
->builder
,
79 LLVMGetUndef(LLVMTypeOf(value
)),
80 LLVMConstVector(swizzles
, 4), "");
85 struct lp_build_tgsi_soa_context
*bld
,
86 const struct tgsi_full_src_register
*reg
,
89 struct gallivm_state
* gallivm
= bld
->bld_base
.base
.gallivm
;
91 LLVMValueRef addr
= LLVMBuildLoad(gallivm
->builder
,
92 bld
->addr
[reg
->Indirect
.Index
][swizzle
], "");
93 LLVMValueRef offset
= lp_build_const_int32(gallivm
, reg
->Register
.Index
);
94 LLVMValueRef hw_index
= LLVMBuildAdd(gallivm
->builder
, addr
, offset
, "");
95 LLVMValueRef soa_index
= LLVMBuildMul(gallivm
->builder
, hw_index
,
96 lp_build_const_int32(gallivm
, 4), "");
97 LLVMValueRef array_index
= LLVMBuildAdd(gallivm
->builder
, soa_index
,
98 lp_build_const_int32(gallivm
, swizzle
), "");
104 emit_fetch_immediate(
105 struct lp_build_tgsi_context
*bld_base
,
106 const struct tgsi_full_src_register
*reg
,
107 enum tgsi_opcode_type type
,
111 LLVMContextRef ctx
= bld_base
->base
.gallivm
->context
;
114 case TGSI_TYPE_UNSIGNED
:
115 case TGSI_TYPE_SIGNED
:
116 ctype
= LLVMInt32TypeInContext(ctx
);
118 case TGSI_TYPE_UNTYPED
:
119 case TGSI_TYPE_FLOAT
:
120 ctype
= LLVMFloatTypeInContext(ctx
);
127 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
128 return LLVMConstBitCast(bld
->immediates
[reg
->Register
.Index
][swizzle
], ctype
);
133 struct lp_build_tgsi_context
*bld_base
,
134 const struct tgsi_full_src_register
*reg
,
135 enum tgsi_opcode_type type
,
138 struct radeon_llvm_context
* ctx
= radeon_llvm_context(bld_base
);
140 LLVMValueRef values
[TGSI_NUM_CHANNELS
] = {};
142 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
143 values
[chan
] = ctx
->inputs
[radeon_llvm_reg_index_soa(
144 reg
->Register
.Index
, chan
)];
146 return lp_build_gather_values(bld_base
->base
.gallivm
, values
,
149 return bitcast(bld_base
, type
, ctx
->inputs
[radeon_llvm_reg_index_soa(reg
->Register
.Index
, swizzle
)]);
154 emit_fetch_temporary(
155 struct lp_build_tgsi_context
*bld_base
,
156 const struct tgsi_full_src_register
*reg
,
157 enum tgsi_opcode_type type
,
160 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
161 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
162 if (reg
->Register
.Indirect
) {
163 LLVMValueRef array_index
= emit_array_index(bld
, reg
, swizzle
);
164 LLVMValueRef ptr
= LLVMBuildGEP(builder
, bld
->temps_array
, &array_index
,
166 return LLVMBuildLoad(builder
, ptr
, "");
168 LLVMValueRef temp_ptr
;
169 temp_ptr
= lp_get_temp_ptr_soa(bld
, reg
->Register
.Index
, swizzle
);
170 return bitcast(bld_base
,type
,LLVMBuildLoad(builder
, temp_ptr
, ""));
176 struct lp_build_tgsi_context
*bld_base
,
177 const struct tgsi_full_src_register
*reg
,
178 enum tgsi_opcode_type type
,
181 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
182 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
183 if (reg
->Register
.Indirect
) {
184 LLVMValueRef array_index
= emit_array_index(bld
, reg
, swizzle
);
185 LLVMValueRef ptr
= LLVMBuildGEP(builder
, bld
->outputs_array
, &array_index
,
187 return LLVMBuildLoad(builder
, ptr
, "");
189 LLVMValueRef temp_ptr
;
190 temp_ptr
= lp_get_output_ptr(bld
, reg
->Register
.Index
, swizzle
);
191 return LLVMBuildLoad(builder
, temp_ptr
, "");
195 static void emit_declaration(
196 struct lp_build_tgsi_context
* bld_base
,
197 const struct tgsi_full_declaration
*decl
)
199 struct radeon_llvm_context
* ctx
= radeon_llvm_context(bld_base
);
200 switch(decl
->Declaration
.File
) {
201 case TGSI_FILE_ADDRESS
:
204 for (idx
= decl
->Range
.First
; idx
<= decl
->Range
.Last
; idx
++) {
206 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
207 ctx
->soa
.addr
[idx
][chan
] = lp_build_alloca(
209 ctx
->soa
.bld_base
.uint_bld
.elem_type
, "");
215 case TGSI_FILE_TEMPORARY
:
216 lp_emit_declaration_soa(bld_base
, decl
);
219 case TGSI_FILE_INPUT
:
222 for (idx
= decl
->Range
.First
; idx
<= decl
->Range
.Last
; idx
++) {
223 ctx
->load_input(ctx
, idx
, decl
);
228 case TGSI_FILE_SYSTEM_VALUE
:
231 for (idx
= decl
->Range
.First
; idx
<= decl
->Range
.Last
; idx
++) {
232 ctx
->load_system_value(ctx
, idx
, decl
);
237 case TGSI_FILE_OUTPUT
:
240 for (idx
= decl
->Range
.First
; idx
<= decl
->Range
.Last
; idx
++) {
242 assert(idx
< RADEON_LLVM_MAX_OUTPUTS
);
243 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
244 ctx
->soa
.outputs
[idx
][chan
] = lp_build_alloca(&ctx
->gallivm
,
245 ctx
->soa
.bld_base
.base
.elem_type
, "");
249 ctx
->output_reg_count
= MAX2(ctx
->output_reg_count
,
250 decl
->Range
.Last
+ 1);
261 struct lp_build_tgsi_context
* bld_base
,
262 const struct tgsi_full_instruction
* inst
,
263 const struct tgsi_opcode_info
* info
,
266 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
267 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
268 struct lp_build_context base
= bld
->bld_base
.base
;
269 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[0];
270 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
271 LLVMValueRef temp_ptr
;
272 unsigned chan
, chan_index
;
273 boolean is_vec_store
= FALSE
;
275 LLVMTypeKind k
= LLVMGetTypeKind(LLVMTypeOf(dst
[0]));
276 is_vec_store
= (k
== LLVMVectorTypeKind
);
280 LLVMValueRef values
[4] = {};
281 TGSI_FOR_EACH_DST0_ENABLED_CHANNEL(inst
, chan
) {
282 LLVMValueRef index
= lp_build_const_int32(gallivm
, chan
);
283 values
[chan
] = LLVMBuildExtractElement(gallivm
->builder
,
286 bld_base
->emit_store(bld_base
, inst
, info
, values
);
290 TGSI_FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
291 LLVMValueRef value
= dst
[chan_index
];
293 if (inst
->Instruction
.Saturate
!= TGSI_SAT_NONE
) {
294 struct lp_build_emit_data clamp_emit_data
;
296 memset(&clamp_emit_data
, 0, sizeof(clamp_emit_data
));
297 clamp_emit_data
.arg_count
= 3;
298 clamp_emit_data
.args
[0] = value
;
299 clamp_emit_data
.args
[2] = base
.one
;
301 switch(inst
->Instruction
.Saturate
) {
302 case TGSI_SAT_ZERO_ONE
:
303 clamp_emit_data
.args
[1] = base
.zero
;
305 case TGSI_SAT_MINUS_PLUS_ONE
:
306 clamp_emit_data
.args
[1] = LLVMConstReal(
307 base
.elem_type
, -1.0f
);
312 value
= lp_build_emit_llvm(bld_base
, TGSI_OPCODE_CLAMP
,
316 switch(reg
->Register
.File
) {
317 case TGSI_FILE_OUTPUT
:
318 temp_ptr
= bld
->outputs
[reg
->Register
.Index
][chan_index
];
321 case TGSI_FILE_TEMPORARY
:
322 temp_ptr
= lp_get_temp_ptr_soa(bld
, reg
->Register
.Index
, chan_index
);
329 value
= bitcast(bld_base
, TGSI_TYPE_FLOAT
, value
);
331 LLVMBuildStore(builder
, value
, temp_ptr
);
335 static void bgnloop_emit(
336 const struct lp_build_tgsi_action
* action
,
337 struct lp_build_tgsi_context
* bld_base
,
338 struct lp_build_emit_data
* emit_data
)
340 struct radeon_llvm_context
* ctx
= radeon_llvm_context(bld_base
);
341 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
342 LLVMBasicBlockRef loop_block
;
343 LLVMBasicBlockRef endloop_block
;
344 endloop_block
= LLVMAppendBasicBlockInContext(gallivm
->context
,
345 ctx
->main_fn
, "ENDLOOP");
346 loop_block
= LLVMInsertBasicBlockInContext(gallivm
->context
,
347 endloop_block
, "LOOP");
348 LLVMBuildBr(gallivm
->builder
, loop_block
);
349 LLVMPositionBuilderAtEnd(gallivm
->builder
, loop_block
);
351 ctx
->loop
[ctx
->loop_depth
- 1].loop_block
= loop_block
;
352 ctx
->loop
[ctx
->loop_depth
- 1].endloop_block
= endloop_block
;
355 static void brk_emit(
356 const struct lp_build_tgsi_action
* action
,
357 struct lp_build_tgsi_context
* bld_base
,
358 struct lp_build_emit_data
* emit_data
)
360 struct radeon_llvm_context
* ctx
= radeon_llvm_context(bld_base
);
361 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
362 struct radeon_llvm_loop
* current_loop
= get_current_loop(ctx
);
364 LLVMBuildBr(gallivm
->builder
, current_loop
->endloop_block
);
367 static void cont_emit(
368 const struct lp_build_tgsi_action
* action
,
369 struct lp_build_tgsi_context
* bld_base
,
370 struct lp_build_emit_data
* emit_data
)
372 struct radeon_llvm_context
* ctx
= radeon_llvm_context(bld_base
);
373 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
374 struct radeon_llvm_loop
* current_loop
= get_current_loop(ctx
);
376 LLVMBuildBr(gallivm
->builder
, current_loop
->loop_block
);
379 static void else_emit(
380 const struct lp_build_tgsi_action
* action
,
381 struct lp_build_tgsi_context
* bld_base
,
382 struct lp_build_emit_data
* emit_data
)
384 struct radeon_llvm_context
* ctx
= radeon_llvm_context(bld_base
);
385 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
386 struct radeon_llvm_branch
* current_branch
= get_current_branch(ctx
);
387 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(gallivm
->builder
);
389 /* We need to add a terminator to the current block if the previous
390 * instruction was an ENDIF.Example:
403 if (current_block
!= current_branch
->if_block
) {
404 LLVMBuildBr(gallivm
->builder
, current_branch
->endif_block
);
406 if (!LLVMGetBasicBlockTerminator(current_branch
->if_block
)) {
407 LLVMBuildBr(gallivm
->builder
, current_branch
->endif_block
);
409 current_branch
->has_else
= 1;
410 LLVMPositionBuilderAtEnd(gallivm
->builder
, current_branch
->else_block
);
413 static void endif_emit(
414 const struct lp_build_tgsi_action
* action
,
415 struct lp_build_tgsi_context
* bld_base
,
416 struct lp_build_emit_data
* emit_data
)
418 struct radeon_llvm_context
* ctx
= radeon_llvm_context(bld_base
);
419 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
420 struct radeon_llvm_branch
* current_branch
= get_current_branch(ctx
);
421 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(gallivm
->builder
);
423 /* If we have consecutive ENDIF instructions, then the first ENDIF
424 * will not have a terminator, so we need to add one. */
425 if (current_block
!= current_branch
->if_block
426 && current_block
!= current_branch
->else_block
427 && !LLVMGetBasicBlockTerminator(current_block
)) {
429 LLVMBuildBr(gallivm
->builder
, current_branch
->endif_block
);
431 if (!LLVMGetBasicBlockTerminator(current_branch
->else_block
)) {
432 LLVMPositionBuilderAtEnd(gallivm
->builder
, current_branch
->else_block
);
433 LLVMBuildBr(gallivm
->builder
, current_branch
->endif_block
);
436 if (!LLVMGetBasicBlockTerminator(current_branch
->if_block
)) {
437 LLVMPositionBuilderAtEnd(gallivm
->builder
, current_branch
->if_block
);
438 LLVMBuildBr(gallivm
->builder
, current_branch
->endif_block
);
441 LLVMPositionBuilderAtEnd(gallivm
->builder
, current_branch
->endif_block
);
445 static void endloop_emit(
446 const struct lp_build_tgsi_action
* action
,
447 struct lp_build_tgsi_context
* bld_base
,
448 struct lp_build_emit_data
* emit_data
)
450 struct radeon_llvm_context
* ctx
= radeon_llvm_context(bld_base
);
451 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
452 struct radeon_llvm_loop
* current_loop
= get_current_loop(ctx
);
454 if (!LLVMGetBasicBlockTerminator(LLVMGetInsertBlock(gallivm
->builder
))) {
455 LLVMBuildBr(gallivm
->builder
, current_loop
->loop_block
);
458 LLVMPositionBuilderAtEnd(gallivm
->builder
, current_loop
->endloop_block
);
463 const struct lp_build_tgsi_action
* action
,
464 struct lp_build_tgsi_context
* bld_base
,
465 struct lp_build_emit_data
* emit_data
)
467 struct radeon_llvm_context
* ctx
= radeon_llvm_context(bld_base
);
468 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
470 LLVMBasicBlockRef if_block
, else_block
, endif_block
;
472 cond
= LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
,
473 bitcast(bld_base
, TGSI_TYPE_UNSIGNED
, emit_data
->args
[0]),
474 bld_base
->int_bld
.zero
, "");
476 endif_block
= LLVMAppendBasicBlockInContext(gallivm
->context
,
477 ctx
->main_fn
, "ENDIF");
478 if_block
= LLVMInsertBasicBlockInContext(gallivm
->context
,
480 else_block
= LLVMInsertBasicBlockInContext(gallivm
->context
,
481 endif_block
, "ELSE");
482 LLVMBuildCondBr(gallivm
->builder
, cond
, if_block
, else_block
);
483 LLVMPositionBuilderAtEnd(gallivm
->builder
, if_block
);
486 ctx
->branch
[ctx
->branch_depth
- 1].endif_block
= endif_block
;
487 ctx
->branch
[ctx
->branch_depth
- 1].if_block
= if_block
;
488 ctx
->branch
[ctx
->branch_depth
- 1].else_block
= else_block
;
489 ctx
->branch
[ctx
->branch_depth
- 1].has_else
= 0;
492 static void kil_emit(
493 const struct lp_build_tgsi_action
* action
,
494 struct lp_build_tgsi_context
* bld_base
,
495 struct lp_build_emit_data
* emit_data
)
498 for (i
= 0; i
< emit_data
->arg_count
; i
++) {
499 emit_data
->output
[i
] = lp_build_intrinsic_unary(
500 bld_base
->base
.gallivm
->builder
,
502 emit_data
->dst_type
, emit_data
->args
[i
]);
507 static void emit_prepare_cube_coords(
508 struct lp_build_tgsi_context
* bld_base
,
509 struct lp_build_emit_data
* emit_data
)
511 boolean shadowcube
= (emit_data
->inst
->Texture
.Texture
== TGSI_TEXTURE_SHADOWCUBE
);
512 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
513 LLVMBuilderRef builder
= gallivm
->builder
;
514 LLVMTypeRef type
= bld_base
->base
.elem_type
;
515 LLVMValueRef coords
[4];
516 LLVMValueRef mad_args
[3];
519 LLVMValueRef v
= build_intrinsic(builder
, "llvm.AMDGPU.cube",
520 LLVMVectorType(type
, 4),
521 &emit_data
->args
[0],1, LLVMReadNoneAttribute
);
523 /* save src.w for shadow cube */
524 cnt
= shadowcube
? 3 : 4;
526 for (i
= 0; i
< cnt
; ++i
) {
527 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
528 coords
[i
] = LLVMBuildExtractElement(builder
, v
, idx
, "");
531 coords
[2] = build_intrinsic(builder
, "llvm.AMDIL.fabs.",
532 type
, &coords
[2], 1, LLVMReadNoneAttribute
);
533 coords
[2] = build_intrinsic(builder
, "llvm.AMDGPU.rcp",
534 type
, &coords
[2], 1, LLVMReadNoneAttribute
);
536 mad_args
[1] = coords
[2];
537 mad_args
[2] = LLVMConstReal(type
, 1.5);
539 mad_args
[0] = coords
[0];
540 coords
[0] = build_intrinsic(builder
, "llvm.AMDIL.mad.",
541 type
, mad_args
, 3, LLVMReadNoneAttribute
);
543 mad_args
[0] = coords
[1];
544 coords
[1] = build_intrinsic(builder
, "llvm.AMDIL.mad.",
545 type
, mad_args
, 3, LLVMReadNoneAttribute
);
547 /* apply yxwy swizzle to cooords */
548 coords
[2] = coords
[3];
549 coords
[3] = coords
[1];
550 coords
[1] = coords
[0];
551 coords
[0] = coords
[3];
553 emit_data
->args
[0] = lp_build_gather_values(bld_base
->base
.gallivm
,
557 static void txd_fetch_args(
558 struct lp_build_tgsi_context
* bld_base
,
559 struct lp_build_emit_data
* emit_data
)
561 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
563 LLVMValueRef coords
[4];
565 for (src
= 0; src
< 3; src
++) {
566 for (chan
= 0; chan
< 4; chan
++)
567 coords
[chan
] = lp_build_emit_fetch(bld_base
, inst
, src
, chan
);
569 emit_data
->args
[src
] = lp_build_gather_values(bld_base
->base
.gallivm
,
572 emit_data
->arg_count
= 3;
573 emit_data
->dst_type
= LLVMVectorType(bld_base
->base
.elem_type
, 4);
577 static void txp_fetch_args(
578 struct lp_build_tgsi_context
* bld_base
,
579 struct lp_build_emit_data
* emit_data
)
581 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
584 LLVMValueRef coords
[4];
586 emit_data
->dst_type
= LLVMVectorType(bld_base
->base
.elem_type
, 4);
587 src_w
= lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_W
);
589 for (chan
= 0; chan
< 3; chan
++ ) {
590 LLVMValueRef arg
= lp_build_emit_fetch(bld_base
,
591 emit_data
->inst
, 0, chan
);
592 coords
[chan
] = lp_build_emit_llvm_binary(bld_base
,
593 TGSI_OPCODE_DIV
, arg
, src_w
);
595 coords
[3] = bld_base
->base
.one
;
596 emit_data
->args
[0] = lp_build_gather_values(bld_base
->base
.gallivm
,
598 emit_data
->arg_count
= 1;
600 if ((inst
->Texture
.Texture
== TGSI_TEXTURE_CUBE
||
601 inst
->Texture
.Texture
== TGSI_TEXTURE_SHADOWCUBE
) &&
602 inst
->Instruction
.Opcode
!= TGSI_OPCODE_TXQ
) {
603 emit_prepare_cube_coords(bld_base
, emit_data
);
607 static void tex_fetch_args(
608 struct lp_build_tgsi_context
* bld_base
,
609 struct lp_build_emit_data
* emit_data
)
611 /* XXX: lp_build_swizzle_aos() was failing with wrong arg types,
612 * when we used CHAN_ALL. We should be able to get this to work,
613 * but for now we will swizzle it ourselves
614 emit_data->args[0] = lp_build_emit_fetch(bld_base, emit_data->inst,
619 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
621 LLVMValueRef coords
[4];
623 for (chan
= 0; chan
< 4; chan
++) {
624 coords
[chan
] = lp_build_emit_fetch(bld_base
, inst
, 0, chan
);
627 emit_data
->arg_count
= 1;
628 emit_data
->args
[0] = lp_build_gather_values(bld_base
->base
.gallivm
,
630 emit_data
->dst_type
= LLVMVectorType(bld_base
->base
.elem_type
, 4);
632 if ((inst
->Texture
.Texture
== TGSI_TEXTURE_CUBE
||
633 inst
->Texture
.Texture
== TGSI_TEXTURE_SHADOWCUBE
) &&
634 inst
->Instruction
.Opcode
!= TGSI_OPCODE_TXQ
) {
635 emit_prepare_cube_coords(bld_base
, emit_data
);
639 static void txf_fetch_args(
640 struct lp_build_tgsi_context
* bld_base
,
641 struct lp_build_emit_data
* emit_data
)
643 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
644 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
645 const struct tgsi_texture_offset
* off
= inst
->TexOffsets
;
646 LLVMTypeRef offset_type
= bld_base
->int_bld
.elem_type
;
648 /* fetch tex coords */
649 tex_fetch_args(bld_base
, emit_data
);
651 /* fetch tex offsets */
652 if (inst
->Texture
.NumOffsets
) {
653 assert(inst
->Texture
.NumOffsets
== 1);
655 emit_data
->args
[1] = LLVMConstBitCast(
656 bld
->immediates
[off
->Index
][off
->SwizzleX
],
658 emit_data
->args
[2] = LLVMConstBitCast(
659 bld
->immediates
[off
->Index
][off
->SwizzleY
],
661 emit_data
->args
[3] = LLVMConstBitCast(
662 bld
->immediates
[off
->Index
][off
->SwizzleZ
],
665 emit_data
->args
[1] = bld_base
->int_bld
.zero
;
666 emit_data
->args
[2] = bld_base
->int_bld
.zero
;
667 emit_data
->args
[3] = bld_base
->int_bld
.zero
;
670 emit_data
->arg_count
= 4;
673 static void emit_icmp(
674 const struct lp_build_tgsi_action
* action
,
675 struct lp_build_tgsi_context
* bld_base
,
676 struct lp_build_emit_data
* emit_data
)
679 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
680 LLVMContextRef context
= bld_base
->base
.gallivm
->context
;
682 switch (emit_data
->inst
->Instruction
.Opcode
) {
683 case TGSI_OPCODE_USEQ
: pred
= LLVMIntEQ
; break;
684 case TGSI_OPCODE_USNE
: pred
= LLVMIntNE
; break;
685 case TGSI_OPCODE_USGE
: pred
= LLVMIntUGE
; break;
686 case TGSI_OPCODE_USLT
: pred
= LLVMIntULT
; break;
687 case TGSI_OPCODE_ISGE
: pred
= LLVMIntSGE
; break;
688 case TGSI_OPCODE_ISLT
: pred
= LLVMIntSLT
; break;
690 assert(!"unknown instruction");
693 LLVMValueRef v
= LLVMBuildICmp(builder
, pred
,
694 emit_data
->args
[0], emit_data
->args
[1],"");
696 v
= LLVMBuildSExtOrBitCast(builder
, v
,
697 LLVMInt32TypeInContext(context
), "");
699 emit_data
->output
[emit_data
->chan
] = v
;
702 static void emit_cmp(
703 const struct lp_build_tgsi_action
*action
,
704 struct lp_build_tgsi_context
* bld_base
,
705 struct lp_build_emit_data
* emit_data
)
707 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
708 LLVMRealPredicate pred
;
711 /* XXX I'm not sure whether to do unordered or ordered comparisons,
712 * but llvmpipe uses unordered comparisons, so for consistency we use
713 * unordered. (The authors of llvmpipe aren't sure about using
714 * unordered vs ordered comparisons either.
716 switch (emit_data
->inst
->Instruction
.Opcode
) {
717 case TGSI_OPCODE_SGE
: pred
= LLVMRealUGE
; break;
718 case TGSI_OPCODE_SEQ
: pred
= LLVMRealUEQ
; break;
719 case TGSI_OPCODE_SLE
: pred
= LLVMRealULE
; break;
720 case TGSI_OPCODE_SLT
: pred
= LLVMRealULT
; break;
721 case TGSI_OPCODE_SNE
: pred
= LLVMRealUNE
; break;
722 case TGSI_OPCODE_SGT
: pred
= LLVMRealUGT
; break;
723 default: assert(!"unknown instruction");
726 cond
= LLVMBuildFCmp(builder
,
727 pred
, emit_data
->args
[0], emit_data
->args
[1], "");
729 emit_data
->output
[emit_data
->chan
] = LLVMBuildSelect(builder
,
730 cond
, bld_base
->base
.one
, bld_base
->base
.zero
, "");
733 static void emit_not(
734 const struct lp_build_tgsi_action
* action
,
735 struct lp_build_tgsi_context
* bld_base
,
736 struct lp_build_emit_data
* emit_data
)
738 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
739 LLVMValueRef v
= bitcast(bld_base
, TGSI_TYPE_UNSIGNED
,
741 emit_data
->output
[emit_data
->chan
] = LLVMBuildNot(builder
, v
, "");
744 static void emit_and(
745 const struct lp_build_tgsi_action
* action
,
746 struct lp_build_tgsi_context
* bld_base
,
747 struct lp_build_emit_data
* emit_data
)
749 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
750 emit_data
->output
[emit_data
->chan
] = LLVMBuildAnd(builder
,
751 emit_data
->args
[0], emit_data
->args
[1], "");
755 const struct lp_build_tgsi_action
* action
,
756 struct lp_build_tgsi_context
* bld_base
,
757 struct lp_build_emit_data
* emit_data
)
759 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
760 emit_data
->output
[emit_data
->chan
] = LLVMBuildOr(builder
,
761 emit_data
->args
[0], emit_data
->args
[1], "");
764 static void emit_uadd(
765 const struct lp_build_tgsi_action
* action
,
766 struct lp_build_tgsi_context
* bld_base
,
767 struct lp_build_emit_data
* emit_data
)
769 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
770 emit_data
->output
[emit_data
->chan
] = LLVMBuildAdd(builder
,
771 emit_data
->args
[0], emit_data
->args
[1], "");
774 static void emit_udiv(
775 const struct lp_build_tgsi_action
* action
,
776 struct lp_build_tgsi_context
* bld_base
,
777 struct lp_build_emit_data
* emit_data
)
779 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
780 emit_data
->output
[emit_data
->chan
] = LLVMBuildUDiv(builder
,
781 emit_data
->args
[0], emit_data
->args
[1], "");
784 static void emit_idiv(
785 const struct lp_build_tgsi_action
* action
,
786 struct lp_build_tgsi_context
* bld_base
,
787 struct lp_build_emit_data
* emit_data
)
789 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
790 emit_data
->output
[emit_data
->chan
] = LLVMBuildSDiv(builder
,
791 emit_data
->args
[0], emit_data
->args
[1], "");
794 static void emit_mod(
795 const struct lp_build_tgsi_action
* action
,
796 struct lp_build_tgsi_context
* bld_base
,
797 struct lp_build_emit_data
* emit_data
)
799 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
800 emit_data
->output
[emit_data
->chan
] = LLVMBuildSRem(builder
,
801 emit_data
->args
[0], emit_data
->args
[1], "");
804 static void emit_umod(
805 const struct lp_build_tgsi_action
* action
,
806 struct lp_build_tgsi_context
* bld_base
,
807 struct lp_build_emit_data
* emit_data
)
809 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
810 emit_data
->output
[emit_data
->chan
] = LLVMBuildURem(builder
,
811 emit_data
->args
[0], emit_data
->args
[1], "");
814 static void emit_shl(
815 const struct lp_build_tgsi_action
* action
,
816 struct lp_build_tgsi_context
* bld_base
,
817 struct lp_build_emit_data
* emit_data
)
819 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
820 emit_data
->output
[emit_data
->chan
] = LLVMBuildShl(builder
,
821 emit_data
->args
[0], emit_data
->args
[1], "");
824 static void emit_ushr(
825 const struct lp_build_tgsi_action
* action
,
826 struct lp_build_tgsi_context
* bld_base
,
827 struct lp_build_emit_data
* emit_data
)
829 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
830 emit_data
->output
[emit_data
->chan
] = LLVMBuildLShr(builder
,
831 emit_data
->args
[0], emit_data
->args
[1], "");
833 static void emit_ishr(
834 const struct lp_build_tgsi_action
* action
,
835 struct lp_build_tgsi_context
* bld_base
,
836 struct lp_build_emit_data
* emit_data
)
838 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
839 emit_data
->output
[emit_data
->chan
] = LLVMBuildAShr(builder
,
840 emit_data
->args
[0], emit_data
->args
[1], "");
843 static void emit_xor(
844 const struct lp_build_tgsi_action
* action
,
845 struct lp_build_tgsi_context
* bld_base
,
846 struct lp_build_emit_data
* emit_data
)
848 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
849 emit_data
->output
[emit_data
->chan
] = LLVMBuildXor(builder
,
850 emit_data
->args
[0], emit_data
->args
[1], "");
853 static void emit_ssg(
854 const struct lp_build_tgsi_action
* action
,
855 struct lp_build_tgsi_context
* bld_base
,
856 struct lp_build_emit_data
* emit_data
)
858 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
860 LLVMValueRef cmp
, val
;
862 if (emit_data
->inst
->Instruction
.Opcode
== TGSI_OPCODE_ISSG
) {
863 cmp
= LLVMBuildICmp(builder
, LLVMIntSGT
, emit_data
->args
[0], bld_base
->int_bld
.zero
, "");
864 val
= LLVMBuildSelect(builder
, cmp
, bld_base
->int_bld
.one
, emit_data
->args
[0], "");
865 cmp
= LLVMBuildICmp(builder
, LLVMIntSGE
, val
, bld_base
->int_bld
.zero
, "");
866 val
= LLVMBuildSelect(builder
, cmp
, val
, LLVMConstInt(bld_base
->int_bld
.elem_type
, -1, true), "");
867 } else { // float SSG
868 cmp
= LLVMBuildFCmp(builder
, LLVMRealUGT
, emit_data
->args
[0], bld_base
->int_bld
.zero
, "");
869 val
= LLVMBuildSelect(builder
, cmp
, bld_base
->base
.one
, emit_data
->args
[0], "");
870 cmp
= LLVMBuildFCmp(builder
, LLVMRealUGE
, val
, bld_base
->base
.zero
, "");
871 val
= LLVMBuildSelect(builder
, cmp
, val
, LLVMConstReal(bld_base
->base
.elem_type
, -1), "");
874 emit_data
->output
[emit_data
->chan
] = val
;
877 static void emit_ineg(
878 const struct lp_build_tgsi_action
* action
,
879 struct lp_build_tgsi_context
* bld_base
,
880 struct lp_build_emit_data
* emit_data
)
882 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
883 emit_data
->output
[emit_data
->chan
] = LLVMBuildNeg(builder
,
884 emit_data
->args
[0], "");
887 static void emit_f2i(
888 const struct lp_build_tgsi_action
* action
,
889 struct lp_build_tgsi_context
* bld_base
,
890 struct lp_build_emit_data
* emit_data
)
892 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
893 emit_data
->output
[emit_data
->chan
] = LLVMBuildFPToSI(builder
,
894 emit_data
->args
[0], bld_base
->int_bld
.elem_type
, "");
897 static void emit_f2u(
898 const struct lp_build_tgsi_action
* action
,
899 struct lp_build_tgsi_context
* bld_base
,
900 struct lp_build_emit_data
* emit_data
)
902 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
903 emit_data
->output
[emit_data
->chan
] = LLVMBuildFPToUI(builder
,
904 emit_data
->args
[0], bld_base
->uint_bld
.elem_type
, "");
907 static void emit_i2f(
908 const struct lp_build_tgsi_action
* action
,
909 struct lp_build_tgsi_context
* bld_base
,
910 struct lp_build_emit_data
* emit_data
)
912 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
913 emit_data
->output
[emit_data
->chan
] = LLVMBuildSIToFP(builder
,
914 emit_data
->args
[0], bld_base
->base
.elem_type
, "");
917 static void emit_u2f(
918 const struct lp_build_tgsi_action
* action
,
919 struct lp_build_tgsi_context
* bld_base
,
920 struct lp_build_emit_data
* emit_data
)
922 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
923 emit_data
->output
[emit_data
->chan
] = LLVMBuildUIToFP(builder
,
924 emit_data
->args
[0], bld_base
->base
.elem_type
, "");
927 static void emit_immediate(struct lp_build_tgsi_context
* bld_base
,
928 const struct tgsi_full_immediate
*imm
)
931 struct radeon_llvm_context
* ctx
= radeon_llvm_context(bld_base
);
933 for (i
= 0; i
< 4; ++i
) {
934 ctx
->soa
.immediates
[ctx
->soa
.num_immediates
][i
] =
935 LLVMConstInt(bld_base
->uint_bld
.elem_type
, imm
->u
[i
].Uint
, false );
938 ctx
->soa
.num_immediates
++;
942 build_intrinsic(LLVMBuilderRef builder
,
944 LLVMTypeRef ret_type
,
949 LLVMModuleRef module
= LLVMGetGlobalParent(LLVMGetBasicBlockParent(LLVMGetInsertBlock(builder
)));
950 LLVMValueRef function
;
952 function
= LLVMGetNamedFunction(module
, name
);
954 LLVMTypeRef arg_types
[LP_MAX_FUNC_ARGS
];
957 assert(num_args
<= LP_MAX_FUNC_ARGS
);
959 for(i
= 0; i
< num_args
; ++i
) {
961 arg_types
[i
] = LLVMTypeOf(args
[i
]);
964 function
= lp_declare_intrinsic(module
, name
, ret_type
, arg_types
, num_args
);
967 LLVMAddFunctionAttr(function
, attr
);
970 return LLVMBuildCall(builder
, function
, args
, num_args
, "");
974 build_tgsi_intrinsic_nomem(
975 const struct lp_build_tgsi_action
* action
,
976 struct lp_build_tgsi_context
* bld_base
,
977 struct lp_build_emit_data
* emit_data
)
979 struct lp_build_context
* base
= &bld_base
->base
;
980 emit_data
->output
[emit_data
->chan
] = build_intrinsic(
981 base
->gallivm
->builder
, action
->intr_name
,
982 emit_data
->dst_type
, emit_data
->args
,
983 emit_data
->arg_count
, LLVMReadNoneAttribute
);
986 void radeon_llvm_context_init(struct radeon_llvm_context
* ctx
)
989 LLVMTypeRef main_fn_type
;
990 LLVMBasicBlockRef main_fn_body
;
992 /* Initialize the gallivm object:
993 * We are only using the module, context, and builder fields of this struct.
994 * This should be enough for us to be able to pass our gallivm struct to the
995 * helper functions in the gallivm module.
997 memset(&ctx
->gallivm
, 0, sizeof (ctx
->gallivm
));
998 memset(&ctx
->soa
, 0, sizeof(ctx
->soa
));
999 ctx
->gallivm
.context
= LLVMContextCreate();
1000 ctx
->gallivm
.module
= LLVMModuleCreateWithNameInContext("tgsi",
1001 ctx
->gallivm
.context
);
1002 ctx
->gallivm
.builder
= LLVMCreateBuilderInContext(ctx
->gallivm
.context
);
1004 /* Setup the module */
1005 main_fn_type
= LLVMFunctionType(LLVMVoidTypeInContext(ctx
->gallivm
.context
),
1007 ctx
->main_fn
= LLVMAddFunction(ctx
->gallivm
.module
, "main", main_fn_type
);
1008 main_fn_body
= LLVMAppendBasicBlockInContext(ctx
->gallivm
.context
,
1009 ctx
->main_fn
, "main_body");
1010 LLVMPositionBuilderAtEnd(ctx
->gallivm
.builder
, main_fn_body
);
1012 ctx
->store_output_intr
= "llvm.AMDGPU.store.output.";
1013 ctx
->swizzle_intr
= "llvm.AMDGPU.swizzle";
1014 struct lp_build_tgsi_context
* bld_base
= &ctx
->soa
.bld_base
;
1016 /* XXX: We need to revisit this.I think the correct way to do this is
1017 * to use length = 4 here and use the elem_bld for everything. */
1018 type
.floating
= TRUE
;
1023 lp_build_context_init(&bld_base
->base
, &ctx
->gallivm
, type
);
1024 lp_build_context_init(&ctx
->soa
.bld_base
.uint_bld
, &ctx
->gallivm
, lp_uint_type(type
));
1025 lp_build_context_init(&ctx
->soa
.bld_base
.int_bld
, &ctx
->gallivm
, lp_int_type(type
));
1028 bld_base
->emit_store
= emit_store
;
1029 bld_base
->emit_swizzle
= emit_swizzle
;
1030 bld_base
->emit_declaration
= emit_declaration
;
1031 bld_base
->emit_immediate
= emit_immediate
;
1033 bld_base
->emit_fetch_funcs
[TGSI_FILE_IMMEDIATE
] = emit_fetch_immediate
;
1034 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = emit_fetch_input
;
1035 bld_base
->emit_fetch_funcs
[TGSI_FILE_TEMPORARY
] = emit_fetch_temporary
;
1036 bld_base
->emit_fetch_funcs
[TGSI_FILE_OUTPUT
] = emit_fetch_output
;
1038 /* Allocate outputs */
1039 ctx
->soa
.outputs
= ctx
->outputs
;
1041 /* XXX: Is there a better way to initialize all this ? */
1043 lp_set_default_actions(bld_base
);
1045 bld_base
->op_actions
[TGSI_OPCODE_IABS
].emit
= build_tgsi_intrinsic_nomem
;
1046 bld_base
->op_actions
[TGSI_OPCODE_IABS
].intr_name
= "llvm.AMDIL.abs.";
1047 bld_base
->op_actions
[TGSI_OPCODE_NOT
].emit
= emit_not
;
1048 bld_base
->op_actions
[TGSI_OPCODE_AND
].emit
= emit_and
;
1049 bld_base
->op_actions
[TGSI_OPCODE_XOR
].emit
= emit_xor
;
1050 bld_base
->op_actions
[TGSI_OPCODE_OR
].emit
= emit_or
;
1051 bld_base
->op_actions
[TGSI_OPCODE_UADD
].emit
= emit_uadd
;
1052 bld_base
->op_actions
[TGSI_OPCODE_UDIV
].emit
= emit_udiv
;
1053 bld_base
->op_actions
[TGSI_OPCODE_IDIV
].emit
= emit_idiv
;
1054 bld_base
->op_actions
[TGSI_OPCODE_MOD
].emit
= emit_mod
;
1055 bld_base
->op_actions
[TGSI_OPCODE_UMOD
].emit
= emit_umod
;
1056 bld_base
->op_actions
[TGSI_OPCODE_INEG
].emit
= emit_ineg
;
1057 bld_base
->op_actions
[TGSI_OPCODE_SHL
].emit
= emit_shl
;
1058 bld_base
->op_actions
[TGSI_OPCODE_ISHR
].emit
= emit_ishr
;
1059 bld_base
->op_actions
[TGSI_OPCODE_USHR
].emit
= emit_ushr
;
1060 bld_base
->op_actions
[TGSI_OPCODE_SSG
].emit
= emit_ssg
;
1061 bld_base
->op_actions
[TGSI_OPCODE_ISSG
].emit
= emit_ssg
;
1062 bld_base
->op_actions
[TGSI_OPCODE_I2F
].emit
= emit_i2f
;
1063 bld_base
->op_actions
[TGSI_OPCODE_U2F
].emit
= emit_u2f
;
1064 bld_base
->op_actions
[TGSI_OPCODE_F2I
].emit
= emit_f2i
;
1065 bld_base
->op_actions
[TGSI_OPCODE_F2U
].emit
= emit_f2u
;
1066 bld_base
->op_actions
[TGSI_OPCODE_DDX
].intr_name
= "llvm.AMDGPU.ddx";
1067 bld_base
->op_actions
[TGSI_OPCODE_DDX
].fetch_args
= tex_fetch_args
;
1068 bld_base
->op_actions
[TGSI_OPCODE_DDY
].intr_name
= "llvm.AMDGPU.ddy";
1069 bld_base
->op_actions
[TGSI_OPCODE_DDY
].fetch_args
= tex_fetch_args
;
1070 bld_base
->op_actions
[TGSI_OPCODE_USEQ
].emit
= emit_icmp
;
1071 bld_base
->op_actions
[TGSI_OPCODE_USGE
].emit
= emit_icmp
;
1072 bld_base
->op_actions
[TGSI_OPCODE_USLT
].emit
= emit_icmp
;
1073 bld_base
->op_actions
[TGSI_OPCODE_USNE
].emit
= emit_icmp
;
1074 bld_base
->op_actions
[TGSI_OPCODE_ISGE
].emit
= emit_icmp
;
1075 bld_base
->op_actions
[TGSI_OPCODE_ISLT
].emit
= emit_icmp
;
1076 bld_base
->op_actions
[TGSI_OPCODE_ROUND
].emit
= build_tgsi_intrinsic_nomem
;
1077 bld_base
->op_actions
[TGSI_OPCODE_ROUND
].intr_name
= "llvm.AMDIL.round.nearest.";
1078 bld_base
->op_actions
[TGSI_OPCODE_MIN
].emit
= build_tgsi_intrinsic_nomem
;
1079 bld_base
->op_actions
[TGSI_OPCODE_MIN
].intr_name
= "llvm.AMDIL.min.";
1080 bld_base
->op_actions
[TGSI_OPCODE_MAX
].emit
= build_tgsi_intrinsic_nomem
;
1081 bld_base
->op_actions
[TGSI_OPCODE_MAX
].intr_name
= "llvm.AMDIL.max.";
1082 bld_base
->op_actions
[TGSI_OPCODE_IMIN
].emit
= build_tgsi_intrinsic_nomem
;
1083 bld_base
->op_actions
[TGSI_OPCODE_IMIN
].intr_name
= "llvm.AMDGPU.imin";
1084 bld_base
->op_actions
[TGSI_OPCODE_IMAX
].emit
= build_tgsi_intrinsic_nomem
;
1085 bld_base
->op_actions
[TGSI_OPCODE_IMAX
].intr_name
= "llvm.AMDGPU.imax";
1086 bld_base
->op_actions
[TGSI_OPCODE_UMIN
].emit
= build_tgsi_intrinsic_nomem
;
1087 bld_base
->op_actions
[TGSI_OPCODE_UMIN
].intr_name
= "llvm.AMDGPU.umin";
1088 bld_base
->op_actions
[TGSI_OPCODE_UMAX
].emit
= build_tgsi_intrinsic_nomem
;
1089 bld_base
->op_actions
[TGSI_OPCODE_UMAX
].intr_name
= "llvm.AMDGPU.umax";
1090 bld_base
->op_actions
[TGSI_OPCODE_TXF
].fetch_args
= txf_fetch_args
;
1091 bld_base
->op_actions
[TGSI_OPCODE_TXF
].intr_name
= "llvm.AMDGPU.txf";
1092 bld_base
->op_actions
[TGSI_OPCODE_TXQ
].fetch_args
= tex_fetch_args
;
1093 bld_base
->op_actions
[TGSI_OPCODE_TXQ
].intr_name
= "llvm.AMDGPU.txq";
1094 bld_base
->op_actions
[TGSI_OPCODE_CEIL
].emit
= build_tgsi_intrinsic_nomem
;
1095 bld_base
->op_actions
[TGSI_OPCODE_CEIL
].intr_name
= "llvm.AMDIL.round.posinf.";
1099 bld_base
->op_actions
[TGSI_OPCODE_ABS
].emit
= build_tgsi_intrinsic_nomem
;
1100 bld_base
->op_actions
[TGSI_OPCODE_ABS
].intr_name
= "llvm.AMDIL.fabs.";
1101 bld_base
->op_actions
[TGSI_OPCODE_ARL
].emit
= build_tgsi_intrinsic_nomem
;
1102 bld_base
->op_actions
[TGSI_OPCODE_ARL
].intr_name
= "llvm.AMDGPU.arl";
1103 bld_base
->op_actions
[TGSI_OPCODE_BGNLOOP
].emit
= bgnloop_emit
;
1104 bld_base
->op_actions
[TGSI_OPCODE_BRK
].emit
= brk_emit
;
1105 bld_base
->op_actions
[TGSI_OPCODE_CONT
].emit
= cont_emit
;
1106 bld_base
->op_actions
[TGSI_OPCODE_CLAMP
].emit
= build_tgsi_intrinsic_nomem
;
1107 bld_base
->op_actions
[TGSI_OPCODE_CLAMP
].intr_name
= "llvm.AMDIL.clamp.";
1108 bld_base
->op_actions
[TGSI_OPCODE_CMP
].emit
= build_tgsi_intrinsic_nomem
;
1109 bld_base
->op_actions
[TGSI_OPCODE_CMP
].intr_name
= "llvm.AMDGPU.cndlt";
1110 bld_base
->op_actions
[TGSI_OPCODE_COS
].emit
= build_tgsi_intrinsic_nomem
;
1111 bld_base
->op_actions
[TGSI_OPCODE_COS
].intr_name
= "llvm.AMDGPU.cos";
1112 bld_base
->op_actions
[TGSI_OPCODE_DIV
].emit
= build_tgsi_intrinsic_nomem
;
1113 bld_base
->op_actions
[TGSI_OPCODE_DIV
].intr_name
= "llvm.AMDGPU.div";
1114 bld_base
->op_actions
[TGSI_OPCODE_ELSE
].emit
= else_emit
;
1115 bld_base
->op_actions
[TGSI_OPCODE_ENDIF
].emit
= endif_emit
;
1116 bld_base
->op_actions
[TGSI_OPCODE_ENDLOOP
].emit
= endloop_emit
;
1117 bld_base
->op_actions
[TGSI_OPCODE_EX2
].emit
= build_tgsi_intrinsic_nomem
;
1118 bld_base
->op_actions
[TGSI_OPCODE_EX2
].intr_name
= "llvm.AMDIL.exp.";
1119 bld_base
->op_actions
[TGSI_OPCODE_FLR
].emit
= build_tgsi_intrinsic_nomem
;
1120 bld_base
->op_actions
[TGSI_OPCODE_FLR
].intr_name
= "llvm.AMDGPU.floor";
1121 bld_base
->op_actions
[TGSI_OPCODE_FRC
].emit
= build_tgsi_intrinsic_nomem
;
1122 bld_base
->op_actions
[TGSI_OPCODE_FRC
].intr_name
= "llvm.AMDIL.fraction.";
1123 bld_base
->op_actions
[TGSI_OPCODE_IF
].emit
= if_emit
;
1124 bld_base
->op_actions
[TGSI_OPCODE_KIL
].emit
= kil_emit
;
1125 bld_base
->op_actions
[TGSI_OPCODE_KIL
].intr_name
= "llvm.AMDGPU.kill";
1126 bld_base
->op_actions
[TGSI_OPCODE_KILP
].emit
= lp_build_tgsi_intrinsic
;
1127 bld_base
->op_actions
[TGSI_OPCODE_KILP
].intr_name
= "llvm.AMDGPU.kilp";
1128 bld_base
->op_actions
[TGSI_OPCODE_LG2
].emit
= build_tgsi_intrinsic_nomem
;
1129 bld_base
->op_actions
[TGSI_OPCODE_LG2
].intr_name
= "llvm.AMDIL.log.";
1130 bld_base
->op_actions
[TGSI_OPCODE_LRP
].emit
= build_tgsi_intrinsic_nomem
;
1131 bld_base
->op_actions
[TGSI_OPCODE_LRP
].intr_name
= "llvm.AMDGPU.lrp";
1132 bld_base
->op_actions
[TGSI_OPCODE_MIN
].emit
= build_tgsi_intrinsic_nomem
;
1133 bld_base
->op_actions
[TGSI_OPCODE_MIN
].intr_name
= "llvm.AMDIL.min.";
1134 bld_base
->op_actions
[TGSI_OPCODE_MAD
].emit
= build_tgsi_intrinsic_nomem
;
1135 bld_base
->op_actions
[TGSI_OPCODE_MAD
].intr_name
= "llvm.AMDIL.mad.";
1136 bld_base
->op_actions
[TGSI_OPCODE_MAX
].emit
= build_tgsi_intrinsic_nomem
;
1137 bld_base
->op_actions
[TGSI_OPCODE_MAX
].intr_name
= "llvm.AMDIL.max.";
1138 bld_base
->op_actions
[TGSI_OPCODE_MUL
].emit
= build_tgsi_intrinsic_nomem
;
1139 bld_base
->op_actions
[TGSI_OPCODE_MUL
].intr_name
= "llvm.AMDGPU.mul";
1140 bld_base
->op_actions
[TGSI_OPCODE_POW
].emit
= build_tgsi_intrinsic_nomem
;
1141 bld_base
->op_actions
[TGSI_OPCODE_POW
].intr_name
= "llvm.AMDGPU.pow";
1142 bld_base
->op_actions
[TGSI_OPCODE_RCP
].emit
= build_tgsi_intrinsic_nomem
;
1143 bld_base
->op_actions
[TGSI_OPCODE_RCP
].intr_name
= "llvm.AMDGPU.rcp";
1144 bld_base
->op_actions
[TGSI_OPCODE_SSG
].emit
= build_tgsi_intrinsic_nomem
;
1145 bld_base
->op_actions
[TGSI_OPCODE_SSG
].intr_name
= "llvm.AMDGPU.ssg";
1146 bld_base
->op_actions
[TGSI_OPCODE_SGE
].emit
= emit_cmp
;
1147 bld_base
->op_actions
[TGSI_OPCODE_SEQ
].emit
= emit_cmp
;
1148 bld_base
->op_actions
[TGSI_OPCODE_SLE
].emit
= emit_cmp
;
1149 bld_base
->op_actions
[TGSI_OPCODE_SLT
].emit
= emit_cmp
;
1150 bld_base
->op_actions
[TGSI_OPCODE_SNE
].emit
= emit_cmp
;
1151 bld_base
->op_actions
[TGSI_OPCODE_SGT
].emit
= emit_cmp
;
1152 bld_base
->op_actions
[TGSI_OPCODE_SIN
].emit
= build_tgsi_intrinsic_nomem
;
1153 bld_base
->op_actions
[TGSI_OPCODE_SIN
].intr_name
= "llvm.AMDGPU.sin";
1154 bld_base
->op_actions
[TGSI_OPCODE_TEX
].fetch_args
= tex_fetch_args
;
1155 bld_base
->op_actions
[TGSI_OPCODE_TEX
].intr_name
= "llvm.AMDGPU.tex";
1156 bld_base
->op_actions
[TGSI_OPCODE_TXB
].fetch_args
= tex_fetch_args
;
1157 bld_base
->op_actions
[TGSI_OPCODE_TXB
].intr_name
= "llvm.AMDGPU.txb";
1158 bld_base
->op_actions
[TGSI_OPCODE_TXD
].fetch_args
= txd_fetch_args
;
1159 bld_base
->op_actions
[TGSI_OPCODE_TXD
].intr_name
= "llvm.AMDGPU.txd";
1160 bld_base
->op_actions
[TGSI_OPCODE_TXL
].fetch_args
= tex_fetch_args
;
1161 bld_base
->op_actions
[TGSI_OPCODE_TXL
].intr_name
= "llvm.AMDGPU.txl";
1162 bld_base
->op_actions
[TGSI_OPCODE_TXP
].fetch_args
= txp_fetch_args
;
1163 bld_base
->op_actions
[TGSI_OPCODE_TXP
].intr_name
= "llvm.AMDGPU.tex";
1164 bld_base
->op_actions
[TGSI_OPCODE_TRUNC
].emit
= build_tgsi_intrinsic_nomem
;
1165 bld_base
->op_actions
[TGSI_OPCODE_TRUNC
].intr_name
= "llvm.AMDGPU.trunc";
1167 bld_base
->rsq_action
.emit
= build_tgsi_intrinsic_nomem
;
1168 bld_base
->rsq_action
.intr_name
= "llvm.AMDGPU.rsq";
1171 void radeon_llvm_finalize_module(struct radeon_llvm_context
* ctx
)
1173 struct gallivm_state
* gallivm
= ctx
->soa
.bld_base
.base
.gallivm
;
1174 /* End the main function with Return*/
1175 LLVMBuildRetVoid(gallivm
->builder
);
1177 /* Create the pass manager */
1178 ctx
->gallivm
.passmgr
= LLVMCreateFunctionPassManagerForModule(
1181 /* This pass should eliminate all the load and store instructions */
1182 LLVMAddPromoteMemoryToRegisterPass(gallivm
->passmgr
);
1184 /* Add some optimization passes */
1185 LLVMAddScalarReplAggregatesPass(gallivm
->passmgr
);
1186 LLVMAddCFGSimplificationPass(gallivm
->passmgr
);
1189 LLVMRunFunctionPassManager(gallivm
->passmgr
, ctx
->main_fn
);
1191 LLVMDisposeBuilder(gallivm
->builder
);
1192 LLVMDisposePassManager(gallivm
->passmgr
);
1196 void radeon_llvm_dispose(struct radeon_llvm_context
* ctx
)
1198 LLVMDisposeModule(ctx
->soa
.bld_base
.base
.gallivm
->module
);
1199 LLVMContextDispose(ctx
->soa
.bld_base
.base
.gallivm
->context
);