-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat[next]: Add IR transform to remove unnecessary cast expressions (#…
…1688) Add IR transformation that removes cast expressions where the argument is already in the target type.
- Loading branch information
Showing
3 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# GT4Py - GridTools Framework | ||
# | ||
# Copyright (c) 2014-2024, ETH Zurich | ||
# All rights reserved. | ||
# | ||
# Please, refer to the LICENSE file in the root directory. | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
|
||
from gt4py.eve import NodeTranslator, PreserveLocationVisitor | ||
from gt4py.next.iterator import ir | ||
from gt4py.next.iterator.ir_utils import common_pattern_matcher as cpm | ||
from gt4py.next.type_system import type_specifications as ts | ||
|
||
|
||
class PruneCasts(PreserveLocationVisitor, NodeTranslator): | ||
""" | ||
Removes cast expressions where the argument is already in the target type. | ||
This transformation requires the IR to be fully type-annotated, | ||
therefore it should be applied after type-inference. | ||
""" | ||
|
||
def visit_FunCall(self, node: ir.FunCall) -> ir.Node: | ||
node = self.generic_visit(node) | ||
|
||
if not cpm.is_call_to(node, "cast_"): | ||
return node | ||
|
||
value, type_constructor = node.args | ||
|
||
assert ( | ||
value.type | ||
and isinstance(type_constructor, ir.SymRef) | ||
and (type_constructor.id in ir.TYPEBUILTINS) | ||
) | ||
dtype = ts.ScalarType(kind=getattr(ts.ScalarKind, type_constructor.id.upper())) | ||
|
||
if value.type == dtype: | ||
return value | ||
|
||
return node | ||
|
||
@classmethod | ||
def apply(cls, node: ir.Node) -> ir.Node: | ||
return cls().visit(node) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
tests/next_tests/unit_tests/iterator_tests/transforms_tests/test_prune_casts.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# GT4Py - GridTools Framework | ||
# | ||
# Copyright (c) 2014-2024, ETH Zurich | ||
# All rights reserved. | ||
# | ||
# Please, refer to the LICENSE file in the root directory. | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
|
||
from gt4py.next.iterator.ir_utils import ir_makers as im | ||
from gt4py.next.type_system import type_specifications as ts | ||
from gt4py.next.iterator.transforms.prune_casts import PruneCasts | ||
from gt4py.next.iterator.type_system import inference as type_inference | ||
|
||
|
||
def test_prune_casts_simple(): | ||
x_ref = im.ref("x", ts.ScalarType(kind=ts.ScalarKind.FLOAT32)) | ||
y_ref = im.ref("y", ts.ScalarType(kind=ts.ScalarKind.FLOAT64)) | ||
testee = im.call("plus")(im.call("cast_")(x_ref, "float64"), im.call("cast_")(y_ref, "float64")) | ||
testee = type_inference.infer(testee, offset_provider={}, allow_undeclared_symbols=True) | ||
|
||
expected = im.call("plus")(im.call("cast_")(x_ref, "float64"), y_ref) | ||
actual = PruneCasts.apply(testee) | ||
assert actual == expected |