Globally Optimized Build Parallelism?

This is a post about swiftc and build system performance characteristics.

Are there any resources on how to manage and optimize available hardware, where there's competing parallelism – in the build system (parallel build targets) and in swiftc (parallel frontend jobs)? In particular, I'm currently interested in developer builds, where -enable-batch-mode is being used.

Two high level questions I have are:

  1. How can available cores be allocated across both parallel build actions and swift frontend jobs – in order to achieve optimal total build times?
  2. Possibly as a subquestion, how can build times of individual modules be optimized by identifying the optimal amount of batching for that module/source files?

I'm just starting to investigate, but here's a list of things that have surprised me:

  1. Xcode appears to always use -j8 (on my machine), even when building multiple targets in parallel, which seems like it's not doing any global allocation of cpu
  2. Possibly due lack of global parallelism, this bug report shows builds can be faster by using less swiftc parallelism (smaller -j)
  3. Conversely, some targets in my work's project build faster by applying -j8 where it was not previously being used
  4. With Xcode always passing -j8 on my machine, that means modules of 8 (or less) files are not actually being batched, there are 8 frontends, same jobs that are used in single file mode
  5. There's a long interesting comment in Complation.cpp that reasons about the batch size calculation, but it's unclear how valid that remains today, and also how to recalculate the numbers specific to your own build
  6. I've always thought of -j as being used to add/increase parallelism, but with swiftc it's also an upper limit on parallelism, and so for modules with a lot of files, using -j8 can actually decrease parallelism (by increasing batch size)

Is this a solved problem? Is this a hard problem? Does anyone have relevant docs/blogs/resources/papers about this?