Mercurial > octave-nkf
annotate src/jit-typeinfo.cc @ 15078:fe4752f772e2
Generate ND indexing functions on demand in JIT.
* src/jit-typeinfo.cc (jit_operation::~jit_operation,
jit_operation::do_generate, jit_operation::generate,
jit_operation::signature_cmp::operator()): New function.
(jit_operation::overload): Call do_generate when lookup fails.
(jit_index_operation, jit_paren_subsref, jit_paren_subsasgn): New class.
(jit_typeinfo::jit_typeinfo): Update to use jit_paren_subsref and
jit_paren_subsasgn.
(jit_typeinfo::gen_subsref, jit_typeinfo::gen_subsasgn): Removed functions.
* src/jit-typeinfo.h (jit_operation::~jit_operation, jit_operation::generate,
jit_operation::do_generate): New declaration.
(jit_operation::add_overload, jit_operation::overload, jit_operation::result,
jit_operation::to_idx): Use signature_vec typedef.
(jit_operation::singature_cmp): New class.
(jit_index_operation, jit_paren_subsref, jit_paren_subsasgn): New class.
(jit_typeinfo::get_scalar_ptr): Nwe function.
(jit_typeinfo::gen_subsref, jit_typeinfo::gen_subsasgn): Removed declaration.
* src/pt-jit.cc: New test.
author | Max Brister <max@2bass.com> |
---|---|
date | Wed, 01 Aug 2012 17:00:12 -0500 |
parents | f57d7578c1a6 |
children | 9df70a18aa27 |
rev | line source |
---|---|
15016 | 1 /* |
2 | |
3 Copyright (C) 2012 Max Brister <max@2bass.com> | |
4 | |
5 This file is part of Octave. | |
6 | |
7 Octave is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
9 Free Software Foundation; either version 3 of the License, or (at your | |
10 option) any later version. | |
11 | |
12 Octave is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
18 along with Octave; see the file COPYING. If not, see | |
19 <http://www.gnu.org/licenses/>. | |
20 | |
21 */ | |
22 | |
23 // defines required by llvm | |
24 #define __STDC_LIMIT_MACROS | |
25 #define __STDC_CONSTANT_MACROS | |
26 | |
27 #ifdef HAVE_CONFIG_H | |
28 #include <config.h> | |
29 #endif | |
30 | |
31 #ifdef HAVE_LLVM | |
32 | |
33 #include "jit-typeinfo.h" | |
34 | |
35 #include <llvm/Analysis/Verifier.h> | |
36 #include <llvm/GlobalVariable.h> | |
37 #include <llvm/ExecutionEngine/ExecutionEngine.h> | |
38 #include <llvm/LLVMContext.h> | |
39 #include <llvm/Function.h> | |
40 #include <llvm/Instructions.h> | |
41 #include <llvm/Intrinsics.h> | |
42 #include <llvm/Support/IRBuilder.h> | |
43 #include <llvm/Support/raw_os_ostream.h> | |
44 | |
45 #include "jit-ir.h" | |
46 #include "ov.h" | |
47 #include "ov-builtin.h" | |
48 #include "ov-complex.h" | |
49 #include "ov-scalar.h" | |
50 #include "pager.h" | |
51 | |
52 static llvm::LLVMContext& context = llvm::getGlobalContext (); | |
53 | |
54 jit_typeinfo *jit_typeinfo::instance = 0; | |
55 | |
56 std::ostream& jit_print (std::ostream& os, jit_type *atype) | |
57 { | |
58 if (! atype) | |
59 return os << "null"; | |
60 return os << atype->name (); | |
61 } | |
62 | |
63 // function that jit code calls | |
64 extern "C" void | |
65 octave_jit_print_any (const char *name, octave_base_value *obv) | |
66 { | |
67 obv->print_with_name (octave_stdout, name, true); | |
68 } | |
69 | |
70 extern "C" void | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
71 octave_jit_print_scalar (const char *name, double value) |
15016 | 72 { |
73 // FIXME: We should avoid allocating a new octave_scalar each time | |
74 octave_value ov (value); | |
75 ov.print_with_name (octave_stdout, name); | |
76 } | |
77 | |
78 extern "C" octave_base_value* | |
79 octave_jit_binary_any_any (octave_value::binary_op op, octave_base_value *lhs, | |
80 octave_base_value *rhs) | |
81 { | |
82 octave_value olhs (lhs, true); | |
83 octave_value orhs (rhs, true); | |
84 octave_value result = do_binary_op (op, olhs, orhs); | |
85 octave_base_value *rep = result.internal_rep (); | |
86 rep->grab (); | |
87 return rep; | |
88 } | |
89 | |
90 extern "C" octave_idx_type | |
91 octave_jit_compute_nelem (double base, double limit, double inc) | |
92 { | |
93 Range rng = Range (base, limit, inc); | |
94 return rng.nelem (); | |
95 } | |
96 | |
97 extern "C" void | |
98 octave_jit_release_any (octave_base_value *obv) | |
99 { | |
100 obv->release (); | |
101 } | |
102 | |
103 extern "C" void | |
104 octave_jit_release_matrix (jit_matrix *m) | |
105 { | |
106 delete m->array; | |
107 } | |
108 | |
109 extern "C" octave_base_value * | |
110 octave_jit_grab_any (octave_base_value *obv) | |
111 { | |
112 obv->grab (); | |
113 return obv; | |
114 } | |
115 | |
116 extern "C" void | |
117 octave_jit_grab_matrix (jit_matrix *result, jit_matrix *m) | |
118 { | |
119 *result = *m->array; | |
120 } | |
121 | |
122 extern "C" octave_base_value * | |
123 octave_jit_cast_any_matrix (jit_matrix *m) | |
124 { | |
125 octave_value ret (*m->array); | |
126 octave_base_value *rep = ret.internal_rep (); | |
127 rep->grab (); | |
128 delete m->array; | |
129 | |
130 return rep; | |
131 } | |
132 | |
133 extern "C" void | |
134 octave_jit_cast_matrix_any (jit_matrix *ret, octave_base_value *obv) | |
135 { | |
136 NDArray m = obv->array_value (); | |
137 *ret = m; | |
138 obv->release (); | |
139 } | |
140 | |
15027
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
141 extern "C" octave_base_value * |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
142 octave_jit_cast_any_range (jit_range *rng) |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
143 { |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
144 Range temp (*rng); |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
145 octave_value ret (temp); |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
146 octave_base_value *rep = ret.internal_rep (); |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
147 rep->grab (); |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
148 |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
149 return rep; |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
150 } |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
151 extern "C" void |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
152 octave_jit_cast_range_any (jit_range *ret, octave_base_value *obv) |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
153 { |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
154 |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
155 jit_range r (obv->range_value ()); |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
156 *ret = r; |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
157 obv->release (); |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
158 } |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
159 |
15016 | 160 extern "C" double |
161 octave_jit_cast_scalar_any (octave_base_value *obv) | |
162 { | |
163 double ret = obv->double_value (); | |
164 obv->release (); | |
165 return ret; | |
166 } | |
167 | |
168 extern "C" octave_base_value * | |
169 octave_jit_cast_any_scalar (double value) | |
170 { | |
171 return new octave_scalar (value); | |
172 } | |
173 | |
174 extern "C" Complex | |
175 octave_jit_cast_complex_any (octave_base_value *obv) | |
176 { | |
177 Complex ret = obv->complex_value (); | |
178 obv->release (); | |
179 return ret; | |
180 } | |
181 | |
182 extern "C" octave_base_value * | |
183 octave_jit_cast_any_complex (Complex c) | |
184 { | |
185 if (c.imag () == 0) | |
186 return new octave_scalar (c.real ()); | |
187 else | |
188 return new octave_complex (c); | |
189 } | |
190 | |
191 extern "C" void | |
192 octave_jit_gripe_nan_to_logical_conversion (void) | |
193 { | |
194 try | |
195 { | |
196 gripe_nan_to_logical_conversion (); | |
197 } | |
198 catch (const octave_execution_exception&) | |
199 { | |
200 gripe_library_execution_error (); | |
201 } | |
202 } | |
203 | |
204 extern "C" void | |
205 octave_jit_ginvalid_index (void) | |
206 { | |
207 try | |
208 { | |
209 gripe_invalid_index (); | |
210 } | |
211 catch (const octave_execution_exception&) | |
212 { | |
213 gripe_library_execution_error (); | |
214 } | |
215 } | |
216 | |
217 extern "C" void | |
218 octave_jit_gindex_range (int nd, int dim, octave_idx_type iext, | |
219 octave_idx_type ext) | |
220 { | |
221 try | |
222 { | |
223 gripe_index_out_of_range (nd, dim, iext, ext); | |
224 } | |
225 catch (const octave_execution_exception&) | |
226 { | |
227 gripe_library_execution_error (); | |
228 } | |
229 } | |
230 | |
231 extern "C" void | |
15027
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
232 octave_jit_paren_subsasgn_impl (jit_matrix *ret, jit_matrix *mat, |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
233 octave_idx_type index, double value) |
15016 | 234 { |
235 NDArray *array = mat->array; | |
236 if (array->nelem () < index) | |
237 array->resize1 (index); | |
238 | |
239 double *data = array->fortran_vec (); | |
240 data[index - 1] = value; | |
241 | |
242 mat->update (); | |
15027
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
243 *ret = *mat; |
15016 | 244 } |
245 | |
15068
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
246 static void |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
247 make_indices (double *indices, octave_idx_type idx_count, |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
248 Array<idx_vector>& result) |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
249 { |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
250 result.resize (dim_vector (1, idx_count)); |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
251 for (octave_idx_type i = 0; i < idx_count; ++i) |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
252 result(i) = idx_vector (indices[i]); |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
253 } |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
254 |
15067 | 255 extern "C" double |
256 octave_jit_paren_scalar (jit_matrix *mat, double *indicies, | |
257 octave_idx_type idx_count) | |
258 { | |
259 // FIXME: Replace this with a more optimal version | |
260 try | |
261 { | |
15068
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
262 Array<idx_vector> idx; |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
263 make_indices (indicies, idx_count, idx); |
15067 | 264 |
265 Array<double> ret = mat->array->index (idx); | |
266 return ret.xelem (0); | |
267 } | |
268 catch (const octave_execution_exception&) | |
269 { | |
270 gripe_library_execution_error (); | |
271 return 0; | |
272 } | |
273 } | |
274 | |
15016 | 275 extern "C" void |
15068
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
276 octave_jit_paren_scalar_subsasgn (jit_matrix *ret, jit_matrix *mat, |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
277 double *indices, octave_idx_type idx_count, |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
278 double value) |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
279 { |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
280 // FIXME: Replace this with a more optimal version |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
281 try |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
282 { |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
283 Array<idx_vector> idx; |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
284 make_indices (indices, idx_count, idx); |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
285 |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
286 Matrix temp (1, 1); |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
287 temp.xelem(0) = value; |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
288 mat->array->assign (idx, temp); |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
289 ret->update (mat->array); |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
290 } |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
291 catch (const octave_execution_exception&) |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
292 { |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
293 gripe_library_execution_error (); |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
294 } |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
295 } |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
296 |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
297 extern "C" void |
15016 | 298 octave_jit_paren_subsasgn_matrix_range (jit_matrix *result, jit_matrix *mat, |
299 jit_range *index, double value) | |
300 { | |
301 NDArray *array = mat->array; | |
302 bool done = false; | |
303 | |
304 // optimize for the simple case (no resizing and no errors) | |
305 if (*array->jit_ref_count () == 1 | |
306 && index->all_elements_are_ints ()) | |
307 { | |
308 // this code is similar to idx_vector::fill, but we avoid allocating an | |
309 // idx_vector and its associated rep | |
310 octave_idx_type start = static_cast<octave_idx_type> (index->base) - 1; | |
311 octave_idx_type step = static_cast<octave_idx_type> (index->inc); | |
312 octave_idx_type nelem = index->nelem; | |
313 octave_idx_type final = start + nelem * step; | |
314 if (step < 0) | |
315 { | |
316 step = -step; | |
317 std::swap (final, start); | |
318 } | |
319 | |
320 if (start >= 0 && final < mat->slice_len) | |
321 { | |
322 done = true; | |
323 | |
324 double *data = array->jit_slice_data (); | |
325 if (step == 1) | |
326 std::fill (data + start, data + start + nelem, value); | |
327 else | |
328 { | |
329 for (octave_idx_type i = start; i < final; i += step) | |
330 data[i] = value; | |
331 } | |
332 } | |
333 } | |
334 | |
335 if (! done) | |
336 { | |
337 idx_vector idx (*index); | |
338 NDArray avalue (dim_vector (1, 1)); | |
339 avalue.xelem (0) = value; | |
340 array->assign (idx, avalue); | |
341 } | |
342 | |
343 result->update (array); | |
344 } | |
345 | |
346 extern "C" Complex | |
347 octave_jit_complex_div (Complex lhs, Complex rhs) | |
348 { | |
349 // see src/OPERATORS/op-cs-cs.cc | |
350 if (rhs == 0.0) | |
351 gripe_divide_by_zero (); | |
352 | |
353 return lhs / rhs; | |
354 } | |
355 | |
356 // FIXME: CP form src/xpow.cc | |
357 static inline int | |
358 xisint (double x) | |
359 { | |
360 return (D_NINT (x) == x | |
361 && ((x >= 0 && x < INT_MAX) | |
362 || (x <= 0 && x > INT_MIN))); | |
363 } | |
364 | |
365 extern "C" Complex | |
366 octave_jit_pow_scalar_scalar (double lhs, double rhs) | |
367 { | |
368 // FIXME: almost CP from src/xpow.cc | |
369 if (lhs < 0.0 && ! xisint (rhs)) | |
370 return std::pow (Complex (lhs), rhs); | |
371 return std::pow (lhs, rhs); | |
372 } | |
373 | |
374 extern "C" Complex | |
375 octave_jit_pow_complex_complex (Complex lhs, Complex rhs) | |
376 { | |
377 if (lhs.imag () == 0 && rhs.imag () == 0) | |
378 return octave_jit_pow_scalar_scalar (lhs.real (), rhs.real ()); | |
379 return std::pow (lhs, rhs); | |
380 } | |
381 | |
382 extern "C" Complex | |
383 octave_jit_pow_complex_scalar (Complex lhs, double rhs) | |
384 { | |
385 if (lhs.imag () == 0) | |
386 return octave_jit_pow_scalar_scalar (lhs.real (), rhs); | |
387 return std::pow (lhs, rhs); | |
388 } | |
389 | |
390 extern "C" Complex | |
391 octave_jit_pow_scalar_complex (double lhs, Complex rhs) | |
392 { | |
393 if (rhs.imag () == 0) | |
394 return octave_jit_pow_scalar_scalar (lhs, rhs.real ()); | |
395 return std::pow (lhs, rhs); | |
396 } | |
397 | |
398 extern "C" void | |
399 octave_jit_print_matrix (jit_matrix *m) | |
400 { | |
401 std::cout << *m << std::endl; | |
402 } | |
403 | |
404 static void | |
405 gripe_bad_result (void) | |
406 { | |
407 error ("incorrect type information given to the JIT compiler"); | |
408 } | |
409 | |
410 // FIXME: Add support for multiple outputs | |
411 extern "C" octave_base_value * | |
412 octave_jit_call (octave_builtin::fcn fn, size_t nargin, | |
413 octave_base_value **argin, jit_type *result_type) | |
414 { | |
415 octave_value_list ovl (nargin); | |
416 for (size_t i = 0; i < nargin; ++i) | |
417 ovl.xelem (i) = octave_value (argin[i]); | |
418 | |
419 ovl = fn (ovl, 1); | |
420 | |
421 // These type checks are not strictly required, but I'm guessing that | |
422 // incorrect types will be entered on occasion. This will be very difficult to | |
423 // debug unless we do the sanity check here. | |
424 if (result_type) | |
425 { | |
426 if (ovl.length () != 1) | |
427 { | |
428 gripe_bad_result (); | |
429 return 0; | |
430 } | |
431 | |
432 octave_value& result = ovl.xelem (0); | |
433 jit_type *jtype = jit_typeinfo::join (jit_typeinfo::type_of (result), | |
434 result_type); | |
435 if (jtype != result_type) | |
436 { | |
437 gripe_bad_result (); | |
438 return 0; | |
439 } | |
440 | |
441 octave_base_value *ret = result.internal_rep (); | |
442 ret->grab (); | |
443 return ret; | |
444 } | |
445 | |
446 if (! (ovl.length () == 0 | |
447 || (ovl.length () == 1 && ovl.xelem (0).is_undefined ()))) | |
448 gripe_bad_result (); | |
449 | |
450 return 0; | |
451 } | |
452 | |
453 // -------------------- jit_range -------------------- | |
454 bool | |
455 jit_range::all_elements_are_ints () const | |
456 { | |
457 Range r (*this); | |
458 return r.all_elements_are_ints (); | |
459 } | |
460 | |
461 std::ostream& | |
462 operator<< (std::ostream& os, const jit_range& rng) | |
463 { | |
464 return os << "Range[" << rng.base << ", " << rng.limit << ", " << rng.inc | |
465 << ", " << rng.nelem << "]"; | |
466 } | |
467 | |
468 // -------------------- jit_matrix -------------------- | |
469 | |
470 std::ostream& | |
471 operator<< (std::ostream& os, const jit_matrix& mat) | |
472 { | |
473 return os << "Matrix[" << mat.ref_count << ", " << mat.slice_data << ", " | |
474 << mat.slice_len << ", " << mat.dimensions << ", " | |
475 << mat.array << "]"; | |
476 } | |
477 | |
478 // -------------------- jit_type -------------------- | |
479 jit_type::jit_type (const std::string& aname, jit_type *aparent, | |
480 llvm::Type *allvm_type, int aid) : | |
481 mname (aname), mparent (aparent), llvm_type (allvm_type), mid (aid), | |
482 mdepth (aparent ? aparent->mdepth + 1 : 0) | |
483 { | |
484 std::memset (msret, 0, sizeof (msret)); | |
485 std::memset (mpointer_arg, 0, sizeof (mpointer_arg)); | |
486 std::memset (mpack, 0, sizeof (mpack)); | |
487 std::memset (munpack, 0, sizeof (munpack)); | |
488 | |
489 for (size_t i = 0; i < jit_convention::length; ++i) | |
490 mpacked_type[i] = llvm_type; | |
491 } | |
492 | |
493 llvm::Type * | |
494 jit_type::to_llvm_arg (void) const | |
495 { | |
496 return llvm_type ? llvm_type->getPointerTo () : 0; | |
497 } | |
498 | |
499 // -------------------- jit_function -------------------- | |
500 jit_function::jit_function () : module (0), llvm_function (0), mresult (0), | |
501 call_conv (jit_convention::length), | |
502 mcan_error (false) | |
503 {} | |
504 | |
505 jit_function::jit_function (llvm::Module *amodule, | |
506 jit_convention::type acall_conv, | |
507 const llvm::Twine& aname, jit_type *aresult, | |
508 const std::vector<jit_type *>& aargs) | |
509 : module (amodule), mresult (aresult), args (aargs), call_conv (acall_conv), | |
510 mcan_error (false) | |
511 { | |
512 llvm::SmallVector<llvm::Type *, 15> llvm_args; | |
513 | |
514 llvm::Type *rtype = llvm::Type::getVoidTy (context); | |
515 if (mresult) | |
516 { | |
517 rtype = mresult->packed_type (call_conv); | |
518 if (sret ()) | |
519 { | |
520 llvm_args.push_back (rtype->getPointerTo ()); | |
521 rtype = llvm::Type::getVoidTy (context); | |
522 } | |
523 } | |
524 | |
525 for (std::vector<jit_type *>::const_iterator iter = args.begin (); | |
526 iter != args.end (); ++iter) | |
527 { | |
528 jit_type *ty = *iter; | |
529 assert (ty); | |
530 llvm::Type *argty = ty->packed_type (call_conv); | |
531 if (ty->pointer_arg (call_conv)) | |
532 argty = argty->getPointerTo (); | |
533 | |
534 llvm_args.push_back (argty); | |
535 } | |
536 | |
537 // we mark all functinos as external linkage because this prevents llvm | |
538 // from getting rid of always inline functions | |
539 llvm::FunctionType *ft = llvm::FunctionType::get (rtype, llvm_args, false); | |
540 llvm_function = llvm::Function::Create (ft, llvm::Function::ExternalLinkage, | |
541 aname, module); | |
542 if (call_conv == jit_convention::internal) | |
543 llvm_function->addFnAttr (llvm::Attribute::AlwaysInline); | |
544 } | |
545 | |
546 jit_function::jit_function (const jit_function& fn, jit_type *aresult, | |
547 const std::vector<jit_type *>& aargs) | |
548 : module (fn.module), llvm_function (fn.llvm_function), mresult (aresult), | |
549 args (aargs), call_conv (fn.call_conv), mcan_error (fn.mcan_error) | |
550 { | |
551 } | |
552 | |
553 jit_function::jit_function (const jit_function& fn) | |
554 : module (fn.module), llvm_function (fn.llvm_function), mresult (fn.mresult), | |
555 args (fn.args), call_conv (fn.call_conv), mcan_error (fn.mcan_error) | |
556 {} | |
557 | |
558 std::string | |
559 jit_function::name (void) const | |
560 { | |
561 return llvm_function->getName (); | |
562 } | |
563 | |
564 llvm::BasicBlock * | |
565 jit_function::new_block (const std::string& aname, | |
566 llvm::BasicBlock *insert_before) | |
567 { | |
568 return llvm::BasicBlock::Create (context, aname, llvm_function, | |
569 insert_before); | |
570 } | |
571 | |
572 llvm::Value * | |
573 jit_function::call (llvm::IRBuilderD& builder, | |
574 const std::vector<jit_value *>& in_args) const | |
575 { | |
15056
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
576 if (! valid ()) |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
577 throw jit_fail_exception ("Call not implemented"); |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
578 |
15016 | 579 assert (in_args.size () == args.size ()); |
580 std::vector<llvm::Value *> llvm_args (args.size ()); | |
581 for (size_t i = 0; i < in_args.size (); ++i) | |
582 llvm_args[i] = in_args[i]->to_llvm (); | |
583 | |
584 return call (builder, llvm_args); | |
585 } | |
586 | |
587 llvm::Value * | |
588 jit_function::call (llvm::IRBuilderD& builder, | |
589 const std::vector<llvm::Value *>& in_args) const | |
590 { | |
15056
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
591 if (! valid ()) |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
592 throw jit_fail_exception ("Call not implemented"); |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
593 |
15016 | 594 assert (in_args.size () == args.size ()); |
595 llvm::Function *stacksave | |
596 = llvm::Intrinsic::getDeclaration (module, llvm::Intrinsic::stacksave); | |
597 llvm::SmallVector<llvm::Value *, 10> llvm_args; | |
598 llvm_args.reserve (in_args.size () + sret ()); | |
599 | |
600 llvm::Value *sret_mem = 0; | |
601 llvm::Value *saved_stack = 0; | |
602 if (sret ()) | |
603 { | |
604 saved_stack = builder.CreateCall (stacksave); | |
605 sret_mem = builder.CreateAlloca (mresult->packed_type (call_conv)); | |
606 llvm_args.push_back (sret_mem); | |
607 } | |
608 | |
609 for (size_t i = 0; i < in_args.size (); ++i) | |
610 { | |
611 llvm::Value *arg = in_args[i]; | |
612 jit_type::convert_fn convert = args[i]->pack (call_conv); | |
613 if (convert) | |
614 arg = convert (builder, arg); | |
615 | |
616 if (args[i]->pointer_arg (call_conv)) | |
617 { | |
618 if (! saved_stack) | |
619 saved_stack = builder.CreateCall (stacksave); | |
620 | |
621 arg = builder.CreateAlloca (args[i]->to_llvm ()); | |
622 builder.CreateStore (in_args[i], arg); | |
623 } | |
624 | |
625 llvm_args.push_back (arg); | |
626 } | |
627 | |
628 llvm::Value *ret = builder.CreateCall (llvm_function, llvm_args); | |
629 if (sret_mem) | |
630 ret = builder.CreateLoad (sret_mem); | |
631 | |
632 if (mresult) | |
633 { | |
634 jit_type::convert_fn unpack = mresult->unpack (call_conv); | |
635 if (unpack) | |
636 ret = unpack (builder, ret); | |
637 } | |
638 | |
639 if (saved_stack) | |
640 { | |
641 llvm::Function *stackrestore | |
642 = llvm::Intrinsic::getDeclaration (module, | |
643 llvm::Intrinsic::stackrestore); | |
644 builder.CreateCall (stackrestore, saved_stack); | |
645 } | |
646 | |
647 return ret; | |
648 } | |
649 | |
650 llvm::Value * | |
651 jit_function::argument (llvm::IRBuilderD& builder, size_t idx) const | |
652 { | |
653 assert (idx < args.size ()); | |
654 | |
655 // FIXME: We should be treating arguments like a list, not a vector. Shouldn't | |
656 // matter much for now, as the number of arguments shouldn't be much bigger | |
657 // than 4 | |
658 llvm::Function::arg_iterator iter = llvm_function->arg_begin (); | |
659 if (sret ()) | |
660 ++iter; | |
661 | |
662 for (size_t i = 0; i < idx; ++i, ++iter); | |
663 | |
664 if (args[idx]->pointer_arg (call_conv)) | |
665 return builder.CreateLoad (iter); | |
666 | |
667 return iter; | |
668 } | |
669 | |
670 void | |
671 jit_function::do_return (llvm::IRBuilderD& builder, llvm::Value *rval) | |
672 { | |
673 assert (! rval == ! mresult); | |
674 | |
675 if (rval) | |
676 { | |
677 jit_type::convert_fn convert = mresult->pack (call_conv); | |
678 if (convert) | |
679 rval = convert (builder, rval); | |
680 | |
681 if (sret ()) | |
682 builder.CreateStore (rval, llvm_function->arg_begin ()); | |
683 else | |
684 builder.CreateRet (rval); | |
685 } | |
686 else | |
687 builder.CreateRetVoid (); | |
688 | |
689 llvm::verifyFunction (*llvm_function); | |
690 } | |
691 | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
692 void |
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
693 jit_function::do_add_mapping (llvm::ExecutionEngine *engine, void *fn) |
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
694 { |
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
695 assert (valid ()); |
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
696 engine->addGlobalMapping (llvm_function, fn); |
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
697 } |
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
698 |
15016 | 699 std::ostream& |
700 operator<< (std::ostream& os, const jit_function& fn) | |
701 { | |
702 llvm::Function *lfn = fn.to_llvm (); | |
703 os << "jit_function: cc=" << fn.call_conv; | |
704 llvm::raw_os_ostream llvm_out (os); | |
705 lfn->print (llvm_out); | |
706 llvm_out.flush (); | |
707 return os; | |
708 } | |
709 | |
710 // -------------------- jit_operation -------------------- | |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
711 jit_operation::~jit_operation (void) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
712 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
713 for (generated_map::iterator iter = generated.begin (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
714 iter != generated.end (); ++iter) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
715 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
716 delete iter->first; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
717 delete iter->second; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
718 } |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
719 } |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
720 |
15016 | 721 void |
722 jit_operation::add_overload (const jit_function& func, | |
723 const std::vector<jit_type*>& args) | |
724 { | |
725 if (args.size () >= overloads.size ()) | |
726 overloads.resize (args.size () + 1); | |
727 | |
728 Array<jit_function>& over = overloads[args.size ()]; | |
729 dim_vector dv (over.dims ()); | |
730 Array<octave_idx_type> idx = to_idx (args); | |
731 bool must_resize = false; | |
732 | |
733 if (dv.length () != idx.numel ()) | |
734 { | |
735 dv.resize (idx.numel ()); | |
736 must_resize = true; | |
737 } | |
738 | |
739 for (octave_idx_type i = 0; i < dv.length (); ++i) | |
740 if (dv(i) <= idx(i)) | |
741 { | |
742 must_resize = true; | |
743 dv(i) = idx(i) + 1; | |
744 } | |
745 | |
746 if (must_resize) | |
747 over.resize (dv); | |
748 | |
749 over(idx) = func; | |
750 } | |
751 | |
752 const jit_function& | |
753 jit_operation::overload (const std::vector<jit_type*>& types) const | |
754 { | |
755 static jit_function null_overload; | |
756 for (size_t i =0; i < types.size (); ++i) | |
757 if (! types[i]) | |
758 return null_overload; | |
759 | |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
760 if (types.size () >= overloads.size ()) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
761 return do_generate (types); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
762 |
15016 | 763 const Array<jit_function>& over = overloads[types.size ()]; |
764 dim_vector dv (over.dims ()); | |
765 Array<octave_idx_type> idx = to_idx (types); | |
766 for (octave_idx_type i = 0; i < dv.length (); ++i) | |
767 if (idx(i) >= dv(i)) | |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
768 return do_generate (types); |
15016 | 769 |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
770 const jit_function& ret = over(idx); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
771 if (! ret.valid ()) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
772 return do_generate (types); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
773 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
774 return ret; |
15016 | 775 } |
776 | |
777 Array<octave_idx_type> | |
778 jit_operation::to_idx (const std::vector<jit_type*>& types) const | |
779 { | |
780 octave_idx_type numel = types.size (); | |
781 if (numel == 1) | |
782 numel = 2; | |
783 | |
784 Array<octave_idx_type> idx (dim_vector (1, numel)); | |
785 for (octave_idx_type i = 0; i < static_cast<octave_idx_type> (types.size ()); | |
786 ++i) | |
787 idx(i) = types[i]->type_id (); | |
788 | |
789 if (types.size () == 1) | |
790 { | |
791 idx(1) = idx(0); | |
792 idx(0) = 0; | |
793 } | |
794 | |
795 return idx; | |
796 } | |
797 | |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
798 const jit_function& |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
799 jit_operation::do_generate (const signature_vec& types) const |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
800 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
801 static jit_function null_overload; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
802 generated_map::const_iterator find = generated.find (&types); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
803 if (find != generated.end ()) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
804 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
805 if (find->second) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
806 return *find->second; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
807 else |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
808 return null_overload; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
809 } |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
810 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
811 jit_function *ret = generate (types); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
812 generated[new signature_vec (types)] = ret; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
813 return ret ? *ret : null_overload; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
814 } |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
815 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
816 jit_function * |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
817 jit_operation::generate (const signature_vec& types) const |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
818 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
819 return 0; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
820 } |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
821 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
822 bool |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
823 jit_operation::signature_cmp |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
824 ::operator() (const signature_vec *lhs, const signature_vec *rhs) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
825 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
826 const signature_vec& l = *lhs; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
827 const signature_vec& r = *rhs; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
828 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
829 if (l.size () < r.size ()) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
830 return true; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
831 else if (l.size () > r.size ()) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
832 return false; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
833 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
834 for (size_t i = 0; i < l.size (); ++i) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
835 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
836 if (l[i]->type_id () < r[i]->type_id ()) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
837 return true; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
838 else if (l[i]->type_id () > r[i]->type_id ()) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
839 return false; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
840 } |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
841 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
842 return false; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
843 } |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
844 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
845 // -------------------- jit_index_operation -------------------- |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
846 jit_function * |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
847 jit_index_operation::generate (const signature_vec& types) const |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
848 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
849 if (types.size () > 2 && types[0] == jit_typeinfo::get_matrix ()) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
850 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
851 // indexing a matrix with scalars |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
852 jit_type *scalar = jit_typeinfo::get_scalar (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
853 for (size_t i = 1; i < types.size (); ++i) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
854 if (types[i] != scalar) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
855 return 0; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
856 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
857 return generate_matrix (types); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
858 } |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
859 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
860 return 0; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
861 } |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
862 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
863 llvm::Value * |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
864 jit_index_operation::create_arg_array (llvm::IRBuilderD& builder, |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
865 const jit_function &fn, size_t start_idx, |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
866 size_t end_idx) const |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
867 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
868 size_t n = end_idx - start_idx; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
869 llvm::Type *scalar_t = jit_typeinfo::get_scalar_llvm (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
870 llvm::ArrayType *array_t = llvm::ArrayType::get (scalar_t, n); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
871 llvm::Value *array = llvm::UndefValue::get (array_t); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
872 for (size_t i = start_idx; i < end_idx; ++i) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
873 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
874 llvm::Value *idx = fn.argument (builder, i); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
875 array = builder.CreateInsertValue (array, idx, i - start_idx); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
876 } |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
877 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
878 llvm::Value *array_mem = builder.CreateAlloca (array_t); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
879 builder.CreateStore (array, array_mem); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
880 return builder.CreateBitCast (array_mem, scalar_t->getPointerTo ()); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
881 } |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
882 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
883 // -------------------- jit_paren_subsref -------------------- |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
884 jit_function * |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
885 jit_paren_subsref::generate_matrix (const signature_vec& types) const |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
886 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
887 std::stringstream ss; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
888 ss << "jit_paren_subsref_matrix_scalar" << (types.size () - 1); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
889 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
890 jit_type *scalar = jit_typeinfo::get_scalar (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
891 jit_function *fn = new jit_function (module, jit_convention::internal, |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
892 ss.str (), scalar, types); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
893 fn->mark_can_error (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
894 llvm::BasicBlock *body = fn->new_block (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
895 llvm::IRBuilder<> builder (body); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
896 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
897 llvm::Value *array = create_arg_array (builder, *fn, 1, types.size ()); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
898 jit_type *index = jit_typeinfo::get_index (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
899 llvm::Value *nelem = llvm::ConstantInt::get (index->to_llvm (), |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
900 types.size () - 1); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
901 llvm::Value *mat = fn->argument (builder, 0); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
902 llvm::Value *ret = paren_scalar.call (builder, mat, array, nelem); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
903 fn->do_return (builder, ret); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
904 return fn; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
905 } |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
906 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
907 void |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
908 jit_paren_subsref::do_initialize (void) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
909 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
910 std::vector<jit_type *> types (3); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
911 types[0] = jit_typeinfo::get_matrix (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
912 types[1] = jit_typeinfo::get_scalar_ptr (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
913 types[2] = jit_typeinfo::get_index (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
914 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
915 jit_type *scalar = jit_typeinfo::get_scalar (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
916 paren_scalar = jit_function (module, jit_convention::external, |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
917 "octave_jit_paren_scalar", scalar, types); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
918 paren_scalar.add_mapping (engine, &octave_jit_paren_scalar); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
919 paren_scalar.mark_can_error (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
920 } |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
921 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
922 // -------------------- jit_paren_subsasgn -------------------- |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
923 jit_function * |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
924 jit_paren_subsasgn::generate_matrix (const signature_vec& types) const |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
925 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
926 std::stringstream ss; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
927 ss << "jit_paren_subsasgn_matrix_scalar" << (types.size () - 2); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
928 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
929 jit_type *matrix = jit_typeinfo::get_matrix (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
930 jit_function *fn = new jit_function (module, jit_convention::internal, |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
931 ss.str (), matrix, types); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
932 fn->mark_can_error (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
933 llvm::BasicBlock *body = fn->new_block (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
934 llvm::IRBuilder<> builder (body); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
935 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
936 llvm::Value *array = create_arg_array (builder, *fn, 1, types.size () - 1); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
937 jit_type *index = jit_typeinfo::get_index (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
938 llvm::Value *nelem = llvm::ConstantInt::get (index->to_llvm (), |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
939 types.size () - 2); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
940 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
941 llvm::Value *mat = fn->argument (builder, 0); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
942 llvm::Value *value = fn->argument (builder, types.size () - 1); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
943 llvm::Value *ret = paren_scalar.call (builder, mat, array, nelem, value); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
944 fn->do_return (builder, ret); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
945 return fn; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
946 } |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
947 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
948 void |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
949 jit_paren_subsasgn::do_initialize (void) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
950 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
951 if (paren_scalar.valid ()) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
952 return; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
953 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
954 jit_type *matrix = jit_typeinfo::get_matrix (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
955 std::vector<jit_type *> types (4); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
956 types[0] = matrix; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
957 types[1] = jit_typeinfo::get_scalar_ptr (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
958 types[2] = jit_typeinfo::get_index (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
959 types[3] = jit_typeinfo::get_scalar (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
960 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
961 paren_scalar = jit_function (module, jit_convention::external, |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
962 "octave_jit_paren_scalar", matrix, types); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
963 paren_scalar.add_mapping (engine, &octave_jit_paren_scalar_subsasgn); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
964 paren_scalar.mark_can_error (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
965 } |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
966 |
15016 | 967 // -------------------- jit_typeinfo -------------------- |
968 void | |
969 jit_typeinfo::initialize (llvm::Module *m, llvm::ExecutionEngine *e) | |
970 { | |
971 new jit_typeinfo (m, e); | |
972 } | |
973 | |
974 jit_typeinfo::jit_typeinfo (llvm::Module *m, llvm::ExecutionEngine *e) | |
975 : module (m), engine (e), next_id (0), | |
976 builder (*new llvm::IRBuilderD (context)) | |
977 { | |
978 instance = this; | |
979 | |
980 // FIXME: We should be registering types like in octave_value_typeinfo | |
981 llvm::Type *any_t = llvm::StructType::create (context, "octave_base_value"); | |
982 any_t = any_t->getPointerTo (); | |
983 | |
984 llvm::Type *scalar_t = llvm::Type::getDoubleTy (context); | |
985 llvm::Type *bool_t = llvm::Type::getInt1Ty (context); | |
986 llvm::Type *string_t = llvm::Type::getInt8Ty (context); | |
987 string_t = string_t->getPointerTo (); | |
988 llvm::Type *index_t = llvm::Type::getIntNTy (context, | |
989 sizeof(octave_idx_type) * 8); | |
990 | |
991 llvm::StructType *range_t = llvm::StructType::create (context, "range"); | |
992 std::vector<llvm::Type *> range_contents (4, scalar_t); | |
993 range_contents[3] = index_t; | |
994 range_t->setBody (range_contents); | |
995 | |
996 llvm::Type *refcount_t = llvm::Type::getIntNTy (context, sizeof(int) * 8); | |
997 | |
998 llvm::StructType *matrix_t = llvm::StructType::create (context, "matrix"); | |
999 llvm::Type *matrix_contents[5]; | |
1000 matrix_contents[0] = refcount_t->getPointerTo (); | |
1001 matrix_contents[1] = scalar_t->getPointerTo (); | |
1002 matrix_contents[2] = index_t; | |
1003 matrix_contents[3] = index_t->getPointerTo (); | |
1004 matrix_contents[4] = string_t; | |
1005 matrix_t->setBody (llvm::makeArrayRef (matrix_contents, 5)); | |
1006 | |
1007 llvm::Type *complex_t = llvm::VectorType::get (scalar_t, 2); | |
1008 | |
1009 // complex_ret is what is passed to C functions in order to get calling | |
1010 // convention right | |
1011 complex_ret = llvm::StructType::create (context, "complex_ret"); | |
1012 llvm::Type *complex_ret_contents[] = {scalar_t, scalar_t}; | |
1013 complex_ret->setBody (complex_ret_contents); | |
1014 | |
1015 // create types | |
1016 any = new_type ("any", 0, any_t); | |
1017 matrix = new_type ("matrix", any, matrix_t); | |
1018 complex = new_type ("complex", any, complex_t); | |
1019 scalar = new_type ("scalar", complex, scalar_t); | |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
1020 scalar_ptr = new_type ("scalar_ptr", 0, scalar_t->getPointerTo ()); |
15016 | 1021 range = new_type ("range", any, range_t); |
1022 string = new_type ("string", any, string_t); | |
1023 boolean = new_type ("bool", any, bool_t); | |
1024 index = new_type ("index", any, index_t); | |
1025 | |
1026 create_int (8); | |
1027 create_int (16); | |
1028 create_int (32); | |
1029 create_int (64); | |
1030 | |
1031 casts.resize (next_id + 1); | |
1032 identities.resize (next_id + 1); | |
1033 | |
1034 // specify calling conventions | |
1035 // FIXME: We should detect architecture and do something sane based on that | |
1036 // here we assume x86 or x86_64 | |
1037 matrix->mark_sret (); | |
1038 matrix->mark_pointer_arg (); | |
1039 | |
1040 range->mark_sret (); | |
1041 range->mark_pointer_arg (); | |
1042 | |
1043 complex->set_pack (jit_convention::external, &jit_typeinfo::pack_complex); | |
1044 complex->set_unpack (jit_convention::external, &jit_typeinfo::unpack_complex); | |
1045 complex->set_packed_type (jit_convention::external, complex_ret); | |
1046 | |
1047 if (sizeof (void *) == 4) | |
1048 complex->mark_sret (); | |
1049 | |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
1050 paren_subsref_fn.initialize (module, engine); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
1051 paren_subsasgn_fn.initialize (module, engine); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
1052 |
15016 | 1053 // bind global variables |
1054 lerror_state = new llvm::GlobalVariable (*module, bool_t, false, | |
1055 llvm::GlobalValue::ExternalLinkage, | |
1056 0, "error_state"); | |
1057 engine->addGlobalMapping (lerror_state, | |
1058 reinterpret_cast<void *> (&error_state)); | |
1059 | |
1060 // any with anything is an any op | |
1061 jit_function fn; | |
1062 jit_type *binary_op_type = intN (sizeof (octave_value::binary_op) * 8); | |
1063 llvm::Type *llvm_bo_type = binary_op_type->to_llvm (); | |
1064 jit_function any_binary = create_function (jit_convention::external, | |
1065 "octave_jit_binary_any_any", | |
1066 any, binary_op_type, any, any); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1067 any_binary.add_mapping (engine, &octave_jit_binary_any_any); |
15016 | 1068 any_binary.mark_can_error (); |
1069 binary_ops.resize (octave_value::num_binary_ops); | |
1070 for (size_t i = 0; i < octave_value::num_binary_ops; ++i) | |
1071 { | |
1072 octave_value::binary_op op = static_cast<octave_value::binary_op> (i); | |
1073 std::string op_name = octave_value::binary_op_as_string (op); | |
1074 binary_ops[i].stash_name ("binary" + op_name); | |
1075 } | |
1076 | |
1077 for (int op = 0; op < octave_value::num_binary_ops; ++op) | |
1078 { | |
1079 llvm::Twine fn_name ("octave_jit_binary_any_any_"); | |
1080 fn_name = fn_name + llvm::Twine (op); | |
1081 | |
1082 fn = create_function (jit_convention::internal, fn_name, any, any, any); | |
1083 fn.mark_can_error (); | |
1084 llvm::BasicBlock *block = fn.new_block (); | |
1085 builder.SetInsertPoint (block); | |
1086 llvm::APInt op_int(sizeof (octave_value::binary_op) * 8, op, | |
1087 std::numeric_limits<octave_value::binary_op>::is_signed); | |
1088 llvm::Value *op_as_llvm = llvm::ConstantInt::get (llvm_bo_type, op_int); | |
1089 llvm::Value *ret = any_binary.call (builder, op_as_llvm, | |
1090 fn.argument (builder, 0), | |
1091 fn.argument (builder, 1)); | |
1092 fn.do_return (builder, ret); | |
1093 binary_ops[op].add_overload (fn); | |
1094 } | |
1095 | |
1096 // grab any | |
1097 fn = create_function (jit_convention::external, "octave_jit_grab_any", any, | |
1098 any); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1099 fn.add_mapping (engine, &octave_jit_grab_any); |
15016 | 1100 grab_fn.add_overload (fn); |
1101 grab_fn.stash_name ("grab"); | |
1102 | |
1103 // grab matrix | |
1104 fn = create_function (jit_convention::external, "octave_jit_grab_matrix", | |
1105 matrix, matrix); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1106 fn.add_mapping (engine, &octave_jit_grab_matrix); |
15016 | 1107 grab_fn.add_overload (fn); |
1108 | |
1109 // release any | |
1110 fn = create_function (jit_convention::external, "octave_jit_release_any", 0, | |
1111 any); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1112 fn.add_mapping (engine, &octave_jit_release_any); |
15016 | 1113 release_fn.add_overload (fn); |
1114 release_fn.stash_name ("release"); | |
1115 | |
1116 // release matrix | |
1117 fn = create_function (jit_convention::external, "octave_jit_release_matrix", | |
1118 0, matrix); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1119 fn.add_mapping (engine, &octave_jit_release_matrix); |
15016 | 1120 release_fn.add_overload (fn); |
1121 | |
1122 // release scalar | |
1123 fn = create_identity (scalar); | |
1124 release_fn.add_overload (fn); | |
1125 | |
1126 // release complex | |
1127 fn = create_identity (complex); | |
1128 release_fn.add_overload (fn); | |
1129 | |
1130 // release index | |
1131 fn = create_identity (index); | |
1132 release_fn.add_overload (fn); | |
1133 | |
1134 // now for binary scalar operations | |
1135 // FIXME: Finish all operations | |
1136 add_binary_op (scalar, octave_value::op_add, llvm::Instruction::FAdd); | |
1137 add_binary_op (scalar, octave_value::op_sub, llvm::Instruction::FSub); | |
1138 add_binary_op (scalar, octave_value::op_mul, llvm::Instruction::FMul); | |
1139 add_binary_op (scalar, octave_value::op_el_mul, llvm::Instruction::FMul); | |
1140 | |
1141 add_binary_fcmp (scalar, octave_value::op_lt, llvm::CmpInst::FCMP_ULT); | |
1142 add_binary_fcmp (scalar, octave_value::op_le, llvm::CmpInst::FCMP_ULE); | |
1143 add_binary_fcmp (scalar, octave_value::op_eq, llvm::CmpInst::FCMP_UEQ); | |
1144 add_binary_fcmp (scalar, octave_value::op_ge, llvm::CmpInst::FCMP_UGE); | |
1145 add_binary_fcmp (scalar, octave_value::op_gt, llvm::CmpInst::FCMP_UGT); | |
1146 add_binary_fcmp (scalar, octave_value::op_ne, llvm::CmpInst::FCMP_UNE); | |
1147 | |
1148 jit_function gripe_div0 = create_function (jit_convention::external, | |
1149 "gripe_divide_by_zero", 0); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1150 gripe_div0.add_mapping (engine, &gripe_divide_by_zero); |
15016 | 1151 gripe_div0.mark_can_error (); |
1152 | |
1153 // divide is annoying because it might error | |
1154 fn = create_function (jit_convention::internal, | |
1155 "octave_jit_div_scalar_scalar", scalar, scalar, scalar); | |
1156 fn.mark_can_error (); | |
1157 | |
1158 llvm::BasicBlock *body = fn.new_block (); | |
1159 builder.SetInsertPoint (body); | |
1160 { | |
1161 llvm::BasicBlock *warn_block = fn.new_block ("warn"); | |
1162 llvm::BasicBlock *normal_block = fn.new_block ("normal"); | |
1163 | |
1164 llvm::Value *zero = llvm::ConstantFP::get (scalar_t, 0); | |
1165 llvm::Value *check = builder.CreateFCmpUEQ (zero, fn.argument (builder, 0)); | |
1166 builder.CreateCondBr (check, warn_block, normal_block); | |
1167 | |
1168 builder.SetInsertPoint (warn_block); | |
1169 gripe_div0.call (builder); | |
1170 builder.CreateBr (normal_block); | |
1171 | |
1172 builder.SetInsertPoint (normal_block); | |
1173 llvm::Value *ret = builder.CreateFDiv (fn.argument (builder, 0), | |
1174 fn.argument (builder, 1)); | |
1175 fn.do_return (builder, ret); | |
1176 } | |
1177 binary_ops[octave_value::op_div].add_overload (fn); | |
1178 binary_ops[octave_value::op_el_div].add_overload (fn); | |
1179 | |
1180 // ldiv is the same as div with the operators reversed | |
1181 fn = mirror_binary (fn); | |
1182 binary_ops[octave_value::op_ldiv].add_overload (fn); | |
1183 binary_ops[octave_value::op_el_ldiv].add_overload (fn); | |
1184 | |
1185 // In general, the result of scalar ^ scalar is a complex number. We might be | |
1186 // able to improve on this if we keep track of the range of values varaibles | |
1187 // can take on. | |
1188 fn = create_function (jit_convention::external, | |
1189 "octave_jit_pow_scalar_scalar", complex, scalar, | |
1190 scalar); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1191 fn.add_mapping (engine, &octave_jit_pow_scalar_scalar); |
15016 | 1192 binary_ops[octave_value::op_pow].add_overload (fn); |
1193 binary_ops[octave_value::op_el_pow].add_overload (fn); | |
1194 | |
1195 // now for binary complex operations | |
1196 add_binary_op (complex, octave_value::op_add, llvm::Instruction::FAdd); | |
1197 add_binary_op (complex, octave_value::op_sub, llvm::Instruction::FSub); | |
1198 | |
1199 fn = create_function (jit_convention::internal, | |
1200 "octave_jit_*_complex_complex", complex, complex, | |
1201 complex); | |
1202 body = fn.new_block (); | |
1203 builder.SetInsertPoint (body); | |
1204 { | |
1205 // (x0*x1 - y0*y1, x0*y1 + y0*x1) = (x0,y0) * (x1,y1) | |
1206 // We compute this in one vectorized multiplication, a subtraction, and an | |
1207 // addition. | |
1208 llvm::Value *lhs = fn.argument (builder, 0); | |
1209 llvm::Value *rhs = fn.argument (builder, 1); | |
1210 | |
1211 // FIXME: We need a better way of doing this, working with llvm's IR | |
1212 // directly is sort of a pain. | |
1213 llvm::Value *zero = builder.getInt32 (0); | |
1214 llvm::Value *one = builder.getInt32 (1); | |
1215 llvm::Value *two = builder.getInt32 (2); | |
1216 llvm::Value *three = builder.getInt32 (3); | |
1217 | |
1218 llvm::Type *vec4 = llvm::VectorType::get (scalar_t, 4); | |
1219 llvm::Value *mlhs = llvm::UndefValue::get (vec4); | |
1220 llvm::Value *mrhs = mlhs; | |
1221 | |
1222 llvm::Value *temp = complex_real (lhs); | |
1223 mlhs = builder.CreateInsertElement (mlhs, temp, zero); | |
1224 mlhs = builder.CreateInsertElement (mlhs, temp, two); | |
1225 temp = complex_imag (lhs); | |
1226 mlhs = builder.CreateInsertElement (mlhs, temp, one); | |
1227 mlhs = builder.CreateInsertElement (mlhs, temp, three); | |
1228 | |
1229 temp = complex_real (rhs); | |
1230 mrhs = builder.CreateInsertElement (mrhs, temp, zero); | |
1231 mrhs = builder.CreateInsertElement (mrhs, temp, three); | |
1232 temp = complex_imag (rhs); | |
1233 mrhs = builder.CreateInsertElement (mrhs, temp, one); | |
1234 mrhs = builder.CreateInsertElement (mrhs, temp, two); | |
1235 | |
1236 llvm::Value *mres = builder.CreateFMul (mlhs, mrhs); | |
1237 llvm::Value *tlhs = builder.CreateExtractElement (mres, zero); | |
1238 llvm::Value *trhs = builder.CreateExtractElement (mres, one); | |
1239 llvm::Value *ret_real = builder.CreateFSub (tlhs, trhs); | |
1240 | |
1241 tlhs = builder.CreateExtractElement (mres, two); | |
1242 trhs = builder.CreateExtractElement (mres, three); | |
1243 llvm::Value *ret_imag = builder.CreateFAdd (tlhs, trhs); | |
1244 fn.do_return (builder, complex_new (ret_real, ret_imag)); | |
1245 } | |
1246 | |
1247 binary_ops[octave_value::op_mul].add_overload (fn); | |
1248 binary_ops[octave_value::op_el_mul].add_overload (fn); | |
1249 | |
1250 jit_function complex_div = create_function (jit_convention::external, | |
1251 "octave_jit_complex_div", | |
1252 complex, complex, complex); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1253 complex_div.add_mapping (engine, &octave_jit_complex_div); |
15016 | 1254 complex_div.mark_can_error (); |
1255 binary_ops[octave_value::op_div].add_overload (fn); | |
1256 binary_ops[octave_value::op_ldiv].add_overload (fn); | |
1257 | |
1258 fn = mirror_binary (complex_div); | |
1259 binary_ops[octave_value::op_ldiv].add_overload (fn); | |
1260 binary_ops[octave_value::op_el_ldiv].add_overload (fn); | |
1261 | |
1262 fn = create_function (jit_convention::external, | |
1263 "octave_jit_pow_complex_complex", complex, complex, | |
1264 complex); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1265 fn.add_mapping (engine, &octave_jit_pow_complex_complex); |
15016 | 1266 binary_ops[octave_value::op_pow].add_overload (fn); |
1267 binary_ops[octave_value::op_el_pow].add_overload (fn); | |
1268 | |
1269 fn = create_function (jit_convention::internal, | |
1270 "octave_jit_*_scalar_complex", complex, scalar, | |
1271 complex); | |
1272 jit_function mul_scalar_complex = fn; | |
1273 body = fn.new_block (); | |
1274 builder.SetInsertPoint (body); | |
1275 { | |
1276 llvm::Value *lhs = fn.argument (builder, 0); | |
1277 llvm::Value *tlhs = complex_new (lhs, lhs); | |
1278 llvm::Value *rhs = fn.argument (builder, 1); | |
1279 fn.do_return (builder, builder.CreateFMul (tlhs, rhs)); | |
1280 } | |
1281 binary_ops[octave_value::op_mul].add_overload (fn); | |
1282 binary_ops[octave_value::op_el_mul].add_overload (fn); | |
1283 | |
1284 | |
1285 fn = mirror_binary (mul_scalar_complex); | |
1286 binary_ops[octave_value::op_mul].add_overload (fn); | |
1287 binary_ops[octave_value::op_el_mul].add_overload (fn); | |
1288 | |
1289 fn = create_function (jit_convention::internal, "octave_jit_+_scalar_complex", | |
1290 complex, scalar, complex); | |
1291 body = fn.new_block (); | |
1292 builder.SetInsertPoint (body); | |
1293 { | |
1294 llvm::Value *lhs = fn.argument (builder, 0); | |
1295 llvm::Value *rhs = fn.argument (builder, 1); | |
1296 llvm::Value *real = builder.CreateFAdd (lhs, complex_real (rhs)); | |
1297 fn.do_return (builder, complex_real (rhs, real)); | |
1298 } | |
1299 binary_ops[octave_value::op_add].add_overload (fn); | |
1300 | |
1301 fn = mirror_binary (fn); | |
1302 binary_ops[octave_value::op_add].add_overload (fn); | |
1303 | |
1304 fn = create_function (jit_convention::internal, "octave_jit_-_complex_scalar", | |
1305 complex, complex, scalar); | |
1306 body = fn.new_block (); | |
1307 builder.SetInsertPoint (body); | |
1308 { | |
1309 llvm::Value *lhs = fn.argument (builder, 0); | |
1310 llvm::Value *rhs = fn.argument (builder, 1); | |
1311 llvm::Value *real = builder.CreateFSub (complex_real (lhs), rhs); | |
1312 fn.do_return (builder, complex_real (lhs, real)); | |
1313 } | |
1314 binary_ops[octave_value::op_sub].add_overload (fn); | |
1315 | |
1316 fn = create_function (jit_convention::internal, "octave_jit_-_scalar_complex", | |
1317 complex, scalar, complex); | |
1318 body = fn.new_block (); | |
1319 builder.SetInsertPoint (body); | |
1320 { | |
1321 llvm::Value *lhs = fn.argument (builder, 0); | |
1322 llvm::Value *rhs = fn.argument (builder, 1); | |
1323 llvm::Value *real = builder.CreateFSub (lhs, complex_real (rhs)); | |
1324 fn.do_return (builder, complex_real (rhs, real)); | |
1325 } | |
1326 binary_ops[octave_value::op_sub].add_overload (fn); | |
1327 | |
1328 fn = create_function (jit_convention::external, | |
1329 "octave_jit_pow_scalar_complex", complex, scalar, | |
1330 complex); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1331 fn.add_mapping (engine, &octave_jit_pow_scalar_complex); |
15016 | 1332 binary_ops[octave_value::op_pow].add_overload (fn); |
1333 binary_ops[octave_value::op_el_pow].add_overload (fn); | |
1334 | |
1335 fn = create_function (jit_convention::external, | |
1336 "octave_jit_pow_complex_scalar", complex, complex, | |
1337 scalar); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1338 fn.add_mapping (engine, &octave_jit_pow_complex_scalar); |
15016 | 1339 binary_ops[octave_value::op_pow].add_overload (fn); |
1340 binary_ops[octave_value::op_el_pow].add_overload (fn); | |
1341 | |
1342 // now for binary index operators | |
1343 add_binary_op (index, octave_value::op_add, llvm::Instruction::Add); | |
1344 | |
1345 // and binary bool operators | |
1346 add_binary_op (boolean, octave_value::op_el_or, llvm::Instruction::Or); | |
1347 add_binary_op (boolean, octave_value::op_el_and, llvm::Instruction::And); | |
1348 | |
1349 // now for printing functions | |
1350 print_fn.stash_name ("print"); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1351 add_print (any, reinterpret_cast<void *> (&octave_jit_print_any)); |
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1352 add_print (scalar, reinterpret_cast<void *> (&octave_jit_print_scalar)); |
15016 | 1353 |
1354 // initialize for loop | |
1355 for_init_fn.stash_name ("for_init"); | |
1356 | |
1357 fn = create_function (jit_convention::internal, "octave_jit_for_range_init", | |
1358 index, range); | |
1359 body = fn.new_block (); | |
1360 builder.SetInsertPoint (body); | |
1361 { | |
1362 llvm::Value *zero = llvm::ConstantInt::get (index_t, 0); | |
1363 fn.do_return (builder, zero); | |
1364 } | |
1365 for_init_fn.add_overload (fn); | |
1366 | |
1367 // bounds check for for loop | |
1368 for_check_fn.stash_name ("for_check"); | |
1369 | |
1370 fn = create_function (jit_convention::internal, "octave_jit_for_range_check", | |
1371 boolean, range, index); | |
1372 body = fn.new_block (); | |
1373 builder.SetInsertPoint (body); | |
1374 { | |
1375 llvm::Value *nelem | |
1376 = builder.CreateExtractValue (fn.argument (builder, 0), 3); | |
1377 llvm::Value *idx = fn.argument (builder, 1); | |
1378 llvm::Value *ret = builder.CreateICmpULT (idx, nelem); | |
1379 fn.do_return (builder, ret); | |
1380 } | |
1381 for_check_fn.add_overload (fn); | |
1382 | |
1383 // index variabe for for loop | |
1384 for_index_fn.stash_name ("for_index"); | |
1385 | |
1386 fn = create_function (jit_convention::internal, "octave_jit_for_range_idx", | |
1387 scalar, range, index); | |
1388 body = fn.new_block (); | |
1389 builder.SetInsertPoint (body); | |
1390 { | |
1391 llvm::Value *idx = fn.argument (builder, 1); | |
1392 llvm::Value *didx = builder.CreateSIToFP (idx, scalar_t); | |
1393 llvm::Value *rng = fn.argument (builder, 0); | |
1394 llvm::Value *base = builder.CreateExtractValue (rng, 0); | |
1395 llvm::Value *inc = builder.CreateExtractValue (rng, 2); | |
1396 | |
1397 llvm::Value *ret = builder.CreateFMul (didx, inc); | |
1398 ret = builder.CreateFAdd (base, ret); | |
1399 fn.do_return (builder, ret); | |
1400 } | |
1401 for_index_fn.add_overload (fn); | |
1402 | |
1403 // logically true | |
1404 logically_true_fn.stash_name ("logically_true"); | |
1405 | |
1406 jit_function gripe_nantl | |
1407 = create_function (jit_convention::external, | |
1408 "octave_jit_gripe_nan_to_logical_conversion", 0); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1409 gripe_nantl.add_mapping (engine, &octave_jit_gripe_nan_to_logical_conversion); |
15016 | 1410 gripe_nantl.mark_can_error (); |
1411 | |
1412 fn = create_function (jit_convention::internal, | |
1413 "octave_jit_logically_true_scalar", boolean, scalar); | |
1414 fn.mark_can_error (); | |
1415 | |
1416 body = fn.new_block (); | |
1417 builder.SetInsertPoint (body); | |
1418 { | |
1419 llvm::BasicBlock *error_block = fn.new_block ("error"); | |
1420 llvm::BasicBlock *normal_block = fn.new_block ("normal"); | |
1421 | |
1422 llvm::Value *check = builder.CreateFCmpUNE (fn.argument (builder, 0), | |
1423 fn.argument (builder, 0)); | |
1424 builder.CreateCondBr (check, error_block, normal_block); | |
1425 | |
1426 builder.SetInsertPoint (error_block); | |
1427 gripe_nantl.call (builder); | |
1428 builder.CreateBr (normal_block); | |
1429 builder.SetInsertPoint (normal_block); | |
1430 | |
1431 llvm::Value *zero = llvm::ConstantFP::get (scalar_t, 0); | |
1432 llvm::Value *ret = builder.CreateFCmpONE (fn.argument (builder, 0), zero); | |
1433 fn.do_return (builder, ret); | |
1434 } | |
1435 logically_true_fn.add_overload (fn); | |
1436 | |
1437 // logically_true boolean | |
1438 fn = create_identity (boolean); | |
1439 logically_true_fn.add_overload (fn); | |
1440 | |
1441 // make_range | |
1442 // FIXME: May be benificial to implement all in LLVM | |
1443 make_range_fn.stash_name ("make_range"); | |
1444 jit_function compute_nelem | |
1445 = create_function (jit_convention::external, "octave_jit_compute_nelem", | |
1446 index, scalar, scalar, scalar); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1447 compute_nelem.add_mapping (engine, &octave_jit_compute_nelem); |
15016 | 1448 |
1449 fn = create_function (jit_convention::internal, "octave_jit_make_range", | |
1450 range, scalar, scalar, scalar); | |
1451 body = fn.new_block (); | |
1452 builder.SetInsertPoint (body); | |
1453 { | |
1454 llvm::Value *base = fn.argument (builder, 0); | |
1455 llvm::Value *limit = fn.argument (builder, 1); | |
1456 llvm::Value *inc = fn.argument (builder, 2); | |
1457 llvm::Value *nelem = compute_nelem.call (builder, base, limit, inc); | |
1458 | |
1459 llvm::Value *dzero = llvm::ConstantFP::get (scalar_t, 0); | |
1460 llvm::Value *izero = llvm::ConstantInt::get (index_t, 0); | |
1461 llvm::Value *rng = llvm::ConstantStruct::get (range_t, dzero, dzero, dzero, | |
1462 izero, NULL); | |
1463 rng = builder.CreateInsertValue (rng, base, 0); | |
1464 rng = builder.CreateInsertValue (rng, limit, 1); | |
1465 rng = builder.CreateInsertValue (rng, inc, 2); | |
1466 rng = builder.CreateInsertValue (rng, nelem, 3); | |
1467 fn.do_return (builder, rng); | |
1468 } | |
1469 make_range_fn.add_overload (fn); | |
1470 | |
1471 // paren_subsref | |
1472 jit_type *jit_int = intN (sizeof (int) * 8); | |
1473 llvm::Type *int_t = jit_int->to_llvm (); | |
1474 jit_function ginvalid_index | |
1475 = create_function (jit_convention::external, "octave_jit_ginvalid_index", | |
1476 0); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1477 ginvalid_index.add_mapping (engine, &octave_jit_ginvalid_index); |
15016 | 1478 jit_function gindex_range = create_function (jit_convention::external, |
1479 "octave_jit_gindex_range", | |
1480 0, jit_int, jit_int, index, | |
1481 index); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1482 gindex_range.add_mapping (engine, &octave_jit_gindex_range); |
15016 | 1483 |
1484 fn = create_function (jit_convention::internal, "()subsref", scalar, matrix, | |
1485 scalar); | |
1486 fn.mark_can_error (); | |
1487 | |
1488 body = fn.new_block (); | |
1489 builder.SetInsertPoint (body); | |
1490 { | |
1491 llvm::Value *one = llvm::ConstantInt::get (index_t, 1); | |
1492 llvm::Value *ione; | |
1493 if (index_t == int_t) | |
1494 ione = one; | |
1495 else | |
1496 ione = llvm::ConstantInt::get (int_t, 1); | |
1497 | |
1498 llvm::Value *undef = llvm::UndefValue::get (scalar_t); | |
1499 llvm::Value *mat = fn.argument (builder, 0); | |
1500 llvm::Value *idx = fn.argument (builder, 1); | |
1501 | |
1502 // convert index to scalar to integer, and check index >= 1 | |
1503 llvm::Value *int_idx = builder.CreateFPToSI (idx, index_t); | |
1504 llvm::Value *check_idx = builder.CreateSIToFP (int_idx, scalar_t); | |
1505 llvm::Value *cond0 = builder.CreateFCmpUNE (idx, check_idx); | |
1506 llvm::Value *cond1 = builder.CreateICmpSLT (int_idx, one); | |
1507 llvm::Value *cond = builder.CreateOr (cond0, cond1); | |
1508 | |
1509 llvm::BasicBlock *done = fn.new_block ("done"); | |
1510 llvm::BasicBlock *conv_error = fn.new_block ("conv_error", done); | |
1511 llvm::BasicBlock *normal = fn.new_block ("normal", done); | |
1512 builder.CreateCondBr (cond, conv_error, normal); | |
1513 | |
1514 builder.SetInsertPoint (conv_error); | |
1515 ginvalid_index.call (builder); | |
1516 builder.CreateBr (done); | |
1517 | |
1518 builder.SetInsertPoint (normal); | |
1519 llvm::Value *len = builder.CreateExtractValue (mat, | |
1520 llvm::ArrayRef<unsigned> (2)); | |
1521 cond = builder.CreateICmpSGT (int_idx, len); | |
1522 | |
1523 | |
1524 llvm::BasicBlock *bounds_error = fn.new_block ("bounds_error", done); | |
1525 llvm::BasicBlock *success = fn.new_block ("success", done); | |
1526 builder.CreateCondBr (cond, bounds_error, success); | |
1527 | |
1528 builder.SetInsertPoint (bounds_error); | |
1529 gindex_range.call (builder, ione, ione, int_idx, len); | |
1530 builder.CreateBr (done); | |
1531 | |
1532 builder.SetInsertPoint (success); | |
1533 llvm::Value *data = builder.CreateExtractValue (mat, | |
1534 llvm::ArrayRef<unsigned> (1)); | |
1535 llvm::Value *gep = builder.CreateInBoundsGEP (data, int_idx); | |
1536 llvm::Value *ret = builder.CreateLoad (gep); | |
1537 builder.CreateBr (done); | |
1538 | |
1539 builder.SetInsertPoint (done); | |
1540 | |
1541 llvm::PHINode *merge = llvm::PHINode::Create (scalar_t, 3); | |
1542 builder.Insert (merge); | |
1543 merge->addIncoming (undef, conv_error); | |
1544 merge->addIncoming (undef, bounds_error); | |
1545 merge->addIncoming (ret, success); | |
1546 fn.do_return (builder, merge); | |
1547 } | |
1548 paren_subsref_fn.add_overload (fn); | |
1549 | |
1550 // paren subsasgn | |
1551 paren_subsasgn_fn.stash_name ("()subsasgn"); | |
1552 | |
1553 jit_function resize_paren_subsasgn | |
1554 = create_function (jit_convention::external, | |
15027
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1555 "octave_jit_paren_subsasgn_impl", matrix, matrix, index, |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1556 scalar); |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1557 resize_paren_subsasgn.add_mapping (engine, &octave_jit_paren_subsasgn_impl); |
15016 | 1558 fn = create_function (jit_convention::internal, "octave_jit_paren_subsasgn", |
1559 matrix, matrix, scalar, scalar); | |
1560 fn.mark_can_error (); | |
1561 body = fn.new_block (); | |
1562 builder.SetInsertPoint (body); | |
1563 { | |
1564 llvm::Value *one = llvm::ConstantInt::get (index_t, 1); | |
1565 | |
1566 llvm::Value *mat = fn.argument (builder, 0); | |
1567 llvm::Value *idx = fn.argument (builder, 1); | |
1568 llvm::Value *value = fn.argument (builder, 2); | |
1569 | |
1570 llvm::Value *int_idx = builder.CreateFPToSI (idx, index_t); | |
1571 llvm::Value *check_idx = builder.CreateSIToFP (int_idx, scalar_t); | |
1572 llvm::Value *cond0 = builder.CreateFCmpUNE (idx, check_idx); | |
1573 llvm::Value *cond1 = builder.CreateICmpSLT (int_idx, one); | |
1574 llvm::Value *cond = builder.CreateOr (cond0, cond1); | |
1575 | |
1576 llvm::BasicBlock *done = fn.new_block ("done"); | |
1577 | |
1578 llvm::BasicBlock *conv_error = fn.new_block ("conv_error", done); | |
1579 llvm::BasicBlock *normal = fn.new_block ("normal", done); | |
1580 builder.CreateCondBr (cond, conv_error, normal); | |
1581 builder.SetInsertPoint (conv_error); | |
1582 ginvalid_index.call (builder); | |
1583 builder.CreateBr (done); | |
1584 | |
1585 builder.SetInsertPoint (normal); | |
15056
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
1586 llvm::Value *len = builder.CreateExtractValue (mat, 2); |
15016 | 1587 cond0 = builder.CreateICmpSGT (int_idx, len); |
1588 | |
1589 llvm::Value *rcount = builder.CreateExtractValue (mat, 0); | |
1590 rcount = builder.CreateLoad (rcount); | |
1591 cond1 = builder.CreateICmpSGT (rcount, one); | |
1592 cond = builder.CreateOr (cond0, cond1); | |
1593 | |
1594 llvm::BasicBlock *bounds_error = fn.new_block ("bounds_error", done); | |
1595 llvm::BasicBlock *success = fn.new_block ("success", done); | |
1596 builder.CreateCondBr (cond, bounds_error, success); | |
1597 | |
1598 // resize on out of bounds access | |
1599 builder.SetInsertPoint (bounds_error); | |
15027
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1600 llvm::Value *resize_result = resize_paren_subsasgn.call (builder, mat, |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1601 int_idx, value); |
15016 | 1602 builder.CreateBr (done); |
1603 | |
1604 builder.SetInsertPoint (success); | |
1605 llvm::Value *data = builder.CreateExtractValue (mat, | |
1606 llvm::ArrayRef<unsigned> (1)); | |
1607 llvm::Value *gep = builder.CreateInBoundsGEP (data, int_idx); | |
1608 builder.CreateStore (value, gep); | |
1609 builder.CreateBr (done); | |
1610 | |
1611 builder.SetInsertPoint (done); | |
1612 | |
1613 llvm::PHINode *merge = llvm::PHINode::Create (matrix_t, 3); | |
1614 builder.Insert (merge); | |
1615 merge->addIncoming (mat, conv_error); | |
1616 merge->addIncoming (resize_result, bounds_error); | |
1617 merge->addIncoming (mat, success); | |
1618 fn.do_return (builder, merge); | |
1619 } | |
1620 paren_subsasgn_fn.add_overload (fn); | |
1621 | |
1622 fn = create_function (jit_convention::external, | |
1623 "octave_jit_paren_subsasgn_matrix_range", matrix, | |
1624 matrix, range, scalar); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1625 fn.add_mapping (engine, &octave_jit_paren_subsasgn_matrix_range); |
15016 | 1626 fn.mark_can_error (); |
1627 paren_subsasgn_fn.add_overload (fn); | |
1628 | |
15056
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
1629 end_fn.stash_name ("end"); |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
1630 fn = create_function (jit_convention::internal, "octave_jit_end_matrix", |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
1631 scalar, matrix); |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
1632 body = fn.new_block (); |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
1633 builder.SetInsertPoint (body); |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
1634 { |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
1635 llvm::Value *mat = fn.argument (builder, 0); |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
1636 llvm::Value *ret = builder.CreateExtractValue (mat, 2); |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
1637 fn.do_return (builder, builder.CreateSIToFP (ret, scalar_t)); |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
1638 } |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
1639 end_fn.add_overload (fn); |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
1640 |
15016 | 1641 casts[any->type_id ()].stash_name ("(any)"); |
1642 casts[scalar->type_id ()].stash_name ("(scalar)"); | |
1643 casts[complex->type_id ()].stash_name ("(complex)"); | |
1644 casts[matrix->type_id ()].stash_name ("(matrix)"); | |
15027
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1645 casts[any->type_id ()].stash_name ("(range)"); |
15016 | 1646 |
1647 // cast any <- matrix | |
1648 fn = create_function (jit_convention::external, "octave_jit_cast_any_matrix", | |
1649 any, matrix); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1650 fn.add_mapping (engine, &octave_jit_cast_any_matrix); |
15016 | 1651 casts[any->type_id ()].add_overload (fn); |
1652 | |
1653 // cast matrix <- any | |
1654 fn = create_function (jit_convention::external, "octave_jit_cast_matrix_any", | |
1655 matrix, any); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1656 fn.add_mapping (engine, &octave_jit_cast_matrix_any); |
15016 | 1657 casts[matrix->type_id ()].add_overload (fn); |
1658 | |
15027
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1659 // cast any <- range |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1660 fn = create_function (jit_convention::external, "octave_jit_cast_any_range", |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1661 any, range); |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1662 fn.add_mapping (engine, &octave_jit_cast_any_range); |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1663 casts[any->type_id ()].add_overload (fn); |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1664 |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1665 // cast range <- any |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1666 fn = create_function (jit_convention::external, "octave_jit_cast_range_any", |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1667 range, any); |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1668 fn.add_mapping (engine, &octave_jit_cast_range_any); |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1669 casts[range->type_id ()].add_overload (fn); |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1670 |
15016 | 1671 // cast any <- scalar |
1672 fn = create_function (jit_convention::external, "octave_jit_cast_any_scalar", | |
1673 any, scalar); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1674 fn.add_mapping (engine, &octave_jit_cast_any_scalar); |
15016 | 1675 casts[any->type_id ()].add_overload (fn); |
1676 | |
1677 // cast scalar <- any | |
1678 fn = create_function (jit_convention::external, "octave_jit_cast_scalar_any", | |
1679 scalar, any); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1680 fn.add_mapping (engine, &octave_jit_cast_scalar_any); |
15016 | 1681 casts[scalar->type_id ()].add_overload (fn); |
1682 | |
1683 // cast any <- complex | |
1684 fn = create_function (jit_convention::external, "octave_jit_cast_any_complex", | |
1685 any, complex); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1686 fn.add_mapping (engine, &octave_jit_cast_any_complex); |
15016 | 1687 casts[any->type_id ()].add_overload (fn); |
1688 | |
1689 // cast complex <- any | |
1690 fn = create_function (jit_convention::external, "octave_jit_cast_complex_any", | |
1691 complex, any); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1692 fn.add_mapping (engine, &octave_jit_cast_complex_any); |
15016 | 1693 casts[complex->type_id ()].add_overload (fn); |
1694 | |
1695 // cast complex <- scalar | |
1696 fn = create_function (jit_convention::internal, | |
1697 "octave_jit_cast_complex_scalar", complex, scalar); | |
1698 body = fn.new_block (); | |
1699 builder.SetInsertPoint (body); | |
1700 { | |
1701 llvm::Value *zero = llvm::ConstantFP::get (scalar_t, 0); | |
1702 fn.do_return (builder, complex_new (fn.argument (builder, 0), zero)); | |
1703 } | |
1704 casts[complex->type_id ()].add_overload (fn); | |
1705 | |
1706 // cast scalar <- complex | |
1707 fn = create_function (jit_convention::internal, | |
1708 "octave_jit_cast_scalar_complex", scalar, complex); | |
1709 body = fn.new_block (); | |
1710 builder.SetInsertPoint (body); | |
1711 fn.do_return (builder, complex_real (fn.argument (builder, 0))); | |
1712 casts[scalar->type_id ()].add_overload (fn); | |
1713 | |
1714 // cast any <- any | |
1715 fn = create_identity (any); | |
1716 casts[any->type_id ()].add_overload (fn); | |
1717 | |
1718 // cast scalar <- scalar | |
1719 fn = create_identity (scalar); | |
1720 casts[scalar->type_id ()].add_overload (fn); | |
1721 | |
1722 // cast complex <- complex | |
1723 fn = create_identity (complex); | |
1724 casts[complex->type_id ()].add_overload (fn); | |
1725 | |
1726 // -------------------- builtin functions -------------------- | |
1727 add_builtin ("#unknown_function"); | |
1728 unknown_function = builtins["#unknown_function"]; | |
1729 | |
1730 add_builtin ("sin"); | |
1731 register_intrinsic ("sin", llvm::Intrinsic::sin, scalar, scalar); | |
1732 register_generic ("sin", matrix, matrix); | |
1733 | |
1734 add_builtin ("cos"); | |
1735 register_intrinsic ("cos", llvm::Intrinsic::cos, scalar, scalar); | |
1736 register_generic ("cos", matrix, matrix); | |
1737 | |
1738 add_builtin ("exp"); | |
1739 register_intrinsic ("exp", llvm::Intrinsic::cos, scalar, scalar); | |
1740 register_generic ("exp", matrix, matrix); | |
1741 | |
1742 casts.resize (next_id + 1); | |
1743 jit_function any_id = create_identity (any); | |
1744 jit_function release_any = get_release (any); | |
1745 std::vector<jit_type *> args; | |
1746 args.resize (1); | |
1747 | |
1748 for (std::map<std::string, jit_type *>::iterator iter = builtins.begin (); | |
1749 iter != builtins.end (); ++iter) | |
1750 { | |
1751 jit_type *btype = iter->second; | |
1752 args[0] = btype; | |
1753 | |
1754 release_fn.add_overload (jit_function (release_any, 0, args)); | |
1755 casts[any->type_id ()].add_overload (jit_function (any_id, any, args)); | |
1756 | |
1757 args[0] = any; | |
1758 casts[btype->type_id ()].add_overload (jit_function (any_id, btype, | |
1759 args)); | |
1760 } | |
1761 } | |
1762 | |
1763 void | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1764 jit_typeinfo::add_print (jit_type *ty, void *fptr) |
15016 | 1765 { |
1766 std::stringstream name; | |
1767 name << "octave_jit_print_" << ty->name (); | |
1768 jit_function fn = create_function (jit_convention::external, name.str (), 0, | |
1769 intN (8), ty); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1770 fn.add_mapping (engine, fptr); |
15016 | 1771 print_fn.add_overload (fn); |
1772 } | |
1773 | |
1774 // FIXME: cp between add_binary_op, add_binary_icmp, and add_binary_fcmp | |
1775 void | |
1776 jit_typeinfo::add_binary_op (jit_type *ty, int op, int llvm_op) | |
1777 { | |
1778 std::stringstream fname; | |
1779 octave_value::binary_op ov_op = static_cast<octave_value::binary_op>(op); | |
1780 fname << "octave_jit_" << octave_value::binary_op_as_string (ov_op) | |
1781 << "_" << ty->name (); | |
1782 | |
1783 jit_function fn = create_function (jit_convention::internal, fname.str (), | |
1784 ty, ty, ty); | |
1785 llvm::BasicBlock *block = fn.new_block (); | |
1786 builder.SetInsertPoint (block); | |
1787 llvm::Instruction::BinaryOps temp | |
1788 = static_cast<llvm::Instruction::BinaryOps>(llvm_op); | |
1789 | |
1790 llvm::Value *ret = builder.CreateBinOp (temp, fn.argument (builder, 0), | |
1791 fn.argument (builder, 1)); | |
1792 fn.do_return (builder, ret); | |
1793 binary_ops[op].add_overload (fn); | |
1794 } | |
1795 | |
1796 void | |
1797 jit_typeinfo::add_binary_icmp (jit_type *ty, int op, int llvm_op) | |
1798 { | |
1799 std::stringstream fname; | |
1800 octave_value::binary_op ov_op = static_cast<octave_value::binary_op>(op); | |
1801 fname << "octave_jit" << octave_value::binary_op_as_string (ov_op) | |
1802 << "_" << ty->name (); | |
1803 | |
1804 jit_function fn = create_function (jit_convention::internal, fname.str (), | |
1805 boolean, ty, ty); | |
1806 llvm::BasicBlock *block = fn.new_block (); | |
1807 builder.SetInsertPoint (block); | |
1808 llvm::CmpInst::Predicate temp | |
1809 = static_cast<llvm::CmpInst::Predicate>(llvm_op); | |
1810 llvm::Value *ret = builder.CreateICmp (temp, fn.argument (builder, 0), | |
1811 fn.argument (builder, 1)); | |
1812 fn.do_return (builder, ret); | |
1813 binary_ops[op].add_overload (fn); | |
1814 } | |
1815 | |
1816 void | |
1817 jit_typeinfo::add_binary_fcmp (jit_type *ty, int op, int llvm_op) | |
1818 { | |
1819 std::stringstream fname; | |
1820 octave_value::binary_op ov_op = static_cast<octave_value::binary_op>(op); | |
1821 fname << "octave_jit" << octave_value::binary_op_as_string (ov_op) | |
1822 << "_" << ty->name (); | |
1823 | |
1824 jit_function fn = create_function (jit_convention::internal, fname.str (), | |
1825 boolean, ty, ty); | |
1826 llvm::BasicBlock *block = fn.new_block (); | |
1827 builder.SetInsertPoint (block); | |
1828 llvm::CmpInst::Predicate temp | |
1829 = static_cast<llvm::CmpInst::Predicate>(llvm_op); | |
1830 llvm::Value *ret = builder.CreateFCmp (temp, fn.argument (builder, 0), | |
1831 fn.argument (builder, 1)); | |
1832 fn.do_return (builder, ret); | |
1833 binary_ops[op].add_overload (fn); | |
1834 } | |
1835 | |
1836 jit_function | |
1837 jit_typeinfo::create_function (jit_convention::type cc, const llvm::Twine& name, | |
1838 jit_type *ret, | |
1839 const std::vector<jit_type *>& args) | |
1840 { | |
1841 jit_function result (module, cc, name, ret, args); | |
1842 return result; | |
1843 } | |
1844 | |
1845 jit_function | |
1846 jit_typeinfo::create_identity (jit_type *type) | |
1847 { | |
1848 size_t id = type->type_id (); | |
1849 if (id >= identities.size ()) | |
1850 identities.resize (id + 1); | |
1851 | |
1852 if (! identities[id].valid ()) | |
1853 { | |
1854 jit_function fn = create_function (jit_convention::internal, "id", type, | |
1855 type); | |
1856 llvm::BasicBlock *body = fn.new_block (); | |
1857 builder.SetInsertPoint (body); | |
1858 fn.do_return (builder, fn.argument (builder, 0)); | |
1859 return identities[id] = fn; | |
1860 } | |
1861 | |
1862 return identities[id]; | |
1863 } | |
1864 | |
1865 llvm::Value * | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1866 jit_typeinfo::do_insert_error_check (llvm::IRBuilderD& abuilder) |
15016 | 1867 { |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1868 return abuilder.CreateLoad (lerror_state); |
15016 | 1869 } |
1870 | |
1871 void | |
1872 jit_typeinfo::add_builtin (const std::string& name) | |
1873 { | |
1874 jit_type *btype = new_type (name, any, any->to_llvm ()); | |
1875 builtins[name] = btype; | |
1876 | |
1877 octave_builtin *ov_builtin = find_builtin (name); | |
1878 if (ov_builtin) | |
1879 ov_builtin->stash_jit (*btype); | |
1880 } | |
1881 | |
1882 void | |
1883 jit_typeinfo::register_intrinsic (const std::string& name, size_t iid, | |
1884 jit_type *result, | |
1885 const std::vector<jit_type *>& args) | |
1886 { | |
1887 jit_type *builtin_type = builtins[name]; | |
1888 size_t nargs = args.size (); | |
1889 llvm::SmallVector<llvm::Type *, 5> llvm_args (nargs); | |
1890 for (size_t i = 0; i < nargs; ++i) | |
1891 llvm_args[i] = args[i]->to_llvm (); | |
1892 | |
1893 llvm::Intrinsic::ID id = static_cast<llvm::Intrinsic::ID> (iid); | |
1894 llvm::Function *ifun = llvm::Intrinsic::getDeclaration (module, id, | |
1895 llvm_args); | |
1896 std::stringstream fn_name; | |
1897 fn_name << "octave_jit_" << name; | |
1898 | |
1899 std::vector<jit_type *> args1 (nargs + 1); | |
1900 args1[0] = builtin_type; | |
1901 std::copy (args.begin (), args.end (), args1.begin () + 1); | |
1902 | |
1903 // The first argument will be the Octave function, but we already know that | |
1904 // the function call is the equivalent of the intrinsic, so we ignore it and | |
1905 // call the intrinsic with the remaining arguments. | |
1906 jit_function fn = create_function (jit_convention::internal, fn_name.str (), | |
1907 result, args1); | |
1908 llvm::BasicBlock *body = fn.new_block (); | |
1909 builder.SetInsertPoint (body); | |
1910 | |
1911 llvm::SmallVector<llvm::Value *, 5> fargs (nargs); | |
1912 for (size_t i = 0; i < nargs; ++i) | |
1913 fargs[i] = fn.argument (builder, i + 1); | |
1914 | |
1915 llvm::Value *ret = builder.CreateCall (ifun, fargs); | |
1916 fn.do_return (builder, ret); | |
1917 paren_subsref_fn.add_overload (fn); | |
1918 } | |
1919 | |
1920 octave_builtin * | |
1921 jit_typeinfo::find_builtin (const std::string& name) | |
1922 { | |
1923 // FIXME: Finalize what we want to store in octave_builtin, then add functions | |
1924 // to access these values in octave_value | |
1925 octave_value ov_builtin = symbol_table::find (name); | |
1926 return dynamic_cast<octave_builtin *> (ov_builtin.internal_rep ()); | |
1927 } | |
1928 | |
1929 void | |
1930 jit_typeinfo::register_generic (const std::string&, jit_type *, | |
1931 const std::vector<jit_type *>&) | |
1932 { | |
1933 // FIXME: Implement | |
1934 } | |
1935 | |
1936 jit_function | |
1937 jit_typeinfo::mirror_binary (const jit_function& fn) | |
1938 { | |
1939 jit_function ret = create_function (jit_convention::internal, | |
1940 fn.name () + "_reverse", | |
1941 fn.result (), fn.argument_type (1), | |
1942 fn.argument_type (0)); | |
1943 if (fn.can_error ()) | |
1944 ret.mark_can_error (); | |
1945 | |
1946 llvm::BasicBlock *body = ret.new_block (); | |
1947 builder.SetInsertPoint (body); | |
1948 llvm::Value *result = fn.call (builder, ret.argument (builder, 1), | |
1949 ret.argument (builder, 0)); | |
1950 if (ret.result ()) | |
1951 ret.do_return (builder, result); | |
1952 else | |
1953 ret.do_return (builder); | |
1954 | |
1955 return ret; | |
1956 } | |
1957 | |
1958 llvm::Value * | |
1959 jit_typeinfo::pack_complex (llvm::IRBuilderD& bld, llvm::Value *cplx) | |
1960 { | |
1961 llvm::Type *complex_ret = instance->complex_ret; | |
1962 llvm::Value *real = bld.CreateExtractElement (cplx, bld.getInt32 (0)); | |
1963 llvm::Value *imag = bld.CreateExtractElement (cplx, bld.getInt32 (1)); | |
1964 llvm::Value *ret = llvm::UndefValue::get (complex_ret); | |
1965 ret = bld.CreateInsertValue (ret, real, 0); | |
1966 return bld.CreateInsertValue (ret, imag, 1); | |
1967 } | |
1968 | |
1969 llvm::Value * | |
1970 jit_typeinfo::unpack_complex (llvm::IRBuilderD& bld, llvm::Value *result) | |
1971 { | |
1972 llvm::Type *complex_t = get_complex ()->to_llvm (); | |
1973 llvm::Value *real = bld.CreateExtractValue (result, 0); | |
1974 llvm::Value *imag = bld.CreateExtractValue (result, 1); | |
1975 llvm::Value *ret = llvm::UndefValue::get (complex_t); | |
1976 ret = bld.CreateInsertElement (ret, real, bld.getInt32 (0)); | |
1977 return bld.CreateInsertElement (ret, imag, bld.getInt32 (1)); | |
1978 } | |
1979 | |
1980 llvm::Value * | |
1981 jit_typeinfo::complex_real (llvm::Value *cx) | |
1982 { | |
1983 return builder.CreateExtractElement (cx, builder.getInt32 (0)); | |
1984 } | |
1985 | |
1986 llvm::Value * | |
1987 jit_typeinfo::complex_real (llvm::Value *cx, llvm::Value *real) | |
1988 { | |
1989 return builder.CreateInsertElement (cx, real, builder.getInt32 (0)); | |
1990 } | |
1991 | |
1992 llvm::Value * | |
1993 jit_typeinfo::complex_imag (llvm::Value *cx) | |
1994 { | |
1995 return builder.CreateExtractElement (cx, builder.getInt32 (1)); | |
1996 } | |
1997 | |
1998 llvm::Value * | |
1999 jit_typeinfo::complex_imag (llvm::Value *cx, llvm::Value *imag) | |
2000 { | |
2001 return builder.CreateInsertElement (cx, imag, builder.getInt32 (1)); | |
2002 } | |
2003 | |
2004 llvm::Value * | |
2005 jit_typeinfo::complex_new (llvm::Value *real, llvm::Value *imag) | |
2006 { | |
2007 llvm::Value *ret = llvm::UndefValue::get (complex->to_llvm ()); | |
2008 ret = complex_real (ret, real); | |
2009 return complex_imag (ret, imag); | |
2010 } | |
2011 | |
2012 void | |
2013 jit_typeinfo::create_int (size_t nbits) | |
2014 { | |
2015 std::stringstream tname; | |
2016 tname << "int" << nbits; | |
2017 ints[nbits] = new_type (tname.str (), any, llvm::Type::getIntNTy (context, | |
2018 nbits)); | |
2019 } | |
2020 | |
2021 jit_type * | |
2022 jit_typeinfo::intN (size_t nbits) const | |
2023 { | |
2024 std::map<size_t, jit_type *>::const_iterator iter = ints.find (nbits); | |
2025 if (iter != ints.end ()) | |
2026 return iter->second; | |
2027 | |
2028 throw jit_fail_exception ("No such integer type"); | |
2029 } | |
2030 | |
2031 jit_type * | |
2032 jit_typeinfo::do_type_of (const octave_value &ov) const | |
2033 { | |
2034 if (ov.is_function ()) | |
2035 { | |
2036 // FIXME: This is ugly, we need to finalize how we want to to this, then | |
2037 // have octave_value fully support the needed functionality | |
2038 octave_builtin *builtin | |
2039 = dynamic_cast<octave_builtin *> (ov.internal_rep ()); | |
2040 return builtin && builtin->to_jit () ? builtin->to_jit () | |
2041 : unknown_function; | |
2042 } | |
2043 | |
2044 if (ov.is_range ()) | |
2045 return get_range (); | |
2046 | |
2047 if (ov.is_double_type ()) | |
2048 { | |
2049 if (ov.is_real_scalar ()) | |
2050 return get_scalar (); | |
2051 | |
2052 if (ov.is_matrix_type ()) | |
2053 return get_matrix (); | |
2054 } | |
2055 | |
2056 if (ov.is_complex_scalar ()) | |
2057 return get_complex (); | |
2058 | |
2059 return get_any (); | |
2060 } | |
2061 | |
2062 jit_type* | |
2063 jit_typeinfo::new_type (const std::string& name, jit_type *parent, | |
2064 llvm::Type *llvm_type) | |
2065 { | |
2066 jit_type *ret = new jit_type (name, parent, llvm_type, next_id++); | |
2067 id_to_type.push_back (ret); | |
2068 return ret; | |
2069 } | |
2070 | |
2071 #endif |