diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -133,6 +133,12 @@ return errorCount() == 0; } +void elf::cleanup() { + // Delete the global context and clear the global context pointer, so that it + // cannot be accessed anymore. + CommonLinkerContext::destroy(); +} + // Parses a linker -m option. static std::tuple parseEmulation(StringRef emul) { uint8_t osabi = 0; diff --git a/lld/include/lld/Common/Driver.h b/lld/include/lld/Common/Driver.h --- a/lld/include/lld/Common/Driver.h +++ b/lld/include/lld/Common/Driver.h @@ -41,7 +41,12 @@ namespace elf { bool link(llvm::ArrayRef args, llvm::raw_ostream &stdoutOS, llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput); -} + +// Temporary API that forces global state cleanup between explicit calls to the +// ELF driver above. DO NOT USE - this will be replaced by safeLldMain(). See +// discussion in https://reviews.llvm.org/D119049. +void cleanup(); +} // namespace elf namespace macho { bool link(llvm::ArrayRef args, llvm::raw_ostream &stdoutOS, diff --git a/mlir/lib/Dialect/GPU/Transforms/SerializeToHsaco.cpp b/mlir/lib/Dialect/GPU/Transforms/SerializeToHsaco.cpp --- a/mlir/lib/Dialect/GPU/Transforms/SerializeToHsaco.cpp +++ b/mlir/lib/Dialect/GPU/Transforms/SerializeToHsaco.cpp @@ -438,10 +438,13 @@ static std::mutex mutex; const std::lock_guard lock(mutex); // Invoke lld. Expect a true return value from lld. - if (!lld::elf::link({"ld.lld", "-shared", tempIsaBinaryFilename.c_str(), - "-o", tempHsacoFilename.c_str()}, - llvm::outs(), llvm::errs(), /*exitEarly=*/true, - /*disableOutput=*/false)) { + bool r = lld::elf::link({"ld.lld", "-shared", tempIsaBinaryFilename.c_str(), + "-o", tempHsacoFilename.c_str()}, + llvm::outs(), llvm::errs(), /*exitEarly=*/false, + /*disableOutput=*/false); + // Allow for calling the driver again in the same process. + lld::cleanup(); + if (!r) { emitError(loc, "lld invocation error"); return {}; }