Skip to content

Commit

Permalink
Revise LLVM fix to work when no V8 or WABT available (#7635)
Browse files Browse the repository at this point in the history
* Revise LLVM fix to work when no V8 or WABT available

* Update WasmExecutor.cpp

* Update WasmExecutor.cpp

* Update WasmExecutor.cpp
  • Loading branch information
steven-johnson authored Jun 15, 2023
1 parent 932ad0b commit 2149734
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions src/WasmExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@
#endif // WITH_V8
// clang-format on

#if WITH_WABT || WITH_V8
#if LLVM_VERSION >= 170
LLD_HAS_DRIVER(wasm)
#endif
#endif

namespace Halide {
namespace Internal {
Expand Down Expand Up @@ -339,7 +341,10 @@ std::vector<char> compile_to_wasm(const Module &module, const std::string &fn_na

std::string lld_arg_strs[] = {
"HalideJITLinker",
"-flavor", "wasm",
#if LLVM_VERSION >= 170
"-flavor",
"wasm",
#endif
// For debugging purposes:
// "--verbose",
// "-error-limit=0",
Expand All @@ -350,7 +355,8 @@ std::vector<char> compile_to_wasm(const Module &module, const std::string &fn_na
obj_file.pathname(),
"--entry=" + fn_name,
"-o",
wasm_output.pathname()};
wasm_output.pathname()
};

constexpr int c = sizeof(lld_arg_strs) / sizeof(lld_arg_strs[0]);
const char *lld_args[c];
Expand All @@ -359,15 +365,29 @@ std::vector<char> compile_to_wasm(const Module &module, const std::string &fn_na
}

#if LLVM_VERSION >= 170
// lld will temporarily hijack the signal handlers to ensure that temp files get cleaned up,
// but rather than preserving custom handlers in place, it restores the default handlers.
// This conflicts with some of our testing infrastructure, which relies on a SIGABRT handler
// set at global-ctor time to stay set. Therefore we'll save and restore this ourselves.
// Note that we must restore it before using internal_error (and also on the non-error path).
auto old_abort_handler = std::signal(SIGABRT, SIG_DFL);

llvm::ArrayRef<const char *> args(lld_args, lld_args + c);
auto r = lld::lldMain(args, llvm::outs(), llvm::errs(), {{lld::Wasm, &lld::wasm::link}});
if (!r.canRunAgain) {
std::cerr << "lld::wasm::link failed catastrophically, exiting with: " << r.retCode << "\n";
lld::exitLld(r.retCode); // Exit now, can't re-execute again.
}
// TODO: https://reviews.llvm.org/D119049 suggests that you should call exitLld()
// if canRunAgain is false, but doing do fails with SIGABRT rather than exit(1), which
// breaks our error tests. For now, just following the old practice.
//
// if (!r.canRunAgain) {
// std::cerr << "lld::wasm::link failed catastrophically, exiting with: " << r.retCode << "\n";
// lld::exitLld(r.retCode); // Exit now, can't re-execute again.
// }
if (r.retCode != 0) {
std::signal(SIGABRT, old_abort_handler);
internal_error << "lld::wasm::link failed with: " << r.retCode << "\n";
}

std::signal(SIGABRT, old_abort_handler);
#else
// lld will temporarily hijack the signal handlers to ensure that temp files get cleaned up,
// but rather than preserving custom handlers in place, it restores the default handlers.
Expand Down

0 comments on commit 2149734

Please sign in to comment.