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

bug[next]: Fix shift / remap lowering #1231

Merged
merged 7 commits into from
Apr 24, 2023

Conversation

tehrengruber
Copy link
Contributor

@tehrengruber tehrengruber commented Apr 12, 2023

While working on something else I recognized a rather sneaky bug in the lowering of shifts from FOAST to ITIR. Contrary to the rest of the lowering we were not wrapping the shifts inside a lifted stencil, which can lead to bugs in user code. This is particularly devastating when executing with the gtfn backend in Release Mode, as you might just run into a segfault or incorrect results. I am actually rather surprised this did not surface earlier somewhere as something like this

@field_operator(backend=fieldview_backend)
def composed_shift_unstructured_intermediate_result(
    inp: Field[[Vertex], float64]
) -> Field[[Cell], float64]:
    tmp = inp(E2V[0])
    return tmp(C2E[0])

lowers to the (incorrect) ITIR:

λ(inp) → ·(λ(tmp__0) → ⟪C2Eₒ, 0ₒ⟫(tmp__0))(⟪E2Vₒ, 0ₒ⟫(inp))

(Note how the ordering of the shifts is wrong). I had found this bug much earlier while working on (#965), but sadly the change went lost while merging (was very easy to overlook so @havogt and me bost missed it). What I did not recognize back then was that this bug could actually be triggered. I assumed it only occurred for expression like field(E2V[0])(C2E[0]) which were not allowed (for unrelated reasons). For completeness this PR also adds support for such expressions. In principle it is possible to remove this feature from the PR.

@@ -206,17 +206,19 @@ def _visit_shift(self, node: foast.Call, **kwargs) -> itir.Expr:
)
case _:
raise FieldOperatorLoweringError("Unexpected shift arguments!")
return shift_offset(self.visit(node.func, **kwargs))
return im.lift_(im.lambda__("it")(im.deref_(shift_offset("it"))))(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For the reviewer this is the core of this PR.

@havogt havogt self-requested a review April 12, 2023 12:49
Comment on lines 549 to 574
func_str_repr: str # just for error handling
if isinstance(
new_func.type,
(ts.FunctionType, ts_ffront.FieldOperatorType, ts_ffront.ScanOperatorType),
):
# Since we use the `id` attribute in the latter part of the toolchain ensure we
# have the proper format here.
if not isinstance(
new_func,
(foast.FunctionDefinition, foast.FieldOperator, foast.ScanOperator, foast.Name),
):
raise FieldOperatorTypeDeductionError.from_foast_node(
node, msg="Functions can only be called directly!"
)
func_str_repr = new_func.id
elif isinstance(new_func.type, ts.FieldType):
func_str_repr = str(new_func)
else:
raise FieldOperatorTypeDeductionError.from_foast_node(
node,
msg=f"Expression of type `{new_func.type}` is not callable, must be a `Function`, `FieldOperator`, `ScanOperator` or `Field`.",
)

# ensure signature is valid
try:
type_info.accepts_args(
Copy link
Contributor

Choose a reason for hiding this comment

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

This snippet, including the type_info.accepts_args is confusing. How does that work in case of new_func.type == FieldType? Let's talk about it.

@@ -556,7 +579,7 @@ def visit_Call(self, node: foast.Call, **kwargs) -> foast.Call:
)
except GTTypeError as err:
raise FieldOperatorTypeDeductionError.from_foast_node(
node, msg=f"Invalid argument types in call to `{node.func.id}`!"
node, msg=f"Invalid argument types in call to `{func_str_repr}`!"
Copy link
Contributor

Choose a reason for hiding this comment

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

Once you have implemented the change that you wanted to make to avoid func_str_repr, it should be good to go.

@tehrengruber tehrengruber merged commit 4998dec into GridTools:main Apr 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants