Fix multiple RISC‑V build issues: correct -march generation and improve LLD detection (CMake & Makefile)#14530
Fix multiple RISC‑V build issues: correct -march generation and improve LLD detection (CMake & Makefile)#14530fengpengboa wants to merge 1 commit into
Conversation
|
Hi @fengpengboa! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Hi @archang19 , I have a CLA-related question for this PR.Our company has previously signed the Meta Corporate CLA for other open source projects. When I tried to sign again for RocksDB, the system indicated that our company is already registered and prevented me from signing a new one.However, I do not know who the original signer was, and I am unable to locate that person internally to be added to the authorized contributor list. |
6c6276d to
311feb7
Compare
|
Could you close multiple PRs, and just have one PR for fixing build issue on RISC-V? |
Sure, will close the other two PRs and put everything into this one. Thanks for the suggestion! |
311feb7 to
a696c65
Compare
…ve LLD detection (CMake & Makefile)
a696c65 to
fb23811
Compare
|
@xingbowang, I have merged the RISC-V build/compilation PR into this one. Could you please review when you get a chance? Thanks. |
Description
This PR consolidates fixes for two independent but related build problems on RISC‑V:
All changes are now included in this single PR. The other two PRs will be closed.
Problem 1 – Dead code and broken -march on modern RISC‑V
Background
In
build_tools/build_detect_platform, there is a section intended to optimize RocksDB for RISC-V natively by parsing/proc/cpuinfoto populate the-marchflag.However, it contained a typo: it assigned the result toRISC_ISAbut checkedif [ -n "${RISCV_ISA}" ]. Due to this typo, the RISC-V specific hardware optimization was effectively dead code and never executed.If one simply fixes the typo (RISC_ISA->RISCV_ISA), the build immediately breaks on modern RISC-V hardware due to a mismatch between bleeding-edge Linux kernels and the GNU toolchain.Crash 1: GCC rejects privileged extensions
Modern RISC-V Linux kernels report Supervisor-level (
_s*), Hypervisor-level (_h*), and Custom (_x*) extensions in/proc/cpuinfo(e.g.,sscofpmf_sstc_svinval).Passing this raw string to GCC when compiling user-space code like RocksDB causes a fatal error:
Crash 2: Binutils (Assembler) truncates the ISA string and loses
zbb/zbsEven if we strip the privileged
_s*extensions, the kernel also exposes newly ratified user-space extensions (likezicntrorzihpm). Older versions of Binutils (as) do not recognize these newzi*extensions.When the assembler encounters an unknown extension like
zicntr, it throws:The Solution:
To make the build script robust against future kernel updates and lagging toolchains, this PR introduces:
rv64imafdcv) and strictly append only safe, performance-enhancing extensions that RocksDB actually benefits from:_zb*(Bitmanip),_zv*(Vector), and_zk*(Crypto).echo "int main(){}" | $CXX ...). Before trusting the synthesized-marchstring, we test if the local host compiler can actually parse it. If it fails (e.g., severely outdated GCC), it gracefully falls back to the compiler's default.Problem 2 – LLD detection is too simple and breaks on RISC‑V (CMake + Makefile)
Background——CMake
Currently, CMake only checks if the
lldexecutable exists and can output its version (execute_process(COMMAND ... -Wl,--version)). It does not verify iflldcan actually link the object files generated by the current compiler.On RISC-V architecture, modern GCC emits
R_RISCV_SET_ULEB128(60) andR_RISCV_SUB_ULEB128(61) relocations in the.gcc_except_tablesection for C++ exception handling. Older versions of LLD do not support these ULEB128 relocations. Because the current CMake script blindly enables LLD without a feature test,the build fails midway withld.lld: error: unknown relocation (60) against symbolThe Solution :
Replaced the simple version check with a robust feature test using CMake's
CheckCXXSourceCompiles. I added a minimal test snippet containing atry-catchblock. This forces the compiler to generate the.gcc_except_table. If LLD fails to link it, CMake gracefully falls back to the default compiler linker (e.g., GNUld.bfd) instead of breaking the build.Background——Makefile
This is the Makefile/script equivalent of the CMake fix for the LLD linker detection. It ensures that
build_detect_platformdoes not falsely enable LLD on systems where it cannot link C++ exception tables (specifically on RISC-V with older LLD versions).The current script checks if LLD is usable by compiling a trivial program:int main() { return 0; }.This check is too simple because it does not generate a
.gcc_except_tablesection.On RISC-V, modern compilers use
R_RISCV_SET_ULEB128relocations for C++ exceptions. Older LLD versions do not support these relocations. Because the trivialmain()test passes successfully, the script adds-fuse-ld=lldtoPLATFORM_LDFLAGS. Later, when compiling actual RocksDB code (which heavily uses exceptions), the build crashes withunknown relocation (60).The Solution:
Updated the dummy C++ code in
build_tools/build_detect_platformto include atry { throw 1; } catch (...) {}block.This forces the compiler to generate the exception handling sections during the detection phase. If the installed LLD cannot link these sections, the test fails, and the script safely ignores LLD, falling back to the default GNU linker.
Test
Tested natively on a modern RISC-V (rv64) SG2044 environment