Mercurial > octave-nkf
annotate libinterp/interp-core/jit-typeinfo.cc @ 15603:44272909d926
Stop JIT on interrupt
* jit-ir.cc (jit_call::needs_release): Move to cc file and do not release
artificial assigns.
(jit_error_check::variable_to_strign): New function.
(jit_error_check::print): Move to cc file and improve output.
* jit-ir.h (jit_call::needs_release): Move to cc file.
(jit_error_check::variable): New enum.
(jit_error_check::variable_to_string): New declaration.
(jit_error_check::jit_error_check): Add variable argument and new overload.
(jit_error_check::check_variable, jit_error_check::has_check_for): New function.
(jit_error_check::check_for): Ensure has_check_for is true.
(jit_error_check::print): Move to cc file.
(jit_error_check::check_alive): Always true if has_check_for is false.
(jit_error_check::mvariable): New variable.
* jit-typeinfo.cc (jit_typeinfo::jit_typeinfo): Initialize
loctave_interrupt_state and fix name of cast to any.
(jit_typeinfo::do_insert_interrupt_check): New function.
* jit-typeinfo.h (jit_typeinfo::insert_interrupt_check): New function.
(jit_typeinfo::do_insert_interrupt_check): New declaration.
(jit_typeinfo::loctave_interrupt_state): New variable.
* pt-jit.cc (jit_convert::visit_simple_for_command,
jit_convert::visit_while_command): Check interrupt state.
(jit_convert::create_check_impl): Specify var_error_state check.
(jit_convert_llvm::visit): Generate var_interrupt error check.
(jit_function_info::execute, jit_info::execute): Call octave_quit.
author | Max Brister <max@2bass.com> |
---|---|
date | Sun, 04 Nov 2012 21:11:33 -0700 |
parents | 0754bdfbc8fe |
children | 1f076c40c133 |
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 | |
15603 | 1113 // sig_atomic_type is going to be some sort of integer |
1114 sig_atomic_type = llvm::Type::getIntNTy (context, sizeof(sig_atomic_t) * 8); | |
1115 loctave_interrupt_state | |
1116 = new llvm::GlobalVariable (*module, sig_atomic_type, false, | |
1117 llvm::GlobalValue::ExternalLinkage, 0, | |
1118 "octave_interrupt_state"); | |
1119 engine->addGlobalMapping (loctave_interrupt_state, | |
1120 reinterpret_cast<void *> (&octave_interrupt_state)); | |
1121 | |
15135
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
1122 // generic call function |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
1123 { |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
1124 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
|
1125 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
|
1126 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
|
1127 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
|
1128 } |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
1129 |
15016 | 1130 // any with anything is an any op |
1131 jit_function fn; | |
1132 jit_type *binary_op_type = intN (sizeof (octave_value::binary_op) * 8); | |
1133 llvm::Type *llvm_bo_type = binary_op_type->to_llvm (); | |
1134 jit_function any_binary = create_function (jit_convention::external, | |
1135 "octave_jit_binary_any_any", | |
1136 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
|
1137 any_binary.add_mapping (engine, &octave_jit_binary_any_any); |
15016 | 1138 any_binary.mark_can_error (); |
1139 binary_ops.resize (octave_value::num_binary_ops); | |
1140 for (size_t i = 0; i < octave_value::num_binary_ops; ++i) | |
1141 { | |
1142 octave_value::binary_op op = static_cast<octave_value::binary_op> (i); | |
1143 std::string op_name = octave_value::binary_op_as_string (op); | |
1144 binary_ops[i].stash_name ("binary" + op_name); | |
1145 } | |
1146 | |
15146
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1147 unary_ops.resize (octave_value::num_unary_ops); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1148 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
|
1149 { |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1150 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
|
1151 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
|
1152 unary_ops[i].stash_name ("unary" + op_name); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1153 } |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1154 |
15016 | 1155 for (int op = 0; op < octave_value::num_binary_ops; ++op) |
1156 { | |
1157 llvm::Twine fn_name ("octave_jit_binary_any_any_"); | |
1158 fn_name = fn_name + llvm::Twine (op); | |
1159 | |
1160 fn = create_function (jit_convention::internal, fn_name, any, any, any); | |
1161 fn.mark_can_error (); | |
1162 llvm::BasicBlock *block = fn.new_block (); | |
1163 builder.SetInsertPoint (block); | |
1164 llvm::APInt op_int(sizeof (octave_value::binary_op) * 8, op, | |
1165 std::numeric_limits<octave_value::binary_op>::is_signed); | |
1166 llvm::Value *op_as_llvm = llvm::ConstantInt::get (llvm_bo_type, op_int); | |
1167 llvm::Value *ret = any_binary.call (builder, op_as_llvm, | |
1168 fn.argument (builder, 0), | |
1169 fn.argument (builder, 1)); | |
1170 fn.do_return (builder, ret); | |
1171 binary_ops[op].add_overload (fn); | |
1172 } | |
1173 | |
1174 // grab matrix | |
1175 fn = create_function (jit_convention::external, "octave_jit_grab_matrix", | |
1176 matrix, matrix); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1177 fn.add_mapping (engine, &octave_jit_grab_matrix); |
15016 | 1178 grab_fn.add_overload (fn); |
1179 | |
15334
8125773322d4
Error on undefined an unused variables in JIT
Max Brister <max@2bass.com>
parents:
15311
diff
changeset
|
1180 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
|
1181 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
|
1182 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
|
1183 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
|
1184 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
|
1185 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
|
1186 |
15016 | 1187 // release any |
1188 fn = create_function (jit_convention::external, "octave_jit_release_any", 0, | |
1189 any); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1190 fn.add_mapping (engine, &octave_jit_release_any); |
15016 | 1191 release_fn.add_overload (fn); |
1192 release_fn.stash_name ("release"); | |
1193 | |
1194 // release matrix | |
1195 fn = create_function (jit_convention::external, "octave_jit_release_matrix", | |
1196 0, matrix); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1197 fn.add_mapping (engine, &octave_jit_release_matrix); |
15016 | 1198 release_fn.add_overload (fn); |
1199 | |
15337
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
1200 // destroy |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
1201 destroy_fn = release_fn; |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
1202 destroy_fn.stash_name ("destroy"); |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
1203 destroy_fn.add_overload (create_identity(scalar)); |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
1204 destroy_fn.add_overload (create_identity(boolean)); |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
1205 destroy_fn.add_overload (create_identity(index)); |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
1206 destroy_fn.add_overload (create_identity(complex)); |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
1207 |
15016 | 1208 // now for binary scalar operations |
1209 add_binary_op (scalar, octave_value::op_add, llvm::Instruction::FAdd); | |
1210 add_binary_op (scalar, octave_value::op_sub, llvm::Instruction::FSub); | |
1211 add_binary_op (scalar, octave_value::op_mul, llvm::Instruction::FMul); | |
1212 add_binary_op (scalar, octave_value::op_el_mul, llvm::Instruction::FMul); | |
1213 | |
1214 add_binary_fcmp (scalar, octave_value::op_lt, llvm::CmpInst::FCMP_ULT); | |
1215 add_binary_fcmp (scalar, octave_value::op_le, llvm::CmpInst::FCMP_ULE); | |
1216 add_binary_fcmp (scalar, octave_value::op_eq, llvm::CmpInst::FCMP_UEQ); | |
1217 add_binary_fcmp (scalar, octave_value::op_ge, llvm::CmpInst::FCMP_UGE); | |
1218 add_binary_fcmp (scalar, octave_value::op_gt, llvm::CmpInst::FCMP_UGT); | |
1219 add_binary_fcmp (scalar, octave_value::op_ne, llvm::CmpInst::FCMP_UNE); | |
1220 | |
1221 jit_function gripe_div0 = create_function (jit_convention::external, | |
1222 "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
|
1223 gripe_div0.add_mapping (engine, &gripe_divide_by_zero); |
15016 | 1224 gripe_div0.mark_can_error (); |
1225 | |
1226 // divide is annoying because it might error | |
1227 fn = create_function (jit_convention::internal, | |
1228 "octave_jit_div_scalar_scalar", scalar, scalar, scalar); | |
1229 fn.mark_can_error (); | |
1230 | |
1231 llvm::BasicBlock *body = fn.new_block (); | |
1232 builder.SetInsertPoint (body); | |
1233 { | |
1234 llvm::BasicBlock *warn_block = fn.new_block ("warn"); | |
1235 llvm::BasicBlock *normal_block = fn.new_block ("normal"); | |
1236 | |
1237 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
|
1238 llvm::Value *check = builder.CreateFCmpUEQ (zero, fn.argument (builder, 1)); |
15016 | 1239 builder.CreateCondBr (check, warn_block, normal_block); |
1240 | |
1241 builder.SetInsertPoint (warn_block); | |
1242 gripe_div0.call (builder); | |
1243 builder.CreateBr (normal_block); | |
1244 | |
1245 builder.SetInsertPoint (normal_block); | |
1246 llvm::Value *ret = builder.CreateFDiv (fn.argument (builder, 0), | |
1247 fn.argument (builder, 1)); | |
1248 fn.do_return (builder, ret); | |
1249 } | |
1250 binary_ops[octave_value::op_div].add_overload (fn); | |
1251 binary_ops[octave_value::op_el_div].add_overload (fn); | |
1252 | |
1253 // ldiv is the same as div with the operators reversed | |
1254 fn = mirror_binary (fn); | |
1255 binary_ops[octave_value::op_ldiv].add_overload (fn); | |
1256 binary_ops[octave_value::op_el_ldiv].add_overload (fn); | |
1257 | |
1258 // In general, the result of scalar ^ scalar is a complex number. We might be | |
1259 // able to improve on this if we keep track of the range of values varaibles | |
1260 // can take on. | |
1261 fn = create_function (jit_convention::external, | |
1262 "octave_jit_pow_scalar_scalar", complex, scalar, | |
1263 scalar); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1264 fn.add_mapping (engine, &octave_jit_pow_scalar_scalar); |
15016 | 1265 binary_ops[octave_value::op_pow].add_overload (fn); |
1266 binary_ops[octave_value::op_el_pow].add_overload (fn); | |
1267 | |
15146
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1268 // now for unary scalar operations |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1269 // FIXME: Impelment not |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1270 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
|
1271 scalar); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1272 body = fn.new_block (); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1273 builder.SetInsertPoint (body); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1274 { |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1275 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
|
1276 llvm::Value *val = fn.argument (builder, 0); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1277 val = builder.CreateFAdd (val, one); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1278 fn.do_return (builder, val); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1279 } |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1280 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
|
1281 |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1282 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
|
1283 scalar); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1284 body = fn.new_block (); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1285 builder.SetInsertPoint (body); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1286 { |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1287 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
|
1288 llvm::Value *val = fn.argument (builder, 0); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1289 val = builder.CreateFSub (val, one); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1290 fn.do_return (builder, val); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1291 } |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1292 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
|
1293 |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1294 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
|
1295 scalar); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1296 body = fn.new_block (); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1297 builder.SetInsertPoint (body); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1298 { |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1299 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
|
1300 llvm::Value *val = fn.argument (builder, 0); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1301 val = builder.CreateFMul (val, mone); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1302 fn.do_return (builder, val); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1303 } |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1304 |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1305 fn = create_identity (scalar); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1306 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
|
1307 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
|
1308 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
|
1309 |
15016 | 1310 // now for binary complex operations |
1311 add_binary_op (complex, octave_value::op_add, llvm::Instruction::FAdd); | |
1312 add_binary_op (complex, octave_value::op_sub, llvm::Instruction::FSub); | |
1313 | |
1314 fn = create_function (jit_convention::internal, | |
1315 "octave_jit_*_complex_complex", complex, complex, | |
1316 complex); | |
1317 body = fn.new_block (); | |
1318 builder.SetInsertPoint (body); | |
1319 { | |
1320 // (x0*x1 - y0*y1, x0*y1 + y0*x1) = (x0,y0) * (x1,y1) | |
1321 // We compute this in one vectorized multiplication, a subtraction, and an | |
1322 // addition. | |
1323 llvm::Value *lhs = fn.argument (builder, 0); | |
1324 llvm::Value *rhs = fn.argument (builder, 1); | |
1325 | |
1326 // FIXME: We need a better way of doing this, working with llvm's IR | |
1327 // directly is sort of a pain. | |
1328 llvm::Value *zero = builder.getInt32 (0); | |
1329 llvm::Value *one = builder.getInt32 (1); | |
1330 llvm::Value *two = builder.getInt32 (2); | |
1331 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
|
1332 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
|
1333 |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1334 // 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
|
1335 // 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
|
1336 // 0+0i * NaN |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1337 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
|
1338 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
|
1339 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
|
1340 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
|
1341 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
|
1342 temp = builder.CreateAnd (temp, temp2); |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1343 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
|
1344 |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1345 builder.SetInsertPoint(real_mul); |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1346 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
|
1347 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
|
1348 builder.CreateBr (ret_block); |
15016 | 1349 |
1350 llvm::Type *vec4 = llvm::VectorType::get (scalar_t, 4); | |
1351 llvm::Value *mlhs = llvm::UndefValue::get (vec4); | |
1352 llvm::Value *mrhs = mlhs; | |
15583
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1353 builder.SetInsertPoint (complex_mul); |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1354 temp = complex_real (lhs); |
15016 | 1355 mlhs = builder.CreateInsertElement (mlhs, temp, zero); |
1356 mlhs = builder.CreateInsertElement (mlhs, temp, two); | |
1357 temp = complex_imag (lhs); | |
1358 mlhs = builder.CreateInsertElement (mlhs, temp, one); | |
1359 mlhs = builder.CreateInsertElement (mlhs, temp, three); | |
1360 | |
1361 temp = complex_real (rhs); | |
1362 mrhs = builder.CreateInsertElement (mrhs, temp, zero); | |
1363 mrhs = builder.CreateInsertElement (mrhs, temp, three); | |
1364 temp = complex_imag (rhs); | |
1365 mrhs = builder.CreateInsertElement (mrhs, temp, one); | |
1366 mrhs = builder.CreateInsertElement (mrhs, temp, two); | |
1367 | |
1368 llvm::Value *mres = builder.CreateFMul (mlhs, mrhs); | |
1369 llvm::Value *tlhs = builder.CreateExtractElement (mres, zero); | |
1370 llvm::Value *trhs = builder.CreateExtractElement (mres, one); | |
1371 llvm::Value *ret_real = builder.CreateFSub (tlhs, trhs); | |
1372 | |
1373 tlhs = builder.CreateExtractElement (mres, two); | |
1374 trhs = builder.CreateExtractElement (mres, three); | |
1375 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
|
1376 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
|
1377 builder.CreateBr (ret_block); |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1378 |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1379 builder.SetInsertPoint (ret_block); |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1380 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
|
1381 builder.Insert (merge); |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1382 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
|
1383 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
|
1384 fn.do_return (builder, merge); |
15016 | 1385 } |
1386 | |
1387 binary_ops[octave_value::op_mul].add_overload (fn); | |
1388 binary_ops[octave_value::op_el_mul].add_overload (fn); | |
1389 | |
1390 jit_function complex_div = create_function (jit_convention::external, | |
1391 "octave_jit_complex_div", | |
1392 complex, complex, complex); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1393 complex_div.add_mapping (engine, &octave_jit_complex_div); |
15016 | 1394 complex_div.mark_can_error (); |
1395 binary_ops[octave_value::op_div].add_overload (fn); | |
1396 binary_ops[octave_value::op_ldiv].add_overload (fn); | |
1397 | |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
1398 // 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
|
1399 // 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
|
1400 // binary_ops[octave_value::op_el_ldiv].add_overload (fn); |
15016 | 1401 |
1402 fn = create_function (jit_convention::external, | |
1403 "octave_jit_pow_complex_complex", complex, complex, | |
1404 complex); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1405 fn.add_mapping (engine, &octave_jit_pow_complex_complex); |
15016 | 1406 binary_ops[octave_value::op_pow].add_overload (fn); |
1407 binary_ops[octave_value::op_el_pow].add_overload (fn); | |
1408 | |
1409 fn = create_function (jit_convention::internal, | |
1410 "octave_jit_*_scalar_complex", complex, scalar, | |
1411 complex); | |
1412 jit_function mul_scalar_complex = fn; | |
1413 body = fn.new_block (); | |
1414 builder.SetInsertPoint (body); | |
1415 { | |
15583
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1416 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
|
1417 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
|
1418 |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1419 llvm::Value *fzero = llvm::ConstantFP::get (scalar_t, 0); |
15016 | 1420 llvm::Value *lhs = fn.argument (builder, 0); |
1421 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
|
1422 |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1423 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
|
1424 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
|
1425 |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1426 builder.SetInsertPoint (scalar_mul); |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1427 llvm::Value *temp = complex_real (rhs); |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1428 temp = builder.CreateFMul (lhs, temp); |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1429 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
|
1430 |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1431 |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1432 builder.SetInsertPoint (complex_mul); |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1433 temp = complex_new (lhs, lhs); |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1434 fn.do_return (builder, builder.CreateFMul (temp, rhs)); |
15016 | 1435 } |
1436 binary_ops[octave_value::op_mul].add_overload (fn); | |
1437 binary_ops[octave_value::op_el_mul].add_overload (fn); | |
1438 | |
1439 | |
1440 fn = mirror_binary (mul_scalar_complex); | |
1441 binary_ops[octave_value::op_mul].add_overload (fn); | |
1442 binary_ops[octave_value::op_el_mul].add_overload (fn); | |
1443 | |
1444 fn = create_function (jit_convention::internal, "octave_jit_+_scalar_complex", | |
1445 complex, scalar, complex); | |
1446 body = fn.new_block (); | |
1447 builder.SetInsertPoint (body); | |
1448 { | |
1449 llvm::Value *lhs = fn.argument (builder, 0); | |
1450 llvm::Value *rhs = fn.argument (builder, 1); | |
1451 llvm::Value *real = builder.CreateFAdd (lhs, complex_real (rhs)); | |
1452 fn.do_return (builder, complex_real (rhs, real)); | |
1453 } | |
1454 binary_ops[octave_value::op_add].add_overload (fn); | |
1455 | |
1456 fn = mirror_binary (fn); | |
1457 binary_ops[octave_value::op_add].add_overload (fn); | |
1458 | |
1459 fn = create_function (jit_convention::internal, "octave_jit_-_complex_scalar", | |
1460 complex, complex, scalar); | |
1461 body = fn.new_block (); | |
1462 builder.SetInsertPoint (body); | |
1463 { | |
1464 llvm::Value *lhs = fn.argument (builder, 0); | |
1465 llvm::Value *rhs = fn.argument (builder, 1); | |
1466 llvm::Value *real = builder.CreateFSub (complex_real (lhs), rhs); | |
1467 fn.do_return (builder, complex_real (lhs, real)); | |
1468 } | |
1469 binary_ops[octave_value::op_sub].add_overload (fn); | |
1470 | |
1471 fn = create_function (jit_convention::internal, "octave_jit_-_scalar_complex", | |
1472 complex, scalar, complex); | |
1473 body = fn.new_block (); | |
1474 builder.SetInsertPoint (body); | |
1475 { | |
1476 llvm::Value *lhs = fn.argument (builder, 0); | |
1477 llvm::Value *rhs = fn.argument (builder, 1); | |
1478 llvm::Value *real = builder.CreateFSub (lhs, complex_real (rhs)); | |
1479 fn.do_return (builder, complex_real (rhs, real)); | |
1480 } | |
1481 binary_ops[octave_value::op_sub].add_overload (fn); | |
1482 | |
1483 fn = create_function (jit_convention::external, | |
1484 "octave_jit_pow_scalar_complex", complex, scalar, | |
1485 complex); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1486 fn.add_mapping (engine, &octave_jit_pow_scalar_complex); |
15016 | 1487 binary_ops[octave_value::op_pow].add_overload (fn); |
1488 binary_ops[octave_value::op_el_pow].add_overload (fn); | |
1489 | |
1490 fn = create_function (jit_convention::external, | |
1491 "octave_jit_pow_complex_scalar", complex, complex, | |
1492 scalar); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1493 fn.add_mapping (engine, &octave_jit_pow_complex_scalar); |
15016 | 1494 binary_ops[octave_value::op_pow].add_overload (fn); |
1495 binary_ops[octave_value::op_el_pow].add_overload (fn); | |
1496 | |
1497 // now for binary index operators | |
1498 add_binary_op (index, octave_value::op_add, llvm::Instruction::Add); | |
1499 | |
1500 // and binary bool operators | |
1501 add_binary_op (boolean, octave_value::op_el_or, llvm::Instruction::Or); | |
1502 add_binary_op (boolean, octave_value::op_el_and, llvm::Instruction::And); | |
1503 | |
1504 // now for printing functions | |
1505 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
|
1506 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
|
1507 add_print (scalar, reinterpret_cast<void *> (&octave_jit_print_scalar)); |
15016 | 1508 |
1509 // initialize for loop | |
1510 for_init_fn.stash_name ("for_init"); | |
1511 | |
1512 fn = create_function (jit_convention::internal, "octave_jit_for_range_init", | |
1513 index, range); | |
1514 body = fn.new_block (); | |
1515 builder.SetInsertPoint (body); | |
1516 { | |
1517 llvm::Value *zero = llvm::ConstantInt::get (index_t, 0); | |
1518 fn.do_return (builder, zero); | |
1519 } | |
1520 for_init_fn.add_overload (fn); | |
1521 | |
1522 // bounds check for for loop | |
1523 for_check_fn.stash_name ("for_check"); | |
1524 | |
1525 fn = create_function (jit_convention::internal, "octave_jit_for_range_check", | |
1526 boolean, range, index); | |
1527 body = fn.new_block (); | |
1528 builder.SetInsertPoint (body); | |
1529 { | |
1530 llvm::Value *nelem | |
1531 = builder.CreateExtractValue (fn.argument (builder, 0), 3); | |
1532 llvm::Value *idx = fn.argument (builder, 1); | |
1533 llvm::Value *ret = builder.CreateICmpULT (idx, nelem); | |
1534 fn.do_return (builder, ret); | |
1535 } | |
1536 for_check_fn.add_overload (fn); | |
1537 | |
1538 // index variabe for for loop | |
1539 for_index_fn.stash_name ("for_index"); | |
1540 | |
1541 fn = create_function (jit_convention::internal, "octave_jit_for_range_idx", | |
1542 scalar, range, index); | |
1543 body = fn.new_block (); | |
1544 builder.SetInsertPoint (body); | |
1545 { | |
1546 llvm::Value *idx = fn.argument (builder, 1); | |
1547 llvm::Value *didx = builder.CreateSIToFP (idx, scalar_t); | |
1548 llvm::Value *rng = fn.argument (builder, 0); | |
1549 llvm::Value *base = builder.CreateExtractValue (rng, 0); | |
1550 llvm::Value *inc = builder.CreateExtractValue (rng, 2); | |
1551 | |
1552 llvm::Value *ret = builder.CreateFMul (didx, inc); | |
1553 ret = builder.CreateFAdd (base, ret); | |
1554 fn.do_return (builder, ret); | |
1555 } | |
1556 for_index_fn.add_overload (fn); | |
1557 | |
1558 // logically true | |
1559 logically_true_fn.stash_name ("logically_true"); | |
1560 | |
1561 jit_function gripe_nantl | |
1562 = create_function (jit_convention::external, | |
1563 "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
|
1564 gripe_nantl.add_mapping (engine, &octave_jit_gripe_nan_to_logical_conversion); |
15016 | 1565 gripe_nantl.mark_can_error (); |
1566 | |
1567 fn = create_function (jit_convention::internal, | |
1568 "octave_jit_logically_true_scalar", boolean, scalar); | |
1569 fn.mark_can_error (); | |
1570 | |
1571 body = fn.new_block (); | |
1572 builder.SetInsertPoint (body); | |
1573 { | |
1574 llvm::BasicBlock *error_block = fn.new_block ("error"); | |
1575 llvm::BasicBlock *normal_block = fn.new_block ("normal"); | |
1576 | |
1577 llvm::Value *check = builder.CreateFCmpUNE (fn.argument (builder, 0), | |
1578 fn.argument (builder, 0)); | |
1579 builder.CreateCondBr (check, error_block, normal_block); | |
1580 | |
1581 builder.SetInsertPoint (error_block); | |
1582 gripe_nantl.call (builder); | |
1583 builder.CreateBr (normal_block); | |
1584 builder.SetInsertPoint (normal_block); | |
1585 | |
1586 llvm::Value *zero = llvm::ConstantFP::get (scalar_t, 0); | |
1587 llvm::Value *ret = builder.CreateFCmpONE (fn.argument (builder, 0), zero); | |
1588 fn.do_return (builder, ret); | |
1589 } | |
1590 logically_true_fn.add_overload (fn); | |
1591 | |
1592 // logically_true boolean | |
1593 fn = create_identity (boolean); | |
1594 logically_true_fn.add_overload (fn); | |
1595 | |
1596 // make_range | |
1597 // FIXME: May be benificial to implement all in LLVM | |
1598 make_range_fn.stash_name ("make_range"); | |
1599 jit_function compute_nelem | |
1600 = create_function (jit_convention::external, "octave_jit_compute_nelem", | |
1601 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
|
1602 compute_nelem.add_mapping (engine, &octave_jit_compute_nelem); |
15016 | 1603 |
1604 fn = create_function (jit_convention::internal, "octave_jit_make_range", | |
1605 range, scalar, scalar, scalar); | |
1606 body = fn.new_block (); | |
1607 builder.SetInsertPoint (body); | |
1608 { | |
1609 llvm::Value *base = fn.argument (builder, 0); | |
1610 llvm::Value *limit = fn.argument (builder, 1); | |
1611 llvm::Value *inc = fn.argument (builder, 2); | |
1612 llvm::Value *nelem = compute_nelem.call (builder, base, limit, inc); | |
1613 | |
1614 llvm::Value *dzero = llvm::ConstantFP::get (scalar_t, 0); | |
1615 llvm::Value *izero = llvm::ConstantInt::get (index_t, 0); | |
1616 llvm::Value *rng = llvm::ConstantStruct::get (range_t, dzero, dzero, dzero, | |
1617 izero, NULL); | |
1618 rng = builder.CreateInsertValue (rng, base, 0); | |
1619 rng = builder.CreateInsertValue (rng, limit, 1); | |
1620 rng = builder.CreateInsertValue (rng, inc, 2); | |
1621 rng = builder.CreateInsertValue (rng, nelem, 3); | |
1622 fn.do_return (builder, rng); | |
1623 } | |
1624 make_range_fn.add_overload (fn); | |
1625 | |
1626 // paren_subsref | |
1627 jit_type *jit_int = intN (sizeof (int) * 8); | |
1628 llvm::Type *int_t = jit_int->to_llvm (); | |
1629 jit_function ginvalid_index | |
1630 = create_function (jit_convention::external, "octave_jit_ginvalid_index", | |
1631 0); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1632 ginvalid_index.add_mapping (engine, &octave_jit_ginvalid_index); |
15016 | 1633 jit_function gindex_range = create_function (jit_convention::external, |
1634 "octave_jit_gindex_range", | |
1635 0, jit_int, jit_int, index, | |
1636 index); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1637 gindex_range.add_mapping (engine, &octave_jit_gindex_range); |
15016 | 1638 |
1639 fn = create_function (jit_convention::internal, "()subsref", scalar, matrix, | |
1640 scalar); | |
1641 fn.mark_can_error (); | |
1642 | |
1643 body = fn.new_block (); | |
1644 builder.SetInsertPoint (body); | |
1645 { | |
1646 llvm::Value *one = llvm::ConstantInt::get (index_t, 1); | |
1647 llvm::Value *ione; | |
1648 if (index_t == int_t) | |
1649 ione = one; | |
1650 else | |
1651 ione = llvm::ConstantInt::get (int_t, 1); | |
1652 | |
1653 llvm::Value *undef = llvm::UndefValue::get (scalar_t); | |
1654 llvm::Value *mat = fn.argument (builder, 0); | |
1655 llvm::Value *idx = fn.argument (builder, 1); | |
1656 | |
1657 // convert index to scalar to integer, and check index >= 1 | |
1658 llvm::Value *int_idx = builder.CreateFPToSI (idx, index_t); | |
1659 llvm::Value *check_idx = builder.CreateSIToFP (int_idx, scalar_t); | |
1660 llvm::Value *cond0 = builder.CreateFCmpUNE (idx, check_idx); | |
1661 llvm::Value *cond1 = builder.CreateICmpSLT (int_idx, one); | |
1662 llvm::Value *cond = builder.CreateOr (cond0, cond1); | |
1663 | |
1664 llvm::BasicBlock *done = fn.new_block ("done"); | |
1665 llvm::BasicBlock *conv_error = fn.new_block ("conv_error", done); | |
1666 llvm::BasicBlock *normal = fn.new_block ("normal", done); | |
1667 builder.CreateCondBr (cond, conv_error, normal); | |
1668 | |
1669 builder.SetInsertPoint (conv_error); | |
1670 ginvalid_index.call (builder); | |
1671 builder.CreateBr (done); | |
1672 | |
1673 builder.SetInsertPoint (normal); | |
1674 llvm::Value *len = builder.CreateExtractValue (mat, | |
1675 llvm::ArrayRef<unsigned> (2)); | |
1676 cond = builder.CreateICmpSGT (int_idx, len); | |
1677 | |
1678 | |
1679 llvm::BasicBlock *bounds_error = fn.new_block ("bounds_error", done); | |
1680 llvm::BasicBlock *success = fn.new_block ("success", done); | |
1681 builder.CreateCondBr (cond, bounds_error, success); | |
1682 | |
1683 builder.SetInsertPoint (bounds_error); | |
1684 gindex_range.call (builder, ione, ione, int_idx, len); | |
1685 builder.CreateBr (done); | |
1686 | |
1687 builder.SetInsertPoint (success); | |
1688 llvm::Value *data = builder.CreateExtractValue (mat, | |
1689 llvm::ArrayRef<unsigned> (1)); | |
1690 llvm::Value *gep = builder.CreateInBoundsGEP (data, int_idx); | |
1691 llvm::Value *ret = builder.CreateLoad (gep); | |
1692 builder.CreateBr (done); | |
1693 | |
1694 builder.SetInsertPoint (done); | |
1695 | |
1696 llvm::PHINode *merge = llvm::PHINode::Create (scalar_t, 3); | |
1697 builder.Insert (merge); | |
1698 merge->addIncoming (undef, conv_error); | |
1699 merge->addIncoming (undef, bounds_error); | |
1700 merge->addIncoming (ret, success); | |
1701 fn.do_return (builder, merge); | |
1702 } | |
1703 paren_subsref_fn.add_overload (fn); | |
1704 | |
1705 // paren subsasgn | |
1706 paren_subsasgn_fn.stash_name ("()subsasgn"); | |
1707 | |
1708 jit_function resize_paren_subsasgn | |
1709 = create_function (jit_convention::external, | |
15027
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1710 "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
|
1711 scalar); |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1712 resize_paren_subsasgn.add_mapping (engine, &octave_jit_paren_subsasgn_impl); |
15016 | 1713 fn = create_function (jit_convention::internal, "octave_jit_paren_subsasgn", |
1714 matrix, matrix, scalar, scalar); | |
1715 fn.mark_can_error (); | |
1716 body = fn.new_block (); | |
1717 builder.SetInsertPoint (body); | |
1718 { | |
1719 llvm::Value *one = llvm::ConstantInt::get (index_t, 1); | |
1720 | |
1721 llvm::Value *mat = fn.argument (builder, 0); | |
1722 llvm::Value *idx = fn.argument (builder, 1); | |
1723 llvm::Value *value = fn.argument (builder, 2); | |
1724 | |
1725 llvm::Value *int_idx = builder.CreateFPToSI (idx, index_t); | |
1726 llvm::Value *check_idx = builder.CreateSIToFP (int_idx, scalar_t); | |
1727 llvm::Value *cond0 = builder.CreateFCmpUNE (idx, check_idx); | |
1728 llvm::Value *cond1 = builder.CreateICmpSLT (int_idx, one); | |
1729 llvm::Value *cond = builder.CreateOr (cond0, cond1); | |
1730 | |
1731 llvm::BasicBlock *done = fn.new_block ("done"); | |
1732 | |
1733 llvm::BasicBlock *conv_error = fn.new_block ("conv_error", done); | |
1734 llvm::BasicBlock *normal = fn.new_block ("normal", done); | |
1735 builder.CreateCondBr (cond, conv_error, normal); | |
1736 builder.SetInsertPoint (conv_error); | |
1737 ginvalid_index.call (builder); | |
1738 builder.CreateBr (done); | |
1739 | |
1740 builder.SetInsertPoint (normal); | |
15056
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
1741 llvm::Value *len = builder.CreateExtractValue (mat, 2); |
15016 | 1742 cond0 = builder.CreateICmpSGT (int_idx, len); |
1743 | |
1744 llvm::Value *rcount = builder.CreateExtractValue (mat, 0); | |
1745 rcount = builder.CreateLoad (rcount); | |
1746 cond1 = builder.CreateICmpSGT (rcount, one); | |
1747 cond = builder.CreateOr (cond0, cond1); | |
1748 | |
1749 llvm::BasicBlock *bounds_error = fn.new_block ("bounds_error", done); | |
1750 llvm::BasicBlock *success = fn.new_block ("success", done); | |
1751 builder.CreateCondBr (cond, bounds_error, success); | |
1752 | |
1753 // resize on out of bounds access | |
1754 builder.SetInsertPoint (bounds_error); | |
15027
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1755 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
|
1756 int_idx, value); |
15016 | 1757 builder.CreateBr (done); |
1758 | |
1759 builder.SetInsertPoint (success); | |
1760 llvm::Value *data = builder.CreateExtractValue (mat, | |
1761 llvm::ArrayRef<unsigned> (1)); | |
1762 llvm::Value *gep = builder.CreateInBoundsGEP (data, int_idx); | |
1763 builder.CreateStore (value, gep); | |
1764 builder.CreateBr (done); | |
1765 | |
1766 builder.SetInsertPoint (done); | |
1767 | |
1768 llvm::PHINode *merge = llvm::PHINode::Create (matrix_t, 3); | |
1769 builder.Insert (merge); | |
1770 merge->addIncoming (mat, conv_error); | |
1771 merge->addIncoming (resize_result, bounds_error); | |
1772 merge->addIncoming (mat, success); | |
1773 fn.do_return (builder, merge); | |
1774 } | |
1775 paren_subsasgn_fn.add_overload (fn); | |
1776 | |
1777 fn = create_function (jit_convention::external, | |
1778 "octave_jit_paren_subsasgn_matrix_range", matrix, | |
1779 matrix, range, scalar); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1780 fn.add_mapping (engine, &octave_jit_paren_subsasgn_matrix_range); |
15016 | 1781 fn.mark_can_error (); |
1782 paren_subsasgn_fn.add_overload (fn); | |
1783 | |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1784 end1_fn.stash_name ("end1"); |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1785 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
|
1786 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
|
1787 body = fn.new_block (); |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
1788 builder.SetInsertPoint (body); |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
1789 { |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
1790 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
|
1791 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
|
1792 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
|
1793 } |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1794 end1_fn.add_overload (fn); |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1795 |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1796 end_fn.stash_name ("end"); |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1797 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
|
1798 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
|
1799 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
|
1800 |
15337
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
1801 // -------------------- create_undef -------------------- |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
1802 create_undef_fn.stash_name ("create_undef"); |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
1803 fn = create_function (jit_convention::external, "octave_jit_create_undef", |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
1804 any); |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
1805 create_undef_fn.add_overload (fn); |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
1806 |
15016 | 1807 casts[any->type_id ()].stash_name ("(any)"); |
1808 casts[scalar->type_id ()].stash_name ("(scalar)"); | |
1809 casts[complex->type_id ()].stash_name ("(complex)"); | |
1810 casts[matrix->type_id ()].stash_name ("(matrix)"); | |
15603 | 1811 casts[range->type_id ()].stash_name ("(range)"); |
15016 | 1812 |
1813 // cast any <- matrix | |
1814 fn = create_function (jit_convention::external, "octave_jit_cast_any_matrix", | |
1815 any, matrix); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1816 fn.add_mapping (engine, &octave_jit_cast_any_matrix); |
15016 | 1817 casts[any->type_id ()].add_overload (fn); |
1818 | |
1819 // cast matrix <- any | |
1820 fn = create_function (jit_convention::external, "octave_jit_cast_matrix_any", | |
1821 matrix, any); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1822 fn.add_mapping (engine, &octave_jit_cast_matrix_any); |
15016 | 1823 casts[matrix->type_id ()].add_overload (fn); |
1824 | |
15027
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1825 // cast any <- range |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1826 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
|
1827 any, range); |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1828 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
|
1829 casts[any->type_id ()].add_overload (fn); |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1830 |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1831 // cast range <- any |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1832 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
|
1833 range, any); |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1834 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
|
1835 casts[range->type_id ()].add_overload (fn); |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1836 |
15016 | 1837 // cast any <- scalar |
1838 fn = create_function (jit_convention::external, "octave_jit_cast_any_scalar", | |
1839 any, scalar); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1840 fn.add_mapping (engine, &octave_jit_cast_any_scalar); |
15016 | 1841 casts[any->type_id ()].add_overload (fn); |
1842 | |
1843 // cast scalar <- any | |
1844 fn = create_function (jit_convention::external, "octave_jit_cast_scalar_any", | |
1845 scalar, any); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1846 fn.add_mapping (engine, &octave_jit_cast_scalar_any); |
15016 | 1847 casts[scalar->type_id ()].add_overload (fn); |
1848 | |
1849 // cast any <- complex | |
1850 fn = create_function (jit_convention::external, "octave_jit_cast_any_complex", | |
1851 any, complex); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1852 fn.add_mapping (engine, &octave_jit_cast_any_complex); |
15016 | 1853 casts[any->type_id ()].add_overload (fn); |
1854 | |
1855 // cast complex <- any | |
1856 fn = create_function (jit_convention::external, "octave_jit_cast_complex_any", | |
1857 complex, any); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1858 fn.add_mapping (engine, &octave_jit_cast_complex_any); |
15016 | 1859 casts[complex->type_id ()].add_overload (fn); |
1860 | |
1861 // cast complex <- scalar | |
1862 fn = create_function (jit_convention::internal, | |
1863 "octave_jit_cast_complex_scalar", complex, scalar); | |
1864 body = fn.new_block (); | |
1865 builder.SetInsertPoint (body); | |
1866 { | |
1867 llvm::Value *zero = llvm::ConstantFP::get (scalar_t, 0); | |
1868 fn.do_return (builder, complex_new (fn.argument (builder, 0), zero)); | |
1869 } | |
1870 casts[complex->type_id ()].add_overload (fn); | |
1871 | |
1872 // cast scalar <- complex | |
1873 fn = create_function (jit_convention::internal, | |
1874 "octave_jit_cast_scalar_complex", scalar, complex); | |
1875 body = fn.new_block (); | |
1876 builder.SetInsertPoint (body); | |
1877 fn.do_return (builder, complex_real (fn.argument (builder, 0))); | |
1878 casts[scalar->type_id ()].add_overload (fn); | |
1879 | |
1880 // cast any <- any | |
1881 fn = create_identity (any); | |
1882 casts[any->type_id ()].add_overload (fn); | |
1883 | |
1884 // cast scalar <- scalar | |
1885 fn = create_identity (scalar); | |
1886 casts[scalar->type_id ()].add_overload (fn); | |
1887 | |
1888 // cast complex <- complex | |
1889 fn = create_identity (complex); | |
1890 casts[complex->type_id ()].add_overload (fn); | |
1891 | |
1892 // -------------------- builtin functions -------------------- | |
1893 add_builtin ("#unknown_function"); | |
1894 unknown_function = builtins["#unknown_function"]; | |
1895 | |
1896 add_builtin ("sin"); | |
1897 register_intrinsic ("sin", llvm::Intrinsic::sin, scalar, scalar); | |
1898 register_generic ("sin", matrix, matrix); | |
1899 | |
1900 add_builtin ("cos"); | |
1901 register_intrinsic ("cos", llvm::Intrinsic::cos, scalar, scalar); | |
1902 register_generic ("cos", matrix, matrix); | |
1903 | |
1904 add_builtin ("exp"); | |
1905 register_intrinsic ("exp", llvm::Intrinsic::cos, scalar, scalar); | |
1906 register_generic ("exp", matrix, matrix); | |
1907 | |
15169
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 ("balance"); |
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 ("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
|
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 ("cond"); |
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 ("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
|
1913 |
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
1914 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
|
1915 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
|
1916 |
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
1917 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
|
1918 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
|
1919 |
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
1920 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
|
1921 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
|
1922 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
|
1923 |
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
1924 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
|
1925 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
|
1926 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
|
1927 |
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
1928 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
|
1929 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
|
1930 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
|
1931 |
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
1932 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
|
1933 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
|
1934 |
15016 | 1935 casts.resize (next_id + 1); |
1936 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
|
1937 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
|
1938 "octave_jit_grab_any", any, any); |
8125773322d4
Error on undefined an unused variables in JIT
Max Brister <max@2bass.com>
parents:
15311
diff
changeset
|
1939 grab_any.add_mapping (engine, &octave_jit_grab_any); |
15016 | 1940 jit_function release_any = get_release (any); |
1941 std::vector<jit_type *> args; | |
1942 args.resize (1); | |
1943 | |
1944 for (std::map<std::string, jit_type *>::iterator iter = builtins.begin (); | |
1945 iter != builtins.end (); ++iter) | |
1946 { | |
1947 jit_type *btype = iter->second; | |
1948 args[0] = btype; | |
1949 | |
15334
8125773322d4
Error on undefined an unused variables in JIT
Max Brister <max@2bass.com>
parents:
15311
diff
changeset
|
1950 grab_fn.add_overload (jit_function (grab_any, btype, args)); |
15016 | 1951 release_fn.add_overload (jit_function (release_any, 0, args)); |
1952 casts[any->type_id ()].add_overload (jit_function (any_id, any, args)); | |
1953 | |
1954 args[0] = any; | |
1955 casts[btype->type_id ()].add_overload (jit_function (any_id, btype, | |
1956 args)); | |
1957 } | |
1958 } | |
1959 | |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1960 const jit_function& |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1961 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
|
1962 { |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1963 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
|
1964 if (ccount && ccount->value () == 1) |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1965 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
|
1966 |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1967 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
|
1968 } |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1969 |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1970 jit_type* |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1971 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
|
1972 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
|
1973 { |
15124
0464e3ceb85b
Skip functions when resolving end context in JIT
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
1974 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
|
1975 id_to_type.push_back (ret); |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1976 return ret; |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1977 } |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1978 |
15016 | 1979 void |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1980 jit_typeinfo::add_print (jit_type *ty, void *fptr) |
15016 | 1981 { |
1982 std::stringstream name; | |
1983 name << "octave_jit_print_" << ty->name (); | |
1984 jit_function fn = create_function (jit_convention::external, name.str (), 0, | |
1985 intN (8), ty); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1986 fn.add_mapping (engine, fptr); |
15016 | 1987 print_fn.add_overload (fn); |
1988 } | |
1989 | |
1990 // FIXME: cp between add_binary_op, add_binary_icmp, and add_binary_fcmp | |
1991 void | |
1992 jit_typeinfo::add_binary_op (jit_type *ty, int op, int llvm_op) | |
1993 { | |
1994 std::stringstream fname; | |
1995 octave_value::binary_op ov_op = static_cast<octave_value::binary_op>(op); | |
1996 fname << "octave_jit_" << octave_value::binary_op_as_string (ov_op) | |
1997 << "_" << ty->name (); | |
1998 | |
1999 jit_function fn = create_function (jit_convention::internal, fname.str (), | |
2000 ty, ty, ty); | |
2001 llvm::BasicBlock *block = fn.new_block (); | |
2002 builder.SetInsertPoint (block); | |
2003 llvm::Instruction::BinaryOps temp | |
2004 = static_cast<llvm::Instruction::BinaryOps>(llvm_op); | |
2005 | |
2006 llvm::Value *ret = builder.CreateBinOp (temp, fn.argument (builder, 0), | |
2007 fn.argument (builder, 1)); | |
2008 fn.do_return (builder, ret); | |
2009 binary_ops[op].add_overload (fn); | |
2010 } | |
2011 | |
2012 void | |
2013 jit_typeinfo::add_binary_icmp (jit_type *ty, int op, int llvm_op) | |
2014 { | |
2015 std::stringstream fname; | |
2016 octave_value::binary_op ov_op = static_cast<octave_value::binary_op>(op); | |
2017 fname << "octave_jit" << octave_value::binary_op_as_string (ov_op) | |
2018 << "_" << ty->name (); | |
2019 | |
2020 jit_function fn = create_function (jit_convention::internal, fname.str (), | |
2021 boolean, ty, ty); | |
2022 llvm::BasicBlock *block = fn.new_block (); | |
2023 builder.SetInsertPoint (block); | |
2024 llvm::CmpInst::Predicate temp | |
2025 = static_cast<llvm::CmpInst::Predicate>(llvm_op); | |
2026 llvm::Value *ret = builder.CreateICmp (temp, fn.argument (builder, 0), | |
2027 fn.argument (builder, 1)); | |
2028 fn.do_return (builder, ret); | |
2029 binary_ops[op].add_overload (fn); | |
2030 } | |
2031 | |
2032 void | |
2033 jit_typeinfo::add_binary_fcmp (jit_type *ty, int op, int llvm_op) | |
2034 { | |
2035 std::stringstream fname; | |
2036 octave_value::binary_op ov_op = static_cast<octave_value::binary_op>(op); | |
2037 fname << "octave_jit" << octave_value::binary_op_as_string (ov_op) | |
2038 << "_" << ty->name (); | |
2039 | |
2040 jit_function fn = create_function (jit_convention::internal, fname.str (), | |
2041 boolean, ty, ty); | |
2042 llvm::BasicBlock *block = fn.new_block (); | |
2043 builder.SetInsertPoint (block); | |
2044 llvm::CmpInst::Predicate temp | |
2045 = static_cast<llvm::CmpInst::Predicate>(llvm_op); | |
2046 llvm::Value *ret = builder.CreateFCmp (temp, fn.argument (builder, 0), | |
2047 fn.argument (builder, 1)); | |
2048 fn.do_return (builder, ret); | |
2049 binary_ops[op].add_overload (fn); | |
2050 } | |
2051 | |
2052 jit_function | |
2053 jit_typeinfo::create_function (jit_convention::type cc, const llvm::Twine& name, | |
2054 jit_type *ret, | |
2055 const std::vector<jit_type *>& args) | |
2056 { | |
2057 jit_function result (module, cc, name, ret, args); | |
2058 return result; | |
2059 } | |
2060 | |
2061 jit_function | |
2062 jit_typeinfo::create_identity (jit_type *type) | |
2063 { | |
2064 size_t id = type->type_id (); | |
2065 if (id >= identities.size ()) | |
2066 identities.resize (id + 1); | |
2067 | |
2068 if (! identities[id].valid ()) | |
2069 { | |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
2070 std::stringstream name; |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
2071 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
|
2072 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
|
2073 type, type); |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
2074 |
15016 | 2075 llvm::BasicBlock *body = fn.new_block (); |
2076 builder.SetInsertPoint (body); | |
2077 fn.do_return (builder, fn.argument (builder, 0)); | |
2078 return identities[id] = fn; | |
2079 } | |
2080 | |
2081 return identities[id]; | |
2082 } | |
2083 | |
2084 llvm::Value * | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
2085 jit_typeinfo::do_insert_error_check (llvm::IRBuilderD& abuilder) |
15016 | 2086 { |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
2087 return abuilder.CreateLoad (lerror_state); |
15016 | 2088 } |
2089 | |
15603 | 2090 llvm::Value * |
2091 jit_typeinfo::do_insert_interrupt_check (llvm::IRBuilderD& abuilder) | |
2092 { | |
2093 llvm::LoadInst *val = abuilder.CreateLoad (loctave_interrupt_state); | |
2094 val->setVolatile (true); | |
2095 return abuilder.CreateICmpSGT (val, abuilder.getInt32 (0)); | |
2096 } | |
2097 | |
15016 | 2098 void |
2099 jit_typeinfo::add_builtin (const std::string& name) | |
2100 { | |
15124
0464e3ceb85b
Skip functions when resolving end context in JIT
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
2101 jit_type *btype = new_type (name, any, any->to_llvm (), true); |
15016 | 2102 builtins[name] = btype; |
2103 | |
2104 octave_builtin *ov_builtin = find_builtin (name); | |
2105 if (ov_builtin) | |
2106 ov_builtin->stash_jit (*btype); | |
2107 } | |
2108 | |
2109 void | |
2110 jit_typeinfo::register_intrinsic (const std::string& name, size_t iid, | |
2111 jit_type *result, | |
2112 const std::vector<jit_type *>& args) | |
2113 { | |
2114 jit_type *builtin_type = builtins[name]; | |
2115 size_t nargs = args.size (); | |
2116 llvm::SmallVector<llvm::Type *, 5> llvm_args (nargs); | |
2117 for (size_t i = 0; i < nargs; ++i) | |
2118 llvm_args[i] = args[i]->to_llvm (); | |
2119 | |
2120 llvm::Intrinsic::ID id = static_cast<llvm::Intrinsic::ID> (iid); | |
2121 llvm::Function *ifun = llvm::Intrinsic::getDeclaration (module, id, | |
2122 llvm_args); | |
2123 std::stringstream fn_name; | |
2124 fn_name << "octave_jit_" << name; | |
2125 | |
2126 std::vector<jit_type *> args1 (nargs + 1); | |
2127 args1[0] = builtin_type; | |
2128 std::copy (args.begin (), args.end (), args1.begin () + 1); | |
2129 | |
2130 // The first argument will be the Octave function, but we already know that | |
2131 // the function call is the equivalent of the intrinsic, so we ignore it and | |
2132 // call the intrinsic with the remaining arguments. | |
2133 jit_function fn = create_function (jit_convention::internal, fn_name.str (), | |
2134 result, args1); | |
2135 llvm::BasicBlock *body = fn.new_block (); | |
2136 builder.SetInsertPoint (body); | |
2137 | |
2138 llvm::SmallVector<llvm::Value *, 5> fargs (nargs); | |
2139 for (size_t i = 0; i < nargs; ++i) | |
2140 fargs[i] = fn.argument (builder, i + 1); | |
2141 | |
2142 llvm::Value *ret = builder.CreateCall (ifun, fargs); | |
2143 fn.do_return (builder, ret); | |
2144 paren_subsref_fn.add_overload (fn); | |
2145 } | |
2146 | |
2147 octave_builtin * | |
2148 jit_typeinfo::find_builtin (const std::string& name) | |
2149 { | |
2150 // FIXME: Finalize what we want to store in octave_builtin, then add functions | |
2151 // to access these values in octave_value | |
2152 octave_value ov_builtin = symbol_table::find (name); | |
2153 return dynamic_cast<octave_builtin *> (ov_builtin.internal_rep ()); | |
2154 } | |
2155 | |
2156 void | |
15135
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2157 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
|
2158 const std::vector<jit_type *>& args) |
15016 | 2159 { |
15135
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2160 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
|
2161 if (! builtin) |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2162 return; |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2163 |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2164 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
|
2165 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
|
2166 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
|
2167 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
|
2168 fn_args); |
15136
eeaaac7c86b6
jit-typeinfo.cc (jit_typeinfo::register_generic): Mark can error
Max Brister <max@2bass.com>
parents:
15135
diff
changeset
|
2169 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
|
2170 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
|
2171 builder.SetInsertPoint (block); |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2172 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
|
2173 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
|
2174 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
|
2175 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
|
2176 { |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2177 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
|
2178 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
|
2179 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
|
2180 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
|
2181 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
|
2182 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
|
2183 } |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2184 |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2185 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
|
2186 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
|
2187 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
|
2188 |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2189 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
|
2190 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
|
2191 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
|
2192 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
|
2193 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
|
2194 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
|
2195 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
|
2196 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
|
2197 |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2198 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
|
2199 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
|
2200 paren_subsref_fn.add_overload (fn); |
15016 | 2201 } |
2202 | |
2203 jit_function | |
2204 jit_typeinfo::mirror_binary (const jit_function& fn) | |
2205 { | |
2206 jit_function ret = create_function (jit_convention::internal, | |
2207 fn.name () + "_reverse", | |
2208 fn.result (), fn.argument_type (1), | |
2209 fn.argument_type (0)); | |
2210 if (fn.can_error ()) | |
2211 ret.mark_can_error (); | |
2212 | |
2213 llvm::BasicBlock *body = ret.new_block (); | |
2214 builder.SetInsertPoint (body); | |
2215 llvm::Value *result = fn.call (builder, ret.argument (builder, 1), | |
2216 ret.argument (builder, 0)); | |
2217 if (ret.result ()) | |
2218 ret.do_return (builder, result); | |
2219 else | |
2220 ret.do_return (builder); | |
2221 | |
2222 return ret; | |
2223 } | |
2224 | |
2225 llvm::Value * | |
2226 jit_typeinfo::pack_complex (llvm::IRBuilderD& bld, llvm::Value *cplx) | |
2227 { | |
2228 llvm::Type *complex_ret = instance->complex_ret; | |
2229 llvm::Value *real = bld.CreateExtractElement (cplx, bld.getInt32 (0)); | |
2230 llvm::Value *imag = bld.CreateExtractElement (cplx, bld.getInt32 (1)); | |
2231 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
|
2232 |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
2233 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
|
2234 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
|
2235 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
|
2236 return bld.CreateInsertValue (ret, imag, im_idx); |
15016 | 2237 } |
2238 | |
2239 llvm::Value * | |
2240 jit_typeinfo::unpack_complex (llvm::IRBuilderD& bld, llvm::Value *result) | |
2241 { | |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
2242 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
|
2243 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
|
2244 |
15016 | 2245 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
|
2246 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
|
2247 llvm::Value *imag = bld.CreateExtractValue (result, im_idx); |
15016 | 2248 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
|
2249 |
15016 | 2250 ret = bld.CreateInsertElement (ret, real, bld.getInt32 (0)); |
2251 return bld.CreateInsertElement (ret, imag, bld.getInt32 (1)); | |
2252 } | |
2253 | |
2254 llvm::Value * | |
2255 jit_typeinfo::complex_real (llvm::Value *cx) | |
2256 { | |
2257 return builder.CreateExtractElement (cx, builder.getInt32 (0)); | |
2258 } | |
2259 | |
2260 llvm::Value * | |
2261 jit_typeinfo::complex_real (llvm::Value *cx, llvm::Value *real) | |
2262 { | |
2263 return builder.CreateInsertElement (cx, real, builder.getInt32 (0)); | |
2264 } | |
2265 | |
2266 llvm::Value * | |
2267 jit_typeinfo::complex_imag (llvm::Value *cx) | |
2268 { | |
2269 return builder.CreateExtractElement (cx, builder.getInt32 (1)); | |
2270 } | |
2271 | |
2272 llvm::Value * | |
2273 jit_typeinfo::complex_imag (llvm::Value *cx, llvm::Value *imag) | |
2274 { | |
2275 return builder.CreateInsertElement (cx, imag, builder.getInt32 (1)); | |
2276 } | |
2277 | |
2278 llvm::Value * | |
2279 jit_typeinfo::complex_new (llvm::Value *real, llvm::Value *imag) | |
2280 { | |
2281 llvm::Value *ret = llvm::UndefValue::get (complex->to_llvm ()); | |
2282 ret = complex_real (ret, real); | |
2283 return complex_imag (ret, imag); | |
2284 } | |
2285 | |
2286 void | |
2287 jit_typeinfo::create_int (size_t nbits) | |
2288 { | |
2289 std::stringstream tname; | |
2290 tname << "int" << nbits; | |
2291 ints[nbits] = new_type (tname.str (), any, llvm::Type::getIntNTy (context, | |
2292 nbits)); | |
2293 } | |
2294 | |
2295 jit_type * | |
2296 jit_typeinfo::intN (size_t nbits) const | |
2297 { | |
2298 std::map<size_t, jit_type *>::const_iterator iter = ints.find (nbits); | |
2299 if (iter != ints.end ()) | |
2300 return iter->second; | |
2301 | |
2302 throw jit_fail_exception ("No such integer type"); | |
2303 } | |
2304 | |
2305 jit_type * | |
2306 jit_typeinfo::do_type_of (const octave_value &ov) const | |
2307 { | |
2308 if (ov.is_function ()) | |
2309 { | |
2310 // FIXME: This is ugly, we need to finalize how we want to to this, then | |
2311 // have octave_value fully support the needed functionality | |
2312 octave_builtin *builtin | |
2313 = dynamic_cast<octave_builtin *> (ov.internal_rep ()); | |
2314 return builtin && builtin->to_jit () ? builtin->to_jit () | |
2315 : unknown_function; | |
2316 } | |
2317 | |
2318 if (ov.is_range ()) | |
2319 return get_range (); | |
2320 | |
15311
de9bfcf637df
Fix error when compiling with complex matrix (bug #37247)
Max Brister <max@2bass.com>
parents:
15219
diff
changeset
|
2321 if (ov.is_double_type () && ! ov.is_complex_type ()) |
15016 | 2322 { |
2323 if (ov.is_real_scalar ()) | |
2324 return get_scalar (); | |
2325 | |
2326 if (ov.is_matrix_type ()) | |
2327 return get_matrix (); | |
2328 } | |
2329 | |
2330 if (ov.is_complex_scalar ()) | |
15583
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
2331 { |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
2332 Complex cv = ov.complex_value (); |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
2333 |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
2334 // 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
|
2335 // 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
|
2336 if (cv.imag () == 0) |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
2337 return get_complex (); |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
2338 } |
15016 | 2339 |
2340 return get_any (); | |
2341 } | |
2342 | |
2343 #endif |