bc9e6ac1bd7b3414dda4eabe64156329cf6b68b0
[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 "lp_screen.h"
41 #include "lp_bld_intr.h"
42 #include "lp_bld_misc.h"
43 #include "lp_jit.h"
44
45
46 static void
47 lp_jit_init_globals(struct llvmpipe_screen *screen)
48 {
49 LLVMTypeRef texture_type;
50
51 /* struct lp_jit_texture */
52 {
53 LLVMTypeRef elem_types[4];
54
55 elem_types[LP_JIT_TEXTURE_WIDTH] = LLVMInt32Type();
56 elem_types[LP_JIT_TEXTURE_HEIGHT] = LLVMInt32Type();
57 elem_types[LP_JIT_TEXTURE_STRIDE] = LLVMInt32Type();
58 elem_types[LP_JIT_TEXTURE_DATA] = LLVMPointerType(LLVMInt8Type(), 0);
59
60 texture_type = LLVMStructType(elem_types, Elements(elem_types), 0);
61
62 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, width,
63 screen->target, texture_type,
64 LP_JIT_TEXTURE_WIDTH);
65 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, height,
66 screen->target, texture_type,
67 LP_JIT_TEXTURE_HEIGHT);
68 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, stride,
69 screen->target, texture_type,
70 LP_JIT_TEXTURE_STRIDE);
71 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, data,
72 screen->target, texture_type,
73 LP_JIT_TEXTURE_DATA);
74 LP_CHECK_STRUCT_SIZE(struct lp_jit_texture,
75 screen->target, texture_type);
76
77 LLVMAddTypeName(screen->module, "texture", texture_type);
78 }
79
80 /* struct lp_jit_context */
81 {
82 LLVMTypeRef elem_types[5];
83 LLVMTypeRef context_type;
84
85 elem_types[0] = LLVMPointerType(LLVMFloatType(), 0); /* constants */
86 elem_types[1] = LLVMPointerType(LLVMInt8Type(), 0); /* samplers */
87 elem_types[2] = LLVMFloatType(); /* alpha_ref_value */
88 elem_types[3] = LLVMPointerType(LLVMInt8Type(), 0); /* blend_color */
89 elem_types[4] = LLVMArrayType(texture_type, PIPE_MAX_SAMPLERS); /* textures */
90
91 context_type = LLVMStructType(elem_types, Elements(elem_types), 0);
92
93 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, constants,
94 screen->target, context_type, 0);
95 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, samplers,
96 screen->target, context_type, 1);
97 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, alpha_ref_value,
98 screen->target, context_type, 2);
99 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, blend_color,
100 screen->target, context_type, 3);
101 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, textures,
102 screen->target, context_type,
103 LP_JIT_CONTEXT_TEXTURES_INDEX);
104 LP_CHECK_STRUCT_SIZE(struct lp_jit_context,
105 screen->target, context_type);
106
107 LLVMAddTypeName(screen->module, "context", context_type);
108
109 screen->context_ptr_type = LLVMPointerType(context_type, 0);
110 }
111
112 #ifdef DEBUG
113 LLVMDumpModule(screen->module);
114 #endif
115 }
116
117
118 void
119 lp_jit_screen_cleanup(struct llvmpipe_screen *screen)
120 {
121 if(screen->engine)
122 LLVMDisposeExecutionEngine(screen->engine);
123
124 if(screen->pass)
125 LLVMDisposePassManager(screen->pass);
126 }
127
128
129 void
130 lp_jit_screen_init(struct llvmpipe_screen *screen)
131 {
132 char *error = NULL;
133
134 util_cpu_detect();
135
136 #if 0
137 /* For simulating less capable machines */
138 util_cpu_caps.has_sse3 = 0;
139 util_cpu_caps.has_ssse3 = 0;
140 util_cpu_caps.has_sse4_1 = 0;
141 #endif
142
143 LLVMLinkInJIT();
144 LLVMInitializeNativeTarget();
145
146 screen->module = LLVMModuleCreateWithName("llvmpipe");
147
148 screen->provider = LLVMCreateModuleProviderForExistingModule(screen->module);
149
150 if (LLVMCreateJITCompiler(&screen->engine, screen->provider, 1, &error)) {
151 _debug_printf("%s\n", error);
152 LLVMDisposeMessage(error);
153 assert(0);
154 }
155
156 screen->target = LLVMGetExecutionEngineTargetData(screen->engine);
157
158 screen->pass = LLVMCreateFunctionPassManager(screen->provider);
159 LLVMAddTargetData(screen->target, screen->pass);
160 /* These are the passes currently listed in llvm-c/Transforms/Scalar.h,
161 * but there are more on SVN. */
162 /* TODO: Add more passes */
163 LLVMAddConstantPropagationPass(screen->pass);
164 if(util_cpu_caps.has_sse4_1) {
165 /* FIXME: There is a bug in this pass, whereby the combination of fptosi
166 * and sitofp (necessary for trunc/floor/ceil/round implementation)
167 * somehow becomes invalid code.
168 */
169 LLVMAddInstructionCombiningPass(screen->pass);
170 }
171 LLVMAddPromoteMemoryToRegisterPass(screen->pass);
172 LLVMAddGVNPass(screen->pass);
173 LLVMAddCFGSimplificationPass(screen->pass);
174
175 lp_jit_init_globals(screen);
176 }