1
0
mirror of https://github.com/exaloop/codon.git synced 2025-06-03 15:03:52 +08:00
codon/test/transform/canonical.codon
A. R. Shajii ebd344f894
GPU and other updates (#52)
* Add nvptx pass

* Fix spaces

* Don't change name

* Add runtime support

* Add init call

* Add more runtime functions

* Add launch function

* Add intrinsics

* Fix codegen

* Run GPU pass between general opt passes

* Set data layout

* Create context

* Link libdevice

* Add function remapping

* Fix linkage

* Fix libdevice link

* Fix linking

* Fix personality

* Fix linking

* Fix linking

* Fix linking

* Add internalize pass

* Add more math conversions

* Add more re-mappings

* Fix conversions

* Fix __str__

* Add decorator attribute for any decorator

* Update kernel decorator

* Fix kernel decorator

* Fix kernel decorator

* Fix kernel decorator

* Fix kernel decorator

* Remove old decorator

* Fix pointer calc

* Fix fill-in codegen

* Fix linkage

* Add comment

* Update list conversion

* Add more conversions

* Add dict and set conversions

* Add float32 type to IR/LLVM

* Add float32

* Add float32 stdlib

* Keep required global values in PTX module

* Fix PTX module pruning

* Fix malloc

* Set will-return

* Fix name cleanup

* Fix access

* Fix name cleanup

* Fix function renaming

* Update dimension API

* Fix args

* Clean up API

* Move GPU transformations to end of opt pipeline

* Fix alloc replacements

* Fix naming

* Target PTX 4.2

* Fix global renaming

* Fix early return in static blocks; Add __realized__ function

* Format

* Add __llvm_name__ for functions

* Add vector type to IR

* SIMD support [wip]

* Update kernel naming

* Fix early returns; Fix SIMD calls

* Fix kernel naming

* Fix IR matcher

* Remove module print

* Update realloc

* Add overloads for 32-bit float math ops

* Add gpu.Pointer type for working with raw pointers

* Add float32 conversion

* Add to_gpu and from_gpu

* clang-format

* Add f32 reduction support to OpenMP

* Fix automatic GPU class conversions

* Fix conversion functions

* Fix conversions

* Rename self

* Fix tuple conversion

* Fix conversions

* Fix conversions

* Update PTX filename

* Fix filename

* Add raw function

* Add GPU docs

* Allow nested object conversions

* Add tests (WIP)

* Update SIMD

* Add staticrange and statictuple loop support

* SIMD updates

* Add new Vec constructors

* Fix UInt conversion

* Fix size-0 allocs

* Add more tests

* Add matmul test

* Rename gpu test file

* Add more tests

* Add alloc cache

* Fix object_to_gpu

* Fix frees

* Fix str conversion

* Fix set conversion

* Fix conversions

* Fix class conversion

* Fix str conversion

* Fix byte conversion

* Fix list conversion

* Fix pointer conversions

* Fix conversions

* Fix conversions

* Update tests

* Fix conversions

* Fix tuple conversion

* Fix tuple conversion

* Fix auto conversions

* Fix conversion

* Fix magics

* Update tests

* Support GPU in JIT mode

* Fix GPU+JIT

* Fix kernel filename in JIT mode

* Add __static_print__; Add earlyDefines; Various domination bugfixes; SimplifyContext RAII base handling

* Fix global static handling

* Fix float32 tests

* FIx gpu module

* Support OpenMP "collapse" option

* Add more collapse tests

* Capture generics and statics

* TraitVar handling

* Python exceptions / isinstance [wip; no_ci]

* clang-format

* Add list comparison operators

* Support empty raise in IR

* Add dict 'or' operator

* Fix repr

* Add copy module

* Fix spacing

* Use sm_30

* Python exceptions

* TypeTrait support; Fix defaultDict

* Fix earlyDefines

* Add defaultdict

* clang-format

* Fix invalid canonicalizations

* Fix empty raise

* Fix copyright

* Add Python numerics option

* Support py-numerics in math module

* Update docs

* Add static Python division / modulus

* Add static py numerics tests

* Fix staticrange/tuple; Add KwTuple.__getitem__

* clang-format

* Add gpu parameter to par

* Fix globals

* Don't init loop vars on loop collapse

* Add par-gpu tests

* Update gpu docs

* Fix isinstance check

* Remove invalid test

* Add -libdevice to set custom path [skip ci]

* Add release notes; bump version [skip ci]

* Add libdevice docs [skip ci]

Co-authored-by: Ibrahim Numanagić <ibrahimpasa@gmail.com>
2022-09-15 15:40:00 -04:00

201 lines
4.9 KiB
Python

@tuple
class Vec[T]:
x: T
y: T
@pure
def __abs__(self):
return ((self.x * self.x) + (self.y * self.y)) ** 0.5
@pure
@commutative
@associative
def __add__(self, other: Vec[T]):
print 'vec add', self, other
return Vec[T](self.x + other.x, self.y + other.y)
@pure
@commutative
@associative
def __add__(self, other: T):
print 'vec add', self, other
return Vec[T](self.x + other, self.y + other)
@pure
def __sub__(self, other: Vec[T]):
print 'vec sub', self, other
return Vec[T](self.x - other.x, self.y - other.y)
@pure
def __sub__(self, other: T):
print 'vec sub', self, other
return Vec[T](self.x - other, self.y - other)
@pure
@commutative
@associative
@distributive
def __mul__(self, other: Vec[T]):
print 'vec mul', self, other
return Vec[T](self.x * other.x, self.y * other.y)
@pure
@commutative
@associative
@distributive
def __mul__(self, other: T):
print 'vec mul', self, other
return Vec[T](self.x * other, self.y * other)
@pure
@commutative
def __eq__(self, other: Vec[T]):
print 'vec eq', self, other
return abs(self) == abs(other)
@pure
@commutative
def __ne__(self, other: Vec[T]):
print 'vec ne', self, other
return abs(self) != abs(other)
@pure
def __lt__(self, other: Vec[T]):
print 'vec lt', self, other
return abs(self) < abs(other)
@pure
def __le__(self, other: Vec[T]):
print 'vec le', self, other
return abs(self) <= abs(other)
@pure
def __gt__(self, other: Vec[T]):
print 'vec gt', self, other
return abs(self) > abs(other)
@pure
def __ge__(self, other: Vec[T]):
print 'vec ge', self, other
return abs(self) >= abs(other)
@test
def test_op_chain_canon():
@pure
def f(a): return a
a = Vec(1, 2)
b = Vec(3, 4)
c = a + f(b) # -> f(b) + a
assert (c.x, c.y) == (4, 6)
# EXPECT: vec add (x: 3, y: 4) (x: 1, y: 2)
a = Vec(1, 2)
b = Vec(3, 4)
c = Vec(5, 6)
d = f(a + f(b) + f(f(c))) # -> f(f(f(c)) + f(b) + a)
assert (d.x, d.y) == (9, 12)
# EXPECT: vec add (x: 5, y: 6) (x: 3, y: 4)
# EXPECT: vec add (x: 8, y: 10) (x: 1, y: 2)
a = Vec(1, 2)
b = Vec(3, 4)
c = Vec(5, 6)
d = f(a + (f(b) + f(f(c)))) # -> f(f(f(c)) + f(b) + a)
assert (d.x, d.y) == (9, 12)
# EXPECT: vec add (x: 5, y: 6) (x: 3, y: 4)
# EXPECT: vec add (x: 8, y: 10) (x: 1, y: 2)
a = Vec(1, 2)
b = Vec(3, 4)
c = a - f(b) # -> no change
assert (c.x, c.y) == (-2, -2)
# EXPECT: vec sub (x: 1, y: 2) (x: 3, y: 4)
# don't canon float ops
assert f(1e100) + f(f(-1e100)) + f(f(f(1.))) == 1.
test_op_chain_canon()
class C:
n: int
def __lt__(self: C, other: C):
return self.n < other.n
@test
def test_inequality_canon():
@pure
def f(a): return a
a = Vec(1,1)
b = Vec(2,2)
assert not (f(a) == b)
assert f(a) != b
assert f(a) < b
assert f(a) <= b
assert not (f(a) > b)
assert not (f(a) >= b)
# EXPECT: vec eq (x: 1, y: 1) (x: 2, y: 2)
# EXPECT: vec ne (x: 1, y: 1) (x: 2, y: 2)
# EXPECT: vec lt (x: 1, y: 1) (x: 2, y: 2)
# EXPECT: vec le (x: 1, y: 1) (x: 2, y: 2)
# EXPECT: vec gt (x: 1, y: 1) (x: 2, y: 2)
# EXPECT: vec ge (x: 1, y: 1) (x: 2, y: 2)
assert not (a == f(b))
assert a != f(b)
assert a < f(b)
assert a <= f(b)
assert not (a > f(b))
assert not (a >= f(b))
# EXPECT: vec eq (x: 2, y: 2) (x: 1, y: 1)
# EXPECT: vec ne (x: 2, y: 2) (x: 1, y: 1)
# EXPECT: vec gt (x: 2, y: 2) (x: 1, y: 1)
# EXPECT: vec ge (x: 2, y: 2) (x: 1, y: 1)
# EXPECT: vec lt (x: 2, y: 2) (x: 1, y: 1)
# EXPECT: vec le (x: 2, y: 2) (x: 1, y: 1)
c1 = C(1)
c2 = C(2)
# ensure we don't use missing ops
assert c1 < f(c2)
test_inequality_canon()
@test
def test_add_mul_canon():
@pure
def f(a): return a
a = Vec(1,1)
b = Vec(2,2)
c = Vec(3,3)
d = (a*f(b) + c*a) # -> (f(b) + c) * a
assert (d.x, d.y) == (5, 5)
# EXPECT: vec add (x: 2, y: 2) (x: 3, y: 3)
# EXPECT: vec mul (x: 5, y: 5) (x: 1, y: 1)
d = (a + c*a) # -> (c + 1) * a
assert (d.x, d.y) == (4, 4)
# EXPECT: vec add (x: 3, y: 3) 1
# EXPECT: vec mul (x: 4, y: 4) (x: 1, y: 1)
d = (c*a + a) # -> (c + 1) * a
assert (d.x, d.y) == (4, 4)
# EXPECT: vec add (x: 3, y: 3) 1
# EXPECT: vec mul (x: 4, y: 4) (x: 1, y: 1)
a = Vec(1,1)
b = a + a + a + a + a
assert (b.x, b.y) == (5, 5)
# EXPECT: vec mul (x: 1, y: 1) 5
a = Vec(1,1)
b = a + a*2 + a*3 + a*4 + a*5
assert (b.x, b.y) == (15, 15)
# EXPECT: vec mul (x: 1, y: 1) 15
x = f(100.) # don't distribute float ops
assert (x * 0.1) + (x * 0.2) == 30.
test_add_mul_canon()