Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Builder] Add index from/to Float/Fixed/UFixed type casting #242

Merged
merged 2 commits into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions allo/ir/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ def build_cast_op(ctx, op, src_type, res_type, shape=None):
if type(res_type) is type(src_type) and res_type == src_type:
return op

# Single-step type conversions
cast_map = {
# Index <-> UInt/Int
(Int, Index): arith_d.IndexCastOp,
Expand All @@ -367,9 +368,6 @@ def build_cast_op(ctx, op, src_type, res_type, shape=None):
(UInt, Float): arith_d.UIToFPOp,
(Float, Int): arith_d.FPToSIOp,
(Float, UInt): arith_d.FPToUIOp,
# FP to Index is not supported in MLIR
# (Float, Index): RuntimeError,
# (Index, Float): RuntimeError,
Comment on lines -370 to -372
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add float<->index testing?

# Float <-> Fixed/UFixed
(Float, Fixed): allo_d.FloatToFixedOp,
(Float, UFixed): allo_d.FloatToFixedOp,
Expand Down Expand Up @@ -402,6 +400,16 @@ def build_cast_op(ctx, op, src_type, res_type, shape=None):
IntegerType.get_signless(32), op.result, ip=ctx.get_ip()
)
opcls = arith_d.SIToFPOp # proceed to build cast to float
elif isinstance(src_type, Index) and isinstance(res_type, (Fixed, UFixed)):
op = arith_d.IndexCastOp(
IntegerType.get_signless(32), op.result, ip=ctx.get_ip()
)
opcls = allo_d.IntToFixedOp # proceed to build cast to float
elif isinstance(src_type, (Fixed, UFixed)) and isinstance(res_type, Index):
op = allo_d.FixedToIntOp(
IntegerType.get_signless(32), op.result, ip=ctx.get_ip()
)
opcls = arith_d.IndexCastOp
elif isinstance(src_type, (Int, UInt)) and isinstance(res_type, (Int, UInt)):
if src_type.bits > res_type.bits:
opcls = arith_d.TruncIOp
Expand Down
39 changes: 39 additions & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
uint1,
int32,
float32,
index,
)
import allo.ir.types as T

Expand Down Expand Up @@ -42,6 +43,44 @@ def kernel(a: int32) -> float32:
assert mod(1) == kernel(1)


def test_index_fixed_casting():
def test_one_cast(fixed):
def kernel(a: fixed) -> float32:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where a is used here?

sum_val: fixed = 0.0
ret_val: float32 = 0.0
for step in range(10):
sum_val += step
ret_val = sum_val
return ret_val

s = allo.customize(kernel)
mod = s.build()
assert mod(1) == kernel(1)

for i in range(1, 20):
test_one_cast(Fixed(32, i))
test_one_cast(UFixed(32, i))


def test_fixed_index_casting():

def test_one_cast(fixed):
def kernel(a: float32) -> int32:
a_fix: fixed = a
a_idx: index = a_fix
b: index = 2
ret: int32 = a_idx + b
return ret

s = allo.customize(kernel)
mod = s.build()
assert mod(2.0) == kernel(2.0)

for i in range(1, 20):
test_one_cast(Fixed(32, i))
test_one_cast(UFixed(32, i))


def test_large_bitwidth():
def kernel(a: Int(65536)[1], b: Int(345)[1], c: Int(65536)[1]):
c[0] = a[0] + b[0]
Expand Down