Mercurial > octave-nkf
annotate libinterp/corefcn/jit-typeinfo.cc @ 16892:68fc671a9339
maint: Collapse interpfcn and interpfcn-core directories into corefcn directory.
* libgui/src/module.mk: Remove -I references to interp-core, interpfcn, add
reference to corefcn.
* libinterp/Makefile.am: Remove -I references to interp-core, interpfcn, add
reference to corefcn.
* libinterp/corefcn/module.mk: Add files from interp-core, interpfcn to
build system. Copy over special rules from module.mk files in interp-core
andd interpfcn.
* src/Makefile.am: Replace references to interp-core, interpfcn with those
to corefcn.
* libinterp/corefcn/Cell.cc, libinterp/corefcn/Cell.h,
libinterp/corefcn/action-container.h, libinterp/corefcn/c-file-ptr-stream.cc,
libinterp/corefcn/c-file-ptr-stream.h, libinterp/corefcn/comment-list.cc,
libinterp/corefcn/comment-list.h, libinterp/corefcn/cutils.c,
libinterp/corefcn/cutils.h, libinterp/corefcn/data.cc,
libinterp/corefcn/data.h, libinterp/corefcn/debug.cc,
libinterp/corefcn/debug.h, libinterp/corefcn/defaults.cc,
libinterp/corefcn/defaults.in.h, libinterp/corefcn/defun-dld.h,
libinterp/corefcn/defun-int.h, libinterp/corefcn/defun.cc,
libinterp/corefcn/defun.h, libinterp/corefcn/dirfns.cc,
libinterp/corefcn/dirfns.h, libinterp/corefcn/display.cc,
libinterp/corefcn/display.h, libinterp/corefcn/dynamic-ld.cc,
libinterp/corefcn/dynamic-ld.h, libinterp/corefcn/error.cc,
libinterp/corefcn/error.h, libinterp/corefcn/event-queue.h,
libinterp/corefcn/file-io.cc, libinterp/corefcn/file-io.h,
libinterp/corefcn/gl-render.cc, libinterp/corefcn/gl-render.h,
libinterp/corefcn/gl2ps-renderer.cc, libinterp/corefcn/gl2ps-renderer.h,
libinterp/corefcn/gl2ps.c, libinterp/corefcn/gl2ps.h,
libinterp/corefcn/graphics.cc, libinterp/corefcn/graphics.in.h,
libinterp/corefcn/gripes.cc, libinterp/corefcn/gripes.h,
libinterp/corefcn/help.cc, libinterp/corefcn/help.h,
libinterp/corefcn/hook-fcn.cc, libinterp/corefcn/hook-fcn.h,
libinterp/corefcn/input.cc, libinterp/corefcn/input.h,
libinterp/corefcn/jit-ir.cc, libinterp/corefcn/jit-ir.h,
libinterp/corefcn/jit-typeinfo.cc, libinterp/corefcn/jit-typeinfo.h,
libinterp/corefcn/jit-util.cc, libinterp/corefcn/jit-util.h,
libinterp/corefcn/load-path.cc, libinterp/corefcn/load-path.h,
libinterp/corefcn/load-save.cc, libinterp/corefcn/load-save.h,
libinterp/corefcn/ls-ascii-helper.cc, libinterp/corefcn/ls-ascii-helper.h,
libinterp/corefcn/ls-hdf5.cc, libinterp/corefcn/ls-hdf5.h,
libinterp/corefcn/ls-mat-ascii.cc, libinterp/corefcn/ls-mat-ascii.h,
libinterp/corefcn/ls-mat4.cc, libinterp/corefcn/ls-mat4.h,
libinterp/corefcn/ls-mat5.cc, libinterp/corefcn/ls-mat5.h,
libinterp/corefcn/ls-oct-ascii.cc, libinterp/corefcn/ls-oct-ascii.h,
libinterp/corefcn/ls-oct-binary.cc, libinterp/corefcn/ls-oct-binary.h,
libinterp/corefcn/ls-utils.cc, libinterp/corefcn/ls-utils.h,
libinterp/corefcn/matherr.c, libinterp/corefcn/mex.cc, libinterp/corefcn/mex.h,
libinterp/corefcn/mexproto.h, libinterp/corefcn/mxarray.in.h,
libinterp/corefcn/oct-errno.h, libinterp/corefcn/oct-errno.in.cc,
libinterp/corefcn/oct-fstrm.cc, libinterp/corefcn/oct-fstrm.h,
libinterp/corefcn/oct-hdf5.h, libinterp/corefcn/oct-hist.cc,
libinterp/corefcn/oct-hist.h, libinterp/corefcn/oct-iostrm.cc,
libinterp/corefcn/oct-iostrm.h, libinterp/corefcn/oct-lvalue.cc,
libinterp/corefcn/oct-lvalue.h, libinterp/corefcn/oct-map.cc,
libinterp/corefcn/oct-map.h, libinterp/corefcn/oct-obj.cc,
libinterp/corefcn/oct-obj.h, libinterp/corefcn/oct-prcstrm.cc,
libinterp/corefcn/oct-prcstrm.h, libinterp/corefcn/oct-procbuf.cc,
libinterp/corefcn/oct-procbuf.h, libinterp/corefcn/oct-stdstrm.h,
libinterp/corefcn/oct-stream.cc, libinterp/corefcn/oct-stream.h,
libinterp/corefcn/oct-strstrm.cc, libinterp/corefcn/oct-strstrm.h,
libinterp/corefcn/oct.h, libinterp/corefcn/octave-link.cc,
libinterp/corefcn/octave-link.h, libinterp/corefcn/pager.cc,
libinterp/corefcn/pager.h, libinterp/corefcn/pr-output.cc,
libinterp/corefcn/pr-output.h, libinterp/corefcn/procstream.cc,
libinterp/corefcn/procstream.h, libinterp/corefcn/profiler.cc,
libinterp/corefcn/profiler.h, libinterp/corefcn/pt-jit.cc,
libinterp/corefcn/pt-jit.h, libinterp/corefcn/sighandlers.cc,
libinterp/corefcn/sighandlers.h, libinterp/corefcn/siglist.c,
libinterp/corefcn/siglist.h, libinterp/corefcn/sparse-xdiv.cc,
libinterp/corefcn/sparse-xdiv.h, libinterp/corefcn/sparse-xpow.cc,
libinterp/corefcn/sparse-xpow.h, libinterp/corefcn/symtab.cc,
libinterp/corefcn/symtab.h, libinterp/corefcn/sysdep.cc,
libinterp/corefcn/sysdep.h, libinterp/corefcn/toplev.cc,
libinterp/corefcn/toplev.h, libinterp/corefcn/txt-eng-ft.cc,
libinterp/corefcn/txt-eng-ft.h, libinterp/corefcn/txt-eng.h,
libinterp/corefcn/unwind-prot.cc, libinterp/corefcn/unwind-prot.h,
libinterp/corefcn/utils.cc, libinterp/corefcn/utils.h,
libinterp/corefcn/variables.cc, libinterp/corefcn/variables.h,
libinterp/corefcn/workspace-element.h, libinterp/corefcn/xdiv.cc,
libinterp/corefcn/xdiv.h, libinterp/corefcn/xgl2ps.c,
libinterp/corefcn/xnorm.cc, libinterp/corefcn/xnorm.h,
libinterp/corefcn/xpow.cc, libinterp/corefcn/xpow.h,
libinterp/corefcn/zfstream.cc, libinterp/corefcn/zfstream.h:
Files moved from interp-core and interpfcn directories.
* libinterp/interp-core/Cell.cc, libinterp/interp-core/Cell.h,
libinterp/interp-core/action-container.h,
libinterp/interp-core/c-file-ptr-stream.cc,
libinterp/interp-core/c-file-ptr-stream.h,
libinterp/interp-core/comment-list.cc, libinterp/interp-core/comment-list.h,
libinterp/interp-core/cutils.c, libinterp/interp-core/cutils.h,
libinterp/interp-core/defun-dld.h, libinterp/interp-core/defun-int.h,
libinterp/interp-core/display.cc, libinterp/interp-core/display.h,
libinterp/interp-core/dynamic-ld.cc, libinterp/interp-core/dynamic-ld.h,
libinterp/interp-core/event-queue.h, libinterp/interp-core/gl-render.cc,
libinterp/interp-core/gl-render.h, libinterp/interp-core/gl2ps-renderer.cc,
libinterp/interp-core/gl2ps-renderer.h, libinterp/interp-core/gl2ps.c,
libinterp/interp-core/gl2ps.h, libinterp/interp-core/gripes.cc,
libinterp/interp-core/gripes.h, libinterp/interp-core/jit-ir.cc,
libinterp/interp-core/jit-ir.h, libinterp/interp-core/jit-typeinfo.cc,
libinterp/interp-core/jit-typeinfo.h, libinterp/interp-core/jit-util.cc,
libinterp/interp-core/jit-util.h, libinterp/interp-core/ls-ascii-helper.cc,
libinterp/interp-core/ls-ascii-helper.h, libinterp/interp-core/ls-hdf5.cc,
libinterp/interp-core/ls-hdf5.h, libinterp/interp-core/ls-mat-ascii.cc,
libinterp/interp-core/ls-mat-ascii.h, libinterp/interp-core/ls-mat4.cc,
libinterp/interp-core/ls-mat4.h, libinterp/interp-core/ls-mat5.cc,
libinterp/interp-core/ls-mat5.h, libinterp/interp-core/ls-oct-binary.cc,
libinterp/interp-core/ls-oct-binary.h, libinterp/interp-core/ls-utils.cc,
libinterp/interp-core/ls-utils.h, libinterp/interp-core/matherr.c,
libinterp/interp-core/mex.cc, libinterp/interp-core/mex.h,
libinterp/interp-core/mexproto.h, libinterp/interp-core/module.mk,
libinterp/interp-core/mxarray.in.h, libinterp/interp-core/oct-errno.h,
libinterp/interp-core/oct-errno.in.cc, libinterp/interp-core/oct-fstrm.cc,
libinterp/interp-core/oct-fstrm.h, libinterp/interp-core/oct-hdf5.h,
libinterp/interp-core/oct-iostrm.cc, libinterp/interp-core/oct-iostrm.h,
libinterp/interp-core/oct-lvalue.cc, libinterp/interp-core/oct-lvalue.h,
libinterp/interp-core/oct-map.cc, libinterp/interp-core/oct-map.h,
libinterp/interp-core/oct-obj.cc, libinterp/interp-core/oct-obj.h,
libinterp/interp-core/oct-prcstrm.cc, libinterp/interp-core/oct-prcstrm.h,
libinterp/interp-core/oct-procbuf.cc, libinterp/interp-core/oct-procbuf.h,
libinterp/interp-core/oct-stdstrm.h, libinterp/interp-core/oct-stream.cc,
libinterp/interp-core/oct-stream.h, libinterp/interp-core/oct-strstrm.cc,
libinterp/interp-core/oct-strstrm.h, libinterp/interp-core/oct.h,
libinterp/interp-core/procstream.cc, libinterp/interp-core/procstream.h,
libinterp/interp-core/pt-jit.cc, libinterp/interp-core/pt-jit.h,
libinterp/interp-core/siglist.c, libinterp/interp-core/siglist.h,
libinterp/interp-core/sparse-xdiv.cc, libinterp/interp-core/sparse-xdiv.h,
libinterp/interp-core/sparse-xpow.cc, libinterp/interp-core/sparse-xpow.h,
libinterp/interp-core/txt-eng-ft.cc, libinterp/interp-core/txt-eng-ft.h,
libinterp/interp-core/txt-eng.h, libinterp/interp-core/unwind-prot.cc,
libinterp/interp-core/unwind-prot.h, libinterp/interp-core/xdiv.cc,
libinterp/interp-core/xdiv.h, libinterp/interp-core/xgl2ps.c,
libinterp/interp-core/xnorm.cc, libinterp/interp-core/xnorm.h,
libinterp/interp-core/xpow.cc, libinterp/interp-core/xpow.h,
libinterp/interp-core/zfstream.cc, libinterp/interp-core/zfstream.h,
libinterp/interpfcn/data.cc, libinterp/interpfcn/data.h,
libinterp/interpfcn/debug.cc, libinterp/interpfcn/debug.h,
libinterp/interpfcn/defaults.cc, libinterp/interpfcn/defaults.in.h,
libinterp/interpfcn/defun.cc, libinterp/interpfcn/defun.h,
libinterp/interpfcn/dirfns.cc, libinterp/interpfcn/dirfns.h,
libinterp/interpfcn/error.cc, libinterp/interpfcn/error.h,
libinterp/interpfcn/file-io.cc, libinterp/interpfcn/file-io.h,
libinterp/interpfcn/graphics.cc, libinterp/interpfcn/graphics.in.h,
libinterp/interpfcn/help.cc, libinterp/interpfcn/help.h,
libinterp/interpfcn/hook-fcn.cc, libinterp/interpfcn/hook-fcn.h,
libinterp/interpfcn/input.cc, libinterp/interpfcn/input.h,
libinterp/interpfcn/load-path.cc, libinterp/interpfcn/load-path.h,
libinterp/interpfcn/load-save.cc, libinterp/interpfcn/load-save.h,
libinterp/interpfcn/ls-oct-ascii.cc, libinterp/interpfcn/ls-oct-ascii.h,
libinterp/interpfcn/module.mk, libinterp/interpfcn/oct-hist.cc,
libinterp/interpfcn/oct-hist.h, libinterp/interpfcn/octave-link.cc,
libinterp/interpfcn/octave-link.h, libinterp/interpfcn/pager.cc,
libinterp/interpfcn/pager.h, libinterp/interpfcn/pr-output.cc,
libinterp/interpfcn/pr-output.h, libinterp/interpfcn/profiler.cc,
libinterp/interpfcn/profiler.h, libinterp/interpfcn/sighandlers.cc,
libinterp/interpfcn/sighandlers.h, libinterp/interpfcn/symtab.cc,
libinterp/interpfcn/symtab.h, libinterp/interpfcn/sysdep.cc,
libinterp/interpfcn/sysdep.h, libinterp/interpfcn/toplev.cc,
libinterp/interpfcn/toplev.h, libinterp/interpfcn/utils.cc,
libinterp/interpfcn/utils.h, libinterp/interpfcn/variables.cc,
libinterp/interpfcn/variables.h, libinterp/interpfcn/workspace-element.h:
deleted files.
author | Rik <rik@octave.org> |
---|---|
date | Wed, 03 Jul 2013 17:43:48 -0700 |
parents | libinterp/interp-core/jit-typeinfo.cc@e2de3c8882be |
children | 38bcfd413db0 |
rev | line source |
---|---|
15016 | 1 /* |
2 | |
16768 | 3 Copyright (C) 2012 Max Brister |
15016 | 4 |
5 This file is part of Octave. | |
6 | |
7 Octave is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
9 Free Software Foundation; either version 3 of the License, or (at your | |
10 option) any later version. | |
11 | |
12 Octave is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
18 along with Octave; see the file COPYING. If not, see | |
19 <http://www.gnu.org/licenses/>. | |
20 | |
21 */ | |
22 | |
16768 | 23 // Author: Max Brister <max@2bass.com> |
24 | |
15016 | 25 // defines required by llvm |
26 #define __STDC_LIMIT_MACROS | |
27 #define __STDC_CONSTANT_MACROS | |
28 | |
29 #ifdef HAVE_CONFIG_H | |
30 #include <config.h> | |
31 #endif | |
32 | |
33 #ifdef HAVE_LLVM | |
34 | |
35 #include "jit-typeinfo.h" | |
36 | |
37 #include <llvm/Analysis/Verifier.h> | |
38 #include <llvm/GlobalVariable.h> | |
39 #include <llvm/ExecutionEngine/ExecutionEngine.h> | |
40 #include <llvm/LLVMContext.h> | |
41 #include <llvm/Function.h> | |
42 #include <llvm/Instructions.h> | |
43 #include <llvm/Intrinsics.h> | |
44 #include <llvm/Support/IRBuilder.h> | |
45 #include <llvm/Support/raw_os_ostream.h> | |
46 | |
47 #include "jit-ir.h" | |
48 #include "ov.h" | |
49 #include "ov-builtin.h" | |
50 #include "ov-complex.h" | |
51 #include "ov-scalar.h" | |
52 #include "pager.h" | |
53 | |
54 static llvm::LLVMContext& context = llvm::getGlobalContext (); | |
55 | |
56 jit_typeinfo *jit_typeinfo::instance = 0; | |
57 | |
58 std::ostream& jit_print (std::ostream& os, jit_type *atype) | |
59 { | |
60 if (! atype) | |
61 return os << "null"; | |
62 return os << atype->name (); | |
63 } | |
64 | |
65 // function that jit code calls | |
66 extern "C" void | |
67 octave_jit_print_any (const char *name, octave_base_value *obv) | |
68 { | |
69 obv->print_with_name (octave_stdout, name, true); | |
70 } | |
71 | |
72 extern "C" void | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
73 octave_jit_print_scalar (const char *name, double value) |
15016 | 74 { |
75 // FIXME: We should avoid allocating a new octave_scalar each time | |
76 octave_value ov (value); | |
77 ov.print_with_name (octave_stdout, name); | |
78 } | |
79 | |
80 extern "C" octave_base_value* | |
81 octave_jit_binary_any_any (octave_value::binary_op op, octave_base_value *lhs, | |
82 octave_base_value *rhs) | |
83 { | |
84 octave_value olhs (lhs, true); | |
85 octave_value orhs (rhs, true); | |
86 octave_value result = do_binary_op (op, olhs, orhs); | |
87 octave_base_value *rep = result.internal_rep (); | |
88 rep->grab (); | |
89 return rep; | |
90 } | |
91 | |
92 extern "C" octave_idx_type | |
93 octave_jit_compute_nelem (double base, double limit, double inc) | |
94 { | |
95 Range rng = Range (base, limit, inc); | |
96 return rng.nelem (); | |
97 } | |
98 | |
99 extern "C" void | |
100 octave_jit_release_any (octave_base_value *obv) | |
101 { | |
102 obv->release (); | |
103 } | |
104 | |
105 extern "C" void | |
106 octave_jit_release_matrix (jit_matrix *m) | |
107 { | |
108 delete m->array; | |
109 } | |
110 | |
111 extern "C" octave_base_value * | |
112 octave_jit_grab_any (octave_base_value *obv) | |
113 { | |
114 obv->grab (); | |
115 return obv; | |
116 } | |
117 | |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
118 extern "C" jit_matrix |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
119 octave_jit_grab_matrix (jit_matrix *m) |
15016 | 120 { |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
121 return *m->array; |
15016 | 122 } |
123 | |
124 extern "C" octave_base_value * | |
125 octave_jit_cast_any_matrix (jit_matrix *m) | |
126 { | |
127 octave_value ret (*m->array); | |
128 octave_base_value *rep = ret.internal_rep (); | |
129 rep->grab (); | |
130 delete m->array; | |
131 | |
132 return rep; | |
133 } | |
134 | |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
135 extern "C" jit_matrix |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
136 octave_jit_cast_matrix_any (octave_base_value *obv) |
15016 | 137 { |
138 NDArray m = obv->array_value (); | |
139 obv->release (); | |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
140 return m; |
15016 | 141 } |
142 | |
15027
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
143 extern "C" octave_base_value * |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
144 octave_jit_cast_any_range (jit_range *rng) |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
145 { |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
146 Range temp (*rng); |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
147 octave_value ret (temp); |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
148 octave_base_value *rep = ret.internal_rep (); |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
149 rep->grab (); |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
150 |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
151 return rep; |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
152 } |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
153 extern "C" jit_range |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
154 octave_jit_cast_range_any (octave_base_value *obv) |
15027
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
155 { |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
156 |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
157 jit_range r (obv->range_value ()); |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
158 obv->release (); |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
159 return r; |
15027
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
160 } |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
161 |
15016 | 162 extern "C" double |
163 octave_jit_cast_scalar_any (octave_base_value *obv) | |
164 { | |
165 double ret = obv->double_value (); | |
166 obv->release (); | |
167 return ret; | |
168 } | |
169 | |
170 extern "C" octave_base_value * | |
171 octave_jit_cast_any_scalar (double value) | |
172 { | |
173 return new octave_scalar (value); | |
174 } | |
175 | |
176 extern "C" Complex | |
177 octave_jit_cast_complex_any (octave_base_value *obv) | |
178 { | |
179 Complex ret = obv->complex_value (); | |
180 obv->release (); | |
181 return ret; | |
182 } | |
183 | |
184 extern "C" octave_base_value * | |
185 octave_jit_cast_any_complex (Complex c) | |
186 { | |
187 if (c.imag () == 0) | |
188 return new octave_scalar (c.real ()); | |
189 else | |
190 return new octave_complex (c); | |
191 } | |
192 | |
193 extern "C" void | |
194 octave_jit_gripe_nan_to_logical_conversion (void) | |
195 { | |
196 try | |
197 { | |
198 gripe_nan_to_logical_conversion (); | |
199 } | |
200 catch (const octave_execution_exception&) | |
201 { | |
202 gripe_library_execution_error (); | |
203 } | |
204 } | |
205 | |
206 extern "C" void | |
207 octave_jit_ginvalid_index (void) | |
208 { | |
209 try | |
210 { | |
211 gripe_invalid_index (); | |
212 } | |
213 catch (const octave_execution_exception&) | |
214 { | |
215 gripe_library_execution_error (); | |
216 } | |
217 } | |
218 | |
219 extern "C" void | |
220 octave_jit_gindex_range (int nd, int dim, octave_idx_type iext, | |
221 octave_idx_type ext) | |
222 { | |
223 try | |
224 { | |
225 gripe_index_out_of_range (nd, dim, iext, ext); | |
226 } | |
227 catch (const octave_execution_exception&) | |
228 { | |
229 gripe_library_execution_error (); | |
230 } | |
231 } | |
232 | |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
233 extern "C" jit_matrix |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
234 octave_jit_paren_subsasgn_impl (jit_matrix *mat, octave_idx_type index, |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
235 double value) |
15016 | 236 { |
237 NDArray *array = mat->array; | |
238 if (array->nelem () < index) | |
239 array->resize1 (index); | |
240 | |
241 double *data = array->fortran_vec (); | |
242 data[index - 1] = value; | |
243 | |
244 mat->update (); | |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
245 return *mat; |
15016 | 246 } |
247 | |
15068
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
248 static void |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
249 make_indices (double *indices, octave_idx_type idx_count, |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
250 Array<idx_vector>& result) |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
251 { |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
252 result.resize (dim_vector (1, idx_count)); |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
253 for (octave_idx_type i = 0; i < idx_count; ++i) |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
254 result(i) = idx_vector (indices[i]); |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
255 } |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
256 |
15067 | 257 extern "C" double |
258 octave_jit_paren_scalar (jit_matrix *mat, double *indicies, | |
259 octave_idx_type idx_count) | |
260 { | |
261 // FIXME: Replace this with a more optimal version | |
262 try | |
263 { | |
15068
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
264 Array<idx_vector> idx; |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
265 make_indices (indicies, idx_count, idx); |
15067 | 266 |
267 Array<double> ret = mat->array->index (idx); | |
268 return ret.xelem (0); | |
269 } | |
270 catch (const octave_execution_exception&) | |
271 { | |
272 gripe_library_execution_error (); | |
273 return 0; | |
274 } | |
275 } | |
276 | |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
277 extern "C" jit_matrix |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
278 octave_jit_paren_scalar_subsasgn (jit_matrix *mat, double *indices, |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
279 octave_idx_type idx_count, double value) |
15068
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
280 { |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
281 // FIXME: Replace this with a more optimal version |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
282 jit_matrix ret; |
15068
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
283 try |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
284 { |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
285 Array<idx_vector> idx; |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
286 make_indices (indices, idx_count, idx); |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
287 |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
288 Matrix temp (1, 1); |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
289 temp.xelem(0) = value; |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
290 mat->array->assign (idx, temp); |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
291 ret.update (mat->array); |
15068
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
292 } |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
293 catch (const octave_execution_exception&) |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
294 { |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
295 gripe_library_execution_error (); |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
296 } |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
297 |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
298 return ret; |
15068
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
299 } |
f57d7578c1a6
Support ND matrix indexing with scalar assignment in JIT.
Max Brister <max@2bass.com>
parents:
15067
diff
changeset
|
300 |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
301 extern "C" jit_matrix |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
302 octave_jit_paren_subsasgn_matrix_range (jit_matrix *mat, jit_range *index, |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
303 double value) |
15016 | 304 { |
305 NDArray *array = mat->array; | |
306 bool done = false; | |
307 | |
308 // optimize for the simple case (no resizing and no errors) | |
309 if (*array->jit_ref_count () == 1 | |
310 && index->all_elements_are_ints ()) | |
311 { | |
312 // this code is similar to idx_vector::fill, but we avoid allocating an | |
313 // idx_vector and its associated rep | |
314 octave_idx_type start = static_cast<octave_idx_type> (index->base) - 1; | |
315 octave_idx_type step = static_cast<octave_idx_type> (index->inc); | |
316 octave_idx_type nelem = index->nelem; | |
317 octave_idx_type final = start + nelem * step; | |
318 if (step < 0) | |
319 { | |
320 step = -step; | |
321 std::swap (final, start); | |
322 } | |
323 | |
324 if (start >= 0 && final < mat->slice_len) | |
325 { | |
326 done = true; | |
327 | |
328 double *data = array->jit_slice_data (); | |
329 if (step == 1) | |
330 std::fill (data + start, data + start + nelem, value); | |
331 else | |
332 { | |
333 for (octave_idx_type i = start; i < final; i += step) | |
334 data[i] = value; | |
335 } | |
336 } | |
337 } | |
338 | |
339 if (! done) | |
340 { | |
341 idx_vector idx (*index); | |
342 NDArray avalue (dim_vector (1, 1)); | |
343 avalue.xelem (0) = value; | |
344 array->assign (idx, avalue); | |
345 } | |
346 | |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
347 jit_matrix ret; |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
348 ret.update (array); |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
349 return ret; |
15016 | 350 } |
351 | |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
352 extern "C" double |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
353 octave_jit_end_matrix (jit_matrix *mat, octave_idx_type idx, |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
354 octave_idx_type count) |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
355 { |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
356 octave_idx_type ndim = mat->dimensions[-1]; |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
357 if (ndim == count) |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
358 return mat->dimensions[idx]; |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
359 else if (ndim > count) |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
360 { |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
361 if (idx == count - 1) |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
362 { |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
363 double ret = mat->dimensions[idx]; |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
364 for (octave_idx_type i = idx + 1; i < ndim; ++i) |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
365 ret *= mat->dimensions[idx]; |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
366 return ret; |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
367 } |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
368 |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
369 return mat->dimensions[idx]; |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
370 } |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
371 else // ndim < count |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
372 return idx < ndim ? mat->dimensions[idx] : 1; |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
373 } |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
374 |
15337
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
375 extern "C" octave_base_value * |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
376 octave_jit_create_undef (void) |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
377 { |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
378 octave_value undef; |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
379 octave_base_value *ret = undef.internal_rep (); |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
380 ret->grab (); |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
381 |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
382 return ret; |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
383 } |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
384 |
15016 | 385 extern "C" Complex |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
386 octave_jit_complex_mul (Complex lhs, Complex rhs) |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
387 { |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
388 if (lhs.imag () == 0 && rhs.imag() == 0) |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
389 return Complex (lhs.real () * rhs.real (), 0); |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
390 |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
391 return lhs * rhs; |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
392 } |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
393 |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
394 extern "C" Complex |
15016 | 395 octave_jit_complex_div (Complex lhs, Complex rhs) |
396 { | |
397 // see src/OPERATORS/op-cs-cs.cc | |
398 if (rhs == 0.0) | |
399 gripe_divide_by_zero (); | |
400 | |
401 return lhs / rhs; | |
402 } | |
403 | |
404 // FIXME: CP form src/xpow.cc | |
405 static inline int | |
406 xisint (double x) | |
407 { | |
408 return (D_NINT (x) == x | |
15215
9020dddc925a
use std::numeric_limits for integer max and min values
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
409 && ((x >= 0 && x < std::numeric_limits<int>::max ()) |
15219
aeba1adfd76b
Correct small typo in jit-typeinfo.cc
Melvin Robinson <melvin.robinson@mavs.uta.edu>
parents:
15215
diff
changeset
|
410 || (x <= 0 && x > std::numeric_limits<int>::min ()))); |
15016 | 411 } |
412 | |
413 extern "C" Complex | |
414 octave_jit_pow_scalar_scalar (double lhs, double rhs) | |
415 { | |
416 // FIXME: almost CP from src/xpow.cc | |
417 if (lhs < 0.0 && ! xisint (rhs)) | |
418 return std::pow (Complex (lhs), rhs); | |
419 return std::pow (lhs, rhs); | |
420 } | |
421 | |
422 extern "C" Complex | |
423 octave_jit_pow_complex_complex (Complex lhs, Complex rhs) | |
424 { | |
425 if (lhs.imag () == 0 && rhs.imag () == 0) | |
426 return octave_jit_pow_scalar_scalar (lhs.real (), rhs.real ()); | |
427 return std::pow (lhs, rhs); | |
428 } | |
429 | |
430 extern "C" Complex | |
431 octave_jit_pow_complex_scalar (Complex lhs, double rhs) | |
432 { | |
433 if (lhs.imag () == 0) | |
434 return octave_jit_pow_scalar_scalar (lhs.real (), rhs); | |
435 return std::pow (lhs, rhs); | |
436 } | |
437 | |
438 extern "C" Complex | |
439 octave_jit_pow_scalar_complex (double lhs, Complex rhs) | |
440 { | |
441 if (rhs.imag () == 0) | |
442 return octave_jit_pow_scalar_scalar (lhs, rhs.real ()); | |
443 return std::pow (lhs, rhs); | |
444 } | |
445 | |
446 extern "C" void | |
447 octave_jit_print_matrix (jit_matrix *m) | |
448 { | |
449 std::cout << *m << std::endl; | |
450 } | |
451 | |
452 static void | |
453 gripe_bad_result (void) | |
454 { | |
455 error ("incorrect type information given to the JIT compiler"); | |
456 } | |
457 | |
458 // FIXME: Add support for multiple outputs | |
459 extern "C" octave_base_value * | |
460 octave_jit_call (octave_builtin::fcn fn, size_t nargin, | |
461 octave_base_value **argin, jit_type *result_type) | |
462 { | |
463 octave_value_list ovl (nargin); | |
464 for (size_t i = 0; i < nargin; ++i) | |
465 ovl.xelem (i) = octave_value (argin[i]); | |
466 | |
467 ovl = fn (ovl, 1); | |
468 | |
15169
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
469 // FIXME: Check result_type somehow |
15016 | 470 if (result_type) |
471 { | |
15169
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
472 if (ovl.length () < 1) |
15016 | 473 { |
474 gripe_bad_result (); | |
475 return 0; | |
476 } | |
477 | |
15169
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
478 octave_value result = ovl.xelem(0); |
15016 | 479 octave_base_value *ret = result.internal_rep (); |
480 ret->grab (); | |
481 return ret; | |
482 } | |
483 | |
484 if (! (ovl.length () == 0 | |
485 || (ovl.length () == 1 && ovl.xelem (0).is_undefined ()))) | |
486 gripe_bad_result (); | |
487 | |
488 return 0; | |
489 } | |
490 | |
491 // -------------------- jit_range -------------------- | |
492 bool | |
493 jit_range::all_elements_are_ints () const | |
494 { | |
495 Range r (*this); | |
496 return r.all_elements_are_ints (); | |
497 } | |
498 | |
499 std::ostream& | |
500 operator<< (std::ostream& os, const jit_range& rng) | |
501 { | |
502 return os << "Range[" << rng.base << ", " << rng.limit << ", " << rng.inc | |
503 << ", " << rng.nelem << "]"; | |
504 } | |
505 | |
506 // -------------------- jit_matrix -------------------- | |
507 | |
508 std::ostream& | |
509 operator<< (std::ostream& os, const jit_matrix& mat) | |
510 { | |
511 return os << "Matrix[" << mat.ref_count << ", " << mat.slice_data << ", " | |
512 << mat.slice_len << ", " << mat.dimensions << ", " | |
513 << mat.array << "]"; | |
514 } | |
515 | |
516 // -------------------- jit_type -------------------- | |
517 jit_type::jit_type (const std::string& aname, jit_type *aparent, | |
15124
0464e3ceb85b
Skip functions when resolving end context in JIT
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
518 llvm::Type *allvm_type, bool askip_paren, int aid) : |
15016 | 519 mname (aname), mparent (aparent), llvm_type (allvm_type), mid (aid), |
15124
0464e3ceb85b
Skip functions when resolving end context in JIT
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
520 mdepth (aparent ? aparent->mdepth + 1 : 0), mskip_paren (askip_paren) |
15016 | 521 { |
522 std::memset (msret, 0, sizeof (msret)); | |
523 std::memset (mpointer_arg, 0, sizeof (mpointer_arg)); | |
524 std::memset (mpack, 0, sizeof (mpack)); | |
525 std::memset (munpack, 0, sizeof (munpack)); | |
526 | |
527 for (size_t i = 0; i < jit_convention::length; ++i) | |
528 mpacked_type[i] = llvm_type; | |
529 } | |
530 | |
531 llvm::Type * | |
532 jit_type::to_llvm_arg (void) const | |
533 { | |
534 return llvm_type ? llvm_type->getPointerTo () : 0; | |
535 } | |
536 | |
537 // -------------------- jit_function -------------------- | |
538 jit_function::jit_function () : module (0), llvm_function (0), mresult (0), | |
539 call_conv (jit_convention::length), | |
540 mcan_error (false) | |
541 {} | |
542 | |
543 jit_function::jit_function (llvm::Module *amodule, | |
544 jit_convention::type acall_conv, | |
545 const llvm::Twine& aname, jit_type *aresult, | |
546 const std::vector<jit_type *>& aargs) | |
547 : module (amodule), mresult (aresult), args (aargs), call_conv (acall_conv), | |
548 mcan_error (false) | |
549 { | |
550 llvm::SmallVector<llvm::Type *, 15> llvm_args; | |
551 | |
552 llvm::Type *rtype = llvm::Type::getVoidTy (context); | |
553 if (mresult) | |
554 { | |
555 rtype = mresult->packed_type (call_conv); | |
556 if (sret ()) | |
557 { | |
558 llvm_args.push_back (rtype->getPointerTo ()); | |
559 rtype = llvm::Type::getVoidTy (context); | |
560 } | |
561 } | |
562 | |
563 for (std::vector<jit_type *>::const_iterator iter = args.begin (); | |
564 iter != args.end (); ++iter) | |
565 { | |
566 jit_type *ty = *iter; | |
567 assert (ty); | |
568 llvm::Type *argty = ty->packed_type (call_conv); | |
569 if (ty->pointer_arg (call_conv)) | |
570 argty = argty->getPointerTo (); | |
571 | |
572 llvm_args.push_back (argty); | |
573 } | |
574 | |
575 // we mark all functinos as external linkage because this prevents llvm | |
576 // from getting rid of always inline functions | |
577 llvm::FunctionType *ft = llvm::FunctionType::get (rtype, llvm_args, false); | |
578 llvm_function = llvm::Function::Create (ft, llvm::Function::ExternalLinkage, | |
579 aname, module); | |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
580 |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
581 if (sret ()) |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
582 llvm_function->addAttribute (1, llvm::Attribute::StructRet); |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
583 |
15016 | 584 if (call_conv == jit_convention::internal) |
585 llvm_function->addFnAttr (llvm::Attribute::AlwaysInline); | |
586 } | |
587 | |
588 jit_function::jit_function (const jit_function& fn, jit_type *aresult, | |
589 const std::vector<jit_type *>& aargs) | |
590 : module (fn.module), llvm_function (fn.llvm_function), mresult (aresult), | |
591 args (aargs), call_conv (fn.call_conv), mcan_error (fn.mcan_error) | |
592 { | |
593 } | |
594 | |
595 jit_function::jit_function (const jit_function& fn) | |
596 : module (fn.module), llvm_function (fn.llvm_function), mresult (fn.mresult), | |
597 args (fn.args), call_conv (fn.call_conv), mcan_error (fn.mcan_error) | |
598 {} | |
599 | |
15385
8ccb187b24e9
Erase partially created functions on JIT compilation failure (bug #37308)
Max Brister <max@2bass.com>
parents:
15370
diff
changeset
|
600 void |
8ccb187b24e9
Erase partially created functions on JIT compilation failure (bug #37308)
Max Brister <max@2bass.com>
parents:
15370
diff
changeset
|
601 jit_function::erase (void) |
8ccb187b24e9
Erase partially created functions on JIT compilation failure (bug #37308)
Max Brister <max@2bass.com>
parents:
15370
diff
changeset
|
602 { |
8ccb187b24e9
Erase partially created functions on JIT compilation failure (bug #37308)
Max Brister <max@2bass.com>
parents:
15370
diff
changeset
|
603 if (! llvm_function) |
8ccb187b24e9
Erase partially created functions on JIT compilation failure (bug #37308)
Max Brister <max@2bass.com>
parents:
15370
diff
changeset
|
604 return; |
8ccb187b24e9
Erase partially created functions on JIT compilation failure (bug #37308)
Max Brister <max@2bass.com>
parents:
15370
diff
changeset
|
605 |
8ccb187b24e9
Erase partially created functions on JIT compilation failure (bug #37308)
Max Brister <max@2bass.com>
parents:
15370
diff
changeset
|
606 llvm_function->eraseFromParent (); |
8ccb187b24e9
Erase partially created functions on JIT compilation failure (bug #37308)
Max Brister <max@2bass.com>
parents:
15370
diff
changeset
|
607 llvm_function = 0; |
8ccb187b24e9
Erase partially created functions on JIT compilation failure (bug #37308)
Max Brister <max@2bass.com>
parents:
15370
diff
changeset
|
608 } |
8ccb187b24e9
Erase partially created functions on JIT compilation failure (bug #37308)
Max Brister <max@2bass.com>
parents:
15370
diff
changeset
|
609 |
15016 | 610 std::string |
611 jit_function::name (void) const | |
612 { | |
613 return llvm_function->getName (); | |
614 } | |
615 | |
616 llvm::BasicBlock * | |
617 jit_function::new_block (const std::string& aname, | |
618 llvm::BasicBlock *insert_before) | |
619 { | |
620 return llvm::BasicBlock::Create (context, aname, llvm_function, | |
621 insert_before); | |
622 } | |
623 | |
624 llvm::Value * | |
625 jit_function::call (llvm::IRBuilderD& builder, | |
626 const std::vector<jit_value *>& in_args) const | |
627 { | |
15056
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
628 if (! valid ()) |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
629 throw jit_fail_exception ("Call not implemented"); |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
630 |
15016 | 631 assert (in_args.size () == args.size ()); |
632 std::vector<llvm::Value *> llvm_args (args.size ()); | |
633 for (size_t i = 0; i < in_args.size (); ++i) | |
634 llvm_args[i] = in_args[i]->to_llvm (); | |
635 | |
636 return call (builder, llvm_args); | |
637 } | |
638 | |
639 llvm::Value * | |
640 jit_function::call (llvm::IRBuilderD& builder, | |
641 const std::vector<llvm::Value *>& in_args) const | |
642 { | |
15056
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
643 if (! valid ()) |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
644 throw jit_fail_exception ("Call not implemented"); |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
645 |
15016 | 646 assert (in_args.size () == args.size ()); |
647 llvm::SmallVector<llvm::Value *, 10> llvm_args; | |
648 llvm_args.reserve (in_args.size () + sret ()); | |
649 | |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
650 llvm::BasicBlock *insert_block = builder.GetInsertBlock (); |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
651 llvm::Function *parent = insert_block->getParent (); |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
652 assert (parent); |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
653 |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
654 // we insert allocas inside the prelude block to prevent stack overflows |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
655 llvm::BasicBlock& prelude = parent->getEntryBlock (); |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
656 llvm::IRBuilder<> pre_builder (&prelude, prelude.begin ()); |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
657 |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
658 llvm::AllocaInst *sret_mem = 0; |
15016 | 659 if (sret ()) |
660 { | |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
661 sret_mem = pre_builder.CreateAlloca (mresult->packed_type (call_conv)); |
15016 | 662 llvm_args.push_back (sret_mem); |
663 } | |
664 | |
665 for (size_t i = 0; i < in_args.size (); ++i) | |
666 { | |
667 llvm::Value *arg = in_args[i]; | |
668 jit_type::convert_fn convert = args[i]->pack (call_conv); | |
669 if (convert) | |
670 arg = convert (builder, arg); | |
671 | |
672 if (args[i]->pointer_arg (call_conv)) | |
673 { | |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
674 llvm::Type *ty = args[i]->packed_type (call_conv); |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
675 llvm::Value *alloca = pre_builder.CreateAlloca (ty); |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
676 builder.CreateStore (arg, alloca); |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
677 arg = alloca; |
15016 | 678 } |
679 | |
680 llvm_args.push_back (arg); | |
681 } | |
682 | |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
683 llvm::CallInst *callinst = builder.CreateCall (llvm_function, llvm_args); |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
684 llvm::Value *ret = callinst; |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
685 |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
686 if (sret ()) |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
687 { |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
688 callinst->addAttribute (1, llvm::Attribute::StructRet); |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
689 ret = builder.CreateLoad (sret_mem); |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
690 } |
15016 | 691 |
692 if (mresult) | |
693 { | |
694 jit_type::convert_fn unpack = mresult->unpack (call_conv); | |
695 if (unpack) | |
696 ret = unpack (builder, ret); | |
697 } | |
698 | |
699 return ret; | |
700 } | |
701 | |
702 llvm::Value * | |
703 jit_function::argument (llvm::IRBuilderD& builder, size_t idx) const | |
704 { | |
705 assert (idx < args.size ()); | |
706 | |
707 // FIXME: We should be treating arguments like a list, not a vector. Shouldn't | |
708 // matter much for now, as the number of arguments shouldn't be much bigger | |
709 // than 4 | |
710 llvm::Function::arg_iterator iter = llvm_function->arg_begin (); | |
711 if (sret ()) | |
712 ++iter; | |
713 | |
714 for (size_t i = 0; i < idx; ++i, ++iter); | |
715 | |
716 if (args[idx]->pointer_arg (call_conv)) | |
717 return builder.CreateLoad (iter); | |
718 | |
719 return iter; | |
720 } | |
721 | |
722 void | |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
723 jit_function::do_return (llvm::IRBuilderD& builder, llvm::Value *rval, |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
724 bool verify) |
15016 | 725 { |
726 assert (! rval == ! mresult); | |
727 | |
728 if (rval) | |
729 { | |
730 jit_type::convert_fn convert = mresult->pack (call_conv); | |
731 if (convert) | |
732 rval = convert (builder, rval); | |
733 | |
734 if (sret ()) | |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
735 { |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
736 builder.CreateStore (rval, llvm_function->arg_begin ()); |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
737 builder.CreateRetVoid (); |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
738 } |
15016 | 739 else |
740 builder.CreateRet (rval); | |
741 } | |
742 else | |
743 builder.CreateRetVoid (); | |
744 | |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
745 if (verify) |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
746 llvm::verifyFunction (*llvm_function); |
15016 | 747 } |
748 | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
749 void |
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
750 jit_function::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
|
751 { |
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
752 assert (valid ()); |
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
753 engine->addGlobalMapping (llvm_function, fn); |
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
754 } |
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
755 |
15016 | 756 std::ostream& |
757 operator<< (std::ostream& os, const jit_function& fn) | |
758 { | |
759 llvm::Function *lfn = fn.to_llvm (); | |
760 os << "jit_function: cc=" << fn.call_conv; | |
761 llvm::raw_os_ostream llvm_out (os); | |
762 lfn->print (llvm_out); | |
763 llvm_out.flush (); | |
764 return os; | |
765 } | |
766 | |
767 // -------------------- jit_operation -------------------- | |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
768 jit_operation::~jit_operation (void) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
769 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
770 for (generated_map::iterator iter = generated.begin (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
771 iter != generated.end (); ++iter) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
772 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
773 delete iter->first; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
774 delete iter->second; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
775 } |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
776 } |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
777 |
15016 | 778 void |
779 jit_operation::add_overload (const jit_function& func, | |
780 const std::vector<jit_type*>& args) | |
781 { | |
782 if (args.size () >= overloads.size ()) | |
783 overloads.resize (args.size () + 1); | |
784 | |
785 Array<jit_function>& over = overloads[args.size ()]; | |
786 dim_vector dv (over.dims ()); | |
787 Array<octave_idx_type> idx = to_idx (args); | |
788 bool must_resize = false; | |
789 | |
790 if (dv.length () != idx.numel ()) | |
791 { | |
792 dv.resize (idx.numel ()); | |
793 must_resize = true; | |
794 } | |
795 | |
796 for (octave_idx_type i = 0; i < dv.length (); ++i) | |
797 if (dv(i) <= idx(i)) | |
798 { | |
799 must_resize = true; | |
800 dv(i) = idx(i) + 1; | |
801 } | |
802 | |
803 if (must_resize) | |
804 over.resize (dv); | |
805 | |
806 over(idx) = func; | |
807 } | |
808 | |
809 const jit_function& | |
810 jit_operation::overload (const std::vector<jit_type*>& types) const | |
811 { | |
812 static jit_function null_overload; | |
813 for (size_t i =0; i < types.size (); ++i) | |
814 if (! types[i]) | |
815 return null_overload; | |
816 | |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
817 if (types.size () >= overloads.size ()) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
818 return do_generate (types); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
819 |
15016 | 820 const Array<jit_function>& over = overloads[types.size ()]; |
821 dim_vector dv (over.dims ()); | |
822 Array<octave_idx_type> idx = to_idx (types); | |
823 for (octave_idx_type i = 0; i < dv.length (); ++i) | |
824 if (idx(i) >= dv(i)) | |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
825 return do_generate (types); |
15016 | 826 |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
827 const jit_function& ret = over(idx); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
828 if (! ret.valid ()) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
829 return do_generate (types); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
830 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
831 return ret; |
15016 | 832 } |
833 | |
834 Array<octave_idx_type> | |
835 jit_operation::to_idx (const std::vector<jit_type*>& types) const | |
836 { | |
837 octave_idx_type numel = types.size (); | |
15337
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
838 numel = std::max (2, numel); |
15016 | 839 |
840 Array<octave_idx_type> idx (dim_vector (1, numel)); | |
841 for (octave_idx_type i = 0; i < static_cast<octave_idx_type> (types.size ()); | |
842 ++i) | |
843 idx(i) = types[i]->type_id (); | |
844 | |
15337
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
845 if (types.size () == 0) |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
846 idx(0) = idx(1) = 0; |
15016 | 847 if (types.size () == 1) |
848 { | |
849 idx(1) = idx(0); | |
850 idx(0) = 0; | |
851 } | |
852 | |
853 return idx; | |
854 } | |
855 | |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
856 const jit_function& |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
857 jit_operation::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
|
858 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
859 static jit_function null_overload; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
860 generated_map::const_iterator find = generated.find (&types); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
861 if (find != generated.end ()) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
862 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
863 if (find->second) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
864 return *find->second; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
865 else |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
866 return null_overload; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
867 } |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
868 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
869 jit_function *ret = generate (types); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
870 generated[new signature_vec (types)] = ret; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
871 return ret ? *ret : null_overload; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
872 } |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
873 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
874 jit_function * |
15135
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
875 jit_operation::generate (const signature_vec&) const |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
876 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
877 return 0; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
878 } |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
879 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
880 bool |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
881 jit_operation::signature_cmp |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
882 ::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
|
883 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
884 const signature_vec& l = *lhs; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
885 const signature_vec& r = *rhs; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
886 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
887 if (l.size () < r.size ()) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
888 return true; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
889 else if (l.size () > r.size ()) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
890 return false; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
891 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
892 for (size_t i = 0; i < l.size (); ++i) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
893 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
894 if (l[i]->type_id () < r[i]->type_id ()) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
895 return true; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
896 else if (l[i]->type_id () > r[i]->type_id ()) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
897 return false; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
898 } |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
899 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
900 return false; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
901 } |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
902 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
903 // -------------------- jit_index_operation -------------------- |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
904 jit_function * |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
905 jit_index_operation::generate (const signature_vec& types) const |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
906 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
907 if (types.size () > 2 && types[0] == jit_typeinfo::get_matrix ()) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
908 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
909 // indexing a matrix with scalars |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
910 jit_type *scalar = jit_typeinfo::get_scalar (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
911 for (size_t i = 1; i < types.size (); ++i) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
912 if (types[i] != scalar) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
913 return 0; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
914 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
915 return generate_matrix (types); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
916 } |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
917 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
918 return 0; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
919 } |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
920 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
921 llvm::Value * |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
922 jit_index_operation::create_arg_array (llvm::IRBuilderD& builder, |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
923 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
|
924 size_t end_idx) const |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
925 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
926 size_t n = end_idx - start_idx; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
927 llvm::Type *scalar_t = jit_typeinfo::get_scalar_llvm (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
928 llvm::ArrayType *array_t = llvm::ArrayType::get (scalar_t, n); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
929 llvm::Value *array = llvm::UndefValue::get (array_t); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
930 for (size_t i = start_idx; i < end_idx; ++i) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
931 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
932 llvm::Value *idx = fn.argument (builder, i); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
933 array = builder.CreateInsertValue (array, idx, i - start_idx); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
934 } |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
935 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
936 llvm::Value *array_mem = builder.CreateAlloca (array_t); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
937 builder.CreateStore (array, array_mem); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
938 return builder.CreateBitCast (array_mem, scalar_t->getPointerTo ()); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
939 } |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
940 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
941 // -------------------- jit_paren_subsref -------------------- |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
942 jit_function * |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
943 jit_paren_subsref::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
|
944 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
945 std::stringstream ss; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
946 ss << "jit_paren_subsref_matrix_scalar" << (types.size () - 1); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
947 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
948 jit_type *scalar = jit_typeinfo::get_scalar (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
949 jit_function *fn = new jit_function (module, jit_convention::internal, |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
950 ss.str (), scalar, types); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
951 fn->mark_can_error (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
952 llvm::BasicBlock *body = fn->new_block (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
953 llvm::IRBuilder<> builder (body); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
954 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
955 llvm::Value *array = create_arg_array (builder, *fn, 1, types.size ()); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
956 jit_type *index = jit_typeinfo::get_index (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
957 llvm::Value *nelem = llvm::ConstantInt::get (index->to_llvm (), |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
958 types.size () - 1); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
959 llvm::Value *mat = fn->argument (builder, 0); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
960 llvm::Value *ret = paren_scalar.call (builder, mat, array, nelem); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
961 fn->do_return (builder, ret); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
962 return fn; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
963 } |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
964 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
965 void |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
966 jit_paren_subsref::do_initialize (void) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
967 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
968 std::vector<jit_type *> types (3); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
969 types[0] = jit_typeinfo::get_matrix (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
970 types[1] = jit_typeinfo::get_scalar_ptr (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
971 types[2] = jit_typeinfo::get_index (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
972 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
973 jit_type *scalar = jit_typeinfo::get_scalar (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
974 paren_scalar = jit_function (module, jit_convention::external, |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
975 "octave_jit_paren_scalar", scalar, types); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
976 paren_scalar.add_mapping (engine, &octave_jit_paren_scalar); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
977 paren_scalar.mark_can_error (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
978 } |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
979 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
980 // -------------------- jit_paren_subsasgn -------------------- |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
981 jit_function * |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
982 jit_paren_subsasgn::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
|
983 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
984 std::stringstream ss; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
985 ss << "jit_paren_subsasgn_matrix_scalar" << (types.size () - 2); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
986 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
987 jit_type *matrix = jit_typeinfo::get_matrix (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
988 jit_function *fn = new jit_function (module, jit_convention::internal, |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
989 ss.str (), matrix, types); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
990 fn->mark_can_error (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
991 llvm::BasicBlock *body = fn->new_block (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
992 llvm::IRBuilder<> builder (body); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
993 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
994 llvm::Value *array = create_arg_array (builder, *fn, 1, types.size () - 1); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
995 jit_type *index = jit_typeinfo::get_index (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
996 llvm::Value *nelem = llvm::ConstantInt::get (index->to_llvm (), |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
997 types.size () - 2); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
998 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
999 llvm::Value *mat = fn->argument (builder, 0); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
1000 llvm::Value *value = fn->argument (builder, types.size () - 1); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
1001 llvm::Value *ret = paren_scalar.call (builder, mat, array, nelem, value); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
1002 fn->do_return (builder, ret); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
1003 return fn; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
1004 } |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
1005 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
1006 void |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
1007 jit_paren_subsasgn::do_initialize (void) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
1008 { |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
1009 if (paren_scalar.valid ()) |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
1010 return; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
1011 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
1012 jit_type *matrix = jit_typeinfo::get_matrix (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
1013 std::vector<jit_type *> types (4); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
1014 types[0] = matrix; |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
1015 types[1] = jit_typeinfo::get_scalar_ptr (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
1016 types[2] = jit_typeinfo::get_index (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
1017 types[3] = jit_typeinfo::get_scalar (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
1018 |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
1019 paren_scalar = jit_function (module, jit_convention::external, |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
1020 "octave_jit_paren_scalar", matrix, types); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
1021 paren_scalar.add_mapping (engine, &octave_jit_paren_scalar_subsasgn); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
1022 paren_scalar.mark_can_error (); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
1023 } |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
1024 |
15016 | 1025 // -------------------- jit_typeinfo -------------------- |
1026 void | |
1027 jit_typeinfo::initialize (llvm::Module *m, llvm::ExecutionEngine *e) | |
1028 { | |
1029 new jit_typeinfo (m, e); | |
1030 } | |
1031 | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1032 // wrap function names to simplify jit_typeinfo::create_external |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1033 #define JIT_FN(fn) engine, &fn, #fn |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1034 |
15016 | 1035 jit_typeinfo::jit_typeinfo (llvm::Module *m, llvm::ExecutionEngine *e) |
1036 : module (m), engine (e), next_id (0), | |
1037 builder (*new llvm::IRBuilderD (context)) | |
1038 { | |
1039 instance = this; | |
1040 | |
1041 // FIXME: We should be registering types like in octave_value_typeinfo | |
1042 llvm::Type *any_t = llvm::StructType::create (context, "octave_base_value"); | |
1043 any_t = any_t->getPointerTo (); | |
1044 | |
1045 llvm::Type *scalar_t = llvm::Type::getDoubleTy (context); | |
1046 llvm::Type *bool_t = llvm::Type::getInt1Ty (context); | |
1047 llvm::Type *string_t = llvm::Type::getInt8Ty (context); | |
1048 string_t = string_t->getPointerTo (); | |
1049 llvm::Type *index_t = llvm::Type::getIntNTy (context, | |
1050 sizeof(octave_idx_type) * 8); | |
1051 | |
1052 llvm::StructType *range_t = llvm::StructType::create (context, "range"); | |
1053 std::vector<llvm::Type *> range_contents (4, scalar_t); | |
1054 range_contents[3] = index_t; | |
1055 range_t->setBody (range_contents); | |
1056 | |
1057 llvm::Type *refcount_t = llvm::Type::getIntNTy (context, sizeof(int) * 8); | |
1058 | |
1059 llvm::StructType *matrix_t = llvm::StructType::create (context, "matrix"); | |
1060 llvm::Type *matrix_contents[5]; | |
1061 matrix_contents[0] = refcount_t->getPointerTo (); | |
1062 matrix_contents[1] = scalar_t->getPointerTo (); | |
1063 matrix_contents[2] = index_t; | |
1064 matrix_contents[3] = index_t->getPointerTo (); | |
1065 matrix_contents[4] = string_t; | |
1066 matrix_t->setBody (llvm::makeArrayRef (matrix_contents, 5)); | |
1067 | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1068 llvm::Type *complex_t = llvm::ArrayType::get (scalar_t, 2); |
15016 | 1069 |
1070 // complex_ret is what is passed to C functions in order to get calling | |
1071 // convention right | |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
1072 llvm::Type *cmplx_inner_cont[] = {scalar_t, scalar_t}; |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
1073 llvm::StructType *cmplx_inner = llvm::StructType::create (cmplx_inner_cont); |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
1074 |
15016 | 1075 complex_ret = llvm::StructType::create (context, "complex_ret"); |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
1076 { |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
1077 llvm::Type *contents[] = {cmplx_inner}; |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
1078 complex_ret->setBody (contents); |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
1079 } |
15016 | 1080 |
1081 // create types | |
1082 any = new_type ("any", 0, any_t); | |
1083 matrix = new_type ("matrix", any, matrix_t); | |
1084 complex = new_type ("complex", any, complex_t); | |
1085 scalar = new_type ("scalar", complex, scalar_t); | |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
1086 scalar_ptr = new_type ("scalar_ptr", 0, scalar_t->getPointerTo ()); |
15135
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
1087 any_ptr = new_type ("any_ptr", 0, any_t->getPointerTo ()); |
15016 | 1088 range = new_type ("range", any, range_t); |
1089 string = new_type ("string", any, string_t); | |
1090 boolean = new_type ("bool", any, bool_t); | |
1091 index = new_type ("index", any, index_t); | |
1092 | |
1093 create_int (8); | |
1094 create_int (16); | |
1095 create_int (32); | |
1096 create_int (64); | |
1097 | |
1098 casts.resize (next_id + 1); | |
1099 identities.resize (next_id + 1); | |
1100 | |
1101 // specify calling conventions | |
1102 // FIXME: We should detect architecture and do something sane based on that | |
1103 // here we assume x86 or x86_64 | |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
1104 matrix->mark_sret (jit_convention::external); |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
1105 matrix->mark_pointer_arg (jit_convention::external); |
15016 | 1106 |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
1107 range->mark_sret (jit_convention::external); |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
1108 range->mark_pointer_arg (jit_convention::external); |
15016 | 1109 |
1110 complex->set_pack (jit_convention::external, &jit_typeinfo::pack_complex); | |
1111 complex->set_unpack (jit_convention::external, &jit_typeinfo::unpack_complex); | |
1112 complex->set_packed_type (jit_convention::external, complex_ret); | |
1113 | |
1114 if (sizeof (void *) == 4) | |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
1115 complex->mark_sret (jit_convention::external); |
15016 | 1116 |
15078
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
1117 paren_subsref_fn.initialize (module, engine); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
1118 paren_subsasgn_fn.initialize (module, engine); |
fe4752f772e2
Generate ND indexing functions on demand in JIT.
Max Brister <max@2bass.com>
parents:
15068
diff
changeset
|
1119 |
15016 | 1120 // bind global variables |
1121 lerror_state = new llvm::GlobalVariable (*module, bool_t, false, | |
1122 llvm::GlobalValue::ExternalLinkage, | |
1123 0, "error_state"); | |
1124 engine->addGlobalMapping (lerror_state, | |
1125 reinterpret_cast<void *> (&error_state)); | |
1126 | |
15603 | 1127 // sig_atomic_type is going to be some sort of integer |
1128 sig_atomic_type = llvm::Type::getIntNTy (context, sizeof(sig_atomic_t) * 8); | |
1129 loctave_interrupt_state | |
1130 = new llvm::GlobalVariable (*module, sig_atomic_type, false, | |
1131 llvm::GlobalValue::ExternalLinkage, 0, | |
1132 "octave_interrupt_state"); | |
1133 engine->addGlobalMapping (loctave_interrupt_state, | |
1134 reinterpret_cast<void *> (&octave_interrupt_state)); | |
1135 | |
15135
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
1136 // generic call function |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
1137 { |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
1138 jit_type *int_t = intN (sizeof (octave_builtin::fcn) * 8); |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1139 any_call = create_external (JIT_FN (octave_jit_call), any, int_t, int_t, |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1140 any_ptr, int_t); |
15135
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
1141 } |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
1142 |
15016 | 1143 // any with anything is an any op |
1144 jit_function fn; | |
1145 jit_type *binary_op_type = intN (sizeof (octave_value::binary_op) * 8); | |
1146 llvm::Type *llvm_bo_type = binary_op_type->to_llvm (); | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1147 jit_function any_binary = create_external (JIT_FN (octave_jit_binary_any_any), |
15016 | 1148 any, binary_op_type, any, any); |
1149 any_binary.mark_can_error (); | |
1150 binary_ops.resize (octave_value::num_binary_ops); | |
1151 for (size_t i = 0; i < octave_value::num_binary_ops; ++i) | |
1152 { | |
1153 octave_value::binary_op op = static_cast<octave_value::binary_op> (i); | |
1154 std::string op_name = octave_value::binary_op_as_string (op); | |
1155 binary_ops[i].stash_name ("binary" + op_name); | |
1156 } | |
1157 | |
15146
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1158 unary_ops.resize (octave_value::num_unary_ops); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1159 for (size_t i = 0; i < octave_value::num_unary_ops; ++i) |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1160 { |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1161 octave_value::unary_op op = static_cast<octave_value::unary_op> (i); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1162 std::string op_name = octave_value::unary_op_as_string (op); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1163 unary_ops[i].stash_name ("unary" + op_name); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1164 } |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1165 |
15016 | 1166 for (int op = 0; op < octave_value::num_binary_ops; ++op) |
1167 { | |
1168 llvm::Twine fn_name ("octave_jit_binary_any_any_"); | |
1169 fn_name = fn_name + llvm::Twine (op); | |
1170 | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1171 fn = create_internal (fn_name, any, any, any); |
15016 | 1172 fn.mark_can_error (); |
1173 llvm::BasicBlock *block = fn.new_block (); | |
1174 builder.SetInsertPoint (block); | |
1175 llvm::APInt op_int(sizeof (octave_value::binary_op) * 8, op, | |
1176 std::numeric_limits<octave_value::binary_op>::is_signed); | |
1177 llvm::Value *op_as_llvm = llvm::ConstantInt::get (llvm_bo_type, op_int); | |
1178 llvm::Value *ret = any_binary.call (builder, op_as_llvm, | |
1179 fn.argument (builder, 0), | |
1180 fn.argument (builder, 1)); | |
1181 fn.do_return (builder, ret); | |
1182 binary_ops[op].add_overload (fn); | |
1183 } | |
1184 | |
1185 // grab matrix | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1186 fn = create_external (JIT_FN (octave_jit_grab_matrix), matrix, matrix); |
15016 | 1187 grab_fn.add_overload (fn); |
1188 | |
15334
8125773322d4
Error on undefined an unused variables in JIT
Max Brister <max@2bass.com>
parents:
15311
diff
changeset
|
1189 grab_fn.add_overload (create_identity (scalar)); |
8125773322d4
Error on undefined an unused variables in JIT
Max Brister <max@2bass.com>
parents:
15311
diff
changeset
|
1190 grab_fn.add_overload (create_identity (scalar_ptr)); |
8125773322d4
Error on undefined an unused variables in JIT
Max Brister <max@2bass.com>
parents:
15311
diff
changeset
|
1191 grab_fn.add_overload (create_identity (any_ptr)); |
8125773322d4
Error on undefined an unused variables in JIT
Max Brister <max@2bass.com>
parents:
15311
diff
changeset
|
1192 grab_fn.add_overload (create_identity (boolean)); |
8125773322d4
Error on undefined an unused variables in JIT
Max Brister <max@2bass.com>
parents:
15311
diff
changeset
|
1193 grab_fn.add_overload (create_identity (complex)); |
8125773322d4
Error on undefined an unused variables in JIT
Max Brister <max@2bass.com>
parents:
15311
diff
changeset
|
1194 grab_fn.add_overload (create_identity (index)); |
8125773322d4
Error on undefined an unused variables in JIT
Max Brister <max@2bass.com>
parents:
15311
diff
changeset
|
1195 |
15016 | 1196 // release any |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1197 fn = create_external (JIT_FN (octave_jit_release_any), 0, any); |
15016 | 1198 release_fn.add_overload (fn); |
1199 release_fn.stash_name ("release"); | |
1200 | |
1201 // release matrix | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1202 fn = create_external (JIT_FN (octave_jit_release_matrix), 0, matrix); |
15016 | 1203 release_fn.add_overload (fn); |
1204 | |
15337
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
1205 // destroy |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
1206 destroy_fn = release_fn; |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
1207 destroy_fn.stash_name ("destroy"); |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
1208 destroy_fn.add_overload (create_identity(scalar)); |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
1209 destroy_fn.add_overload (create_identity(boolean)); |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
1210 destroy_fn.add_overload (create_identity(index)); |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
1211 destroy_fn.add_overload (create_identity(complex)); |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
1212 |
15016 | 1213 // now for binary scalar operations |
1214 add_binary_op (scalar, octave_value::op_add, llvm::Instruction::FAdd); | |
1215 add_binary_op (scalar, octave_value::op_sub, llvm::Instruction::FSub); | |
1216 add_binary_op (scalar, octave_value::op_mul, llvm::Instruction::FMul); | |
1217 add_binary_op (scalar, octave_value::op_el_mul, llvm::Instruction::FMul); | |
1218 | |
1219 add_binary_fcmp (scalar, octave_value::op_lt, llvm::CmpInst::FCMP_ULT); | |
1220 add_binary_fcmp (scalar, octave_value::op_le, llvm::CmpInst::FCMP_ULE); | |
1221 add_binary_fcmp (scalar, octave_value::op_eq, llvm::CmpInst::FCMP_UEQ); | |
1222 add_binary_fcmp (scalar, octave_value::op_ge, llvm::CmpInst::FCMP_UGE); | |
1223 add_binary_fcmp (scalar, octave_value::op_gt, llvm::CmpInst::FCMP_UGT); | |
1224 add_binary_fcmp (scalar, octave_value::op_ne, llvm::CmpInst::FCMP_UNE); | |
1225 | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1226 jit_function gripe_div0 = create_external (JIT_FN (gripe_divide_by_zero), 0); |
15016 | 1227 gripe_div0.mark_can_error (); |
1228 | |
1229 // divide is annoying because it might error | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1230 fn = create_internal ("octave_jit_div_scalar_scalar", scalar, scalar, scalar); |
15016 | 1231 fn.mark_can_error (); |
1232 | |
1233 llvm::BasicBlock *body = fn.new_block (); | |
1234 builder.SetInsertPoint (body); | |
1235 { | |
1236 llvm::BasicBlock *warn_block = fn.new_block ("warn"); | |
1237 llvm::BasicBlock *normal_block = fn.new_block ("normal"); | |
1238 | |
1239 llvm::Value *zero = llvm::ConstantFP::get (scalar_t, 0); | |
15095
9df70a18aa27
Correct division by zero check in JIT
Max Brister <max@2bass.com>
parents:
15078
diff
changeset
|
1240 llvm::Value *check = builder.CreateFCmpUEQ (zero, fn.argument (builder, 1)); |
15016 | 1241 builder.CreateCondBr (check, warn_block, normal_block); |
1242 | |
1243 builder.SetInsertPoint (warn_block); | |
1244 gripe_div0.call (builder); | |
1245 builder.CreateBr (normal_block); | |
1246 | |
1247 builder.SetInsertPoint (normal_block); | |
1248 llvm::Value *ret = builder.CreateFDiv (fn.argument (builder, 0), | |
1249 fn.argument (builder, 1)); | |
1250 fn.do_return (builder, ret); | |
1251 } | |
1252 binary_ops[octave_value::op_div].add_overload (fn); | |
1253 binary_ops[octave_value::op_el_div].add_overload (fn); | |
1254 | |
1255 // ldiv is the same as div with the operators reversed | |
1256 fn = mirror_binary (fn); | |
1257 binary_ops[octave_value::op_ldiv].add_overload (fn); | |
1258 binary_ops[octave_value::op_el_ldiv].add_overload (fn); | |
1259 | |
1260 // In general, the result of scalar ^ scalar is a complex number. We might be | |
1261 // able to improve on this if we keep track of the range of values varaibles | |
1262 // can take on. | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1263 fn = create_external (JIT_FN (octave_jit_pow_scalar_scalar), complex, scalar, |
15016 | 1264 scalar); |
1265 binary_ops[octave_value::op_pow].add_overload (fn); | |
1266 binary_ops[octave_value::op_el_pow].add_overload (fn); | |
1267 | |
15146
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1268 // now for unary scalar operations |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1269 // FIXME: Impelment not |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1270 fn = create_internal ("octave_jit_++", scalar, scalar); |
15146
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1271 body = fn.new_block (); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1272 builder.SetInsertPoint (body); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1273 { |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1274 llvm::Value *one = llvm::ConstantFP::get (scalar_t, 1); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1275 llvm::Value *val = fn.argument (builder, 0); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1276 val = builder.CreateFAdd (val, one); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1277 fn.do_return (builder, val); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1278 } |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1279 unary_ops[octave_value::op_incr].add_overload (fn); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1280 |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1281 fn = create_internal ("octave_jit_--", scalar, scalar); |
15146
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1282 body = fn.new_block (); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1283 builder.SetInsertPoint (body); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1284 { |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1285 llvm::Value *one = llvm::ConstantFP::get (scalar_t, 1); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1286 llvm::Value *val = fn.argument (builder, 0); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1287 val = builder.CreateFSub (val, one); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1288 fn.do_return (builder, val); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1289 } |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1290 unary_ops[octave_value::op_decr].add_overload (fn); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1291 |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1292 fn = create_internal ("octave_jit_uminus", scalar, scalar); |
15146
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1293 body = fn.new_block (); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1294 builder.SetInsertPoint (body); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1295 { |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1296 llvm::Value *mone = llvm::ConstantFP::get (scalar_t, -1); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1297 llvm::Value *val = fn.argument (builder, 0); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1298 val = builder.CreateFMul (val, mone); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1299 fn.do_return (builder, val); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1300 } |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1301 |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1302 fn = create_identity (scalar); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1303 unary_ops[octave_value::op_uplus].add_overload (fn); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1304 unary_ops[octave_value::op_transpose].add_overload (fn); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1305 unary_ops[octave_value::op_hermitian].add_overload (fn); |
709e8928e68c
Scalar unary operation support in JIT
Max Brister <max@2bass.com>
parents:
15136
diff
changeset
|
1306 |
15016 | 1307 // now for binary complex operations |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1308 fn = create_internal ("octave_jit_+_complex_complex", complex, complex, |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1309 complex); |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1310 body = fn.new_block (); |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1311 builder.SetInsertPoint (body); |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1312 { |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1313 llvm::Value *lhs = fn.argument (builder, 0); |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1314 llvm::Value *rhs = fn.argument (builder, 1); |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1315 llvm::Value *real = builder.CreateFAdd (complex_real (lhs), |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1316 complex_real (rhs)); |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1317 llvm::Value *imag = builder.CreateFAdd (complex_imag (lhs), |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1318 complex_imag (rhs)); |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1319 fn.do_return (builder, complex_new (real, imag)); |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1320 } |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1321 binary_ops[octave_value::op_add].add_overload (fn); |
15016 | 1322 |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1323 fn = create_internal ("octave_jit_-_complex_complex", complex, complex, |
15016 | 1324 complex); |
1325 body = fn.new_block (); | |
1326 builder.SetInsertPoint (body); | |
1327 { | |
1328 llvm::Value *lhs = fn.argument (builder, 0); | |
1329 llvm::Value *rhs = fn.argument (builder, 1); | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1330 llvm::Value *real = builder.CreateFSub (complex_real (lhs), |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1331 complex_real (rhs)); |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1332 llvm::Value *imag = builder.CreateFSub (complex_imag (lhs), |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1333 complex_imag (rhs)); |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1334 fn.do_return (builder, complex_new (real, imag)); |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1335 } |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1336 binary_ops[octave_value::op_sub].add_overload (fn); |
15016 | 1337 |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1338 fn = create_external (JIT_FN (octave_jit_complex_mul), |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1339 complex, complex, complex); |
15016 | 1340 binary_ops[octave_value::op_mul].add_overload (fn); |
1341 binary_ops[octave_value::op_el_mul].add_overload (fn); | |
1342 | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1343 jit_function complex_div = create_external (JIT_FN (octave_jit_complex_div), |
15016 | 1344 complex, complex, complex); |
1345 complex_div.mark_can_error (); | |
1346 binary_ops[octave_value::op_div].add_overload (fn); | |
1347 binary_ops[octave_value::op_ldiv].add_overload (fn); | |
1348 | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1349 fn = create_external (JIT_FN (octave_jit_pow_complex_complex), complex, |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1350 complex, complex); |
15016 | 1351 binary_ops[octave_value::op_pow].add_overload (fn); |
1352 binary_ops[octave_value::op_el_pow].add_overload (fn); | |
1353 | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1354 fn = create_internal ("octave_jit_*_scalar_complex", complex, scalar, |
15016 | 1355 complex); |
1356 jit_function mul_scalar_complex = fn; | |
1357 body = fn.new_block (); | |
1358 builder.SetInsertPoint (body); | |
1359 { | |
15583
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1360 llvm::BasicBlock *complex_mul = fn.new_block ("complex_mul"); |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1361 llvm::BasicBlock *scalar_mul = fn.new_block ("scalar_mul"); |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1362 |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1363 llvm::Value *fzero = llvm::ConstantFP::get (scalar_t, 0); |
15016 | 1364 llvm::Value *lhs = fn.argument (builder, 0); |
1365 llvm::Value *rhs = fn.argument (builder, 1); | |
15583
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1366 |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1367 llvm::Value *cmp = builder.CreateFCmpUEQ (complex_imag (rhs), fzero); |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1368 builder.CreateCondBr (cmp, scalar_mul, complex_mul); |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1369 |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1370 builder.SetInsertPoint (scalar_mul); |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1371 llvm::Value *temp = complex_real (rhs); |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1372 temp = builder.CreateFMul (lhs, temp); |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1373 fn.do_return (builder, complex_new (temp, fzero), false); |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1374 |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1375 |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
1376 builder.SetInsertPoint (complex_mul); |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1377 temp = complex_new (builder.CreateFMul (lhs, complex_real (rhs)), |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1378 builder.CreateFMul (lhs, complex_imag (rhs))); |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1379 fn.do_return (builder, temp); |
15016 | 1380 } |
1381 binary_ops[octave_value::op_mul].add_overload (fn); | |
1382 binary_ops[octave_value::op_el_mul].add_overload (fn); | |
1383 | |
1384 | |
1385 fn = mirror_binary (mul_scalar_complex); | |
1386 binary_ops[octave_value::op_mul].add_overload (fn); | |
1387 binary_ops[octave_value::op_el_mul].add_overload (fn); | |
1388 | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1389 fn = create_internal ("octave_jit_+_scalar_complex", complex, scalar, |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1390 complex); |
15016 | 1391 body = fn.new_block (); |
1392 builder.SetInsertPoint (body); | |
1393 { | |
1394 llvm::Value *lhs = fn.argument (builder, 0); | |
1395 llvm::Value *rhs = fn.argument (builder, 1); | |
1396 llvm::Value *real = builder.CreateFAdd (lhs, complex_real (rhs)); | |
1397 fn.do_return (builder, complex_real (rhs, real)); | |
1398 } | |
1399 binary_ops[octave_value::op_add].add_overload (fn); | |
1400 | |
1401 fn = mirror_binary (fn); | |
1402 binary_ops[octave_value::op_add].add_overload (fn); | |
1403 | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1404 fn = create_internal ("octave_jit_-_complex_scalar", complex, complex, |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1405 scalar); |
15016 | 1406 body = fn.new_block (); |
1407 builder.SetInsertPoint (body); | |
1408 { | |
1409 llvm::Value *lhs = fn.argument (builder, 0); | |
1410 llvm::Value *rhs = fn.argument (builder, 1); | |
1411 llvm::Value *real = builder.CreateFSub (complex_real (lhs), rhs); | |
1412 fn.do_return (builder, complex_real (lhs, real)); | |
1413 } | |
1414 binary_ops[octave_value::op_sub].add_overload (fn); | |
1415 | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1416 fn = create_internal ("octave_jit_-_scalar_complex", complex, scalar, |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1417 complex); |
15016 | 1418 body = fn.new_block (); |
1419 builder.SetInsertPoint (body); | |
1420 { | |
1421 llvm::Value *lhs = fn.argument (builder, 0); | |
1422 llvm::Value *rhs = fn.argument (builder, 1); | |
1423 llvm::Value *real = builder.CreateFSub (lhs, complex_real (rhs)); | |
1424 fn.do_return (builder, complex_real (rhs, real)); | |
1425 } | |
1426 binary_ops[octave_value::op_sub].add_overload (fn); | |
1427 | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1428 fn = create_external (JIT_FN (octave_jit_pow_scalar_complex), complex, scalar, |
15016 | 1429 complex); |
1430 binary_ops[octave_value::op_pow].add_overload (fn); | |
1431 binary_ops[octave_value::op_el_pow].add_overload (fn); | |
1432 | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1433 fn = create_external (JIT_FN (octave_jit_pow_complex_scalar), complex, |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1434 complex, scalar); |
15016 | 1435 binary_ops[octave_value::op_pow].add_overload (fn); |
1436 binary_ops[octave_value::op_el_pow].add_overload (fn); | |
1437 | |
1438 // now for binary index operators | |
1439 add_binary_op (index, octave_value::op_add, llvm::Instruction::Add); | |
1440 | |
1441 // and binary bool operators | |
1442 add_binary_op (boolean, octave_value::op_el_or, llvm::Instruction::Or); | |
1443 add_binary_op (boolean, octave_value::op_el_and, llvm::Instruction::And); | |
1444 | |
1445 // now for printing functions | |
1446 print_fn.stash_name ("print"); | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1447 add_print (any, reinterpret_cast<void *> (&octave_jit_print_any)); |
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1448 add_print (scalar, reinterpret_cast<void *> (&octave_jit_print_scalar)); |
15016 | 1449 |
1450 // initialize for loop | |
1451 for_init_fn.stash_name ("for_init"); | |
1452 | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1453 fn = create_internal ("octave_jit_for_range_init", index, range); |
15016 | 1454 body = fn.new_block (); |
1455 builder.SetInsertPoint (body); | |
1456 { | |
1457 llvm::Value *zero = llvm::ConstantInt::get (index_t, 0); | |
1458 fn.do_return (builder, zero); | |
1459 } | |
1460 for_init_fn.add_overload (fn); | |
1461 | |
1462 // bounds check for for loop | |
1463 for_check_fn.stash_name ("for_check"); | |
1464 | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1465 fn = create_internal ("octave_jit_for_range_check", boolean, range, index); |
15016 | 1466 body = fn.new_block (); |
1467 builder.SetInsertPoint (body); | |
1468 { | |
1469 llvm::Value *nelem | |
1470 = builder.CreateExtractValue (fn.argument (builder, 0), 3); | |
1471 llvm::Value *idx = fn.argument (builder, 1); | |
1472 llvm::Value *ret = builder.CreateICmpULT (idx, nelem); | |
1473 fn.do_return (builder, ret); | |
1474 } | |
1475 for_check_fn.add_overload (fn); | |
1476 | |
1477 // index variabe for for loop | |
1478 for_index_fn.stash_name ("for_index"); | |
1479 | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1480 fn = create_internal ("octave_jit_for_range_idx", scalar, range, index); |
15016 | 1481 body = fn.new_block (); |
1482 builder.SetInsertPoint (body); | |
1483 { | |
1484 llvm::Value *idx = fn.argument (builder, 1); | |
1485 llvm::Value *didx = builder.CreateSIToFP (idx, scalar_t); | |
1486 llvm::Value *rng = fn.argument (builder, 0); | |
1487 llvm::Value *base = builder.CreateExtractValue (rng, 0); | |
1488 llvm::Value *inc = builder.CreateExtractValue (rng, 2); | |
1489 | |
1490 llvm::Value *ret = builder.CreateFMul (didx, inc); | |
1491 ret = builder.CreateFAdd (base, ret); | |
1492 fn.do_return (builder, ret); | |
1493 } | |
1494 for_index_fn.add_overload (fn); | |
1495 | |
1496 // logically true | |
1497 logically_true_fn.stash_name ("logically_true"); | |
1498 | |
1499 jit_function gripe_nantl | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1500 = create_external (JIT_FN (octave_jit_gripe_nan_to_logical_conversion), 0); |
15016 | 1501 gripe_nantl.mark_can_error (); |
1502 | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1503 fn = create_internal ("octave_jit_logically_true_scalar", boolean, scalar); |
15016 | 1504 fn.mark_can_error (); |
1505 | |
1506 body = fn.new_block (); | |
1507 builder.SetInsertPoint (body); | |
1508 { | |
1509 llvm::BasicBlock *error_block = fn.new_block ("error"); | |
1510 llvm::BasicBlock *normal_block = fn.new_block ("normal"); | |
1511 | |
1512 llvm::Value *check = builder.CreateFCmpUNE (fn.argument (builder, 0), | |
1513 fn.argument (builder, 0)); | |
1514 builder.CreateCondBr (check, error_block, normal_block); | |
1515 | |
1516 builder.SetInsertPoint (error_block); | |
1517 gripe_nantl.call (builder); | |
1518 builder.CreateBr (normal_block); | |
1519 builder.SetInsertPoint (normal_block); | |
1520 | |
1521 llvm::Value *zero = llvm::ConstantFP::get (scalar_t, 0); | |
1522 llvm::Value *ret = builder.CreateFCmpONE (fn.argument (builder, 0), zero); | |
1523 fn.do_return (builder, ret); | |
1524 } | |
1525 logically_true_fn.add_overload (fn); | |
1526 | |
1527 // logically_true boolean | |
1528 fn = create_identity (boolean); | |
1529 logically_true_fn.add_overload (fn); | |
1530 | |
1531 // make_range | |
1532 // FIXME: May be benificial to implement all in LLVM | |
1533 make_range_fn.stash_name ("make_range"); | |
1534 jit_function compute_nelem | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1535 = create_external (JIT_FN (octave_jit_compute_nelem), |
15016 | 1536 index, scalar, scalar, scalar); |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1537 |
15016 | 1538 |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1539 fn = create_internal ("octave_jit_make_range", range, scalar, scalar, scalar); |
15016 | 1540 body = fn.new_block (); |
1541 builder.SetInsertPoint (body); | |
1542 { | |
1543 llvm::Value *base = fn.argument (builder, 0); | |
1544 llvm::Value *limit = fn.argument (builder, 1); | |
1545 llvm::Value *inc = fn.argument (builder, 2); | |
1546 llvm::Value *nelem = compute_nelem.call (builder, base, limit, inc); | |
1547 | |
1548 llvm::Value *dzero = llvm::ConstantFP::get (scalar_t, 0); | |
1549 llvm::Value *izero = llvm::ConstantInt::get (index_t, 0); | |
1550 llvm::Value *rng = llvm::ConstantStruct::get (range_t, dzero, dzero, dzero, | |
1551 izero, NULL); | |
1552 rng = builder.CreateInsertValue (rng, base, 0); | |
1553 rng = builder.CreateInsertValue (rng, limit, 1); | |
1554 rng = builder.CreateInsertValue (rng, inc, 2); | |
1555 rng = builder.CreateInsertValue (rng, nelem, 3); | |
1556 fn.do_return (builder, rng); | |
1557 } | |
1558 make_range_fn.add_overload (fn); | |
1559 | |
1560 // paren_subsref | |
1561 jit_type *jit_int = intN (sizeof (int) * 8); | |
1562 llvm::Type *int_t = jit_int->to_llvm (); | |
1563 jit_function ginvalid_index | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1564 = create_external (JIT_FN (octave_jit_ginvalid_index), 0); |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1565 jit_function gindex_range = create_external (JIT_FN (octave_jit_gindex_range), |
15016 | 1566 0, jit_int, jit_int, index, |
1567 index); | |
1568 | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1569 fn = create_internal ("()subsref", scalar, matrix, scalar); |
15016 | 1570 fn.mark_can_error (); |
1571 | |
1572 body = fn.new_block (); | |
1573 builder.SetInsertPoint (body); | |
1574 { | |
1575 llvm::Value *one = llvm::ConstantInt::get (index_t, 1); | |
1576 llvm::Value *ione; | |
1577 if (index_t == int_t) | |
1578 ione = one; | |
1579 else | |
1580 ione = llvm::ConstantInt::get (int_t, 1); | |
1581 | |
1582 llvm::Value *undef = llvm::UndefValue::get (scalar_t); | |
1583 llvm::Value *mat = fn.argument (builder, 0); | |
1584 llvm::Value *idx = fn.argument (builder, 1); | |
1585 | |
1586 // convert index to scalar to integer, and check index >= 1 | |
1587 llvm::Value *int_idx = builder.CreateFPToSI (idx, index_t); | |
1588 llvm::Value *check_idx = builder.CreateSIToFP (int_idx, scalar_t); | |
1589 llvm::Value *cond0 = builder.CreateFCmpUNE (idx, check_idx); | |
1590 llvm::Value *cond1 = builder.CreateICmpSLT (int_idx, one); | |
1591 llvm::Value *cond = builder.CreateOr (cond0, cond1); | |
1592 | |
1593 llvm::BasicBlock *done = fn.new_block ("done"); | |
1594 llvm::BasicBlock *conv_error = fn.new_block ("conv_error", done); | |
1595 llvm::BasicBlock *normal = fn.new_block ("normal", done); | |
1596 builder.CreateCondBr (cond, conv_error, normal); | |
1597 | |
1598 builder.SetInsertPoint (conv_error); | |
1599 ginvalid_index.call (builder); | |
1600 builder.CreateBr (done); | |
1601 | |
1602 builder.SetInsertPoint (normal); | |
1603 llvm::Value *len = builder.CreateExtractValue (mat, | |
1604 llvm::ArrayRef<unsigned> (2)); | |
1605 cond = builder.CreateICmpSGT (int_idx, len); | |
1606 | |
1607 | |
1608 llvm::BasicBlock *bounds_error = fn.new_block ("bounds_error", done); | |
1609 llvm::BasicBlock *success = fn.new_block ("success", done); | |
1610 builder.CreateCondBr (cond, bounds_error, success); | |
1611 | |
1612 builder.SetInsertPoint (bounds_error); | |
1613 gindex_range.call (builder, ione, ione, int_idx, len); | |
1614 builder.CreateBr (done); | |
1615 | |
1616 builder.SetInsertPoint (success); | |
1617 llvm::Value *data = builder.CreateExtractValue (mat, | |
1618 llvm::ArrayRef<unsigned> (1)); | |
1619 llvm::Value *gep = builder.CreateInBoundsGEP (data, int_idx); | |
1620 llvm::Value *ret = builder.CreateLoad (gep); | |
1621 builder.CreateBr (done); | |
1622 | |
1623 builder.SetInsertPoint (done); | |
1624 | |
1625 llvm::PHINode *merge = llvm::PHINode::Create (scalar_t, 3); | |
1626 builder.Insert (merge); | |
1627 merge->addIncoming (undef, conv_error); | |
1628 merge->addIncoming (undef, bounds_error); | |
1629 merge->addIncoming (ret, success); | |
1630 fn.do_return (builder, merge); | |
1631 } | |
1632 paren_subsref_fn.add_overload (fn); | |
1633 | |
1634 // paren subsasgn | |
1635 paren_subsasgn_fn.stash_name ("()subsasgn"); | |
1636 | |
1637 jit_function resize_paren_subsasgn | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1638 = create_external (JIT_FN (octave_jit_paren_subsasgn_impl), matrix, matrix, |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1639 index, scalar); |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1640 |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1641 fn = create_internal ("octave_jit_paren_subsasgn", matrix, matrix, scalar, |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1642 scalar); |
15016 | 1643 fn.mark_can_error (); |
1644 body = fn.new_block (); | |
1645 builder.SetInsertPoint (body); | |
1646 { | |
1647 llvm::Value *one = llvm::ConstantInt::get (index_t, 1); | |
1648 | |
1649 llvm::Value *mat = fn.argument (builder, 0); | |
1650 llvm::Value *idx = fn.argument (builder, 1); | |
1651 llvm::Value *value = fn.argument (builder, 2); | |
1652 | |
1653 llvm::Value *int_idx = builder.CreateFPToSI (idx, index_t); | |
1654 llvm::Value *check_idx = builder.CreateSIToFP (int_idx, scalar_t); | |
1655 llvm::Value *cond0 = builder.CreateFCmpUNE (idx, check_idx); | |
1656 llvm::Value *cond1 = builder.CreateICmpSLT (int_idx, one); | |
1657 llvm::Value *cond = builder.CreateOr (cond0, cond1); | |
1658 | |
1659 llvm::BasicBlock *done = fn.new_block ("done"); | |
1660 | |
1661 llvm::BasicBlock *conv_error = fn.new_block ("conv_error", done); | |
1662 llvm::BasicBlock *normal = fn.new_block ("normal", done); | |
1663 builder.CreateCondBr (cond, conv_error, normal); | |
1664 builder.SetInsertPoint (conv_error); | |
1665 ginvalid_index.call (builder); | |
1666 builder.CreateBr (done); | |
1667 | |
1668 builder.SetInsertPoint (normal); | |
15056
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
1669 llvm::Value *len = builder.CreateExtractValue (mat, 2); |
15016 | 1670 cond0 = builder.CreateICmpSGT (int_idx, len); |
1671 | |
1672 llvm::Value *rcount = builder.CreateExtractValue (mat, 0); | |
1673 rcount = builder.CreateLoad (rcount); | |
1674 cond1 = builder.CreateICmpSGT (rcount, one); | |
1675 cond = builder.CreateOr (cond0, cond1); | |
1676 | |
1677 llvm::BasicBlock *bounds_error = fn.new_block ("bounds_error", done); | |
1678 llvm::BasicBlock *success = fn.new_block ("success", done); | |
1679 builder.CreateCondBr (cond, bounds_error, success); | |
1680 | |
1681 // resize on out of bounds access | |
1682 builder.SetInsertPoint (bounds_error); | |
15027
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1683 llvm::Value *resize_result = resize_paren_subsasgn.call (builder, mat, |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1684 int_idx, value); |
15016 | 1685 builder.CreateBr (done); |
1686 | |
1687 builder.SetInsertPoint (success); | |
1688 llvm::Value *data = builder.CreateExtractValue (mat, | |
1689 llvm::ArrayRef<unsigned> (1)); | |
1690 llvm::Value *gep = builder.CreateInBoundsGEP (data, int_idx); | |
1691 builder.CreateStore (value, gep); | |
1692 builder.CreateBr (done); | |
1693 | |
1694 builder.SetInsertPoint (done); | |
1695 | |
1696 llvm::PHINode *merge = llvm::PHINode::Create (matrix_t, 3); | |
1697 builder.Insert (merge); | |
1698 merge->addIncoming (mat, conv_error); | |
1699 merge->addIncoming (resize_result, bounds_error); | |
1700 merge->addIncoming (mat, success); | |
1701 fn.do_return (builder, merge); | |
1702 } | |
1703 paren_subsasgn_fn.add_overload (fn); | |
1704 | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1705 fn = create_external (JIT_FN (octave_jit_paren_subsasgn_matrix_range), matrix, |
15016 | 1706 matrix, range, scalar); |
1707 fn.mark_can_error (); | |
1708 paren_subsasgn_fn.add_overload (fn); | |
1709 | |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1710 end1_fn.stash_name ("end1"); |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1711 fn = create_internal ("octave_jit_end1_matrix", scalar, matrix, index, index); |
15056
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
1712 body = fn.new_block (); |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
1713 builder.SetInsertPoint (body); |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
1714 { |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
1715 llvm::Value *mat = fn.argument (builder, 0); |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
1716 llvm::Value *ret = builder.CreateExtractValue (mat, 2); |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
1717 fn.do_return (builder, builder.CreateSIToFP (ret, scalar_t)); |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
1718 } |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1719 end1_fn.add_overload (fn); |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1720 |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1721 end_fn.stash_name ("end"); |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1722 fn = create_external (JIT_FN (octave_jit_end_matrix),scalar, matrix, index, |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1723 index); |
15056
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
1724 end_fn.add_overload (fn); |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15027
diff
changeset
|
1725 |
15337
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
1726 // -------------------- create_undef -------------------- |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
1727 create_undef_fn.stash_name ("create_undef"); |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1728 fn = create_external (JIT_FN (octave_jit_create_undef), any); |
15337
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
1729 create_undef_fn.add_overload (fn); |
3f43e9d6d86e
JIT compile anonymous functions
Max Brister <max@2bass.com>
parents:
15334
diff
changeset
|
1730 |
15016 | 1731 casts[any->type_id ()].stash_name ("(any)"); |
1732 casts[scalar->type_id ()].stash_name ("(scalar)"); | |
1733 casts[complex->type_id ()].stash_name ("(complex)"); | |
1734 casts[matrix->type_id ()].stash_name ("(matrix)"); | |
15603 | 1735 casts[range->type_id ()].stash_name ("(range)"); |
15016 | 1736 |
1737 // cast any <- matrix | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1738 fn = create_external (JIT_FN (octave_jit_cast_any_matrix), any, matrix); |
15016 | 1739 casts[any->type_id ()].add_overload (fn); |
1740 | |
1741 // cast matrix <- any | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1742 fn = create_external (JIT_FN (octave_jit_cast_matrix_any), matrix, any); |
15016 | 1743 casts[matrix->type_id ()].add_overload (fn); |
1744 | |
15027
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1745 // cast any <- range |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1746 fn = create_external (JIT_FN (octave_jit_cast_any_range), any, range); |
15027
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1747 casts[any->type_id ()].add_overload (fn); |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1748 |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1749 // cast range <- any |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1750 fn = create_external (JIT_FN (octave_jit_cast_range_any), range, any); |
15027
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1751 casts[range->type_id ()].add_overload (fn); |
741d2dbcc117
Check trip count before compiling for loops.
Max Brister <max@2bass.com>
parents:
15019
diff
changeset
|
1752 |
15016 | 1753 // cast any <- scalar |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1754 fn = create_external (JIT_FN (octave_jit_cast_any_scalar), any, scalar); |
15016 | 1755 casts[any->type_id ()].add_overload (fn); |
1756 | |
1757 // cast scalar <- any | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1758 fn = create_external (JIT_FN (octave_jit_cast_scalar_any), scalar, any); |
15016 | 1759 casts[scalar->type_id ()].add_overload (fn); |
1760 | |
1761 // cast any <- complex | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1762 fn = create_external (JIT_FN (octave_jit_cast_any_complex), any, complex); |
15016 | 1763 casts[any->type_id ()].add_overload (fn); |
1764 | |
1765 // cast complex <- any | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1766 fn = create_external (JIT_FN (octave_jit_cast_complex_any), complex, any); |
15016 | 1767 casts[complex->type_id ()].add_overload (fn); |
1768 | |
1769 // cast complex <- scalar | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1770 fn = create_internal ("octave_jit_cast_complex_scalar", complex, scalar); |
15016 | 1771 body = fn.new_block (); |
1772 builder.SetInsertPoint (body); | |
1773 { | |
1774 llvm::Value *zero = llvm::ConstantFP::get (scalar_t, 0); | |
1775 fn.do_return (builder, complex_new (fn.argument (builder, 0), zero)); | |
1776 } | |
1777 casts[complex->type_id ()].add_overload (fn); | |
1778 | |
1779 // cast scalar <- complex | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1780 fn = create_internal ("octave_jit_cast_scalar_complex", scalar, complex); |
15016 | 1781 body = fn.new_block (); |
1782 builder.SetInsertPoint (body); | |
1783 fn.do_return (builder, complex_real (fn.argument (builder, 0))); | |
1784 casts[scalar->type_id ()].add_overload (fn); | |
1785 | |
1786 // cast any <- any | |
1787 fn = create_identity (any); | |
1788 casts[any->type_id ()].add_overload (fn); | |
1789 | |
1790 // cast scalar <- scalar | |
1791 fn = create_identity (scalar); | |
1792 casts[scalar->type_id ()].add_overload (fn); | |
1793 | |
1794 // cast complex <- complex | |
1795 fn = create_identity (complex); | |
1796 casts[complex->type_id ()].add_overload (fn); | |
1797 | |
1798 // -------------------- builtin functions -------------------- | |
1799 add_builtin ("#unknown_function"); | |
1800 unknown_function = builtins["#unknown_function"]; | |
1801 | |
1802 add_builtin ("sin"); | |
1803 register_intrinsic ("sin", llvm::Intrinsic::sin, scalar, scalar); | |
1804 register_generic ("sin", matrix, matrix); | |
1805 | |
1806 add_builtin ("cos"); | |
1807 register_intrinsic ("cos", llvm::Intrinsic::cos, scalar, scalar); | |
1808 register_generic ("cos", matrix, matrix); | |
1809 | |
1810 add_builtin ("exp"); | |
1811 register_intrinsic ("exp", llvm::Intrinsic::cos, scalar, scalar); | |
1812 register_generic ("exp", matrix, matrix); | |
1813 | |
15169
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
1814 add_builtin ("balance"); |
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
1815 register_generic ("balance", matrix, matrix); |
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
1816 |
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
1817 add_builtin ("cond"); |
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
1818 register_generic ("cond", scalar, matrix); |
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
1819 |
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
1820 add_builtin ("det"); |
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
1821 register_generic ("det", scalar, matrix); |
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
1822 |
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
1823 add_builtin ("norm"); |
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
1824 register_generic ("norm", scalar, matrix); |
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
1825 |
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
1826 add_builtin ("rand"); |
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
1827 register_generic ("rand", matrix, scalar); |
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
1828 register_generic ("rand", matrix, std::vector<jit_type *> (2, scalar)); |
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
1829 |
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
1830 add_builtin ("magic"); |
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
1831 register_generic ("magic", matrix, scalar); |
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
1832 register_generic ("magic", matrix, std::vector<jit_type *> (2, scalar)); |
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
1833 |
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
1834 add_builtin ("eye"); |
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
1835 register_generic ("eye", matrix, scalar); |
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
1836 register_generic ("eye", matrix, std::vector<jit_type *> (2, scalar)); |
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
1837 |
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
1838 add_builtin ("mod"); |
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
1839 register_generic ("mod", scalar, std::vector<jit_type *> (2, scalar)); |
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
1840 |
15016 | 1841 casts.resize (next_id + 1); |
1842 jit_function any_id = create_identity (any); | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1843 jit_function grab_any = create_external (JIT_FN (octave_jit_grab_any), |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1844 any, any); |
15016 | 1845 jit_function release_any = get_release (any); |
1846 std::vector<jit_type *> args; | |
1847 args.resize (1); | |
1848 | |
1849 for (std::map<std::string, jit_type *>::iterator iter = builtins.begin (); | |
1850 iter != builtins.end (); ++iter) | |
1851 { | |
1852 jit_type *btype = iter->second; | |
1853 args[0] = btype; | |
1854 | |
15334
8125773322d4
Error on undefined an unused variables in JIT
Max Brister <max@2bass.com>
parents:
15311
diff
changeset
|
1855 grab_fn.add_overload (jit_function (grab_any, btype, args)); |
15016 | 1856 release_fn.add_overload (jit_function (release_any, 0, args)); |
1857 casts[any->type_id ()].add_overload (jit_function (any_id, any, args)); | |
1858 | |
1859 args[0] = any; | |
1860 casts[btype->type_id ()].add_overload (jit_function (any_id, btype, | |
1861 args)); | |
1862 } | |
1863 } | |
1864 | |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1865 const jit_function& |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1866 jit_typeinfo::do_end (jit_value *value, jit_value *idx, jit_value *count) |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1867 { |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1868 jit_const_index *ccount = dynamic_cast<jit_const_index *> (count); |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1869 if (ccount && ccount->value () == 1) |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1870 return end1_fn.overload (value->type (), idx->type (), count->type ()); |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1871 |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1872 return end_fn.overload (value->type (), idx->type (), count->type ()); |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1873 } |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1874 |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1875 jit_type* |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1876 jit_typeinfo::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
|
1877 llvm::Type *llvm_type, bool skip_paren) |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1878 { |
15124
0464e3ceb85b
Skip functions when resolving end context in JIT
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
1879 jit_type *ret = new jit_type (name, parent, llvm_type, skip_paren, next_id++); |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1880 id_to_type.push_back (ret); |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1881 return ret; |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1882 } |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1883 |
15016 | 1884 void |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1885 jit_typeinfo::add_print (jit_type *ty, void *fptr) |
15016 | 1886 { |
1887 std::stringstream name; | |
1888 name << "octave_jit_print_" << ty->name (); | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1889 jit_function fn = create_external (engine, fptr, name.str (), 0, intN (8), ty); |
15016 | 1890 print_fn.add_overload (fn); |
1891 } | |
1892 | |
1893 // FIXME: cp between add_binary_op, add_binary_icmp, and add_binary_fcmp | |
1894 void | |
1895 jit_typeinfo::add_binary_op (jit_type *ty, int op, int llvm_op) | |
1896 { | |
1897 std::stringstream fname; | |
1898 octave_value::binary_op ov_op = static_cast<octave_value::binary_op>(op); | |
1899 fname << "octave_jit_" << octave_value::binary_op_as_string (ov_op) | |
1900 << "_" << ty->name (); | |
1901 | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1902 jit_function fn = create_internal (fname.str (), ty, ty, ty); |
15016 | 1903 llvm::BasicBlock *block = fn.new_block (); |
1904 builder.SetInsertPoint (block); | |
1905 llvm::Instruction::BinaryOps temp | |
1906 = static_cast<llvm::Instruction::BinaryOps>(llvm_op); | |
1907 | |
1908 llvm::Value *ret = builder.CreateBinOp (temp, fn.argument (builder, 0), | |
1909 fn.argument (builder, 1)); | |
1910 fn.do_return (builder, ret); | |
1911 binary_ops[op].add_overload (fn); | |
1912 } | |
1913 | |
1914 void | |
1915 jit_typeinfo::add_binary_icmp (jit_type *ty, int op, int llvm_op) | |
1916 { | |
1917 std::stringstream fname; | |
1918 octave_value::binary_op ov_op = static_cast<octave_value::binary_op>(op); | |
1919 fname << "octave_jit" << octave_value::binary_op_as_string (ov_op) | |
1920 << "_" << ty->name (); | |
1921 | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1922 jit_function fn = create_internal (fname.str (), boolean, ty, ty); |
15016 | 1923 llvm::BasicBlock *block = fn.new_block (); |
1924 builder.SetInsertPoint (block); | |
1925 llvm::CmpInst::Predicate temp | |
1926 = static_cast<llvm::CmpInst::Predicate>(llvm_op); | |
1927 llvm::Value *ret = builder.CreateICmp (temp, fn.argument (builder, 0), | |
1928 fn.argument (builder, 1)); | |
1929 fn.do_return (builder, ret); | |
1930 binary_ops[op].add_overload (fn); | |
1931 } | |
1932 | |
1933 void | |
1934 jit_typeinfo::add_binary_fcmp (jit_type *ty, int op, int llvm_op) | |
1935 { | |
1936 std::stringstream fname; | |
1937 octave_value::binary_op ov_op = static_cast<octave_value::binary_op>(op); | |
1938 fname << "octave_jit" << octave_value::binary_op_as_string (ov_op) | |
1939 << "_" << ty->name (); | |
1940 | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1941 jit_function fn = create_internal (fname.str (), boolean, ty, ty); |
15016 | 1942 llvm::BasicBlock *block = fn.new_block (); |
1943 builder.SetInsertPoint (block); | |
1944 llvm::CmpInst::Predicate temp | |
1945 = static_cast<llvm::CmpInst::Predicate>(llvm_op); | |
1946 llvm::Value *ret = builder.CreateFCmp (temp, fn.argument (builder, 0), | |
1947 fn.argument (builder, 1)); | |
1948 fn.do_return (builder, ret); | |
1949 binary_ops[op].add_overload (fn); | |
1950 } | |
1951 | |
1952 jit_function | |
1953 jit_typeinfo::create_function (jit_convention::type cc, const llvm::Twine& name, | |
1954 jit_type *ret, | |
1955 const std::vector<jit_type *>& args) | |
1956 { | |
1957 jit_function result (module, cc, name, ret, args); | |
1958 return result; | |
1959 } | |
1960 | |
1961 jit_function | |
1962 jit_typeinfo::create_identity (jit_type *type) | |
1963 { | |
1964 size_t id = type->type_id (); | |
1965 if (id >= identities.size ()) | |
1966 identities.resize (id + 1); | |
1967 | |
1968 if (! identities[id].valid ()) | |
1969 { | |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
1970 std::stringstream name; |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
1971 name << "id_" << type->name (); |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
1972 |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
1973 jit_function fn = create_internal (name.str (), type, type); |
15016 | 1974 llvm::BasicBlock *body = fn.new_block (); |
1975 builder.SetInsertPoint (body); | |
1976 fn.do_return (builder, fn.argument (builder, 0)); | |
1977 return identities[id] = fn; | |
1978 } | |
1979 | |
1980 return identities[id]; | |
1981 } | |
1982 | |
1983 llvm::Value * | |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1984 jit_typeinfo::do_insert_error_check (llvm::IRBuilderD& abuilder) |
15016 | 1985 { |
15019
ae3670d4df29
Update the execution engine's global mapping for external functions
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1986 return abuilder.CreateLoad (lerror_state); |
15016 | 1987 } |
1988 | |
15603 | 1989 llvm::Value * |
1990 jit_typeinfo::do_insert_interrupt_check (llvm::IRBuilderD& abuilder) | |
1991 { | |
1992 llvm::LoadInst *val = abuilder.CreateLoad (loctave_interrupt_state); | |
1993 val->setVolatile (true); | |
1994 return abuilder.CreateICmpSGT (val, abuilder.getInt32 (0)); | |
1995 } | |
1996 | |
15016 | 1997 void |
1998 jit_typeinfo::add_builtin (const std::string& name) | |
1999 { | |
15124
0464e3ceb85b
Skip functions when resolving end context in JIT
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
2000 jit_type *btype = new_type (name, any, any->to_llvm (), true); |
15016 | 2001 builtins[name] = btype; |
2002 | |
2003 octave_builtin *ov_builtin = find_builtin (name); | |
2004 if (ov_builtin) | |
2005 ov_builtin->stash_jit (*btype); | |
2006 } | |
2007 | |
2008 void | |
2009 jit_typeinfo::register_intrinsic (const std::string& name, size_t iid, | |
2010 jit_type *result, | |
2011 const std::vector<jit_type *>& args) | |
2012 { | |
2013 jit_type *builtin_type = builtins[name]; | |
2014 size_t nargs = args.size (); | |
2015 llvm::SmallVector<llvm::Type *, 5> llvm_args (nargs); | |
2016 for (size_t i = 0; i < nargs; ++i) | |
2017 llvm_args[i] = args[i]->to_llvm (); | |
2018 | |
2019 llvm::Intrinsic::ID id = static_cast<llvm::Intrinsic::ID> (iid); | |
2020 llvm::Function *ifun = llvm::Intrinsic::getDeclaration (module, id, | |
2021 llvm_args); | |
2022 std::stringstream fn_name; | |
2023 fn_name << "octave_jit_" << name; | |
2024 | |
2025 std::vector<jit_type *> args1 (nargs + 1); | |
2026 args1[0] = builtin_type; | |
2027 std::copy (args.begin (), args.end (), args1.begin () + 1); | |
2028 | |
2029 // The first argument will be the Octave function, but we already know that | |
2030 // the function call is the equivalent of the intrinsic, so we ignore it and | |
2031 // call the intrinsic with the remaining arguments. | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
2032 jit_function fn = create_internal (fn_name.str (), result, args1); |
15016 | 2033 llvm::BasicBlock *body = fn.new_block (); |
2034 builder.SetInsertPoint (body); | |
2035 | |
2036 llvm::SmallVector<llvm::Value *, 5> fargs (nargs); | |
2037 for (size_t i = 0; i < nargs; ++i) | |
2038 fargs[i] = fn.argument (builder, i + 1); | |
2039 | |
2040 llvm::Value *ret = builder.CreateCall (ifun, fargs); | |
2041 fn.do_return (builder, ret); | |
2042 paren_subsref_fn.add_overload (fn); | |
2043 } | |
2044 | |
2045 octave_builtin * | |
2046 jit_typeinfo::find_builtin (const std::string& name) | |
2047 { | |
2048 // FIXME: Finalize what we want to store in octave_builtin, then add functions | |
2049 // to access these values in octave_value | |
2050 octave_value ov_builtin = symbol_table::find (name); | |
2051 return dynamic_cast<octave_builtin *> (ov_builtin.internal_rep ()); | |
2052 } | |
2053 | |
2054 void | |
15135
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2055 jit_typeinfo::register_generic (const std::string& name, jit_type *result, |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2056 const std::vector<jit_type *>& args) |
15016 | 2057 { |
15135
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2058 octave_builtin *builtin = find_builtin (name); |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2059 if (! builtin) |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2060 return; |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2061 |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2062 std::vector<jit_type *> fn_args (args.size () + 1); |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2063 fn_args[0] = builtins[name]; |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2064 std::copy (args.begin (), args.end (), fn_args.begin () + 1); |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
2065 jit_function fn = create_internal (name, result, fn_args); |
15136
eeaaac7c86b6
jit-typeinfo.cc (jit_typeinfo::register_generic): Mark can error
Max Brister <max@2bass.com>
parents:
15135
diff
changeset
|
2066 fn.mark_can_error (); |
15135
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2067 llvm::BasicBlock *block = fn.new_block (); |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2068 builder.SetInsertPoint (block); |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2069 llvm::Type *any_t = any->to_llvm (); |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2070 llvm::ArrayType *array_t = llvm::ArrayType::get (any_t, args.size ()); |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2071 llvm::Value *array = llvm::UndefValue::get (array_t); |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2072 for (size_t i = 0; i < args.size (); ++i) |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2073 { |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2074 llvm::Value *arg = fn.argument (builder, i + 1); |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2075 jit_function agrab = get_grab (args[i]); |
15169
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
2076 if (agrab.valid ()) |
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
2077 arg = agrab.call (builder, arg); |
15135
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2078 jit_function acast = cast (any, args[i]); |
15169
6242904370bd
Support balance, cond, det, norm, rand, magic, eye, and mod in JIT
Max Brister <max@2bass.com>
parents:
15148
diff
changeset
|
2079 array = builder.CreateInsertValue (array, acast.call (builder, arg), i); |
15135
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2080 } |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2081 |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2082 llvm::Value *array_mem = builder.CreateAlloca (array_t); |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2083 builder.CreateStore (array, array_mem); |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2084 array = builder.CreateBitCast (array_mem, any_t->getPointerTo ()); |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2085 |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2086 jit_type *jintTy = intN (sizeof (octave_builtin::fcn) * 8); |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2087 llvm::Type *intTy = jintTy->to_llvm (); |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2088 size_t fcn_int = reinterpret_cast<size_t> (builtin->function ()); |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2089 llvm::Value *fcn = llvm::ConstantInt::get (intTy, fcn_int); |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2090 llvm::Value *nargin = llvm::ConstantInt::get (intTy, args.size ()); |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2091 size_t result_int = reinterpret_cast<size_t> (result); |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2092 llvm::Value *res_llvm = llvm::ConstantInt::get (intTy, result_int); |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2093 llvm::Value *ret = any_call.call (builder, fcn, nargin, array, res_llvm); |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2094 |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2095 jit_function cast_result = cast (result, any); |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2096 fn.do_return (builder, cast_result.call (builder, ret)); |
bd6bb87e2bea
Support sin, cos, and exp with matrix arguments in JIT
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
2097 paren_subsref_fn.add_overload (fn); |
15016 | 2098 } |
2099 | |
2100 jit_function | |
2101 jit_typeinfo::mirror_binary (const jit_function& fn) | |
2102 { | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
2103 jit_function ret = create_internal (fn.name () + "_reverse", |
15016 | 2104 fn.result (), fn.argument_type (1), |
2105 fn.argument_type (0)); | |
2106 if (fn.can_error ()) | |
2107 ret.mark_can_error (); | |
2108 | |
2109 llvm::BasicBlock *body = ret.new_block (); | |
2110 builder.SetInsertPoint (body); | |
2111 llvm::Value *result = fn.call (builder, ret.argument (builder, 1), | |
2112 ret.argument (builder, 0)); | |
2113 if (ret.result ()) | |
2114 ret.do_return (builder, result); | |
2115 else | |
2116 ret.do_return (builder); | |
2117 | |
2118 return ret; | |
2119 } | |
2120 | |
2121 llvm::Value * | |
2122 jit_typeinfo::pack_complex (llvm::IRBuilderD& bld, llvm::Value *cplx) | |
2123 { | |
2124 llvm::Type *complex_ret = instance->complex_ret; | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
2125 llvm::Value *real = bld.CreateExtractValue (cplx, 0); |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
2126 llvm::Value *imag = bld.CreateExtractValue (cplx, 1); |
15016 | 2127 llvm::Value *ret = llvm::UndefValue::get (complex_ret); |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
2128 |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
2129 unsigned int re_idx[] = {0, 0}; |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
2130 unsigned int im_idx[] = {0, 1}; |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
2131 ret = bld.CreateInsertValue (ret, real, re_idx); |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
2132 return bld.CreateInsertValue (ret, imag, im_idx); |
15016 | 2133 } |
2134 | |
2135 llvm::Value * | |
2136 jit_typeinfo::unpack_complex (llvm::IRBuilderD& bld, llvm::Value *result) | |
2137 { | |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
2138 unsigned int re_idx[] = {0, 0}; |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
2139 unsigned int im_idx[] = {0, 1}; |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
2140 |
15016 | 2141 llvm::Type *complex_t = get_complex ()->to_llvm (); |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
2142 llvm::Value *real = bld.CreateExtractValue (result, re_idx); |
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
2143 llvm::Value *imag = bld.CreateExtractValue (result, im_idx); |
15016 | 2144 llvm::Value *ret = llvm::UndefValue::get (complex_t); |
15370
8355fddce815
Use sret and do not use save/restore stack (bug #37308)
Max Brister <max@2bass.com>
parents:
15337
diff
changeset
|
2145 |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
2146 ret = bld.CreateInsertValue (ret, real, 0); |
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
2147 return bld.CreateInsertValue (ret, imag, 1); |
15016 | 2148 } |
2149 | |
2150 llvm::Value * | |
2151 jit_typeinfo::complex_real (llvm::Value *cx) | |
2152 { | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
2153 return builder.CreateExtractValue (cx, 0); |
15016 | 2154 } |
2155 | |
2156 llvm::Value * | |
2157 jit_typeinfo::complex_real (llvm::Value *cx, llvm::Value *real) | |
2158 { | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
2159 return builder.CreateInsertValue (cx, real, 0); |
15016 | 2160 } |
2161 | |
2162 llvm::Value * | |
2163 jit_typeinfo::complex_imag (llvm::Value *cx) | |
2164 { | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
2165 return builder.CreateExtractValue (cx, 1); |
15016 | 2166 } |
2167 | |
2168 llvm::Value * | |
2169 jit_typeinfo::complex_imag (llvm::Value *cx, llvm::Value *imag) | |
2170 { | |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
2171 return builder.CreateInsertValue (cx, imag, 1); |
15016 | 2172 } |
2173 | |
2174 llvm::Value * | |
2175 jit_typeinfo::complex_new (llvm::Value *real, llvm::Value *imag) | |
2176 { | |
2177 llvm::Value *ret = llvm::UndefValue::get (complex->to_llvm ()); | |
2178 ret = complex_real (ret, real); | |
2179 return complex_imag (ret, imag); | |
2180 } | |
2181 | |
2182 void | |
2183 jit_typeinfo::create_int (size_t nbits) | |
2184 { | |
2185 std::stringstream tname; | |
2186 tname << "int" << nbits; | |
2187 ints[nbits] = new_type (tname.str (), any, llvm::Type::getIntNTy (context, | |
2188 nbits)); | |
2189 } | |
2190 | |
2191 jit_type * | |
2192 jit_typeinfo::intN (size_t nbits) const | |
2193 { | |
2194 std::map<size_t, jit_type *>::const_iterator iter = ints.find (nbits); | |
2195 if (iter != ints.end ()) | |
2196 return iter->second; | |
2197 | |
2198 throw jit_fail_exception ("No such integer type"); | |
2199 } | |
2200 | |
2201 jit_type * | |
2202 jit_typeinfo::do_type_of (const octave_value &ov) const | |
2203 { | |
2204 if (ov.is_function ()) | |
2205 { | |
2206 // FIXME: This is ugly, we need to finalize how we want to to this, then | |
2207 // have octave_value fully support the needed functionality | |
2208 octave_builtin *builtin | |
2209 = dynamic_cast<octave_builtin *> (ov.internal_rep ()); | |
2210 return builtin && builtin->to_jit () ? builtin->to_jit () | |
2211 : unknown_function; | |
2212 } | |
2213 | |
2214 if (ov.is_range ()) | |
2215 return get_range (); | |
2216 | |
15311
de9bfcf637df
Fix error when compiling with complex matrix (bug #37247)
Max Brister <max@2bass.com>
parents:
15219
diff
changeset
|
2217 if (ov.is_double_type () && ! ov.is_complex_type ()) |
15016 | 2218 { |
2219 if (ov.is_real_scalar ()) | |
2220 return get_scalar (); | |
2221 | |
2222 if (ov.is_matrix_type ()) | |
2223 return get_matrix (); | |
2224 } | |
2225 | |
2226 if (ov.is_complex_scalar ()) | |
15583
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
2227 { |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
2228 Complex cv = ov.complex_value (); |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
2229 |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
2230 // We don't really represent complex values, instead we represent |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
2231 // complex_or_scalar. If the imag value is zero, we assume a scalar. |
15893
1f076c40c133
Do not use vectorized llvm commands for complex numbers
Max Brister <max@2bass.com>
parents:
15603
diff
changeset
|
2232 if (cv.imag () != 0) |
15583
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
2233 return get_complex (); |
0754bdfbc8fe
Correct multiplication complex multiplication with NaN in JIT
Max Brister <max@2bass.com>
parents:
15385
diff
changeset
|
2234 } |
15016 | 2235 |
2236 return get_any (); | |
2237 } | |
2238 | |
2239 #endif |