Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion docs/development/ADRs/next/0012-GridTools_Cpp_OTF_Steps.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ tags: [backend, cpp, gridtools, bindings, otf]
- **Updated**: 2026-09-15

> [!NOTE]
> The entry points named below have since been renamed or moved: `program_processors.formatters.gtfn.format_sourcecode` is now `format_cpp`; `program_processors.codegens.gtfn_modules.translate_program` is now the `GTFNTranslationStep` class in `program_processors.codegens.gtfn.gtfn_module`; `otf.binding.pybind.bind_source` is now `otf.binding.nanobind.create_bindings` (nanobind replaced pybind11); and `program_processors.runners.gtfn_cpu.run_gtfn` is now `program_processors.runners.gtfn.run_gtfn`. `otf.step_types` was merged into `otf.stages`, `otf.workflow.Step` is now the `otf.workflow.Workflow` protocol, and `processor_interface` was split up — `ProgramFormatter` lives in `program_processors.program_formatter`. The decision — building the GTFN backend out of composable OTF steps — is unchanged.
> The entry points named below have since been renamed or moved: `program_processors.formatters.gtfn.format_sourcecode` is now `format_cpp`; `program_processors.codegens.gtfn_modules.translate_program` is now the `GTFNTranslationStep` class in `program_processors.codegens.gtfn.gtfn_module`; `otf.binding.pybind.bind_source` is now `otf.binding.nanobind.create_bindings` (nanobind replaced pybind11); and `program_processors.runners.gtfn_cpu.run_gtfn` is now `program_processors.runners.gtfn.run_gtfn`. `otf.step_types` was merged into `otf.stages`, `otf.workflow.Step` is now the `otf.workflow.Workflow` protocol, and `processor_interface` was split up — `ProgramFormatter` lives in `program_processors.program_formatter`. The decision — building the GTFN backend out of composable OTF steps — is unchanged. `SourceCodeSpec` no longer carries formatting options, and generated code is no longer auto-formatted in `gt4py.next`.

## Context

Expand Down
7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ profiling = [
]
scripts = ["pyyaml>=6.0.1", "typer>=0.16.0", "packaging"]
test = [
'black>=25.11',
'clang-format>=18.1',
'hypothesis>=6.0.0',
'nbmake>=1.4.6',
'nox>=2025.02.09',
Expand Down Expand Up @@ -90,7 +92,6 @@ classifiers = [
dependencies = [
'attrs>=21.3',
'array-api-compat>=1.13',
'black>=25.11',
'boltons>=20.1',
'cached-property>=1.5.1',
'click>=8.0.0',
Expand Down Expand Up @@ -142,7 +143,7 @@ readme = 'README.md'
requires-python = '>=3.12, <3.15, !=3.13.10, !=3.14.1'

[project.optional-dependencies]
cartesian = ['gt4py[jax,standard,testing]']
cartesian = ['black>=25.11', 'clang-format>=18.1', 'gt4py[jax,standard,testing]']
cuda12 = ['cupy-cuda12x>=12.0']
cuda13 = ['cupy-cuda13x>=14.0']
jax = [
Expand All @@ -154,7 +155,7 @@ jax-cuda13 = ['jax[cuda13-local]>=0.7.0', 'gt4py[cuda13]']
next = ['gt4py[jax,standard,testing]']
rocm6 = ['cupy>=13.4.1,<14.0']
rocm7 = ['cupy-rocm-7-0>=14.0']
standard = ['clang-format>=18.1', 'scipy>=1.16.1']
standard = ['scipy>=1.16.1']
testing = ['hypothesis>=6.93', 'pytest>=7.0']

[project.urls]
Expand Down
17 changes: 2 additions & 15 deletions src/gt4py/cartesian/backend/dace_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
from gt4py.cartesian.gtc.passes.oir_optimizations.utils import compute_fields_extents
from gt4py.cartesian.gtc.passes.oir_pipeline import DefaultPipeline
from gt4py.cartesian.utils import shash
from gt4py.eve import codegen
from gt4py.eve.codegen import MakoTemplate as as_mako
from gt4py.storage.cartesian import layout, layout_registry

Expand Down Expand Up @@ -510,7 +509,7 @@ def __call__(self) -> dict[str, dict[str, str]]:

implementation = DaCeComputationCodegen.apply(self.backend.builder, sdfg)

bindings = DaCeBindingsCodegen.apply(sdfg, self.module_name, backend=self.backend)
bindings = DaCeBindingsCodegen(self.backend).generate_sdfg_bindings(sdfg, self.module_name)

bindings_ext = "cu" if self.backend.storage_info["device"] == "gpu" else "cpp"
return {
Expand Down Expand Up @@ -664,7 +663,7 @@ def apply(cls, builder: StencilBuilder, sdfg: SDFG) -> str:
state_suffix=config.Config.get("compiler.codegen_state_struct_suffix"),
)
computations = cls._postprocess_dace_code(code_objects, is_gpu)
generated_code = f"""\
return f"""\
#include <gridtools/sid/sid_shift_origin.hpp>
#include <gridtools/sid/allocator.hpp>
#include <gridtools/stencil/cartesian.hpp>
Expand All @@ -676,11 +675,6 @@ def apply(cls, builder: StencilBuilder, sdfg: SDFG) -> str:
{interface}
"""

if builder.options.format_source:
generated_code = codegen.format_source("cpp", generated_code, style="LLVM")

return generated_code

def generate_dace_args(self, stencil_ir: gtir.Stencil, sdfg: SDFG) -> list[str]:
oir = GTIRToOIR().visit(stencil_ir)
field_extents = compute_fields_extents(oir, add_k=True)
Expand Down Expand Up @@ -857,13 +851,6 @@ def generate_sdfg_bindings(self, sdfg: SDFG, module_name: str) -> str:
sid_params=self.generate_sid_params(sdfg),
)

@classmethod
def apply(cls, sdfg: SDFG, module_name: str, *, backend: BaseDaceBackend) -> str:
generated_code = cls(backend).generate_sdfg_bindings(sdfg, module_name)
if backend.builder.options.format_source:
generated_code = codegen.format_source("cpp", generated_code, style="LLVM")
return generated_code


class DaCePyExtModuleGenerator(PyExtModuleGenerator):
def __init__(self, builder: StencilBuilder) -> None:
Expand Down
4 changes: 2 additions & 2 deletions src/gt4py/cartesian/backend/debug_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from gt4py.cartesian.gtc.debug.debug_codegen import DebugCodeGen
from gt4py.cartesian.gtc.gtir_to_oir import GTIRToOIR
from gt4py.cartesian.gtc.passes import oir_optimizations
from gt4py.eve import codegen
from gt4py.eve import formatting
from gt4py.storage import layout
from gt4py.storage.cartesian import layout_registry

Expand Down Expand Up @@ -45,7 +45,7 @@ def _generate_computation(self) -> dict[str, str | dict]:
source_code = DebugCodeGen().visit(oir)

if self.builder.options.format_source:
source_code = codegen.format_source("python", source_code)
source_code = formatting.format_python_source(source_code)

caching = self.builder.caching
computation_name = f"{caching.module_prefix}computation{caching.module_postfix}.py"
Expand Down
6 changes: 5 additions & 1 deletion src/gt4py/cartesian/backend/gtc_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from gt4py.cartesian.backend.module_generator import BaseModuleGenerator, ModuleData
from gt4py.cartesian.gtc import gtir, utils as gtc_utils
from gt4py.cartesian.gtc.passes.oir_pipeline import OirPipeline
from gt4py.eve import codegen
from gt4py.eve import codegen, formatting


if TYPE_CHECKING:
Expand Down Expand Up @@ -273,6 +273,10 @@ def _make_extension_sources(self) -> dict[str, dict[str, str]]:
)
gt_pyext_generator = self.PYEXT_GENERATOR_CLASS(class_name, module_name, self)
gt_pyext_sources = gt_pyext_generator()
if self.builder.options.format_source:
for sources in gt_pyext_sources.values():
for file_name, source in sources.items():
sources[file_name] = formatting.format_cpp_source(source)
Comment thread
Copilot marked this conversation as resolved.
Outdated
final_ext = ".cu" if self.languages and self.languages["computation"] == "cuda" else ".cpp"
comp_src = gt_pyext_sources["computation"]
for key in [k for k in comp_src.keys() if k.endswith(".src")]:
Expand Down
14 changes: 3 additions & 11 deletions src/gt4py/cartesian/backend/gtcpp_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,11 @@ def __call__(self) -> dict[str, dict[str, str]]:
)
oir_node = oir_pipeline.run(base_oir)
gtcpp_ir = OIRToGTCpp().visit(oir_node)
format_source = self.backend.builder.options.format_source
implementation = gtcpp_codegen.GTCppCodegen.apply(
gtcpp_ir, gt_backend_t=self.backend.GT_BACKEND_T, format_source=format_source
gtcpp_ir, gt_backend_t=self.backend.GT_BACKEND_T
)
bindings = GTCppBindingsCodegen.apply(
gtcpp_ir,
module_name=self.module_name,
backend=self.backend,
format_source=format_source,
gtcpp_ir, module_name=self.module_name, backend=self.backend
)
bindings_ext = ".cu" if self.backend.GT_BACKEND_T == "gpu" else ".cpp"
return {
Expand Down Expand Up @@ -115,11 +111,7 @@ def visit_Program(self, node: gtcpp.Program, **kwargs):

@classmethod
def apply(cls, root, *, module_name="stencil", **kwargs) -> str:
generated_code = cls(kwargs.get("backend")).visit(root, module_name=module_name, **kwargs)
if kwargs.get("format_source", True):
generated_code = codegen.format_source("cpp", generated_code, style="LLVM")

return generated_code
return cls(kwargs.get("backend")).visit(root, module_name=module_name, **kwargs)


class GTBaseBackend(BaseGTBackend):
Expand Down
18 changes: 5 additions & 13 deletions src/gt4py/cartesian/backend/module_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from gt4py.cartesian.gtc.passes.oir_access_kinds import compute_access_kinds
from gt4py.cartesian.gtc.passes.oir_optimizations.utils import compute_fields_extents
from gt4py.cartesian.gtc.utils import dimension_flags_to_names
from gt4py.eve import codegen
from gt4py.eve import formatting


if TYPE_CHECKING:
Expand Down Expand Up @@ -107,7 +107,6 @@ def make_args_data_from_gtir(pipeline: GtirPipeline) -> ModuleData:


class BaseModuleGenerator(abc.ABC):
SOURCE_LINE_LENGTH = 120
TEMPLATE_INDENT_SIZE = 4
TEMPLATE_RESOURCE = "stencil_module.py.in"

Expand Down Expand Up @@ -149,10 +148,8 @@ def __call__(self, args_data: ModuleData) -> str:
post_run=self.generate_post_run(),
implementation=self.generate_implementation(),
)
if self.builder.options.as_dict()["format_source"]:
module_source = codegen.format_source(
"python", module_source, line_length=self.SOURCE_LINE_LENGTH
)
if self.builder.options.format_source:
module_source = formatting.format_python_source(module_source)

return module_source

Expand Down Expand Up @@ -202,16 +199,11 @@ def generate_backend_name(self) -> str:

def generate_sources(self) -> dict[str, str]:
"""
Return the source code of the stencil definition in string format.
Return the source code of the stencil definition verbatim, in string format.

This is unlikely to require overriding.
"""
if self.builder.gtir.sources is not None:
return {
key: codegen.format_source("python", value, line_length=self.SOURCE_LINE_LENGTH)
for key, value in self.builder.gtir.sources.items()
}
return {}
return dict(self.builder.gtir.sources or {})
Comment thread
Copilot marked this conversation as resolved.
Outdated

def generate_constants(self) -> dict[str, str]:
"""
Expand Down
4 changes: 2 additions & 2 deletions src/gt4py/cartesian/backend/numpy_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from gt4py.cartesian.gtc.gtir_to_oir import GTIRToOIR
from gt4py.cartesian.gtc.numpy import npir
from gt4py.cartesian.gtc.passes import oir_optimizations as oir_opt
from gt4py.eve import codegen
from gt4py.eve import formatting
from gt4py.storage import layout
from gt4py.storage.cartesian import layout_registry

Expand Down Expand Up @@ -44,7 +44,7 @@ def generate_computation(self) -> dict[str, str | dict]:
source = numpy.NpirCodegen.apply(self.npir, ignore_np_errstate=ignore_np_errstate)

if self.builder.options.format_source:
source = codegen.format_source("python", source)
source = formatting.format_python_source(source)

caching = self.builder.caching
computation_name = f"{caching.module_prefix}computation{caching.module_postfix}.py"
Expand Down
6 changes: 1 addition & 5 deletions src/gt4py/cartesian/gtc/gtcpp/gtcpp_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,4 @@ def apply(cls, root: LeafNode, **kwargs: Any) -> str:
raise ValueError("apply() requires gtcpp.Progam root node")
if "gt_backend_t" not in kwargs:
raise TypeError("apply() missing 1 required keyword-only argument: 'gt_backend_t'")
generated_code = super().apply(root, offset_limit=_offset_limit(root), **kwargs)
if kwargs.get("format_source", True):
generated_code = codegen.format_source("cpp", generated_code, style="LLVM")

return generated_code
return super().apply(root, offset_limit=_offset_limit(root), **kwargs)
2 changes: 1 addition & 1 deletion src/gt4py/eve/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
The internal dependencies between modules are the following (each module depends
on some of the previous ones):

0. xtyping
0. formatting, xtyping
1. exceptions, pattern_matching, type_definitions
2. utils
3. type_validation
Expand Down
Loading
Loading