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