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