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