From f1e10e849f63210b491d1be4b0c548f00330182c Mon Sep 17 00:00:00 2001 From: Siu Kwan Lam Date: Fri, 3 Aug 2012 17:02:01 -0700 Subject: [PATCH] Fix __div__ for py3. Add test for nested-loops. --- llvm_cbuilder/builder.py | 3 + tests/test_atomic_add.py | 4 +- ..._atomic_ldst.py => test_atomic_cmpxchg.py} | 4 +- tests/test_nestedloops.py | 128 ++++++++++++++++++ 4 files changed, 135 insertions(+), 4 deletions(-) rename tests/{test_atomic_ldst.py => test_atomic_cmpxchg.py} (97%) create mode 100644 tests/test_nestedloops.py diff --git a/llvm_cbuilder/builder.py b/llvm_cbuilder/builder.py index 5bc31d1..0582a25 100644 --- a/llvm_cbuilder/builder.py +++ b/llvm_cbuilder/builder.py @@ -379,6 +379,9 @@ class CValue(object): def __div__(self, rhs): return self._use_binop('div')(rhs) + def __truediv__(self, rhs): + return self.__div__(rhs) + def __mod__(self, rhs): return self._use_binop('mod')(rhs) diff --git a/tests/test_atomic_add.py b/tests/test_atomic_add.py index eb63a44..63b795a 100644 --- a/tests/test_atomic_add.py +++ b/tests/test_atomic_add.py @@ -65,8 +65,8 @@ def gen_test_pthread(mod): cb.close() return cb.function -class TestPThread(unittest.TestCase): - def test_pthread(self): +class TestAtomicAdd(unittest.TestCase): + def test_atomic_add(self): mod = Module.new(__name__) # add pthread functions diff --git a/tests/test_atomic_ldst.py b/tests/test_atomic_cmpxchg.py similarity index 97% rename from tests/test_atomic_ldst.py rename to tests/test_atomic_cmpxchg.py index 147ec59..eb2a45b 100644 --- a/tests/test_atomic_ldst.py +++ b/tests/test_atomic_cmpxchg.py @@ -70,8 +70,8 @@ def gen_test_pthread(mod): cb.close() return cb.function -class TestPThread(unittest.TestCase): - def test_pthread(self): +class TestAtomicCmpXchg(unittest.TestCase): + def test_atomic_cmpxchg(self): mod = Module.new(__name__) # add pthread functions diff --git a/tests/test_nestedloops.py b/tests/test_nestedloops.py new file mode 100644 index 0000000..823a54f --- /dev/null +++ b/tests/test_nestedloops.py @@ -0,0 +1,128 @@ +from llvm.core import * +from llvm.passes import * +from llvm.ee import * +from llvm_cbuilder import * +import llvm_cbuilder.shortnames as C +import unittest, logging + +def nestedloop1(d): + z = 0 + for x in range(100): + for y in range(100): + z += x * d + int(y / d) + return z + +def gen_nestedloop1(mod): + functype = Type.function(C.int, [C.int]) + func = mod.add_function(functype, 'nestedloop1') + + cb = CBuilder(func) + + d = cb.args[0] + x = cb.var(C.int) + y = cb.var(C.int) + z = cb.var(C.int) + + one = cb.constant(C.int, 1) + zero = cb.constant(C.int, 0) + limit = cb.constant(C.int, 100) + + z.assign(zero) + x.assign(zero) + with cb.loop() as outer: + with outer.condition() as setcond: + setcond( x < limit ) + + with outer.body(): + y.assign(zero) + with cb.loop() as inner: + with inner.condition() as setcond: + setcond( y < limit ) + + with inner.body(): + z += x * d + y / d + y += one + x += one + + cb.ret(z) + cb.close() + return func + + +def nestedloop2(d): + z = 0 + for x in range(1, 100): + for y in range(1, 100): + if x > y: + z += int(x / y) * d + else: + z += int(y / x) * d + return z + +def gen_nestedloop2(mod): + functype = Type.function(C.int, [C.int]) + func = mod.add_function(functype, 'nestedloop1') + + cb = CBuilder(func) + + d = cb.args[0] + x = cb.var(C.int) + y = cb.var(C.int) + z = cb.var(C.int) + + one = cb.constant(C.int, 1) + zero = cb.constant(C.int, 0) + limit = cb.constant(C.int, 100) + + z.assign(zero) + x.assign(one) + with cb.loop() as outer: + with outer.condition() as setcond: + setcond( x < limit ) + + with outer.body(): + y.assign(one) + with cb.loop() as inner: + with inner.condition() as setcond: + setcond( y < limit ) + + with inner.body(): + with cb.ifelse(x > y) as ifelse: + with ifelse.then(): + z += x / y * d + with ifelse.otherwise(): + z += y / x * d + y += one + x += one + + cb.ret(z) + cb.close() + return func + + +class TestNestedLoop(unittest.TestCase): + def test_nestedloop1(self): + mod = Module.new(__name__) + lfunc = gen_nestedloop1(mod) + logging.debug(mod) + mod.verify() + + exe = CExecutor(mod) + func = exe.get_ctype_function(lfunc, 'int, int') + for x in range(1, 100): + self.assertEqual(func(x), int(nestedloop1(x))) + + def test_nestedloop2(self): + mod = Module.new(__name__) + lfunc = gen_nestedloop2(mod) + logging.debug(mod) + mod.verify() + + exe = CExecutor(mod) + func = exe.get_ctype_function(lfunc, 'int, int') + for x in range(1, 100): + self.assertEqual(func(x), int(nestedloop2(x))) + +if __name__ == '__main__': + unittest.main() +