Skip to content

Commit

Permalink
[next]: Fix inline lambda pass opcount preserving option (#1687)
Browse files Browse the repository at this point in the history
In #1531 the `itir.Node` class got a `type` attribute, that until now
contributed to the hash computation of all nodes. As such two
`itir.SymRef` with the same `id`, but one with a type inferred and one
without (i.e. `None`) got a different hash value. Consequently the
`inline_lambda` pass did not recognize them as a reference to the same
symbol and erroneously inlined the expression even with
`opcount_preserving=True`. This PR fixes the hash computation, such that
again `node1 == node2` implies `hash(node1) == hash(node2)`.
  • Loading branch information
tehrengruber authored Oct 14, 2024
1 parent b339b82 commit 9feb51d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/gt4py/next/iterator/ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,14 @@ def __str__(self) -> str:
return pformat(self)

def __hash__(self) -> int:
return hash(type(self)) ^ hash(
tuple(
hash(tuple(v)) if isinstance(v, list) else hash(v)
for v in self.iter_children_values()
return hash(
(
type(self),
*(
tuple(v) if isinstance(v, list) else v
for (k, v) in self.iter_children_items()
if k not in ["location", "type"]
),
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import pytest

from gt4py.next.type_system import type_specifications as ts
from gt4py.next.iterator.ir_utils import ir_makers as im
from gt4py.next.iterator.transforms.inline_lambdas import InlineLambdas

Expand Down Expand Up @@ -39,6 +40,21 @@
),
im.multiplies_(im.plus(2, 1), im.plus("x", "x")),
),
(
# ensure opcount preserving option works whether `itir.SymRef` has a type or not
"typed_ref",
im.let("a", im.call("opaque")())(
im.plus(im.ref("a", ts.ScalarType(kind=ts.ScalarKind.FLOAT32)), im.ref("a", None))
),
{
True: im.let("a", im.call("opaque")())(
im.plus( # stays as is
im.ref("a", ts.ScalarType(kind=ts.ScalarKind.FLOAT32)), im.ref("a", None)
)
),
False: im.plus(im.call("opaque")(), im.call("opaque")()),
},
),
]


Expand Down

0 comments on commit 9feb51d

Please sign in to comment.