Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wasm] Enable PIC for WebAssembly on LLVM v18.x #7803

Merged
merged 6 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/CodeGen_LLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,17 @@ void CodeGen_LLVM::optimize_module() {
using OptimizationLevel = llvm::OptimizationLevel;
OptimizationLevel level = OptimizationLevel::O3;

#if LLVM_VERSION >= 180
if (tm->isPositionIndependent()) {
// Add a pass that converts lookup tables to relative lookup tables to make them PIC-friendly.
// See https://bugs.llvm.org/show_bug.cgi?id=45244
pb.registerOptimizerLastEPCallback(
[&](ModulePassManager &mpm, OptimizationLevel level) {
mpm.addPass(RelLookupTableConverterPass());
});
}
#endif

if (get_target().has_feature(Target::SanitizerCoverage)) {
pb.registerOptimizerLastEPCallback(
[&](ModulePassManager &mpm, OptimizationLevel level) {
Expand Down
19 changes: 19 additions & 0 deletions src/CodeGen_WebAssembly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,14 @@ string CodeGen_WebAssembly::mattrs() const {
sep = ",";
}

// PIC implies +mutable-globals because the PIC ABI used by the linker
// depends on importing and exporting mutable globals. Also -pthread implies
// mutable-globals too, so quitely enable it if either of these are specified.
if (use_pic() || target.has_feature(Target::WasmThreads)) {
s << sep << "+mutable-globals";
sep = ",";
}

// Recent Emscripten builds assume that specifying `-pthread` implies bulk-memory too,
// so quietly enable it if either of these are specified.
if (target.has_feature(Target::WasmBulkMemory) || target.has_feature(Target::WasmThreads)) {
Expand All @@ -362,7 +370,18 @@ bool CodeGen_WebAssembly::use_soft_float_abi() const {
}

bool CodeGen_WebAssembly::use_pic() const {
#if LLVM_VERSION >= 180
// Issues with WASM PIC and dynamic linking only got fixed in LLVM v18.x (June 26th 2023)
// See https://reviews.llvm.org/D153293

// Always emitting PIC "does add a little bloat to the object files, due to the extra
// indirection, but when linked into a static binary 100% of this can be removed by
// wasm-opt in release builds."
// See https://github.com/halide/Halide/issues/7796
return true;
#else
return false;
#endif
}

int CodeGen_WebAssembly::native_vector_bits() const {
Expand Down
3 changes: 3 additions & 0 deletions src/LLVM_Headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@
#include <llvm/Transforms/Scalar/GVN.h>
#include <llvm/Transforms/Utils/ModuleUtils.h>
#include <llvm/Transforms/Utils/SymbolRewriter.h>
#if LLVM_VERSION >= 180
#include <llvm/Transforms/Utils/RelLookupTableConverter.h>
#endif

// IWYU pragma: end_exports

Expand Down
4 changes: 4 additions & 0 deletions src/LLVM_Output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,10 @@ void emit_file(const llvm::Module &module_in, Internal::LLVMOStream &out,
pass_manager.add(llvm::createRewriteSymbolsPass());
#endif

if (target_machine->isPositionIndependent()) {
Internal::debug(1) << "Target machine is Position Independent!\n";
}

// Override default to generate verbose assembly.
target_machine->Options.MCOptions.AsmVerbose = true;

Expand Down