File: lljit.py

package info (click to toggle)
llvmlite 0.46.0-0.1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,140 kB
  • sloc: python: 13,605; cpp: 3,192; makefile: 185; sh: 168
file content (46 lines) | stat: -rw-r--r-- 1,346 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Tutorial example from
# https://llvmlite.readthedocs.io/en/latest/user-guide/binding/examples.html
# adapted to use LLJIT instead of MCJIT
#
# Additionally this example also demonstrates that typed pointers still work in
# LLVM 14 (and 15, if built against that).

from ctypes import CFUNCTYPE, POINTER, c_double

import llvmlite.binding as llvm
import numpy as np

llvm.initialize_native_target()
llvm.initialize_native_asmprinter()  # yes, even this one

llvm_ir = """
   ; ModuleID = "examples/ir_fpadd.py"
   target triple = "unknown-unknown-unknown"
   target datalayout = ""

   define double @"fpadd"(double %".1", double %".2", double* %"dummy")
   {
   entry:
     %"res" = fadd double %".1", %".2"
     %"val" = load double, double* %"dummy"
     %"res2" = fadd double %"res", %"val"
     ret double %"res2"
   }
   """

lljit = llvm.create_lljit_compiler()
rt = llvm.JITLibraryBuilder().add_ir(llvm_ir).export_symbol('fpadd')\
    .link(lljit, 'fpadd')

cfunc = CFUNCTYPE(c_double, c_double, c_double, POINTER(c_double))(rt['fpadd'])

# Create some floating point data and get a pointer to it that we can pass in
# to the jitted function
x = np.asarray([7.2])
data_ptr = x.ctypes.data_as(POINTER(c_double))


args = (1.0, 3.5, data_ptr)
res = cfunc(*args)
print(f"x[0] = {x[0]}")
print(f"fpadd({args[0]}, {args[1]}, &x[0]) = {res}")