Skip to content

Conversation

@ihb2032
Copy link
Contributor

@ihb2032 ihb2032 commented Sep 22, 2025

Description

This PR significantly improves the reliability of the RISC-V Vector (RVV) feature detection in the CMake build system. It replaces the previous brittle architecture name check with a robust try_compile-based toolchain validation.

Motivation and Context

The existing method for enabling RVV optimizations relied solely on checking if the target architecture string matched riscv64. This approach is fragile and can cause the build to fail in several common scenarios:

  • Using an older RISC-V toolchain that does not support the 'V' extension.
  • Using a modern toolchain that was configured and built without vector support.
  • Incorrectly configured cross-compilation environments.

The goal of this PR is to prevent these build failures by verifying the compiler's capabilities directly, ensuring a smoother and more reliable experience for developers targeting RISC-V.

Key Changes

  1. Replaced Architecture String Check: The simple if (... MATCHES "riscv64") logic has been replaced with a more intelligent try_compile block.
  2. Robust Toolchain Validation: The new check compiles a small test program to ensure the toolchain can correctly handle the -march=rv64gcv flag and defines the __riscv_v macro, which is the standard-compliant way to verify vector support.
  3. Cross-Compilation Safe: This method works seamlessly for both native and cross-compilation, as it tests the configured target compiler, not the host compiler.
  4. Improved Diagnostic Messages: Clear STATUS and WARNING messages are now provided to inform the user about the result of the toolchain check.
  5. Automatic Cleanup: The entire temporary directory (CMakeFiles/CMakeTmp) used by try_compile is now removed after the check, ensuring a clean build directory.

How Has This Been Tested?

I have tested this new logic on my development environment:

  • OS: EulixOS 3.0
  • Compiler: gcc15.1
  • Platform: sg2044
  • Scenarios:
    • Successfully configured and built the project with the RVV backend enabled, using a capable toolchain.

This change should make the build process much more robust for all developers working with the RISC-V backend.

The previous RVV detection mechanism was unreliable as it only checked the architecture name string ("riscv64"). This could lead to build failures if the configured C/C++ toolchain, despite targeting riscv64, did not actually support the Vector ('V') extension or the required compiler flags.
This commit replaces the simple string check with a robust `try_compile` test. The new implementation verifies the toolchain's actual capabilities by:
- Compiling a test source file with the `-march=rv64gcv` flag.
- Ensuring the `__riscv_v` macro is defined by the compiler.
This approach correctly handles both native and cross-compilation scenarios, preventing build errors and providing clearer diagnostic messages. Additionally, it cleans up all temporary files used during the check.

Signed-off-by: lyd1992 <[email protected]>
Signed-off-by: ihb2032 <[email protected]>
Refactored the RISC-V backend CMake script to resolve a `g++` error during the main build, which was caused by duplicate compiler flags.

- **Removed**: The logic that hard-coded `-march` and `-mabi` flags via `target_compile_options` is now gone.
- **Kept**: The `try_compile` test is preserved, but its role is now only to **verify** that the flags passed globally by the user (via `CMAKE_C(XX)_FLAGS`) effectively enable RVV support.
- **Reliance**: Compilation of RVV sources now depends entirely on the user's externally specified global build options, which prevents conflicts and parameter errors.
- **Improved**: If `MNN_USE_RVV=ON` but the flag verification fails, the configuration now terminates immediately with a `FATAL_ERROR`, providing earlier and clearer feedback.

Signed-off-by: lyd1992 <[email protected]>
Signed-off-by: ihb2032 <[email protected]>
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.

1 participant