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