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