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

Fix typing of cast_ builtin in ITIR type inference #1182

Merged
merged 6 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
28 changes: 27 additions & 1 deletion src/gt4py/next/iterator/type_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,6 @@ class LetPolymorphic(Type):
ret=Val_BOOL_T1,
),
"if_": FunctionType(args=Tuple.from_elems(Val_BOOL_T1, Val_T0_T1, Val_T0_T1), ret=Val_T0_T1),
"cast_": FunctionType(args=Tuple.from_elems(Val_T0_T1, Val_T0_T1), ret=Val_T0_T1),
"lift": FunctionType(
args=Tuple.from_elems(
FunctionType(
Expand Down Expand Up @@ -402,6 +401,7 @@ def visit_SymRef(
"shift",
"cartesian_domain",
"unstructured_domain",
"cast_",
):
raise TypeError(
f"Builtin '{node.id}' is only supported as applied/called function by the type checker"
Expand Down Expand Up @@ -477,6 +477,32 @@ def visit_FunCall(
)
constraints.add((tup, val))
return Val(kind=kind, dtype=elem, size=size)
if node.fun.id == "cast_":
Copy link
Contributor

Choose a reason for hiding this comment

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

The alternative would be to bring type of type into the typesystem, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I discussed the idea with @egparedes a while ago (in the context of the frontend). I believe the conclusion was to wait until we really need this.

if len(node.args) != 2:
raise TypeError("cast_ requires exactly two arguments.")
val_arg_type = self.visit(node.args[0], constraints=constraints, symtypes=symtypes)
type_arg = node.args[1]
if not isinstance(type_arg, ir.SymRef) or type_arg.id not in ir.TYPEBUILTINS:
raise TypeError("The second argument to `cast_` must be a type literal.")

size = TypeVar.fresh()

constraints.add(
(
val_arg_type,
Val(
kind=Value(),
dtype=TypeVar.fresh(),
size=size,
),
)
)

return Val(
kind=Value(),
dtype=Primitive(name=type_arg.id),
size=size,
)
if node.fun.id == "shift":
# Calls to shift are handled as being part of the grammar, not
# as function calls, as the type depends on the offset provider
Expand Down
10 changes: 10 additions & 0 deletions tests/next_tests/iterator_tests/test_type_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ def test_and():
assert ti.pformat(inferred) == "(bool⁰, bool⁰) → bool⁰"


def test_cast():
testee = ir.FunCall(
fun=ir.SymRef(id="cast_"), args=[ir.Literal(value="1.", type="float"), ir.SymRef(id="int")]
)
expected = ti.Val(kind=ti.Value(), dtype=ti.Primitive(name="int"), size=ti.TypeVar(idx=0))
inferred = ti.infer(testee)
assert inferred == expected
assert ti.pformat(inferred) == "int⁰"


def test_lift():
testee = ir.SymRef(id="lift")
expected = ti.FunctionType(
Expand Down