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