def test_branch_elimination(self): from nitrous.module import dump add_5 = False add_any = True @function(Long, a=Long, b=Bool) def f1(a, b): if add_any and b: a += 5 return a @function(Long, a=Long) def f2(a): if add_any and add_5: a += 5 return a m1 = module([f1]) ir = " ".join(dump(m1).split("\n")) # In first function, conditional depends on a parameter self.assertRegexpMatches(ir, "icmp") m2 = module([f2]) ir = " ".join(dump(m2).split("\n")) # In second, entire conditional is resolved at # compile time and optimized away self.assertNotRegexpMatches(ir, "icmp")
def test(self): from nitrous.module import dump @function(Float, a=Double) def cast_(a): return Float(a) m = module([cast_]) self.assertEqual(m.cast_(1.0), 1.0) self.assertRegexpMatches(dump(m), "trunc")
def test(self): from nitrous.module import dump from nitrous.function import options @options(inline=True) @function(Long, a=Long, b=Long) def foo(a, b): return a + b m = module([foo]) self.assertRegexpMatches(dump(m), "alwaysinline")
def test_cast_long_to_int(self): """Cast between wider and narrower integer""" from nitrous.lib import cast from nitrous.types import Int, Byte from nitrous.module import dump @function(Byte, a=Int) def int_to_long(a): return cast(a, Byte) m = module([int_to_long]) self.assertEqual(m.int_to_long(3), 3) self.assertRegexpMatches(dump(m), "trunc")
def test_cast_float_to_double(self): """Cast between narrower and wider float""" from nitrous.lib import cast from nitrous.types import Float from nitrous.module import dump @function(Double, a=Float) def float_to_double(a): return cast(a, Double) m = module([float_to_double]) self.assertEqual(m.float_to_double(1.0), 1.0) self.assertRegexpMatches(dump(m), "ext")
def test_cast_double_to_float(self): """Cast between wider and narrower float""" from nitrous.lib import cast from nitrous.types import Float from nitrous.module import dump @function(Float, a=Double) def double_to_float(a): return cast(a, Float) m = module([double_to_float]) self.assertEqual(m.double_to_float(1.0), 1.0) self.assertRegexpMatches(dump(m), "trunc")
def test_mixed_dynamic_dimension(self): """Some dimensions are dynamic, other than major one""" from nitrous.module import dump D = Slice(Long, shape=(Any, 3, Any)) X, Y, Z = range(3) @function(Long, a=D) def f(a): return a[2, 1, 2] m = module([f]) # Should have run-time multiplications during index flattening. self.assertRegexpMatches(dump(m), "mul") self.assertEqual(m.f(self.data), 14)
def test_all_dynamic_dimension(self): """All dimensions are dynamic, no indices can be resolved at runtime""" from nitrous.module import dump D = Slice(Long, shape=(Any, Any, Any)) X, Y, Z = range(3) @function(Long, a=D) def f(a): return a[2, 1, 2] m = module([f]) # Should have run-time multiplications during index flattening. self.assertRegexpMatches(dump(m), "mul") self.assertEqual(m.f(self.data), 14)
def test_static_dimension(self): """Replace access to known dimensions with direct constants""" from nitrous.module import dump D = Slice(Long, shape=(Any, 3, 3)) X, Y, Z = range(3) @function(Long, a=D) def f(a): return a[2, 1, 2] m = module([f]) # All indices should be resolved at run-time, so there should be no multiplications. self.assertNotRegexpMatches(dump(m), "mul") self.assertEqual(m.f(self.data), 14)
def test_encode_args(self): """Function name is uniqued according to its argument types.""" from nitrous.types.array import Slice from nitrous.types import Float, Double def get0(T): @function(T.element_type, v=T) def get0(v): return v[0] return get0 get0f = get0(Slice(Float)) get0d = get0(Slice(Double)) m = module([get0f, get0d]) ir = dump(m) self.assertIn("get0_RBdAnyf4", ir) self.assertIn("get0_RBdAnyf8", ir)