blob: 893a509d9172a56c34e8f3bcdb0aeb679906469f [file] [view]
# Tint Architecture
```
┏━━━━━━┓ ┏━━━━━━━━┓┏━━━━━━┓┏━━━━━━┓┏━━━━━┓ ┏━━━━━━┓
┃ WGSL ┃ ┃ SPIR-V ┃┃ HLSL ┃┃ GLSL ┃┃ MSL ┃ ┃ WGSL ┃
┗━━━┳━━┛ ┗━━━━┳━━━┛┗━━━┳━━┛┗━━━┳━━┛┗━━┳━━┛ ┗━━━┳━━┛
▼ ▲ ▲ ▲ ▲ ▲
┏━━━━━━━━┻━━━━━━━┓ ┃ ┃ ┃ ┃ ┃
┃ WGSL Parser ┃ ┃ ┃ ┃ ┃ ┏━━━━━━┻━━━━━━┓
┗━━━━━━━━┳━━━━━━━┛ ┃ ┃ ┃ ┃ ┃ ASTPrinter ┃
▼ ┃ ┃ ┃ ┃ ┗━━━━━━┳━━━━━━┛
┏━━━━━━━━┻━━━━━━━┓ ┃ ┃ ┃ ┃ ▲
┃ ProgramBuilder ┃ ┃ ┃ ┃ ┃ ┃
┃ (mutable) ┃ ┃ ┃ ┃ ┃ ┃
┗━━━━━━━━┳━━━━━━━┛ ┏━━━━┻━━━━━━━━┻━━━━━━━┻━━━━━━┻━━┓ ┃
▼ ┃ Printers ┃ ┏━━━━━━┻━━━━━━┓
┏━━━━━━━━┻━━━━━━━┓ ┗━━━━┳━━━━━━━━┳━━━━━━━┳━━━━━━┳━━┛ ┃ Program ┃
┃ Resolver ┃ ▲ ▲ ▲ ▲ ┗━━━━━━┳━━━━━━┛
┗━━━━━━━━┳━━━━━━━┛ ┃ ┃ ┃ ┃ ▲
▼ ┃ ┃ ┃ ┃ ┃
┏━━━━━━━━┻━━━━━━━┓ ┏━━━━━━━━━━━┓ ┃ ┃ ┃ ┃ ┃
┃ Program ┣━►┫ Inspector ┃ ┃ ┃ ┃ ┃ ┃
┃ (immutable) ┃ ┗━━━━━━━━━━━┛ ┃ ┃ ┃ ┃ ┏━━━━━━┻━━━━━━┓
┗━━━━━━━━┳━━━━━━━┛ ┃ ┃ ┃ ┃ ┃ IRToProgram ┃
▼ ┃ ┃ ┃ ┃ ┗━━━━━━┳━━━━━━┛
┏━━━━━━━━┻━━━━━━━┓ ┏━━━━━━━━┓ ┏━━━━┻━━━━━━━━┻━━━━━━━┻━━━━━━┻━━┓ ▲
┃ ProgramToIR ┃ ┃ SPIR-V ┃ ┃ Backend dialects ┃ ┃
┗━━━━━━━━┳━━━━━━━┛ ┗━━━━┳━━━┛ ┗━━━━┳━━━━━━━━┳━━━━━━━┳━━━━━━┳━━┛ ┃
┃ ▼ ▲ ▲ ▲ ▲ ┃
┃ ┏━━━━┻━━━━┓ ┃ ┃ ┃ ┃ ┏━━━━━━┻━━━━━━┓
┃ ┃ SPIR-V ┃ ┃ ┃ ┃ ┃ ┃ WGSL ┃
┃ ┃ Parser ┃ ┃ ┃ ┃ ┃ ┃ dialect ┃
┃ ┗━━━━┳━━━━┛ ┃ ┃ ┃ ┃ ┗━━━━━━┳━━━━━━┛
▼ ▼ ┃ ┃ ┃ ┃ ▲
┏━━━━━━━━┻━━━━━━━┓ ┏━━━━┻━━━━┓ ┃ ┃ ┃ ┃ ┃
┃ WGSL ┃ ┃ SPIR-V ┃ ┃ ┃ ┃ ┃ ┃
┃ dialect ┃ ┃ dialect ┃ ┏━━━━┻━━━━━━━━┻━━━━━━━┻━━━━━━┻━━━━━━━━━━━┻━━━━━━┓
┗━━━━━━━━┳━━━━━━━┛ ┗━━━━┳━━━━┛ ┃ Raise ┃
▼ ▼ ┃ (core and backend transforms) ┃
┏━━━━━━━━┻━━━━━━━┓ ┏━━━━┻━━━━┓ ┗━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━┛
┃ Lower ┃ ┃ Lower ┃ ▲
┗━━━━━━━━┳━━━━━━━┛ ┗━━━━┳━━━━┛ ┃
▼ ▼ ┃
┏━━━━━━━━┻━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Core dialect ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
```
## Overview
Tint has two main representations of a shader program:
* The `Program`, which holds the WGSL AST and its semantic information. This
is only used for WGSL. The WGSL reader uses it so that Tint can fully
validate WGSL and produce good error messages that point back to the user's
source. The WGSL writer uses it to print WGSL.
* The IR, held by a `core::ir::Module`. This is the representation that all
the transforms operate on, and that all the writers start from.
Every shader flows down from its reader to the core dialect of the IR, and then
back up to the dialect of the output language.
The readers are on the left, and work down towards the core dialect. A WGSL
shader is parsed and resolved into a `Program`, which is then converted to IR
in the WGSL dialect. A SPIR-V shader is parsed directly into IR in the SPIR-V
dialect. Both are then *lowered* to the core dialect.
The writers are on the right, and work up from the core dialect. Each writer
*raises* the IR from the core dialect to its own dialect. The SPIR-V, HLSL,
GLSL and MSL writers then print the IR as the output shader language. The WGSL
writer is different: it converts the WGSL dialect back into a `Program`, and
prints the AST.
The core dialect is the only point where the readers and writers meet. A reader
does not need to know anything about the writers, and a writer does not need
to know which reader was used.
## Readers
Readers are responsible for parsing a shader program and producing IR in the
core dialect (see [Dialects](#dialects)).
### WGSL Reader
The WGSL reader is a recursive descent parser. It closely follows the WGSL
grammar in the naming of the parse methods. The parser populates a
`ProgramBuilder` with the parsed AST and symbol information, and then the
`Resolver` is run to build the `Program`.
`wgsl::reader::Parse()` returns the `Program`. This can be used with the
`Inspector`, or kept around to be converted to IR later.
`wgsl::reader::ProgramToLoweredIR()` converts a valid `Program` into IR. This
is done in two steps:
1. `ProgramToIR` walks the AST and semantic information and builds IR in the
WGSL dialect. This IR is then validated.
2. `Lower` runs the transforms that convert the WGSL dialect into the core
dialect. For example, WGSL builtin calls are replaced with core builtin
calls, and `workgroupUniformLoad()` is replaced with barriers and a load.
`wgsl::reader::WgslToIR()` performs both the parse and the conversion to IR.
### SPIR-V Reader
The SPIR-V reader does not use the AST at all. `spirv::reader::ReadIR()`
parses the SPIR-V binary into IR in the SPIR-V dialect, and then `Lower`
converts this into the core dialect. Lowering handles things that have no
direct equivalent in the core dialect, such as row-major matrices, strided
arrays and combined image samplers. The resulting IR is always validated, so
that invalid SPIR-V produces an error instead of an internal compiler error
later on.
See [spirv-reader-overview.md](spirv-reader-overview.md) for more detail.
## ProgramBuilder
A `ProgramBuilder` is the interface to construct an immutable `Program`.
There are a large number of helper methods for simplifying the creation of the
AST nodes. A `ProgramBuilder` can only be used once, and must be discarded after
the `Program` is constructed.
A `Program` is built from the `ProgramBuilder` via a call to
`resolver::Resolve()`, which will perform validation and semantic analysis.
The returned program will contain the semantic information which can be obtained
by calling `Program::Sem()`.
At any time before building the `Program`, `ProgramBuilder::IsValid()` may be
called to ensure that no error diagnostics have been raised during the
construction of the AST. This includes parser syntax errors, but not semantic
validation which happens during the `Resolve` phase.
These days the `ProgramBuilder` is mostly used by the WGSL parser, by
`IRToProgram` in the WGSL writer, and by tests.
## AST
The Abstract Syntax Tree is a directed acyclic graph of `ast::Node`s which
encode the syntactic structure of the WGSL program.
The root of the AST is the `ast::Module` class which holds each of the declared
functions, variables, user declared types (type aliases and structures),
`const_assert`s and directives (such as `enable`, `requires` and
`diagnostic`).
Each `ast::Node` represents a **single** part of the program's source, and so
`ast::Node`s are not shared.
The AST does not perform any verification of its content. For example, the
element count of an `array<T, N>` is just an expression in the AST, and it is
not validated to be within the WGSL specification limits until validation of
the `Resolver`.
The AST code lives in `src/tint/lang/wgsl/ast`.
## Semantic information
Semantic information is held by `sem::Node`s which describe the program at
a higher / more abstract level than the AST. This includes information such as
the resolved type of each expression, the resolved overload of a builtin
function call, the constant value of an expression, and the module scoped
variables used by each function.
Semantic information is generated by the `Resolver` when the `Program`
is built from a `ProgramBuilder`.
The `sem::Info` class holds a map of `ast::Node`s to `sem::Node`s. Each AST
node has at most one semantic node, but semantic nodes often refer to other
semantic nodes that are shared. For example, each `ast::IdentifierExpression`
that refers to a variable has its own `sem::VariableUser`, and all of these
point to the same `sem::Variable`. The `sem::Variable` can list all of its
users with `sem::Variable::Users()`.
Unlike `ast::Node`s, semantic nodes may not necessarily form a directed acyclic
graph, and the semantic graph may contain diamonds.
The semantic code lives in `src/tint/lang/wgsl/sem`.
## Types
There are two kinds of type in Tint: AST types and `core::type::Type`s.
AST types are held by `ast::Type`, which is a thin wrapper around an
`ast::IdentifierExpression` that names the type, such as `i32` or
`array<f32, 4>`. Like other AST nodes, these represent a single part of the
parsed source code. Identical AST types are not de-duplicated as they refer to
the source usage of the type.
`core::type::Type`s are the resolved types. They are created by the
`Resolver`, and are also used by the IR. Both the `Program` and the
`core::ir::Module` own a `core::type::Manager`, which constructs and holds the
types.
The `core::type::Manager` makes sure that each `core::type::Type` **uniquely**
represents a particular type, so you can compare `core::type::Type*` pointers
to check for type equivalence. For example, a `Program` will only hold one
instance of the `core::type::I32` type, no matter how many times an `i32` is
mentioned in the source program.
WGSL type aliases resolve to their target type. For example, given:
```wgsl
alias MyI32 = i32;
const a : i32 = 1;
const b : MyI32 = 2;
```
The **resolved** types for the variables `a` and `b` will both be the same
`core::type::I32` pointer.
The type code lives in `src/tint/lang/core/type`. Backends may add their own
types, which live in the `type` directory of that backend.
## Symbols
Symbols represent a unique string identifier in the source program. These string
identifiers are transformed into symbols by the WGSL parser, and are held by
a `SymbolTable`.
The IR does not need names to function, but a `core::ir::Module` also has a
`SymbolTable`, which is used to give names to values and functions. These
names are kept so that the output shader is easier to read and debug, but
they are not guaranteed to match the names in the original source.
The HLSL, GLSL, MSL and WGSL writers run the `RenameConflicts` transform as
part of `Raise()`. This renames any declaration that clashes with, or shadows,
another declaration. The printers then rename any identifier that is a keyword
of the output language.
## Resolver
The `Resolver` is run by `resolver::Resolve()`, which builds a `Program` from a
`ProgramBuilder`. A `Resolver` creates the `Program`'s semantic information by analyzing the
`Program`'s AST and type information.
The `Resolver` will validate to make sure the generated `Program` is
semantically valid. This includes the uniformity analysis (see
[uniformity_analysis.md](uniformity_analysis.md)), and the evaluation of
constant expressions.
## Program
A `Program` holds an immutable version of the information from the
`ProgramBuilder` along with semantic information generated by the
`Resolver`.
`Program::IsValid()` may be called to ensure the program is structurally correct
**and** semantically valid, and that the `Resolver` did not report any errors
during validation.
Unlike the `ProgramBuilder`, a `Program` is fully immutable. The immutable
nature of `Program`s make these entirely safe to share between multiple threads
without the use of synchronization primitives. Dawn keeps the `Program` for each
shader module, and converts it to IR for each pipeline stage that it compiles.
## Inspector
The inspector's job is to go through the `Program` and pull out various pieces
of information. This includes the entry points, their inputs and outputs, the
resource bindings they use and the overrides they declare. Dawn uses this to
validate pipelines against the shader, and to build the options passed to the
writers.
The input `Program` to the inspector must be valid (pass validation).
## IR
The IR (intermediate representation) is a mutable representation of the
program, which is designed to be easy to transform and to generate code from.
It is made of functions, blocks, instructions and values. Unlike the AST, there
is only one way to express each thing. For example, all of the WGSL loop
statements become a single `loop` instruction, and complex expressions are
broken down into one instruction per operation.
The IR lives in `src/tint/lang/core/ir`. See [ir.md](ir.md) for details of
the IR design.
### Dialects
The core IR can be extended with new types and instructions. A set of these
extensions is called a dialect. Each language has its own dialect:
* The **core** dialect is what all the readers produce and all the writers
accept. It is language independent.
* The **WGSL** dialect adds WGSL builtin calls and other instructions that
map directly to WGSL. It lives in `src/tint/lang/wgsl/ir`.
* The **SPIR-V**, **HLSL**, **GLSL** and **MSL** dialects add instructions and
types for the builtins and features of each of those languages. These live
in the `ir` directory of each language, and in the `type` directory for those
languages that add types.
A reader produces its own dialect and then *lowers* it to the core dialect.
A writer takes the core dialect and *raises* it to its own dialect, so that
the printer only has to deal with things that map directly to the output
language. For WGSL, the WGSL dialect is converted back into a `Program` before
it is printed.
The `core::ir::Module` also has a set of `Properties`. Most of these describe
things that the module is allowed to contain that would otherwise be rejected
by the validator, and a few restrict the module further. Properties are set by
the readers, and are added or removed by transforms as they change the module.
### Validation
The IR has a validator, which checks that a module is well formed. The IR built
from a WGSL `Program` or from SPIR-V is validated before it is passed on.
After this point, any error found by the validator is a bug in Tint, so the
validator is usually only run between transforms when IR validation asserts are
enabled. The exceptions are `SubstituteOverrides`, as the override values may
produce invalid IR, and `IRToProgram` in the WGSL writer. These always validate
the module.
### Disassembly
An IR module can be printed as text using the `core::ir::Disassembler`. This
is used by the IR tests, and is very useful for debugging. Passing
`--dump-ir` to the `tint` executable will print the IR each time it is
validated, which is before each transform and before the printer. This requires
Tint to be built with IR dumping enabled.
## Transforms
Transforms are used to modify the IR. Examples include robustness, vertex
pulling, polyfills for builtins that a backend does not support, and the
transforms that move the shader inputs and outputs into the form needed by
each backend.
Each transform is a function that takes a `core::ir::Module` and an optional
configuration, and changes the module in place. There is no transform manager.
Transforms that need to return information do so through their result.
Transforms that are used by more than one backend live in
`src/tint/lang/core/ir/transform`. Transforms that are specific to a single
backend live in that backend's `writer/raise` directory, and transforms used by
a reader live in that reader's `lower` directory.
The input to a transform must be valid IR, and the transform must produce valid
IR.
## Writers
A writer is responsible for writing the IR in the target shader language.
The SPIR-V, HLSL, GLSL and MSL writers all work in the same way. Each has a
`Generate()` function which takes the core dialect IR and a set of options, and
does the following:
1. Checks that the module and options are supported by the backend.
2. Runs `Raise()`, which runs all the transforms needed for that backend. This
always includes `SingleEntryPoint`, which removes everything but the
requested entry point, and `SubstituteOverrides`, which replaces all
overrides with their values. Some of these transforms convert the module
into the backend's dialect.
3. Runs the printer, which walks the IR in the backend dialect and emits the
output shader.
The writers all take the IR by non-const reference, as the transforms change
the module in place. To produce output for more than one backend or entry
point, the IR must be built again for each one.
The WGSL writer is different, as it uses the AST to print WGSL.
`wgsl::writer::ProgramFromIR()` raises the IR to the WGSL dialect, and then
`IRToProgram` builds a new `Program` from it. `wgsl::writer::Generate()` then
prints the `Program` as WGSL with the `ASTPrinter`. Dawn uses
`ProgramFromIR()` to turn SPIR-V shaders into a `Program`, so that they can be
inspected and handled the same way as WGSL shaders.
There is also a `null` writer, which runs `SingleEntryPoint` and
`SubstituteOverrides`, and returns information about the workgroup size and
storage. It does not print a shader. Dawn's null and WebGPU backends use it for
compute pipelines.