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