Skip to content

perf(input): compile sources in parallel, each in its directory - #272

Draft
SizzleUnrlsd wants to merge 17 commits into
mainfrom
perf/parallel-compilation
Draft

SizzleUnrlsd wants to merge 17 commits into
mainfrom
perf/parallel-compilation

Conversation

@SizzleUnrlsd

@SizzleUnrlsd SizzleUnrlsd commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Every compilation held gCompileInvokeMutex and moved the whole process to its compile command's directory, so the sources of a run compiled one at a time (#271). coretrace-compiler gives each compilation the directory of its -working-directory without moving the process (CoreTrace/coretrace-compiler#262, released in v0.13.0). The analyzer passes each compile command's directory that way, compilations share the lock, and the inputs of a multi-file run load at once, on a bounded number of workers.

Closes #271

What --jobs does

--jobs Inputs loading at once Other phases
not given, or auto 2, or 1 on a machine of one processor every processor, as on main
N min(N, 4) N, as on main

On main the inputs load one at a time, whatever --jobs (#285). Each compilation in progress holds its own memory, which is what the bound limits. A run of one input is not concerned.

Change

A compile command whose directory does not exist still fails the analysis, with the message it gave before. coretrace-compiler reports such a directory, but compiles to memory all the same (CoreTrace/coretrace-compiler#264), so the analyzer checks the directory itself before compiling.

Measurements

On Linux

Measured on 2026-11-21 on a GitHub runner (run 44956551908): ubuntu-24.04 on x86-64, 4 CPUs, g++ 13.3, LLVM 20, glibc 2.39, each run in a cgroup of 4 CPUs and 6 GiB without swap. --format=json --timing, no IR cache, an empty resource summary cache. Two builds, both with coretrace-compiler v0.13.0:

  • main: main (588e800) with the compiler moved to v0.13.0, and nothing else;
  • this loading: main merged with this branch at bbaab08, the inputs loading on min(--jobs, 4) workers, the freed memory given back after loading.

This head differs from the second build by the bound without --jobs (2 workers, where that build took 4), by the tests and by the documentation. Every run below gives --jobs.

Medians of 3 runs; the builds alternate from one repetition to the next, and each passed the tests before the measures. The percentages compare those medians.

--jobs This repository's 53 C++ sources main this loading
1 Whole run 104.5 s 105.4 s +0.9 %
Loading 94.9 s 96.0 s
Phases after loading 9.5 s 9.4 s
Peak resident memory 1023 MiB 1023 MiB
Anonymous memory once loaded 714 MiB 627 MiB −12 %
2 Whole run 103.6 s 58.1 s −44 %
Loading 94.6 s 49.2 s −48 %
Phases after loading 9.0 s 8.8 s
Peak resident memory 1023 MiB 1186 MiB +16 %
Anonymous memory once loaded 747 MiB 646 MiB −14 %
4 Whole run 104.1 s 47.5 s −54 %
Loading 95.1 s 38.8 s −59 %
Phases after loading 9.0 s 8.7 s
Peak resident memory 1023 MiB 1242 MiB +21 %
Anonymous memory once loaded 748 MiB 641 MiB −14 %
  • At one job the two ranges of the whole run overlap: 103.0 to 108.1 s on main, 103.9 to 105.6 s with this loading.
  • Peak memory at 4 jobs went from 1212 to 1300 MiB over the three runs.
  • Lua 5.4.6, 34 C files: the whole run takes 7.10 s on main and 7.13 s with this loading at one job, 7.24 s and 6.72 s at 2 jobs, 7.14 s and 6.80 s at 4 jobs. Loading takes 0.76 s on main, and 0.76 s, 0.45 s and 0.37 s with this loading. Peak memory is 175 MiB on main and 170 to 171 MiB with this loading.
  • Reports: the same diagnostics and functions with both builds, at each job count, for each set of sources. They are compared without the order of the diagnostics, which varies from run to run on Lua's.

Not measured:

  • A run without --jobs at this head. It loads on 2 workers and runs the other phases on every processor, where the runs at 2 jobs above ran both on 2.
  • More than 4 jobs, and macOS since the rework: the macOS measure below predates the bound and the release of freed memory.
  • This branch against main as it is, with coretrace-compiler v0.10.0. Both builds above have the update. Another run, on a runner of the same model (run 44387933701), measured the update alone at one job, with main's code: the whole run takes 1.4 % more time on the 53 sources and 9 % more on Lua's (6.44 s to 7.01 s), where the phases after loading take all of it. The cause is not known. On Lua's sources, this loading saves 0.3 to 0.5 s at 2 and 4 jobs, less than the 0.6 s the update costs in that other run.

On macOS, before the rework

Measured on 2026-11-12, and on 2026-11-15 for 4 jobs: this branch as it was then, the inputs loading on --jobs workers without a bound, which is this head's loading at 2 and 4 jobs. macOS arm64 (M3, 8 cores), LLVM 20.1.2, --format=json --timing, no IR cache, an empty resource summary cache. Both builds use coretrace-compiler from source: main (f0a22d9) for db7fb25, CoreTrace/coretrace-compiler#262 for this branch. Medians of 5 runs, 3 for the 53 sources; the builds alternate within each repetition.

Sources --jobs db7fb25 this branch Loading: db7fb25, this branch
Lua 5.4.6, 34 C files 1 7.70 s 7.71 s 1.14 s, 1.17 s
2 7.68 s 7.51 s 1.12 s, 0.90 s
8 C++ files, each including 11 standard headers 1 4.27 s 4.07 s 3.81 s, 3.68 s
2 4.39 s 2.61 s 4.03 s, 2.22 s
This repository's 53 C++ sources 1 55.0 s 55.1 s 44.1 s, 43.9 s
2 52.3 s 34.1 s 42.2 s, 23.7 s
4 53.0 s 25.3 s 43.2 s, 15.2 s
  • The --jobs=4 row was measured apart, on the 53 sources only: this branch at e6ae53e, with coretrace-compiler fetched at the pinned commit, against db7fb25 built as above.
  • Reports: the same diagnostics and functions with both builds, at every job count measured. The order of Lua's diagnostics, and so their ids, varies from run to run with either build.
  • Peak memory grew with the compilations in flight: at --jobs=2, 239 MB to 328 MB for the 8 C++ files, 1.26 GB to 1.42 GB for the 53 sources; at --jobs=4, 1.35 GB to 1.62 GB for the 53 sources. The memory freed by the loading was not given back in that build.

Tests

Two functional checks are added to run_test.py. Each writes twelve C files that call one another in a chain, each reading a global of its own before any write, and runs the analyzer on them directly, without the runner's extra arguments.

  • check_loading_jobs: the files are loaded without a cache, with --timing. An input logs Compiling when its load starts and Bitcode parse done in once its module is there; the check reads the log line after line and keeps the most inputs between the two at once. It expects 2 without --jobs and with --jobs=auto, on a machine of two processors or more, min(N, 4) with --jobs=N for 1, 2, 3, 4 and 8, and 4 at --jobs=8 with --no-resource-cross-tu and --no-uninitialized-cross-tu.
  • check_reports_of_an_empty_and_a_filled_compile_ir_cache: the files are analyzed at --jobs=1 without a cache, then, without --jobs and with --jobs=4, from an empty compile IR cache and again from the cache that run filled. The four runs must give the diagnostics and the functions of the first one, compared without the diagnostics' ids, which follow their place in the report. The runs on a filled cache must log Compilation cache hit for the twelve inputs, the runs on an empty one for none. This check also passes with main's loading: it guards the loads that fill the cache together.

No build was made locally for this rework: the CI below is its validation. Before the push:

  • The counting of the first check, on the logs kept from the Linux measure: main has 1 input loading at once at 2 and at 4 jobs, on the 53 sources and on Lua's; the loading measured has 1, 2 and 4 at 1, 2 and 4 jobs, in each of its runs. The check was not run on a build of main: that it fails there is read from those logs.
  • Both checks against a stand-in for the analyzer, a script that writes the same log lines and a report: they pass with this pull request's bounds, and fail with one load at a time, with --jobs loads at once, with another report from the filled cache, and with an input missing from the cache.
  • scripts/format-check.sh with clang-format 20, scripts/check-license-compliance.sh and the commit checker pass.

The unit test of the review fix, testLoadLeavesTheProcessDirectory, is unchanged.

CI at 3445f89

  • Build run 45506007623: the three jobs pass, on ubuntu-24.04, macos-15 and macos-latest.
    • unit tests: both programs run in each job, and end with All analyzer module unit tests passed. and All ownership engine unit tests passed.;
    • functional tests: 2918/2918 in each job, the two new checks among them.
  • check_loading_jobs, in each of the three jobs: 2 inputs loading at once without --jobs and with --jobs=auto; 1, 2, 3 and 4 at --jobs=1, 2, 3 and 4; 4 at --jobs=8, with the cross-TU summaries and without them. The runners have 4 processors on ubuntu-24.04 and 3 on macOS.
  • check_reports_of_an_empty_and_a_filled_compile_ir_cache, in each of the three jobs: without --jobs and with --jobs=4, the run on an empty cache and the run on the cache it filled give the report of the run without a cache, and read 0 and 12 of the 12 inputs from the cache.
  • Other checks: the Linux binaries (amd64 and arm64), the Docker image, the GitHub Action, the consumer project, the concurrency analysis, format-check, license-compliance and the commit conventions pass.

For the review

  • scripts/ci/run_code_analysis.py is not changed. It splits the inputs into one chunk per processor and runs one analyzer per chunk at once, without --jobs unless its caller passes one. Each of those analyzers loaded one input at a time on main and loads two at once here: the script's run holds up to twice as many compilations in progress. It is the entry point of Dockerfile.ci and the self-analysis step of ci.yml. That step ran 4 analyzers at once on 55 files and took 92 s in the run above; it took 55 to 107 s in recent runs of main and of four other branches, so this run does not tell the two loadings apart.
  • No ThreadSanitizer run covers the compilations in progress at once. The checks are the test suite on the three systems of the CI, the concurrency analysis and the measured runs.
  • The bound is set where a multi-file run loads all its inputs before the analysis (analyzeWithSharedModuleLoading). Every run of more than one input goes there today, as its global read-before-write summaries need all the modules. The other path, analyzeWithoutSharedModuleLoading, still takes --jobs workers that each load and analyze an input, and is reached with one input only. The last case of check_loading_jobs fails the day a multi-file run takes it.
  • check_loading_jobs needs the loads to overlap to see the bound reached: twelve inputs that each include three standard headers. On the logs of the Linux measure, the 34 files of Lua reached 2 and 4 at once in each run.
  • The release of freed memory has no test of its own. Its effect is the anonymous memory once loaded in the table above.

…rectory

The pinned commit, from CoreTrace/coretrace-compiler#262, gives each
compilation a file system of its own, whose working directory
-working-directory moves while the process's stays. The analyzer does
not use it yet: it still moves the process around each compilation,
under its lock.

Refs #271
Every compilation held one global lock and moved the whole process to
its compile command's directory: the --jobs threads compiled one
source at a time. coretrace-compiler now gives each compilation the
directory of its -working-directory without moving the process: the
analyzer passes the directory that way, and compilations share the
lock.

Some compilations still hold it alone, since they change state that
the whole process shares: those with an option that Clang applies to
LLVM's state (-mllvm, -mdebug-pass, -mlimit-float-precision,
-ftime-report), and those with -fno-integrated-cc1, whose jobs run as
programs, for which coretrace-compiler moves the process.

A compile command whose directory does not exist still fails the
analysis: coretrace-compiler reports a directory it cannot enter, but
compiles to memory all the same, so the analyzer checks the directory
itself, with the message it gave before.

Refs #271
@SizzleUnrlsd SizzleUnrlsd added the enhancement New feature or request label Nov 12, 2026
@SizzleUnrlsd SizzleUnrlsd self-assigned this Nov 12, 2026
The newer commit of CoreTrace/coretrace-compiler#262 also writes the
fragment of -MJ, the statistics of -save-stats, the graph of
-dependency-dot and the module cache in the directory of
-working-directory, as the analyzer did when it moved the process
there.

Refs #271
With -fno-integrated-cc1, the jobs of a compile command run as
programs: coretrace-compiler moves the process to the command's
directory and leaves it there. The scoped change of directory that
this branch removed used to bring the process back. After a load of
such a source, the process stayed in the command's directory, where a
relative source without a compile command was no longer found. A
compilation that holds the lock alone now puts the process back before
it releases the lock. The compilations that share the lock do not move
the process, and still run at once.

Refs #271
A source whose compile command passes -fno-integrated-cc1 is loaded,
then a relative source without a compile command. Before the fix, the
process was left in the command's directory and the second load
failed.

Refs #271
SizzleUnrlsd added a commit that referenced this pull request Nov 17, 2026
The table of each phase was kept for the first repetition only: the
phases after loading can now be compared over every repetition.

Refs #272
SizzleUnrlsd added a commit that referenced this pull request Nov 17, 2026
In the first measure, the phases after loading took longer in the rework
than on main, at --jobs=1 as well. The rework holds #274: a fourth
analyzer, the head of #274, tells its effect from that of the rework.

Refs #272
SizzleUnrlsd added a commit that referenced this pull request Nov 21, 2026
The third run told the compiler's update from #274 at one job. This one
tells whether the rework of #272 needs #274: three analyzers that build
coretrace-compiler v0.13.0, measured in turn at 1, 2 and 4 jobs.

- main: main with the compiler of #274 and nothing else of it.
- 272: main merged with #272, with the rework's loading on at most four
  workers and its release of freed memory. The bitcode is read back.
- 272-274: the same with #274 merged too.

The three trees are laid out on the runner without a commit, and compared
with the ones computed before the run.

Refs #272
README.md had the one conflict, on the sentence that describes --jobs: main's
stays (#285). After this merge the inputs still load one at a time, as on
main, since src/app/AnalyzerApp.cpp is main's.
The branch pinned a commit of CoreTrace/coretrace-compiler#262 while that
pull request was open. v0.13.0 (e213053) is the release that holds it.

Refs #271
…--jobs=N

Since #285 the inputs of a multi-file run load one after the other: their
compilations ran under one lock, so a loading worker only parsed its
bitcode in parallel. On this branch the compilations share that lock, and
the loading workers compile at once.

Each compilation in progress holds its own memory, so the loading takes
fewer workers than --jobs allows: two when the count is left to the
machine (the default, or --jobs=auto), and min(N, 4) with --jobs=N. The
other phases use --jobs as before.

Refs #271
Under glibc, the memory a compilation frees stays in the arena of the
thread that ran it, for the rest of the run (#285). Once the inputs are
loaded, malloc_trim gives it back. The call is compiled under glibc only.

On the Linux measure of this loading (run 44956551908, this repository's
53 C++ sources, medians of 3 runs), the analyzer holds 627 MiB of
anonymous memory after loading at one job, where the loading is otherwise
main's, against 714 MiB for main with the same compiler; 646 and 641 MiB
at 2 and 4 jobs, against 747 and 748 MiB.

Refs #271
Twelve C files are loaded without a cache, with --timing. The check counts
the inputs whose load has started and is not done, line after line of the
log: two at once without --jobs and with --jobs=auto, on a machine of two
processors or more, and min(N, 4) with --jobs=N, for 1, 2, 3, 4 and 8.

Refs #271
…cache

Twelve C files are analyzed one at a time without a cache, then at once
from an empty compile IR cache, which their loads fill together, then from
that cache, without --jobs and with --jobs=4. The four runs must give the
diagnostics and the functions of the first, the runs on the filled cache
must read every input from it, and the others none.

Refs #271
The loading is bounded where a multi-file run loads all its inputs before
the analysis, which every multi-file run does today. The check adds a run
with --no-resource-cross-tu and --no-uninitialized-cross-tu at --jobs=8,
to fail the day such a run loads its inputs another way.

Refs #271
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sources compile one at a time, whatever --jobs

1 participant