From a6f7596fb9dd6790b237f6a628d6af8d80930bdb Mon Sep 17 00:00:00 2001 From: DianQK Date: Sat, 19 Aug 2023 16:10:49 +0800 Subject: Add `PreservedSymbols` from LLVM to LTO. When building with LTO, builtin functions that are defined but whose calls have not been inserted yet, get internalized. We need to prevent these symbols from being internalized at LTO time. Refer to https://reviews.llvm.org/D49434. --- compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp | 104 ++++++++++++++++++++++- tests/run-make/wasm-builtins-import/Makefile | 7 ++ tests/run-make/wasm-builtins-import/main.rs | 14 +++ tests/run-make/wasm-builtins-import/verify.js | 9 ++ tests/run-make/wasm-spurious-import/Makefile | 7 -- tests/run-make/wasm-spurious-import/main.rs | 14 --- tests/run-make/wasm-spurious-import/verify.js | 9 -- 7 files changed, 133 insertions(+), 31 deletions(-) create mode 100644 tests/run-make/wasm-builtins-import/Makefile create mode 100644 tests/run-make/wasm-builtins-import/main.rs create mode 100644 tests/run-make/wasm-builtins-import/verify.js delete mode 100644 tests/run-make/wasm-spurious-import/Makefile delete mode 100644 tests/run-make/wasm-spurious-import/main.rs delete mode 100644 tests/run-make/wasm-spurious-import/verify.js diff --git a/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp index ff2ec388a5f..f338b914c67 100644 --- a/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp +++ b/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp @@ -1120,6 +1120,102 @@ extern "C" void LLVMRustPrintPasses() { PB.printPassNames(outs()); } +// from https://github.com/llvm/llvm-project/blob/7021182d6b43de9488ab70de626192ce70b3a4a6/llvm/lib/Object/IRSymtab.cpp#L48-L57 +static const char *PreservedLibcallSymbols[] = { +#define HANDLE_LIBCALL(code, name) name, +#include "llvm/IR/RuntimeLibcalls.def" +#undef HANDLE_LIBCALL + // RuntimeLibcalls.def missing symbols. + "__ctzsi2", + "__ctzdi2", + "__ctzti2", + "__ffssi2", + "__ffsdi2", + "__ffsti2", + "__paritysi2", + "__paritydi2", + "__parityti2", + "__popcountsi2", + "__popcountdi2", + "__popcountti2", + "__bswapsi2", + "__bswapdi2", + "__negti2", + "__udivmoddi4", + "__udivmodti4", + "__udivmodsi4", + "__divmodsi4", + "__divmoddi4", + "__divmodti4", + "__absvsi2", + "__absvdi2", + "__absvti2", + "__negvsi2", + "__negvdi2", + "__negvti2", + "__addvsi3", + "__addvdi3", + "__addvti3", + "__subvsi3", + "__subvdi3", + "__subvti3", + "__mulvsi3", + "__mulvdi3", + "__mulvti3", + "__cmpdi2", + "__cmpti2", + "__ucmpdi2", + "__ucmpti2", + "__mulsc3", + "__muldc3", + "__mulxc3", + "__multc3", + "__divsc3", + "__divdc3", + "__divxc3", + "__divtc3", + "__clear_cache", + "__enable_execute_stack", + "__gcc_personality_v0", + "__eprintf", + "__emutls_get_address", + "__trampoline_setup", + "__addsf3vfp", + "__adddf3vfp", + "__divsf3vfp", + "__divdf3vfp", + "__eqsf2vfp", + "__eqdf2vfp", + "__extendsfdf2vfp", + "__fixdfsivfp", + "__fixsfsivfp", + "__fixunssfsivfp", + "__fixunsdfsivfp", + "__floatsidfvfp", + "__floatsisfvfp", + "__floatunssidfvfp", + "__floatunssisfvfp", + "__gedf2vfp", + "__gesf2vfp", + "__gtdf2vfp", + "__gtsf2vfp", + "__ledf2vfp", + "__lesf2vfp", + "__ltdf2vfp", + "__ltsf2vfp", + "__muldf3vfp", + "__mulsf3vfp", + "__nedf2vfp", + "__negdf2vfp", + "__negsf2vfp", + "__negsf2vfp", + "__subdf3vfp", + "__subsf3vfp", + "__truncdfsf2vfp", + "__unorddf2vfp", + "__unordsf2vfp", +}; + extern "C" void LLVMRustRunRestrictionPass(LLVMModuleRef M, char **Symbols, size_t Len) { auto PreserveFunctions = [=](const GlobalValue &GV) { @@ -1135,7 +1231,7 @@ extern "C" void LLVMRustRunRestrictionPass(LLVMModuleRef M, char **Symbols, return true; } } - return false; + return llvm::is_contained(PreservedLibcallSymbols, GV.getName()); }; internalizeModule(*unwrap(M), PreserveFunctions); @@ -1293,6 +1389,12 @@ LLVMRustCreateThinLTOData(LLVMRustThinLTOModule *modules, auto GUID = GlobalValue::getGUID(preserved_symbols[i]); Ret->GUIDPreservedSymbols.insert(GUID); } + for (int i = 0; i < sizeof(PreservedLibcallSymbols) / sizeof(PreservedLibcallSymbols[0]); i++) { + if (auto *PreservedLibcallSymbol = PreservedLibcallSymbols[i]) { + auto GUID = GlobalValue::getGUID(PreservedLibcallSymbol); + Ret->GUIDPreservedSymbols.insert(GUID); + } + } // Collect the import/export lists for all modules from the call-graph in the // combined index diff --git a/tests/run-make/wasm-builtins-import/Makefile b/tests/run-make/wasm-builtins-import/Makefile new file mode 100644 index 00000000000..ff9dfeac6d0 --- /dev/null +++ b/tests/run-make/wasm-builtins-import/Makefile @@ -0,0 +1,7 @@ +include ../tools.mk + +# only-wasm32-bare + +all: + $(RUSTC) main.rs -C overflow-checks=yes -C panic=abort -C lto -C opt-level=z --target wasm32-unknown-unknown + $(NODE) verify.js $(TMPDIR)/main.wasm diff --git a/tests/run-make/wasm-builtins-import/main.rs b/tests/run-make/wasm-builtins-import/main.rs new file mode 100644 index 00000000000..d7dbbe32ca4 --- /dev/null +++ b/tests/run-make/wasm-builtins-import/main.rs @@ -0,0 +1,14 @@ +#![crate_type = "cdylib"] +#![no_std] + +#[panic_handler] +fn my_panic(_info: &core::panic::PanicInfo) -> ! { + loop {} +} + +#[no_mangle] +pub fn multer(a: i128, b: i128) -> i128 { + // Trigger usage of the __multi3 compiler intrinsic which then leads to an imported function + // such as panic or __multi3 in case of a bug. We verify that no imports exist in our verifier. + a * b +} diff --git a/tests/run-make/wasm-builtins-import/verify.js b/tests/run-make/wasm-builtins-import/verify.js new file mode 100644 index 00000000000..d3b2101b662 --- /dev/null +++ b/tests/run-make/wasm-builtins-import/verify.js @@ -0,0 +1,9 @@ +const fs = require('fs'); +const process = require('process'); +const assert = require('assert'); +const buffer = fs.readFileSync(process.argv[2]); + +let m = new WebAssembly.Module(buffer); +let imports = WebAssembly.Module.imports(m); +console.log('imports', imports); +assert.strictEqual(imports.length, 0); diff --git a/tests/run-make/wasm-spurious-import/Makefile b/tests/run-make/wasm-spurious-import/Makefile deleted file mode 100644 index ff9dfeac6d0..00000000000 --- a/tests/run-make/wasm-spurious-import/Makefile +++ /dev/null @@ -1,7 +0,0 @@ -include ../tools.mk - -# only-wasm32-bare - -all: - $(RUSTC) main.rs -C overflow-checks=yes -C panic=abort -C lto -C opt-level=z --target wasm32-unknown-unknown - $(NODE) verify.js $(TMPDIR)/main.wasm diff --git a/tests/run-make/wasm-spurious-import/main.rs b/tests/run-make/wasm-spurious-import/main.rs deleted file mode 100644 index fcbead5e28b..00000000000 --- a/tests/run-make/wasm-spurious-import/main.rs +++ /dev/null @@ -1,14 +0,0 @@ -#![crate_type = "cdylib"] -#![no_std] - -#[panic_handler] -fn my_panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} - -#[no_mangle] -pub fn multer(a: i128, b: i128) -> i128 { - // Trigger usage of the __multi3 compiler intrinsic which then leads to an imported - // panic function in case of a bug. We verify that no imports exist in our verifier. - a * b -} diff --git a/tests/run-make/wasm-spurious-import/verify.js b/tests/run-make/wasm-spurious-import/verify.js deleted file mode 100644 index d3b2101b662..00000000000 --- a/tests/run-make/wasm-spurious-import/verify.js +++ /dev/null @@ -1,9 +0,0 @@ -const fs = require('fs'); -const process = require('process'); -const assert = require('assert'); -const buffer = fs.readFileSync(process.argv[2]); - -let m = new WebAssembly.Module(buffer); -let imports = WebAssembly.Module.imports(m); -console.log('imports', imports); -assert.strictEqual(imports.length, 0); -- cgit 1.4.1-3-g733a5