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