1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
32 * @author Jose Fonseca <jfonseca@vmware.com>
36 #include <llvm-c/Transforms/Scalar.h>
38 #include "util/u_memory.h"
39 #include "util/u_cpu_detect.h"
40 #include "gallivm/lp_bld_init.h"
42 #include "lp_screen.h"
43 #include "gallivm/lp_bld_intr.h"
48 lp_jit_init_globals(struct llvmpipe_screen
*screen
)
50 LLVMTypeRef texture_type
;
52 /* struct lp_jit_texture */
54 LLVMTypeRef elem_types
[6];
56 elem_types
[LP_JIT_TEXTURE_WIDTH
] = LLVMInt32Type();
57 elem_types
[LP_JIT_TEXTURE_HEIGHT
] = LLVMInt32Type();
58 elem_types
[LP_JIT_TEXTURE_DEPTH
] = LLVMInt32Type();
59 elem_types
[LP_JIT_TEXTURE_LAST_LEVEL
] = LLVMInt32Type();
60 elem_types
[LP_JIT_TEXTURE_STRIDE
] = LLVMInt32Type();
61 elem_types
[LP_JIT_TEXTURE_DATA
] =
62 LLVMArrayType(LLVMPointerType(LLVMInt8Type(), 0),
63 LP_MAX_TEXTURE_2D_LEVELS
);
65 texture_type
= LLVMStructType(elem_types
, Elements(elem_types
), 0);
67 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture
, width
,
68 screen
->target
, texture_type
,
69 LP_JIT_TEXTURE_WIDTH
);
70 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture
, height
,
71 screen
->target
, texture_type
,
72 LP_JIT_TEXTURE_HEIGHT
);
73 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture
, depth
,
74 screen
->target
, texture_type
,
75 LP_JIT_TEXTURE_DEPTH
);
76 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture
, last_level
,
77 screen
->target
, texture_type
,
78 LP_JIT_TEXTURE_LAST_LEVEL
);
79 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture
, stride
,
80 screen
->target
, texture_type
,
81 LP_JIT_TEXTURE_STRIDE
);
82 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture
, data
,
83 screen
->target
, texture_type
,
85 LP_CHECK_STRUCT_SIZE(struct lp_jit_texture
,
86 screen
->target
, texture_type
);
88 LLVMAddTypeName(screen
->module
, "texture", texture_type
);
91 /* struct lp_jit_context */
93 LLVMTypeRef elem_types
[8];
94 LLVMTypeRef context_type
;
96 elem_types
[0] = LLVMPointerType(LLVMFloatType(), 0); /* constants */
97 elem_types
[1] = LLVMFloatType(); /* alpha_ref_value */ elem_types
[2] = LLVMFloatType(); /* scissor_xmin */
98 elem_types
[3] = LLVMFloatType(); /* scissor_ymin */
99 elem_types
[4] = LLVMFloatType(); /* scissor_xmax */
100 elem_types
[5] = LLVMFloatType(); /* scissor_ymax */
101 elem_types
[6] = LLVMPointerType(LLVMInt8Type(), 0); /* blend_color */
102 elem_types
[7] = LLVMArrayType(texture_type
, PIPE_MAX_SAMPLERS
); /* textures */
104 context_type
= LLVMStructType(elem_types
, Elements(elem_types
), 0);
106 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, constants
,
107 screen
->target
, context_type
, 0);
108 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, alpha_ref_value
,
109 screen
->target
, context_type
, 1);
110 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, scissor_xmin
,
111 screen
->target
, context_type
, 2);
112 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, scissor_ymin
,
113 screen
->target
, context_type
, 3);
114 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, scissor_xmax
,
115 screen
->target
, context_type
, 4);
116 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, scissor_ymax
,
117 screen
->target
, context_type
, 5);
118 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, blend_color
,
119 screen
->target
, context_type
, 6);
120 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, textures
,
121 screen
->target
, context_type
,
122 LP_JIT_CONTEXT_TEXTURES_INDEX
);
123 LP_CHECK_STRUCT_SIZE(struct lp_jit_context
,
124 screen
->target
, context_type
);
126 LLVMAddTypeName(screen
->module
, "context", context_type
);
128 screen
->context_ptr_type
= LLVMPointerType(context_type
, 0);
132 LLVMDumpModule(screen
->module
);
138 lp_jit_screen_cleanup(struct llvmpipe_screen
*screen
)
141 LLVMDisposeExecutionEngine(screen
->engine
);
144 LLVMDisposePassManager(screen
->pass
);
149 lp_jit_screen_init(struct llvmpipe_screen
*screen
)
156 /* For simulating less capable machines */
157 util_cpu_caps
.has_sse3
= 0;
158 util_cpu_caps
.has_ssse3
= 0;
159 util_cpu_caps
.has_sse4_1
= 0;
164 screen
->module
= LLVMModuleCreateWithName("llvmpipe");
166 screen
->provider
= LLVMCreateModuleProviderForExistingModule(screen
->module
);
168 if (LLVMCreateJITCompiler(&screen
->engine
, screen
->provider
, 1, &error
)) {
169 _debug_printf("%s\n", error
);
170 LLVMDisposeMessage(error
);
174 screen
->target
= LLVMGetExecutionEngineTargetData(screen
->engine
);
176 screen
->pass
= LLVMCreateFunctionPassManager(screen
->provider
);
177 LLVMAddTargetData(screen
->target
, screen
->pass
);
179 if ((LP_DEBUG
& DEBUG_NO_LLVM_OPT
) == 0) {
180 /* These are the passes currently listed in llvm-c/Transforms/Scalar.h,
181 * but there are more on SVN. */
182 /* TODO: Add more passes */
183 LLVMAddConstantPropagationPass(screen
->pass
);
184 if(util_cpu_caps
.has_sse4_1
) {
185 /* FIXME: There is a bug in this pass, whereby the combination of fptosi
186 * and sitofp (necessary for trunc/floor/ceil/round implementation)
187 * somehow becomes invalid code.
189 LLVMAddInstructionCombiningPass(screen
->pass
);
191 LLVMAddPromoteMemoryToRegisterPass(screen
->pass
);
192 LLVMAddGVNPass(screen
->pass
);
193 LLVMAddCFGSimplificationPass(screen
->pass
);
196 lp_jit_init_globals(screen
);