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