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