How stfc/psyclone works
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: generator.py Compilation generator · loop_fuse_trans.py PSyIR transformations · algorithm.py Algorithm parser · psyGen.py PSyKAl generation
Components 18
CLI & orchestration
- Compiler executables · psyclone, psyclone-kern, psyad and psyclonefc entry points.
bin/psyclone - Configuration policy · Selects API defaults and compiler behaviour.
src/psyclone/configuration.py - Compilation generator · Coordinates parsing, schedules, scripts and output.
src/psyclone/generator.py - User transform script · Customises generated schedules with transformations.
- PSyAD · Converts tangent-linear Fortran into adjoint code through PSyIR.
src/psyclone/psyad/main.py
Parsing & discovery
- Algorithm parser · Translates algorithm invokes into compiler objects.
src/psyclone/parse/algorithm.py - Kernel and module discovery · Finds modules, kernel definitions and metadata.
src/psyclone/parse/kernel.py
PSyIR & transforms
- Fortran frontend · Builds PSyIR from Fortran source.
src/psyclone/psyir/frontend/fortran.py - PSyIR · Language- and domain-independent node and symbol tree.
- Access analysis · Provides data-dependence and variable-access information.
src/psyclone/core/variables_access_map.py - PSyIR transformations · Applies loop, directive, extraction and profiling changes.
src/psyclone/psyir/transformations/loop_fuse_trans.py
Domain specialisation
- PSyKAl generation · Legacy/domain schedule and code-generation support.
src/psyclone/psyGen.py - LFRic specialisation · Enriches invokes with LFRic kernels, loops, halos and reductions.
src/psyclone/domain/lfric/lfric_psy.py - GOcean specialisation · Provides finite-difference and OpenCL domain transformations.
src/psyclone/domain/gocean/gocean_driver_creator.py
Generated code & runtimes
- Target backends · Lowers PSyIR to Fortran, OpenCL or C.
src/psyclone/psyir/backend/fortran.py - Transformed Fortran · Optimised, parallelised or instrumented application source.
- Instrumentation runtimes · Optional extraction, profiling and validation support.
- LFRic infrastructure · MPI/distributed-memory support for LFRic-oriented output.
Connections 22
- 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