[PATCH] D47024: [MathExtras] Add alignToPowerOf2

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri May 18 08:42:58 PDT 2018


MaskRay added a comment.

https://github.com/llvm-mirror/lld/tree/master/ELF/SyntheticSections.cpp#L2517

I found this when I was reading how SHF_MERGE sections are merged. Why does the code claim that power of 2 makes the parallelism more efficient?

    // Concurrency level. Must be a power of 2 to avoid expensive modulo
    // operations in the following tight loop.
    size_t Concurrency = 1;
    if (ThreadsEnabled)
      Concurrency =
          std::min<size_t>(PowerOf2Floor(hardware_concurrency()), NumShards);
  
    // Add section pieces to the builders.
    parallelForEachN(0, Concurrency, [&](size_t ThreadId) {
      for (MergeInputSection *Sec : Sections) {
        for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I) {
          size_t ShardId = getShardId(Sec->Pieces[I].Hash);
  ///////////////////////////////////// does this bitwise 
          if ((ShardId & (Concurrency - 1)) == ThreadId && Sec->Pieces[I].Live)
            Sec->Pieces[I].OutputOff = Shards[ShardId].add(Sec->getData(I));
        }
      }
    });


Repository:
  rL LLVM

https://reviews.llvm.org/D47024





More information about the llvm-commits mailing list