# Air.Legalize: revert to loops for scalarizations · gitcafe/zig

[View on GitCafe](https://git.cafe/gitcafe/zig/commit/69f39868b4125e79e4070a88bbdfcd3643dbc90d)

Repository: [gitcafe/zig](https://git.cafe/gitcafe/zig)

Visibility: public

Requested revision: 69f39868b4125e79e4070a88bbdfcd3643dbc90d

Requested commit: 69f39868b4125e79e4070a88bbdfcd3643dbc90d

Commit: 69f39868b4125e79e4070a88bbdfcd3643dbc90d

Tree: 7e90f08d2b5d1cb234957dbe0b6a48034afe4e30

Author: Matthew Lugg

Committer: Matthew Lugg

## Message

```
Air.Legalize: revert to loops for scalarizations

I had tried unrolling the loops to avoid requiring the
`vector_store_elem` instruction, but it's arguably a problem to generate
O(N) code for an operation on `@Vector(N, T)`. In addition, that
lowering emitted a lot of `.aggregate_init` instructions, which is
itself a quite difficult operation to codegen.

This requires reintroducing runtime vector indexing internally. However,
I've put it in a couple of instructions which are intended only for use
by `Air.Legalize`, named `legalize_vec_elem_val` (like `array_elem_val`,
but for indexing a vector with a runtime-known index) and
`legalize_vec_store_elem` (like the old `vector_store_elem`
instruction). These are explicitly documented as *not* being emitted by
Sema, so need only be implemented by backends if they actually use an
`Air.Legalize.Feature` which emits them (otherwise they can be marked as
`unreachable`).

```

## Parents

- [99a7884308d288bd39df9192c9094439b179ff60](https://git.cafe/gitcafe/zig/commit/99a7884308d288bd39df9192c9094439b179ff60?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zig/tree/69f39868b4125e79e4070a88bbdfcd3643dbc90d?format=markdown)
