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