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

feat[next]: Only inline scalars outside of stencils #1794

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions src/gt4py/next/iterator/transforms/inline_scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ def apply(cls, program: itir.Program, offset_provider_type: common.OffsetProvide
program = itir_inference.infer(program, offset_provider_type=offset_provider_type)
return cls().visit(program)

def generic_visit(self, node, **kwargs):
if cpm.is_call_to(node, "as_fieldop"):
return node

return super().generic_visit(node, **kwargs)

def visit_Expr(self, node: itir.Expr):
node = self.generic_visit(node)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 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
import pytest

from gt4py.next import common
from gt4py.next.iterator import ir as itir
from gt4py.next.type_system import type_specifications as ts
from gt4py.next.iterator.transforms import inline_scalar
from gt4py.next.iterator.ir_utils import ir_makers as im

TDim = common.Dimension(value="TDim")
int_type = ts.ScalarType(kind=ts.ScalarKind.INT32)


def program_factory(expr: itir.Expr) -> itir.Program:
return itir.Program(
id="testee",
function_definitions=[],
params=[im.sym("out", ts.FieldType(dims=[TDim], dtype=int_type))],
declarations=[],
body=[
itir.SetAt(
expr=expr,
target=im.ref("out"),
domain=im.domain(common.GridType.CARTESIAN, {TDim: (0, 1)}),
)
],
)


def test_simple():
testee = program_factory(im.let("a", 1)(im.op_as_fieldop("plus")("a", "a")))
expected = program_factory(im.op_as_fieldop("plus")(1, 1))
actual = inline_scalar.InlineScalar.apply(testee, offset_provider_type={})
assert actual == expected


def test_fo_inline_only():
scalar_expr = im.let("a", 1)(im.plus("a", "a"))
testee = program_factory(im.as_fieldop(im.lambda_()(scalar_expr))())
actual = inline_scalar.InlineScalar.apply(testee, offset_provider_type={})
assert actual == testee
Loading