forked from numba/llvmlite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a couple of examples with opaque pointers
This patch adds a couple of examples on how to use the new opaque pointer builder interfaces. The examples are based on the already existing examples with the same name.
- Loading branch information
Showing
2 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import llvmlite | ||
llvmlite.opaque_pointers_enabled = True | ||
|
||
import llvmlite.ir as ll | ||
|
||
fntype = ll.FunctionType(ll.IntType(32), [ll.IntType(32), ll.IntType(32)]) | ||
|
||
module = ll.Module() | ||
|
||
func = ll.Function(module, fntype, name='foo') | ||
bb_entry = func.append_basic_block() | ||
|
||
builder = ll.IRBuilder() | ||
builder.position_at_end(bb_entry) | ||
|
||
stackint = builder.alloca(ll.IntType(32)) | ||
# Instead of stackint.type.pointee we can access stackint.allocated_type | ||
# directly. | ||
builder.store(ll.Constant(stackint.allocated_type, 123), stackint) | ||
myint = builder.load(stackint) | ||
|
||
addinstr = builder.add(func.args[0], func.args[1]) | ||
mulinstr = builder.mul(addinstr, ll.Constant(ll.IntType(32), 123)) | ||
pred = builder.icmp_signed('<', addinstr, mulinstr) | ||
builder.ret(mulinstr) | ||
|
||
bb_block = func.append_basic_block() | ||
builder.position_at_end(bb_block) | ||
|
||
bb_exit = func.append_basic_block() | ||
|
||
pred = builder.trunc(addinstr, ll.IntType(1)) | ||
builder.cbranch(pred, bb_block, bb_exit) | ||
|
||
builder.position_at_end(bb_exit) | ||
builder.ret(myint) | ||
|
||
print(module) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
from __future__ import print_function | ||
|
||
from ctypes import CFUNCTYPE, c_int, POINTER | ||
import sys | ||
try: | ||
from time import perf_counter as time | ||
except ImportError: | ||
from time import time | ||
|
||
import numpy as np | ||
|
||
try: | ||
import faulthandler; faulthandler.enable() | ||
except ImportError: | ||
pass | ||
|
||
import llvmlite | ||
llvmlite.opaque_pointers_enabled = True | ||
|
||
import llvmlite.ir as ll | ||
import llvmlite.binding as llvm | ||
|
||
|
||
llvm.initialize() | ||
llvm.initialize_native_target() | ||
llvm.initialize_native_asmprinter() | ||
|
||
|
||
t1 = time() | ||
|
||
# Pointers are opaque, so we should define them as such here. | ||
fnty = ll.FunctionType(ll.IntType(32), [ll.PointerType(), ll.IntType(32)]) | ||
module = ll.Module() | ||
|
||
func = ll.Function(module, fnty, name="sum") | ||
|
||
bb_entry = func.append_basic_block() | ||
bb_loop = func.append_basic_block() | ||
bb_exit = func.append_basic_block() | ||
|
||
builder = ll.IRBuilder() | ||
builder.position_at_end(bb_entry) | ||
|
||
builder.branch(bb_loop) | ||
builder.position_at_end(bb_loop) | ||
|
||
index = builder.phi(ll.IntType(32)) | ||
index.add_incoming(ll.Constant(index.type, 0), bb_entry) | ||
accum = builder.phi(ll.IntType(32)) | ||
accum.add_incoming(ll.Constant(accum.type, 0), bb_entry) | ||
|
||
# These GEP and load need an excplicit type. | ||
ptr = builder.gep(func.args[0], [index], source_etype=ll.IntType(32)) | ||
value = builder.load(ptr, typ=ll.IntType(32)) | ||
|
||
added = builder.add(accum, value) | ||
accum.add_incoming(added, bb_loop) | ||
|
||
indexp1 = builder.add(index, ll.Constant(index.type, 1)) | ||
index.add_incoming(indexp1, bb_loop) | ||
|
||
cond = builder.icmp_unsigned('<', indexp1, func.args[1]) | ||
builder.cbranch(cond, bb_loop, bb_exit) | ||
|
||
builder.position_at_end(bb_exit) | ||
builder.ret(added) | ||
|
||
strmod = str(module) | ||
|
||
t2 = time() | ||
|
||
print("-- generate IR:", t2-t1) | ||
|
||
t3 = time() | ||
|
||
llmod = llvm.parse_assembly(strmod) | ||
|
||
t4 = time() | ||
|
||
print("-- parse assembly:", t4-t3) | ||
|
||
print(llmod) | ||
|
||
pmb = llvm.create_pass_manager_builder() | ||
pmb.opt_level = 2 | ||
pm = llvm.create_module_pass_manager() | ||
pmb.populate(pm) | ||
|
||
t5 = time() | ||
|
||
pm.run(llmod) | ||
|
||
t6 = time() | ||
|
||
print("-- optimize:", t6-t5) | ||
|
||
t7 = time() | ||
|
||
target_machine = llvm.Target.from_default_triple().create_target_machine() | ||
|
||
with llvm.create_mcjit_compiler(llmod, target_machine) as ee: | ||
ee.finalize_object() | ||
cfptr = ee.get_function_address("sum") | ||
|
||
t8 = time() | ||
print("-- JIT compile:", t8 - t7) | ||
|
||
print(target_machine.emit_assembly(llmod)) | ||
|
||
cfunc = CFUNCTYPE(c_int, POINTER(c_int), c_int)(cfptr) | ||
A = np.arange(10, dtype=np.int32) | ||
res = cfunc(A.ctypes.data_as(POINTER(c_int)), A.size) | ||
|
||
print(res, A.sum()) | ||
|