Mercurial > octave-nkf
annotate libinterp/corefcn/jit-typeinfo.h @ 20654:b65888ec820e draft default tip gccjit
dmalcom gcc jit import
author | Stefan Mahr <dac922@gmx.de> |
---|---|
date | Fri, 27 Feb 2015 16:59:36 +0100 |
parents | d35201e5ce5d |
children |
rev | line source |
---|---|
15016 | 1 /* |
2 | |
19731
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
18222
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 #if !defined (octave_jit_typeinfo_h) |
26 #define octave_jit_typeinfo_h 1 | |
27 | |
20654 | 28 #ifdef HAVE_JIT |
15016 | 29 |
30 #include <map> | |
31 #include <vector> | |
32 | |
33 #include "Range.h" | |
34 #include "jit-util.h" | |
35 | |
36 // Defines the type system used by jit and a singleton class, jit_typeinfo, to | |
37 // manage the types. | |
38 // | |
39 // FIXME: | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
40 // Operations are defined and implemented in jit_typeinfo. Eventually they |
15016 | 41 // should be moved elsewhere. (just like with octave_typeinfo) |
42 | |
20132
1f9ed81bd173
maint: Fix spelling and grammar mistakes in docs and comments (bug #44878)
Rafael Laboissiere <rafael@laboissiere.net>
parents:
19731
diff
changeset
|
43 // jit_range is compatible with the llvm range structure |
15016 | 44 struct |
45 jit_range | |
46 { | |
47 jit_range (const Range& from) : base (from.base ()), limit (from.limit ()), | |
20649
d35201e5ce5d
Fix compilation of jit broken by dd6345fd8a97 (bug #46191).
Rik <rik@octave.org>
parents:
20264
diff
changeset
|
48 inc (from.inc ()), nelem (from.numel ()) |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
49 { } |
15016 | 50 |
51 operator Range () const | |
52 { | |
53 return Range (base, limit, inc); | |
54 } | |
55 | |
56 bool all_elements_are_ints () const; | |
57 | |
58 double base; | |
59 double limit; | |
60 double inc; | |
61 octave_idx_type nelem; | |
62 }; | |
63 | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
64 std::ostream& operator << (std::ostream& os, const jit_range& rng); |
15016 | 65 |
20132
1f9ed81bd173
maint: Fix spelling and grammar mistakes in docs and comments (bug #44878)
Rafael Laboissiere <rafael@laboissiere.net>
parents:
19731
diff
changeset
|
66 // jit_array is compatible with the llvm array/matrix structures |
15016 | 67 template <typename T, typename U> |
68 struct | |
69 jit_array | |
70 { | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
71 jit_array () : array (0) { } |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
72 |
15016 | 73 jit_array (T& from) : array (new T (from)) |
74 { | |
75 update (); | |
76 } | |
77 | |
78 void update (void) | |
79 { | |
80 ref_count = array->jit_ref_count (); | |
81 slice_data = array->jit_slice_data () - 1; | |
20264
5dfaaaae784f
Deprecate Array::capacity() and Sparse::capacity() for numel() and nzmax().
Carnë Draug <carandraug@octave.org>
parents:
20132
diff
changeset
|
82 slice_len = array->numel (); |
15016 | 83 dimensions = array->jit_dimensions (); |
84 } | |
85 | |
86 void update (T *aarray) | |
87 { | |
88 array = aarray; | |
89 update (); | |
90 } | |
91 | |
92 operator T () const | |
93 { | |
94 return *array; | |
95 } | |
96 | |
97 int *ref_count; | |
98 | |
99 U *slice_data; | |
100 octave_idx_type slice_len; | |
101 octave_idx_type *dimensions; | |
102 | |
103 T *array; | |
104 }; | |
105 | |
106 typedef jit_array<NDArray, double> jit_matrix; | |
107 | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
108 std::ostream& operator << (std::ostream& os, const jit_matrix& mat); |
15016 | 109 |
110 // calling convention | |
111 namespace | |
112 jit_convention | |
113 { | |
114 enum | |
115 type | |
116 { | |
117 // internal to jit | |
118 internal, | |
119 | |
120 // an external C call | |
121 external, | |
122 | |
123 length | |
124 }; | |
125 } | |
126 | |
127 // Used to keep track of estimated (infered) types during JIT. This is a | |
128 // hierarchical type system which includes both concrete and abstract types. | |
129 // | |
130 // The types form a lattice. Currently we only allow for one parent type, but | |
131 // eventually we may allow for multiple predecessors. | |
132 class | |
133 jit_type | |
134 { | |
135 public: | |
20654 | 136 #ifdef HAVE_LLVM |
15016 | 137 typedef llvm::Value *(*convert_fn) (llvm::IRBuilderD&, llvm::Value *); |
20654 | 138 #endif |
15016 | 139 |
20654 | 140 jit_type (const std::string& aname, jit_type *aparent |
141 #ifdef HAVE_LLVM | |
142 , llvm::Type *allvm_type | |
143 #endif | |
144 #ifdef HAVE_GCCJIT | |
145 , gccjit::type agcc_type | |
146 #endif | |
147 , bool askip_paren, int aid); | |
15016 | 148 |
149 // a user readable type name | |
150 const std::string& name (void) const { return mname; } | |
151 | |
152 // a unique id for the type | |
153 int type_id (void) const { return mid; } | |
154 | |
155 // An abstract base type, may be null | |
156 jit_type *parent (void) const { return mparent; } | |
157 | |
20654 | 158 #ifdef HAVE_LLVM |
15016 | 159 // convert to an llvm type |
160 llvm::Type *to_llvm (void) const { return llvm_type; } | |
161 | |
162 // how this type gets passed as a function argument | |
163 llvm::Type *to_llvm_arg (void) const; | |
20654 | 164 #endif |
165 | |
166 #ifdef HAVE_GCCJIT | |
167 gccjit::type to_gccjit (void) const { return gccjit_type; } | |
168 #endif | |
15016 | 169 |
170 size_t depth (void) const { return mdepth; } | |
171 | |
15124
0464e3ceb85b
Skip functions when resolving end context in JIT
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
172 bool skip_paren (void) const { return mskip_paren; } |
0464e3ceb85b
Skip functions when resolving end context in JIT
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
173 |
15016 | 174 // -------------------- Calling Convention information -------------------- |
175 | |
176 // A function declared like: mytype foo (int arg0, int arg1); | |
177 // Will be converted to: void foo (mytype *retval, int arg0, int arg1) | |
178 // if mytype is sret. The caller is responsible for allocating space for | |
179 // retval. (on the stack) | |
180 bool sret (jit_convention::type cc) const { return msret[cc]; } | |
181 | |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
182 void mark_sret (jit_convention::type cc) |
15016 | 183 { msret[cc] = true; } |
184 | |
185 // A function like: void foo (mytype arg0) | |
186 // Will be converted to: void foo (mytype *arg0) | |
187 // Basically just pass by reference. | |
188 bool pointer_arg (jit_convention::type cc) const { return mpointer_arg[cc]; } | |
189 | |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
190 void mark_pointer_arg (jit_convention::type cc) |
15016 | 191 { mpointer_arg[cc] = true; } |
192 | |
193 // Convert into an equivalent form before calling. For example, complex is | |
194 // represented as two values llvm vector, but we need to pass it as a two | |
195 // valued llvm structure to C functions. | |
20654 | 196 #ifdef HAVE_LLVM |
15016 | 197 convert_fn pack (jit_convention::type cc) { return mpack[cc]; } |
198 | |
199 void set_pack (jit_convention::type cc, convert_fn fn) { mpack[cc] = fn; } | |
200 | |
201 // The inverse operation of pack. | |
202 convert_fn unpack (jit_convention::type cc) { return munpack[cc]; } | |
203 | |
204 void set_unpack (jit_convention::type cc, convert_fn fn) | |
205 { munpack[cc] = fn; } | |
206 | |
207 // The resulting type after pack is called. | |
208 llvm::Type *packed_type (jit_convention::type cc) | |
209 { return mpacked_type[cc]; } | |
210 | |
211 void set_packed_type (jit_convention::type cc, llvm::Type *ty) | |
212 { mpacked_type[cc] = ty; } | |
20654 | 213 #endif |
214 | |
15016 | 215 private: |
216 std::string mname; | |
217 jit_type *mparent; | |
20654 | 218 #ifdef HAVE_LLVM |
15016 | 219 llvm::Type *llvm_type; |
20654 | 220 #endif |
221 #ifdef HAVE_GCCJIT | |
222 gccjit::type gccjit_type; | |
223 #endif | |
15016 | 224 int mid; |
225 size_t mdepth; | |
15124
0464e3ceb85b
Skip functions when resolving end context in JIT
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
226 bool mskip_paren; |
15016 | 227 |
228 bool msret[jit_convention::length]; | |
229 bool mpointer_arg[jit_convention::length]; | |
230 | |
20654 | 231 #ifdef HAVE_LLVM |
15016 | 232 convert_fn mpack[jit_convention::length]; |
233 convert_fn munpack[jit_convention::length]; | |
234 | |
235 llvm::Type *mpacked_type[jit_convention::length]; | |
20654 | 236 #endif |
15016 | 237 }; |
238 // seperate print function to allow easy printing if type is null | |
239 std::ostream& jit_print (std::ostream& os, jit_type *atype); | |
240 | |
241 class jit_value; | |
242 | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
243 // An abstraction for calling llvm functions with jit_values. Deals with |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
244 // calling convention details. |
15016 | 245 class |
246 jit_function | |
247 { | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
248 friend std::ostream& operator << (std::ostream& os, const jit_function& fn); |
15016 | 249 public: |
250 // create a function in an invalid state | |
251 jit_function (); | |
252 | |
20654 | 253 jit_function ( |
254 #ifdef HAVE_LLVM | |
255 llvm::Module *amodule, | |
256 #endif | |
257 #ifdef HAVE_GCCJIT | |
258 gccjit::context gccjit_ctxt, | |
259 #endif | |
260 jit_convention::type acall_conv, | |
261 std::string aname, | |
262 jit_type *aresult, | |
15016 | 263 const std::vector<jit_type *>& aargs); |
264 | |
265 // Use an existing function, but change the argument types. The new argument | |
266 // types must behave the same for the current calling convention. | |
267 jit_function (const jit_function& fn, jit_type *aresult, | |
268 const std::vector<jit_type *>& aargs); | |
269 | |
270 jit_function (const jit_function& fn); | |
271 | |
15385
8ccb187b24e9
Erase partially created functions on JIT compilation failure (bug #37308)
Max Brister <max@2bass.com>
parents:
15370
diff
changeset
|
272 // erase the interal LLVM function (if it exists). Will become invalid. |
8ccb187b24e9
Erase partially created functions on JIT compilation failure (bug #37308)
Max Brister <max@2bass.com>
parents:
15370
diff
changeset
|
273 void erase (void); |
8ccb187b24e9
Erase partially created functions on JIT compilation failure (bug #37308)
Max Brister <max@2bass.com>
parents:
15370
diff
changeset
|
274 |
20654 | 275 #ifdef HAVE_LLVM |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
276 template <typename T> |
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
277 void add_mapping (llvm::ExecutionEngine *engine, T fn) |
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
278 { |
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
279 do_add_mapping (engine, reinterpret_cast<void *> (fn)); |
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
280 } |
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
281 |
15016 | 282 bool valid (void) const { return llvm_function; } |
20654 | 283 #else |
284 bool valid (void) const; | |
285 #endif | |
15016 | 286 |
287 std::string name (void) const; | |
288 | |
20654 | 289 #ifdef HAVE_LLVM |
15016 | 290 llvm::BasicBlock *new_block (const std::string& aname = "body", |
291 llvm::BasicBlock *insert_before = 0); | |
292 | |
293 llvm::Value *call (llvm::IRBuilderD& builder, | |
294 const std::vector<jit_value *>& in_args) const; | |
295 | |
296 llvm::Value *call (llvm::IRBuilderD& builder, | |
297 const std::vector<llvm::Value *>& in_args | |
298 = std::vector<llvm::Value *> ()) const; | |
299 | |
300 #define JIT_PARAM_ARGS llvm::IRBuilderD& builder, | |
301 #define JIT_PARAMS builder, | |
302 #define JIT_CALL(N) JIT_EXPAND (llvm::Value *, call, llvm::Value *, const, N) | |
303 | |
304 JIT_CALL (1) | |
305 JIT_CALL (2) | |
306 JIT_CALL (3) | |
307 JIT_CALL (4) | |
308 JIT_CALL (5) | |
309 | |
310 #undef JIT_CALL | |
311 | |
312 #define JIT_CALL(N) JIT_EXPAND (llvm::Value *, call, jit_value *, const, N) | |
313 | |
314 JIT_CALL (1); | |
315 JIT_CALL (2); | |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
316 JIT_CALL (3); |
15016 | 317 |
318 #undef JIT_CALL | |
319 #undef JIT_PARAMS | |
320 #undef JIT_PARAM_ARGS | |
321 | |
322 llvm::Value *argument (llvm::IRBuilderD& builder, size_t idx) const; | |
323 | |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
324 void do_return (llvm::IRBuilderD& builder, llvm::Value *rval = 0, |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
325 bool verify = true); |
15016 | 326 |
327 llvm::Function *to_llvm (void) const { return llvm_function; } | |
20654 | 328 #endif |
329 | |
330 #ifdef HAVE_GCCJIT | |
331 gccjit::rvalue call (gccjit::context ctxt, | |
332 gccjit::block block, | |
333 const std::vector<jit_value *>& in_args) const; | |
334 gccjit::rvalue call (gccjit::context ctxt, | |
335 gccjit::block block, | |
336 std::vector<gccjit::rvalue>& in_args) const; | |
337 gccjit::lvalue argument (size_t idx) const; | |
338 #endif | |
15016 | 339 |
340 // If true, then the return value is passed as a pointer in the first argument | |
341 bool sret (void) const { return mresult && mresult->sret (call_conv); } | |
342 | |
343 bool can_error (void) const { return mcan_error; } | |
344 | |
345 void mark_can_error (void) { mcan_error = true; } | |
346 | |
347 jit_type *result (void) const { return mresult; } | |
348 | |
349 jit_type *argument_type (size_t idx) const | |
350 { | |
351 assert (idx < args.size ()); | |
352 return args[idx]; | |
353 } | |
354 | |
355 const std::vector<jit_type *>& arguments (void) const { return args; } | |
356 private: | |
20654 | 357 #ifdef HAVE_LLVM |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
358 void 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
|
359 |
15016 | 360 llvm::Module *module; |
361 llvm::Function *llvm_function; | |
20654 | 362 #endif |
363 #ifdef HAVE_GCCJIT | |
364 public: | |
365 gccjit::function gccjit_function; | |
366 private: | |
367 #endif | |
15016 | 368 jit_type *mresult; |
369 std::vector<jit_type *> args; | |
370 jit_convention::type call_conv; | |
371 bool mcan_error; | |
372 }; | |
373 | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
374 std::ostream& operator << (std::ostream& os, const jit_function& fn); |
15016 | 375 |
376 | |
377 // Keeps track of information about how to implement operations (+, -, *, ect) | |
378 // and their resulting types. | |
379 class | |
380 jit_operation | |
381 { | |
382 public: | |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
383 // type signature vector |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
384 typedef std::vector<jit_type *> signature_vec; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
385 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
386 virtual ~jit_operation (void); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
387 |
15016 | 388 void add_overload (const jit_function& func) |
389 { | |
390 add_overload (func, func.arguments ()); | |
391 } | |
392 | |
393 void add_overload (const jit_function& func, | |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
394 const signature_vec& args); |
15016 | 395 |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
396 const jit_function& overload (const signature_vec& types) const; |
15016 | 397 |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
398 jit_type *result (const signature_vec& types) const |
15016 | 399 { |
400 const jit_function& temp = overload (types); | |
401 return temp.result (); | |
402 } | |
403 | |
404 #define JIT_PARAMS | |
405 #define JIT_PARAM_ARGS | |
406 #define JIT_OVERLOAD(N) \ | |
407 JIT_EXPAND (const jit_function&, overload, jit_type *, const, N) \ | |
408 JIT_EXPAND (jit_type *, result, jit_type *, const, N) | |
409 | |
410 JIT_OVERLOAD (1); | |
411 JIT_OVERLOAD (2); | |
412 JIT_OVERLOAD (3); | |
413 | |
414 #undef JIT_PARAMS | |
415 #undef JIT_PARAM_ARGS | |
416 | |
417 const std::string& name (void) const { return mname; } | |
418 | |
419 void stash_name (const std::string& aname) { mname = aname; } | |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
420 protected: |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
421 virtual jit_function *generate (const signature_vec& types) const; |
15016 | 422 private: |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
423 Array<octave_idx_type> to_idx (const signature_vec& types) const; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
424 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
425 const jit_function& 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
|
426 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
427 struct signature_cmp |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
428 { |
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
|
429 bool 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
|
430 }; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
431 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
432 typedef std::map<const signature_vec *, jit_function *, signature_cmp> |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
433 generated_map; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
434 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
435 mutable generated_map generated; |
15016 | 436 |
437 std::vector<Array<jit_function> > overloads; | |
438 | |
439 std::string mname; | |
440 }; | |
441 | |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
442 class |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
443 jit_index_operation : public jit_operation |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
444 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
445 public: |
20654 | 446 jit_index_operation (void) {} //: module (0), engine (0) { } |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
447 |
20654 | 448 #ifdef HAVE_LLVM |
449 void initialize (llvm::Module *amodule, llvm::ExecutionEngine *aengine | |
450 #ifdef HAVE_GCCJIT | |
451 , gccjit::context agccjit_ctxt | |
452 #endif | |
453 ) | |
454 | |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
455 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
456 module = amodule; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
457 engine = aengine; |
20654 | 458 #ifdef HAVE_GCCJIT |
459 gccjit_ctxt = agccjit_ctxt; | |
460 #endif | |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
461 do_initialize (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
462 } |
20654 | 463 #endif |
464 | |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
465 protected: |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
466 virtual jit_function *generate (const signature_vec& types) const; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
467 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
468 virtual jit_function *generate_matrix (const signature_vec& types) const = 0; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
469 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
470 virtual void do_initialize (void) = 0; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
471 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
472 // helper functions |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
473 // [start_idx, end_idx). |
20654 | 474 #ifdef HAVE_LLVM |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
475 llvm::Value *create_arg_array (llvm::IRBuilderD& builder, |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
476 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
|
477 size_t end_idx) const; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
478 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
479 llvm::Module *module; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
480 llvm::ExecutionEngine *engine; |
20654 | 481 #endif |
482 #ifdef HAVE_GCCJIT | |
483 gccjit::rvalue create_arg_array (const jit_function &fn, | |
484 gccjit::block block, | |
485 size_t start_idx, | |
486 size_t end_idx) const; | |
487 | |
488 gccjit::context gccjit_ctxt; | |
489 #endif | |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
490 }; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
491 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
492 class |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
493 jit_paren_subsref : public jit_index_operation |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
494 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
495 protected: |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
496 virtual jit_function *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
|
497 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
498 virtual void do_initialize (void); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
499 private: |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
500 jit_function paren_scalar; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
501 }; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
502 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
503 class |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
504 jit_paren_subsasgn : public jit_index_operation |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
505 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
506 protected: |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
507 jit_function *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
|
508 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
509 virtual void do_initialize (void); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
510 private: |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
511 jit_function paren_scalar; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
512 }; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
513 |
15016 | 514 // A singleton class which handles the construction of jit_types and |
515 // jit_operations. | |
516 class | |
517 jit_typeinfo | |
518 { | |
519 public: | |
20654 | 520 static void initialize ( |
521 #ifdef HAVE_LLVM | |
522 llvm::Module *m, llvm::ExecutionEngine *e | |
523 #endif | |
524 ); | |
15016 | 525 |
526 static jit_type *join (jit_type *lhs, jit_type *rhs) | |
527 { | |
528 return instance->do_join (lhs, rhs); | |
529 } | |
530 | |
531 static jit_type *get_any (void) { return instance->any; } | |
532 | |
533 static jit_type *get_matrix (void) { return instance->matrix; } | |
534 | |
535 static jit_type *get_scalar (void) { return instance->scalar; } | |
20654 | 536 #ifdef HAVE_LLVM |
15016 | 537 static llvm::Type *get_scalar_llvm (void) |
538 { return instance->scalar->to_llvm (); } | |
20654 | 539 #endif |
540 #ifdef HAVE_GCCJIT | |
541 static gccjit::type get_scalar_gccjit (void) | |
542 { return instance->scalar->to_gccjit (); } | |
543 #endif | |
15016 | 544 |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
545 static jit_type *get_scalar_ptr (void) { return instance->scalar_ptr; } |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
546 |
15337
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
547 static jit_type *get_any_ptr (void) { return instance->any_ptr; } |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
548 |
15016 | 549 static jit_type *get_range (void) { return instance->range; } |
550 | |
551 static jit_type *get_string (void) { return instance->string; } | |
552 | |
553 static jit_type *get_bool (void) { return instance->boolean; } | |
554 | |
555 static jit_type *get_index (void) { return instance->index; } | |
556 | |
20654 | 557 #ifdef HAVE_LLVM |
15016 | 558 static llvm::Type *get_index_llvm (void) |
559 { return instance->index->to_llvm (); } | |
20654 | 560 #endif |
15016 | 561 |
562 static jit_type *get_complex (void) { return instance->complex; } | |
563 | |
564 // Get the jit_type of an octave_value | |
565 static jit_type *type_of (const octave_value& ov) | |
566 { | |
567 return instance->do_type_of (ov); | |
568 } | |
569 | |
570 static const jit_operation& binary_op (int op) | |
571 { | |
572 return instance->do_binary_op (op); | |
573 } | |
574 | |
15146
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15135
diff
changeset
|
575 static const jit_operation& unary_op (int op) |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15135
diff
changeset
|
576 { |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15135
diff
changeset
|
577 return instance->do_unary_op (op); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15135
diff
changeset
|
578 } |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15135
diff
changeset
|
579 |
15016 | 580 static const jit_operation& grab (void) { return instance->grab_fn; } |
581 | |
582 static const jit_function& get_grab (jit_type *type) | |
583 { | |
584 return instance->grab_fn.overload (type); | |
585 } | |
586 | |
587 static const jit_operation& release (void) | |
588 { | |
589 return instance->release_fn; | |
590 } | |
591 | |
592 static const jit_function& get_release (jit_type *type) | |
593 { | |
594 return instance->release_fn.overload (type); | |
595 } | |
596 | |
15337
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
597 static const jit_operation& destroy (void) |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
598 { |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
599 return instance->destroy_fn; |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
600 } |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
601 |
15016 | 602 static const jit_operation& print_value (void) |
603 { | |
604 return instance->print_fn; | |
605 } | |
606 | |
607 static const jit_operation& for_init (void) | |
608 { | |
609 return instance->for_init_fn; | |
610 } | |
611 | |
612 static const jit_operation& for_check (void) | |
613 { | |
614 return instance->for_check_fn; | |
615 } | |
616 | |
617 static const jit_operation& for_index (void) | |
618 { | |
619 return instance->for_index_fn; | |
620 } | |
621 | |
622 static const jit_operation& make_range (void) | |
623 { | |
624 return instance->make_range_fn; | |
625 } | |
626 | |
627 static const jit_operation& paren_subsref (void) | |
628 { | |
629 return instance->paren_subsref_fn; | |
630 } | |
631 | |
632 static const jit_operation& paren_subsasgn (void) | |
633 { | |
634 return instance->paren_subsasgn_fn; | |
635 } | |
636 | |
637 static const jit_operation& logically_true (void) | |
638 { | |
639 return instance->logically_true_fn; | |
640 } | |
641 | |
642 static const jit_operation& cast (jit_type *result) | |
643 { | |
644 return instance->do_cast (result); | |
645 } | |
646 | |
647 static const jit_function& cast (jit_type *to, jit_type *from) | |
648 { | |
649 return instance->do_cast (to, from); | |
650 } | |
651 | |
20654 | 652 #ifdef HAVE_LLVM |
15016 | 653 static llvm::Value *insert_error_check (llvm::IRBuilderD& bld) |
654 { | |
655 return instance->do_insert_error_check (bld); | |
656 } | |
15056
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
657 |
15603 | 658 static llvm::Value *insert_interrupt_check (llvm::IRBuilderD& bld) |
659 { | |
660 return instance->do_insert_interrupt_check (bld); | |
661 } | |
20654 | 662 #endif |
663 #ifdef HAVE_GCCJIT | |
664 static gccjit::rvalue insert_error_check (gccjit::function func) | |
665 { | |
666 return instance->do_insert_error_check (func); | |
667 } | |
668 | |
669 static gccjit::rvalue insert_interrupt_check (gccjit::function func) | |
670 { | |
671 return instance->do_insert_interrupt_check (func); | |
672 } | |
673 #endif | |
15603 | 674 |
15056
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
675 static const jit_operation& end (void) |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
676 { |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
677 return instance->end_fn; |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
678 } |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
679 |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
680 static const jit_function& end (jit_value *value, jit_value *index, |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
681 jit_value *count) |
15056
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
682 { |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
683 return instance->do_end (value, index, count); |
15056
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
684 } |
15337
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
685 |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
686 static const jit_operation& create_undef (void) |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
687 { |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
688 return instance->create_undef_fn; |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
689 } |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
690 |
20654 | 691 #ifdef HAVE_LLVM |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
692 static llvm::Value *create_complex (llvm::Value *real, llvm::Value *imag) |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
693 { |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
694 return instance->complex_new (real, imag); |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
695 } |
20654 | 696 #endif |
697 | |
698 #ifdef HAVE_GCCJIT | |
699 static gccjit::context create_gccjit_child_context () | |
700 { | |
701 return instance->gccjit_ctxt.new_child_context (); | |
702 } | |
703 #endif | |
704 | |
705 | |
15016 | 706 private: |
20654 | 707 #ifdef HAVE_LLVM |
15016 | 708 jit_typeinfo (llvm::Module *m, llvm::ExecutionEngine *e); |
20654 | 709 #endif |
15016 | 710 |
711 // FIXME: Do these methods really need to be in jit_typeinfo? | |
712 jit_type *do_join (jit_type *lhs, jit_type *rhs) | |
713 { | |
714 // empty case | |
715 if (! lhs) | |
716 return rhs; | |
717 | |
718 if (! rhs) | |
719 return lhs; | |
720 | |
721 // check for a shared parent | |
722 while (lhs != rhs) | |
723 { | |
724 if (lhs->depth () > rhs->depth ()) | |
725 lhs = lhs->parent (); | |
726 else if (lhs->depth () < rhs->depth ()) | |
727 rhs = rhs->parent (); | |
728 else | |
729 { | |
730 // we MUST have depth > 0 as any is the base type of everything | |
731 do | |
732 { | |
733 lhs = lhs->parent (); | |
734 rhs = rhs->parent (); | |
735 } | |
736 while (lhs != rhs); | |
737 } | |
738 } | |
739 | |
740 return lhs; | |
741 } | |
742 | |
743 jit_type *do_difference (jit_type *lhs, jit_type *) | |
744 { | |
745 // FIXME: Maybe we can do something smarter? | |
746 return lhs; | |
747 } | |
748 | |
749 jit_type *do_type_of (const octave_value &ov) const; | |
750 | |
751 const jit_operation& do_binary_op (int op) const | |
752 { | |
753 assert (static_cast<size_t>(op) < binary_ops.size ()); | |
754 return binary_ops[op]; | |
755 } | |
756 | |
15146
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15135
diff
changeset
|
757 const jit_operation& do_unary_op (int op) const |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15135
diff
changeset
|
758 { |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15135
diff
changeset
|
759 assert (static_cast<size_t> (op) < unary_ops.size ()); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15135
diff
changeset
|
760 return unary_ops[op]; |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15135
diff
changeset
|
761 } |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15135
diff
changeset
|
762 |
15016 | 763 const jit_operation& do_cast (jit_type *to) |
764 { | |
765 static jit_operation null_function; | |
766 if (! to) | |
767 return null_function; | |
768 | |
769 size_t id = to->type_id (); | |
770 if (id >= casts.size ()) | |
771 return null_function; | |
772 return casts[id]; | |
773 } | |
774 | |
775 const jit_function& do_cast (jit_type *to, jit_type *from) | |
776 { | |
777 return do_cast (to).overload (from); | |
778 } | |
779 | |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
780 const jit_function& do_end (jit_value *value, jit_value *index, |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
781 jit_value *count); |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
782 |
20654 | 783 jit_type *new_type (const std::string& name, jit_type *parent |
784 #ifdef HAVE_LLVM | |
785 , llvm::Type *llvm_type | |
786 #endif | |
787 #ifdef HAVE_GCCJIT | |
788 , gccjit::type gccjit_type | |
789 #endif | |
790 , bool skip_paren = false); | |
15016 | 791 |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
792 void add_print (jit_type *ty, void *fptr); |
15016 | 793 |
20654 | 794 void add_binary_op (jit_type *ty, int op |
795 , int llvm_op | |
796 #ifdef HAVE_GCCJIT | |
797 , enum gcc_jit_binary_op gccjit_op | |
798 #endif | |
799 ); | |
15016 | 800 |
20654 | 801 void add_binary_icmp (jit_type *ty, int op |
802 , int llvm_op | |
803 #ifdef HAVE_GCCJIT | |
804 , enum gcc_jit_comparison gccjit_op | |
805 #endif | |
806 ); | |
15016 | 807 |
20654 | 808 void add_binary_fcmp (jit_type *ty, int op |
809 , int llvm_op | |
810 #ifdef HAVE_GCCJIT | |
811 , enum gcc_jit_comparison gccjit_op | |
812 #endif | |
813 ); | |
15016 | 814 |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
815 // create a function with an external calling convention |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
816 // forces the function pointer to be specified |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
817 template <typename T> |
20654 | 818 jit_function create_external ( |
819 #ifdef HAVE_LLVM | |
820 llvm::ExecutionEngine *ee, | |
821 #endif | |
822 T fn, | |
823 std::string name, jit_type *ret, | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
824 const std::vector<jit_type *>& args |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
825 = std::vector<jit_type *> ()) |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
826 { |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
827 jit_function retval = create_function (jit_convention::external, name, ret, |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
828 args); |
20654 | 829 #ifdef HAVE_LLVM |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
830 retval.add_mapping (ee, fn); |
20654 | 831 #endif |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
832 return retval; |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
833 } |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
834 |
20654 | 835 #ifdef HAVE_LLVM |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
836 #define JIT_PARAM_ARGS llvm::ExecutionEngine *ee, T fn, \ |
20654 | 837 std::string name, jit_type *ret, |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
838 #define JIT_PARAMS ee, fn, name, ret, |
20654 | 839 #else |
840 #define JIT_PARAM_ARGS T fn, \ | |
841 std::string name, jit_type *ret, | |
842 #define JIT_PARAMS fn, name, ret, | |
843 #endif | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
844 #define CREATE_FUNCTION(N) JIT_EXPAND(template <typename T> jit_function, \ |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
845 create_external, \ |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
846 jit_type *, /* empty */, N) |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
847 |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
848 CREATE_FUNCTION(1); |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
849 CREATE_FUNCTION(2); |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
850 CREATE_FUNCTION(3); |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
851 CREATE_FUNCTION(4); |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
852 |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
853 #undef JIT_PARAM_ARGS |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
854 #undef JIT_PARAMS |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
855 #undef CREATE_FUNCTION |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
856 |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
857 // use create_external or create_internal directly |
15016 | 858 jit_function create_function (jit_convention::type cc, |
20654 | 859 std::string name, jit_type *ret, |
15016 | 860 const std::vector<jit_type *>& args |
861 = std::vector<jit_type *> ()); | |
862 | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
863 // create an internal calling convention (a function defined in llvm) |
20654 | 864 jit_function create_internal (std::string name, jit_type *ret, |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
865 const std::vector<jit_type *>& args |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
866 = std::vector<jit_type *> ()) |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
867 { |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
868 return create_function (jit_convention::internal, name, ret, args); |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
869 } |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
870 |
20654 | 871 #define JIT_PARAM_ARGS std::string name, jit_type *ret, |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
872 #define JIT_PARAMS name, ret, |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
873 #define CREATE_FUNCTION(N) JIT_EXPAND(jit_function, create_internal, \ |
15016 | 874 jit_type *, /* empty */, N) |
875 | |
876 CREATE_FUNCTION(1); | |
877 CREATE_FUNCTION(2); | |
878 CREATE_FUNCTION(3); | |
879 CREATE_FUNCTION(4); | |
880 | |
881 #undef JIT_PARAM_ARGS | |
882 #undef JIT_PARAMS | |
883 #undef CREATE_FUNCTION | |
884 | |
885 jit_function create_identity (jit_type *type); | |
886 | |
20654 | 887 #ifdef HAVE_LLVM |
15016 | 888 llvm::Value *do_insert_error_check (llvm::IRBuilderD& bld); |
889 | |
15603 | 890 llvm::Value *do_insert_interrupt_check (llvm::IRBuilderD& bld); |
20654 | 891 #endif |
892 | |
893 #ifdef HAVE_GCCJIT | |
894 gccjit::rvalue do_insert_error_check (gccjit::function func); | |
895 | |
896 gccjit::rvalue do_insert_interrupt_check (gccjit::function func); | |
897 #endif | |
15603 | 898 |
15016 | 899 void add_builtin (const std::string& name); |
900 | |
901 void register_intrinsic (const std::string& name, size_t id, | |
902 jit_type *result, jit_type *arg0) | |
903 { | |
904 std::vector<jit_type *> args (1, arg0); | |
905 register_intrinsic (name, id, result, args); | |
906 } | |
907 | |
908 void register_intrinsic (const std::string& name, size_t id, jit_type *result, | |
909 const std::vector<jit_type *>& args); | |
910 | |
911 void register_generic (const std::string& name, jit_type *result, | |
912 jit_type *arg0) | |
913 { | |
914 std::vector<jit_type *> args (1, arg0); | |
915 register_generic (name, result, args); | |
916 } | |
917 | |
918 void register_generic (const std::string& name, jit_type *result, | |
919 const std::vector<jit_type *>& args); | |
920 | |
921 octave_builtin *find_builtin (const std::string& name); | |
922 | |
923 jit_function mirror_binary (const jit_function& fn); | |
924 | |
20654 | 925 #ifdef HAVE_LLVM |
15016 | 926 llvm::Function *wrap_complex (llvm::Function *wrap); |
927 | |
928 static llvm::Value *pack_complex (llvm::IRBuilderD& bld, | |
929 llvm::Value *cplx); | |
930 | |
931 static llvm::Value *unpack_complex (llvm::IRBuilderD& bld, | |
932 llvm::Value *result); | |
933 | |
934 llvm::Value *complex_real (llvm::Value *cx); | |
935 | |
936 llvm::Value *complex_real (llvm::Value *cx, llvm::Value *real); | |
937 | |
938 llvm::Value *complex_imag (llvm::Value *cx); | |
939 | |
940 llvm::Value *complex_imag (llvm::Value *cx, llvm::Value *imag); | |
941 | |
942 llvm::Value *complex_new (llvm::Value *real, llvm::Value *imag); | |
20654 | 943 #endif |
944 | |
945 #ifdef HAVE_GCCJIT | |
946 gccjit::rvalue complex_real (gccjit::rvalue cx); | |
947 | |
948 gccjit::rvalue complex_real (gccjit::block block, | |
949 gccjit::lvalue cx, | |
950 gccjit::rvalue real); | |
951 | |
952 gccjit::rvalue complex_imag (gccjit::rvalue cx); | |
953 | |
954 gccjit::rvalue complex_imag (gccjit::block block, | |
955 gccjit::lvalue cx, | |
956 gccjit::rvalue imag); | |
957 | |
958 gccjit::rvalue complex_new (gccjit::block block, | |
959 gccjit::rvalue real, | |
960 gccjit::rvalue imag); | |
961 #endif | |
15016 | 962 |
963 void create_int (size_t nbits); | |
964 | |
965 jit_type *intN (size_t nbits) const; | |
966 | |
967 static jit_typeinfo *instance; | |
968 | |
20654 | 969 #ifdef HAVE_LLVM |
15016 | 970 llvm::Module *module; |
971 llvm::ExecutionEngine *engine; | |
20654 | 972 #endif |
15016 | 973 int next_id; |
974 | |
20654 | 975 #ifdef HAVE_LLVM |
15016 | 976 llvm::GlobalVariable *lerror_state; |
15603 | 977 llvm::GlobalVariable *loctave_interrupt_state; |
978 | |
979 llvm::Type *sig_atomic_type; | |
20654 | 980 #endif |
981 | |
982 #ifdef HAVE_GCCJIT | |
983 gccjit::rvalue error_state_gccjit; | |
984 gccjit::rvalue octave_interrupt_state_gccjit; | |
985 gccjit::type sig_atomic_type_gccjit; | |
986 #endif | |
15016 | 987 |
988 std::vector<jit_type*> id_to_type; | |
989 jit_type *any; | |
990 jit_type *matrix; | |
20654 | 991 #ifdef HAVE_GCCJIT |
992 gccjit::field field_ref_count; | |
993 gccjit::field field_slice_data; | |
994 gccjit::field field_slice_len; | |
995 gccjit::field field_dimensions; | |
996 gccjit::field field_array; | |
997 #endif | |
15016 | 998 jit_type *scalar; |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
999 jit_type *scalar_ptr; // a fake type for interfacing with C++ |
15135
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
1000 jit_type *any_ptr; // a fake type for interfacing with C++ |
15016 | 1001 jit_type *range; |
20654 | 1002 #ifdef HAVE_GCCJIT |
1003 gccjit::field field_rng_base; | |
1004 gccjit::field field_rng_limit; | |
1005 gccjit::field field_rng_inc; | |
1006 gccjit::field field_rng_nelem; | |
1007 #endif | |
15016 | 1008 jit_type *string; |
1009 jit_type *boolean; | |
1010 jit_type *index; | |
1011 jit_type *complex; | |
1012 jit_type *unknown_function; | |
1013 std::map<size_t, jit_type *> ints; | |
1014 std::map<std::string, jit_type *> builtins; | |
1015 | |
20654 | 1016 #ifdef HAVE_LLVM |
15016 | 1017 llvm::StructType *complex_ret; |
20654 | 1018 #endif |
15016 | 1019 |
1020 std::vector<jit_operation> binary_ops; | |
15146
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15135
diff
changeset
|
1021 std::vector<jit_operation> unary_ops; |
15016 | 1022 jit_operation grab_fn; |
1023 jit_operation release_fn; | |
15337
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
1024 jit_operation destroy_fn; |
15016 | 1025 jit_operation print_fn; |
1026 jit_operation for_init_fn; | |
1027 jit_operation for_check_fn; | |
1028 jit_operation for_index_fn; | |
1029 jit_operation logically_true_fn; | |
1030 jit_operation make_range_fn; | |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
1031 jit_paren_subsref paren_subsref_fn; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
1032 jit_paren_subsasgn paren_subsasgn_fn; |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1033 jit_operation end1_fn; |
15056
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1034 jit_operation end_fn; |
15337
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
1035 jit_operation create_undef_fn; |
15016 | 1036 |
15135
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
1037 jit_function any_call; |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
1038 |
15016 | 1039 // type id -> cast function TO that type |
1040 std::vector<jit_operation> casts; | |
1041 | |
1042 // type id -> identity function | |
1043 std::vector<jit_function> identities; | |
1044 | |
20654 | 1045 #ifdef HAVE_LLVM |
15016 | 1046 llvm::IRBuilderD& builder; |
20654 | 1047 #endif |
1048 | |
1049 #ifdef HAVE_GCCJIT | |
1050 gccjit::context gccjit_ctxt; | |
1051 #endif | |
15016 | 1052 }; |
1053 | |
20654 | 1054 #endif /* ifdef HAVE_JIT */ |
15016 | 1055 #endif |