Since the table of cached expressions is only meaningful within a single
function, it's really something that should only be accessed from
`BlockContext`.
However, to save heap allocations, it makes sense to retain it in the `Writer`
between functions. But the `Writer` field should have a different name, to
ensure people don't use it by accident.
This commit should should cause no visible change in behavior.
Many `back::spv::Writer` methods take long lists of arguments simply describing
the module and function we're generating code for, but these never change while
we generate a given function. This commit moves some of those values into a
`BlockContext` type, and turns those `Writer` methods into `BlockContext`
methods, so that code can access these values through `self`, rather than
threading them through everywhere as arguments.
Many uses of `self.foo` become `self.writer.foo`. Some of these can be
simplified, but to make this commit easier to review, most of those changes are
left to later commits.
This allows us to remove some `#[allow(clippy::too_many_arguments)]` attributes.
This commit should introduce no meaningful code changes. It's simply isolating
the `Writer` methods that should be `BlockContext` methods into their own impl
block, to make it easier to see the meaningful changes needed to introduce
`BlockContext`.
A single `Writer` can be reused to convert any number of Naga IR modules to
SPIR-V. This lets us reuse the heap allocations, but makes us responsible for
resetting the state of the `Writer` fully between modules. The old code
neglected to clear several different fields: logical_layout (all fields),
lookup_type, and cached_constants.
This commit uses Rust struct literal expressions to construct the re-initialized
`Writer`, which makes the compiler check that each field has been handled.
It also introduces a policy limiting how much storage will be retained, so that
processing the occasional whale doesn't leave us with leviathan buffers
permanently.
Rather than giving `Block` an optional `terminator` field in addition to its
`body`, track block termination statically, with two types:
- `Block` is a block without a termination instruction. This is what most code
generation functions operate on.
- `TerminatedBlock` is a block with a termination instruction. This is what
`Function::blocks` holds.
The `Function::consume` method takes a `Block` by value, together with a
termination instruction, and turns it into a `TerminatedBlock`.
This lets us remove some unwraps and awkward conditions.
As part of this change, `Writer::write_block` no longer hits an `unimplemented!`
for Naga statements following `Break`, `Return`, and so on. Instead, it simply
doesn't emit code for them, which is a correct translation. If we want to forbid
these, we should handle that in validation instead.
The `impl` blocks for `Writer` and its associated types remain in spv/writer.rs.
There should be no substantive code changes in this commit, only moving code
around and adjusting `use` declarations to match.