e64bfb1c6cb828ed1a87c45b94e8bfd8d3c3c7eb
[mesa.git] / src / gallium / auxiliary / gallivm / gallivm_cpu.cpp
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 * Authors:
30 * Zack Rusin zack@tungstengraphics.com
31 */
32 #ifdef MESA_LLVM
33
34 #include "gallivm.h"
35 #include "gallivm_p.h"
36
37 #include "instructions.h"
38 #include "loweringpass.h"
39 #include "storage.h"
40 #include "tgsitollvm.h"
41
42 #include "pipe/p_context.h"
43 #include "pipe/p_shader_tokens.h"
44
45 #include "tgsi/tgsi_exec.h"
46 #include "tgsi/tgsi_dump.h"
47
48 #include "util/u_memory.h"
49
50 #include <llvm/Module.h>
51 #include <llvm/CallingConv.h>
52 #include <llvm/Constants.h>
53 #include <llvm/DerivedTypes.h>
54 #include <llvm/Instructions.h>
55 #include <llvm/ModuleProvider.h>
56 #include <llvm/Pass.h>
57 #include <llvm/PassManager.h>
58 #include <llvm/ParameterAttributes.h>
59 #include <llvm/Support/PatternMatch.h>
60 #include <llvm/ExecutionEngine/JIT.h>
61 #include <llvm/ExecutionEngine/Interpreter.h>
62 #include <llvm/ExecutionEngine/GenericValue.h>
63 #include <llvm/Support/MemoryBuffer.h>
64 #include <llvm/LinkAllPasses.h>
65 #include <llvm/Analysis/Verifier.h>
66 #include <llvm/Analysis/LoopPass.h>
67 #include <llvm/Target/TargetData.h>
68 #include <llvm/Bitcode/ReaderWriter.h>
69 #include <llvm/Transforms/Utils/Cloning.h>
70
71 #include <sstream>
72 #include <fstream>
73 #include <iostream>
74
75 struct gallivm_cpu_engine {
76 llvm::ExecutionEngine *engine;
77 };
78
79 static struct gallivm_cpu_engine *CPU = 0;
80
81 typedef int (*fragment_shader_runner)(float x, float y,
82 float (*dests)[16][4],
83 float (*inputs)[16][4],
84 int num_attribs,
85 float (*consts)[4], int num_consts,
86 struct tgsi_sampler *samplers);
87
88 int gallivm_cpu_fs_exec(struct gallivm_prog *prog,
89 float fx, float fy,
90 float (*dests)[16][4],
91 float (*inputs)[16][4],
92 float (*consts)[4],
93 struct tgsi_sampler *samplers)
94 {
95 fragment_shader_runner runner = reinterpret_cast<fragment_shader_runner>(prog->function);
96 assert(runner);
97
98 return runner(fx, fy, dests, inputs, prog->num_interp,
99 consts, prog->num_consts,
100 samplers);
101 }
102
103 static inline llvm::Function *func_for_shader(struct gallivm_prog *prog)
104 {
105 llvm::Module *mod = prog->module;
106 llvm::Function *func = 0;
107
108 switch (prog->type) {
109 case GALLIVM_VS:
110 func = mod->getFunction("vs_shader");
111 break;
112 case GALLIVM_FS:
113 func = mod->getFunction("fs_shader");
114 break;
115 default:
116 assert(!"Unknown shader type!");
117 break;
118 }
119 return func;
120 }
121
122 /*!
123 This function creates a CPU based execution engine for the given gallivm_prog.
124 gallivm_cpu_engine should be used as a singleton throughout the library. Before
125 executing gallivm_prog_exec one needs to call gallivm_cpu_jit_compile.
126 The gallivm_prog instance which is being passed to the constructor is being
127 automatically JIT compiled so one shouldn't call gallivm_cpu_jit_compile
128 with it again.
129 */
130 struct gallivm_cpu_engine * gallivm_cpu_engine_create(struct gallivm_prog *prog)
131 {
132 struct gallivm_cpu_engine *cpu = (struct gallivm_cpu_engine *)
133 calloc(1, sizeof(struct gallivm_cpu_engine));
134 llvm::Module *mod = static_cast<llvm::Module*>(prog->module);
135 llvm::ExistingModuleProvider *mp = new llvm::ExistingModuleProvider(mod);
136 llvm::ExecutionEngine *ee = llvm::ExecutionEngine::create(mp, false);
137 ee->DisableLazyCompilation();
138 cpu->engine = ee;
139
140 llvm::Function *func = func_for_shader(prog);
141
142 prog->function = ee->getPointerToFunction(func);
143 CPU = cpu;
144 return cpu;
145 }
146
147
148 /*!
149 This function JIT compiles the given gallivm_prog with the given cpu based execution engine.
150 The reference to the generated machine code entry point will be stored
151 in the gallivm_prog program. After executing this function one can call gallivm_prog_exec
152 in order to execute the gallivm_prog on the CPU.
153 */
154 void gallivm_cpu_jit_compile(struct gallivm_cpu_engine *cpu, struct gallivm_prog *prog)
155 {
156 llvm::Module *mod = static_cast<llvm::Module*>(prog->module);
157 llvm::ExistingModuleProvider *mp = new llvm::ExistingModuleProvider(mod);
158 llvm::ExecutionEngine *ee = cpu->engine;
159 assert(ee);
160 /*FIXME : remove */
161 ee->DisableLazyCompilation();
162 ee->addModuleProvider(mp);
163
164 llvm::Function *func = func_for_shader(prog);
165 prog->function = ee->getPointerToFunction(func);
166 }
167
168 void gallivm_cpu_engine_delete(struct gallivm_cpu_engine *cpu)
169 {
170 free(cpu);
171 }
172
173 struct gallivm_cpu_engine * gallivm_global_cpu_engine()
174 {
175 return CPU;
176 }
177
178
179 typedef void (*vertex_shader_runner)(void *ainputs,
180 void *dests,
181 float (*aconsts)[4],
182 void *temps);
183
184 #define MAX_TGSI_VERTICES 4
185 /*!
186 This function is used to execute the gallivm_prog in software. Before calling
187 this function the gallivm_prog has to be JIT compiled with the gallivm_cpu_jit_compile
188 function.
189 */
190 int gallivm_cpu_vs_exec(struct gallivm_prog *prog,
191 struct tgsi_exec_machine *machine,
192 const float (*input)[4],
193 unsigned num_inputs,
194 float (*output)[4],
195 unsigned num_outputs,
196 const float (*constants)[4],
197 unsigned count,
198 unsigned input_stride,
199 unsigned output_stride )
200 {
201 unsigned int i, j;
202 unsigned slot;
203 vertex_shader_runner runner = reinterpret_cast<vertex_shader_runner>(prog->function);
204
205 assert(runner);
206
207 for (i = 0; i < count; i += MAX_TGSI_VERTICES) {
208 unsigned int max_vertices = MIN2(MAX_TGSI_VERTICES, count - i);
209
210 /* Swizzle inputs.
211 */
212 for (j = 0; j < max_vertices; j++) {
213 for (slot = 0; slot < num_inputs; slot++) {
214 machine->Inputs[slot].xyzw[0].f[j] = input[slot][0];
215 machine->Inputs[slot].xyzw[1].f[j] = input[slot][1];
216 machine->Inputs[slot].xyzw[2].f[j] = input[slot][2];
217 machine->Inputs[slot].xyzw[3].f[j] = input[slot][3];
218 }
219
220 input = (const float (*)[4])((const char *)input + input_stride);
221 }
222
223 /* run shader */
224 runner(machine->Inputs,
225 machine->Outputs,
226 (float (*)[4]) constants,
227 machine->Temps);
228
229 /* Unswizzle all output results
230 */
231 for (j = 0; j < max_vertices; j++) {
232 for (slot = 0; slot < num_outputs; slot++) {
233 output[slot][0] = machine->Outputs[slot].xyzw[0].f[j];
234 output[slot][1] = machine->Outputs[slot].xyzw[1].f[j];
235 output[slot][2] = machine->Outputs[slot].xyzw[2].f[j];
236 output[slot][3] = machine->Outputs[slot].xyzw[3].f[j];
237 }
238 output = (float (*)[4])((char *)output + output_stride);
239 }
240 }
241
242 return 0;
243 }
244
245 #endif