llvmpipe: Pass fragment context to generated function in a single structure.
[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_jit.h"
41
42
43 static void
44 lp_jit_init_types(struct llvmpipe_screen *screen)
45 {
46 /* struct lp_jit_context */
47 {
48 LLVMTypeRef elem_types[2];
49 LLVMTypeRef context_type;
50
51 elem_types[0] = LLVMPointerType(LLVMFloatType(), 0); /* constants */
52 elem_types[1] = LLVMPointerType(LLVMInt8Type(), 0); /* samplers */
53
54 context_type = LLVMStructType(elem_types, Elements(elem_types), 0);
55
56 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, constants,
57 screen->target, context_type, 0);
58 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, samplers,
59 screen->target, context_type, 1);
60 LP_CHECK_STRUCT_SIZE(struct lp_jit_context,
61 screen->target, context_type);
62
63 LLVMAddTypeName(screen->module, "context", context_type);
64
65 screen->context_ptr_type = LLVMPointerType(context_type, 0);
66 }
67
68 #ifdef DEBUG
69 LLVMDumpModule(screen->module);
70 #endif
71 }
72
73
74 void
75 lp_jit_screen_cleanup(struct llvmpipe_screen *screen)
76 {
77 if(screen->engine)
78 LLVMDisposeExecutionEngine(screen->engine);
79
80 if(screen->pass)
81 LLVMDisposePassManager(screen->pass);
82 }
83
84
85 void
86 lp_jit_screen_init(struct llvmpipe_screen *screen)
87 {
88 char *error = NULL;
89
90 screen->module = LLVMModuleCreateWithName("llvmpipe");
91
92 screen->provider = LLVMCreateModuleProviderForExistingModule(screen->module);
93
94 if (LLVMCreateJITCompiler(&screen->engine, screen->provider, 1, &error)) {
95 fprintf(stderr, "%s\n", error);
96 LLVMDisposeMessage(error);
97 abort();
98 }
99
100 screen->target = LLVMGetExecutionEngineTargetData(screen->engine);
101
102 screen->pass = LLVMCreateFunctionPassManager(screen->provider);
103 LLVMAddTargetData(screen->target, screen->pass);
104 /* These are the passes currently listed in llvm-c/Transforms/Scalar.h,
105 * but there are more on SVN. */
106 LLVMAddConstantPropagationPass(screen->pass);
107 LLVMAddInstructionCombiningPass(screen->pass);
108 LLVMAddPromoteMemoryToRegisterPass(screen->pass);
109 LLVMAddGVNPass(screen->pass);
110 LLVMAddCFGSimplificationPass(screen->pass);
111
112 lp_jit_init_types(screen);
113 }