Mercurial > octave-nkf
annotate src/jit-typeinfo.cc @ 15027:741d2dbcc117
Check trip count before compiling for loops.
* src/jit-typeinfo.cc (octave_jit_cast_any_range, octave_jit_cast_range_any):
New function.
(octave_jit_paren_subsasgn_impl): Add return argument.
(jit_typeinfo::jit_typeinfo): Update octave_jit_paren_subsasgn_impl call and add
any <-> range casts.
* src/pt-eval.cc (tree_evaluator::visit_simple_for_command): Try jit after
computing loop bounds.
* src/pt-jit.cc (jit_convert::jit_convert): Add and handle for bounds argument.
(jit_convert::visit_binary_expression): Use next_shortcircut_result.
(jit_convert::visit_simple_for_command): Use next_iterator and check for
precomputed bounds.
(jit_convert::find_variable, jit_convert::create_variable,
jit_convert::next_name, tree_jit::trip_count, jit_info::initialize,
jit_info::find): New function.
(jit_convert::get_variable): Use find_variable and create_variable.
(tree_jit::execute): Allow for precomputed loop bounds and check trip count.
(jit_info::jit_info): Added new overload and defer work to initialize.
(jit_info::execute): Support precomputed bounds.
(jit_info::match): Support precomputed bounds.
* src/pt-jit.h (jit_convert::jit_convert, jit_convert::execute,
jit_info::execute, jit_info::match): New parameter.
(jit_convert::find_variable, jit_convert::create_variable,
tree_jit::trip_count, jit_info::initialize, jit_info::find): New declaration.
(jit_convert::next_iterator, jit_convert::next_for_bounds,
jit_convert::next_shortcircut_result, jit_convert::next_name): New function.
author | Max Brister <max@2bass.com> |
---|---|
date | Thu, 26 Jul 2012 17:03:15 -0500 |
parents | ae3670d4df29 |
children | bc32288f4a42 |
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 | |
246 extern "C" void | |
247 octave_jit_paren_subsasgn_matrix_range (jit_matrix *result, jit_matrix *mat, | |
248 jit_range *index, double value) | |
249 { | |
250 NDArray *array = mat->array; | |
251 bool done = false; | |
252 | |
253 // optimize for the simple case (no resizing and no errors) | |
254 if (*array->jit_ref_count () == 1 | |
255 && index->all_elements_are_ints ()) | |
256 { | |
257 // this code is similar to idx_vector::fill, but we avoid allocating an | |
258 // idx_vector and its associated rep | |
259 octave_idx_type start = static_cast<octave_idx_type> (index->base) - 1; | |
260 octave_idx_type step = static_cast<octave_idx_type> (index->inc); | |
261 octave_idx_type nelem = index->nelem; | |
262 octave_idx_type final = start + nelem * step; | |
263 if (step < 0) | |
264 { | |
265 step = -step; | |
266 std::swap (final, start); | |
267 } | |
268 | |
269 if (start >= 0 && final < mat->slice_len) | |
270 { | |
271 done = true; | |
272 | |
273 double *data = array->jit_slice_data (); | |
274 if (step == 1) | |
275 std::fill (data + start, data + start + nelem, value); | |
276 else | |
277 { | |
278 for (octave_idx_type i = start; i < final; i += step) | |
279 data[i] = value; | |
280 } | |
281 } | |
282 } | |
283 | |
284 if (! done) | |
285 { | |
286 idx_vector idx (*index); | |
287 NDArray avalue (dim_vector (1, 1)); | |
288 avalue.xelem (0) = value; | |
289 array->assign (idx, avalue); | |
290 } | |
291 | |
292 result->update (array); | |
293 } | |
294 | |
295 extern "C" Complex | |
296 octave_jit_complex_div (Complex lhs, Complex rhs) | |
297 { | |
298 // see src/OPERATORS/op-cs-cs.cc | |
299 if (rhs == 0.0) | |
300 gripe_divide_by_zero (); | |
301 | |
302 return lhs / rhs; | |
303 } | |
304 | |
305 // FIXME: CP form src/xpow.cc | |
306 static inline int | |
307 xisint (double x) | |
308 { | |
309 return (D_NINT (x) == x | |
310 && ((x >= 0 && x < INT_MAX) | |
311 || (x <= 0 && x > INT_MIN))); | |
312 } | |
313 | |
314 extern "C" Complex | |
315 octave_jit_pow_scalar_scalar (double lhs, double rhs) | |
316 { | |
317 // FIXME: almost CP from src/xpow.cc | |
318 if (lhs < 0.0 && ! xisint (rhs)) | |
319 return std::pow (Complex (lhs), rhs); | |
320 return std::pow (lhs, rhs); | |
321 } | |
322 | |
323 extern "C" Complex | |
324 octave_jit_pow_complex_complex (Complex lhs, Complex rhs) | |
325 { | |
326 if (lhs.imag () == 0 && rhs.imag () == 0) | |
327 return octave_jit_pow_scalar_scalar (lhs.real (), rhs.real ()); | |
328 return std::pow (lhs, rhs); | |
329 } | |
330 | |
331 extern "C" Complex | |
332 octave_jit_pow_complex_scalar (Complex lhs, double rhs) | |
333 { | |
334 if (lhs.imag () == 0) | |
335 return octave_jit_pow_scalar_scalar (lhs.real (), rhs); | |
336 return std::pow (lhs, rhs); | |
337 } | |
338 | |
339 extern "C" Complex | |
340 octave_jit_pow_scalar_complex (double lhs, Complex rhs) | |
341 { | |
342 if (rhs.imag () == 0) | |
343 return octave_jit_pow_scalar_scalar (lhs, rhs.real ()); | |
344 return std::pow (lhs, rhs); | |
345 } | |
346 | |
347 extern "C" void | |
348 octave_jit_print_matrix (jit_matrix *m) | |
349 { | |
350 std::cout << *m << std::endl; | |
351 } | |
352 | |
353 static void | |
354 gripe_bad_result (void) | |
355 { | |
356 error ("incorrect type information given to the JIT compiler"); | |
357 } | |
358 | |
359 // FIXME: Add support for multiple outputs | |
360 extern "C" octave_base_value * | |
361 octave_jit_call (octave_builtin::fcn fn, size_t nargin, | |
362 octave_base_value **argin, jit_type *result_type) | |
363 { | |
364 octave_value_list ovl (nargin); | |
365 for (size_t i = 0; i < nargin; ++i) | |
366 ovl.xelem (i) = octave_value (argin[i]); | |
367 | |
368 ovl = fn (ovl, 1); | |
369 | |
370 // These type checks are not strictly required, but I'm guessing that | |
371 // incorrect types will be entered on occasion. This will be very difficult to | |
372 // debug unless we do the sanity check here. | |
373 if (result_type) | |
374 { | |
375 if (ovl.length () != 1) | |
376 { | |
377 gripe_bad_result (); | |
378 return 0; | |
379 } | |
380 | |
381 octave_value& result = ovl.xelem (0); | |
382 jit_type *jtype = jit_typeinfo::join (jit_typeinfo::type_of (result), | |
383 result_type); | |
384 if (jtype != result_type) | |
385 { | |
386 gripe_bad_result (); | |
387 return 0; | |
388 } | |
389 | |
390 octave_base_value *ret = result.internal_rep (); | |
391 ret->grab (); | |
392 return ret; | |
393 } | |
394 | |
395 if (! (ovl.length () == 0 | |
396 || (ovl.length () == 1 && ovl.xelem (0).is_undefined ()))) | |
397 gripe_bad_result (); | |
398 | |
399 return 0; | |
400 } | |
401 | |
402 // -------------------- jit_range -------------------- | |
403 bool | |
404 jit_range::all_elements_are_ints () const | |
405 { | |
406 Range r (*this); | |
407 return r.all_elements_are_ints (); | |
408 } | |
409 | |
410 std::ostream& | |
411 operator<< (std::ostream& os, const jit_range& rng) | |
412 { | |
413 return os << "Range[" << rng.base << ", " << rng.limit << ", " << rng.inc | |
414 << ", " << rng.nelem << "]"; | |
415 } | |
416 | |
417 // -------------------- jit_matrix -------------------- | |
418 | |
419 std::ostream& | |
420 operator<< (std::ostream& os, const jit_matrix& mat) | |
421 { | |
422 return os << "Matrix[" << mat.ref_count << ", " << mat.slice_data << ", " | |
423 << mat.slice_len << ", " << mat.dimensions << ", " | |
424 << mat.array << "]"; | |
425 } | |
426 | |
427 // -------------------- jit_type -------------------- | |
428 jit_type::jit_type (const std::string& aname, jit_type *aparent, | |
429 llvm::Type *allvm_type, int aid) : | |
430 mname (aname), mparent (aparent), llvm_type (allvm_type), mid (aid), | |
431 mdepth (aparent ? aparent->mdepth + 1 : 0) | |
432 { | |
433 std::memset (msret, 0, sizeof (msret)); | |
434 std::memset (mpointer_arg, 0, sizeof (mpointer_arg)); | |
435 std::memset (mpack, 0, sizeof (mpack)); | |
436 std::memset (munpack, 0, sizeof (munpack)); | |
437 | |
438 for (size_t i = 0; i < jit_convention::length; ++i) | |
439 mpacked_type[i] = llvm_type; | |
440 } | |
441 | |
442 llvm::Type * | |
443 jit_type::to_llvm_arg (void) const | |
444 { | |
445 return llvm_type ? llvm_type->getPointerTo () : 0; | |
446 } | |
447 | |
448 // -------------------- jit_function -------------------- | |
449 jit_function::jit_function () : module (0), llvm_function (0), mresult (0), | |
450 call_conv (jit_convention::length), | |
451 mcan_error (false) | |
452 {} | |
453 | |
454 jit_function::jit_function (llvm::Module *amodule, | |
455 jit_convention::type acall_conv, | |
456 const llvm::Twine& aname, jit_type *aresult, | |
457 const std::vector<jit_type *>& aargs) | |
458 : module (amodule), mresult (aresult), args (aargs), call_conv (acall_conv), | |
459 mcan_error (false) | |
460 { | |
461 llvm::SmallVector<llvm::Type *, 15> llvm_args; | |
462 | |
463 llvm::Type *rtype = llvm::Type::getVoidTy (context); | |
464 if (mresult) | |
465 { | |
466 rtype = mresult->packed_type (call_conv); | |
467 if (sret ()) | |
468 { | |
469 llvm_args.push_back (rtype->getPointerTo ()); | |
470 rtype = llvm::Type::getVoidTy (context); | |
471 } | |
472 } | |
473 | |
474 for (std::vector<jit_type *>::const_iterator iter = args.begin (); | |
475 iter != args.end (); ++iter) | |
476 { | |
477 jit_type *ty = *iter; | |
478 assert (ty); | |
479 llvm::Type *argty = ty->packed_type (call_conv); | |
480 if (ty->pointer_arg (call_conv)) | |
481 argty = argty->getPointerTo (); | |
482 | |
483 llvm_args.push_back (argty); | |
484 } | |
485 | |
486 // we mark all functinos as external linkage because this prevents llvm | |
487 // from getting rid of always inline functions | |
488 llvm::FunctionType *ft = llvm::FunctionType::get (rtype, llvm_args, false); | |
489 llvm_function = llvm::Function::Create (ft, llvm::Function::ExternalLinkage, | |
490 aname, module); | |
491 if (call_conv == jit_convention::internal) | |
492 llvm_function->addFnAttr (llvm::Attribute::AlwaysInline); | |
493 } | |
494 | |
495 jit_function::jit_function (const jit_function& fn, jit_type *aresult, | |
496 const std::vector<jit_type *>& aargs) | |
497 : module (fn.module), llvm_function (fn.llvm_function), mresult (aresult), | |
498 args (aargs), call_conv (fn.call_conv), mcan_error (fn.mcan_error) | |
499 { | |
500 } | |
501 | |
502 jit_function::jit_function (const jit_function& fn) | |
503 : module (fn.module), llvm_function (fn.llvm_function), mresult (fn.mresult), | |
504 args (fn.args), call_conv (fn.call_conv), mcan_error (fn.mcan_error) | |
505 {} | |
506 | |
507 std::string | |
508 jit_function::name (void) const | |
509 { | |
510 return llvm_function->getName (); | |
511 } | |
512 | |
513 llvm::BasicBlock * | |
514 jit_function::new_block (const std::string& aname, | |
515 llvm::BasicBlock *insert_before) | |
516 { | |
517 return llvm::BasicBlock::Create (context, aname, llvm_function, | |
518 insert_before); | |
519 } | |
520 | |
521 llvm::Value * | |
522 jit_function::call (llvm::IRBuilderD& builder, | |
523 const std::vector<jit_value *>& in_args) const | |
524 { | |
525 assert (in_args.size () == args.size ()); | |
526 | |
527 std::vector<llvm::Value *> llvm_args (args.size ()); | |
528 for (size_t i = 0; i < in_args.size (); ++i) | |
529 llvm_args[i] = in_args[i]->to_llvm (); | |
530 | |
531 return call (builder, llvm_args); | |
532 } | |
533 | |
534 llvm::Value * | |
535 jit_function::call (llvm::IRBuilderD& builder, | |
536 const std::vector<llvm::Value *>& in_args) const | |
537 { | |
538 assert (valid ()); | |
539 assert (in_args.size () == args.size ()); | |
540 llvm::Function *stacksave | |
541 = llvm::Intrinsic::getDeclaration (module, llvm::Intrinsic::stacksave); | |
542 llvm::SmallVector<llvm::Value *, 10> llvm_args; | |
543 llvm_args.reserve (in_args.size () + sret ()); | |
544 | |
545 llvm::Value *sret_mem = 0; | |
546 llvm::Value *saved_stack = 0; | |
547 if (sret ()) | |
548 { | |
549 saved_stack = builder.CreateCall (stacksave); | |
550 sret_mem = builder.CreateAlloca (mresult->packed_type (call_conv)); | |
551 llvm_args.push_back (sret_mem); | |
552 } | |
553 | |
554 for (size_t i = 0; i < in_args.size (); ++i) | |
555 { | |
556 llvm::Value *arg = in_args[i]; | |
557 jit_type::convert_fn convert = args[i]->pack (call_conv); | |
558 if (convert) | |
559 arg = convert (builder, arg); | |
560 | |
561 if (args[i]->pointer_arg (call_conv)) | |
562 { | |
563 if (! saved_stack) | |
564 saved_stack = builder.CreateCall (stacksave); | |
565 | |
566 arg = builder.CreateAlloca (args[i]->to_llvm ()); | |
567 builder.CreateStore (in_args[i], arg); | |
568 } | |
569 | |
570 llvm_args.push_back (arg); | |
571 } | |
572 | |
573 llvm::Value *ret = builder.CreateCall (llvm_function, llvm_args); | |
574 if (sret_mem) | |
575 ret = builder.CreateLoad (sret_mem); | |
576 | |
577 if (mresult) | |
578 { | |
579 jit_type::convert_fn unpack = mresult->unpack (call_conv); | |
580 if (unpack) | |
581 ret = unpack (builder, ret); | |
582 } | |
583 | |
584 if (saved_stack) | |
585 { | |
586 llvm::Function *stackrestore | |
587 = llvm::Intrinsic::getDeclaration (module, | |
588 llvm::Intrinsic::stackrestore); | |
589 builder.CreateCall (stackrestore, saved_stack); | |
590 } | |
591 | |
592 return ret; | |
593 } | |
594 | |
595 llvm::Value * | |
596 jit_function::argument (llvm::IRBuilderD& builder, size_t idx) const | |
597 { | |
598 assert (idx < args.size ()); | |
599 | |
600 // FIXME: We should be treating arguments like a list, not a vector. Shouldn't | |
601 // matter much for now, as the number of arguments shouldn't be much bigger | |
602 // than 4 | |
603 llvm::Function::arg_iterator iter = llvm_function->arg_begin (); | |
604 if (sret ()) | |
605 ++iter; | |
606 | |
607 for (size_t i = 0; i < idx; ++i, ++iter); | |
608 | |
609 if (args[idx]->pointer_arg (call_conv)) | |
610 return builder.CreateLoad (iter); | |
611 | |
612 return iter; | |
613 } | |
614 | |
615 void | |
616 jit_function::do_return (llvm::IRBuilderD& builder, llvm::Value *rval) | |
617 { | |
618 assert (! rval == ! mresult); | |
619 | |
620 if (rval) | |
621 { | |
622 jit_type::convert_fn convert = mresult->pack (call_conv); | |
623 if (convert) | |
624 rval = convert (builder, rval); | |
625 | |
626 if (sret ()) | |
627 builder.CreateStore (rval, llvm_function->arg_begin ()); | |
628 else | |
629 builder.CreateRet (rval); | |
630 } | |
631 else | |
632 builder.CreateRetVoid (); | |
633 | |
634 llvm::verifyFunction (*llvm_function); | |
635 } | |
636 | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
637 void |
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
638 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
|
639 { |
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
640 assert (valid ()); |
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
641 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
|
642 } |
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
643 |
15016 | 644 std::ostream& |
645 operator<< (std::ostream& os, const jit_function& fn) | |
646 { | |
647 llvm::Function *lfn = fn.to_llvm (); | |
648 os << "jit_function: cc=" << fn.call_conv; | |
649 llvm::raw_os_ostream llvm_out (os); | |
650 lfn->print (llvm_out); | |
651 llvm_out.flush (); | |
652 return os; | |
653 } | |
654 | |
655 // -------------------- jit_operation -------------------- | |
656 void | |
657 jit_operation::add_overload (const jit_function& func, | |
658 const std::vector<jit_type*>& args) | |
659 { | |
660 if (args.size () >= overloads.size ()) | |
661 overloads.resize (args.size () + 1); | |
662 | |
663 Array<jit_function>& over = overloads[args.size ()]; | |
664 dim_vector dv (over.dims ()); | |
665 Array<octave_idx_type> idx = to_idx (args); | |
666 bool must_resize = false; | |
667 | |
668 if (dv.length () != idx.numel ()) | |
669 { | |
670 dv.resize (idx.numel ()); | |
671 must_resize = true; | |
672 } | |
673 | |
674 for (octave_idx_type i = 0; i < dv.length (); ++i) | |
675 if (dv(i) <= idx(i)) | |
676 { | |
677 must_resize = true; | |
678 dv(i) = idx(i) + 1; | |
679 } | |
680 | |
681 if (must_resize) | |
682 over.resize (dv); | |
683 | |
684 over(idx) = func; | |
685 } | |
686 | |
687 const jit_function& | |
688 jit_operation::overload (const std::vector<jit_type*>& types) const | |
689 { | |
690 // FIXME: We should search for the next best overload on failure | |
691 static jit_function null_overload; | |
692 if (types.size () >= overloads.size ()) | |
693 return null_overload; | |
694 | |
695 for (size_t i =0; i < types.size (); ++i) | |
696 if (! types[i]) | |
697 return null_overload; | |
698 | |
699 const Array<jit_function>& over = overloads[types.size ()]; | |
700 dim_vector dv (over.dims ()); | |
701 Array<octave_idx_type> idx = to_idx (types); | |
702 for (octave_idx_type i = 0; i < dv.length (); ++i) | |
703 if (idx(i) >= dv(i)) | |
704 return null_overload; | |
705 | |
706 return over(idx); | |
707 } | |
708 | |
709 Array<octave_idx_type> | |
710 jit_operation::to_idx (const std::vector<jit_type*>& types) const | |
711 { | |
712 octave_idx_type numel = types.size (); | |
713 if (numel == 1) | |
714 numel = 2; | |
715 | |
716 Array<octave_idx_type> idx (dim_vector (1, numel)); | |
717 for (octave_idx_type i = 0; i < static_cast<octave_idx_type> (types.size ()); | |
718 ++i) | |
719 idx(i) = types[i]->type_id (); | |
720 | |
721 if (types.size () == 1) | |
722 { | |
723 idx(1) = idx(0); | |
724 idx(0) = 0; | |
725 } | |
726 | |
727 return idx; | |
728 } | |
729 | |
730 // -------------------- jit_typeinfo -------------------- | |
731 void | |
732 jit_typeinfo::initialize (llvm::Module *m, llvm::ExecutionEngine *e) | |
733 { | |
734 new jit_typeinfo (m, e); | |
735 } | |
736 | |
737 jit_typeinfo::jit_typeinfo (llvm::Module *m, llvm::ExecutionEngine *e) | |
738 : module (m), engine (e), next_id (0), | |
739 builder (*new llvm::IRBuilderD (context)) | |
740 { | |
741 instance = this; | |
742 | |
743 // FIXME: We should be registering types like in octave_value_typeinfo | |
744 llvm::Type *any_t = llvm::StructType::create (context, "octave_base_value"); | |
745 any_t = any_t->getPointerTo (); | |
746 | |
747 llvm::Type *scalar_t = llvm::Type::getDoubleTy (context); | |
748 llvm::Type *bool_t = llvm::Type::getInt1Ty (context); | |
749 llvm::Type *string_t = llvm::Type::getInt8Ty (context); | |
750 string_t = string_t->getPointerTo (); | |
751 llvm::Type *index_t = llvm::Type::getIntNTy (context, | |
752 sizeof(octave_idx_type) * 8); | |
753 | |
754 llvm::StructType *range_t = llvm::StructType::create (context, "range"); | |
755 std::vector<llvm::Type *> range_contents (4, scalar_t); | |
756 range_contents[3] = index_t; | |
757 range_t->setBody (range_contents); | |
758 | |
759 llvm::Type *refcount_t = llvm::Type::getIntNTy (context, sizeof(int) * 8); | |
760 | |
761 llvm::StructType *matrix_t = llvm::StructType::create (context, "matrix"); | |
762 llvm::Type *matrix_contents[5]; | |
763 matrix_contents[0] = refcount_t->getPointerTo (); | |
764 matrix_contents[1] = scalar_t->getPointerTo (); | |
765 matrix_contents[2] = index_t; | |
766 matrix_contents[3] = index_t->getPointerTo (); | |
767 matrix_contents[4] = string_t; | |
768 matrix_t->setBody (llvm::makeArrayRef (matrix_contents, 5)); | |
769 | |
770 llvm::Type *complex_t = llvm::VectorType::get (scalar_t, 2); | |
771 | |
772 // complex_ret is what is passed to C functions in order to get calling | |
773 // convention right | |
774 complex_ret = llvm::StructType::create (context, "complex_ret"); | |
775 llvm::Type *complex_ret_contents[] = {scalar_t, scalar_t}; | |
776 complex_ret->setBody (complex_ret_contents); | |
777 | |
778 // create types | |
779 any = new_type ("any", 0, any_t); | |
780 matrix = new_type ("matrix", any, matrix_t); | |
781 complex = new_type ("complex", any, complex_t); | |
782 scalar = new_type ("scalar", complex, scalar_t); | |
783 range = new_type ("range", any, range_t); | |
784 string = new_type ("string", any, string_t); | |
785 boolean = new_type ("bool", any, bool_t); | |
786 index = new_type ("index", any, index_t); | |
787 | |
788 create_int (8); | |
789 create_int (16); | |
790 create_int (32); | |
791 create_int (64); | |
792 | |
793 casts.resize (next_id + 1); | |
794 identities.resize (next_id + 1); | |
795 | |
796 // specify calling conventions | |
797 // FIXME: We should detect architecture and do something sane based on that | |
798 // here we assume x86 or x86_64 | |
799 matrix->mark_sret (); | |
800 matrix->mark_pointer_arg (); | |
801 | |
802 range->mark_sret (); | |
803 range->mark_pointer_arg (); | |
804 | |
805 complex->set_pack (jit_convention::external, &jit_typeinfo::pack_complex); | |
806 complex->set_unpack (jit_convention::external, &jit_typeinfo::unpack_complex); | |
807 complex->set_packed_type (jit_convention::external, complex_ret); | |
808 | |
809 if (sizeof (void *) == 4) | |
810 complex->mark_sret (); | |
811 | |
812 // bind global variables | |
813 lerror_state = new llvm::GlobalVariable (*module, bool_t, false, | |
814 llvm::GlobalValue::ExternalLinkage, | |
815 0, "error_state"); | |
816 engine->addGlobalMapping (lerror_state, | |
817 reinterpret_cast<void *> (&error_state)); | |
818 | |
819 // any with anything is an any op | |
820 jit_function fn; | |
821 jit_type *binary_op_type = intN (sizeof (octave_value::binary_op) * 8); | |
822 llvm::Type *llvm_bo_type = binary_op_type->to_llvm (); | |
823 jit_function any_binary = create_function (jit_convention::external, | |
824 "octave_jit_binary_any_any", | |
825 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
|
826 any_binary.add_mapping (engine, &octave_jit_binary_any_any); |
15016 | 827 any_binary.mark_can_error (); |
828 binary_ops.resize (octave_value::num_binary_ops); | |
829 for (size_t i = 0; i < octave_value::num_binary_ops; ++i) | |
830 { | |
831 octave_value::binary_op op = static_cast<octave_value::binary_op> (i); | |
832 std::string op_name = octave_value::binary_op_as_string (op); | |
833 binary_ops[i].stash_name ("binary" + op_name); | |
834 } | |
835 | |
836 for (int op = 0; op < octave_value::num_binary_ops; ++op) | |
837 { | |
838 llvm::Twine fn_name ("octave_jit_binary_any_any_"); | |
839 fn_name = fn_name + llvm::Twine (op); | |
840 | |
841 fn = create_function (jit_convention::internal, fn_name, any, any, any); | |
842 fn.mark_can_error (); | |
843 llvm::BasicBlock *block = fn.new_block (); | |
844 builder.SetInsertPoint (block); | |
845 llvm::APInt op_int(sizeof (octave_value::binary_op) * 8, op, | |
846 std::numeric_limits<octave_value::binary_op>::is_signed); | |
847 llvm::Value *op_as_llvm = llvm::ConstantInt::get (llvm_bo_type, op_int); | |
848 llvm::Value *ret = any_binary.call (builder, op_as_llvm, | |
849 fn.argument (builder, 0), | |
850 fn.argument (builder, 1)); | |
851 fn.do_return (builder, ret); | |
852 binary_ops[op].add_overload (fn); | |
853 } | |
854 | |
855 // grab any | |
856 fn = create_function (jit_convention::external, "octave_jit_grab_any", any, | |
857 any); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
858 fn.add_mapping (engine, &octave_jit_grab_any); |
15016 | 859 grab_fn.add_overload (fn); |
860 grab_fn.stash_name ("grab"); | |
861 | |
862 // grab matrix | |
863 fn = create_function (jit_convention::external, "octave_jit_grab_matrix", | |
864 matrix, matrix); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
865 fn.add_mapping (engine, &octave_jit_grab_matrix); |
15016 | 866 grab_fn.add_overload (fn); |
867 | |
868 // release any | |
869 fn = create_function (jit_convention::external, "octave_jit_release_any", 0, | |
870 any); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
871 fn.add_mapping (engine, &octave_jit_release_any); |
15016 | 872 release_fn.add_overload (fn); |
873 release_fn.stash_name ("release"); | |
874 | |
875 // release matrix | |
876 fn = create_function (jit_convention::external, "octave_jit_release_matrix", | |
877 0, matrix); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
878 fn.add_mapping (engine, &octave_jit_release_matrix); |
15016 | 879 release_fn.add_overload (fn); |
880 | |
881 // release scalar | |
882 fn = create_identity (scalar); | |
883 release_fn.add_overload (fn); | |
884 | |
885 // release complex | |
886 fn = create_identity (complex); | |
887 release_fn.add_overload (fn); | |
888 | |
889 // release index | |
890 fn = create_identity (index); | |
891 release_fn.add_overload (fn); | |
892 | |
893 // now for binary scalar operations | |
894 // FIXME: Finish all operations | |
895 add_binary_op (scalar, octave_value::op_add, llvm::Instruction::FAdd); | |
896 add_binary_op (scalar, octave_value::op_sub, llvm::Instruction::FSub); | |
897 add_binary_op (scalar, octave_value::op_mul, llvm::Instruction::FMul); | |
898 add_binary_op (scalar, octave_value::op_el_mul, llvm::Instruction::FMul); | |
899 | |
900 add_binary_fcmp (scalar, octave_value::op_lt, llvm::CmpInst::FCMP_ULT); | |
901 add_binary_fcmp (scalar, octave_value::op_le, llvm::CmpInst::FCMP_ULE); | |
902 add_binary_fcmp (scalar, octave_value::op_eq, llvm::CmpInst::FCMP_UEQ); | |
903 add_binary_fcmp (scalar, octave_value::op_ge, llvm::CmpInst::FCMP_UGE); | |
904 add_binary_fcmp (scalar, octave_value::op_gt, llvm::CmpInst::FCMP_UGT); | |
905 add_binary_fcmp (scalar, octave_value::op_ne, llvm::CmpInst::FCMP_UNE); | |
906 | |
907 jit_function gripe_div0 = create_function (jit_convention::external, | |
908 "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
|
909 gripe_div0.add_mapping (engine, &gripe_divide_by_zero); |
15016 | 910 gripe_div0.mark_can_error (); |
911 | |
912 // divide is annoying because it might error | |
913 fn = create_function (jit_convention::internal, | |
914 "octave_jit_div_scalar_scalar", scalar, scalar, scalar); | |
915 fn.mark_can_error (); | |
916 | |
917 llvm::BasicBlock *body = fn.new_block (); | |
918 builder.SetInsertPoint (body); | |
919 { | |
920 llvm::BasicBlock *warn_block = fn.new_block ("warn"); | |
921 llvm::BasicBlock *normal_block = fn.new_block ("normal"); | |
922 | |
923 llvm::Value *zero = llvm::ConstantFP::get (scalar_t, 0); | |
924 llvm::Value *check = builder.CreateFCmpUEQ (zero, fn.argument (builder, 0)); | |
925 builder.CreateCondBr (check, warn_block, normal_block); | |
926 | |
927 builder.SetInsertPoint (warn_block); | |
928 gripe_div0.call (builder); | |
929 builder.CreateBr (normal_block); | |
930 | |
931 builder.SetInsertPoint (normal_block); | |
932 llvm::Value *ret = builder.CreateFDiv (fn.argument (builder, 0), | |
933 fn.argument (builder, 1)); | |
934 fn.do_return (builder, ret); | |
935 } | |
936 binary_ops[octave_value::op_div].add_overload (fn); | |
937 binary_ops[octave_value::op_el_div].add_overload (fn); | |
938 | |
939 // ldiv is the same as div with the operators reversed | |
940 fn = mirror_binary (fn); | |
941 binary_ops[octave_value::op_ldiv].add_overload (fn); | |
942 binary_ops[octave_value::op_el_ldiv].add_overload (fn); | |
943 | |
944 // In general, the result of scalar ^ scalar is a complex number. We might be | |
945 // able to improve on this if we keep track of the range of values varaibles | |
946 // can take on. | |
947 fn = create_function (jit_convention::external, | |
948 "octave_jit_pow_scalar_scalar", complex, scalar, | |
949 scalar); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
950 fn.add_mapping (engine, &octave_jit_pow_scalar_scalar); |
15016 | 951 binary_ops[octave_value::op_pow].add_overload (fn); |
952 binary_ops[octave_value::op_el_pow].add_overload (fn); | |
953 | |
954 // now for binary complex operations | |
955 add_binary_op (complex, octave_value::op_add, llvm::Instruction::FAdd); | |
956 add_binary_op (complex, octave_value::op_sub, llvm::Instruction::FSub); | |
957 | |
958 fn = create_function (jit_convention::internal, | |
959 "octave_jit_*_complex_complex", complex, complex, | |
960 complex); | |
961 body = fn.new_block (); | |
962 builder.SetInsertPoint (body); | |
963 { | |
964 // (x0*x1 - y0*y1, x0*y1 + y0*x1) = (x0,y0) * (x1,y1) | |
965 // We compute this in one vectorized multiplication, a subtraction, and an | |
966 // addition. | |
967 llvm::Value *lhs = fn.argument (builder, 0); | |
968 llvm::Value *rhs = fn.argument (builder, 1); | |
969 | |
970 // FIXME: We need a better way of doing this, working with llvm's IR | |
971 // directly is sort of a pain. | |
972 llvm::Value *zero = builder.getInt32 (0); | |
973 llvm::Value *one = builder.getInt32 (1); | |
974 llvm::Value *two = builder.getInt32 (2); | |
975 llvm::Value *three = builder.getInt32 (3); | |
976 | |
977 llvm::Type *vec4 = llvm::VectorType::get (scalar_t, 4); | |
978 llvm::Value *mlhs = llvm::UndefValue::get (vec4); | |
979 llvm::Value *mrhs = mlhs; | |
980 | |
981 llvm::Value *temp = complex_real (lhs); | |
982 mlhs = builder.CreateInsertElement (mlhs, temp, zero); | |
983 mlhs = builder.CreateInsertElement (mlhs, temp, two); | |
984 temp = complex_imag (lhs); | |
985 mlhs = builder.CreateInsertElement (mlhs, temp, one); | |
986 mlhs = builder.CreateInsertElement (mlhs, temp, three); | |
987 | |
988 temp = complex_real (rhs); | |
989 mrhs = builder.CreateInsertElement (mrhs, temp, zero); | |
990 mrhs = builder.CreateInsertElement (mrhs, temp, three); | |
991 temp = complex_imag (rhs); | |
992 mrhs = builder.CreateInsertElement (mrhs, temp, one); | |
993 mrhs = builder.CreateInsertElement (mrhs, temp, two); | |
994 | |
995 llvm::Value *mres = builder.CreateFMul (mlhs, mrhs); | |
996 llvm::Value *tlhs = builder.CreateExtractElement (mres, zero); | |
997 llvm::Value *trhs = builder.CreateExtractElement (mres, one); | |
998 llvm::Value *ret_real = builder.CreateFSub (tlhs, trhs); | |
999 | |
1000 tlhs = builder.CreateExtractElement (mres, two); | |
1001 trhs = builder.CreateExtractElement (mres, three); | |
1002 llvm::Value *ret_imag = builder.CreateFAdd (tlhs, trhs); | |
1003 fn.do_return (builder, complex_new (ret_real, ret_imag)); | |
1004 } | |
1005 | |
1006 binary_ops[octave_value::op_mul].add_overload (fn); | |
1007 binary_ops[octave_value::op_el_mul].add_overload (fn); | |
1008 | |
1009 jit_function complex_div = create_function (jit_convention::external, | |
1010 "octave_jit_complex_div", | |
1011 complex, complex, complex); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1012 complex_div.add_mapping (engine, &octave_jit_complex_div); |
15016 | 1013 complex_div.mark_can_error (); |
1014 binary_ops[octave_value::op_div].add_overload (fn); | |
1015 binary_ops[octave_value::op_ldiv].add_overload (fn); | |
1016 | |
1017 fn = mirror_binary (complex_div); | |
1018 binary_ops[octave_value::op_ldiv].add_overload (fn); | |
1019 binary_ops[octave_value::op_el_ldiv].add_overload (fn); | |
1020 | |
1021 fn = create_function (jit_convention::external, | |
1022 "octave_jit_pow_complex_complex", complex, complex, | |
1023 complex); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1024 fn.add_mapping (engine, &octave_jit_pow_complex_complex); |
15016 | 1025 binary_ops[octave_value::op_pow].add_overload (fn); |
1026 binary_ops[octave_value::op_el_pow].add_overload (fn); | |
1027 | |
1028 fn = create_function (jit_convention::internal, | |
1029 "octave_jit_*_scalar_complex", complex, scalar, | |
1030 complex); | |
1031 jit_function mul_scalar_complex = fn; | |
1032 body = fn.new_block (); | |
1033 builder.SetInsertPoint (body); | |
1034 { | |
1035 llvm::Value *lhs = fn.argument (builder, 0); | |
1036 llvm::Value *tlhs = complex_new (lhs, lhs); | |
1037 llvm::Value *rhs = fn.argument (builder, 1); | |
1038 fn.do_return (builder, builder.CreateFMul (tlhs, rhs)); | |
1039 } | |
1040 binary_ops[octave_value::op_mul].add_overload (fn); | |
1041 binary_ops[octave_value::op_el_mul].add_overload (fn); | |
1042 | |
1043 | |
1044 fn = mirror_binary (mul_scalar_complex); | |
1045 binary_ops[octave_value::op_mul].add_overload (fn); | |
1046 binary_ops[octave_value::op_el_mul].add_overload (fn); | |
1047 | |
1048 fn = create_function (jit_convention::internal, "octave_jit_+_scalar_complex", | |
1049 complex, scalar, complex); | |
1050 body = fn.new_block (); | |
1051 builder.SetInsertPoint (body); | |
1052 { | |
1053 llvm::Value *lhs = fn.argument (builder, 0); | |
1054 llvm::Value *rhs = fn.argument (builder, 1); | |
1055 llvm::Value *real = builder.CreateFAdd (lhs, complex_real (rhs)); | |
1056 fn.do_return (builder, complex_real (rhs, real)); | |
1057 } | |
1058 binary_ops[octave_value::op_add].add_overload (fn); | |
1059 | |
1060 fn = mirror_binary (fn); | |
1061 binary_ops[octave_value::op_add].add_overload (fn); | |
1062 | |
1063 fn = create_function (jit_convention::internal, "octave_jit_-_complex_scalar", | |
1064 complex, complex, scalar); | |
1065 body = fn.new_block (); | |
1066 builder.SetInsertPoint (body); | |
1067 { | |
1068 llvm::Value *lhs = fn.argument (builder, 0); | |
1069 llvm::Value *rhs = fn.argument (builder, 1); | |
1070 llvm::Value *real = builder.CreateFSub (complex_real (lhs), rhs); | |
1071 fn.do_return (builder, complex_real (lhs, real)); | |
1072 } | |
1073 binary_ops[octave_value::op_sub].add_overload (fn); | |
1074 | |
1075 fn = create_function (jit_convention::internal, "octave_jit_-_scalar_complex", | |
1076 complex, scalar, complex); | |
1077 body = fn.new_block (); | |
1078 builder.SetInsertPoint (body); | |
1079 { | |
1080 llvm::Value *lhs = fn.argument (builder, 0); | |
1081 llvm::Value *rhs = fn.argument (builder, 1); | |
1082 llvm::Value *real = builder.CreateFSub (lhs, complex_real (rhs)); | |
1083 fn.do_return (builder, complex_real (rhs, real)); | |
1084 } | |
1085 binary_ops[octave_value::op_sub].add_overload (fn); | |
1086 | |
1087 fn = create_function (jit_convention::external, | |
1088 "octave_jit_pow_scalar_complex", complex, scalar, | |
1089 complex); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1090 fn.add_mapping (engine, &octave_jit_pow_scalar_complex); |
15016 | 1091 binary_ops[octave_value::op_pow].add_overload (fn); |
1092 binary_ops[octave_value::op_el_pow].add_overload (fn); | |
1093 | |
1094 fn = create_function (jit_convention::external, | |
1095 "octave_jit_pow_complex_scalar", complex, complex, | |
1096 scalar); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1097 fn.add_mapping (engine, &octave_jit_pow_complex_scalar); |
15016 | 1098 binary_ops[octave_value::op_pow].add_overload (fn); |
1099 binary_ops[octave_value::op_el_pow].add_overload (fn); | |
1100 | |
1101 // now for binary index operators | |
1102 add_binary_op (index, octave_value::op_add, llvm::Instruction::Add); | |
1103 | |
1104 // and binary bool operators | |
1105 add_binary_op (boolean, octave_value::op_el_or, llvm::Instruction::Or); | |
1106 add_binary_op (boolean, octave_value::op_el_and, llvm::Instruction::And); | |
1107 | |
1108 // now for printing functions | |
1109 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
|
1110 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
|
1111 add_print (scalar, reinterpret_cast<void *> (&octave_jit_print_scalar)); |
15016 | 1112 |
1113 // initialize for loop | |
1114 for_init_fn.stash_name ("for_init"); | |
1115 | |
1116 fn = create_function (jit_convention::internal, "octave_jit_for_range_init", | |
1117 index, range); | |
1118 body = fn.new_block (); | |
1119 builder.SetInsertPoint (body); | |
1120 { | |
1121 llvm::Value *zero = llvm::ConstantInt::get (index_t, 0); | |
1122 fn.do_return (builder, zero); | |
1123 } | |
1124 for_init_fn.add_overload (fn); | |
1125 | |
1126 // bounds check for for loop | |
1127 for_check_fn.stash_name ("for_check"); | |
1128 | |
1129 fn = create_function (jit_convention::internal, "octave_jit_for_range_check", | |
1130 boolean, range, index); | |
1131 body = fn.new_block (); | |
1132 builder.SetInsertPoint (body); | |
1133 { | |
1134 llvm::Value *nelem | |
1135 = builder.CreateExtractValue (fn.argument (builder, 0), 3); | |
1136 llvm::Value *idx = fn.argument (builder, 1); | |
1137 llvm::Value *ret = builder.CreateICmpULT (idx, nelem); | |
1138 fn.do_return (builder, ret); | |
1139 } | |
1140 for_check_fn.add_overload (fn); | |
1141 | |
1142 // index variabe for for loop | |
1143 for_index_fn.stash_name ("for_index"); | |
1144 | |
1145 fn = create_function (jit_convention::internal, "octave_jit_for_range_idx", | |
1146 scalar, range, index); | |
1147 body = fn.new_block (); | |
1148 builder.SetInsertPoint (body); | |
1149 { | |
1150 llvm::Value *idx = fn.argument (builder, 1); | |
1151 llvm::Value *didx = builder.CreateSIToFP (idx, scalar_t); | |
1152 llvm::Value *rng = fn.argument (builder, 0); | |
1153 llvm::Value *base = builder.CreateExtractValue (rng, 0); | |
1154 llvm::Value *inc = builder.CreateExtractValue (rng, 2); | |
1155 | |
1156 llvm::Value *ret = builder.CreateFMul (didx, inc); | |
1157 ret = builder.CreateFAdd (base, ret); | |
1158 fn.do_return (builder, ret); | |
1159 } | |
1160 for_index_fn.add_overload (fn); | |
1161 | |
1162 // logically true | |
1163 logically_true_fn.stash_name ("logically_true"); | |
1164 | |
1165 jit_function gripe_nantl | |
1166 = create_function (jit_convention::external, | |
1167 "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
|
1168 gripe_nantl.add_mapping (engine, &octave_jit_gripe_nan_to_logical_conversion); |
15016 | 1169 gripe_nantl.mark_can_error (); |
1170 | |
1171 fn = create_function (jit_convention::internal, | |
1172 "octave_jit_logically_true_scalar", boolean, scalar); | |
1173 fn.mark_can_error (); | |
1174 | |
1175 body = fn.new_block (); | |
1176 builder.SetInsertPoint (body); | |
1177 { | |
1178 llvm::BasicBlock *error_block = fn.new_block ("error"); | |
1179 llvm::BasicBlock *normal_block = fn.new_block ("normal"); | |
1180 | |
1181 llvm::Value *check = builder.CreateFCmpUNE (fn.argument (builder, 0), | |
1182 fn.argument (builder, 0)); | |
1183 builder.CreateCondBr (check, error_block, normal_block); | |
1184 | |
1185 builder.SetInsertPoint (error_block); | |
1186 gripe_nantl.call (builder); | |
1187 builder.CreateBr (normal_block); | |
1188 builder.SetInsertPoint (normal_block); | |
1189 | |
1190 llvm::Value *zero = llvm::ConstantFP::get (scalar_t, 0); | |
1191 llvm::Value *ret = builder.CreateFCmpONE (fn.argument (builder, 0), zero); | |
1192 fn.do_return (builder, ret); | |
1193 } | |
1194 logically_true_fn.add_overload (fn); | |
1195 | |
1196 // logically_true boolean | |
1197 fn = create_identity (boolean); | |
1198 logically_true_fn.add_overload (fn); | |
1199 | |
1200 // make_range | |
1201 // FIXME: May be benificial to implement all in LLVM | |
1202 make_range_fn.stash_name ("make_range"); | |
1203 jit_function compute_nelem | |
1204 = create_function (jit_convention::external, "octave_jit_compute_nelem", | |
1205 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
|
1206 compute_nelem.add_mapping (engine, &octave_jit_compute_nelem); |
15016 | 1207 |
1208 fn = create_function (jit_convention::internal, "octave_jit_make_range", | |
1209 range, scalar, scalar, scalar); | |
1210 body = fn.new_block (); | |
1211 builder.SetInsertPoint (body); | |
1212 { | |
1213 llvm::Value *base = fn.argument (builder, 0); | |
1214 llvm::Value *limit = fn.argument (builder, 1); | |
1215 llvm::Value *inc = fn.argument (builder, 2); | |
1216 llvm::Value *nelem = compute_nelem.call (builder, base, limit, inc); | |
1217 | |
1218 llvm::Value *dzero = llvm::ConstantFP::get (scalar_t, 0); | |
1219 llvm::Value *izero = llvm::ConstantInt::get (index_t, 0); | |
1220 llvm::Value *rng = llvm::ConstantStruct::get (range_t, dzero, dzero, dzero, | |
1221 izero, NULL); | |
1222 rng = builder.CreateInsertValue (rng, base, 0); | |
1223 rng = builder.CreateInsertValue (rng, limit, 1); | |
1224 rng = builder.CreateInsertValue (rng, inc, 2); | |
1225 rng = builder.CreateInsertValue (rng, nelem, 3); | |
1226 fn.do_return (builder, rng); | |
1227 } | |
1228 make_range_fn.add_overload (fn); | |
1229 | |
1230 // paren_subsref | |
1231 jit_type *jit_int = intN (sizeof (int) * 8); | |
1232 llvm::Type *int_t = jit_int->to_llvm (); | |
1233 jit_function ginvalid_index | |
1234 = create_function (jit_convention::external, "octave_jit_ginvalid_index", | |
1235 0); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1236 ginvalid_index.add_mapping (engine, &octave_jit_ginvalid_index); |
15016 | 1237 jit_function gindex_range = create_function (jit_convention::external, |
1238 "octave_jit_gindex_range", | |
1239 0, jit_int, jit_int, index, | |
1240 index); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1241 gindex_range.add_mapping (engine, &octave_jit_gindex_range); |
15016 | 1242 |
1243 fn = create_function (jit_convention::internal, "()subsref", scalar, matrix, | |
1244 scalar); | |
1245 fn.mark_can_error (); | |
1246 | |
1247 body = fn.new_block (); | |
1248 builder.SetInsertPoint (body); | |
1249 { | |
1250 llvm::Value *one = llvm::ConstantInt::get (index_t, 1); | |
1251 llvm::Value *ione; | |
1252 if (index_t == int_t) | |
1253 ione = one; | |
1254 else | |
1255 ione = llvm::ConstantInt::get (int_t, 1); | |
1256 | |
1257 llvm::Value *undef = llvm::UndefValue::get (scalar_t); | |
1258 llvm::Value *mat = fn.argument (builder, 0); | |
1259 llvm::Value *idx = fn.argument (builder, 1); | |
1260 | |
1261 // convert index to scalar to integer, and check index >= 1 | |
1262 llvm::Value *int_idx = builder.CreateFPToSI (idx, index_t); | |
1263 llvm::Value *check_idx = builder.CreateSIToFP (int_idx, scalar_t); | |
1264 llvm::Value *cond0 = builder.CreateFCmpUNE (idx, check_idx); | |
1265 llvm::Value *cond1 = builder.CreateICmpSLT (int_idx, one); | |
1266 llvm::Value *cond = builder.CreateOr (cond0, cond1); | |
1267 | |
1268 llvm::BasicBlock *done = fn.new_block ("done"); | |
1269 llvm::BasicBlock *conv_error = fn.new_block ("conv_error", done); | |
1270 llvm::BasicBlock *normal = fn.new_block ("normal", done); | |
1271 builder.CreateCondBr (cond, conv_error, normal); | |
1272 | |
1273 builder.SetInsertPoint (conv_error); | |
1274 ginvalid_index.call (builder); | |
1275 builder.CreateBr (done); | |
1276 | |
1277 builder.SetInsertPoint (normal); | |
1278 llvm::Value *len = builder.CreateExtractValue (mat, | |
1279 llvm::ArrayRef<unsigned> (2)); | |
1280 cond = builder.CreateICmpSGT (int_idx, len); | |
1281 | |
1282 | |
1283 llvm::BasicBlock *bounds_error = fn.new_block ("bounds_error", done); | |
1284 llvm::BasicBlock *success = fn.new_block ("success", done); | |
1285 builder.CreateCondBr (cond, bounds_error, success); | |
1286 | |
1287 builder.SetInsertPoint (bounds_error); | |
1288 gindex_range.call (builder, ione, ione, int_idx, len); | |
1289 builder.CreateBr (done); | |
1290 | |
1291 builder.SetInsertPoint (success); | |
1292 llvm::Value *data = builder.CreateExtractValue (mat, | |
1293 llvm::ArrayRef<unsigned> (1)); | |
1294 llvm::Value *gep = builder.CreateInBoundsGEP (data, int_idx); | |
1295 llvm::Value *ret = builder.CreateLoad (gep); | |
1296 builder.CreateBr (done); | |
1297 | |
1298 builder.SetInsertPoint (done); | |
1299 | |
1300 llvm::PHINode *merge = llvm::PHINode::Create (scalar_t, 3); | |
1301 builder.Insert (merge); | |
1302 merge->addIncoming (undef, conv_error); | |
1303 merge->addIncoming (undef, bounds_error); | |
1304 merge->addIncoming (ret, success); | |
1305 fn.do_return (builder, merge); | |
1306 } | |
1307 paren_subsref_fn.add_overload (fn); | |
1308 | |
1309 // paren subsasgn | |
1310 paren_subsasgn_fn.stash_name ("()subsasgn"); | |
1311 | |
1312 jit_function resize_paren_subsasgn | |
1313 = create_function (jit_convention::external, | |
15027
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1314 "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
|
1315 scalar); |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1316 resize_paren_subsasgn.add_mapping (engine, &octave_jit_paren_subsasgn_impl); |
15016 | 1317 fn = create_function (jit_convention::internal, "octave_jit_paren_subsasgn", |
1318 matrix, matrix, scalar, scalar); | |
1319 fn.mark_can_error (); | |
1320 body = fn.new_block (); | |
1321 builder.SetInsertPoint (body); | |
1322 { | |
1323 llvm::Value *one = llvm::ConstantInt::get (index_t, 1); | |
1324 | |
1325 llvm::Value *mat = fn.argument (builder, 0); | |
1326 llvm::Value *idx = fn.argument (builder, 1); | |
1327 llvm::Value *value = fn.argument (builder, 2); | |
1328 | |
1329 llvm::Value *int_idx = builder.CreateFPToSI (idx, index_t); | |
1330 llvm::Value *check_idx = builder.CreateSIToFP (int_idx, scalar_t); | |
1331 llvm::Value *cond0 = builder.CreateFCmpUNE (idx, check_idx); | |
1332 llvm::Value *cond1 = builder.CreateICmpSLT (int_idx, one); | |
1333 llvm::Value *cond = builder.CreateOr (cond0, cond1); | |
1334 | |
1335 llvm::BasicBlock *done = fn.new_block ("done"); | |
1336 | |
1337 llvm::BasicBlock *conv_error = fn.new_block ("conv_error", done); | |
1338 llvm::BasicBlock *normal = fn.new_block ("normal", done); | |
1339 builder.CreateCondBr (cond, conv_error, normal); | |
1340 builder.SetInsertPoint (conv_error); | |
1341 ginvalid_index.call (builder); | |
1342 builder.CreateBr (done); | |
1343 | |
1344 builder.SetInsertPoint (normal); | |
1345 llvm::Value *len = builder.CreateExtractValue (mat, | |
1346 llvm::ArrayRef<unsigned> (2)); | |
1347 cond0 = builder.CreateICmpSGT (int_idx, len); | |
1348 | |
1349 llvm::Value *rcount = builder.CreateExtractValue (mat, 0); | |
1350 rcount = builder.CreateLoad (rcount); | |
1351 cond1 = builder.CreateICmpSGT (rcount, one); | |
1352 cond = builder.CreateOr (cond0, cond1); | |
1353 | |
1354 llvm::BasicBlock *bounds_error = fn.new_block ("bounds_error", done); | |
1355 llvm::BasicBlock *success = fn.new_block ("success", done); | |
1356 builder.CreateCondBr (cond, bounds_error, success); | |
1357 | |
1358 // resize on out of bounds access | |
1359 builder.SetInsertPoint (bounds_error); | |
15027
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1360 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
|
1361 int_idx, value); |
15016 | 1362 builder.CreateBr (done); |
1363 | |
1364 builder.SetInsertPoint (success); | |
1365 llvm::Value *data = builder.CreateExtractValue (mat, | |
1366 llvm::ArrayRef<unsigned> (1)); | |
1367 llvm::Value *gep = builder.CreateInBoundsGEP (data, int_idx); | |
1368 builder.CreateStore (value, gep); | |
1369 builder.CreateBr (done); | |
1370 | |
1371 builder.SetInsertPoint (done); | |
1372 | |
1373 llvm::PHINode *merge = llvm::PHINode::Create (matrix_t, 3); | |
1374 builder.Insert (merge); | |
1375 merge->addIncoming (mat, conv_error); | |
1376 merge->addIncoming (resize_result, bounds_error); | |
1377 merge->addIncoming (mat, success); | |
1378 fn.do_return (builder, merge); | |
1379 } | |
1380 paren_subsasgn_fn.add_overload (fn); | |
1381 | |
1382 fn = create_function (jit_convention::external, | |
1383 "octave_jit_paren_subsasgn_matrix_range", matrix, | |
1384 matrix, range, scalar); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1385 fn.add_mapping (engine, &octave_jit_paren_subsasgn_matrix_range); |
15016 | 1386 fn.mark_can_error (); |
1387 paren_subsasgn_fn.add_overload (fn); | |
1388 | |
1389 casts[any->type_id ()].stash_name ("(any)"); | |
1390 casts[scalar->type_id ()].stash_name ("(scalar)"); | |
1391 casts[complex->type_id ()].stash_name ("(complex)"); | |
1392 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
|
1393 casts[any->type_id ()].stash_name ("(range)"); |
15016 | 1394 |
1395 // cast any <- matrix | |
1396 fn = create_function (jit_convention::external, "octave_jit_cast_any_matrix", | |
1397 any, matrix); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1398 fn.add_mapping (engine, &octave_jit_cast_any_matrix); |
15016 | 1399 casts[any->type_id ()].add_overload (fn); |
1400 | |
1401 // cast matrix <- any | |
1402 fn = create_function (jit_convention::external, "octave_jit_cast_matrix_any", | |
1403 matrix, any); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1404 fn.add_mapping (engine, &octave_jit_cast_matrix_any); |
15016 | 1405 casts[matrix->type_id ()].add_overload (fn); |
1406 | |
15027
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1407 // cast any <- range |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1408 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
|
1409 any, range); |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1410 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
|
1411 casts[any->type_id ()].add_overload (fn); |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1412 |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1413 // cast range <- any |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1414 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
|
1415 range, any); |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1416 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
|
1417 casts[range->type_id ()].add_overload (fn); |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1418 |
15016 | 1419 // cast any <- scalar |
1420 fn = create_function (jit_convention::external, "octave_jit_cast_any_scalar", | |
1421 any, scalar); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1422 fn.add_mapping (engine, &octave_jit_cast_any_scalar); |
15016 | 1423 casts[any->type_id ()].add_overload (fn); |
1424 | |
1425 // cast scalar <- any | |
1426 fn = create_function (jit_convention::external, "octave_jit_cast_scalar_any", | |
1427 scalar, any); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1428 fn.add_mapping (engine, &octave_jit_cast_scalar_any); |
15016 | 1429 casts[scalar->type_id ()].add_overload (fn); |
1430 | |
1431 // cast any <- complex | |
1432 fn = create_function (jit_convention::external, "octave_jit_cast_any_complex", | |
1433 any, complex); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1434 fn.add_mapping (engine, &octave_jit_cast_any_complex); |
15016 | 1435 casts[any->type_id ()].add_overload (fn); |
1436 | |
1437 // cast complex <- any | |
1438 fn = create_function (jit_convention::external, "octave_jit_cast_complex_any", | |
1439 complex, any); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1440 fn.add_mapping (engine, &octave_jit_cast_complex_any); |
15016 | 1441 casts[complex->type_id ()].add_overload (fn); |
1442 | |
1443 // cast complex <- scalar | |
1444 fn = create_function (jit_convention::internal, | |
1445 "octave_jit_cast_complex_scalar", complex, scalar); | |
1446 body = fn.new_block (); | |
1447 builder.SetInsertPoint (body); | |
1448 { | |
1449 llvm::Value *zero = llvm::ConstantFP::get (scalar_t, 0); | |
1450 fn.do_return (builder, complex_new (fn.argument (builder, 0), zero)); | |
1451 } | |
1452 casts[complex->type_id ()].add_overload (fn); | |
1453 | |
1454 // cast scalar <- complex | |
1455 fn = create_function (jit_convention::internal, | |
1456 "octave_jit_cast_scalar_complex", scalar, complex); | |
1457 body = fn.new_block (); | |
1458 builder.SetInsertPoint (body); | |
1459 fn.do_return (builder, complex_real (fn.argument (builder, 0))); | |
1460 casts[scalar->type_id ()].add_overload (fn); | |
1461 | |
1462 // cast any <- any | |
1463 fn = create_identity (any); | |
1464 casts[any->type_id ()].add_overload (fn); | |
1465 | |
1466 // cast scalar <- scalar | |
1467 fn = create_identity (scalar); | |
1468 casts[scalar->type_id ()].add_overload (fn); | |
1469 | |
1470 // cast complex <- complex | |
1471 fn = create_identity (complex); | |
1472 casts[complex->type_id ()].add_overload (fn); | |
1473 | |
1474 // -------------------- builtin functions -------------------- | |
1475 add_builtin ("#unknown_function"); | |
1476 unknown_function = builtins["#unknown_function"]; | |
1477 | |
1478 add_builtin ("sin"); | |
1479 register_intrinsic ("sin", llvm::Intrinsic::sin, scalar, scalar); | |
1480 register_generic ("sin", matrix, matrix); | |
1481 | |
1482 add_builtin ("cos"); | |
1483 register_intrinsic ("cos", llvm::Intrinsic::cos, scalar, scalar); | |
1484 register_generic ("cos", matrix, matrix); | |
1485 | |
1486 add_builtin ("exp"); | |
1487 register_intrinsic ("exp", llvm::Intrinsic::cos, scalar, scalar); | |
1488 register_generic ("exp", matrix, matrix); | |
1489 | |
1490 casts.resize (next_id + 1); | |
1491 jit_function any_id = create_identity (any); | |
1492 jit_function release_any = get_release (any); | |
1493 std::vector<jit_type *> args; | |
1494 args.resize (1); | |
1495 | |
1496 for (std::map<std::string, jit_type *>::iterator iter = builtins.begin (); | |
1497 iter != builtins.end (); ++iter) | |
1498 { | |
1499 jit_type *btype = iter->second; | |
1500 args[0] = btype; | |
1501 | |
1502 release_fn.add_overload (jit_function (release_any, 0, args)); | |
1503 casts[any->type_id ()].add_overload (jit_function (any_id, any, args)); | |
1504 | |
1505 args[0] = any; | |
1506 casts[btype->type_id ()].add_overload (jit_function (any_id, btype, | |
1507 args)); | |
1508 } | |
1509 } | |
1510 | |
1511 void | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1512 jit_typeinfo::add_print (jit_type *ty, void *fptr) |
15016 | 1513 { |
1514 std::stringstream name; | |
1515 name << "octave_jit_print_" << ty->name (); | |
1516 jit_function fn = create_function (jit_convention::external, name.str (), 0, | |
1517 intN (8), ty); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1518 fn.add_mapping (engine, fptr); |
15016 | 1519 print_fn.add_overload (fn); |
1520 } | |
1521 | |
1522 // FIXME: cp between add_binary_op, add_binary_icmp, and add_binary_fcmp | |
1523 void | |
1524 jit_typeinfo::add_binary_op (jit_type *ty, int op, int llvm_op) | |
1525 { | |
1526 std::stringstream fname; | |
1527 octave_value::binary_op ov_op = static_cast<octave_value::binary_op>(op); | |
1528 fname << "octave_jit_" << octave_value::binary_op_as_string (ov_op) | |
1529 << "_" << ty->name (); | |
1530 | |
1531 jit_function fn = create_function (jit_convention::internal, fname.str (), | |
1532 ty, ty, ty); | |
1533 llvm::BasicBlock *block = fn.new_block (); | |
1534 builder.SetInsertPoint (block); | |
1535 llvm::Instruction::BinaryOps temp | |
1536 = static_cast<llvm::Instruction::BinaryOps>(llvm_op); | |
1537 | |
1538 llvm::Value *ret = builder.CreateBinOp (temp, fn.argument (builder, 0), | |
1539 fn.argument (builder, 1)); | |
1540 fn.do_return (builder, ret); | |
1541 binary_ops[op].add_overload (fn); | |
1542 } | |
1543 | |
1544 void | |
1545 jit_typeinfo::add_binary_icmp (jit_type *ty, int op, int llvm_op) | |
1546 { | |
1547 std::stringstream fname; | |
1548 octave_value::binary_op ov_op = static_cast<octave_value::binary_op>(op); | |
1549 fname << "octave_jit" << octave_value::binary_op_as_string (ov_op) | |
1550 << "_" << ty->name (); | |
1551 | |
1552 jit_function fn = create_function (jit_convention::internal, fname.str (), | |
1553 boolean, ty, ty); | |
1554 llvm::BasicBlock *block = fn.new_block (); | |
1555 builder.SetInsertPoint (block); | |
1556 llvm::CmpInst::Predicate temp | |
1557 = static_cast<llvm::CmpInst::Predicate>(llvm_op); | |
1558 llvm::Value *ret = builder.CreateICmp (temp, fn.argument (builder, 0), | |
1559 fn.argument (builder, 1)); | |
1560 fn.do_return (builder, ret); | |
1561 binary_ops[op].add_overload (fn); | |
1562 } | |
1563 | |
1564 void | |
1565 jit_typeinfo::add_binary_fcmp (jit_type *ty, int op, int llvm_op) | |
1566 { | |
1567 std::stringstream fname; | |
1568 octave_value::binary_op ov_op = static_cast<octave_value::binary_op>(op); | |
1569 fname << "octave_jit" << octave_value::binary_op_as_string (ov_op) | |
1570 << "_" << ty->name (); | |
1571 | |
1572 jit_function fn = create_function (jit_convention::internal, fname.str (), | |
1573 boolean, ty, ty); | |
1574 llvm::BasicBlock *block = fn.new_block (); | |
1575 builder.SetInsertPoint (block); | |
1576 llvm::CmpInst::Predicate temp | |
1577 = static_cast<llvm::CmpInst::Predicate>(llvm_op); | |
1578 llvm::Value *ret = builder.CreateFCmp (temp, fn.argument (builder, 0), | |
1579 fn.argument (builder, 1)); | |
1580 fn.do_return (builder, ret); | |
1581 binary_ops[op].add_overload (fn); | |
1582 } | |
1583 | |
1584 jit_function | |
1585 jit_typeinfo::create_function (jit_convention::type cc, const llvm::Twine& name, | |
1586 jit_type *ret, | |
1587 const std::vector<jit_type *>& args) | |
1588 { | |
1589 jit_function result (module, cc, name, ret, args); | |
1590 return result; | |
1591 } | |
1592 | |
1593 jit_function | |
1594 jit_typeinfo::create_identity (jit_type *type) | |
1595 { | |
1596 size_t id = type->type_id (); | |
1597 if (id >= identities.size ()) | |
1598 identities.resize (id + 1); | |
1599 | |
1600 if (! identities[id].valid ()) | |
1601 { | |
1602 jit_function fn = create_function (jit_convention::internal, "id", type, | |
1603 type); | |
1604 llvm::BasicBlock *body = fn.new_block (); | |
1605 builder.SetInsertPoint (body); | |
1606 fn.do_return (builder, fn.argument (builder, 0)); | |
1607 return identities[id] = fn; | |
1608 } | |
1609 | |
1610 return identities[id]; | |
1611 } | |
1612 | |
1613 llvm::Value * | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1614 jit_typeinfo::do_insert_error_check (llvm::IRBuilderD& abuilder) |
15016 | 1615 { |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1616 return abuilder.CreateLoad (lerror_state); |
15016 | 1617 } |
1618 | |
1619 void | |
1620 jit_typeinfo::add_builtin (const std::string& name) | |
1621 { | |
1622 jit_type *btype = new_type (name, any, any->to_llvm ()); | |
1623 builtins[name] = btype; | |
1624 | |
1625 octave_builtin *ov_builtin = find_builtin (name); | |
1626 if (ov_builtin) | |
1627 ov_builtin->stash_jit (*btype); | |
1628 } | |
1629 | |
1630 void | |
1631 jit_typeinfo::register_intrinsic (const std::string& name, size_t iid, | |
1632 jit_type *result, | |
1633 const std::vector<jit_type *>& args) | |
1634 { | |
1635 jit_type *builtin_type = builtins[name]; | |
1636 size_t nargs = args.size (); | |
1637 llvm::SmallVector<llvm::Type *, 5> llvm_args (nargs); | |
1638 for (size_t i = 0; i < nargs; ++i) | |
1639 llvm_args[i] = args[i]->to_llvm (); | |
1640 | |
1641 llvm::Intrinsic::ID id = static_cast<llvm::Intrinsic::ID> (iid); | |
1642 llvm::Function *ifun = llvm::Intrinsic::getDeclaration (module, id, | |
1643 llvm_args); | |
1644 std::stringstream fn_name; | |
1645 fn_name << "octave_jit_" << name; | |
1646 | |
1647 std::vector<jit_type *> args1 (nargs + 1); | |
1648 args1[0] = builtin_type; | |
1649 std::copy (args.begin (), args.end (), args1.begin () + 1); | |
1650 | |
1651 // The first argument will be the Octave function, but we already know that | |
1652 // the function call is the equivalent of the intrinsic, so we ignore it and | |
1653 // call the intrinsic with the remaining arguments. | |
1654 jit_function fn = create_function (jit_convention::internal, fn_name.str (), | |
1655 result, args1); | |
1656 llvm::BasicBlock *body = fn.new_block (); | |
1657 builder.SetInsertPoint (body); | |
1658 | |
1659 llvm::SmallVector<llvm::Value *, 5> fargs (nargs); | |
1660 for (size_t i = 0; i < nargs; ++i) | |
1661 fargs[i] = fn.argument (builder, i + 1); | |
1662 | |
1663 llvm::Value *ret = builder.CreateCall (ifun, fargs); | |
1664 fn.do_return (builder, ret); | |
1665 paren_subsref_fn.add_overload (fn); | |
1666 } | |
1667 | |
1668 octave_builtin * | |
1669 jit_typeinfo::find_builtin (const std::string& name) | |
1670 { | |
1671 // FIXME: Finalize what we want to store in octave_builtin, then add functions | |
1672 // to access these values in octave_value | |
1673 octave_value ov_builtin = symbol_table::find (name); | |
1674 return dynamic_cast<octave_builtin *> (ov_builtin.internal_rep ()); | |
1675 } | |
1676 | |
1677 void | |
1678 jit_typeinfo::register_generic (const std::string&, jit_type *, | |
1679 const std::vector<jit_type *>&) | |
1680 { | |
1681 // FIXME: Implement | |
1682 } | |
1683 | |
1684 jit_function | |
1685 jit_typeinfo::mirror_binary (const jit_function& fn) | |
1686 { | |
1687 jit_function ret = create_function (jit_convention::internal, | |
1688 fn.name () + "_reverse", | |
1689 fn.result (), fn.argument_type (1), | |
1690 fn.argument_type (0)); | |
1691 if (fn.can_error ()) | |
1692 ret.mark_can_error (); | |
1693 | |
1694 llvm::BasicBlock *body = ret.new_block (); | |
1695 builder.SetInsertPoint (body); | |
1696 llvm::Value *result = fn.call (builder, ret.argument (builder, 1), | |
1697 ret.argument (builder, 0)); | |
1698 if (ret.result ()) | |
1699 ret.do_return (builder, result); | |
1700 else | |
1701 ret.do_return (builder); | |
1702 | |
1703 return ret; | |
1704 } | |
1705 | |
1706 llvm::Value * | |
1707 jit_typeinfo::pack_complex (llvm::IRBuilderD& bld, llvm::Value *cplx) | |
1708 { | |
1709 llvm::Type *complex_ret = instance->complex_ret; | |
1710 llvm::Value *real = bld.CreateExtractElement (cplx, bld.getInt32 (0)); | |
1711 llvm::Value *imag = bld.CreateExtractElement (cplx, bld.getInt32 (1)); | |
1712 llvm::Value *ret = llvm::UndefValue::get (complex_ret); | |
1713 ret = bld.CreateInsertValue (ret, real, 0); | |
1714 return bld.CreateInsertValue (ret, imag, 1); | |
1715 } | |
1716 | |
1717 llvm::Value * | |
1718 jit_typeinfo::unpack_complex (llvm::IRBuilderD& bld, llvm::Value *result) | |
1719 { | |
1720 llvm::Type *complex_t = get_complex ()->to_llvm (); | |
1721 llvm::Value *real = bld.CreateExtractValue (result, 0); | |
1722 llvm::Value *imag = bld.CreateExtractValue (result, 1); | |
1723 llvm::Value *ret = llvm::UndefValue::get (complex_t); | |
1724 ret = bld.CreateInsertElement (ret, real, bld.getInt32 (0)); | |
1725 return bld.CreateInsertElement (ret, imag, bld.getInt32 (1)); | |
1726 } | |
1727 | |
1728 llvm::Value * | |
1729 jit_typeinfo::complex_real (llvm::Value *cx) | |
1730 { | |
1731 return builder.CreateExtractElement (cx, builder.getInt32 (0)); | |
1732 } | |
1733 | |
1734 llvm::Value * | |
1735 jit_typeinfo::complex_real (llvm::Value *cx, llvm::Value *real) | |
1736 { | |
1737 return builder.CreateInsertElement (cx, real, builder.getInt32 (0)); | |
1738 } | |
1739 | |
1740 llvm::Value * | |
1741 jit_typeinfo::complex_imag (llvm::Value *cx) | |
1742 { | |
1743 return builder.CreateExtractElement (cx, builder.getInt32 (1)); | |
1744 } | |
1745 | |
1746 llvm::Value * | |
1747 jit_typeinfo::complex_imag (llvm::Value *cx, llvm::Value *imag) | |
1748 { | |
1749 return builder.CreateInsertElement (cx, imag, builder.getInt32 (1)); | |
1750 } | |
1751 | |
1752 llvm::Value * | |
1753 jit_typeinfo::complex_new (llvm::Value *real, llvm::Value *imag) | |
1754 { | |
1755 llvm::Value *ret = llvm::UndefValue::get (complex->to_llvm ()); | |
1756 ret = complex_real (ret, real); | |
1757 return complex_imag (ret, imag); | |
1758 } | |
1759 | |
1760 void | |
1761 jit_typeinfo::create_int (size_t nbits) | |
1762 { | |
1763 std::stringstream tname; | |
1764 tname << "int" << nbits; | |
1765 ints[nbits] = new_type (tname.str (), any, llvm::Type::getIntNTy (context, | |
1766 nbits)); | |
1767 } | |
1768 | |
1769 jit_type * | |
1770 jit_typeinfo::intN (size_t nbits) const | |
1771 { | |
1772 std::map<size_t, jit_type *>::const_iterator iter = ints.find (nbits); | |
1773 if (iter != ints.end ()) | |
1774 return iter->second; | |
1775 | |
1776 throw jit_fail_exception ("No such integer type"); | |
1777 } | |
1778 | |
1779 jit_type * | |
1780 jit_typeinfo::do_type_of (const octave_value &ov) const | |
1781 { | |
1782 if (ov.is_function ()) | |
1783 { | |
1784 // FIXME: This is ugly, we need to finalize how we want to to this, then | |
1785 // have octave_value fully support the needed functionality | |
1786 octave_builtin *builtin | |
1787 = dynamic_cast<octave_builtin *> (ov.internal_rep ()); | |
1788 return builtin && builtin->to_jit () ? builtin->to_jit () | |
1789 : unknown_function; | |
1790 } | |
1791 | |
1792 if (ov.is_range ()) | |
1793 return get_range (); | |
1794 | |
1795 if (ov.is_double_type ()) | |
1796 { | |
1797 if (ov.is_real_scalar ()) | |
1798 return get_scalar (); | |
1799 | |
1800 if (ov.is_matrix_type ()) | |
1801 return get_matrix (); | |
1802 } | |
1803 | |
1804 if (ov.is_complex_scalar ()) | |
1805 return get_complex (); | |
1806 | |
1807 return get_any (); | |
1808 } | |
1809 | |
1810 jit_type* | |
1811 jit_typeinfo::new_type (const std::string& name, jit_type *parent, | |
1812 llvm::Type *llvm_type) | |
1813 { | |
1814 jit_type *ret = new jit_type (name, parent, llvm_type, next_id++); | |
1815 id_to_type.push_back (ret); | |
1816 return ret; | |
1817 } | |
1818 | |
1819 #endif |