Merge branch '7.8' into master
[mesa.git] / src / gallium / drivers / llvmpipe / lp_jit.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /**
29 * @file
30 * C - JIT interfaces
31 *
32 * @author Jose Fonseca <jfonseca@vmware.com>
33 */
34
35
36 #include <llvm-c/Transforms/Scalar.h>
37
38 #include "util/u_memory.h"
39 #include "util/u_cpu_detect.h"
40 #include "gallivm/lp_bld_init.h"
41 #include "lp_debug.h"
42 #include "lp_screen.h"
43 #include "gallivm/lp_bld_intr.h"
44 #include "lp_jit.h"
45
46
47 static void
48 lp_jit_init_globals(struct llvmpipe_screen *screen)
49 {
50 LLVMTypeRef texture_type;
51
52 /* struct lp_jit_texture */
53 {
54 LLVMTypeRef elem_types[6];
55
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);
65
66 texture_type = LLVMStructType(elem_types, Elements(elem_types), 0);
67
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,
85 LP_JIT_TEXTURE_DATA);
86 LP_CHECK_STRUCT_SIZE(struct lp_jit_texture,
87 screen->target, texture_type);
88
89 LLVMAddTypeName(screen->module, "texture", texture_type);
90 }
91
92 /* struct lp_jit_context */
93 {
94 LLVMTypeRef elem_types[8];
95 LLVMTypeRef context_type;
96
97 elem_types[0] = LLVMPointerType(LLVMFloatType(), 0); /* constants */
98 elem_types[1] = LLVMFloatType(); /* alpha_ref_value */ elem_types[2] = LLVMFloatType(); /* scissor_xmin */
99 elem_types[3] = LLVMFloatType(); /* scissor_ymin */
100 elem_types[4] = LLVMFloatType(); /* scissor_xmax */
101 elem_types[5] = LLVMFloatType(); /* scissor_ymax */
102 elem_types[6] = LLVMPointerType(LLVMInt8Type(), 0); /* blend_color */
103 elem_types[7] = LLVMArrayType(texture_type, PIPE_MAX_SAMPLERS); /* textures */
104
105 context_type = LLVMStructType(elem_types, Elements(elem_types), 0);
106
107 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, constants,
108 screen->target, context_type, 0);
109 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, alpha_ref_value,
110 screen->target, context_type, 1);
111 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, scissor_xmin,
112 screen->target, context_type, 2);
113 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, scissor_ymin,
114 screen->target, context_type, 3);
115 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, scissor_xmax,
116 screen->target, context_type, 4);
117 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, scissor_ymax,
118 screen->target, context_type, 5);
119 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, blend_color,
120 screen->target, context_type, 6);
121 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, textures,
122 screen->target, context_type,
123 LP_JIT_CONTEXT_TEXTURES_INDEX);
124 LP_CHECK_STRUCT_SIZE(struct lp_jit_context,
125 screen->target, context_type);
126
127 LLVMAddTypeName(screen->module, "context", context_type);
128
129 screen->context_ptr_type = LLVMPointerType(context_type, 0);
130 }
131
132 #ifdef DEBUG
133 LLVMDumpModule(screen->module);
134 #endif
135 }
136
137
138 void
139 lp_jit_screen_cleanup(struct llvmpipe_screen *screen)
140 {
141 if(screen->engine)
142 LLVMDisposeExecutionEngine(screen->engine);
143
144 if(screen->pass)
145 LLVMDisposePassManager(screen->pass);
146 }
147
148
149 void
150 lp_jit_screen_init(struct llvmpipe_screen *screen)
151 {
152 char *error = NULL;
153
154 util_cpu_detect();
155
156 #if 0
157 /* For simulating less capable machines */
158 util_cpu_caps.has_sse3 = 0;
159 util_cpu_caps.has_ssse3 = 0;
160 util_cpu_caps.has_sse4_1 = 0;
161 #endif
162
163 lp_build_init();
164
165 screen->module = LLVMModuleCreateWithName("llvmpipe");
166
167 screen->provider = LLVMCreateModuleProviderForExistingModule(screen->module);
168
169 if (LLVMCreateJITCompiler(&screen->engine, screen->provider, 1, &error)) {
170 _debug_printf("%s\n", error);
171 LLVMDisposeMessage(error);
172 assert(0);
173 }
174
175 screen->target = LLVMGetExecutionEngineTargetData(screen->engine);
176
177 screen->pass = LLVMCreateFunctionPassManager(screen->provider);
178 LLVMAddTargetData(screen->target, screen->pass);
179
180 if ((LP_DEBUG & DEBUG_NO_LLVM_OPT) == 0) {
181 /* These are the passes currently listed in llvm-c/Transforms/Scalar.h,
182 * but there are more on SVN. */
183 /* TODO: Add more passes */
184 LLVMAddConstantPropagationPass(screen->pass);
185 if(util_cpu_caps.has_sse4_1) {
186 /* FIXME: There is a bug in this pass, whereby the combination of fptosi
187 * and sitofp (necessary for trunc/floor/ceil/round implementation)
188 * somehow becomes invalid code.
189 */
190 LLVMAddInstructionCombiningPass(screen->pass);
191 }
192 LLVMAddPromoteMemoryToRegisterPass(screen->pass);
193 LLVMAddGVNPass(screen->pass);
194 LLVMAddCFGSimplificationPass(screen->pass);
195 }
196
197 lp_jit_init_globals(screen);
198 }