Skip to content

refactor[next]: fbuiltins declares the builtins, embedded implements them - #3033

Draft
havogt wants to merge 7 commits into
mainfrom
fbuiltins-without-embedded
Draft

havogt wants to merge 7 commits into
mainfrom
fbuiltins-without-embedded

Conversation

@havogt

@havogt havogt commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Description

ffront.fbuiltins (and ffront.experimental) only declare the builtins of the DSL; calling a builtin with values is embedded execution, which the new embedded/builtins.py implements. fbuiltins no longer contains NumPy/math computations or the tuple/named-collection mapping.

fbuiltins: declarations

  • BuiltInFunction.definition (renamed from function) is the declaration: its signature gives __gt_type__, its docstring __doc__. The bodies are .... WhereBuiltinFunction keeps its overloads, for static typing.
  • BuiltInFunction.__call__ hands the call to fbuiltins.call_implementation, a dependency inversion like iterator.runtime.fendef_embedded. It defaults to raising a RuntimeError, and gt4py.next.embedded sets it on import. BuiltInFunction.dispatch is gone.
  • The math builtin name lists are literal; they were derived from the implementation dicts.

embedded/builtins.py: the implementation

A call, in order:

  1. maps over tuples and named collections, for where and concat_where (the branches) and astype (the value). The mapping and its checks move verbatim from WhereBuiltinFunction.__call__ and astype's body.
  2. runs the implementation of the first argument whose type implements the builtin (__gt_builtin_func__, the common.Field protocol; NdArrayField and its registry are unchanged);
  3. runs the scalar implementation: the former default bodies, moved verbatim, minus a per-call runtime cast.

An import-time assert checks that every builtin declared in fbuiltins/experimental has a scalar implementation or is field-only (neighbor_sum, max_over, min_over, where, as_offset, concat_where).

User-visible changes

  • Scalar results are unchanged: astype(math.pi, float32) is np.float32, abs(-1) is int, gamma(float32) stays float32. The tests pin these.
  • A builtin called with arguments it has no embedded implementation for raises TypeError naming the builtin. Before, it raised a bare NotImplementedError (where/neighbor_sum/concat_where with scalars) or an AssertionError (e.g. sqrt(np.ones(2))).
  • BuiltInFunction.function is renamed to definition, and BuiltInFunction.dispatch is removed. Neither has users outside fbuiltins; icon4py does not use them.
  • A BuiltInFunction defined with a body no longer runs that body as its implementation. No builtin in gt4py or icon4py did; the test of the idea is removed.

Not in this PR

FieldOffset.__getitem__/as_connectivity_field still resolve the offset provider through two function-local embedded imports. They move to a separate PR.

Tests

  • ffront_tests/test_fbuiltins.py:
    • __gt_type__, name and __doc__ for every builtin;
    • the call reaches call_implementation with positional and keyword arguments;
    • without an implementation, the call raises RuntimeError;
    • importing fbuiltins loads no embedded module (an existing property, now guarded).
  • embedded_tests/test_builtins.py:
    • every builtin is implemented, and has an NdArrayField implementation;
    • scalar dtypes and result types (moved here, plus eager cases as used in icon4py);
    • TypeError for field-only builtins called with scalars, for an n-d array, and Python's own arity error for too many arguments;
    • where's named-collection and tuple mismatch errors (moved here);
    • nested tuples in where and astype.
  • Locally, CPU and NVIDIA GPU:
    • unit (embedded, ffront, iterator, common): 2595 passed
    • embedded integration (numpy, cupy, jax, torch, torch-cuda, array-api-strict): 2032 passed
    • gtfn and roundtrip (math builtins, where, concat_where, FVM nabla): 162 passed
    • 0 failed

Requirements

  • All fixes and/or new features come with corresponding tests.
  • Important design decisions have been documented in the appropriate ADR inside the docs/development/ADRs/ folder.

…i] is resolved by premap, a fallback dispatcher hook for the builtins
@havogt

havogt commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

🤖 Written by an agent on behalf of @havogt

@havogt havogt changed the title refactor[next]: fbuiltins imports nothing from embedded refactor[next]: fbuiltins declares the builtins, embedded implements them Nov 23, 2026

This branch has not been deployed

No deployments
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.

1 participant