José Fonseca | 08dd41f | 2009-08-23 05:52:20 +0100 | [diff] [blame] | 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 | |
José Fonseca | c022e15 | 2009-08-23 06:35:09 +0100 | [diff] [blame] | 38 | #include "util/u_memory.h" |
José Fonseca | 7cda8ea | 2009-09-29 13:58:58 +0100 | [diff] [blame] | 39 | #include "util/u_cpu_detect.h" |
Brian Paul | 36a0819 | 2010-01-27 17:16:42 -0700 | [diff] [blame] | 40 | #include "lp_debug.h" |
José Fonseca | 08dd41f | 2009-08-23 05:52:20 +0100 | [diff] [blame] | 41 | #include "lp_screen.h" |
Zack Rusin | c61bf36 | 2010-02-08 18:05:22 -0500 | [diff] [blame^] | 42 | #include "gallivm/lp_bld_intr.h" |
José Fonseca | 08dd41f | 2009-08-23 05:52:20 +0100 | [diff] [blame] | 43 | #include "lp_jit.h" |
| 44 | |
| 45 | |
José Fonseca | c022e15 | 2009-08-23 06:35:09 +0100 | [diff] [blame] | 46 | static void |
José Fonseca | 03180dc | 2009-08-23 07:55:29 +0100 | [diff] [blame] | 47 | lp_jit_init_globals(struct llvmpipe_screen *screen) |
José Fonseca | c022e15 | 2009-08-23 06:35:09 +0100 | [diff] [blame] | 48 | { |
José Fonseca | e4c76c0 | 2009-09-07 14:52:39 +0100 | [diff] [blame] | 49 | LLVMTypeRef texture_type; |
| 50 | |
| 51 | /* struct lp_jit_texture */ |
José Fonseca | c022e15 | 2009-08-23 06:35:09 +0100 | [diff] [blame] | 52 | { |
José Fonseca | 635c37e | 2009-08-23 07:38:41 +0100 | [diff] [blame] | 53 | LLVMTypeRef elem_types[4]; |
José Fonseca | e4c76c0 | 2009-09-07 14:52:39 +0100 | [diff] [blame] | 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 | { |
Brian Paul | 4461442 | 2010-01-14 19:15:00 -0700 | [diff] [blame] | 82 | LLVMTypeRef elem_types[8]; |
José Fonseca | c022e15 | 2009-08-23 06:35:09 +0100 | [diff] [blame] | 83 | LLVMTypeRef context_type; |
| 84 | |
| 85 | elem_types[0] = LLVMPointerType(LLVMFloatType(), 0); /* constants */ |
Brian Paul | 4461442 | 2010-01-14 19:15:00 -0700 | [diff] [blame] | 86 | elem_types[1] = LLVMFloatType(); /* alpha_ref_value */ elem_types[2] = LLVMFloatType(); /* scissor_xmin */ |
| 87 | elem_types[3] = LLVMFloatType(); /* scissor_ymin */ |
| 88 | elem_types[4] = LLVMFloatType(); /* scissor_xmax */ |
| 89 | elem_types[5] = LLVMFloatType(); /* scissor_ymax */ |
| 90 | elem_types[6] = LLVMPointerType(LLVMInt8Type(), 0); /* blend_color */ |
| 91 | elem_types[7] = LLVMArrayType(texture_type, PIPE_MAX_SAMPLERS); /* textures */ |
José Fonseca | c022e15 | 2009-08-23 06:35:09 +0100 | [diff] [blame] | 92 | |
| 93 | context_type = LLVMStructType(elem_types, Elements(elem_types), 0); |
| 94 | |
| 95 | LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, constants, |
| 96 | screen->target, context_type, 0); |
José Fonseca | 635c37e | 2009-08-23 07:38:41 +0100 | [diff] [blame] | 97 | LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, alpha_ref_value, |
José Fonseca | 8081c1e | 2010-01-07 16:16:45 +0000 | [diff] [blame] | 98 | screen->target, context_type, 1); |
Brian Paul | 4461442 | 2010-01-14 19:15:00 -0700 | [diff] [blame] | 99 | LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, scissor_xmin, |
José Fonseca | 8081c1e | 2010-01-07 16:16:45 +0000 | [diff] [blame] | 100 | screen->target, context_type, 2); |
Brian Paul | 4461442 | 2010-01-14 19:15:00 -0700 | [diff] [blame] | 101 | LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, scissor_ymin, |
| 102 | screen->target, context_type, 3); |
| 103 | LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, scissor_xmax, |
| 104 | screen->target, context_type, 4); |
| 105 | LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, scissor_ymax, |
| 106 | screen->target, context_type, 5); |
| 107 | LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, blend_color, |
| 108 | screen->target, context_type, 6); |
José Fonseca | e4c76c0 | 2009-09-07 14:52:39 +0100 | [diff] [blame] | 109 | LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, textures, |
| 110 | screen->target, context_type, |
| 111 | LP_JIT_CONTEXT_TEXTURES_INDEX); |
José Fonseca | c022e15 | 2009-08-23 06:35:09 +0100 | [diff] [blame] | 112 | LP_CHECK_STRUCT_SIZE(struct lp_jit_context, |
| 113 | screen->target, context_type); |
| 114 | |
| 115 | LLVMAddTypeName(screen->module, "context", context_type); |
| 116 | |
| 117 | screen->context_ptr_type = LLVMPointerType(context_type, 0); |
| 118 | } |
| 119 | |
| 120 | #ifdef DEBUG |
| 121 | LLVMDumpModule(screen->module); |
| 122 | #endif |
| 123 | } |
| 124 | |
| 125 | |
José Fonseca | 08dd41f | 2009-08-23 05:52:20 +0100 | [diff] [blame] | 126 | void |
| 127 | lp_jit_screen_cleanup(struct llvmpipe_screen *screen) |
| 128 | { |
| 129 | if(screen->engine) |
| 130 | LLVMDisposeExecutionEngine(screen->engine); |
| 131 | |
| 132 | if(screen->pass) |
| 133 | LLVMDisposePassManager(screen->pass); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | void |
| 138 | lp_jit_screen_init(struct llvmpipe_screen *screen) |
| 139 | { |
| 140 | char *error = NULL; |
| 141 | |
José Fonseca | 7cda8ea | 2009-09-29 13:58:58 +0100 | [diff] [blame] | 142 | util_cpu_detect(); |
| 143 | |
José Fonseca | baddcbc | 2009-09-29 17:26:20 +0100 | [diff] [blame] | 144 | #if 0 |
| 145 | /* For simulating less capable machines */ |
| 146 | util_cpu_caps.has_sse3 = 0; |
José Fonseca | 953b74d | 2009-11-26 11:16:19 +0000 | [diff] [blame] | 147 | util_cpu_caps.has_ssse3 = 0; |
José Fonseca | baddcbc | 2009-09-29 17:26:20 +0100 | [diff] [blame] | 148 | util_cpu_caps.has_sse4_1 = 0; |
| 149 | #endif |
| 150 | |
José Fonseca | 1df539c | 2009-09-26 09:33:32 +0100 | [diff] [blame] | 151 | LLVMLinkInJIT(); |
| 152 | LLVMInitializeNativeTarget(); |
José Fonseca | 1df539c | 2009-09-26 09:33:32 +0100 | [diff] [blame] | 153 | |
José Fonseca | 08dd41f | 2009-08-23 05:52:20 +0100 | [diff] [blame] | 154 | screen->module = LLVMModuleCreateWithName("llvmpipe"); |
| 155 | |
| 156 | screen->provider = LLVMCreateModuleProviderForExistingModule(screen->module); |
| 157 | |
| 158 | if (LLVMCreateJITCompiler(&screen->engine, screen->provider, 1, &error)) { |
José Fonseca | 459ea00 | 2009-09-16 10:39:06 +0100 | [diff] [blame] | 159 | _debug_printf("%s\n", error); |
José Fonseca | 08dd41f | 2009-08-23 05:52:20 +0100 | [diff] [blame] | 160 | LLVMDisposeMessage(error); |
José Fonseca | 4ae3e88 | 2009-11-23 11:21:11 +0000 | [diff] [blame] | 161 | assert(0); |
José Fonseca | 08dd41f | 2009-08-23 05:52:20 +0100 | [diff] [blame] | 162 | } |
| 163 | |
José Fonseca | c022e15 | 2009-08-23 06:35:09 +0100 | [diff] [blame] | 164 | screen->target = LLVMGetExecutionEngineTargetData(screen->engine); |
| 165 | |
José Fonseca | 08dd41f | 2009-08-23 05:52:20 +0100 | [diff] [blame] | 166 | screen->pass = LLVMCreateFunctionPassManager(screen->provider); |
José Fonseca | c022e15 | 2009-08-23 06:35:09 +0100 | [diff] [blame] | 167 | LLVMAddTargetData(screen->target, screen->pass); |
Brian Paul | 36a0819 | 2010-01-27 17:16:42 -0700 | [diff] [blame] | 168 | |
| 169 | if ((LP_DEBUG & DEBUG_NO_LLVM_OPT) == 0) { |
| 170 | /* These are the passes currently listed in llvm-c/Transforms/Scalar.h, |
| 171 | * but there are more on SVN. */ |
| 172 | /* TODO: Add more passes */ |
| 173 | LLVMAddConstantPropagationPass(screen->pass); |
| 174 | if(util_cpu_caps.has_sse4_1) { |
| 175 | /* FIXME: There is a bug in this pass, whereby the combination of fptosi |
| 176 | * and sitofp (necessary for trunc/floor/ceil/round implementation) |
| 177 | * somehow becomes invalid code. |
| 178 | */ |
| 179 | LLVMAddInstructionCombiningPass(screen->pass); |
| 180 | } |
| 181 | LLVMAddPromoteMemoryToRegisterPass(screen->pass); |
| 182 | LLVMAddGVNPass(screen->pass); |
| 183 | LLVMAddCFGSimplificationPass(screen->pass); |
José Fonseca | baddcbc | 2009-09-29 17:26:20 +0100 | [diff] [blame] | 184 | } |
José Fonseca | c022e15 | 2009-08-23 06:35:09 +0100 | [diff] [blame] | 185 | |
José Fonseca | 03180dc | 2009-08-23 07:55:29 +0100 | [diff] [blame] | 186 | lp_jit_init_globals(screen); |
José Fonseca | 08dd41f | 2009-08-23 05:52:20 +0100 | [diff] [blame] | 187 | } |