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