Skip to content
This repository has been archived by the owner on Feb 5, 2019. It is now read-only.

Commit

Permalink
Don't compile usage of std::thread
Browse files Browse the repository at this point in the history
As of the time of this writing it's not actually used anywhere meaningfullly
throughout the LLVM repo that we need, and it unfortunately uses `std::thread`
which isn't available in mingw-w64 toolchains with the win32 threading model
(the one that we use).

Two major changes were made to achieve this:

1. The `ThreadPool.cpp` file was just entirely commented out. This isn't used
   anywhere in the LLVM repo nor in Rust itself.
2. The `ParallelCG.cpp` file was mostly deleted. Unfortunately it's used a few
   places in LLVM and is needed to link correctly, but we in Rust don't use it
   at all. For now it's just a stub implementation that hopefully compiles
   everywhere, but perhaps we can find a less invasive (aka doesn't have rebase
   conflicts in the future) change to apply soon.

For reference, the upstream LLVM bug has been reported [1]

[1]: https://llvm.org/bugs/show_bug.cgi?id=26365
  • Loading branch information
alexcrichton committed Feb 20, 2016
1 parent b9e8f0c commit 69ef168
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 62 deletions.
4 changes: 4 additions & 0 deletions include/llvm/Support/ThreadPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
//
//===----------------------------------------------------------------------===//

#if 0

#ifndef LLVM_SUPPORT_THREAD_POOL_H
#define LLVM_SUPPORT_THREAD_POOL_H

Expand Down Expand Up @@ -134,3 +136,5 @@ class ThreadPool {
}

#endif // LLVM_SUPPORT_THREAD_POOL_H

#endif
4 changes: 4 additions & 0 deletions include/llvm/Support/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
//
//===----------------------------------------------------------------------===//

#if 0

#ifndef LLVM_SUPPORT_THREAD_H
#define LLVM_SUPPORT_THREAD_H

Expand Down Expand Up @@ -64,3 +66,5 @@ struct thread {
#endif // LLVM_ENABLE_THREADS

#endif

#endif
63 changes: 1 addition & 62 deletions lib/CodeGen/ParallelCG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,72 +25,11 @@

using namespace llvm;

static void codegen(Module *M, llvm::raw_pwrite_stream &OS,
const Target *TheTarget, StringRef CPU, StringRef Features,
const TargetOptions &Options, Reloc::Model RM,
CodeModel::Model CM, CodeGenOpt::Level OL,
TargetMachine::CodeGenFileType FileType) {
std::unique_ptr<TargetMachine> TM(TheTarget->createTargetMachine(
M->getTargetTriple(), CPU, Features, Options, RM, CM, OL));

legacy::PassManager CodeGenPasses;
if (TM->addPassesToEmitFile(CodeGenPasses, OS, FileType))
report_fatal_error("Failed to setup codegen");
CodeGenPasses.run(*M);
}

std::unique_ptr<Module>
llvm::splitCodeGen(std::unique_ptr<Module> M,
ArrayRef<llvm::raw_pwrite_stream *> OSs, StringRef CPU,
StringRef Features, const TargetOptions &Options,
Reloc::Model RM, CodeModel::Model CM, CodeGenOpt::Level OL,
TargetMachine::CodeGenFileType FileType) {
StringRef TripleStr = M->getTargetTriple();
std::string ErrMsg;
const Target *TheTarget = TargetRegistry::lookupTarget(TripleStr, ErrMsg);
if (!TheTarget)
report_fatal_error(Twine("Target not found: ") + ErrMsg);

if (OSs.size() == 1) {
codegen(M.get(), *OSs[0], TheTarget, CPU, Features, Options, RM, CM,
OL, FileType);
return M;
}

std::vector<thread> Threads;
SplitModule(std::move(M), OSs.size(), [&](std::unique_ptr<Module> MPart) {
// We want to clone the module in a new context to multi-thread the codegen.
// We do it by serializing partition modules to bitcode (while still on the
// main thread, in order to avoid data races) and spinning up new threads
// which deserialize the partitions into separate contexts.
// FIXME: Provide a more direct way to do this in LLVM.
SmallVector<char, 0> BC;
raw_svector_ostream BCOS(BC);
WriteBitcodeToFile(MPart.get(), BCOS);

llvm::raw_pwrite_stream *ThreadOS = OSs[Threads.size()];
Threads.emplace_back(
[TheTarget, CPU, Features, Options, RM, CM, OL, FileType,
ThreadOS](const SmallVector<char, 0> &BC) {
LLVMContext Ctx;
ErrorOr<std::unique_ptr<Module>> MOrErr =
parseBitcodeFile(MemoryBufferRef(StringRef(BC.data(), BC.size()),
"<split-module>"),
Ctx);
if (!MOrErr)
report_fatal_error("Failed to read bitcode");
std::unique_ptr<Module> MPartInCtx = std::move(MOrErr.get());

codegen(MPartInCtx.get(), *ThreadOS, TheTarget, CPU, Features,
Options, RM, CM, OL, FileType);
},
// Pass BC using std::move to ensure that it get moved rather than
// copied into the thread's context.
std::move(BC));
});

for (thread &T : Threads)
T.join();

return {};
return M;
}
4 changes: 4 additions & 0 deletions lib/Support/ThreadPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
//
//===----------------------------------------------------------------------===//

#if 0