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