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