build: Use try_compile for robust RVV toolchain detection#3905
Draft
ihb2032 wants to merge 2 commits intoalibaba:masterfrom
Draft
build: Use try_compile for robust RVV toolchain detection#3905ihb2032 wants to merge 2 commits intoalibaba:masterfrom
ihb2032 wants to merge 2 commits intoalibaba:masterfrom
Conversation
added 2 commits
September 22, 2025 15:33
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 <liuyudong@iscas.ac.cn>
Signed-off-by: ihb2032 <1355790728@qq.com>
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 <liuyudong@iscas.ac.cn> Signed-off-by: ihb2032 <1355790728@qq.com>
Collaborator
|
Hi there, I noticed this PR is still in Draft status. Do you plan to make further modifications to get it ready for merging? Please let me know if you need any feedback. |
|
ihb2032 seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: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
if (... MATCHES "riscv64")logic has been replaced with a more intelligenttry_compileblock.-march=rv64gcvflag and defines the__riscv_vmacro, which is the standard-compliant way to verify vector support.STATUSandWARNINGmessages are now provided to inform the user about the result of the toolchain check.CMakeFiles/CMakeTmp) used bytry_compileis 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:
This change should make the build process much more robust for all developers working with the RISC-V backend.