llvmjitcode.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. '''
  2. Use llvmlite to create executable functions from SymPy expressions
  3. This module requires llvmlite (https://github.com/numba/llvmlite).
  4. '''
  5. import ctypes
  6. from sympy.external import import_module
  7. from sympy.printing.printer import Printer
  8. from sympy.core.singleton import S
  9. from sympy.tensor.indexed import IndexedBase
  10. from sympy.utilities.decorator import doctest_depends_on
  11. llvmlite = import_module('llvmlite')
  12. if llvmlite:
  13. ll = import_module('llvmlite.ir').ir
  14. llvm = import_module('llvmlite.binding').binding
  15. llvm.initialize()
  16. llvm.initialize_native_target()
  17. llvm.initialize_native_asmprinter()
  18. __doctest_requires__ = {('llvm_callable'): ['llvmlite']}
  19. class LLVMJitPrinter(Printer):
  20. '''Convert expressions to LLVM IR'''
  21. def __init__(self, module, builder, fn, *args, **kwargs):
  22. self.func_arg_map = kwargs.pop("func_arg_map", {})
  23. if not llvmlite:
  24. raise ImportError("llvmlite is required for LLVMJITPrinter")
  25. super().__init__(*args, **kwargs)
  26. self.fp_type = ll.DoubleType()
  27. self.module = module
  28. self.builder = builder
  29. self.fn = fn
  30. self.ext_fn = {} # keep track of wrappers to external functions
  31. self.tmp_var = {}
  32. def _add_tmp_var(self, name, value):
  33. self.tmp_var[name] = value
  34. def _print_Number(self, n):
  35. return ll.Constant(self.fp_type, float(n))
  36. def _print_Integer(self, expr):
  37. return ll.Constant(self.fp_type, float(expr.p))
  38. def _print_Symbol(self, s):
  39. val = self.tmp_var.get(s)
  40. if not val:
  41. # look up parameter with name s
  42. val = self.func_arg_map.get(s)
  43. if not val:
  44. raise LookupError("Symbol not found: %s" % s)
  45. return val
  46. def _print_Pow(self, expr):
  47. base0 = self._print(expr.base)
  48. if expr.exp == S.NegativeOne:
  49. return self.builder.fdiv(ll.Constant(self.fp_type, 1.0), base0)
  50. if expr.exp == S.Half:
  51. fn = self.ext_fn.get("sqrt")
  52. if not fn:
  53. fn_type = ll.FunctionType(self.fp_type, [self.fp_type])
  54. fn = ll.Function(self.module, fn_type, "sqrt")
  55. self.ext_fn["sqrt"] = fn
  56. return self.builder.call(fn, [base0], "sqrt")
  57. if expr.exp == 2:
  58. return self.builder.fmul(base0, base0)
  59. exp0 = self._print(expr.exp)
  60. fn = self.ext_fn.get("pow")
  61. if not fn:
  62. fn_type = ll.FunctionType(self.fp_type, [self.fp_type, self.fp_type])
  63. fn = ll.Function(self.module, fn_type, "pow")
  64. self.ext_fn["pow"] = fn
  65. return self.builder.call(fn, [base0, exp0], "pow")
  66. def _print_Mul(self, expr):
  67. nodes = [self._print(a) for a in expr.args]
  68. e = nodes[0]
  69. for node in nodes[1:]:
  70. e = self.builder.fmul(e, node)
  71. return e
  72. def _print_Add(self, expr):
  73. nodes = [self._print(a) for a in expr.args]
  74. e = nodes[0]
  75. for node in nodes[1:]:
  76. e = self.builder.fadd(e, node)
  77. return e
  78. # TODO - assumes all called functions take one double precision argument.
  79. # Should have a list of math library functions to validate this.
  80. def _print_Function(self, expr):
  81. name = expr.func.__name__
  82. e0 = self._print(expr.args[0])
  83. fn = self.ext_fn.get(name)
  84. if not fn:
  85. fn_type = ll.FunctionType(self.fp_type, [self.fp_type])
  86. fn = ll.Function(self.module, fn_type, name)
  87. self.ext_fn[name] = fn
  88. return self.builder.call(fn, [e0], name)
  89. def emptyPrinter(self, expr):
  90. raise TypeError("Unsupported type for LLVM JIT conversion: %s"
  91. % type(expr))
  92. # Used when parameters are passed by array. Often used in callbacks to
  93. # handle a variable number of parameters.
  94. class LLVMJitCallbackPrinter(LLVMJitPrinter):
  95. def __init__(self, *args, **kwargs):
  96. super().__init__(*args, **kwargs)
  97. def _print_Indexed(self, expr):
  98. array, idx = self.func_arg_map[expr.base]
  99. offset = int(expr.indices[0].evalf())
  100. array_ptr = self.builder.gep(array, [ll.Constant(ll.IntType(32), offset)])
  101. fp_array_ptr = self.builder.bitcast(array_ptr, ll.PointerType(self.fp_type))
  102. value = self.builder.load(fp_array_ptr)
  103. return value
  104. def _print_Symbol(self, s):
  105. val = self.tmp_var.get(s)
  106. if val:
  107. return val
  108. array, idx = self.func_arg_map.get(s, [None, 0])
  109. if not array:
  110. raise LookupError("Symbol not found: %s" % s)
  111. array_ptr = self.builder.gep(array, [ll.Constant(ll.IntType(32), idx)])
  112. fp_array_ptr = self.builder.bitcast(array_ptr,
  113. ll.PointerType(self.fp_type))
  114. value = self.builder.load(fp_array_ptr)
  115. return value
  116. # ensure lifetime of the execution engine persists (else call to compiled
  117. # function will seg fault)
  118. exe_engines = []
  119. # ensure names for generated functions are unique
  120. link_names = set()
  121. current_link_suffix = 0
  122. class LLVMJitCode:
  123. def __init__(self, signature):
  124. self.signature = signature
  125. self.fp_type = ll.DoubleType()
  126. self.module = ll.Module('mod1')
  127. self.fn = None
  128. self.llvm_arg_types = []
  129. self.llvm_ret_type = self.fp_type
  130. self.param_dict = {} # map symbol name to LLVM function argument
  131. self.link_name = ''
  132. def _from_ctype(self, ctype):
  133. if ctype == ctypes.c_int:
  134. return ll.IntType(32)
  135. if ctype == ctypes.c_double:
  136. return self.fp_type
  137. if ctype == ctypes.POINTER(ctypes.c_double):
  138. return ll.PointerType(self.fp_type)
  139. if ctype == ctypes.c_void_p:
  140. return ll.PointerType(ll.IntType(32))
  141. if ctype == ctypes.py_object:
  142. return ll.PointerType(ll.IntType(32))
  143. print("Unhandled ctype = %s" % str(ctype))
  144. def _create_args(self, func_args):
  145. """Create types for function arguments"""
  146. self.llvm_ret_type = self._from_ctype(self.signature.ret_type)
  147. self.llvm_arg_types = \
  148. [self._from_ctype(a) for a in self.signature.arg_ctypes]
  149. def _create_function_base(self):
  150. """Create function with name and type signature"""
  151. global link_names, current_link_suffix
  152. default_link_name = 'jit_func'
  153. current_link_suffix += 1
  154. self.link_name = default_link_name + str(current_link_suffix)
  155. link_names.add(self.link_name)
  156. fn_type = ll.FunctionType(self.llvm_ret_type, self.llvm_arg_types)
  157. self.fn = ll.Function(self.module, fn_type, name=self.link_name)
  158. def _create_param_dict(self, func_args):
  159. """Mapping of symbolic values to function arguments"""
  160. for i, a in enumerate(func_args):
  161. self.fn.args[i].name = str(a)
  162. self.param_dict[a] = self.fn.args[i]
  163. def _create_function(self, expr):
  164. """Create function body and return LLVM IR"""
  165. bb_entry = self.fn.append_basic_block('entry')
  166. builder = ll.IRBuilder(bb_entry)
  167. lj = LLVMJitPrinter(self.module, builder, self.fn,
  168. func_arg_map=self.param_dict)
  169. ret = self._convert_expr(lj, expr)
  170. lj.builder.ret(self._wrap_return(lj, ret))
  171. strmod = str(self.module)
  172. return strmod
  173. def _wrap_return(self, lj, vals):
  174. # Return a single double if there is one return value,
  175. # else return a tuple of doubles.
  176. # Don't wrap return value in this case
  177. if self.signature.ret_type == ctypes.c_double:
  178. return vals[0]
  179. # Use this instead of a real PyObject*
  180. void_ptr = ll.PointerType(ll.IntType(32))
  181. # Create a wrapped double: PyObject* PyFloat_FromDouble(double v)
  182. wrap_type = ll.FunctionType(void_ptr, [self.fp_type])
  183. wrap_fn = ll.Function(lj.module, wrap_type, "PyFloat_FromDouble")
  184. wrapped_vals = [lj.builder.call(wrap_fn, [v]) for v in vals]
  185. if len(vals) == 1:
  186. final_val = wrapped_vals[0]
  187. else:
  188. # Create a tuple: PyObject* PyTuple_Pack(Py_ssize_t n, ...)
  189. # This should be Py_ssize_t
  190. tuple_arg_types = [ll.IntType(32)]
  191. tuple_arg_types.extend([void_ptr]*len(vals))
  192. tuple_type = ll.FunctionType(void_ptr, tuple_arg_types)
  193. tuple_fn = ll.Function(lj.module, tuple_type, "PyTuple_Pack")
  194. tuple_args = [ll.Constant(ll.IntType(32), len(wrapped_vals))]
  195. tuple_args.extend(wrapped_vals)
  196. final_val = lj.builder.call(tuple_fn, tuple_args)
  197. return final_val
  198. def _convert_expr(self, lj, expr):
  199. try:
  200. # Match CSE return data structure.
  201. if len(expr) == 2:
  202. tmp_exprs = expr[0]
  203. final_exprs = expr[1]
  204. if len(final_exprs) != 1 and self.signature.ret_type == ctypes.c_double:
  205. raise NotImplementedError("Return of multiple expressions not supported for this callback")
  206. for name, e in tmp_exprs:
  207. val = lj._print(e)
  208. lj._add_tmp_var(name, val)
  209. except TypeError:
  210. final_exprs = [expr]
  211. vals = [lj._print(e) for e in final_exprs]
  212. return vals
  213. def _compile_function(self, strmod):
  214. global exe_engines
  215. llmod = llvm.parse_assembly(strmod)
  216. pmb = llvm.create_pass_manager_builder()
  217. pmb.opt_level = 2
  218. pass_manager = llvm.create_module_pass_manager()
  219. pmb.populate(pass_manager)
  220. pass_manager.run(llmod)
  221. target_machine = \
  222. llvm.Target.from_default_triple().create_target_machine()
  223. exe_eng = llvm.create_mcjit_compiler(llmod, target_machine)
  224. exe_eng.finalize_object()
  225. exe_engines.append(exe_eng)
  226. if False:
  227. print("Assembly")
  228. print(target_machine.emit_assembly(llmod))
  229. fptr = exe_eng.get_function_address(self.link_name)
  230. return fptr
  231. class LLVMJitCodeCallback(LLVMJitCode):
  232. def __init__(self, signature):
  233. super().__init__(signature)
  234. def _create_param_dict(self, func_args):
  235. for i, a in enumerate(func_args):
  236. if isinstance(a, IndexedBase):
  237. self.param_dict[a] = (self.fn.args[i], i)
  238. self.fn.args[i].name = str(a)
  239. else:
  240. self.param_dict[a] = (self.fn.args[self.signature.input_arg],
  241. i)
  242. def _create_function(self, expr):
  243. """Create function body and return LLVM IR"""
  244. bb_entry = self.fn.append_basic_block('entry')
  245. builder = ll.IRBuilder(bb_entry)
  246. lj = LLVMJitCallbackPrinter(self.module, builder, self.fn,
  247. func_arg_map=self.param_dict)
  248. ret = self._convert_expr(lj, expr)
  249. if self.signature.ret_arg:
  250. output_fp_ptr = builder.bitcast(self.fn.args[self.signature.ret_arg],
  251. ll.PointerType(self.fp_type))
  252. for i, val in enumerate(ret):
  253. index = ll.Constant(ll.IntType(32), i)
  254. output_array_ptr = builder.gep(output_fp_ptr, [index])
  255. builder.store(val, output_array_ptr)
  256. builder.ret(ll.Constant(ll.IntType(32), 0)) # return success
  257. else:
  258. lj.builder.ret(self._wrap_return(lj, ret))
  259. strmod = str(self.module)
  260. return strmod
  261. class CodeSignature:
  262. def __init__(self, ret_type):
  263. self.ret_type = ret_type
  264. self.arg_ctypes = []
  265. # Input argument array element index
  266. self.input_arg = 0
  267. # For the case output value is referenced through a parameter rather
  268. # than the return value
  269. self.ret_arg = None
  270. def _llvm_jit_code(args, expr, signature, callback_type):
  271. """Create a native code function from a SymPy expression"""
  272. if callback_type is None:
  273. jit = LLVMJitCode(signature)
  274. else:
  275. jit = LLVMJitCodeCallback(signature)
  276. jit._create_args(args)
  277. jit._create_function_base()
  278. jit._create_param_dict(args)
  279. strmod = jit._create_function(expr)
  280. if False:
  281. print("LLVM IR")
  282. print(strmod)
  283. fptr = jit._compile_function(strmod)
  284. return fptr
  285. @doctest_depends_on(modules=('llvmlite', 'scipy'))
  286. def llvm_callable(args, expr, callback_type=None):
  287. '''Compile function from a SymPy expression
  288. Expressions are evaluated using double precision arithmetic.
  289. Some single argument math functions (exp, sin, cos, etc.) are supported
  290. in expressions.
  291. Parameters
  292. ==========
  293. args : List of Symbol
  294. Arguments to the generated function. Usually the free symbols in
  295. the expression. Currently each one is assumed to convert to
  296. a double precision scalar.
  297. expr : Expr, or (Replacements, Expr) as returned from 'cse'
  298. Expression to compile.
  299. callback_type : string
  300. Create function with signature appropriate to use as a callback.
  301. Currently supported:
  302. 'scipy.integrate'
  303. 'scipy.integrate.test'
  304. 'cubature'
  305. Returns
  306. =======
  307. Compiled function that can evaluate the expression.
  308. Examples
  309. ========
  310. >>> import sympy.printing.llvmjitcode as jit
  311. >>> from sympy.abc import a
  312. >>> e = a*a + a + 1
  313. >>> e1 = jit.llvm_callable([a], e)
  314. >>> e.subs(a, 1.1) # Evaluate via substitution
  315. 3.31000000000000
  316. >>> e1(1.1) # Evaluate using JIT-compiled code
  317. 3.3100000000000005
  318. Callbacks for integration functions can be JIT compiled.
  319. >>> import sympy.printing.llvmjitcode as jit
  320. >>> from sympy.abc import a
  321. >>> from sympy import integrate
  322. >>> from scipy.integrate import quad
  323. >>> e = a*a
  324. >>> e1 = jit.llvm_callable([a], e, callback_type='scipy.integrate')
  325. >>> integrate(e, (a, 0.0, 2.0))
  326. 2.66666666666667
  327. >>> quad(e1, 0.0, 2.0)[0]
  328. 2.66666666666667
  329. The 'cubature' callback is for the Python wrapper around the
  330. cubature package ( https://github.com/saullocastro/cubature )
  331. and ( http://ab-initio.mit.edu/wiki/index.php/Cubature )
  332. There are two signatures for the SciPy integration callbacks.
  333. The first ('scipy.integrate') is the function to be passed to the
  334. integration routine, and will pass the signature checks.
  335. The second ('scipy.integrate.test') is only useful for directly calling
  336. the function using ctypes variables. It will not pass the signature checks
  337. for scipy.integrate.
  338. The return value from the cse module can also be compiled. This
  339. can improve the performance of the compiled function. If multiple
  340. expressions are given to cse, the compiled function returns a tuple.
  341. The 'cubature' callback handles multiple expressions (set `fdim`
  342. to match in the integration call.)
  343. >>> import sympy.printing.llvmjitcode as jit
  344. >>> from sympy import cse
  345. >>> from sympy.abc import x,y
  346. >>> e1 = x*x + y*y
  347. >>> e2 = 4*(x*x + y*y) + 8.0
  348. >>> after_cse = cse([e1,e2])
  349. >>> after_cse
  350. ([(x0, x**2), (x1, y**2)], [x0 + x1, 4*x0 + 4*x1 + 8.0])
  351. >>> j1 = jit.llvm_callable([x,y], after_cse) # doctest: +SKIP
  352. >>> j1(1.0, 2.0) # doctest: +SKIP
  353. (5.0, 28.0)
  354. '''
  355. if not llvmlite:
  356. raise ImportError("llvmlite is required for llvmjitcode")
  357. signature = CodeSignature(ctypes.py_object)
  358. arg_ctypes = []
  359. if callback_type is None:
  360. for _ in args:
  361. arg_ctype = ctypes.c_double
  362. arg_ctypes.append(arg_ctype)
  363. elif callback_type in ('scipy.integrate', 'scipy.integrate.test'):
  364. signature.ret_type = ctypes.c_double
  365. arg_ctypes = [ctypes.c_int, ctypes.POINTER(ctypes.c_double)]
  366. arg_ctypes_formal = [ctypes.c_int, ctypes.c_double]
  367. signature.input_arg = 1
  368. elif callback_type == 'cubature':
  369. arg_ctypes = [ctypes.c_int,
  370. ctypes.POINTER(ctypes.c_double),
  371. ctypes.c_void_p,
  372. ctypes.c_int,
  373. ctypes.POINTER(ctypes.c_double)
  374. ]
  375. signature.ret_type = ctypes.c_int
  376. signature.input_arg = 1
  377. signature.ret_arg = 4
  378. else:
  379. raise ValueError("Unknown callback type: %s" % callback_type)
  380. signature.arg_ctypes = arg_ctypes
  381. fptr = _llvm_jit_code(args, expr, signature, callback_type)
  382. if callback_type and callback_type == 'scipy.integrate':
  383. arg_ctypes = arg_ctypes_formal
  384. cfunc = ctypes.CFUNCTYPE(signature.ret_type, *arg_ctypes)(fptr)
  385. return cfunc