# stfc/psyclone architecture

> Architecture diagram of the GitHub repository stfc/psyclone, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/stfc/psyclone
- Repository: https://github.com/stfc/psyclone

## Overview

#### Purpose and entry points
PSyclone is a Python source-to-source compiler for Fortran HPC applications. It parses Fortran, constructs an intermediate representation, applies user-selected optimisation/parallelisation/instrumentation transformations, and emits transformed source. Primary executables are `bin/psyclone`, `bin/psyclone-kern`, `bin/psyad`, and `bin/psyclonefc`; packaging is defined by `pyproject.toml` and `setup.py`.

#### Compilation orchestration
The main compiler flow is centred on `src/psyclone/generator.py`, with legacy/domain generation support in `src/psyclone/psyGen.py` and algorithm-layer generation in `src/psyclone/alg_gen.py`. Command-line invocation selects an API/configuration and optional transformation script, then coordinates parsing, schedule construction, script application, and Fortran output. Configuration is a key policy boundary: `src/psyclone/configuration.py` loads defaults from `config/psyclone.cfg`.

#### Parsing and source-discovery boundary
Fortran source and kernel metadata enter through parsing services in `src/psyclone/parse/algorithm.py`, `src/psyclone/parse/kernel.py`, and `src/psyclone/parse/module_manager.py`. This layer resolves modules and kernel definitions, translating source-level algorithm invokes and metadata into compiler objects. It separates filesystem/module resolution from later representation and optimisation stages.

#### PSyIR core representation
PSyIR is the central language- and domain-independent IR: node tree definitions are under `src/psyclone/psyir/nodes/`, symbols/types under `src/psyclone/psyir/symbols/`, and dependence/access analysis under `src/psyclone/psyir/tools/`. The Fortran frontend, `src/psyclone/psyir/frontend/fortran.py`, produces PSyIR; backend visitors such as `src/psyclone/psyir/backend/fortran.py`, `src/psyclone/psyir/backend/opencl.py`, and `src/psyclone/psyir/backend/c.py` lower it to target code. This IR/backend boundary permits transformations without directly manipulating source text.

#### Transformation and analysis layer
Generic transformations operate on PSyIR, including loop restructuring, OpenMP/OpenACC directives, extraction, profiling, and correctness instrumentation. Representative paths are `src/psyclone/psyir/transformations/loop_fuse_trans.py`, `src/psyclone/psyir/transformations/omp_parallel_trans.py`, and `src/psyclone/psyir/transformations/extract_trans.py`. User Python scripts are the main customisation interface; they choose and apply these transformations to compiler-generated schedules. Data-dependence and variable-access facilities in `src/psyclone/core/variables_access_map.py` support legality decisions.

#### Domain/API specialisation
Domain packages overlay PSyIR with DSL semantics and code generation:
- LFRic PSyKAl support, including kernel metadata, invokes, loops, halo/reduction handling: `src/psyclone/domain/lfric/lfric_psy.py`, `src/psyclone/domain/lfric/lfric_invoke_schedule.py`, `src/psyclone/domain/lfric/kernel/lfric_kernel_metadata.py`.
- GOcean finite-difference support and OpenCL/domain transformations: `src/psyclone/domain/gocean/gocean_driver_creator.py`, `src/psyclone/domain/gocean/transformations/gocean_opencl_trans.py`.
- NEMO is primarily handled through generic PSyIR transformations, illustrated by `examples/nemo/`.
Domain-specific “raise” and invoke-to-call transformations bridge generic parsed PSyIR to these enriched representations.

#### Automatic differentiation and generated runtime support
PSyAD is a related command path that converts tangent-linear Fortran to adjoint code using PSyIR visitors and transformations: `src/psyclone/psyad/main.py`, `src/psyclone/psyad/adjoint_visitor.py`, and `src/psyclone/psyad/tl2ad.py`. Instrumented generated applications link optional Fortran support libraries: extraction templates/runtime code in `lib/extract/`, profiling adapters in `lib/profiling/`, and read-only/value-range validation in `lib/read_only/` and `lib/value_range_check/`.

#### Runtime and infrastructure context
The compiler runs on Python and consumes/emits Fortran. Generated code targets shared-memory and accelerator models through OpenMP, OpenACC, and OpenCL; LFRic-oriented output can rely on MPI/distributed-memory and LFRic infrastructure, vendored under `external/lfric_infrastructure/`. External Fortran parser dependencies are represented by `external/fparser`; package distribution targets PyPI and also documents Conda and Spack availability.

Main entry points: [`src/psyclone/generator.py`](https://github.com/stfc/psyclone/blob/master/src/psyclone/generator.py) (Compilation generator), [`src/psyclone/psyir/transformations/loop_fuse_trans.py`](https://github.com/stfc/psyclone/blob/master/src/psyclone/psyir/transformations/loop_fuse_trans.py) (PSyIR transformations), [`src/psyclone/parse/algorithm.py`](https://github.com/stfc/psyclone/blob/master/src/psyclone/parse/algorithm.py) (Algorithm parser), [`src/psyclone/psyGen.py`](https://github.com/stfc/psyclone/blob/master/src/psyclone/psyGen.py) (PSyKAl generation)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_cli["CLI &amp; orchestration"]
  node_cli["Compiler executables<br/>Python CLI"]
  node_config["Configuration policy<br/>[configuration.py]"]
  node_generator["Compilation generator<br/>orchestrator<br/>[generator.py]"]
  node_user_script["User transform script<br/>Python extension"]
  node_psyad["PSyAD<br/>adjoint compiler<br/>[main.py]"]
end

subgraph group_parse["Parsing &amp; discovery"]
  node_algorithm_parser["Algorithm parser<br/>Fortran parser<br/>[algorithm.py]"]
  node_kernel_discovery["Kernel and module discovery<br/>source resolver<br/>[kernel.py]"]
end

subgraph group_ir["PSyIR &amp; transforms"]
  node_fortran_frontend["Fortran frontend<br/>PSyIR frontend<br/>[fortran.py]"]
  node_psyir{{"PSyIR<br/>intermediate representation"}}
  node_access_analysis["Access analysis<br/>legality analysis"]
  node_transformations["PSyIR transformations<br/>optimisation layer<br/>[loop_fuse_trans.py]"]
end

subgraph group_domains["Domain specialisation"]
  node_legacy_codegen["PSyKAl generation<br/>domain generator<br/>[psyGen.py]"]
  node_lfric["LFRic specialisation<br/>domain API<br/>[lfric_psy.py]"]
  node_gocean["GOcean specialisation<br/>domain API"]
end

subgraph group_output["Generated code &amp; runtimes"]
  node_backends["Target backends<br/>code generators<br/>[fortran.py]"]
  node_generated_fortran["Transformed Fortran<br/>compiler output"]
  node_runtime_libs["Instrumentation runtimes<br/>Fortran runtime libraries"]
  node_lfric_runtime["LFRic infrastructure<br/>Fortran infrastructure"]
end

node_cli -->|"invokes"| node_generator
node_cli -->|"selects PSyAD"| node_psyad
node_config -->|"sets API policy"| node_generator
node_generator -->|"parses algorithms"| node_algorithm_parser
node_algorithm_parser -->|"resolves kernels"| node_kernel_discovery
node_kernel_discovery -->|"supplies source"| node_fortran_frontend
node_fortran_frontend -->|"constructs"| node_psyir
node_algorithm_parser -->|"builds invokes"| node_legacy_codegen
node_legacy_codegen -.->|"selects LFRic API"| node_lfric
node_legacy_codegen -.->|"selects GOcean API"| node_gocean
node_generator -->|"loads"| node_user_script
node_user_script -->|"applies"| node_transformations
node_psyir -->|"is transformed"| node_transformations
node_access_analysis -->|"checks legality"| node_transformations
node_lfric -->|"raises domain semantics"| node_psyir
node_gocean -->|"applies domain transforms"| node_psyir
node_transformations -->|"lowers transformed IR"| node_backends
node_psyir -->|"lowers"| node_backends
node_backends -->|"emits"| node_generated_fortran
node_psyad -->|"uses visitors and transforms"| node_psyir
node_generated_fortran -.->|"links when instrumented"| node_runtime_libs
node_generated_fortran -.->|"uses for LFRic output"| node_lfric_runtime

click node_cli "https://github.com/stfc/psyclone/blob/master/bin/psyclone"
click node_config "https://github.com/stfc/psyclone/blob/master/src/psyclone/configuration.py"
click node_generator "https://github.com/stfc/psyclone/blob/master/src/psyclone/generator.py"
click node_algorithm_parser "https://github.com/stfc/psyclone/blob/master/src/psyclone/parse/algorithm.py"
click node_kernel_discovery "https://github.com/stfc/psyclone/blob/master/src/psyclone/parse/kernel.py"
click node_fortran_frontend "https://github.com/stfc/psyclone/blob/master/src/psyclone/psyir/frontend/fortran.py"
click node_access_analysis "https://github.com/stfc/psyclone/blob/master/src/psyclone/core/variables_access_map.py"
click node_transformations "https://github.com/stfc/psyclone/blob/master/src/psyclone/psyir/transformations/loop_fuse_trans.py"
click node_legacy_codegen "https://github.com/stfc/psyclone/blob/master/src/psyclone/psyGen.py"
click node_lfric "https://github.com/stfc/psyclone/blob/master/src/psyclone/domain/lfric/lfric_psy.py"
click node_gocean "https://github.com/stfc/psyclone/blob/master/src/psyclone/domain/gocean/gocean_driver_creator.py"
click node_psyad "https://github.com/stfc/psyclone/blob/master/src/psyclone/psyad/main.py"
click node_backends "https://github.com/stfc/psyclone/blob/master/src/psyclone/psyir/backend/fortran.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_config,node_generator,node_user_script,node_psyad toneBlue
class node_algorithm_parser,node_kernel_discovery toneAmber
class node_fortran_frontend,node_psyir,node_access_analysis,node_transformations toneMint
class node_legacy_codegen,node_lfric,node_gocean toneRose
class node_backends,node_generated_fortran,node_runtime_libs,node_lfric_runtime toneIndigo
```

## Components

### CLI & orchestration

- [Compiler executables](https://github.com/stfc/psyclone/blob/master/bin/psyclone): Python CLI · psyclone, psyclone-kern, psyad and psyclonefc entry points. · `bin/psyclone`
- [Configuration policy](https://github.com/stfc/psyclone/blob/master/src/psyclone/configuration.py): configuration · Selects API defaults and compiler behaviour. · `src/psyclone/configuration.py`
- [Compilation generator](https://github.com/stfc/psyclone/blob/master/src/psyclone/generator.py): orchestrator · Coordinates parsing, schedules, scripts and output. · `src/psyclone/generator.py`
- **User transform script**: Python extension · Customises generated schedules with transformations.
- [PSyAD](https://github.com/stfc/psyclone/blob/master/src/psyclone/psyad/main.py): adjoint compiler · Converts tangent-linear Fortran into adjoint code through PSyIR. · `src/psyclone/psyad/main.py`

### Parsing & discovery

- [Algorithm parser](https://github.com/stfc/psyclone/blob/master/src/psyclone/parse/algorithm.py): Fortran parser · Translates algorithm invokes into compiler objects. · `src/psyclone/parse/algorithm.py`
- [Kernel and module discovery](https://github.com/stfc/psyclone/blob/master/src/psyclone/parse/kernel.py): source resolver · Finds modules, kernel definitions and metadata. · `src/psyclone/parse/kernel.py`

### PSyIR & transforms

- [Fortran frontend](https://github.com/stfc/psyclone/blob/master/src/psyclone/psyir/frontend/fortran.py): PSyIR frontend · Builds PSyIR from Fortran source. · `src/psyclone/psyir/frontend/fortran.py`
- **PSyIR**: intermediate representation · Language- and domain-independent node and symbol tree.
- [Access analysis](https://github.com/stfc/psyclone/blob/master/src/psyclone/core/variables_access_map.py): legality analysis · Provides data-dependence and variable-access information. · `src/psyclone/core/variables_access_map.py`
- [PSyIR transformations](https://github.com/stfc/psyclone/blob/master/src/psyclone/psyir/transformations/loop_fuse_trans.py): optimisation layer · Applies loop, directive, extraction and profiling changes. · `src/psyclone/psyir/transformations/loop_fuse_trans.py`

### Domain specialisation

- [PSyKAl generation](https://github.com/stfc/psyclone/blob/master/src/psyclone/psyGen.py): domain generator · Legacy/domain schedule and code-generation support. · `src/psyclone/psyGen.py`
- [LFRic specialisation](https://github.com/stfc/psyclone/blob/master/src/psyclone/domain/lfric/lfric_psy.py): domain API · Enriches invokes with LFRic kernels, loops, halos and reductions. · `src/psyclone/domain/lfric/lfric_psy.py`
- [GOcean specialisation](https://github.com/stfc/psyclone/blob/master/src/psyclone/domain/gocean/gocean_driver_creator.py): domain API · Provides finite-difference and OpenCL domain transformations. · `src/psyclone/domain/gocean/gocean_driver_creator.py`

### Generated code & runtimes

- [Target backends](https://github.com/stfc/psyclone/blob/master/src/psyclone/psyir/backend/fortran.py): code generators · Lowers PSyIR to Fortran, OpenCL or C. · `src/psyclone/psyir/backend/fortran.py`
- **Transformed Fortran**: compiler output · Optimised, parallelised or instrumented application source.
- **Instrumentation runtimes**: Fortran runtime libraries · Optional extraction, profiling and validation support.
- **LFRic infrastructure**: Fortran infrastructure · MPI/distributed-memory support for LFRic-oriented output.

## Connections

- Compiler executables → Compilation generator: invokes
- Compiler executables → PSyAD: selects PSyAD
- Configuration policy → Compilation generator: sets API policy
- Compilation generator → Algorithm parser: parses algorithms
- Algorithm parser → Kernel and module discovery: resolves kernels
- Kernel and module discovery → Fortran frontend: supplies source
- Fortran frontend → PSyIR: constructs
- Algorithm parser → PSyKAl generation: builds invokes
- PSyKAl generation → LFRic specialisation: selects LFRic API
- PSyKAl generation → GOcean specialisation: selects GOcean API
- Compilation generator → User transform script: loads
- User transform script → PSyIR transformations: applies
- PSyIR → PSyIR transformations: is transformed
- Access analysis → PSyIR transformations: checks legality
- LFRic specialisation → PSyIR: raises domain semantics
- GOcean specialisation → PSyIR: applies domain transforms
- PSyIR transformations → Target backends: lowers transformed IR
- PSyIR → Target backends: lowers
- Target backends → Transformed Fortran: emits
- PSyAD → PSyIR: uses visitors and transforms
- Transformed Fortran → Instrumentation runtimes: links when instrumented
- Transformed Fortran → LFRic infrastructure: uses for LFRic output
