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_ROW_STRIDE
] =
61 LLVMArrayType(LLVMInt32Type(), LP_MAX_TEXTURE_2D_LEVELS
);
62 elem_types
[LP_JIT_TEXTURE_DATA
] =
63 LLVMArrayType(LLVMPointerType(LLVMInt8Type(), 0),
64 LP_MAX_TEXTURE_2D_LEVELS
);
66 texture_type
= LLVMStructType(elem_types
, Elements(elem_types
), 0);
68 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture
, width
,
69 screen
->target
, texture_type
,
70 LP_JIT_TEXTURE_WIDTH
);
71 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture
, height
,
72 screen
->target
, texture_type
,
73 LP_JIT_TEXTURE_HEIGHT
);
74 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture
, depth
,
75 screen
->target
, texture_type
,
76 LP_JIT_TEXTURE_DEPTH
);
77 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture
, last_level
,
78 screen
->target
, texture_type
,
79 LP_JIT_TEXTURE_LAST_LEVEL
);
80 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture
, row_stride
,
81 screen
->target
, texture_type
,
82 LP_JIT_TEXTURE_ROW_STRIDE
);
83 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture
, data
,
84 screen
->target
, texture_type
,
86 LP_CHECK_STRUCT_SIZE(struct lp_jit_texture
,
87 screen
->target
, texture_type
);
89 LLVMAddTypeName(screen
->module
, "texture", texture_type
);
92 /* struct lp_jit_context */
94 LLVMTypeRef elem_types
[LP_JIT_CTX_COUNT
];
95 LLVMTypeRef context_type
;
97 elem_types
[LP_JIT_CTX_CONSTANTS
] = LLVMPointerType(LLVMFloatType(), 0);
98 elem_types
[LP_JIT_CTX_ALPHA_REF
] = LLVMFloatType();
99 elem_types
[LP_JIT_CTX_STENCIL_REF_FRONT
] = LLVMInt32Type();
100 elem_types
[LP_JIT_CTX_STENCIL_REF_BACK
] = LLVMInt32Type();
101 elem_types
[LP_JIT_CTX_SCISSOR_XMIN
] = LLVMFloatType();
102 elem_types
[LP_JIT_CTX_SCISSOR_YMIN
] = LLVMFloatType();
103 elem_types
[LP_JIT_CTX_SCISSOR_XMAX
] = LLVMFloatType();
104 elem_types
[LP_JIT_CTX_SCISSOR_YMAX
] = LLVMFloatType();
105 elem_types
[LP_JIT_CTX_BLEND_COLOR
] = LLVMPointerType(LLVMInt8Type(), 0);
106 elem_types
[LP_JIT_CTX_TEXTURES
] = LLVMArrayType(texture_type
,
109 context_type
= LLVMStructType(elem_types
, Elements(elem_types
), 0);
111 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, constants
,
112 screen
->target
, context_type
,
113 LP_JIT_CTX_CONSTANTS
);
114 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, alpha_ref_value
,
115 screen
->target
, context_type
,
116 LP_JIT_CTX_ALPHA_REF
);
117 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, stencil_ref_front
,
118 screen
->target
, context_type
,
119 LP_JIT_CTX_STENCIL_REF_FRONT
);
120 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, stencil_ref_back
,
121 screen
->target
, context_type
,
122 LP_JIT_CTX_STENCIL_REF_BACK
);
123 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, scissor_xmin
,
124 screen
->target
, context_type
,
125 LP_JIT_CTX_SCISSOR_XMIN
);
126 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, scissor_ymin
,
127 screen
->target
, context_type
,
128 LP_JIT_CTX_SCISSOR_YMIN
);
129 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, scissor_xmax
,
130 screen
->target
, context_type
,
131 LP_JIT_CTX_SCISSOR_XMAX
);
132 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, scissor_ymax
,
133 screen
->target
, context_type
,
134 LP_JIT_CTX_SCISSOR_YMAX
);
135 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, blend_color
,
136 screen
->target
, context_type
,
137 LP_JIT_CTX_BLEND_COLOR
);
138 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, textures
,
139 screen
->target
, context_type
,
140 LP_JIT_CTX_TEXTURES
);
141 LP_CHECK_STRUCT_SIZE(struct lp_jit_context
,
142 screen
->target
, context_type
);
144 LLVMAddTypeName(screen
->module
, "context", context_type
);
146 screen
->context_ptr_type
= LLVMPointerType(context_type
, 0);
150 LLVMDumpModule(screen
->module
);
156 lp_jit_screen_cleanup(struct llvmpipe_screen
*screen
)
159 LLVMDisposeExecutionEngine(screen
->engine
);
162 LLVMDisposePassManager(screen
->pass
);
167 lp_jit_screen_init(struct llvmpipe_screen
*screen
)
174 /* For simulating less capable machines */
175 util_cpu_caps
.has_sse3
= 0;
176 util_cpu_caps
.has_ssse3
= 0;
177 util_cpu_caps
.has_sse4_1
= 0;
182 screen
->module
= LLVMModuleCreateWithName("llvmpipe");
184 screen
->provider
= LLVMCreateModuleProviderForExistingModule(screen
->module
);
186 if (LLVMCreateJITCompiler(&screen
->engine
, screen
->provider
, 1, &error
)) {
187 _debug_printf("%s\n", error
);
188 LLVMDisposeMessage(error
);
192 screen
->target
= LLVMGetExecutionEngineTargetData(screen
->engine
);
194 screen
->pass
= LLVMCreateFunctionPassManager(screen
->provider
);
195 LLVMAddTargetData(screen
->target
, screen
->pass
);
197 if ((LP_DEBUG
& DEBUG_NO_LLVM_OPT
) == 0) {
198 /* These are the passes currently listed in llvm-c/Transforms/Scalar.h,
199 * but there are more on SVN. */
200 /* TODO: Add more passes */
201 LLVMAddConstantPropagationPass(screen
->pass
);
202 if(util_cpu_caps
.has_sse4_1
) {
203 /* FIXME: There is a bug in this pass, whereby the combination of fptosi
204 * and sitofp (necessary for trunc/floor/ceil/round implementation)
205 * somehow becomes invalid code.
207 LLVMAddInstructionCombiningPass(screen
->pass
);
209 LLVMAddPromoteMemoryToRegisterPass(screen
->pass
);
210 LLVMAddGVNPass(screen
->pass
);
211 LLVMAddCFGSimplificationPass(screen
->pass
);
214 lp_jit_init_globals(screen
);