Subject: development tracks of bazel-zig-cc v0.9

From: Motiejus Jakštys <[email-masked]>
Date: Tue, 12 Jul 2022 10:19:27 +0300

Hi all,

I will occasionally share some longer-term development updates. This is
one of those. Now we have a couple of ongoing threads:
- linker robustness
- performance issues when using zig-cc.

Linker robustness
-----------------

One of the biggest source of work when adopting zig-cc for our compiler
needs was it's lack of support for various linker arguments. When
compiling a program, you would often see something like this:

    warning: unsupported linker arg: --sort-section
    warning: unsupported linker arg: alignment
    warning: unsupported linker arg: --exclude-libs
    warning: unsupported linker arg: ALL

Zig ignoring the flags was the right thing to do at the beginning, but
this tends to break software in subtle ways during runtime. So I
proposed failing hard on unsupported flags[1] and implemented (some
still under review) the ones that I found in the wild[2]. Please +1.

Performance issues
------------------

We have found non-deterministic performance degradations when building
lots of code using bazel-zig-cc on a 96-core server. Usually our test
shard takes 5-10 minutes to build, but with bazel-zig-cc some time out
after 30 minutes. More about the CI infra here[4].

So far we found:
- zig was translating -Og to -O1, now fixed[5].
- -fno-sanitize=undefined can decrease the build time by 30-50% for
  non-optimized binaries.
- -g0 (remove debug symbols) can reduce the build time by 30-50%.

With these changes the baseline "zig cc" performance is similar to
clang/gcc, but some shards still time out. We currently suspect there is
some contention in zig's caching layer; the track is active, stay tuned.

To be clear, we are not planning to remove the safety/debug features in
bazel-zig-cc for the sake of speed; this is just data we've gathered so
far.

Regards,
Motiejus

[1]: https://github.com/ziglang/zig/pull/11906
[2]: https://github.com/ziglang/zig/pulls?q=is%3Apr+author%3Amotiejus+
[3]: https://github.com/ziglang/zig/pull/11863
[4]: https://eng.uber.com/how-we-halved-go-monorepo-ci-build-time/
[5]: https://github.com/ziglang/zig/pull/11949