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