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