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