Skip to content

vector types: a vector through a reference, in a struct field and in a Vec - #4565

Merged
hiraditya merged 2 commits into
mainfrom
vectors-through-memory
Dec 4, 2026
Merged

hiraditya merged 2 commits into
mainfrom
vectors-through-memory

Conversation

@hiraditya

Copy link
Copy Markdown
Collaborator

Fixes #4381.

A <4 x f64> can now go through memory, as a number can. The three programs of the issue compile with both code generators and print the right answer:

  1. A &mut <4 x f64> parameter.
  2. A struct with a field of a vector type.
  3. A Vec<<4 x f64>>.

The choice

The request was to keep the change small, and to use MLIR or unsafe where a checker change looked large. No checker change was large. The borrow checker already tracks a reference to a vector as a reference to a number. It refuses the same conflicts (E4002, E4004). The checker needed one change: a vector is Copy, so Vec::get takes it. So a program needs no unsafe and no mlir! block. The work is in the code generators, which load and store a vector through a pointer with llvm.load and llvm.store.

Alignment

A load or store of a whole <4 x f64> needs an address that is a multiple of 32. The std containers allocated at 16. With the code generator fixes alone, Vec<<4 x f64>>::push crashed with SIGSEGV: LLVM uses an aligned move, because vxc compiles with -mcpu=native.

std::alloc::heap_align<T>() gives the alignment of T, and at least 16. Vx has no alignof<T>() yet, so it measures the padding before a T that comes after one byte. Vec, Box, RefArena, HashMap, the channels, broadcast and watch allocate with it. A type that needs 16 or less allocates as before. vec_alloc and vec_grow take the alignment. So the 14 tests that match the vec_grow call have one more argument, and so has the test that calls the private vec_grow.

Tests

One test for each case of the issue, in tests/backend/pass/. Each runs with both code generators, and the flat one must not fall back. I broke each part of the change and ran every RUN line:

Part broken Code generator Check that fails
A slot of one vector is not a tensor (has_extents) legacy parameter
A store to a place of a vector type is one store legacy parameter, field, local, struct field, reference, vec
Size of a struct with a vector field legacy sizes, bodies
sizeof of a vector legacy, flat vec
A borrowed vector gets an llvm.alloca flat no fall back
A pointer to a vector, and its loads and stores flat no fall back
Load and address of a vector field flat no fall back
A vector type argument, as in Box<<4 x f64>> flat no fall back
A vector is Copy both vec
Vec allocates at 16 both vec, bodies, in 3 of 3 runs
Box allocates at 16 both box, in 3 of 3 runs
heap_align gives 16 both heap_align

A check after the first failing one does not run. I checked such a line with a copy of the test that keeps only its case.

cargo test --no-fail-fast passed here, except four tests in compile_time_of_ifs_in_a_row. They fail on main too. For their programs, the solver written in Vx (#4557) takes about 10 s in a debug build, and z3 takes 0.4 s.

Left for later

Generated With Claude Code

…a Vec

A `<4 x f64>` can now go through memory, as a number can:

- A `&mut <4 x f64>` parameter, a reference to a vector field and a local
  reference read and write the vector.
- A struct can have fields of vector types.
- `Vec`, `Box` and the other std containers hold vectors, and structs with
  vector fields.

The checker treats a vector as `Copy`, as it treats a number, so `Vec::get`
takes one. The borrow checker needed no other change.

The code generators load and store vectors through pointers and fields:

- The legacy one stored a vector through a pointer or into a field with
  `vector.store`, which takes only a memref. It also took the slot of one
  vector, `memref<vector<4xf64>>`, for a tensor, because the type text has
  an `x`.
- The flat one gives an `llvm.alloca` to a vector that `&v` borrows.
- Both gave `sizeof` of a vector 8 bytes, so a `Vec` buffer was too small.

A load or store of a whole `<4 x f64>` needs an address that is a multiple
of 32, but the containers allocated at 16. `std::alloc::heap_align<T>()`
gives the alignment of `T`, and at least 16. Vx has no `alignof<T>()`, so
it measures the padding before a `T` that comes after one byte. Each
container allocates with it, and `vec_alloc` and `vec_grow` take it.

Left for later (#485): an `extern "C"` function that takes or returns a
vector, or a struct with a vector field, is not refused yet.

Fixes: #4381
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hiraditya
hiraditya merged commit 414bb29 into main Dec 4, 2026
7 checks passed
@hiraditya
hiraditya deleted the vectors-through-memory branch December 4, 2026 07:40
hiraditya added a commit that referenced this pull request Dec 4, 2026
Both code generators decide some facts about an MLIR type from its
printed form. #4565 added more of these tests. This commit removes them,
and the older tests that they extended.

The AST code generator:

- `has_extents` read the text of a memref type to find its rank. It is
  gone. `one_value_memref_element` asks melior for the rank and the
  element type.
- `is_scalar_memref` uses the same query. The places that repeated its
  three text tests now call it.
- A load through `&mut` cut the type text at byte 7 and parsed the rest
  back into a type. Now it takes the element of the memref type.

The flat code generator:

- Five tables for each register gave the kind of slot of a local. One
  `Slot` enum replaces them. A `Store` and a `SlotLoad` now have two
  forms, one for a memref slot and one for an `llvm.alloca` slot.
- A field load found a vector field by the text `vector<`. Now it uses
  the type of its result.
- `elem_of_gid` and `vector_of_gid` hashed up to 242 types to find the
  type of a type ID. Now a lookup table gives it.
- The `imm` of an `Alloca` for a local that `&x` borrows is the named
  constant `IMM_ADDRESS_TAKEN`, not a bare 1.

Issue: #388

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

vector types: a reference to a vector, a vector field in a struct, and a Vec of vectors do not compile

1 participant