Skip to content

Commit

Permalink
fix[cartesian]: Fix @gtscript.function inlining in while loops (#…
Browse files Browse the repository at this point in the history
…1669)

## Description

When calling a `@gtscript.function` from a `while` loop the
`CallInliner` responsible of inlining statements in `gtir` is missing a
`visit_While` leading to ejection of those statements to the first AST
that is parsed properly

## Requirements

- [x] All fixes and/or new features come with corresponding tests.

---------

Co-authored-by: Florian Deconinck <[email protected]>
  • Loading branch information
FlorianDeconinck and Florian Deconinck authored Sep 30, 2024
1 parent ad65d6b commit acce288
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/gt4py/cartesian/frontend/gtscript_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,10 @@ def visit_Assert(self, node: ast.Assert):
# Assertions are removed in the AssertionChecker later.
return node

def visit_While(self, node: ast.While):
node.body = self._process_stmts(node.body)
return node

def visit_Assign(self, node: ast.Assign):
if (
isinstance(node.value, ast.Call)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -716,3 +716,28 @@ def test(out: Field[np.float64], inp: GlobalTable[F64_VEC4]):
out = gt_storage.zeros(backend=backend, shape=(2, 2, 2), dtype=np.float64)
test(out, inp)
assert (out[:] == 42).all()


@pytest.mark.parametrize("backend", ALL_BACKENDS)
def test_function_inline_in_while(backend):
@gtscript.function
def add_42(v):
return v + 42

@gtscript.stencil(backend=backend)
def test(
in_field: Field[np.float64],
out_field: Field[np.float64],
):
with computation(PARALLEL), interval(...):
count = 1
while count < 10:
sa = add_42(out_field)
out_field = in_field + sa
count = count + 1

domain = (5, 5, 2)
in_arr = gt_storage.ones(backend=backend, shape=domain, dtype=np.float64)
out_arr = gt_storage.ones(backend=backend, shape=domain, dtype=np.float64)
test(in_arr, out_arr)
assert (out_arr[:, :, :] == 388.0).all()

0 comments on commit acce288

Please sign in to comment.