Mercurial > octave-nkf
annotate libinterp/interp-core/jit-typeinfo.h @ 15385:8ccb187b24e9
Erase partially created functions on JIT compilation failure (bug #37308)
* pt-jit.cc (tree_jit::optimize): Verify module in debug mode.
(jit_function_info::jit_function_info): Erase functions on failure.
author | Max Brister <max@2bass.com> |
---|---|
date | Fri, 14 Sep 2012 06:09:41 -0600 |
parents | 8355fddce815 |
children | 44272909d926 |
rev | line source |
---|---|
15016 | 1 /* |
2 | |
3 Copyright (C) 2012 Max Brister <max@2bass.com> | |
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 | |
23 #if !defined (octave_jit_typeinfo_h) | |
24 #define octave_jit_typeinfo_h 1 | |
25 | |
26 #ifdef HAVE_LLVM | |
27 | |
28 #include <map> | |
29 #include <vector> | |
30 | |
31 #include "Range.h" | |
32 #include "jit-util.h" | |
33 | |
34 // Defines the type system used by jit and a singleton class, jit_typeinfo, to | |
35 // manage the types. | |
36 // | |
37 // FIXME: | |
38 // Operations are defined and implemented in jit_typeinfo. Eventually they | |
39 // should be moved elsewhere. (just like with octave_typeinfo) | |
40 | |
41 // jit_range is compatable with the llvm range structure | |
42 struct | |
43 jit_range | |
44 { | |
45 jit_range (const Range& from) : base (from.base ()), limit (from.limit ()), | |
46 inc (from.inc ()), nelem (from.nelem ()) | |
47 {} | |
48 | |
49 operator Range () const | |
50 { | |
51 return Range (base, limit, inc); | |
52 } | |
53 | |
54 bool all_elements_are_ints () const; | |
55 | |
56 double base; | |
57 double limit; | |
58 double inc; | |
59 octave_idx_type nelem; | |
60 }; | |
61 | |
62 std::ostream& operator<< (std::ostream& os, const jit_range& rng); | |
63 | |
64 // jit_array is compatable with the llvm array/matrix structures | |
65 template <typename T, typename U> | |
66 struct | |
67 jit_array | |
68 { | |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
69 jit_array () : array (0) {} |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
70 |
15016 | 71 jit_array (T& from) : array (new T (from)) |
72 { | |
73 update (); | |
74 } | |
75 | |
76 void update (void) | |
77 { | |
78 ref_count = array->jit_ref_count (); | |
79 slice_data = array->jit_slice_data () - 1; | |
80 slice_len = array->capacity (); | |
81 dimensions = array->jit_dimensions (); | |
82 } | |
83 | |
84 void update (T *aarray) | |
85 { | |
86 array = aarray; | |
87 update (); | |
88 } | |
89 | |
90 operator T () const | |
91 { | |
92 return *array; | |
93 } | |
94 | |
95 int *ref_count; | |
96 | |
97 U *slice_data; | |
98 octave_idx_type slice_len; | |
99 octave_idx_type *dimensions; | |
100 | |
101 T *array; | |
102 }; | |
103 | |
104 typedef jit_array<NDArray, double> jit_matrix; | |
105 | |
106 std::ostream& operator<< (std::ostream& os, const jit_matrix& mat); | |
107 | |
108 // calling convention | |
109 namespace | |
110 jit_convention | |
111 { | |
112 enum | |
113 type | |
114 { | |
115 // internal to jit | |
116 internal, | |
117 | |
118 // an external C call | |
119 external, | |
120 | |
121 length | |
122 }; | |
123 } | |
124 | |
125 // Used to keep track of estimated (infered) types during JIT. This is a | |
126 // hierarchical type system which includes both concrete and abstract types. | |
127 // | |
128 // The types form a lattice. Currently we only allow for one parent type, but | |
129 // eventually we may allow for multiple predecessors. | |
130 class | |
131 jit_type | |
132 { | |
133 public: | |
134 typedef llvm::Value *(*convert_fn) (llvm::IRBuilderD&, llvm::Value *); | |
135 | |
136 jit_type (const std::string& aname, jit_type *aparent, llvm::Type *allvm_type, | |
15124
0464e3ceb85b
Skip functions when resolving end context in JIT
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
137 bool askip_paren, int aid); |
15016 | 138 |
139 // a user readable type name | |
140 const std::string& name (void) const { return mname; } | |
141 | |
142 // a unique id for the type | |
143 int type_id (void) const { return mid; } | |
144 | |
145 // An abstract base type, may be null | |
146 jit_type *parent (void) const { return mparent; } | |
147 | |
148 // convert to an llvm type | |
149 llvm::Type *to_llvm (void) const { return llvm_type; } | |
150 | |
151 // how this type gets passed as a function argument | |
152 llvm::Type *to_llvm_arg (void) const; | |
153 | |
154 size_t depth (void) const { return mdepth; } | |
155 | |
15124
0464e3ceb85b
Skip functions when resolving end context in JIT
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
156 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
|
157 |
15016 | 158 // -------------------- Calling Convention information -------------------- |
159 | |
160 // A function declared like: mytype foo (int arg0, int arg1); | |
161 // Will be converted to: void foo (mytype *retval, int arg0, int arg1) | |
162 // if mytype is sret. The caller is responsible for allocating space for | |
163 // retval. (on the stack) | |
164 bool sret (jit_convention::type cc) const { return msret[cc]; } | |
165 | |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
166 void mark_sret (jit_convention::type cc) |
15016 | 167 { msret[cc] = true; } |
168 | |
169 // A function like: void foo (mytype arg0) | |
170 // Will be converted to: void foo (mytype *arg0) | |
171 // Basically just pass by reference. | |
172 bool pointer_arg (jit_convention::type cc) const { return mpointer_arg[cc]; } | |
173 | |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
174 void mark_pointer_arg (jit_convention::type cc) |
15016 | 175 { mpointer_arg[cc] = true; } |
176 | |
177 // Convert into an equivalent form before calling. For example, complex is | |
178 // represented as two values llvm vector, but we need to pass it as a two | |
179 // valued llvm structure to C functions. | |
180 convert_fn pack (jit_convention::type cc) { return mpack[cc]; } | |
181 | |
182 void set_pack (jit_convention::type cc, convert_fn fn) { mpack[cc] = fn; } | |
183 | |
184 // The inverse operation of pack. | |
185 convert_fn unpack (jit_convention::type cc) { return munpack[cc]; } | |
186 | |
187 void set_unpack (jit_convention::type cc, convert_fn fn) | |
188 { munpack[cc] = fn; } | |
189 | |
190 // The resulting type after pack is called. | |
191 llvm::Type *packed_type (jit_convention::type cc) | |
192 { return mpacked_type[cc]; } | |
193 | |
194 void set_packed_type (jit_convention::type cc, llvm::Type *ty) | |
195 { mpacked_type[cc] = ty; } | |
196 private: | |
197 std::string mname; | |
198 jit_type *mparent; | |
199 llvm::Type *llvm_type; | |
200 int mid; | |
201 size_t mdepth; | |
15124
0464e3ceb85b
Skip functions when resolving end context in JIT
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
202 bool mskip_paren; |
15016 | 203 |
204 bool msret[jit_convention::length]; | |
205 bool mpointer_arg[jit_convention::length]; | |
206 | |
207 convert_fn mpack[jit_convention::length]; | |
208 convert_fn munpack[jit_convention::length]; | |
209 | |
210 llvm::Type *mpacked_type[jit_convention::length]; | |
211 }; | |
212 | |
213 // seperate print function to allow easy printing if type is null | |
214 std::ostream& jit_print (std::ostream& os, jit_type *atype); | |
215 | |
216 class jit_value; | |
217 | |
218 // An abstraction for calling llvm functions with jit_values. Deals with calling | |
219 // convention details. | |
220 class | |
221 jit_function | |
222 { | |
223 friend std::ostream& operator<< (std::ostream& os, const jit_function& fn); | |
224 public: | |
225 // create a function in an invalid state | |
226 jit_function (); | |
227 | |
228 jit_function (llvm::Module *amodule, jit_convention::type acall_conv, | |
229 const llvm::Twine& aname, jit_type *aresult, | |
230 const std::vector<jit_type *>& aargs); | |
231 | |
232 // Use an existing function, but change the argument types. The new argument | |
233 // types must behave the same for the current calling convention. | |
234 jit_function (const jit_function& fn, jit_type *aresult, | |
235 const std::vector<jit_type *>& aargs); | |
236 | |
237 jit_function (const jit_function& fn); | |
238 | |
15385
8ccb187b24e9
Erase partially created functions on JIT compilation failure (bug #37308)
Max Brister <max@2bass.com>
parents:
15370
diff
changeset
|
239 // 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
|
240 void erase (void); |
8ccb187b24e9
Erase partially created functions on JIT compilation failure (bug #37308)
Max Brister <max@2bass.com>
parents:
15370
diff
changeset
|
241 |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
242 template <typename T> |
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
243 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
|
244 { |
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
245 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
|
246 } |
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
247 |
15016 | 248 bool valid (void) const { return llvm_function; } |
249 | |
250 std::string name (void) const; | |
251 | |
252 llvm::BasicBlock *new_block (const std::string& aname = "body", | |
253 llvm::BasicBlock *insert_before = 0); | |
254 | |
255 llvm::Value *call (llvm::IRBuilderD& builder, | |
256 const std::vector<jit_value *>& in_args) const; | |
257 | |
258 llvm::Value *call (llvm::IRBuilderD& builder, | |
259 const std::vector<llvm::Value *>& in_args | |
260 = std::vector<llvm::Value *> ()) const; | |
261 | |
262 #define JIT_PARAM_ARGS llvm::IRBuilderD& builder, | |
263 #define JIT_PARAMS builder, | |
264 #define JIT_CALL(N) JIT_EXPAND (llvm::Value *, call, llvm::Value *, const, N) | |
265 | |
266 JIT_CALL (1) | |
267 JIT_CALL (2) | |
268 JIT_CALL (3) | |
269 JIT_CALL (4) | |
270 JIT_CALL (5) | |
271 | |
272 #undef JIT_CALL | |
273 | |
274 #define JIT_CALL(N) JIT_EXPAND (llvm::Value *, call, jit_value *, const, N) | |
275 | |
276 JIT_CALL (1); | |
277 JIT_CALL (2); | |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
278 JIT_CALL (3); |
15016 | 279 |
280 #undef JIT_CALL | |
281 #undef JIT_PARAMS | |
282 #undef JIT_PARAM_ARGS | |
283 | |
284 llvm::Value *argument (llvm::IRBuilderD& builder, size_t idx) const; | |
285 | |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
286 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
|
287 bool verify = true); |
15016 | 288 |
289 llvm::Function *to_llvm (void) const { return llvm_function; } | |
290 | |
291 // If true, then the return value is passed as a pointer in the first argument | |
292 bool sret (void) const { return mresult && mresult->sret (call_conv); } | |
293 | |
294 bool can_error (void) const { return mcan_error; } | |
295 | |
296 void mark_can_error (void) { mcan_error = true; } | |
297 | |
298 jit_type *result (void) const { return mresult; } | |
299 | |
300 jit_type *argument_type (size_t idx) const | |
301 { | |
302 assert (idx < args.size ()); | |
303 return args[idx]; | |
304 } | |
305 | |
306 const std::vector<jit_type *>& arguments (void) const { return args; } | |
307 private: | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
308 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
|
309 |
15016 | 310 llvm::Module *module; |
311 llvm::Function *llvm_function; | |
312 jit_type *mresult; | |
313 std::vector<jit_type *> args; | |
314 jit_convention::type call_conv; | |
315 bool mcan_error; | |
316 }; | |
317 | |
318 std::ostream& operator<< (std::ostream& os, const jit_function& fn); | |
319 | |
320 | |
321 // Keeps track of information about how to implement operations (+, -, *, ect) | |
322 // and their resulting types. | |
323 class | |
324 jit_operation | |
325 { | |
326 public: | |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
327 // type signature vector |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
328 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
|
329 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
330 virtual ~jit_operation (void); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
331 |
15016 | 332 void add_overload (const jit_function& func) |
333 { | |
334 add_overload (func, func.arguments ()); | |
335 } | |
336 | |
337 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
|
338 const signature_vec& args); |
15016 | 339 |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
340 const jit_function& overload (const signature_vec& types) const; |
15016 | 341 |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
342 jit_type *result (const signature_vec& types) const |
15016 | 343 { |
344 const jit_function& temp = overload (types); | |
345 return temp.result (); | |
346 } | |
347 | |
348 #define JIT_PARAMS | |
349 #define JIT_PARAM_ARGS | |
350 #define JIT_OVERLOAD(N) \ | |
351 JIT_EXPAND (const jit_function&, overload, jit_type *, const, N) \ | |
352 JIT_EXPAND (jit_type *, result, jit_type *, const, N) | |
353 | |
354 JIT_OVERLOAD (1); | |
355 JIT_OVERLOAD (2); | |
356 JIT_OVERLOAD (3); | |
357 | |
358 #undef JIT_PARAMS | |
359 #undef JIT_PARAM_ARGS | |
360 | |
361 const std::string& name (void) const { return mname; } | |
362 | |
363 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
|
364 protected: |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
365 virtual jit_function *generate (const signature_vec& types) const; |
15016 | 366 private: |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
367 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
|
368 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
369 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
|
370 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
371 struct signature_cmp |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
372 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
373 bool operator() (const signature_vec *lhs, const signature_vec *rhs); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
374 }; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
375 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
376 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
|
377 generated_map; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
378 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
379 mutable generated_map generated; |
15016 | 380 |
381 std::vector<Array<jit_function> > overloads; | |
382 | |
383 std::string mname; | |
384 }; | |
385 | |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
386 class |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
387 jit_index_operation : public jit_operation |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
388 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
389 public: |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
390 jit_index_operation (void) : module (0), engine (0) {} |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
391 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
392 void initialize (llvm::Module *amodule, llvm::ExecutionEngine *aengine) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
393 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
394 module = amodule; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
395 engine = aengine; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
396 do_initialize (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
397 } |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
398 protected: |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
399 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
|
400 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
401 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
|
402 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
403 virtual void do_initialize (void) = 0; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
404 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
405 // helper functions |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
406 // [start_idx, end_idx). |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
407 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
|
408 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
|
409 size_t end_idx) const; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
410 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
411 llvm::Module *module; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
412 llvm::ExecutionEngine *engine; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
413 }; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
414 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
415 class |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
416 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
|
417 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
418 protected: |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
419 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
|
420 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
421 virtual void do_initialize (void); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
422 private: |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
423 jit_function paren_scalar; |
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 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
426 class |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
427 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
|
428 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
429 protected: |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
430 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
|
431 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
432 virtual void do_initialize (void); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
433 private: |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
434 jit_function paren_scalar; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
435 }; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
436 |
15016 | 437 // A singleton class which handles the construction of jit_types and |
438 // jit_operations. | |
439 class | |
440 jit_typeinfo | |
441 { | |
442 public: | |
443 static void initialize (llvm::Module *m, llvm::ExecutionEngine *e); | |
444 | |
445 static jit_type *join (jit_type *lhs, jit_type *rhs) | |
446 { | |
447 return instance->do_join (lhs, rhs); | |
448 } | |
449 | |
450 static jit_type *get_any (void) { return instance->any; } | |
451 | |
452 static jit_type *get_matrix (void) { return instance->matrix; } | |
453 | |
454 static jit_type *get_scalar (void) { return instance->scalar; } | |
455 | |
456 static llvm::Type *get_scalar_llvm (void) | |
457 { return instance->scalar->to_llvm (); } | |
458 | |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
459 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
|
460 |
15337
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
461 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
|
462 |
15016 | 463 static jit_type *get_range (void) { return instance->range; } |
464 | |
465 static jit_type *get_string (void) { return instance->string; } | |
466 | |
467 static jit_type *get_bool (void) { return instance->boolean; } | |
468 | |
469 static jit_type *get_index (void) { return instance->index; } | |
470 | |
471 static llvm::Type *get_index_llvm (void) | |
472 { return instance->index->to_llvm (); } | |
473 | |
474 static jit_type *get_complex (void) { return instance->complex; } | |
475 | |
476 // Get the jit_type of an octave_value | |
477 static jit_type *type_of (const octave_value& ov) | |
478 { | |
479 return instance->do_type_of (ov); | |
480 } | |
481 | |
482 static const jit_operation& binary_op (int op) | |
483 { | |
484 return instance->do_binary_op (op); | |
485 } | |
486 | |
15146
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15135
diff
changeset
|
487 static const jit_operation& unary_op (int op) |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15135
diff
changeset
|
488 { |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15135
diff
changeset
|
489 return instance->do_unary_op (op); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15135
diff
changeset
|
490 } |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15135
diff
changeset
|
491 |
15016 | 492 static const jit_operation& grab (void) { return instance->grab_fn; } |
493 | |
494 static const jit_function& get_grab (jit_type *type) | |
495 { | |
496 return instance->grab_fn.overload (type); | |
497 } | |
498 | |
499 static const jit_operation& release (void) | |
500 { | |
501 return instance->release_fn; | |
502 } | |
503 | |
504 static const jit_function& get_release (jit_type *type) | |
505 { | |
506 return instance->release_fn.overload (type); | |
507 } | |
508 | |
15337
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
509 static const jit_operation& destroy (void) |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
510 { |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
511 return instance->destroy_fn; |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
512 } |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
513 |
15016 | 514 static const jit_operation& print_value (void) |
515 { | |
516 return instance->print_fn; | |
517 } | |
518 | |
519 static const jit_operation& for_init (void) | |
520 { | |
521 return instance->for_init_fn; | |
522 } | |
523 | |
524 static const jit_operation& for_check (void) | |
525 { | |
526 return instance->for_check_fn; | |
527 } | |
528 | |
529 static const jit_operation& for_index (void) | |
530 { | |
531 return instance->for_index_fn; | |
532 } | |
533 | |
534 static const jit_operation& make_range (void) | |
535 { | |
536 return instance->make_range_fn; | |
537 } | |
538 | |
539 static const jit_operation& paren_subsref (void) | |
540 { | |
541 return instance->paren_subsref_fn; | |
542 } | |
543 | |
544 static const jit_operation& paren_subsasgn (void) | |
545 { | |
546 return instance->paren_subsasgn_fn; | |
547 } | |
548 | |
549 static const jit_operation& logically_true (void) | |
550 { | |
551 return instance->logically_true_fn; | |
552 } | |
553 | |
554 static const jit_operation& cast (jit_type *result) | |
555 { | |
556 return instance->do_cast (result); | |
557 } | |
558 | |
559 static const jit_function& cast (jit_type *to, jit_type *from) | |
560 { | |
561 return instance->do_cast (to, from); | |
562 } | |
563 | |
564 static llvm::Value *insert_error_check (llvm::IRBuilderD& bld) | |
565 { | |
566 return instance->do_insert_error_check (bld); | |
567 } | |
15056
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
568 |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
569 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
|
570 { |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
571 return instance->end_fn; |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
572 } |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
573 |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
574 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
|
575 jit_value *count) |
15056
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
576 { |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
577 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
|
578 } |
15337
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
579 |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
580 static const jit_operation& create_undef (void) |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
581 { |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
582 return instance->create_undef_fn; |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
583 } |
15016 | 584 private: |
585 jit_typeinfo (llvm::Module *m, llvm::ExecutionEngine *e); | |
586 | |
587 // FIXME: Do these methods really need to be in jit_typeinfo? | |
588 jit_type *do_join (jit_type *lhs, jit_type *rhs) | |
589 { | |
590 // empty case | |
591 if (! lhs) | |
592 return rhs; | |
593 | |
594 if (! rhs) | |
595 return lhs; | |
596 | |
597 // check for a shared parent | |
598 while (lhs != rhs) | |
599 { | |
600 if (lhs->depth () > rhs->depth ()) | |
601 lhs = lhs->parent (); | |
602 else if (lhs->depth () < rhs->depth ()) | |
603 rhs = rhs->parent (); | |
604 else | |
605 { | |
606 // we MUST have depth > 0 as any is the base type of everything | |
607 do | |
608 { | |
609 lhs = lhs->parent (); | |
610 rhs = rhs->parent (); | |
611 } | |
612 while (lhs != rhs); | |
613 } | |
614 } | |
615 | |
616 return lhs; | |
617 } | |
618 | |
619 jit_type *do_difference (jit_type *lhs, jit_type *) | |
620 { | |
621 // FIXME: Maybe we can do something smarter? | |
622 return lhs; | |
623 } | |
624 | |
625 jit_type *do_type_of (const octave_value &ov) const; | |
626 | |
627 const jit_operation& do_binary_op (int op) const | |
628 { | |
629 assert (static_cast<size_t>(op) < binary_ops.size ()); | |
630 return binary_ops[op]; | |
631 } | |
632 | |
15146
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15135
diff
changeset
|
633 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
|
634 { |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15135
diff
changeset
|
635 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
|
636 return unary_ops[op]; |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15135
diff
changeset
|
637 } |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15135
diff
changeset
|
638 |
15016 | 639 const jit_operation& do_cast (jit_type *to) |
640 { | |
641 static jit_operation null_function; | |
642 if (! to) | |
643 return null_function; | |
644 | |
645 size_t id = to->type_id (); | |
646 if (id >= casts.size ()) | |
647 return null_function; | |
648 return casts[id]; | |
649 } | |
650 | |
651 const jit_function& do_cast (jit_type *to, jit_type *from) | |
652 { | |
653 return do_cast (to).overload (from); | |
654 } | |
655 | |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
656 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
|
657 jit_value *count); |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
658 |
15016 | 659 jit_type *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
|
660 llvm::Type *llvm_type, bool skip_paren = false); |
15016 | 661 |
662 | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
663 void add_print (jit_type *ty, void *fptr); |
15016 | 664 |
665 void add_binary_op (jit_type *ty, int op, int llvm_op); | |
666 | |
667 void add_binary_icmp (jit_type *ty, int op, int llvm_op); | |
668 | |
669 void add_binary_fcmp (jit_type *ty, int op, int llvm_op); | |
670 | |
671 jit_function create_function (jit_convention::type cc, | |
672 const llvm::Twine& name, jit_type *ret, | |
673 const std::vector<jit_type *>& args | |
674 = std::vector<jit_type *> ()); | |
675 | |
676 #define JIT_PARAM_ARGS jit_convention::type cc, const llvm::Twine& name, \ | |
677 jit_type *ret, | |
678 #define JIT_PARAMS cc, name, ret, | |
679 #define CREATE_FUNCTION(N) JIT_EXPAND(jit_function, create_function, \ | |
680 jit_type *, /* empty */, N) | |
681 | |
682 CREATE_FUNCTION(1); | |
683 CREATE_FUNCTION(2); | |
684 CREATE_FUNCTION(3); | |
685 CREATE_FUNCTION(4); | |
686 | |
687 #undef JIT_PARAM_ARGS | |
688 #undef JIT_PARAMS | |
689 #undef CREATE_FUNCTION | |
690 | |
691 jit_function create_identity (jit_type *type); | |
692 | |
693 llvm::Value *do_insert_error_check (llvm::IRBuilderD& bld); | |
694 | |
695 void add_builtin (const std::string& name); | |
696 | |
697 void register_intrinsic (const std::string& name, size_t id, | |
698 jit_type *result, jit_type *arg0) | |
699 { | |
700 std::vector<jit_type *> args (1, arg0); | |
701 register_intrinsic (name, id, result, args); | |
702 } | |
703 | |
704 void register_intrinsic (const std::string& name, size_t id, jit_type *result, | |
705 const std::vector<jit_type *>& args); | |
706 | |
707 void register_generic (const std::string& name, jit_type *result, | |
708 jit_type *arg0) | |
709 { | |
710 std::vector<jit_type *> args (1, arg0); | |
711 register_generic (name, result, args); | |
712 } | |
713 | |
714 void register_generic (const std::string& name, jit_type *result, | |
715 const std::vector<jit_type *>& args); | |
716 | |
717 octave_builtin *find_builtin (const std::string& name); | |
718 | |
719 jit_function mirror_binary (const jit_function& fn); | |
720 | |
721 llvm::Function *wrap_complex (llvm::Function *wrap); | |
722 | |
723 static llvm::Value *pack_complex (llvm::IRBuilderD& bld, | |
724 llvm::Value *cplx); | |
725 | |
726 static llvm::Value *unpack_complex (llvm::IRBuilderD& bld, | |
727 llvm::Value *result); | |
728 | |
729 llvm::Value *complex_real (llvm::Value *cx); | |
730 | |
731 llvm::Value *complex_real (llvm::Value *cx, llvm::Value *real); | |
732 | |
733 llvm::Value *complex_imag (llvm::Value *cx); | |
734 | |
735 llvm::Value *complex_imag (llvm::Value *cx, llvm::Value *imag); | |
736 | |
737 llvm::Value *complex_new (llvm::Value *real, llvm::Value *imag); | |
738 | |
739 void create_int (size_t nbits); | |
740 | |
741 jit_type *intN (size_t nbits) const; | |
742 | |
743 static jit_typeinfo *instance; | |
744 | |
745 llvm::Module *module; | |
746 llvm::ExecutionEngine *engine; | |
747 int next_id; | |
748 | |
749 llvm::GlobalVariable *lerror_state; | |
750 | |
751 std::vector<jit_type*> id_to_type; | |
752 jit_type *any; | |
753 jit_type *matrix; | |
754 jit_type *scalar; | |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
755 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
|
756 jit_type *any_ptr; // a fake type for interfacing with C++ |
15016 | 757 jit_type *range; |
758 jit_type *string; | |
759 jit_type *boolean; | |
760 jit_type *index; | |
761 jit_type *complex; | |
762 jit_type *unknown_function; | |
763 std::map<size_t, jit_type *> ints; | |
764 std::map<std::string, jit_type *> builtins; | |
765 | |
766 llvm::StructType *complex_ret; | |
767 | |
768 std::vector<jit_operation> binary_ops; | |
15146
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15135
diff
changeset
|
769 std::vector<jit_operation> unary_ops; |
15016 | 770 jit_operation grab_fn; |
771 jit_operation release_fn; | |
15337
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
772 jit_operation destroy_fn; |
15016 | 773 jit_operation print_fn; |
774 jit_operation for_init_fn; | |
775 jit_operation for_check_fn; | |
776 jit_operation for_index_fn; | |
777 jit_operation logically_true_fn; | |
778 jit_operation make_range_fn; | |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
779 jit_paren_subsref paren_subsref_fn; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
780 jit_paren_subsasgn paren_subsasgn_fn; |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
781 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
|
782 jit_operation end_fn; |
15337
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
783 jit_operation create_undef_fn; |
15016 | 784 |
15135
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
785 jit_function any_call; |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
786 |
15016 | 787 // type id -> cast function TO that type |
788 std::vector<jit_operation> casts; | |
789 | |
790 // type id -> identity function | |
791 std::vector<jit_function> identities; | |
792 | |
793 llvm::IRBuilderD& builder; | |
794 }; | |
795 | |
796 #endif | |
797 #endif |