Skip to content

Commit

Permalink
[wasm] Enable PIC for WebAssembly on LLVM v18.x (halide#7803)
Browse files Browse the repository at this point in the history
* Enable PIC code generation for WebAssembly for LLVM >18.
Enable +mutable-globals to support dynamic linking

* Fix LLVM v18 interface changes for writeArchive()
Add RelLookupTableConverterPass for PIC (in LLVM v18)

* Resolve conflict for writeArchive interface changes.

* Clang format pass

---------

Co-authored-by: Derek Gerstmann <dgerstmann@adobe.com>
  • Loading branch information
2 people authored and ardier committed Mar 3, 2024
1 parent 64623eb commit 10e0360
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
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

0 comments on commit 10e0360

Please sign in to comment.