Merge commit b84d773fd004 from llvm git (by Fangrui Song):

[Parallel] Revert sequential task changes

  https://reviews.llvm.org/D148728 introduced `bool Sequential` to unify
  `execute` and the old `spawn` without argument. However, sequential
  tasks might be executed by any worker thread (non-deterministic),
  leading to non-determinism output for ld.lld -z nocombreloc (see
  https://reviews.llvm.org/D133003).

  In addition, the extra member variables have overhead.
  This sequential task has only been used for lld parallel relocation
  scanning.

  This patch restores the behavior before https://reviews.llvm.org/D148728 .

  Fix #105958

  Pull Request: https://github.com/llvm/llvm-project/pull/109084

This fixes the non-reproducibility we had noticed when linking our EFI
loaders, and for which we committed a workaround in f5ce3f4ef5.

MFC after:	3 days
This commit is contained in:
Dimitry Andric 2024-09-22 19:08:47 +02:00
parent f5a8f6f71a
commit 54521a2ff9
3 changed files with 48 additions and 61 deletions

View file

@ -1584,30 +1584,44 @@ template <class ELFT> void elf::scanRelocations() {
bool serial = !config->zCombreloc || config->emachine == EM_MIPS || bool serial = !config->zCombreloc || config->emachine == EM_MIPS ||
config->emachine == EM_PPC64; config->emachine == EM_PPC64;
parallel::TaskGroup tg; parallel::TaskGroup tg;
for (ELFFileBase *f : ctx.objectFiles) { auto outerFn = [&]() {
auto fn = [f]() { for (ELFFileBase *f : ctx.objectFiles) {
auto fn = [f]() {
RelocationScanner scanner;
for (InputSectionBase *s : f->getSections()) {
if (s && s->kind() == SectionBase::Regular && s->isLive() &&
(s->flags & SHF_ALLOC) &&
!(s->type == SHT_ARM_EXIDX && config->emachine == EM_ARM))
scanner.template scanSection<ELFT>(*s);
}
};
if (serial)
fn();
else
tg.spawn(fn);
}
auto scanEH = [] {
RelocationScanner scanner; RelocationScanner scanner;
for (InputSectionBase *s : f->getSections()) { for (Partition &part : partitions) {
if (s && s->kind() == SectionBase::Regular && s->isLive() && for (EhInputSection *sec : part.ehFrame->sections)
(s->flags & SHF_ALLOC) && scanner.template scanSection<ELFT>(*sec);
!(s->type == SHT_ARM_EXIDX && config->emachine == EM_ARM)) if (part.armExidx && part.armExidx->isLive())
scanner.template scanSection<ELFT>(*s); for (InputSection *sec : part.armExidx->exidxSections)
if (sec->isLive())
scanner.template scanSection<ELFT>(*sec);
} }
}; };
tg.spawn(fn, serial); if (serial)
} scanEH();
else
tg.spawn([] { tg.spawn(scanEH);
RelocationScanner scanner; };
for (Partition &part : partitions) { // If `serial` is true, call `spawn` to ensure that `scanner` runs in a thread
for (EhInputSection *sec : part.ehFrame->sections) // with valid getThreadIndex().
scanner.template scanSection<ELFT>(*sec); if (serial)
if (part.armExidx && part.armExidx->isLive()) tg.spawn(outerFn);
for (InputSection *sec : part.armExidx->exidxSections) else
if (sec->isLive()) outerFn();
scanner.template scanSection<ELFT>(*sec);
}
});
} }
static bool handleNonPreemptibleIfunc(Symbol &sym, uint16_t flags) { static bool handleNonPreemptibleIfunc(Symbol &sym, uint16_t flags) {

View file

@ -97,9 +97,7 @@ public:
// Spawn a task, but does not wait for it to finish. // Spawn a task, but does not wait for it to finish.
// Tasks marked with \p Sequential will be executed // Tasks marked with \p Sequential will be executed
// exactly in the order which they were spawned. // exactly in the order which they were spawned.
// Note: Sequential tasks may be executed on different void spawn(std::function<void()> f);
// threads, but strictly in sequential order.
void spawn(std::function<void()> f, bool Sequential = false);
void sync() const { L.sync(); } void sync() const { L.sync(); }

View file

@ -12,7 +12,6 @@
#include "llvm/Support/Threading.h" #include "llvm/Support/Threading.h"
#include <atomic> #include <atomic>
#include <deque>
#include <future> #include <future>
#include <thread> #include <thread>
#include <vector> #include <vector>
@ -39,7 +38,7 @@ namespace {
class Executor { class Executor {
public: public:
virtual ~Executor() = default; virtual ~Executor() = default;
virtual void add(std::function<void()> func, bool Sequential = false) = 0; virtual void add(std::function<void()> func) = 0;
virtual size_t getThreadCount() const = 0; virtual size_t getThreadCount() const = 0;
static Executor *getDefaultExecutor(); static Executor *getDefaultExecutor();
@ -98,13 +97,10 @@ public:
static void call(void *Ptr) { ((ThreadPoolExecutor *)Ptr)->stop(); } static void call(void *Ptr) { ((ThreadPoolExecutor *)Ptr)->stop(); }
}; };
void add(std::function<void()> F, bool Sequential = false) override { void add(std::function<void()> F) override {
{ {
std::lock_guard<std::mutex> Lock(Mutex); std::lock_guard<std::mutex> Lock(Mutex);
if (Sequential) WorkStack.push_back(std::move(F));
WorkQueueSequential.emplace_front(std::move(F));
else
WorkQueue.emplace_back(std::move(F));
} }
Cond.notify_one(); Cond.notify_one();
} }
@ -112,42 +108,23 @@ public:
size_t getThreadCount() const override { return ThreadCount; } size_t getThreadCount() const override { return ThreadCount; }
private: private:
bool hasSequentialTasks() const {
return !WorkQueueSequential.empty() && !SequentialQueueIsLocked;
}
bool hasGeneralTasks() const { return !WorkQueue.empty(); }
void work(ThreadPoolStrategy S, unsigned ThreadID) { void work(ThreadPoolStrategy S, unsigned ThreadID) {
threadIndex = ThreadID; threadIndex = ThreadID;
S.apply_thread_strategy(ThreadID); S.apply_thread_strategy(ThreadID);
while (true) { while (true) {
std::unique_lock<std::mutex> Lock(Mutex); std::unique_lock<std::mutex> Lock(Mutex);
Cond.wait(Lock, [&] { Cond.wait(Lock, [&] { return Stop || !WorkStack.empty(); });
return Stop || hasGeneralTasks() || hasSequentialTasks();
});
if (Stop) if (Stop)
break; break;
bool Sequential = hasSequentialTasks(); auto Task = std::move(WorkStack.back());
if (Sequential) WorkStack.pop_back();
SequentialQueueIsLocked = true;
else
assert(hasGeneralTasks());
auto &Queue = Sequential ? WorkQueueSequential : WorkQueue;
auto Task = std::move(Queue.back());
Queue.pop_back();
Lock.unlock(); Lock.unlock();
Task(); Task();
if (Sequential)
SequentialQueueIsLocked = false;
} }
} }
std::atomic<bool> Stop{false}; std::atomic<bool> Stop{false};
std::atomic<bool> SequentialQueueIsLocked{false}; std::vector<std::function<void()>> WorkStack;
std::deque<std::function<void()>> WorkQueue;
std::deque<std::function<void()>> WorkQueueSequential;
std::mutex Mutex; std::mutex Mutex;
std::condition_variable Cond; std::condition_variable Cond;
std::promise<void> ThreadsCreated; std::promise<void> ThreadsCreated;
@ -205,16 +182,14 @@ TaskGroup::~TaskGroup() {
L.sync(); L.sync();
} }
void TaskGroup::spawn(std::function<void()> F, bool Sequential) { void TaskGroup::spawn(std::function<void()> F) {
#if LLVM_ENABLE_THREADS #if LLVM_ENABLE_THREADS
if (Parallel) { if (Parallel) {
L.inc(); L.inc();
detail::Executor::getDefaultExecutor()->add( detail::Executor::getDefaultExecutor()->add([&, F = std::move(F)] {
[&, F = std::move(F)] { F();
F(); L.dec();
L.dec(); });
},
Sequential);
return; return;
} }
#endif #endif