kernelcreation: Translation from SymPy-frontend
The machinery behind `create_kernel` mapping a kernel expressed in the SymPy-based frontend to the backend needs to be rebuilt. The following primary operations need to be reimplemented or adapted:
- **Freeze AST:** The input AssignmentCollection (or whatever container is used) is transformed to the pymbolic-based representation. Output of the freeze should be a pure but still untyped backend-AST containing pymbolic expressions.
- **Type Inference:** The type inference algorithm should compute all missing expression types on the frozen AST. All `Variables` must be replaced by `PsTypedVariable`, all constants replaced by `PsTypedConstant`. After this, the AST is fully typed and must remain so.
- **Loop Nest/ GPU indexing:** Depending on the type of kernel, the AST is wrapped into a loop nest or prepended with a GPU index calculation
- **Optimization**: Various optimizations can be applied. These include:
- Constant folding
- Loop-invariant code motion
- OpenMP parallelization
- Loop transformations (tiling, blocking, splitting)
- Vectorization
After the types are figured out, any operation on the AST must maintain the invariant that the AST is fully and correctly typed.
## Data Structure Correspondence
The following correspondences and translations between front-end and back-end structures exist and might have to be tracked by the translator:
- `sp.Symbol` --(freeze)--> `pb.Variable` --(typing)--> `PsTypedVariable`
- `ps.TypedSymbol` --(freeze)--> `PsTypedVariable`
- `ps.Field` --(freeze)--> `PsLinearizedArray` + `PsTypedVariable`s for pointers and indexing info
## Concept, Components, and Progress
### Iteration Spaces
The kernel's iteration space is modelled using the `IterationSpace` class.
Two variants exist: full and sparse iteration spaces.
The iteration space controls the translation of field accesses to array accesses
and will later be materialized to an index source (e.g. a loop nest) by the platform-specific code generator.
`FullIterationSpace`:
- [x] Creation from ghost layers
- [x] Creation from iteration slice
`SparseIterationSpace`:
- [x] Creation from an index field (requires: struct data types)
### Supported Functions
- [ ] Set up a collection of all supported functions and define their typing behaviour
### FreezeExpressions
The freeze is performed by `freeze.FreezeExpressions`. It inherits from `pb.SympyToPymbolicMapper` and extends it functionality where necessary.
- [x] `Field.Access`:
- [x] Generic and Absolute accesses
- [x] Index field accesses: Require `SparseIterationSpace`
- [x] Buffer accesses: Require inference of buffer indexing
- [ ] Staggered Accesses
- [ ] Exponentials (`sp.Pow`): Expand to multiplications where sensible
- [ ] Functions and function calls: Check if supported and map onto backend function symbols
### Typification
Untyped expressions are typified by `typification.Typifier`.
- [x] Variables: Assign default type from configuration
- [x] Constants: Assign target type from current expression context, or fail if target type is unknown
- [x] Arithmetic expressions: Typify all operands homogenously
- [x] Struct member accesses (`Lookup`): Use struct type
- [x] Array accesses: Typify index expression with `index_dtype` target type
- [x] Functions: Typify according to function type rules (see above *Supported Functions*)
### Platform-Specific Code Generation
The remaining code generation process is controlled by the platform-specific code generator.
All platform-specific code generators shall be subclasses of `platform.Platform` which defines their common interface. They all shall implement:
- [ ] Materialization of the iteration space (sparse and full)
- [x] Basic CPU
- [x] Materialization of functions (map mathematical, in particular transcendental functions, to target-specific implementations)
- [ ] Target-specific optimizations (including vectorization)
- [x] Provide collection of required header files, preprocessor definitions, etc.
The platform instance shall be created from a `TargetSpec` provided by the user.
The main code generation driver shall then call the `Platform` object to finalize code generation.
task