Merge branch '7.8'
[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[LP_JIT_TEXTURE_NUM_FIELDS];
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_LEVELS);
62 elem_types[LP_JIT_TEXTURE_IMG_STRIDE] =
63 LLVMArrayType(LLVMInt32Type(), LP_MAX_TEXTURE_LEVELS);
64 elem_types[LP_JIT_TEXTURE_DATA] =
65 LLVMArrayType(LLVMPointerType(LLVMInt8Type(), 0),
66 LP_MAX_TEXTURE_LEVELS);
67
68 texture_type = LLVMStructType(elem_types, Elements(elem_types), 0);
69
70 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, width,
71 screen->target, texture_type,
72 LP_JIT_TEXTURE_WIDTH);
73 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, height,
74 screen->target, texture_type,
75 LP_JIT_TEXTURE_HEIGHT);
76 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, depth,
77 screen->target, texture_type,
78 LP_JIT_TEXTURE_DEPTH);
79 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, last_level,
80 screen->target, texture_type,
81 LP_JIT_TEXTURE_LAST_LEVEL);
82 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, row_stride,
83 screen->target, texture_type,
84 LP_JIT_TEXTURE_ROW_STRIDE);
85 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, img_stride,
86 screen->target, texture_type,
87 LP_JIT_TEXTURE_IMG_STRIDE);
88 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, data,
89 screen->target, texture_type,
90 LP_JIT_TEXTURE_DATA);
91 LP_CHECK_STRUCT_SIZE(struct lp_jit_texture,
92 screen->target, texture_type);
93
94 LLVMAddTypeName(screen->module, "texture", texture_type);
95 }
96
97 /* struct lp_jit_context */
98 {
99 LLVMTypeRef elem_types[LP_JIT_CTX_COUNT];
100 LLVMTypeRef context_type;
101
102 elem_types[LP_JIT_CTX_CONSTANTS] = LLVMPointerType(LLVMFloatType(), 0);
103 elem_types[LP_JIT_CTX_ALPHA_REF] = LLVMFloatType();
104 elem_types[LP_JIT_CTX_STENCIL_REF_FRONT] = LLVMInt32Type();
105 elem_types[LP_JIT_CTX_STENCIL_REF_BACK] = LLVMInt32Type();
106 elem_types[LP_JIT_CTX_SCISSOR_XMIN] = LLVMFloatType();
107 elem_types[LP_JIT_CTX_SCISSOR_YMIN] = LLVMFloatType();
108 elem_types[LP_JIT_CTX_SCISSOR_XMAX] = LLVMFloatType();
109 elem_types[LP_JIT_CTX_SCISSOR_YMAX] = LLVMFloatType();
110 elem_types[LP_JIT_CTX_BLEND_COLOR] = LLVMPointerType(LLVMInt8Type(), 0);
111 elem_types[LP_JIT_CTX_TEXTURES] = LLVMArrayType(texture_type,
112 PIPE_MAX_SAMPLERS);
113
114 context_type = LLVMStructType(elem_types, Elements(elem_types), 0);
115
116 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, constants,
117 screen->target, context_type,
118 LP_JIT_CTX_CONSTANTS);
119 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, alpha_ref_value,
120 screen->target, context_type,
121 LP_JIT_CTX_ALPHA_REF);
122 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, stencil_ref_front,
123 screen->target, context_type,
124 LP_JIT_CTX_STENCIL_REF_FRONT);
125 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, stencil_ref_back,
126 screen->target, context_type,
127 LP_JIT_CTX_STENCIL_REF_BACK);
128 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, scissor_xmin,
129 screen->target, context_type,
130 LP_JIT_CTX_SCISSOR_XMIN);
131 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, scissor_ymin,
132 screen->target, context_type,
133 LP_JIT_CTX_SCISSOR_YMIN);
134 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, scissor_xmax,
135 screen->target, context_type,
136 LP_JIT_CTX_SCISSOR_XMAX);
137 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, scissor_ymax,
138 screen->target, context_type,
139 LP_JIT_CTX_SCISSOR_YMAX);
140 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, blend_color,
141 screen->target, context_type,
142 LP_JIT_CTX_BLEND_COLOR);
143 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, textures,
144 screen->target, context_type,
145 LP_JIT_CTX_TEXTURES);
146 LP_CHECK_STRUCT_SIZE(struct lp_jit_context,
147 screen->target, context_type);
148
149 LLVMAddTypeName(screen->module, "context", context_type);
150
151 screen->context_ptr_type = LLVMPointerType(context_type, 0);
152 }
153
154 #ifdef DEBUG
155 LLVMDumpModule(screen->module);
156 #endif
157 }
158
159
160 void
161 lp_jit_screen_cleanup(struct llvmpipe_screen *screen)
162 {
163 if(screen->engine)
164 LLVMDisposeExecutionEngine(screen->engine);
165
166 if(screen->pass)
167 LLVMDisposePassManager(screen->pass);
168 }
169
170
171 void
172 lp_jit_screen_init(struct llvmpipe_screen *screen)
173 {
174 lp_build_init();
175
176 screen->module = lp_build_module;
177 screen->provider = lp_build_provider;
178 screen->engine = lp_build_engine;
179 screen->target = lp_build_target;
180
181 screen->pass = LLVMCreateFunctionPassManager(screen->provider);
182 LLVMAddTargetData(screen->target, screen->pass);
183
184 if ((LP_DEBUG & DEBUG_NO_LLVM_OPT) == 0) {
185 /* These are the passes currently listed in llvm-c/Transforms/Scalar.h,
186 * but there are more on SVN. */
187 /* TODO: Add more passes */
188 LLVMAddConstantPropagationPass(screen->pass);
189 if(util_cpu_caps.has_sse4_1) {
190 /* FIXME: There is a bug in this pass, whereby the combination of fptosi
191 * and sitofp (necessary for trunc/floor/ceil/round implementation)
192 * somehow becomes invalid code.
193 */
194 LLVMAddInstructionCombiningPass(screen->pass);
195 }
196 LLVMAddPromoteMemoryToRegisterPass(screen->pass);
197 LLVMAddGVNPass(screen->pass);
198 LLVMAddCFGSimplificationPass(screen->pass);
199 }
200
201 lp_jit_init_globals(screen);
202 }