Mercurial > octave-nkf
annotate src/interp-core/jit-typeinfo.h @ 15102:d29f2583cf7b
Support end in multi indexing in JIT
* src/interp-core/jit-ir.cc (jit_magic_end::context::context): New function.
(jit_magic_end::jit_magic_end): Take context vector as argument.
(jit_magic_end::resolve_context): Return a context.
(jit_magic_end::print): Prettify output.
(jit_magic_end::overload): Use context.
* src/interp-core/jit-ir.h (jit_magic_end::context::context,
jit_magic_end::print): Move implementation to src/jit-ir.cc.
(jit_magic_end::short_print): Prettify output.
(jit_magic_end::resolve_context): Return a context.
* src/interp-core/jit-typeinfo.cc (octave_jit_end_matrix): New function.
(jit_typeinfo::jit_typeinfo): Initilaize end_fn and end1_fn.
(jit_typeinfo::do_end): New function.
(jit_typeinfo::new_type): Moved location in file.
* src/interp-core/jit-typeinfo.h (jit_typeinfo::end): Take index and count
arguments.
(jit_typeinfo::do_end): New declaration.
* src/interp-core/pt-jit.cc (jit_convert::resolve): Pass extra argument to
context constructor.
(jit_convert::convert_llvm::visit): New arguments to jit_magic_end overload.
author | Max Brister <max@2bass.com> |
---|---|
date | Sat, 04 Aug 2012 00:19:07 -0500 |
parents | 909a2797935b |
children | 0464e3ceb85b |
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); | |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
270 JIT_CALL (3); |
15016 | 271 |
272 #undef JIT_CALL | |
273 #undef JIT_PARAMS | |
274 #undef JIT_PARAM_ARGS | |
275 | |
276 llvm::Value *argument (llvm::IRBuilderD& builder, size_t idx) const; | |
277 | |
278 void do_return (llvm::IRBuilderD& builder, llvm::Value *rval = 0); | |
279 | |
280 llvm::Function *to_llvm (void) const { return llvm_function; } | |
281 | |
282 // If true, then the return value is passed as a pointer in the first argument | |
283 bool sret (void) const { return mresult && mresult->sret (call_conv); } | |
284 | |
285 bool can_error (void) const { return mcan_error; } | |
286 | |
287 void mark_can_error (void) { mcan_error = true; } | |
288 | |
289 jit_type *result (void) const { return mresult; } | |
290 | |
291 jit_type *argument_type (size_t idx) const | |
292 { | |
293 assert (idx < args.size ()); | |
294 return args[idx]; | |
295 } | |
296 | |
297 const std::vector<jit_type *>& arguments (void) const { return args; } | |
298 private: | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
299 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
|
300 |
15016 | 301 llvm::Module *module; |
302 llvm::Function *llvm_function; | |
303 jit_type *mresult; | |
304 std::vector<jit_type *> args; | |
305 jit_convention::type call_conv; | |
306 bool mcan_error; | |
307 }; | |
308 | |
309 std::ostream& operator<< (std::ostream& os, const jit_function& fn); | |
310 | |
311 | |
312 // Keeps track of information about how to implement operations (+, -, *, ect) | |
313 // and their resulting types. | |
314 class | |
315 jit_operation | |
316 { | |
317 public: | |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
318 // type signature vector |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
319 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
|
320 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
321 virtual ~jit_operation (void); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
322 |
15016 | 323 void add_overload (const jit_function& func) |
324 { | |
325 add_overload (func, func.arguments ()); | |
326 } | |
327 | |
328 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
|
329 const signature_vec& args); |
15016 | 330 |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
331 const jit_function& overload (const signature_vec& types) const; |
15016 | 332 |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
333 jit_type *result (const signature_vec& types) const |
15016 | 334 { |
335 const jit_function& temp = overload (types); | |
336 return temp.result (); | |
337 } | |
338 | |
339 #define JIT_PARAMS | |
340 #define JIT_PARAM_ARGS | |
341 #define JIT_OVERLOAD(N) \ | |
342 JIT_EXPAND (const jit_function&, overload, jit_type *, const, N) \ | |
343 JIT_EXPAND (jit_type *, result, jit_type *, const, N) | |
344 | |
345 JIT_OVERLOAD (1); | |
346 JIT_OVERLOAD (2); | |
347 JIT_OVERLOAD (3); | |
348 | |
349 #undef JIT_PARAMS | |
350 #undef JIT_PARAM_ARGS | |
351 | |
352 const std::string& name (void) const { return mname; } | |
353 | |
354 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
|
355 protected: |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
356 virtual jit_function *generate (const signature_vec& types) const; |
15016 | 357 private: |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
358 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
|
359 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
360 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
|
361 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
362 struct signature_cmp |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
363 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
364 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
|
365 }; |
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 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
|
368 generated_map; |
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 mutable generated_map generated; |
15016 | 371 |
372 std::vector<Array<jit_function> > overloads; | |
373 | |
374 std::string mname; | |
375 }; | |
376 | |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
377 class |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
378 jit_index_operation : public jit_operation |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
379 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
380 public: |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
381 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
|
382 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
383 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
|
384 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
385 module = amodule; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
386 engine = aengine; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
387 do_initialize (); |
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 protected: |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
390 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
|
391 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
392 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
|
393 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
394 virtual void do_initialize (void) = 0; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
395 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
396 // helper functions |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
397 // [start_idx, end_idx). |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
398 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
|
399 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
|
400 size_t end_idx) const; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
401 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
402 llvm::Module *module; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
403 llvm::ExecutionEngine *engine; |
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 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
406 class |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
407 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
|
408 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
409 protected: |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
410 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
|
411 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
412 virtual void do_initialize (void); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
413 private: |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
414 jit_function paren_scalar; |
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 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
417 class |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
418 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
|
419 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
420 protected: |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
421 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
|
422 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
423 virtual void do_initialize (void); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
424 private: |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
425 jit_function paren_scalar; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
426 }; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
427 |
15016 | 428 // A singleton class which handles the construction of jit_types and |
429 // jit_operations. | |
430 class | |
431 jit_typeinfo | |
432 { | |
433 public: | |
434 static void initialize (llvm::Module *m, llvm::ExecutionEngine *e); | |
435 | |
436 static jit_type *join (jit_type *lhs, jit_type *rhs) | |
437 { | |
438 return instance->do_join (lhs, rhs); | |
439 } | |
440 | |
441 static jit_type *get_any (void) { return instance->any; } | |
442 | |
443 static jit_type *get_matrix (void) { return instance->matrix; } | |
444 | |
445 static jit_type *get_scalar (void) { return instance->scalar; } | |
446 | |
447 static llvm::Type *get_scalar_llvm (void) | |
448 { return instance->scalar->to_llvm (); } | |
449 | |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
450 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
|
451 |
15016 | 452 static jit_type *get_range (void) { return instance->range; } |
453 | |
454 static jit_type *get_string (void) { return instance->string; } | |
455 | |
456 static jit_type *get_bool (void) { return instance->boolean; } | |
457 | |
458 static jit_type *get_index (void) { return instance->index; } | |
459 | |
460 static llvm::Type *get_index_llvm (void) | |
461 { return instance->index->to_llvm (); } | |
462 | |
463 static jit_type *get_complex (void) { return instance->complex; } | |
464 | |
465 // Get the jit_type of an octave_value | |
466 static jit_type *type_of (const octave_value& ov) | |
467 { | |
468 return instance->do_type_of (ov); | |
469 } | |
470 | |
471 static const jit_operation& binary_op (int op) | |
472 { | |
473 return instance->do_binary_op (op); | |
474 } | |
475 | |
476 static const jit_operation& grab (void) { return instance->grab_fn; } | |
477 | |
478 static const jit_function& get_grab (jit_type *type) | |
479 { | |
480 return instance->grab_fn.overload (type); | |
481 } | |
482 | |
483 static const jit_operation& release (void) | |
484 { | |
485 return instance->release_fn; | |
486 } | |
487 | |
488 static const jit_function& get_release (jit_type *type) | |
489 { | |
490 return instance->release_fn.overload (type); | |
491 } | |
492 | |
493 static const jit_operation& print_value (void) | |
494 { | |
495 return instance->print_fn; | |
496 } | |
497 | |
498 static const jit_operation& for_init (void) | |
499 { | |
500 return instance->for_init_fn; | |
501 } | |
502 | |
503 static const jit_operation& for_check (void) | |
504 { | |
505 return instance->for_check_fn; | |
506 } | |
507 | |
508 static const jit_operation& for_index (void) | |
509 { | |
510 return instance->for_index_fn; | |
511 } | |
512 | |
513 static const jit_operation& make_range (void) | |
514 { | |
515 return instance->make_range_fn; | |
516 } | |
517 | |
518 static const jit_operation& paren_subsref (void) | |
519 { | |
520 return instance->paren_subsref_fn; | |
521 } | |
522 | |
523 static const jit_operation& paren_subsasgn (void) | |
524 { | |
525 return instance->paren_subsasgn_fn; | |
526 } | |
527 | |
528 static const jit_operation& logically_true (void) | |
529 { | |
530 return instance->logically_true_fn; | |
531 } | |
532 | |
533 static const jit_operation& cast (jit_type *result) | |
534 { | |
535 return instance->do_cast (result); | |
536 } | |
537 | |
538 static const jit_function& cast (jit_type *to, jit_type *from) | |
539 { | |
540 return instance->do_cast (to, from); | |
541 } | |
542 | |
543 static llvm::Value *insert_error_check (llvm::IRBuilderD& bld) | |
544 { | |
545 return instance->do_insert_error_check (bld); | |
546 } | |
15056
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
547 |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
548 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
|
549 { |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
550 return instance->end_fn; |
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 |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
553 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
|
554 jit_value *count) |
15056
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
555 { |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
556 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
|
557 } |
15016 | 558 private: |
559 jit_typeinfo (llvm::Module *m, llvm::ExecutionEngine *e); | |
560 | |
561 // FIXME: Do these methods really need to be in jit_typeinfo? | |
562 jit_type *do_join (jit_type *lhs, jit_type *rhs) | |
563 { | |
564 // empty case | |
565 if (! lhs) | |
566 return rhs; | |
567 | |
568 if (! rhs) | |
569 return lhs; | |
570 | |
571 // check for a shared parent | |
572 while (lhs != rhs) | |
573 { | |
574 if (lhs->depth () > rhs->depth ()) | |
575 lhs = lhs->parent (); | |
576 else if (lhs->depth () < rhs->depth ()) | |
577 rhs = rhs->parent (); | |
578 else | |
579 { | |
580 // we MUST have depth > 0 as any is the base type of everything | |
581 do | |
582 { | |
583 lhs = lhs->parent (); | |
584 rhs = rhs->parent (); | |
585 } | |
586 while (lhs != rhs); | |
587 } | |
588 } | |
589 | |
590 return lhs; | |
591 } | |
592 | |
593 jit_type *do_difference (jit_type *lhs, jit_type *) | |
594 { | |
595 // FIXME: Maybe we can do something smarter? | |
596 return lhs; | |
597 } | |
598 | |
599 jit_type *do_type_of (const octave_value &ov) const; | |
600 | |
601 const jit_operation& do_binary_op (int op) const | |
602 { | |
603 assert (static_cast<size_t>(op) < binary_ops.size ()); | |
604 return binary_ops[op]; | |
605 } | |
606 | |
607 const jit_operation& do_cast (jit_type *to) | |
608 { | |
609 static jit_operation null_function; | |
610 if (! to) | |
611 return null_function; | |
612 | |
613 size_t id = to->type_id (); | |
614 if (id >= casts.size ()) | |
615 return null_function; | |
616 return casts[id]; | |
617 } | |
618 | |
619 const jit_function& do_cast (jit_type *to, jit_type *from) | |
620 { | |
621 return do_cast (to).overload (from); | |
622 } | |
623 | |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
624 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
|
625 jit_value *count); |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
626 |
15016 | 627 jit_type *new_type (const std::string& name, jit_type *parent, |
628 llvm::Type *llvm_type); | |
629 | |
630 | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
631 void add_print (jit_type *ty, void *fptr); |
15016 | 632 |
633 void add_binary_op (jit_type *ty, int op, int llvm_op); | |
634 | |
635 void add_binary_icmp (jit_type *ty, int op, int llvm_op); | |
636 | |
637 void add_binary_fcmp (jit_type *ty, int op, int llvm_op); | |
638 | |
639 jit_function create_function (jit_convention::type cc, | |
640 const llvm::Twine& name, jit_type *ret, | |
641 const std::vector<jit_type *>& args | |
642 = std::vector<jit_type *> ()); | |
643 | |
644 #define JIT_PARAM_ARGS jit_convention::type cc, const llvm::Twine& name, \ | |
645 jit_type *ret, | |
646 #define JIT_PARAMS cc, name, ret, | |
647 #define CREATE_FUNCTION(N) JIT_EXPAND(jit_function, create_function, \ | |
648 jit_type *, /* empty */, N) | |
649 | |
650 CREATE_FUNCTION(1); | |
651 CREATE_FUNCTION(2); | |
652 CREATE_FUNCTION(3); | |
653 CREATE_FUNCTION(4); | |
654 | |
655 #undef JIT_PARAM_ARGS | |
656 #undef JIT_PARAMS | |
657 #undef CREATE_FUNCTION | |
658 | |
659 jit_function create_identity (jit_type *type); | |
660 | |
661 llvm::Value *do_insert_error_check (llvm::IRBuilderD& bld); | |
662 | |
663 void add_builtin (const std::string& name); | |
664 | |
665 void register_intrinsic (const std::string& name, size_t id, | |
666 jit_type *result, jit_type *arg0) | |
667 { | |
668 std::vector<jit_type *> args (1, arg0); | |
669 register_intrinsic (name, id, result, args); | |
670 } | |
671 | |
672 void register_intrinsic (const std::string& name, size_t id, jit_type *result, | |
673 const std::vector<jit_type *>& args); | |
674 | |
675 void register_generic (const std::string& name, jit_type *result, | |
676 jit_type *arg0) | |
677 { | |
678 std::vector<jit_type *> args (1, arg0); | |
679 register_generic (name, result, args); | |
680 } | |
681 | |
682 void register_generic (const std::string& name, jit_type *result, | |
683 const std::vector<jit_type *>& args); | |
684 | |
685 octave_builtin *find_builtin (const std::string& name); | |
686 | |
687 jit_function mirror_binary (const jit_function& fn); | |
688 | |
689 llvm::Function *wrap_complex (llvm::Function *wrap); | |
690 | |
691 static llvm::Value *pack_complex (llvm::IRBuilderD& bld, | |
692 llvm::Value *cplx); | |
693 | |
694 static llvm::Value *unpack_complex (llvm::IRBuilderD& bld, | |
695 llvm::Value *result); | |
696 | |
697 llvm::Value *complex_real (llvm::Value *cx); | |
698 | |
699 llvm::Value *complex_real (llvm::Value *cx, llvm::Value *real); | |
700 | |
701 llvm::Value *complex_imag (llvm::Value *cx); | |
702 | |
703 llvm::Value *complex_imag (llvm::Value *cx, llvm::Value *imag); | |
704 | |
705 llvm::Value *complex_new (llvm::Value *real, llvm::Value *imag); | |
706 | |
707 void create_int (size_t nbits); | |
708 | |
709 jit_type *intN (size_t nbits) const; | |
710 | |
711 static jit_typeinfo *instance; | |
712 | |
713 llvm::Module *module; | |
714 llvm::ExecutionEngine *engine; | |
715 int next_id; | |
716 | |
717 llvm::GlobalVariable *lerror_state; | |
718 | |
719 std::vector<jit_type*> id_to_type; | |
720 jit_type *any; | |
721 jit_type *matrix; | |
722 jit_type *scalar; | |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
723 jit_type *scalar_ptr; // a fake type for interfacing with C++ |
15016 | 724 jit_type *range; |
725 jit_type *string; | |
726 jit_type *boolean; | |
727 jit_type *index; | |
728 jit_type *complex; | |
729 jit_type *unknown_function; | |
730 std::map<size_t, jit_type *> ints; | |
731 std::map<std::string, jit_type *> builtins; | |
732 | |
733 llvm::StructType *complex_ret; | |
734 | |
735 std::vector<jit_operation> binary_ops; | |
736 jit_operation grab_fn; | |
737 jit_operation release_fn; | |
738 jit_operation print_fn; | |
739 jit_operation for_init_fn; | |
740 jit_operation for_check_fn; | |
741 jit_operation for_index_fn; | |
742 jit_operation logically_true_fn; | |
743 jit_operation make_range_fn; | |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
744 jit_paren_subsref paren_subsref_fn; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
745 jit_paren_subsasgn paren_subsasgn_fn; |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
746 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
|
747 jit_operation end_fn; |
15016 | 748 |
749 // type id -> cast function TO that type | |
750 std::vector<jit_operation> casts; | |
751 | |
752 // type id -> identity function | |
753 std::vector<jit_function> identities; | |
754 | |
755 llvm::IRBuilderD& builder; | |
756 }; | |
757 | |
758 #endif | |
759 #endif |