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