Skip to content

cartesian: computation_src cache in _make_extension_sources never hits (backend_data returns a copy) #2932

Description

@egparedes

Note

This issue was created by a coding agent (Claude) acting on behalf of @egparedes.

cc @FlorianDeconinck @romanc. Found while working on #2927.

Description

BaseGTBackend._make_extension_sources in src/gt4py/cartesian/backend/gtc_common.py tries to cache the generated C++ sources in the builder's backend data, but the cache is never actually stored, so it never hits:

def _make_extension_sources(self) -> dict[str, dict[str, str]]:
    """Generate the source for the stencil independently from use case."""
    if "computation_src" in self.builder.backend_data:          # L265
        return self.builder.backend_data["computation_src"]     # L266
    ...
    self.builder.backend_data["computation_src"] = gt_pyext_sources   # L280
    return gt_pyext_sources

StencilBuilder.backend_data returns a copy of the stored dict (src/gt4py/cartesian/stencil_builder.py, L219-220):

@property
def backend_data(self) -> dict[str, Any]:
    return self._build_data.get("backend_data", {}).copy()

so the assignment at L280 writes into a temporary copy that is immediately discarded, and the lookup at L265 never finds computation_src. The supported way to store backend data is self.builder.with_backend_data({...}), as done e.g. in numpy_backend.py (L82-84) and backend/base.py (L310).

Impact

Effectively none today: the only caller of _make_extension_sources is make_extension (gtc_common.py, L230), which runs once per build, so even a working cache would never be read. The code is therefore dead, but misleading.

Possible fixes

  1. Remove the cache (L265-266 and L280). Simplest, since nothing would read it.
  2. Make it work by storing it with self.builder.with_backend_data({"computation_src": gt_pyext_sources}). Only worth it if _make_extension_sources is expected to be called more than once per build.

Option 1 seems preferable unless there are plans to reuse the generated sources.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    gt4py.cartesianIssues concerning the current version with support only for cartesian grids.triage: bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions