diff --git a/main.cc b/main.cc index 4776dfb1..06d9a478 100644 --- a/main.cc +++ b/main.cc @@ -249,6 +249,41 @@ int main(int argc, char **argv) { { MyTimer t("bin_sections", before_copy); +#if 1 + typedef std::vector> T; + + auto fn = [&](const tbb::blocked_range &range, const T &init) { + T vec = init; + for (int i = range.begin(); i < range.end(); i++) { + ObjectFile *file = files[i]; + for (InputSection *isec : file->sections) { + if (!isec) + continue; + OutputSection *osec = isec->output_section; + vec[osec->idx].push_back(isec); + } + } + return vec; + }; + + auto reduce = [](const T &x, const T &y) { + T ret(x.size()); + for (int i = 0; i < x.size(); i++) + ret[i] = x[i]; + for (int i = 0; i < x.size(); i++) + ret[i].insert(ret[i].end(), y[i].begin(), y[i].end()); + return ret; + }; + + std::vector> vec = + tbb::parallel_reduce(tbb::blocked_range(0, files.size()), + T(OutputSection::all_instances.size()), + fn, reduce); + + for (int i = 0; i < vec.size(); i++) + OutputSection::all_instances[i]->chunks = std::move(vec[i]); + +#else for (ObjectFile *file : files) { for (InputSection *isec : file->sections) { if (!isec) @@ -257,6 +292,7 @@ int main(int argc, char **argv) { osec->chunks.push_back(isec); } } +#endif } { diff --git a/mold.h b/mold.h index d504f3d2..c98d7edd 100644 --- a/mold.h +++ b/mold.h @@ -17,6 +17,7 @@ #include "tbb/concurrent_unordered_set.h" #include "tbb/concurrent_vector.h" #include "tbb/parallel_for_each.h" +#include "tbb/parallel_reduce.h" #include "tbb/parallel_sort.h" #include "tbb/partitioner.h" #include "tbb/task_group.h" @@ -113,13 +114,6 @@ static void for_each(T &arr, Callable callback) { // namespace tbb { -template<> -struct tbb_hash { - size_t operator()(const StringRef& k) const { - return llvm::hash_value(k); - } -}; - template<> struct tbb_hash_compare { static size_t hash(const StringRef& k) { @@ -293,6 +287,7 @@ public: this->name = name; shdr.sh_flags = flags; shdr.sh_type = type; + idx = all_instances.size(); all_instances.push_back(this); } @@ -311,6 +306,8 @@ public: } std::vector chunks; + uint32_t idx; + static std::vector all_instances; };