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), "");
83 static struct tgsi_declaration_range
84 get_array_range(struct lp_build_tgsi_context
*bld_base
,
85 unsigned File
, const struct tgsi_ind_register
*reg
)
87 struct radeon_llvm_context
* ctx
= radeon_llvm_context(bld_base
);
89 if (File
!= TGSI_FILE_TEMPORARY
|| reg
->ArrayID
== 0 ||
90 reg
->ArrayID
> bld_base
->info
->array_max
[TGSI_FILE_TEMPORARY
]) {
91 struct tgsi_declaration_range range
;
93 range
.Last
= bld_base
->info
->file_max
[File
];
97 return ctx
->arrays
[reg
->ArrayID
- 1];
102 struct lp_build_tgsi_soa_context
*bld
,
103 const struct tgsi_ind_register
*reg
,
106 struct gallivm_state
* gallivm
= bld
->bld_base
.base
.gallivm
;
108 LLVMValueRef addr
= LLVMBuildLoad(gallivm
->builder
, bld
->addr
[reg
->Index
][reg
->Swizzle
], "");
109 return LLVMBuildAdd(gallivm
->builder
, addr
, lp_build_const_int32(gallivm
, offset
), "");
114 struct lp_build_tgsi_context
*bld_base
,
115 const struct tgsi_full_src_register
*reg
,
116 enum tgsi_opcode_type type
,
121 struct lp_build_tgsi_context
*bld_base
,
122 unsigned File
, enum tgsi_opcode_type type
,
123 struct tgsi_declaration_range range
,
126 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
127 struct gallivm_state
* gallivm
= bld
->bld_base
.base
.gallivm
;
128 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
130 unsigned i
, size
= range
.Last
- range
.First
+ 1;
131 LLVMTypeRef vec
= LLVMVectorType(tgsi2llvmtype(bld_base
, type
), size
);
132 LLVMValueRef result
= LLVMGetUndef(vec
);
134 struct tgsi_full_src_register tmp_reg
= {};
135 tmp_reg
.Register
.File
= File
;
137 for (i
= 0; i
< size
; ++i
) {
138 tmp_reg
.Register
.Index
= i
+ range
.First
;
139 LLVMValueRef temp
= emit_fetch(bld_base
, &tmp_reg
, type
, swizzle
);
140 result
= LLVMBuildInsertElement(builder
, result
, temp
,
141 lp_build_const_int32(gallivm
, i
), "");
146 static bool uses_temp_indirect_addressing(
147 struct lp_build_tgsi_context
*bld_base
)
149 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
150 return (bld
->indirect_files
& (1 << TGSI_FILE_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 radeon_llvm_context
* ctx
= radeon_llvm_context(bld_base
);
161 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
162 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
163 LLVMValueRef result
= NULL
, ptr
;
166 LLVMValueRef values
[TGSI_NUM_CHANNELS
];
168 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
169 values
[chan
] = emit_fetch(bld_base
, reg
, type
, chan
);
171 return lp_build_gather_values(bld_base
->base
.gallivm
, values
,
175 if (reg
->Register
.Indirect
) {
176 struct tgsi_declaration_range range
= get_array_range(bld_base
,
177 reg
->Register
.File
, ®
->Indirect
);
178 return LLVMBuildExtractElement(builder
,
179 emit_array_fetch(bld_base
, reg
->Register
.File
, type
, range
, swizzle
),
180 emit_array_index(bld
, ®
->Indirect
, reg
->Register
.Index
- range
.First
),
184 switch(reg
->Register
.File
) {
185 case TGSI_FILE_IMMEDIATE
: {
186 LLVMTypeRef ctype
= tgsi2llvmtype(bld_base
, type
);
187 return LLVMConstBitCast(bld
->immediates
[reg
->Register
.Index
][swizzle
], ctype
);
190 case TGSI_FILE_INPUT
:
191 result
= ctx
->inputs
[radeon_llvm_reg_index_soa(reg
->Register
.Index
, swizzle
)];
194 case TGSI_FILE_TEMPORARY
:
195 if (reg
->Register
.Index
>= ctx
->temps_count
)
196 return LLVMGetUndef(tgsi2llvmtype(bld_base
, type
));
197 if (uses_temp_indirect_addressing(bld_base
)) {
198 ptr
= lp_get_temp_ptr_soa(bld
, reg
->Register
.Index
, swizzle
);
201 ptr
= ctx
->temps
[reg
->Register
.Index
* TGSI_NUM_CHANNELS
+ swizzle
];
202 result
= LLVMBuildLoad(builder
, ptr
, "");
205 case TGSI_FILE_OUTPUT
:
206 ptr
= lp_get_output_ptr(bld
, reg
->Register
.Index
, swizzle
);
207 result
= LLVMBuildLoad(builder
, ptr
, "");
211 return LLVMGetUndef(tgsi2llvmtype(bld_base
, type
));
214 return bitcast(bld_base
, type
, result
);
217 static LLVMValueRef
fetch_system_value(
218 struct lp_build_tgsi_context
* bld_base
,
219 const struct tgsi_full_src_register
*reg
,
220 enum tgsi_opcode_type type
,
223 struct radeon_llvm_context
* ctx
= radeon_llvm_context(bld_base
);
224 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
226 LLVMValueRef cval
= ctx
->system_values
[reg
->Register
.Index
];
227 if (LLVMGetTypeKind(LLVMTypeOf(cval
)) == LLVMVectorTypeKind
) {
228 cval
= LLVMBuildExtractElement(gallivm
->builder
, cval
,
229 lp_build_const_int32(gallivm
, swizzle
), "");
231 return bitcast(bld_base
, type
, cval
);
234 static void emit_declaration(
235 struct lp_build_tgsi_context
* bld_base
,
236 const struct tgsi_full_declaration
*decl
)
238 struct radeon_llvm_context
* ctx
= radeon_llvm_context(bld_base
);
239 unsigned first
, last
, i
, idx
;
240 switch(decl
->Declaration
.File
) {
241 case TGSI_FILE_ADDRESS
:
244 for (idx
= decl
->Range
.First
; idx
<= decl
->Range
.Last
; idx
++) {
246 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
247 ctx
->soa
.addr
[idx
][chan
] = lp_build_alloca(
249 ctx
->soa
.bld_base
.uint_bld
.elem_type
, "");
255 case TGSI_FILE_TEMPORARY
:
256 if (decl
->Declaration
.Array
) {
258 int size
= bld_base
->info
->array_max
[TGSI_FILE_TEMPORARY
];
259 ctx
->arrays
= MALLOC(sizeof(ctx
->arrays
[0]) * size
);
262 ctx
->arrays
[decl
->Array
.ArrayID
- 1] = decl
->Range
;
264 if (uses_temp_indirect_addressing(bld_base
)) {
265 lp_emit_declaration_soa(bld_base
, decl
);
268 first
= decl
->Range
.First
;
269 last
= decl
->Range
.Last
;
270 if (!ctx
->temps_count
) {
271 ctx
->temps_count
= bld_base
->info
->file_max
[TGSI_FILE_TEMPORARY
] + 1;
272 ctx
->temps
= MALLOC(TGSI_NUM_CHANNELS
* ctx
->temps_count
* sizeof(LLVMValueRef
));
274 for (idx
= first
; idx
<= last
; idx
++) {
275 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++) {
276 ctx
->temps
[idx
* TGSI_NUM_CHANNELS
+ i
] =
277 lp_build_alloca(bld_base
->base
.gallivm
, bld_base
->base
.vec_type
,
283 case TGSI_FILE_INPUT
:
286 for (idx
= decl
->Range
.First
; idx
<= decl
->Range
.Last
; idx
++) {
288 ctx
->load_input(ctx
, idx
, decl
);
293 case TGSI_FILE_SYSTEM_VALUE
:
296 for (idx
= decl
->Range
.First
; idx
<= decl
->Range
.Last
; idx
++) {
297 ctx
->load_system_value(ctx
, idx
, decl
);
302 case TGSI_FILE_OUTPUT
:
305 for (idx
= decl
->Range
.First
; idx
<= decl
->Range
.Last
; idx
++) {
307 assert(idx
< RADEON_LLVM_MAX_OUTPUTS
);
308 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
309 ctx
->soa
.outputs
[idx
][chan
] = lp_build_alloca(&ctx
->gallivm
,
310 ctx
->soa
.bld_base
.base
.elem_type
, "");
314 ctx
->output_reg_count
= MAX2(ctx
->output_reg_count
,
315 decl
->Range
.Last
+ 1);
324 static LLVMValueRef
radeon_llvm_saturate(struct lp_build_tgsi_context
*bld_base
,
327 struct lp_build_emit_data clamp_emit_data
;
329 memset(&clamp_emit_data
, 0, sizeof(clamp_emit_data
));
330 clamp_emit_data
.arg_count
= 3;
331 clamp_emit_data
.args
[0] = value
;
332 clamp_emit_data
.args
[2] = bld_base
->base
.one
;
333 clamp_emit_data
.args
[1] = bld_base
->base
.zero
;
335 return lp_build_emit_llvm(bld_base
, TGSI_OPCODE_CLAMP
,
341 struct lp_build_tgsi_context
* bld_base
,
342 const struct tgsi_full_instruction
* inst
,
343 const struct tgsi_opcode_info
* info
,
346 struct radeon_llvm_context
* ctx
= radeon_llvm_context(bld_base
);
347 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
348 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
349 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[0];
350 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
351 LLVMValueRef temp_ptr
;
352 unsigned chan
, chan_index
;
353 boolean is_vec_store
= FALSE
;
356 LLVMTypeKind k
= LLVMGetTypeKind(LLVMTypeOf(dst
[0]));
357 is_vec_store
= (k
== LLVMVectorTypeKind
);
361 LLVMValueRef values
[4] = {};
362 TGSI_FOR_EACH_DST0_ENABLED_CHANNEL(inst
, chan
) {
363 LLVMValueRef index
= lp_build_const_int32(gallivm
, chan
);
364 values
[chan
] = LLVMBuildExtractElement(gallivm
->builder
,
367 bld_base
->emit_store(bld_base
, inst
, info
, values
);
371 TGSI_FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
372 LLVMValueRef value
= dst
[chan_index
];
374 if (inst
->Instruction
.Saturate
)
375 value
= radeon_llvm_saturate(bld_base
, value
);
377 if (reg
->Register
.File
== TGSI_FILE_ADDRESS
) {
378 temp_ptr
= bld
->addr
[reg
->Register
.Index
][chan_index
];
379 LLVMBuildStore(builder
, value
, temp_ptr
);
383 value
= bitcast(bld_base
, TGSI_TYPE_FLOAT
, value
);
385 if (reg
->Register
.Indirect
) {
386 struct tgsi_declaration_range range
= get_array_range(bld_base
,
387 reg
->Register
.File
, ®
->Indirect
);
389 unsigned i
, size
= range
.Last
- range
.First
+ 1;
390 LLVMValueRef array
= LLVMBuildInsertElement(builder
,
391 emit_array_fetch(bld_base
, reg
->Register
.File
, TGSI_TYPE_FLOAT
, range
, chan_index
),
392 value
, emit_array_index(bld
, ®
->Indirect
, reg
->Register
.Index
- range
.First
), "");
394 for (i
= 0; i
< size
; ++i
) {
395 switch(reg
->Register
.File
) {
396 case TGSI_FILE_OUTPUT
:
397 temp_ptr
= bld
->outputs
[i
+ range
.First
][chan_index
];
400 case TGSI_FILE_TEMPORARY
:
401 if (range
.First
+ i
>= ctx
->temps_count
)
403 if (uses_temp_indirect_addressing(bld_base
))
404 temp_ptr
= lp_get_temp_ptr_soa(bld
, i
+ range
.First
, chan_index
);
406 temp_ptr
= ctx
->temps
[(i
+ range
.First
) * TGSI_NUM_CHANNELS
+ chan_index
];
412 value
= LLVMBuildExtractElement(builder
, array
,
413 lp_build_const_int32(gallivm
, i
), "");
414 LLVMBuildStore(builder
, value
, temp_ptr
);
418 switch(reg
->Register
.File
) {
419 case TGSI_FILE_OUTPUT
:
420 temp_ptr
= bld
->outputs
[reg
->Register
.Index
][chan_index
];
423 case TGSI_FILE_TEMPORARY
:
424 if (reg
->Register
.Index
>= ctx
->temps_count
)
426 if (uses_temp_indirect_addressing(bld_base
)) {
430 temp_ptr
= ctx
->temps
[ TGSI_NUM_CHANNELS
* reg
->Register
.Index
+ chan_index
];
436 LLVMBuildStore(builder
, value
, temp_ptr
);
441 static void bgnloop_emit(
442 const struct lp_build_tgsi_action
* action
,
443 struct lp_build_tgsi_context
* bld_base
,
444 struct lp_build_emit_data
* emit_data
)
446 struct radeon_llvm_context
* ctx
= radeon_llvm_context(bld_base
);
447 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
448 LLVMBasicBlockRef loop_block
;
449 LLVMBasicBlockRef endloop_block
;
450 endloop_block
= LLVMAppendBasicBlockInContext(gallivm
->context
,
451 ctx
->main_fn
, "ENDLOOP");
452 loop_block
= LLVMInsertBasicBlockInContext(gallivm
->context
,
453 endloop_block
, "LOOP");
454 LLVMBuildBr(gallivm
->builder
, loop_block
);
455 LLVMPositionBuilderAtEnd(gallivm
->builder
, loop_block
);
457 if (++ctx
->loop_depth
> ctx
->loop_depth_max
) {
458 unsigned new_max
= ctx
->loop_depth_max
<< 1;
461 new_max
= RADEON_LLVM_INITIAL_CF_DEPTH
;
463 ctx
->loop
= REALLOC(ctx
->loop
, ctx
->loop_depth_max
*
464 sizeof(ctx
->loop
[0]),
465 new_max
* sizeof(ctx
->loop
[0]));
466 ctx
->loop_depth_max
= new_max
;
469 ctx
->loop
[ctx
->loop_depth
- 1].loop_block
= loop_block
;
470 ctx
->loop
[ctx
->loop_depth
- 1].endloop_block
= endloop_block
;
473 static void brk_emit(
474 const struct lp_build_tgsi_action
* action
,
475 struct lp_build_tgsi_context
* bld_base
,
476 struct lp_build_emit_data
* emit_data
)
478 struct radeon_llvm_context
* ctx
= radeon_llvm_context(bld_base
);
479 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
480 struct radeon_llvm_loop
* current_loop
= get_current_loop(ctx
);
482 LLVMBuildBr(gallivm
->builder
, current_loop
->endloop_block
);
485 static void cont_emit(
486 const struct lp_build_tgsi_action
* action
,
487 struct lp_build_tgsi_context
* bld_base
,
488 struct lp_build_emit_data
* emit_data
)
490 struct radeon_llvm_context
* ctx
= radeon_llvm_context(bld_base
);
491 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
492 struct radeon_llvm_loop
* current_loop
= get_current_loop(ctx
);
494 LLVMBuildBr(gallivm
->builder
, current_loop
->loop_block
);
497 static void else_emit(
498 const struct lp_build_tgsi_action
* action
,
499 struct lp_build_tgsi_context
* bld_base
,
500 struct lp_build_emit_data
* emit_data
)
502 struct radeon_llvm_context
* ctx
= radeon_llvm_context(bld_base
);
503 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
504 struct radeon_llvm_branch
* current_branch
= get_current_branch(ctx
);
505 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(gallivm
->builder
);
507 /* We need to add a terminator to the current block if the previous
508 * instruction was an ENDIF.Example:
521 if (current_block
!= current_branch
->if_block
) {
522 LLVMBuildBr(gallivm
->builder
, current_branch
->endif_block
);
524 if (!LLVMGetBasicBlockTerminator(current_branch
->if_block
)) {
525 LLVMBuildBr(gallivm
->builder
, current_branch
->endif_block
);
527 current_branch
->has_else
= 1;
528 LLVMPositionBuilderAtEnd(gallivm
->builder
, current_branch
->else_block
);
531 static void endif_emit(
532 const struct lp_build_tgsi_action
* action
,
533 struct lp_build_tgsi_context
* bld_base
,
534 struct lp_build_emit_data
* emit_data
)
536 struct radeon_llvm_context
* ctx
= radeon_llvm_context(bld_base
);
537 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
538 struct radeon_llvm_branch
* current_branch
= get_current_branch(ctx
);
539 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(gallivm
->builder
);
541 /* If we have consecutive ENDIF instructions, then the first ENDIF
542 * will not have a terminator, so we need to add one. */
543 if (current_block
!= current_branch
->if_block
544 && current_block
!= current_branch
->else_block
545 && !LLVMGetBasicBlockTerminator(current_block
)) {
547 LLVMBuildBr(gallivm
->builder
, current_branch
->endif_block
);
549 if (!LLVMGetBasicBlockTerminator(current_branch
->else_block
)) {
550 LLVMPositionBuilderAtEnd(gallivm
->builder
, current_branch
->else_block
);
551 LLVMBuildBr(gallivm
->builder
, current_branch
->endif_block
);
554 if (!LLVMGetBasicBlockTerminator(current_branch
->if_block
)) {
555 LLVMPositionBuilderAtEnd(gallivm
->builder
, current_branch
->if_block
);
556 LLVMBuildBr(gallivm
->builder
, current_branch
->endif_block
);
559 LLVMPositionBuilderAtEnd(gallivm
->builder
, current_branch
->endif_block
);
563 static void endloop_emit(
564 const struct lp_build_tgsi_action
* action
,
565 struct lp_build_tgsi_context
* bld_base
,
566 struct lp_build_emit_data
* emit_data
)
568 struct radeon_llvm_context
* ctx
= radeon_llvm_context(bld_base
);
569 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
570 struct radeon_llvm_loop
* current_loop
= get_current_loop(ctx
);
572 if (!LLVMGetBasicBlockTerminator(LLVMGetInsertBlock(gallivm
->builder
))) {
573 LLVMBuildBr(gallivm
->builder
, current_loop
->loop_block
);
576 LLVMPositionBuilderAtEnd(gallivm
->builder
, current_loop
->endloop_block
);
580 static void if_cond_emit(
581 const struct lp_build_tgsi_action
* action
,
582 struct lp_build_tgsi_context
* bld_base
,
583 struct lp_build_emit_data
* emit_data
,
586 struct radeon_llvm_context
* ctx
= radeon_llvm_context(bld_base
);
587 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
588 LLVMBasicBlockRef if_block
, else_block
, endif_block
;
590 endif_block
= LLVMAppendBasicBlockInContext(gallivm
->context
,
591 ctx
->main_fn
, "ENDIF");
592 if_block
= LLVMInsertBasicBlockInContext(gallivm
->context
,
594 else_block
= LLVMInsertBasicBlockInContext(gallivm
->context
,
595 endif_block
, "ELSE");
596 LLVMBuildCondBr(gallivm
->builder
, cond
, if_block
, else_block
);
597 LLVMPositionBuilderAtEnd(gallivm
->builder
, if_block
);
599 if (++ctx
->branch_depth
> ctx
->branch_depth_max
) {
600 unsigned new_max
= ctx
->branch_depth_max
<< 1;
603 new_max
= RADEON_LLVM_INITIAL_CF_DEPTH
;
605 ctx
->branch
= REALLOC(ctx
->branch
, ctx
->branch_depth_max
*
606 sizeof(ctx
->branch
[0]),
607 new_max
* sizeof(ctx
->branch
[0]));
608 ctx
->branch_depth_max
= new_max
;
611 ctx
->branch
[ctx
->branch_depth
- 1].endif_block
= endif_block
;
612 ctx
->branch
[ctx
->branch_depth
- 1].if_block
= if_block
;
613 ctx
->branch
[ctx
->branch_depth
- 1].else_block
= else_block
;
614 ctx
->branch
[ctx
->branch_depth
- 1].has_else
= 0;
618 const struct lp_build_tgsi_action
* action
,
619 struct lp_build_tgsi_context
* bld_base
,
620 struct lp_build_emit_data
* emit_data
)
622 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
625 cond
= LLVMBuildFCmp(gallivm
->builder
, LLVMRealUNE
,
627 bld_base
->base
.zero
, "");
629 if_cond_emit(action
, bld_base
, emit_data
, cond
);
632 static void uif_emit(
633 const struct lp_build_tgsi_action
* action
,
634 struct lp_build_tgsi_context
* bld_base
,
635 struct lp_build_emit_data
* emit_data
)
637 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
640 cond
= LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
,
641 bitcast(bld_base
, TGSI_TYPE_UNSIGNED
, emit_data
->args
[0]),
642 bld_base
->int_bld
.zero
, "");
644 if_cond_emit(action
, bld_base
, emit_data
, cond
);
647 static void kill_if_fetch_args(
648 struct lp_build_tgsi_context
* bld_base
,
649 struct lp_build_emit_data
* emit_data
)
651 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
652 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
653 LLVMBuilderRef builder
= gallivm
->builder
;
655 LLVMValueRef conds
[TGSI_NUM_CHANNELS
];
657 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++) {
658 LLVMValueRef value
= lp_build_emit_fetch(bld_base
, inst
, 0, i
);
659 conds
[i
] = LLVMBuildFCmp(builder
, LLVMRealOLT
, value
,
660 bld_base
->base
.zero
, "");
663 /* Or the conditions together */
664 for (i
= TGSI_NUM_CHANNELS
- 1; i
> 0; i
--) {
665 conds
[i
- 1] = LLVMBuildOr(builder
, conds
[i
], conds
[i
- 1], "");
668 emit_data
->dst_type
= LLVMVoidTypeInContext(gallivm
->context
);
669 emit_data
->arg_count
= 1;
670 emit_data
->args
[0] = LLVMBuildSelect(builder
, conds
[0],
671 lp_build_const_float(gallivm
, -1.0f
),
672 bld_base
->base
.zero
, "");
675 static void kil_emit(
676 const struct lp_build_tgsi_action
* action
,
677 struct lp_build_tgsi_context
* bld_base
,
678 struct lp_build_emit_data
* emit_data
)
681 for (i
= 0; i
< emit_data
->arg_count
; i
++) {
682 emit_data
->output
[i
] = lp_build_intrinsic_unary(
683 bld_base
->base
.gallivm
->builder
,
685 emit_data
->dst_type
, emit_data
->args
[i
]);
689 void radeon_llvm_emit_prepare_cube_coords(
690 struct lp_build_tgsi_context
* bld_base
,
691 struct lp_build_emit_data
* emit_data
,
692 LLVMValueRef
*coords_arg
)
695 unsigned target
= emit_data
->inst
->Texture
.Texture
;
696 unsigned opcode
= emit_data
->inst
->Instruction
.Opcode
;
697 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
698 LLVMBuilderRef builder
= gallivm
->builder
;
699 LLVMTypeRef type
= bld_base
->base
.elem_type
;
700 LLVMValueRef coords
[4];
701 LLVMValueRef mad_args
[3];
703 struct LLVMOpaqueValue
*cube_vec
;
707 cube_vec
= lp_build_gather_values(bld_base
->base
.gallivm
, coords_arg
, 4);
708 v
= build_intrinsic(builder
, "llvm.AMDGPU.cube", LLVMVectorType(type
, 4),
709 &cube_vec
, 1, LLVMReadNoneAttribute
);
711 for (i
= 0; i
< 4; ++i
) {
712 idx
= lp_build_const_int32(gallivm
, i
);
713 coords
[i
] = LLVMBuildExtractElement(builder
, v
, idx
, "");
716 coords
[2] = build_intrinsic(builder
, "fabs",
717 type
, &coords
[2], 1, LLVMReadNoneAttribute
);
718 coords
[2] = lp_build_emit_llvm_unary(bld_base
, TGSI_OPCODE_RCP
, coords
[2]);
720 mad_args
[1] = coords
[2];
721 mad_args
[2] = LLVMConstReal(type
, 1.5);
723 mad_args
[0] = coords
[0];
724 coords
[0] = lp_build_emit_llvm_ternary(bld_base
, TGSI_OPCODE_MAD
,
725 mad_args
[0], mad_args
[1], mad_args
[2]);
727 mad_args
[0] = coords
[1];
728 coords
[1] = lp_build_emit_llvm_ternary(bld_base
, TGSI_OPCODE_MAD
,
729 mad_args
[0], mad_args
[1], mad_args
[2]);
731 /* apply xyz = yxw swizzle to cooords */
732 coords
[2] = coords
[3];
733 coords
[3] = coords
[1];
734 coords
[1] = coords
[0];
735 coords
[0] = coords
[3];
737 if (target
== TGSI_TEXTURE_CUBE_ARRAY
||
738 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
) {
739 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
740 /* coords_arg.w component - array_index for cube arrays */
741 coords
[2] = lp_build_emit_llvm_ternary(bld_base
, TGSI_OPCODE_MAD
,
742 coords_arg
[3], lp_build_const_float(gallivm
, 8.0), coords
[2]);
745 /* Preserve compare/lod/bias. Put it in coords.w. */
746 if (opcode
== TGSI_OPCODE_TEX2
||
747 opcode
== TGSI_OPCODE_TXB2
||
748 opcode
== TGSI_OPCODE_TXL2
) {
749 coords
[3] = coords_arg
[4];
750 } else if (opcode
== TGSI_OPCODE_TXB
||
751 opcode
== TGSI_OPCODE_TXL
||
752 target
== TGSI_TEXTURE_SHADOWCUBE
) {
753 coords
[3] = coords_arg
[3];
756 memcpy(coords_arg
, coords
, sizeof(coords
));
759 static void txd_fetch_args(
760 struct lp_build_tgsi_context
* bld_base
,
761 struct lp_build_emit_data
* emit_data
)
763 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
765 LLVMValueRef coords
[4];
767 for (src
= 0; src
< 3; src
++) {
768 for (chan
= 0; chan
< 4; chan
++)
769 coords
[chan
] = lp_build_emit_fetch(bld_base
, inst
, src
, chan
);
771 emit_data
->args
[src
] = lp_build_gather_values(bld_base
->base
.gallivm
,
774 emit_data
->arg_count
= 3;
775 emit_data
->dst_type
= LLVMVectorType(bld_base
->base
.elem_type
, 4);
779 static void txp_fetch_args(
780 struct lp_build_tgsi_context
* bld_base
,
781 struct lp_build_emit_data
* emit_data
)
783 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
786 LLVMValueRef coords
[5];
788 emit_data
->dst_type
= LLVMVectorType(bld_base
->base
.elem_type
, 4);
789 src_w
= lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_W
);
791 for (chan
= 0; chan
< 3; chan
++ ) {
792 LLVMValueRef arg
= lp_build_emit_fetch(bld_base
,
793 emit_data
->inst
, 0, chan
);
794 coords
[chan
] = lp_build_emit_llvm_binary(bld_base
,
795 TGSI_OPCODE_DIV
, arg
, src_w
);
797 coords
[3] = bld_base
->base
.one
;
799 if ((inst
->Texture
.Texture
== TGSI_TEXTURE_CUBE
||
800 inst
->Texture
.Texture
== TGSI_TEXTURE_CUBE_ARRAY
||
801 inst
->Texture
.Texture
== TGSI_TEXTURE_SHADOWCUBE
||
802 inst
->Texture
.Texture
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
) &&
803 inst
->Instruction
.Opcode
!= TGSI_OPCODE_TXQ
&&
804 inst
->Instruction
.Opcode
!= TGSI_OPCODE_TXQ_LZ
) {
805 radeon_llvm_emit_prepare_cube_coords(bld_base
, emit_data
, coords
);
808 emit_data
->args
[0] = lp_build_gather_values(bld_base
->base
.gallivm
,
810 emit_data
->arg_count
= 1;
813 static void tex_fetch_args(
814 struct lp_build_tgsi_context
* bld_base
,
815 struct lp_build_emit_data
* emit_data
)
817 /* XXX: lp_build_swizzle_aos() was failing with wrong arg types,
818 * when we used CHAN_ALL. We should be able to get this to work,
819 * but for now we will swizzle it ourselves
820 emit_data->args[0] = lp_build_emit_fetch(bld_base, emit_data->inst,
825 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
827 LLVMValueRef coords
[5];
829 for (chan
= 0; chan
< 4; chan
++) {
830 coords
[chan
] = lp_build_emit_fetch(bld_base
, inst
, 0, chan
);
833 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_TEX2
||
834 inst
->Instruction
.Opcode
== TGSI_OPCODE_TXB2
||
835 inst
->Instruction
.Opcode
== TGSI_OPCODE_TXL2
) {
836 /* These instructions have additional operand that should be packed
837 * into the cube coord vector by radeon_llvm_emit_prepare_cube_coords.
838 * That operand should be passed as a float value in the args array
839 * right after the coord vector. After packing it's not used anymore,
840 * that's why arg_count is not increased */
841 coords
[4] = lp_build_emit_fetch(bld_base
, inst
, 1, 0);
844 if ((inst
->Texture
.Texture
== TGSI_TEXTURE_CUBE
||
845 inst
->Texture
.Texture
== TGSI_TEXTURE_CUBE_ARRAY
||
846 inst
->Texture
.Texture
== TGSI_TEXTURE_SHADOWCUBE
||
847 inst
->Texture
.Texture
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
) &&
848 inst
->Instruction
.Opcode
!= TGSI_OPCODE_TXQ
&&
849 inst
->Instruction
.Opcode
!= TGSI_OPCODE_TXQ_LZ
) {
850 radeon_llvm_emit_prepare_cube_coords(bld_base
, emit_data
, coords
);
853 emit_data
->arg_count
= 1;
854 emit_data
->args
[0] = lp_build_gather_values(bld_base
->base
.gallivm
,
856 emit_data
->dst_type
= LLVMVectorType(bld_base
->base
.elem_type
, 4);
859 static void txf_fetch_args(
860 struct lp_build_tgsi_context
* bld_base
,
861 struct lp_build_emit_data
* emit_data
)
863 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
864 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
865 const struct tgsi_texture_offset
* off
= inst
->TexOffsets
;
866 LLVMTypeRef offset_type
= bld_base
->int_bld
.elem_type
;
868 /* fetch tex coords */
869 tex_fetch_args(bld_base
, emit_data
);
871 /* fetch tex offsets */
872 if (inst
->Texture
.NumOffsets
) {
873 assert(inst
->Texture
.NumOffsets
== 1);
875 emit_data
->args
[1] = LLVMConstBitCast(
876 bld
->immediates
[off
->Index
][off
->SwizzleX
],
878 emit_data
->args
[2] = LLVMConstBitCast(
879 bld
->immediates
[off
->Index
][off
->SwizzleY
],
881 emit_data
->args
[3] = LLVMConstBitCast(
882 bld
->immediates
[off
->Index
][off
->SwizzleZ
],
885 emit_data
->args
[1] = bld_base
->int_bld
.zero
;
886 emit_data
->args
[2] = bld_base
->int_bld
.zero
;
887 emit_data
->args
[3] = bld_base
->int_bld
.zero
;
890 emit_data
->arg_count
= 4;
893 static void emit_icmp(
894 const struct lp_build_tgsi_action
* action
,
895 struct lp_build_tgsi_context
* bld_base
,
896 struct lp_build_emit_data
* emit_data
)
899 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
900 LLVMContextRef context
= bld_base
->base
.gallivm
->context
;
902 switch (emit_data
->inst
->Instruction
.Opcode
) {
903 case TGSI_OPCODE_USEQ
: pred
= LLVMIntEQ
; break;
904 case TGSI_OPCODE_USNE
: pred
= LLVMIntNE
; break;
905 case TGSI_OPCODE_USGE
: pred
= LLVMIntUGE
; break;
906 case TGSI_OPCODE_USLT
: pred
= LLVMIntULT
; break;
907 case TGSI_OPCODE_ISGE
: pred
= LLVMIntSGE
; break;
908 case TGSI_OPCODE_ISLT
: pred
= LLVMIntSLT
; break;
910 assert(!"unknown instruction");
915 LLVMValueRef v
= LLVMBuildICmp(builder
, pred
,
916 emit_data
->args
[0], emit_data
->args
[1],"");
918 v
= LLVMBuildSExtOrBitCast(builder
, v
,
919 LLVMInt32TypeInContext(context
), "");
921 emit_data
->output
[emit_data
->chan
] = v
;
924 static void emit_ucmp(
925 const struct lp_build_tgsi_action
* action
,
926 struct lp_build_tgsi_context
* bld_base
,
927 struct lp_build_emit_data
* emit_data
)
929 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
931 LLVMValueRef arg0
= LLVMBuildBitCast(builder
, emit_data
->args
[0],
932 bld_base
->uint_bld
.elem_type
, "");
934 LLVMValueRef v
= LLVMBuildICmp(builder
, LLVMIntNE
, arg0
,
935 bld_base
->uint_bld
.zero
, "");
937 emit_data
->output
[emit_data
->chan
] =
938 LLVMBuildSelect(builder
, v
, emit_data
->args
[1], emit_data
->args
[2], "");
941 static void emit_cmp(
942 const struct lp_build_tgsi_action
*action
,
943 struct lp_build_tgsi_context
* bld_base
,
944 struct lp_build_emit_data
* emit_data
)
946 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
947 LLVMRealPredicate pred
;
950 /* Use ordered for everything but NE (which is usual for
953 switch (emit_data
->inst
->Instruction
.Opcode
) {
954 case TGSI_OPCODE_SGE
: pred
= LLVMRealOGE
; break;
955 case TGSI_OPCODE_SEQ
: pred
= LLVMRealOEQ
; break;
956 case TGSI_OPCODE_SLE
: pred
= LLVMRealOLE
; break;
957 case TGSI_OPCODE_SLT
: pred
= LLVMRealOLT
; break;
958 case TGSI_OPCODE_SNE
: pred
= LLVMRealUNE
; break;
959 case TGSI_OPCODE_SGT
: pred
= LLVMRealOGT
; break;
960 default: assert(!"unknown instruction"); pred
= 0; break;
963 cond
= LLVMBuildFCmp(builder
,
964 pred
, emit_data
->args
[0], emit_data
->args
[1], "");
966 emit_data
->output
[emit_data
->chan
] = LLVMBuildSelect(builder
,
967 cond
, bld_base
->base
.one
, bld_base
->base
.zero
, "");
970 static void emit_fcmp(
971 const struct lp_build_tgsi_action
*action
,
972 struct lp_build_tgsi_context
* bld_base
,
973 struct lp_build_emit_data
* emit_data
)
975 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
976 LLVMContextRef context
= bld_base
->base
.gallivm
->context
;
977 LLVMRealPredicate pred
;
979 /* Use ordered for everything but NE (which is usual for
982 switch (emit_data
->inst
->Instruction
.Opcode
) {
983 case TGSI_OPCODE_FSEQ
: pred
= LLVMRealOEQ
; break;
984 case TGSI_OPCODE_FSGE
: pred
= LLVMRealOGE
; break;
985 case TGSI_OPCODE_FSLT
: pred
= LLVMRealOLT
; break;
986 case TGSI_OPCODE_FSNE
: pred
= LLVMRealUNE
; break;
987 default: assert(!"unknown instruction"); pred
= 0; break;
990 LLVMValueRef v
= LLVMBuildFCmp(builder
, pred
,
991 emit_data
->args
[0], emit_data
->args
[1],"");
993 v
= LLVMBuildSExtOrBitCast(builder
, v
,
994 LLVMInt32TypeInContext(context
), "");
996 emit_data
->output
[emit_data
->chan
] = v
;
999 static void emit_not(
1000 const struct lp_build_tgsi_action
* action
,
1001 struct lp_build_tgsi_context
* bld_base
,
1002 struct lp_build_emit_data
* emit_data
)
1004 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1005 LLVMValueRef v
= bitcast(bld_base
, TGSI_TYPE_UNSIGNED
,
1006 emit_data
->args
[0]);
1007 emit_data
->output
[emit_data
->chan
] = LLVMBuildNot(builder
, v
, "");
1010 static void emit_arl(
1011 const struct lp_build_tgsi_action
* action
,
1012 struct lp_build_tgsi_context
* bld_base
,
1013 struct lp_build_emit_data
* emit_data
)
1015 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1016 LLVMValueRef floor_index
= lp_build_emit_llvm_unary(bld_base
, TGSI_OPCODE_FLR
, emit_data
->args
[0]);
1017 emit_data
->output
[emit_data
->chan
] = LLVMBuildFPToSI(builder
,
1018 floor_index
, bld_base
->base
.int_elem_type
, "");
1021 static void emit_and(
1022 const struct lp_build_tgsi_action
* action
,
1023 struct lp_build_tgsi_context
* bld_base
,
1024 struct lp_build_emit_data
* emit_data
)
1026 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1027 emit_data
->output
[emit_data
->chan
] = LLVMBuildAnd(builder
,
1028 emit_data
->args
[0], emit_data
->args
[1], "");
1031 static void emit_or(
1032 const struct lp_build_tgsi_action
* action
,
1033 struct lp_build_tgsi_context
* bld_base
,
1034 struct lp_build_emit_data
* emit_data
)
1036 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1037 emit_data
->output
[emit_data
->chan
] = LLVMBuildOr(builder
,
1038 emit_data
->args
[0], emit_data
->args
[1], "");
1041 static void emit_uadd(
1042 const struct lp_build_tgsi_action
* action
,
1043 struct lp_build_tgsi_context
* bld_base
,
1044 struct lp_build_emit_data
* emit_data
)
1046 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1047 emit_data
->output
[emit_data
->chan
] = LLVMBuildAdd(builder
,
1048 emit_data
->args
[0], emit_data
->args
[1], "");
1051 static void emit_udiv(
1052 const struct lp_build_tgsi_action
* action
,
1053 struct lp_build_tgsi_context
* bld_base
,
1054 struct lp_build_emit_data
* emit_data
)
1056 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1057 emit_data
->output
[emit_data
->chan
] = LLVMBuildUDiv(builder
,
1058 emit_data
->args
[0], emit_data
->args
[1], "");
1061 static void emit_idiv(
1062 const struct lp_build_tgsi_action
* action
,
1063 struct lp_build_tgsi_context
* bld_base
,
1064 struct lp_build_emit_data
* emit_data
)
1066 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1067 emit_data
->output
[emit_data
->chan
] = LLVMBuildSDiv(builder
,
1068 emit_data
->args
[0], emit_data
->args
[1], "");
1071 static void emit_mod(
1072 const struct lp_build_tgsi_action
* action
,
1073 struct lp_build_tgsi_context
* bld_base
,
1074 struct lp_build_emit_data
* emit_data
)
1076 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1077 emit_data
->output
[emit_data
->chan
] = LLVMBuildSRem(builder
,
1078 emit_data
->args
[0], emit_data
->args
[1], "");
1081 static void emit_umod(
1082 const struct lp_build_tgsi_action
* action
,
1083 struct lp_build_tgsi_context
* bld_base
,
1084 struct lp_build_emit_data
* emit_data
)
1086 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1087 emit_data
->output
[emit_data
->chan
] = LLVMBuildURem(builder
,
1088 emit_data
->args
[0], emit_data
->args
[1], "");
1091 static void emit_shl(
1092 const struct lp_build_tgsi_action
* action
,
1093 struct lp_build_tgsi_context
* bld_base
,
1094 struct lp_build_emit_data
* emit_data
)
1096 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1097 emit_data
->output
[emit_data
->chan
] = LLVMBuildShl(builder
,
1098 emit_data
->args
[0], emit_data
->args
[1], "");
1101 static void emit_ushr(
1102 const struct lp_build_tgsi_action
* action
,
1103 struct lp_build_tgsi_context
* bld_base
,
1104 struct lp_build_emit_data
* emit_data
)
1106 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1107 emit_data
->output
[emit_data
->chan
] = LLVMBuildLShr(builder
,
1108 emit_data
->args
[0], emit_data
->args
[1], "");
1110 static void emit_ishr(
1111 const struct lp_build_tgsi_action
* action
,
1112 struct lp_build_tgsi_context
* bld_base
,
1113 struct lp_build_emit_data
* emit_data
)
1115 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1116 emit_data
->output
[emit_data
->chan
] = LLVMBuildAShr(builder
,
1117 emit_data
->args
[0], emit_data
->args
[1], "");
1120 static void emit_xor(
1121 const struct lp_build_tgsi_action
* action
,
1122 struct lp_build_tgsi_context
* bld_base
,
1123 struct lp_build_emit_data
* emit_data
)
1125 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1126 emit_data
->output
[emit_data
->chan
] = LLVMBuildXor(builder
,
1127 emit_data
->args
[0], emit_data
->args
[1], "");
1130 static void emit_ssg(
1131 const struct lp_build_tgsi_action
* action
,
1132 struct lp_build_tgsi_context
* bld_base
,
1133 struct lp_build_emit_data
* emit_data
)
1135 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1137 LLVMValueRef cmp
, val
;
1139 if (emit_data
->inst
->Instruction
.Opcode
== TGSI_OPCODE_ISSG
) {
1140 cmp
= LLVMBuildICmp(builder
, LLVMIntSGT
, emit_data
->args
[0], bld_base
->int_bld
.zero
, "");
1141 val
= LLVMBuildSelect(builder
, cmp
, bld_base
->int_bld
.one
, emit_data
->args
[0], "");
1142 cmp
= LLVMBuildICmp(builder
, LLVMIntSGE
, val
, bld_base
->int_bld
.zero
, "");
1143 val
= LLVMBuildSelect(builder
, cmp
, val
, LLVMConstInt(bld_base
->int_bld
.elem_type
, -1, true), "");
1144 } else { // float SSG
1145 cmp
= LLVMBuildFCmp(builder
, LLVMRealOGT
, emit_data
->args
[0], bld_base
->base
.zero
, "");
1146 val
= LLVMBuildSelect(builder
, cmp
, bld_base
->base
.one
, emit_data
->args
[0], "");
1147 cmp
= LLVMBuildFCmp(builder
, LLVMRealOGE
, val
, bld_base
->base
.zero
, "");
1148 val
= LLVMBuildSelect(builder
, cmp
, val
, LLVMConstReal(bld_base
->base
.elem_type
, -1), "");
1151 emit_data
->output
[emit_data
->chan
] = val
;
1154 static void emit_ineg(
1155 const struct lp_build_tgsi_action
* action
,
1156 struct lp_build_tgsi_context
* bld_base
,
1157 struct lp_build_emit_data
* emit_data
)
1159 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1160 emit_data
->output
[emit_data
->chan
] = LLVMBuildNeg(builder
,
1161 emit_data
->args
[0], "");
1164 static void emit_f2i(
1165 const struct lp_build_tgsi_action
* action
,
1166 struct lp_build_tgsi_context
* bld_base
,
1167 struct lp_build_emit_data
* emit_data
)
1169 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1170 emit_data
->output
[emit_data
->chan
] = LLVMBuildFPToSI(builder
,
1171 emit_data
->args
[0], bld_base
->int_bld
.elem_type
, "");
1174 static void emit_f2u(
1175 const struct lp_build_tgsi_action
* action
,
1176 struct lp_build_tgsi_context
* bld_base
,
1177 struct lp_build_emit_data
* emit_data
)
1179 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1180 emit_data
->output
[emit_data
->chan
] = LLVMBuildFPToUI(builder
,
1181 emit_data
->args
[0], bld_base
->uint_bld
.elem_type
, "");
1184 static void emit_i2f(
1185 const struct lp_build_tgsi_action
* action
,
1186 struct lp_build_tgsi_context
* bld_base
,
1187 struct lp_build_emit_data
* emit_data
)
1189 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1190 emit_data
->output
[emit_data
->chan
] = LLVMBuildSIToFP(builder
,
1191 emit_data
->args
[0], bld_base
->base
.elem_type
, "");
1194 static void emit_u2f(
1195 const struct lp_build_tgsi_action
* action
,
1196 struct lp_build_tgsi_context
* bld_base
,
1197 struct lp_build_emit_data
* emit_data
)
1199 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1200 emit_data
->output
[emit_data
->chan
] = LLVMBuildUIToFP(builder
,
1201 emit_data
->args
[0], bld_base
->base
.elem_type
, "");
1204 static void emit_immediate(struct lp_build_tgsi_context
* bld_base
,
1205 const struct tgsi_full_immediate
*imm
)
1208 struct radeon_llvm_context
* ctx
= radeon_llvm_context(bld_base
);
1210 for (i
= 0; i
< 4; ++i
) {
1211 ctx
->soa
.immediates
[ctx
->soa
.num_immediates
][i
] =
1212 LLVMConstInt(bld_base
->uint_bld
.elem_type
, imm
->u
[i
].Uint
, false );
1215 ctx
->soa
.num_immediates
++;
1219 build_intrinsic(LLVMBuilderRef builder
,
1221 LLVMTypeRef ret_type
,
1226 LLVMModuleRef module
= LLVMGetGlobalParent(LLVMGetBasicBlockParent(LLVMGetInsertBlock(builder
)));
1227 LLVMValueRef function
;
1229 function
= LLVMGetNamedFunction(module
, name
);
1231 LLVMTypeRef arg_types
[LP_MAX_FUNC_ARGS
];
1234 assert(num_args
<= LP_MAX_FUNC_ARGS
);
1236 for(i
= 0; i
< num_args
; ++i
) {
1238 arg_types
[i
] = LLVMTypeOf(args
[i
]);
1241 function
= lp_declare_intrinsic(module
, name
, ret_type
, arg_types
, num_args
);
1244 LLVMAddFunctionAttr(function
, attr
);
1247 return LLVMBuildCall(builder
, function
, args
, num_args
, "");
1250 static void build_tgsi_intrinsic(
1251 const struct lp_build_tgsi_action
* action
,
1252 struct lp_build_tgsi_context
* bld_base
,
1253 struct lp_build_emit_data
* emit_data
,
1256 struct lp_build_context
* base
= &bld_base
->base
;
1257 emit_data
->output
[emit_data
->chan
] = build_intrinsic(
1258 base
->gallivm
->builder
, action
->intr_name
,
1259 emit_data
->dst_type
, emit_data
->args
,
1260 emit_data
->arg_count
, attr
);
1264 build_tgsi_intrinsic_nomem(
1265 const struct lp_build_tgsi_action
* action
,
1266 struct lp_build_tgsi_context
* bld_base
,
1267 struct lp_build_emit_data
* emit_data
)
1269 build_tgsi_intrinsic(action
, bld_base
, emit_data
, LLVMReadNoneAttribute
);
1272 static void emit_bfi(const struct lp_build_tgsi_action
* action
,
1273 struct lp_build_tgsi_context
* bld_base
,
1274 struct lp_build_emit_data
* emit_data
)
1276 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1277 LLVMBuilderRef builder
= gallivm
->builder
;
1278 LLVMValueRef bfi_args
[3];
1280 // Calculate the bitmask: (((1 << src3) - 1) << src2
1281 bfi_args
[0] = LLVMBuildShl(builder
,
1282 LLVMBuildSub(builder
,
1283 LLVMBuildShl(builder
,
1284 bld_base
->int_bld
.one
,
1285 emit_data
->args
[3], ""),
1286 bld_base
->int_bld
.one
, ""),
1287 emit_data
->args
[2], "");
1289 bfi_args
[1] = LLVMBuildShl(builder
, emit_data
->args
[1],
1290 emit_data
->args
[2], "");
1292 bfi_args
[2] = emit_data
->args
[0];
1295 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
1296 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
1298 emit_data
->output
[emit_data
->chan
] =
1299 LLVMBuildXor(builder
, bfi_args
[2],
1300 LLVMBuildAnd(builder
, bfi_args
[0],
1301 LLVMBuildXor(builder
, bfi_args
[1], bfi_args
[2],
1305 /* this is ffs in C */
1306 static void emit_lsb(const struct lp_build_tgsi_action
* action
,
1307 struct lp_build_tgsi_context
* bld_base
,
1308 struct lp_build_emit_data
* emit_data
)
1310 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1311 LLVMValueRef args
[2] = {
1314 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
1315 * add special code to check for x=0. The reason is that
1316 * the LLVM behavior for x=0 is different from what we
1319 * The hardware already implements the correct behavior.
1321 lp_build_const_int32(gallivm
, 1)
1324 emit_data
->output
[emit_data
->chan
] =
1325 build_intrinsic(gallivm
->builder
, "llvm.cttz.i32",
1326 emit_data
->dst_type
, args
, Elements(args
),
1327 LLVMReadNoneAttribute
);
1330 /* Find the last bit set. */
1331 static void emit_umsb(const struct lp_build_tgsi_action
* action
,
1332 struct lp_build_tgsi_context
* bld_base
,
1333 struct lp_build_emit_data
* emit_data
)
1335 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1336 LLVMBuilderRef builder
= gallivm
->builder
;
1337 LLVMValueRef args
[2] = {
1339 /* Don't generate code for handling zero: */
1340 lp_build_const_int32(gallivm
, 1)
1344 build_intrinsic(builder
, "llvm.ctlz.i32",
1345 emit_data
->dst_type
, args
, Elements(args
),
1346 LLVMReadNoneAttribute
);
1348 /* The HW returns the last bit index from MSB, but TGSI wants
1349 * the index from LSB. Invert it by doing "31 - msb". */
1350 msb
= LLVMBuildSub(builder
, lp_build_const_int32(gallivm
, 31),
1353 /* Check for zero: */
1354 emit_data
->output
[emit_data
->chan
] =
1355 LLVMBuildSelect(builder
,
1356 LLVMBuildICmp(builder
, LLVMIntEQ
, args
[0],
1357 bld_base
->uint_bld
.zero
, ""),
1358 lp_build_const_int32(gallivm
, -1), msb
, "");
1361 /* Find the last bit opposite of the sign bit. */
1362 static void emit_imsb(const struct lp_build_tgsi_action
* action
,
1363 struct lp_build_tgsi_context
* bld_base
,
1364 struct lp_build_emit_data
* emit_data
)
1366 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1367 LLVMBuilderRef builder
= gallivm
->builder
;
1368 LLVMValueRef arg
= emit_data
->args
[0];
1371 build_intrinsic(builder
, "llvm.AMDGPU.flbit.i32",
1372 emit_data
->dst_type
, &arg
, 1,
1373 LLVMReadNoneAttribute
);
1375 /* The HW returns the last bit index from MSB, but TGSI wants
1376 * the index from LSB. Invert it by doing "31 - msb". */
1377 msb
= LLVMBuildSub(builder
, lp_build_const_int32(gallivm
, 31),
1380 /* If arg == 0 || arg == -1 (0xffffffff), return -1. */
1381 LLVMValueRef all_ones
= lp_build_const_int32(gallivm
, -1);
1384 LLVMBuildOr(builder
,
1385 LLVMBuildICmp(builder
, LLVMIntEQ
, arg
,
1386 bld_base
->uint_bld
.zero
, ""),
1387 LLVMBuildICmp(builder
, LLVMIntEQ
, arg
,
1390 emit_data
->output
[emit_data
->chan
] =
1391 LLVMBuildSelect(builder
, cond
, all_ones
, msb
, "");
1394 void radeon_llvm_context_init(struct radeon_llvm_context
* ctx
)
1396 struct lp_type type
;
1398 /* Initialize the gallivm object:
1399 * We are only using the module, context, and builder fields of this struct.
1400 * This should be enough for us to be able to pass our gallivm struct to the
1401 * helper functions in the gallivm module.
1403 memset(&ctx
->gallivm
, 0, sizeof (ctx
->gallivm
));
1404 memset(&ctx
->soa
, 0, sizeof(ctx
->soa
));
1405 ctx
->gallivm
.context
= LLVMContextCreate();
1406 ctx
->gallivm
.module
= LLVMModuleCreateWithNameInContext("tgsi",
1407 ctx
->gallivm
.context
);
1408 ctx
->gallivm
.builder
= LLVMCreateBuilderInContext(ctx
->gallivm
.context
);
1410 ctx
->store_output_intr
= "llvm.AMDGPU.store.output.";
1411 ctx
->swizzle_intr
= "llvm.AMDGPU.swizzle";
1412 struct lp_build_tgsi_context
* bld_base
= &ctx
->soa
.bld_base
;
1414 /* XXX: We need to revisit this.I think the correct way to do this is
1415 * to use length = 4 here and use the elem_bld for everything. */
1416 type
.floating
= TRUE
;
1423 lp_build_context_init(&bld_base
->base
, &ctx
->gallivm
, type
);
1424 lp_build_context_init(&ctx
->soa
.bld_base
.uint_bld
, &ctx
->gallivm
, lp_uint_type(type
));
1425 lp_build_context_init(&ctx
->soa
.bld_base
.int_bld
, &ctx
->gallivm
, lp_int_type(type
));
1428 bld_base
->emit_store
= emit_store
;
1429 bld_base
->emit_swizzle
= emit_swizzle
;
1430 bld_base
->emit_declaration
= emit_declaration
;
1431 bld_base
->emit_immediate
= emit_immediate
;
1433 bld_base
->emit_fetch_funcs
[TGSI_FILE_IMMEDIATE
] = emit_fetch
;
1434 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = emit_fetch
;
1435 bld_base
->emit_fetch_funcs
[TGSI_FILE_TEMPORARY
] = emit_fetch
;
1436 bld_base
->emit_fetch_funcs
[TGSI_FILE_OUTPUT
] = emit_fetch
;
1437 bld_base
->emit_fetch_funcs
[TGSI_FILE_SYSTEM_VALUE
] = fetch_system_value
;
1439 /* Allocate outputs */
1440 ctx
->soa
.outputs
= ctx
->outputs
;
1442 /* XXX: Is there a better way to initialize all this ? */
1444 lp_set_default_actions(bld_base
);
1446 bld_base
->op_actions
[TGSI_OPCODE_ABS
].emit
= build_tgsi_intrinsic_nomem
;
1447 bld_base
->op_actions
[TGSI_OPCODE_ABS
].intr_name
= "fabs";
1448 bld_base
->op_actions
[TGSI_OPCODE_AND
].emit
= emit_and
;
1449 bld_base
->op_actions
[TGSI_OPCODE_ARL
].emit
= emit_arl
;
1450 bld_base
->op_actions
[TGSI_OPCODE_BFI
].emit
= emit_bfi
;
1451 bld_base
->op_actions
[TGSI_OPCODE_BGNLOOP
].emit
= bgnloop_emit
;
1452 bld_base
->op_actions
[TGSI_OPCODE_BREV
].emit
= build_tgsi_intrinsic_nomem
;
1453 bld_base
->op_actions
[TGSI_OPCODE_BREV
].intr_name
= "llvm.AMDGPU.brev";
1454 bld_base
->op_actions
[TGSI_OPCODE_BRK
].emit
= brk_emit
;
1455 bld_base
->op_actions
[TGSI_OPCODE_CEIL
].emit
= build_tgsi_intrinsic_nomem
;
1456 bld_base
->op_actions
[TGSI_OPCODE_CEIL
].intr_name
= "ceil";
1457 bld_base
->op_actions
[TGSI_OPCODE_CLAMP
].emit
= build_tgsi_intrinsic_nomem
;
1458 bld_base
->op_actions
[TGSI_OPCODE_CLAMP
].intr_name
= "llvm.AMDIL.clamp.";
1459 bld_base
->op_actions
[TGSI_OPCODE_CMP
].emit
= build_tgsi_intrinsic_nomem
;
1460 bld_base
->op_actions
[TGSI_OPCODE_CMP
].intr_name
= "llvm.AMDGPU.cndlt";
1461 bld_base
->op_actions
[TGSI_OPCODE_CONT
].emit
= cont_emit
;
1462 bld_base
->op_actions
[TGSI_OPCODE_COS
].emit
= build_tgsi_intrinsic_nomem
;
1463 bld_base
->op_actions
[TGSI_OPCODE_COS
].intr_name
= "llvm.cos.f32";
1464 bld_base
->op_actions
[TGSI_OPCODE_DDX
].intr_name
= "llvm.AMDGPU.ddx";
1465 bld_base
->op_actions
[TGSI_OPCODE_DDX
].fetch_args
= tex_fetch_args
;
1466 bld_base
->op_actions
[TGSI_OPCODE_DDY
].intr_name
= "llvm.AMDGPU.ddy";
1467 bld_base
->op_actions
[TGSI_OPCODE_DDY
].fetch_args
= tex_fetch_args
;
1468 bld_base
->op_actions
[TGSI_OPCODE_ELSE
].emit
= else_emit
;
1469 bld_base
->op_actions
[TGSI_OPCODE_ENDIF
].emit
= endif_emit
;
1470 bld_base
->op_actions
[TGSI_OPCODE_ENDLOOP
].emit
= endloop_emit
;
1471 bld_base
->op_actions
[TGSI_OPCODE_EX2
].emit
= build_tgsi_intrinsic_nomem
;
1472 bld_base
->op_actions
[TGSI_OPCODE_EX2
].intr_name
= "llvm.AMDIL.exp.";
1473 bld_base
->op_actions
[TGSI_OPCODE_FLR
].emit
= build_tgsi_intrinsic_nomem
;
1474 bld_base
->op_actions
[TGSI_OPCODE_FLR
].intr_name
= "floor";
1475 bld_base
->op_actions
[TGSI_OPCODE_FMA
].emit
= build_tgsi_intrinsic_nomem
;
1476 bld_base
->op_actions
[TGSI_OPCODE_FMA
].intr_name
= "llvm.fma.f32";
1477 bld_base
->op_actions
[TGSI_OPCODE_FRC
].emit
= build_tgsi_intrinsic_nomem
;
1478 bld_base
->op_actions
[TGSI_OPCODE_FRC
].intr_name
= "llvm.AMDIL.fraction.";
1479 bld_base
->op_actions
[TGSI_OPCODE_F2I
].emit
= emit_f2i
;
1480 bld_base
->op_actions
[TGSI_OPCODE_F2U
].emit
= emit_f2u
;
1481 bld_base
->op_actions
[TGSI_OPCODE_FSEQ
].emit
= emit_fcmp
;
1482 bld_base
->op_actions
[TGSI_OPCODE_FSGE
].emit
= emit_fcmp
;
1483 bld_base
->op_actions
[TGSI_OPCODE_FSLT
].emit
= emit_fcmp
;
1484 bld_base
->op_actions
[TGSI_OPCODE_FSNE
].emit
= emit_fcmp
;
1485 bld_base
->op_actions
[TGSI_OPCODE_IABS
].emit
= build_tgsi_intrinsic_nomem
;
1486 bld_base
->op_actions
[TGSI_OPCODE_IABS
].intr_name
= "llvm.AMDIL.abs.";
1487 bld_base
->op_actions
[TGSI_OPCODE_IBFE
].emit
= build_tgsi_intrinsic_nomem
;
1488 bld_base
->op_actions
[TGSI_OPCODE_IBFE
].intr_name
= "llvm.AMDGPU.bfe.i32";
1489 bld_base
->op_actions
[TGSI_OPCODE_IDIV
].emit
= emit_idiv
;
1490 bld_base
->op_actions
[TGSI_OPCODE_IF
].emit
= if_emit
;
1491 bld_base
->op_actions
[TGSI_OPCODE_UIF
].emit
= uif_emit
;
1492 bld_base
->op_actions
[TGSI_OPCODE_IMAX
].emit
= build_tgsi_intrinsic_nomem
;
1493 bld_base
->op_actions
[TGSI_OPCODE_IMAX
].intr_name
= "llvm.AMDGPU.imax";
1494 bld_base
->op_actions
[TGSI_OPCODE_IMIN
].emit
= build_tgsi_intrinsic_nomem
;
1495 bld_base
->op_actions
[TGSI_OPCODE_IMIN
].intr_name
= "llvm.AMDGPU.imin";
1496 bld_base
->op_actions
[TGSI_OPCODE_IMSB
].emit
= emit_imsb
;
1497 bld_base
->op_actions
[TGSI_OPCODE_INEG
].emit
= emit_ineg
;
1498 bld_base
->op_actions
[TGSI_OPCODE_ISHR
].emit
= emit_ishr
;
1499 bld_base
->op_actions
[TGSI_OPCODE_ISGE
].emit
= emit_icmp
;
1500 bld_base
->op_actions
[TGSI_OPCODE_ISLT
].emit
= emit_icmp
;
1501 bld_base
->op_actions
[TGSI_OPCODE_ISSG
].emit
= emit_ssg
;
1502 bld_base
->op_actions
[TGSI_OPCODE_I2F
].emit
= emit_i2f
;
1503 bld_base
->op_actions
[TGSI_OPCODE_KILL_IF
].fetch_args
= kill_if_fetch_args
;
1504 bld_base
->op_actions
[TGSI_OPCODE_KILL_IF
].emit
= kil_emit
;
1505 bld_base
->op_actions
[TGSI_OPCODE_KILL_IF
].intr_name
= "llvm.AMDGPU.kill";
1506 bld_base
->op_actions
[TGSI_OPCODE_KILL
].emit
= lp_build_tgsi_intrinsic
;
1507 bld_base
->op_actions
[TGSI_OPCODE_KILL
].intr_name
= "llvm.AMDGPU.kilp";
1508 bld_base
->op_actions
[TGSI_OPCODE_LSB
].emit
= emit_lsb
;
1509 bld_base
->op_actions
[TGSI_OPCODE_LG2
].emit
= build_tgsi_intrinsic_nomem
;
1510 bld_base
->op_actions
[TGSI_OPCODE_LG2
].intr_name
= "llvm.log2.f32";
1511 bld_base
->op_actions
[TGSI_OPCODE_LRP
].emit
= build_tgsi_intrinsic_nomem
;
1512 bld_base
->op_actions
[TGSI_OPCODE_LRP
].intr_name
= "llvm.AMDGPU.lrp";
1513 bld_base
->op_actions
[TGSI_OPCODE_MOD
].emit
= emit_mod
;
1514 bld_base
->op_actions
[TGSI_OPCODE_UMSB
].emit
= emit_umsb
;
1515 bld_base
->op_actions
[TGSI_OPCODE_NOT
].emit
= emit_not
;
1516 bld_base
->op_actions
[TGSI_OPCODE_OR
].emit
= emit_or
;
1517 bld_base
->op_actions
[TGSI_OPCODE_POPC
].emit
= build_tgsi_intrinsic_nomem
;
1518 bld_base
->op_actions
[TGSI_OPCODE_POPC
].intr_name
= "llvm.ctpop.i32";
1519 bld_base
->op_actions
[TGSI_OPCODE_POW
].emit
= build_tgsi_intrinsic_nomem
;
1520 bld_base
->op_actions
[TGSI_OPCODE_POW
].intr_name
= "llvm.pow.f32";
1521 bld_base
->op_actions
[TGSI_OPCODE_ROUND
].emit
= build_tgsi_intrinsic_nomem
;
1522 bld_base
->op_actions
[TGSI_OPCODE_ROUND
].intr_name
= "llvm.AMDIL.round.nearest.";
1523 bld_base
->op_actions
[TGSI_OPCODE_SGE
].emit
= emit_cmp
;
1524 bld_base
->op_actions
[TGSI_OPCODE_SEQ
].emit
= emit_cmp
;
1525 bld_base
->op_actions
[TGSI_OPCODE_SHL
].emit
= emit_shl
;
1526 bld_base
->op_actions
[TGSI_OPCODE_SLE
].emit
= emit_cmp
;
1527 bld_base
->op_actions
[TGSI_OPCODE_SLT
].emit
= emit_cmp
;
1528 bld_base
->op_actions
[TGSI_OPCODE_SNE
].emit
= emit_cmp
;
1529 bld_base
->op_actions
[TGSI_OPCODE_SGT
].emit
= emit_cmp
;
1530 bld_base
->op_actions
[TGSI_OPCODE_SIN
].emit
= build_tgsi_intrinsic_nomem
;
1531 bld_base
->op_actions
[TGSI_OPCODE_SIN
].intr_name
= "llvm.sin.f32";
1532 bld_base
->op_actions
[TGSI_OPCODE_SQRT
].emit
= build_tgsi_intrinsic_nomem
;
1533 bld_base
->op_actions
[TGSI_OPCODE_SQRT
].intr_name
= "llvm.sqrt.f32";
1534 bld_base
->op_actions
[TGSI_OPCODE_SSG
].emit
= emit_ssg
;
1535 bld_base
->op_actions
[TGSI_OPCODE_TEX
].fetch_args
= tex_fetch_args
;
1536 bld_base
->op_actions
[TGSI_OPCODE_TEX
].intr_name
= "llvm.AMDGPU.tex";
1537 bld_base
->op_actions
[TGSI_OPCODE_TEX2
].fetch_args
= tex_fetch_args
;
1538 bld_base
->op_actions
[TGSI_OPCODE_TEX2
].intr_name
= "llvm.AMDGPU.tex";
1539 bld_base
->op_actions
[TGSI_OPCODE_TXB
].fetch_args
= tex_fetch_args
;
1540 bld_base
->op_actions
[TGSI_OPCODE_TXB
].intr_name
= "llvm.AMDGPU.txb";
1541 bld_base
->op_actions
[TGSI_OPCODE_TXB2
].fetch_args
= tex_fetch_args
;
1542 bld_base
->op_actions
[TGSI_OPCODE_TXB2
].intr_name
= "llvm.AMDGPU.txb";
1543 bld_base
->op_actions
[TGSI_OPCODE_TXD
].fetch_args
= txd_fetch_args
;
1544 bld_base
->op_actions
[TGSI_OPCODE_TXD
].intr_name
= "llvm.AMDGPU.txd";
1545 bld_base
->op_actions
[TGSI_OPCODE_TXF
].fetch_args
= txf_fetch_args
;
1546 bld_base
->op_actions
[TGSI_OPCODE_TXF
].intr_name
= "llvm.AMDGPU.txf";
1547 bld_base
->op_actions
[TGSI_OPCODE_TXL
].fetch_args
= tex_fetch_args
;
1548 bld_base
->op_actions
[TGSI_OPCODE_TXL
].intr_name
= "llvm.AMDGPU.txl";
1549 bld_base
->op_actions
[TGSI_OPCODE_TXL2
].fetch_args
= tex_fetch_args
;
1550 bld_base
->op_actions
[TGSI_OPCODE_TXL2
].intr_name
= "llvm.AMDGPU.txl";
1551 bld_base
->op_actions
[TGSI_OPCODE_TXP
].fetch_args
= txp_fetch_args
;
1552 bld_base
->op_actions
[TGSI_OPCODE_TXP
].intr_name
= "llvm.AMDGPU.tex";
1553 bld_base
->op_actions
[TGSI_OPCODE_TXQ
].fetch_args
= tex_fetch_args
;
1554 bld_base
->op_actions
[TGSI_OPCODE_TXQ
].intr_name
= "llvm.AMDGPU.txq";
1555 bld_base
->op_actions
[TGSI_OPCODE_TRUNC
].emit
= build_tgsi_intrinsic_nomem
;
1556 bld_base
->op_actions
[TGSI_OPCODE_TRUNC
].intr_name
= "llvm.AMDGPU.trunc";
1557 bld_base
->op_actions
[TGSI_OPCODE_UADD
].emit
= emit_uadd
;
1558 bld_base
->op_actions
[TGSI_OPCODE_UBFE
].emit
= build_tgsi_intrinsic_nomem
;
1559 bld_base
->op_actions
[TGSI_OPCODE_UBFE
].intr_name
= "llvm.AMDGPU.bfe.u32";
1560 bld_base
->op_actions
[TGSI_OPCODE_UDIV
].emit
= emit_udiv
;
1561 bld_base
->op_actions
[TGSI_OPCODE_UMAX
].emit
= build_tgsi_intrinsic_nomem
;
1562 bld_base
->op_actions
[TGSI_OPCODE_UMAX
].intr_name
= "llvm.AMDGPU.umax";
1563 bld_base
->op_actions
[TGSI_OPCODE_UMIN
].emit
= build_tgsi_intrinsic_nomem
;
1564 bld_base
->op_actions
[TGSI_OPCODE_UMIN
].intr_name
= "llvm.AMDGPU.umin";
1565 bld_base
->op_actions
[TGSI_OPCODE_UMOD
].emit
= emit_umod
;
1566 bld_base
->op_actions
[TGSI_OPCODE_USEQ
].emit
= emit_icmp
;
1567 bld_base
->op_actions
[TGSI_OPCODE_USGE
].emit
= emit_icmp
;
1568 bld_base
->op_actions
[TGSI_OPCODE_USHR
].emit
= emit_ushr
;
1569 bld_base
->op_actions
[TGSI_OPCODE_USLT
].emit
= emit_icmp
;
1570 bld_base
->op_actions
[TGSI_OPCODE_USNE
].emit
= emit_icmp
;
1571 bld_base
->op_actions
[TGSI_OPCODE_U2F
].emit
= emit_u2f
;
1572 bld_base
->op_actions
[TGSI_OPCODE_XOR
].emit
= emit_xor
;
1573 bld_base
->op_actions
[TGSI_OPCODE_UCMP
].emit
= emit_ucmp
;
1575 bld_base
->rsq_action
.emit
= build_tgsi_intrinsic_nomem
;
1576 #if HAVE_LLVM >= 0x0305
1577 bld_base
->rsq_action
.intr_name
= "llvm.AMDGPU.rsq.clamped.f32";
1579 bld_base
->rsq_action
.intr_name
= "llvm.AMDGPU.rsq";
1583 void radeon_llvm_create_func(struct radeon_llvm_context
* ctx
,
1584 LLVMTypeRef
*ParamTypes
, unsigned ParamCount
)
1586 LLVMTypeRef main_fn_type
;
1587 LLVMBasicBlockRef main_fn_body
;
1589 /* Setup the function */
1590 main_fn_type
= LLVMFunctionType(LLVMVoidTypeInContext(ctx
->gallivm
.context
),
1591 ParamTypes
, ParamCount
, 0);
1592 ctx
->main_fn
= LLVMAddFunction(ctx
->gallivm
.module
, "main", main_fn_type
);
1593 main_fn_body
= LLVMAppendBasicBlockInContext(ctx
->gallivm
.context
,
1594 ctx
->main_fn
, "main_body");
1595 LLVMPositionBuilderAtEnd(ctx
->gallivm
.builder
, main_fn_body
);
1598 void radeon_llvm_finalize_module(struct radeon_llvm_context
* ctx
)
1600 struct gallivm_state
* gallivm
= ctx
->soa
.bld_base
.base
.gallivm
;
1601 /* End the main function with Return*/
1602 LLVMBuildRetVoid(gallivm
->builder
);
1604 /* Create the pass manager */
1605 ctx
->gallivm
.passmgr
= LLVMCreateFunctionPassManagerForModule(
1608 /* This pass should eliminate all the load and store instructions */
1609 LLVMAddPromoteMemoryToRegisterPass(gallivm
->passmgr
);
1611 /* Add some optimization passes */
1612 LLVMAddScalarReplAggregatesPass(gallivm
->passmgr
);
1613 LLVMAddLICMPass(gallivm
->passmgr
);
1614 LLVMAddAggressiveDCEPass(gallivm
->passmgr
);
1615 LLVMAddCFGSimplificationPass(gallivm
->passmgr
);
1616 LLVMAddInstructionCombiningPass(gallivm
->passmgr
);
1619 LLVMRunFunctionPassManager(gallivm
->passmgr
, ctx
->main_fn
);
1621 LLVMDisposeBuilder(gallivm
->builder
);
1622 LLVMDisposePassManager(gallivm
->passmgr
);
1626 void radeon_llvm_dispose(struct radeon_llvm_context
* ctx
)
1628 LLVMDisposeModule(ctx
->soa
.bld_base
.base
.gallivm
->module
);
1629 LLVMContextDispose(ctx
->soa
.bld_base
.base
.gallivm
->context
);
1634 ctx
->temps_count
= 0;
1637 ctx
->loop_depth_max
= 0;
1640 ctx
->branch_depth_max
= 0;