Updated more tests to use unittest.

This commit is contained in:
Siu Kwan Lam 2012-07-20 21:46:23 -07:00
commit a9370d933d
5 changed files with 245 additions and 170 deletions

View file

@ -3,9 +3,14 @@
from llvm.core import Module,Type,Builder from llvm.core import Module,Type,Builder
from llvm.ee import ExecutionEngine from llvm.ee import ExecutionEngine
import llvm.core import llvm.core
import ctypes import ctypes
def test_jit_ctypes(): import logging
import unittest
class TestCallJITCtypes(unittest.TestCase):
def test_jit_ctypes(self):
# This example demonstrates calling an LLVM defined function using # This example demonstrates calling an LLVM defined function using
# ctypes. It illustrates the common C pattern of having an output # ctypes. It illustrates the common C pattern of having an output
@ -31,7 +36,8 @@ def test_jit_ctypes():
mult.args[0].name = "a" mult.args[0].name = "a"
mult.args[1].name = "b" mult.args[1].name = "b"
mult.args[2].name = "out" mult.args[2].name = "out"
mult.args[2].add_attribute(llvm.core.ATTR_NO_CAPTURE) # add nocapture to output arg # add nocapture to output arg
mult.args[2].add_attribute(llvm.core.ATTR_NO_CAPTURE)
mult.does_not_throw = True # add nounwind attribute to function mult.does_not_throw = True # add nounwind attribute to function
bb = mult.append_basic_block("entry") bb = mult.append_basic_block("entry")
@ -40,9 +46,8 @@ def test_jit_ctypes():
builder.store( tmp, mult.args[2] ) builder.store( tmp, mult.args[2] )
builder.ret(llvm.core.Constant.int(ty_errcode, 0)) builder.ret(llvm.core.Constant.int(ty_errcode, 0))
if 0:
# print the created module # print the created module
print(my_module) logging.debug(my_module)
# compile the function # compile the function
ee = ExecutionEngine.new(my_module) ee = ExecutionEngine.new(my_module)
@ -55,9 +60,11 @@ def test_jit_ctypes():
# now run the function, calling via ctypes # now run the function, calling via ctypes
output_value = ct_float(123456.0) output_value = ct_float(123456.0)
errcode = py_mult( 2.0, 3.0, ctypes.byref(output_value) ) errcode = py_mult( 2.0, 3.0, ctypes.byref(output_value) )
if errcode != 0:
raise RuntimeError('unexpected error') self.assertEqual(errcode, 0, msg='unexpected error')
assert output_value.value == 6.0
self.assertEqual(output_value.value, 6.0)
if __name__=='__main__': if __name__=='__main__':
test_jit_ctypes() unittest.main()

View file

@ -5,6 +5,12 @@ from llvm import *
from llvm.core import * from llvm.core import *
from llvm.ee import * # new import: ee = Execution Engine from llvm.ee import * # new import: ee = Execution Engine
import logging
import unittest
class TestExampleJIT(unittest.TestCase):
def test_example_jit(self):
# Create a module, as in the previous example. # Create a module, as in the previous example.
my_module = Module.new('my_module') my_module = Module.new('my_module')
ty_int = Type.int() # by default 32 bits ty_int = Type.int() # by default 32 bits
@ -22,12 +28,21 @@ builder.ret(tmp)
ee = ExecutionEngine.new(my_module) ee = ExecutionEngine.new(my_module)
# The arguments needs to be passed as "GenericValue" objects. # The arguments needs to be passed as "GenericValue" objects.
arg1 = GenericValue.int(ty_int, 100) arg1_value = 100
arg2 = GenericValue.int(ty_int, 42) arg2_value = 42
arg1 = GenericValue.int(ty_int, arg1_value)
arg2 = GenericValue.int(ty_int, arg2_value)
# Now let's compile and run! # Now let's compile and run!
retval = ee.run_function(f_sum, [arg1, arg2]) retval = ee.run_function(f_sum, [arg1, arg2])
# The return value is also GenericValue. Let's print it. # The return value is also GenericValue. Let's print it.
print("returned", retval.as_int()) logging.debug("returned %d", retval.as_int())
self.assertEqual(retval.as_int(), (arg1_value + arg2_value))
if __name__ == '__main__':
unittest.main()

View file

@ -4,6 +4,12 @@
from llvm import * from llvm import *
from llvm.core import * from llvm.core import *
import logging
import unittest
class TestExample(unittest.TestCase):
def test_example(self):
# Create an (empty) module. # Create an (empty) module.
my_module = Module.new('my_module') my_module = Module.new('my_module')
@ -20,6 +26,8 @@ ty_func = Type.function(ty_int, [ty_int, ty_int])
# free-standing (in llvm-py); it needs to be contained in a module. # free-standing (in llvm-py); it needs to be contained in a module.
f_sum = my_module.add_function(ty_func, "sum") f_sum = my_module.add_function(ty_func, "sum")
self.assertEqual(str(f_sum).strip(), 'declare i32 @sum(i32, i32)')
# Let's name the function arguments as 'a' and 'b'. # Let's name the function arguments as 'a' and 'b'.
f_sum.args[0].name = "a" f_sum.args[0].name = "a"
f_sum.args[1].name = "b" f_sum.args[1].name = "b"
@ -37,9 +45,17 @@ builder = Builder.new(bb)
# instruction that returns the sum as a value, which we'll use # instruction that returns the sum as a value, which we'll use
# a ret instruction to return. # a ret instruction to return.
tmp = builder.add(f_sum.args[0], f_sum.args[1], "tmp") tmp = builder.add(f_sum.args[0], f_sum.args[1], "tmp")
self.assertEqual(str(tmp).strip(), '%tmp = add i32 %a, %b')
builder.ret(tmp) builder.ret(tmp)
# We've completed the definition now! Let's see the LLVM assembly # We've completed the definition now! Let's see the LLVM assembly
# language representation of what we've created: # language representation of what we've created:
print(my_module) logging.debug(my_module)
if __name__ == '__main__':
unittest.main()

View file

@ -5,18 +5,28 @@
from llvm.core import * from llvm.core import *
from llvm.ee import * from llvm.ee import *
import logging
import unittest
import math
class TestIntrinsic(unittest.TestCase):
def test_bswap(self):
# setup a function and a builder # setup a function and a builder
mod = Module.new('test') mod = Module.new('test')
functy = Type.function(Type.void(), []) functy = Type.function(Type.int(), [])
func = mod.add_function(functy, "showme") func = mod.add_function(functy, "showme")
block = func.append_basic_block("entry") block = func.append_basic_block("entry")
b = Builder.new(block) b = Builder.new(block)
# let's do bswap on a 32-bit integer using llvm.bswap # let's do bswap on a 32-bit integer using llvm.bswap
val = Constant.int(Type.int(), 42) val = Constant.int(Type.int(), 0x42)
bswap = Function.intrinsic(mod, INTR_BSWAP, [Type.int()]) bswap = Function.intrinsic(mod, INTR_BSWAP, [Type.int()])
b.call(bswap, [val])
print(mod) bswap_res = b.call(bswap, [val])
b.ret(bswap_res)
# see the generated IR
logging.debug(mod)
# the output is: # the output is:
# #
@ -24,13 +34,19 @@ print(mod)
# #
# define void @showme() { # define void @showme() {
# entry: # entry:
# call i32 @llvm.bswap.i32( i32 42 ) ; <i32>:0 [#uses=0] # %0 = call i32 @llvm.bswap.i32(i32 42)
# ret i32 %0
# } # }
#
# declare i32 @llvm.bswap.i32(i32) nounwind readnone
#
# let's run the function
ee = ExecutionEngine.new(mod)
retval = ee.run_function(func, [])
self.assertEqual(retval.as_int(), 0x42000000)
def test_mysin(self):
# mysin(x) = sqrt(1.0 - pow(cos(x), 2)) # mysin(x) = sqrt(1.0 - pow(cos(x), 2))
mod = Module.new('test')
float = Type.float() float = Type.float()
mysinty = Type.function( float, [float] ) mysinty = Type.function( float, [float] )
@ -47,10 +63,11 @@ x = mysin.args[0]
one = Constant.real(float, "1") one = Constant.real(float, "1")
cosx = b.call(cos, [x], "cosx") cosx = b.call(cos, [x], "cosx")
cos2 = b.call(pow, [cosx, Constant.int(Type.int(), 2)], "cos2") cos2 = b.call(pow, [cosx, Constant.int(Type.int(), 2)], "cos2")
onemc2 = b.sub(one, cos2, "onemc2") onemc2 = b.fsub(one, cos2, "onemc2") # Should use fsub
sin = b.call(sqrt, [onemc2], "sin") sin = b.call(sqrt, [onemc2], "sin")
b.ret(sin) b.ret(sin)
print(mod)
logging.debug(mod)
# #
# ; ModuleID = 'test' # ; ModuleID = 'test'
@ -77,3 +94,17 @@ print(mod)
# #
# declare float @llvm.cos.f32(float) nounwind readnone # declare float @llvm.cos.f32(float) nounwind readnone
# #
# let's run the function
ee = ExecutionEngine.new(mod)
arg = GenericValue.real(Type.float(), 1.234)
retval = ee.run_function(mysin, [arg])
golden = math.sin(1.234)
answer = retval.as_real(Type.float())
self.assertLess(abs(answer-golden)/golden, 1e-5)
if __name__ == '__main__':
unittest.main()

View file

@ -1,8 +1,11 @@
#!/usr/bin/env python #!/usr/bin/env python
from llvm.core import * from llvm.core import *
import llvm._core
import unittest
class TestIssue10(unittest.TestCase):
def test_issue10(self):
m = Module.new('a') m = Module.new('a')
ti = Type.int() ti = Type.int()
tf = Type.function(ti, [ti, ti]) tf = Type.function(ti, [ti, ti])
@ -19,3 +22,6 @@ b = Builder.new(bb)
b.position_at_end(bb) b.position_at_end(bb)
b.position_at_beginning(bb) b.position_at_beginning(bb)
if __name__ == '__main__':
unittest.main()