Modules

Optional modules are not part of the default library. They are loaded only when named in VECMAT_MODULES. Public modules live in the vecmat-vendors repository. The recommended layout is a vendors/ checkout under this tree. That checkout may be a git submodule; it is optional. If the repository is checked out elsewhere, pass its path in VECMAT_MODULES_PATH. Private modules are loaded the same way from their own tree.

The loader accepts four kinds: backend, width, feature, and device. A missing module.cmake, or any other kind, is Unknown Vecmat module.

Kinds

KindWhat it doesLists it appends
backendRegisters a complete _ptr table with vm_backend_register(). A schedule.VECMAT_MODULE_HOOKS, register includes and calls.
widthOverrides a slot on a core width such as fp16. Does not fill vm_backend_ops.That width's lists (VECMAT_FP16_MODULE_HOOKS, ...).
featureDomain helpers. Contributes sources and headers. Does not register.VECMAT_MODULE_HOOKS (sources), optionally test/bench sources. Not the register lists.
deviceGPU, NPU, or accelerator. Registers with vm_device_register().Its own library. Not VECMAT_MODULE_HOOKS or a backend register list.

A backend owns identity, extra HWCAP bits, and the tuned kernels. There is no vm_cpu_part() in the base library: the backend probes, then registers VM_CPU_BACKEND. A width module does not register a backend. The core owns the storage type; the module only overrides a slot. See doc/fp16.md. Backend and width headers are standalone include/vecmat_*.h files.

A feature module contributes sources via VECMAT_MODULE_HOOKS (the same hook the float object libraries call) and may append VECMAT_MODULE_TEST_SOURCES / VECMAT_MODULE_BENCH_SOURCES. It must not append to a backend or width register list. Its public header is include/vecmat/feature/<name>.h, included as <vecmat/feature/<name>.h>.

Layout

A module is a directory with module.cmake. Under the recommended layout that is vendors/<vendor>/<name>/. module.cmake is the only file the top-level build includes. It must set VECMAT_MODULE_KIND. Use CMAKE_CURRENT_LIST_DIR for its own sources.

A backend module:

vendors/arm/cortex-a5x
├── doc
│   └── cortex-a5x.md
├── include
│   └── vecmat_cortex_a5x.h
├── module.cmake
└── src
    ├── a5x.h
    ├── gemm_a53.c
    ├── gemm_a55.c
    ├── probe.c
    ├── register.c
    └── sched.c

A width module:

vendors/arm/fp16
├── doc
│   └── fp16.md
├── include
│   └── vecmat_arm_fp16.h
├── module.cmake
└── src
    ├── arm_fp16.h
    ├── neon.c
    ├── probe.c
    └── register.c

A feature module:

vendors/alkontek/spectral-graph
├── doc
│   └── spectral-graph.md
├── include
│   └── vecmat
│       └── feature
│           └── spectral_graph.h
├── module.cmake
└── src
    ├── bitset.c
    ├── laplacian.c
    └── unrank.c

Callers include that header as <vecmat/feature/spectral_graph.h>.

A device module is a GPU, NPU, or accelerator. It uses the device API (vm_device_register). It does not fill vm_backend_ops, and it must not append VECMAT_MODULE_HOOKS or a backend register list. It builds its own library.

nvidia/ga10x:

vendors/nvidia/ga10x
├── doc
│   └── ga10x.md
├── include
│   └── vecmat_nvidia_ga10x.h
├── module.cmake
└── src
    ├── ga10x.cu
    └── register.c

module.cmake sets device and builds vecmat_nvidia_ga10x. The module's register function calls vm_device_register.

Build

Empty VECMAT_MODULES is the default. With vendors/ checked out under this tree:

cmake -S . -B build -G Ninja \
  -DCMAKE_BUILD_TYPE=Release \
  -DVECMAT_MODULES=arm/fp16 \
  -DVECMAT_RUNTIME_DISPATCH=ON
cmake --build build

If the modules are elsewhere, add -DVECMAT_MODULES_PATH=../vecmat-vendors. VECMAT_MODULES entries are paths relative to vendors/ (or to each VECMAT_MODULES_PATH root). A private vendors tree is the same.

module.cmake appends, depending on kind:

  • backend: VECMAT_MODULE_HOOKS (called from vecmat_add_float_objects, once per float width), VECMAT_MODULE_REGISTER_INCLUDES, VECMAT_MODULE_REGISTER_CALLS, and optionally VECMAT_MODULE_TEST_HOOKS, VECMAT_MODULE_TEST_SOURCES, VECMAT_MODULE_BENCH_SOURCES.
  • width: the lists for the width it implements (VECMAT_FP16_MODULE_HOOKS, VECMAT_FP16_MODULE_REGISTER_CALLS). Those lists are consumed only by that width's object lib. The module's own module.cmake errors if that width's switch is off. A width module must not append to the backend lists; the loader rejects that.
  • feature: VECMAT_MODULE_HOOKS, and optionally VECMAT_MODULE_TEST_HOOKS, VECMAT_MODULE_TEST_SOURCES, VECMAT_MODULE_BENCH_SOURCES. Not the backend or width register lists.
  • device: its own library, and optionally the test and bench lists. Not VECMAT_MODULE_HOOKS and not a backend register list. The loader rejects those lists. Width lists stay with width modules.

Test and bench sources live in the vendor tree. The mains do not include a module header. VECMAT_MODULE_TEST_HOOKS runs after the test targets exist and may add the public include dir. Do not define a module macro in the test mains.

Dispatch model

Bits are probed facts. vm_cpu_runtime_features() sets one flat bit per CPU capability (VM_CPU_AVX2, VM_CPU_FMA, VM_CPU_NEON, ...). Levels are what a kernel file was compiled to need. VM_LEVEL_AVX2 is VM_CPU_AVX2 | VM_CPU_FMA; the others, VM_LEVEL_AVX512 included, are one bit. A direct-call kernel runs only when vm_cpu_has(level) sees every bit. A file built with -mfma must gate on a level that includes FMA, except AVX-512, where FMA is implied (every AVX-512F CPU has FMA3; a hypervisor that hides it is unsupported). The vm_soa3_* AVX2 file is also exempt because it disables contraction and emits no FMA, so it checks plain VM_CPU_AVX2.

Bits sit in per-family ranges of the 32-bit vm_cpu_features_t. The values are C11 enum constants, so they stay within bits 0..30; bit 31 is unused. Placeholders are commented in cpu.h and not probed yet. VM_CPU_F16C, VM_CPU_AVX512BW, VM_CPU_AVX512VL, VM_CPU_AVX512BF16, VM_CPU_ARM_FP16, VM_CPU_ARM_BF16, and VM_CPU_I8MM are features, not tiers.

BitsFamilyMaskDefinedReserved placeholders
0..3genericVM_CPU_GENERIC_MASKSCALAR 0, BACKEND 1bits 2, 3 reserved
4..15x86VM_CPU_X86_MASKAVX 4, AVX2 5, AVX512F 6, FMA 10, F16C 11, AVX512BW 12, AVX512VL 13, AVX512BF16 14AVX10 7, AMX 8, ACE 9
16..23ARMVM_CPU_ARM_MASKNEON 16, SVE 17, SVE2 18, ARM_FP16 21, ARM_BF16 22, I8MM 23SME 19, SME2 20
24..30RISC-VVM_CPU_RISCV_MASKnoneRVV 24, ZVFH 25, ZVBB 26

AVX10.1 is the AVX-512 set, so its probe will set VM_CPU_AVX512F; VM_CPU_AVX10_2 is for everything beyond that baseline (AVX10.2 and later).

AMX is a feature, not a tier, and Linux needs the arch_prctl opt-in before tile use.

ACE (AI Compute Extensions, v1) is the future standard matrix acceleration (outer-product tiles under a new AMX palette, AVX10 inputs, OCP MX formats with block scaling); a bit is reserved for it. Compare masks with the VM_CPU_* names, never stored numbers.

Tiers come from one ladder. vm_cpu_selected_features() walks a table of {level, tier} rows in priority order (SVE2, SVE, NEON, AVX-512F, AVX2, AVX); the first level fully present wins, else scalar. Without FMA, AVX2 falls to AVX.

Backends are code that targets a level, plus an optional microarchitecture match in their own register hook; a registered backend still wins. A richer backend descriptor may come later.

RISC-V Vector (RVV) will be one new bit (VM_CPU_RVV, bit 24) and one VM_LEVEL_RVV row in the ladder.

GPUs, NPUs, and accelerators use the device API (vm_device_register), never a CPU feature bit or a vm_backend_ops table.

Backend registration

The base library keeps this surface for backends:

  • vm_backend_register() / VM_CPU_BACKEND — a registered table wins
  • vm_backend_builtin() — copy a compiled ISA table and override slots. NEON, SVE, and SVE2 are provided. Returns NULL if that ISA was not compiled.
  • vm_cpu_set_note() / vm_cpu_note() — one extra banner line, printed only if set
  • vm_backend_modules_register() — test and benchmark hook, generated, not a library export
  • VECMAT_FORCE_ISA — override the ISA bound for the table-dispatched _ptr set (vec4, mat4, quat, GEMM ukernel) through vm_cpu_selected_features(), when no backend is registered and that ISA is compiled in. By design, it does not reach the direct-call kernels (vm_array_*, vm_axpy, vm_rsqrt_array, vm_soa3_*, vm_batch3_*, vm_eos_*_n, and the SIMD vm_sph_* kernels): they test the raw vm_cpu_runtime_features() mask on each call

Tests and benchmarks do not include a backend header. They call the generated hook, then vm_cpu_init():

vm_backend_modules_register();
vm_cpu_init();
if (vm_cpu_note())
    printf("%s\n", vm_cpu_note());

With no backends selected, the generated function is empty. A backend that does not match the CPU returns 0 and leaves the builtin table in place. A registered backend wins over VECMAT_FORCE_ISA. The same pointer is idempotent. A table with a missing slot is rejected. VM_BACKEND_MAX is 8.

The descriptor:

FieldRole
nameStable string. vm_cpu_name() returns it when this backend is selected.
featuresVM_CPU_BACKEND if the schedule is not itself an ISA bit.
priorityHigher wins among registered backends.
opsComplete vm_backend_ops. Must outlive the process.

Copy the builtin table, then replace the slots this schedule owns. vm_backend_builtin(VM_CPU_NEON) is NULL when NEON was not compiled.

const vm_backend_ops *builtin = vm_backend_builtin(VM_CPU_NEON);
ops = *builtin;
ops.vm_gemm_ukernel = my_ukernel;
ops.quat_mul_ptr = my_quat_mul;
backend.name = "my-backend";
backend.features = VM_CPU_BACKEND;
backend.priority = 100;
backend.ops = &ops;
vm_cpu_set_note("backend my-backend part=...");
return vm_backend_register(&backend);

Floats interfaces and ABI

vm_gemm_ukernel is the 8×8 tile, including when a backend is registered. vm_gemm_tile_register rejects (8, 8). The core also has a scalar 4×4 tile and, when those kernels are compiled, an AVX2 12×8 tile and an AVX-512 16×14 tile. With no backend, AVX-512 selects 16×14 and AVX2 selects 12×8. Another shape uses vm_gemm_tile_register. See Numerics extras.

Float symbols are width-suffixed. Internal functions that both object libs define need the same treatment.

A vendor float entry point uses VECMAT_SYMBOL. It pastes 32 or 64 with the same rule as include/vecmat/abi.h: VECMAT_ABI_BITS when the library object sets it, otherwise VECMAT_USE_F64, otherwise 32.

When ABI 32 is also built, the f64 object libraries define VECMAT_ABI_REPLICA. Integer bodies compile when it is unset and are skipped on that replica. A function that takes or returns vm_float_t stays outside the guard and uses VECMAT_SYMBOL.

The

string(APPEND VECMAT_MODULE_REGISTER_INCLUDES "#include <vecmat_cortex_a5x.h>\n")
string(APPEND VECMAT_MODULE_REGISTER_CALLS "    vm_cortex_a5x_register();\n")

The header rename (vm_cortex_a5x_register to vm_cortex_a5x_register64 on an f64 TU) happens because the generated file includes that header and the test target defines VECMAT_USE_F64.

Adding a module

  1. Add vendors/<vendor>/<name>/ with module.cmake, include/, src/, and doc/<name>.md.
  2. In module.cmake, set VECMAT_MODULE_KIND to backend, width, feature, or device.
  3. Append the lists for that kind. Do not add an #ifdef to test/test_main.c or test/bench_main.c.
  4. For a feature: put the public header at include/vecmat/feature/<name>.h and append VECMAT_MODULE_HOOKS. Do not append a backend or width register list.
  5. For a backend: probe in the module, not in src/features/cpu.c. Build the ops table from vm_backend_builtin() plus overrides. Pass VM_CPU_BACKEND unless the schedule is one of the existing ISA bits. The public header stays include/vecmat_*.h.
  6. For a device: set VECMAT_MODULE_KIND to device, build a separate library, and do not append VECMAT_MODULE_HOOKS or a backend register list. Register with vm_device_register. Do not fill vm_backend_ops. See nvidia/ga10x.
  7. Document the register call and any VECMAT_FORCE_* knobs in the module's own doc.