Skip to content

Fix riscv SIMD detection#52

Merged
szellmann merged 3 commits into
szellmann:masterfrom
carlosqwqqwq:riscv-visionaray
Jun 19, 2026
Merged

Fix riscv SIMD detection#52
szellmann merged 3 commits into
szellmann:masterfrom
carlosqwqqwq:riscv-visionaray

Conversation

@carlosqwqqwq

@carlosqwqqwq carlosqwqqwq commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Why

Visionaray already has a builtin no-SIMD fallback that can serve as a conservative baseline for riscv64, but its architecture and SIMD detection currently rely on raw host compiler macros. In practice this lets host x86_64 architecture and SSE state leak into simulated or cross-target riscv64 builds, which selects the wrong backend and includes the wrong intrinsics headers.

What changed

  • Update include/visionaray/math/simd/intrinsics.h to recognize __riscv explicitly and distinguish riscv32 from riscv64.
  • Rework SIMD ISA detection so x86 SSE/AVX probing only runs when the selected base architecture is x86, and ARM NEON probing only runs when the selected base architecture is ARM.
  • Leave all non-x86 and non-ARM targets, including riscv64, on the existing builtin no-SIMD fallback instead of inheriting host SIMD state.
  • Guard include/visionaray/math/detail/math.h so x86 intrinsics headers and rdtsc are only used on x86/x64, while non-x86 targets fall back to a conservative generic timer path.
  • Update README.md with a generic cross-compilation note so target CPU flags do not accidentally follow the build host.

Verification

  • Ran native CMake configuration with Ninja and optional components disabled to validate the public library/export path:
    • cmake -S . -B build-native-min -G Ninja -DVSNRAY_ENABLE_CUDA=OFF -DVSNRAY_ENABLE_COMMON=OFF -DVSNRAY_ENABLE_VIEWER=OFF -DVSNRAY_ENABLE_EXAMPLES=OFF -DVSNRAY_ENABLE_UNITTESTS=OFF
  • Ran the native build successfully:
    • cmake --build build-native-min --parallel 4
  • Ran the native install successfully and verified the exported headers and package files were produced:
    • cmake --install build-native-min --prefix build-native-min\install
  • Ran a real riscv64 cross-build in dockcross/linux-riscv64 for a minimal consumer that includes visionaray/math/simd/simd.h and visionaray/math/vector.h.
  • Verified the produced executable with:
    • file
    • readelf -h
  • Ran the resulting binary under qemu-riscv64 and confirmed the vector arithmetic smoke test prints:
    • visionaray-ok 5 7 9 10 21 36
  • Confirmed the native x86 CMake configuration still succeeds after the generic fallback fix.

Notes

This is a conservative portability patch. It does not add RVV or any other RISC-V SIMD backend. The goal is to make riscv64 select Visionaray's existing builtin fallback correctly and to prevent host x86_64 intrinsics or timing code from leaking into cross-target validation.

@szellmann

Copy link
Copy Markdown
Owner

Nice, thanks for the PR. One note on the SIMD fallback: I'm not really happy with that, as the auto vectorizer doesn't recognize the pattern but generates scalar code (the last time I tested this). This is much slower even than using single rays. Therefore I'd rather not advertise this code path explicitly in the README. In all other regards the PR looks good to me, though!

One thought I had was to add a generic vector code path using gcc's __builtin intrinsics, but as I don't use packets anymore these days, this has been in the backlog for a while now (in days of coding agents it might be ported in no time, though). Do you have a specific use case for the SIMD code path, or is this just about getting the project to compile on RISC-V systems?

If you could adapt the note in the readme I'd be really happy to merge the PR though!

@carlosqwqqwq

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback. You're right, this patch is only meant to fix target detection and get the project compiling correctly on RISC-V systems, not to advertise the current packet fallback. I updated the README note accordingly, and I appreciate the guidance.

@szellmann szellmann merged commit 945cf46 into szellmann:master Jun 19, 2026
6 checks passed
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.

2 participants