let it be so

This commit is contained in:
Motiejus Jakštys 2022-10-19 11:35:55 +03:00
commit 64a3ef606e
8 changed files with 171 additions and 0 deletions

2
.bazelrc Normal file
View File

@ -0,0 +1,2 @@
build --incompatible_enable_cc_toolchain_resolution
build --action_env BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1

45
.envrc Normal file
View File

@ -0,0 +1,45 @@
set -eu
BIN_DIR="$(git rev-parse --show-toplevel)/bin"
export PATH="$BIN_DIR:$PATH"
_u_bzl=https://github.com/bazelbuild/bazelisk/releases/download/v1.12.0/bazelisk-
_u_bldf=https://github.com/bazelbuild/buildtools/releases/download/5.1.0/buildifier-
if [[ "${PRINT_TOOL_HASHES:-no}" = "yes" ]]; then
for os in linux darwin; do
for arch in amd64 arm64; do
hash_bzl=$(direnv fetchurl "${_u_bzl}$os-$arch")
hash_bldf=$(direnv fetchurl "${_u_bldf}$os-$arch")
echo -e "bzl: $os-$arch\t$hash_bzl"
echo -e "bldf: $os-$arch\t$hash_bldf"
done
done
fi
# to fetch the hashes, run:
# $ PRINT_TOOL_HASHES=yes bash .envrc
case "$(uname | tr A-Z a-z)-$(uname -m)" in
linux-x86_64)
bzl=$(direnv fetchurl "${_u_bzl}linux-amd64" sha256-awvLLqFbyhb/+r5v2nWANEA3U1TAhUgP42HSy/MlAds=)
bldf=$(direnv fetchurl "${_u_bldf}linux-amd64" sha256-Ur9rECy0+IRk4ZfKrAbWl5P6KwX1rVCn579vvWVmSKM=)
;;
linux-aarch64)
bzl=$(direnv fetchurl "${_u_bzl}linux-arm64" sha256-KdhhykjfJKPo3sV/sAUIumZKMZIQR7JobDjPmiDUY58=)
bldf=$(direnv fetchurl "${_u_bldf}linux-arm64" sha256-kX1ZnbsEDmOuen4a23ENIFeBGQL9yeNczpJev9lm7rg=)
;;
darwin-x86_64)
bzl=$(direnv fetchurl "${_u_bzl}darwin-amd64" sha256-cM9/50gI0WQY03H+uMzU58RCFdsD0sT/x1t2e3ZUCfs=)
bldf=$(direnv fetchurl "${_u_bldf}darwin-amd64" sha256-yTeNn0KT/DjsVKCPvHTnqdKJFNrmiRM0QB5Z849uZdw=)
;;
darwin-arm64)
bzl=$(direnv fetchurl "${_u_bzl}darwin-arm64" sha256-NFu4uQDWue90I06enkE67Tj7Ke8lXkrhisYb9KYQLYQ=)
bldf=$(direnv fetchurl "${_u_bldf}darwin-arm64" sha256-dF/rXqlstv85p2soIcV1kf1wtSgyVWJIbUe10IkA4uQ=)
;;
*)
>&2 echo "unsupported architecture tuple $(uname | tr A-Z a-z)-$(uname -m)"
exit 1;;
esac
ln -sf "${bzl}" "$BIN_DIR/bazel"
ln -sf "${bldf}" "$BIN_DIR/buildifier"

12
.gitignore vendored Normal file
View File

@ -0,0 +1,12 @@
*~
*.sw[op]
/bin/bazel
/bin/buildifier
/bin/bazelisk-*
/bazel-test-zigcc
/bazel-bin
/bazel-out
/bazel-testlogs
/bazel-x

9
BUILD Normal file
View File

@ -0,0 +1,9 @@
NTESTS=96
[
cc_binary(
name = "zigzag-{}".format(i),
srcs = ["main.c"],
copts = ["-DZIGZAG={}".format(i)],
) for i in range(NTESTS)
]

35
README.md Normal file
View File

@ -0,0 +1,35 @@
bazel-zig-cc and llvm
---------------------
bazel-zig-cc has a performance issue when compiling many files. This repository
reproduces that.
Steps to reproduce
------------------
llvm14: 10-11 seconds to compile 96 binaries on an 8-core machine:
```
$ bazel clean; bazel shutdown; bazel build --extra_toolchains=@llvm_toolchain//:cc-toolchain-x86_64-linux ...
INFO: Starting clean (this may take a while). Consider using --async if the clean takes more than several minutes.
Starting local Bazel server and connecting to it...
INFO: Analyzed 96 targets (41 packages loaded, 1458 targets configured).
INFO: Found 96 targets...
INFO: Elapsed time: 10.725s, Critical Path: 0.66s
INFO: 481 processes: 289 internal, 192 linux-sandbox.
INFO: Build completed successfully, 481 total actions
```
zig cc: 3.5 minutes. Anecdotally when looking at `htop` looks like it does not
parallelize at all:
```
$ bazel clean; bazel shutdown; bazel build --platforms=@zig_sdk//libc_aware/platform:linux_amd64_gnu.2.28 ...
INFO: Starting clean (this may take a while). Consider using --async if the clean takes more than several minutes.
Starting local Bazel server and connecting to it...
INFO: Analyzed 96 targets (41 packages loaded, 14394 targets configured).
INFO: Found 96 targets...
INFO: Elapsed time: 214.333s, Critical Path: 18.31s
INFO: 385 processes: 193 internal, 192 linux-sandbox.
INFO: Build completed successfully, 385 total actions
```

57
WORKSPACE Normal file
View File

@ -0,0 +1,57 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
## LLVM-14
BAZEL_TOOLCHAIN_TAG = "0.7.2"
BAZEL_TOOLCHAIN_SHA = "f7aa8e59c9d3cafde6edb372d9bd25fb4ee7293ab20b916d867cd0baaa642529"
http_archive(
name = "com_grail_bazel_toolchain",
sha256 = BAZEL_TOOLCHAIN_SHA,
strip_prefix = "bazel-toolchain-{tag}".format(tag = BAZEL_TOOLCHAIN_TAG),
canonical_id = BAZEL_TOOLCHAIN_TAG,
url = "https://github.com/grailbio/bazel-toolchain/archive/{tag}.tar.gz".format(tag = BAZEL_TOOLCHAIN_TAG),
)
load("@com_grail_bazel_toolchain//toolchain:deps.bzl", "bazel_toolchain_dependencies")
bazel_toolchain_dependencies()
load("@com_grail_bazel_toolchain//toolchain:rules.bzl", "llvm_toolchain")
llvm_toolchain(
name = "llvm_toolchain",
llvm_version = "14.0.0",
)
load("@llvm_toolchain//:toolchains.bzl", "llvm_register_toolchains")
#llvm_register_toolchains()
### ZIG
BAZEL_ZIG_CC_VERSION = "ae2746ebebc4e8d5c9d9638f0d1f858708002681"
http_archive(
name = "bazel-zig-cc",
sha256 = "0f50bb8e130429ccae3017ba3274dd276ac744474b426f72df86a4a4a8c66ca0",
strip_prefix = "bazel-zig-cc-{}".format(BAZEL_ZIG_CC_VERSION),
urls = [
"https://git.sr.ht/~motiejus/bazel-zig-cc/archive/{}.tar.gz".format(BAZEL_ZIG_CC_VERSION),
],
)
load("@bazel-zig-cc//toolchain:defs.bzl", zig_toolchains = "toolchains")
zig_toolchains()
register_toolchains(
# amd64 toolchains for libc-aware platforms:
"@zig_sdk//libc_aware/toolchain:linux_amd64_gnu.2.19",
"@zig_sdk//libc_aware/toolchain:linux_amd64_gnu.2.28",
"@zig_sdk//libc_aware/toolchain:linux_amd64_gnu.2.31",
"@zig_sdk//libc_aware/toolchain:linux_amd64_musl",
# arm64 toolchains for libc-aware platforms:
"@zig_sdk//libc_aware/toolchain:linux_arm64_gnu.2.28",
"@zig_sdk//libc_aware/toolchain:linux_arm64_musl",
)

0
bin/.gitkeep Normal file
View File

11
main.c Normal file
View File

@ -0,0 +1,11 @@
#include <stdio.h>
#include <features.h>
int main() {
#ifdef __GLIBC__
printf("glibc_%d.%d\n", __GLIBC__, __GLIBC_MINOR__);
#else
printf("non-glibc\n");
#endif
return 0;
}