Files
carbon-lang/.pre-commit-config.yaml
T
Chandler Carruth 523e1c2972 Add a native Bazel build for libpfm and use it in benchmarks. (#3601)
The `libpfm` in the Bazel central repository uses `make` to build it,
which is difficult to integrate with our toolchain. Rather than try to
fix all the issues there, it's easy to just add a native Bazel build for
the library. I don't know that any of the relevant upstream folks are
interested in this kind of build, but it seems easy for us to maintain
as a Carbon project build configuration. I've also not tried to port all
of the different configurations as a consequence, and only 64-bit x86
and Arm as that seems the only likely architectures we'll care about in
the near term.

I've kept this using the `bzlmod` stuff as best I can, and I *think* I'm
holding all of those pieces correctly, but if not, happy for suggestions
on adjustments.

The `google_benchmark` package also has an awkward way of enabling
`libpfm` support using a top-level `bazel` command line flag. I think
this is because of how brittle the Bazel build of `libpfm` is, but I'm
not sure. With the new build, it seems easy to patch `google_benchmark`
to detect the same conditions as we build `libpfm` under, and enable it
there. So I've done this to avoid folks having to pass a command line
flag on platforms where it is supported.

The result is that we now get really nice CPU counter support in our
benchmarks out-of-the-box on Linux x86-64 and AArch64. For example on my
Fedora Asahi install on a Mac Mini I get:

```console
$ bazel run -c opt --copt=-gmlt //common:hashing_benchmark --run_under="taskset -c 4" -- --benchmark_counters_tabular=true --benchmark_perf_counters=CYCLES,INSTRUCTIONS
INFO: Invocation ID: 4aaeb9e9-7df5-4f1f-b56b-c03411790268
INFO: Analyzed target //common:hashing_benchmark (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //common:hashing_benchmark up-to-date:
  bazel-bin/common/hashing_benchmark
INFO: Elapsed time: 0.360s, Critical Path: 0.02s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Running command line: /bin/bash -c 'taskset -c 4 bazel-bin/common/hashing_benchmark '\''--benchmark_counters_tabular=true'\'' '\''--benchmark_perf_counters=CYCLES,INSTRUCTIONS'\'''
2024-01-15T00:10:50-08:00
Running /home/chandlerc/.cache/bazel/_bazel_chandlerc/b686aa8910e0845b88c21d715819b076/execroot/_main/bazel-out/aarch64-opt/bin/common/hashing_benchmark
Run on (8 X 2064 MHz CPU s)
CPU Caches:
  L1 Data 64 KiB (x8)
  L1 Instruction 128 KiB (x8)
  L2 Unified 4096 KiB (x2)
Load Average: 0.01, 0.08, 0.08
--------------------------------------------------------------------------------------------------------------------------------------------------------------
Benchmark                                                                           Time             CPU   Iterations     CYCLES INSTRUCTIONS bytes_per_second
--------------------------------------------------------------------------------------------------------------------------------------------------------------
BM_LatencyHash<RandValues<uint8_t>, CarbonHashBench>                             4.11 ns         4.11 ns    170200064    13.1321      9.00587      232.116Mi/s
BM_LatencyHash<RandValues<uint8_t>, AbseilHashBench>                             4.82 ns         4.82 ns    145643520    15.3657      12.0059      197.946Mi/s
BM_LatencyHash<RandValues<uint8_t>, LLVMHashBench>                               7.96 ns         7.95 ns     87956480    25.3737      17.0068      119.991Mi/s
BM_LatencyHash<RandValues<uint16_t>, CarbonHashBench>                            4.11 ns         4.11 ns    170365952    13.1247      9.00587      464.573Mi/s
BM_LatencyHash<RandValues<uint16_t>, AbseilHashBench>                            5.51 ns         5.51 ns    127568896    17.5578      14.0059      346.225Mi/s
BM_LatencyHash<RandValues<uint16_t>, LLVMHashBench>                              8.00 ns         7.99 ns     87085056     25.377      17.0068      238.834Mi/s
BM_LatencyHash<RandValues<std::pair<uint8_t, uint8_t>>, CarbonHashBench>         4.91 ns         4.90 ns    136013824    15.6456      14.0059      389.006Mi/s
BM_LatencyHash<RandValues<std::pair<uint8_t, uint8_t>>, AbseilHashBench>         6.85 ns         6.85 ns    102630400    21.8041      18.0059      278.637Mi/s
BM_LatencyHash<RandValues<std::pair<uint8_t, uint8_t>>, LLVMHashBench>           7.57 ns         7.56 ns     92798976    24.1437      20.0068      252.151Mi/s
BM_LatencyHash<RandValues<uint32_t>, CarbonHashBench>                            4.12 ns         4.12 ns    170229760    13.1272      9.00587      926.444Mi/s
BM_LatencyHash<RandValues<uint32_t>, AbseilHashBench>                            4.93 ns         4.92 ns    145304576    15.3738      12.0059      775.224Mi/s
BM_LatencyHash<RandValues<uint32_t>, LLVMHashBench>                              8.11 ns         8.10 ns     87127040     25.373      17.0068       470.98Mi/s
```
2024-01-17 03:39:01 +00:00

241 lines
7.7 KiB
YAML

# Part of the Carbon Language project, under the Apache License v2.0 with LLVM
# Exceptions. See /LICENSE for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
# Update versions with:
# pre-commit autoupdate --freeze && pre-commit run -a
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
default_language_version:
python: python3 # Defaults to python2, so override it.
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: c4a0b883114b00d8d76b479c820ce7950211c99b # frozen: v4.5.0
hooks:
- id: check-added-large-files
- id: check-case-conflict
- id: check-executables-have-shebangs
- id: check-merge-conflict
- id: check-symlinks
exclude: ^bazel-execroot$
- id: check-yaml
- id: detect-private-key
- id: end-of-file-fixer
exclude: '^(.*/fuzzer_corpus/.*|.*\.svg)$'
- id: mixed-line-ending
args: ['--fix=lf']
exclude: '^(.*/fuzzer_corpus/.*|.*\.svg)$'
- id: trailing-whitespace
exclude: '^(.*/fuzzer_corpus/.*|.*/testdata/.*\.golden|.*\.svg)$'
- repo: https://github.com/google/pre-commit-tool-hooks
rev: e006c8ab09f96ec32ba728b488ea5d17e1f8f6c0 # frozen: v1.2.4
hooks:
- id: check-google-doc-style
- id: markdown-toc
- repo: local
hooks:
- id: fix-cc-deps
name: Fix missing C++ deps
entry: scripts/fix_cc_deps.py
language: python
files: ^.*/(BUILD|[^/]+\.(h|cpp))$
pass_filenames: false
# Formatters should be run late so that they can re-format any prior changes.
- repo: https://github.com/psf/black
rev: 2a1c67e0b2f81df602ec1f6e7aeb030b9709dc7c # frozen: 23.11.0
hooks:
- id: black
- repo: https://github.com/pre-commit/mirrors-prettier
rev: ffb6a759a979008c0e6dff86e39f4745a2d9eac4 # frozen: v3.1.0
hooks:
- id: prettier
- repo: local
hooks:
- id: buildifier
name: Bazel buildifier
entry: scripts/run_buildifier.py
# Beyond just formatting, explicitly fix lint warnings.
args: ['--lint=fix', '--warnings=all', '-r', '.']
language: python
files: |
(?x)^(
.*BUILD.*|
.*MODULE.bazel.*|
.*WORKSPACE.*|
.*\.bzl
)$
- id: check-bazel-mod-deps
# Check this after buildifier because buildifier may modify inputs, and
# MODULE.bazel.lock includes line/column details.
name: Check bazel mod deps
entry: scripts/bazel_mod_deps.py
language: python
files: |
(?x)^(
.*MODULE.bazel.*|
.*WORKSPACE.*
)$
- id: clang-format
name: clang-format
entry: clang-format
types_or: [c++, proto]
language: python
args: ['-i']
additional_dependencies: ['clang-format==17.0.1']
- id: explorer-format-grammar
name: Format the explorer grammar file
entry: explorer/syntax/format_grammar.py
language: python
files: ^explorer/syntax/(lexer.lpp|parser.ypp)$
pass_filenames: false
additional_dependencies: ['clang-format==17.0.1']
- repo: local
hooks:
- id: check-header-guards
# This should run after clang-format, which may reformat a guard.
name: Check header guards
entry: scripts/check_header_guards.py
language: python
files: ^.*\.h$
- id: check-sha-filenames
# This may rename files, so it's deliberately between formatters and
# linters.
name: Check fuzzer SHA filenames
entry: scripts/check_sha_filenames.py
language: python
files: ^.*/fuzzer_corpus/.*$
- id: check-toolchain-diagnostics
name: Check toolchain diagnostics
entry: toolchain/diagnostics/check_diagnostics.py
language: python
files: |
(?x)^(
toolchain/.*\.cpp|
toolchain/.*\.h|
toolchain/diagnostics/check_diagnostics\.py|
toolchain/diagnostics/diagnostic_kind\.def
)$
pass_filenames: false
# Run linters last, as formatters and other checks may fix issues.
- repo: local
hooks:
- id: forbid-llvm-googletest
name: Checks for deps on LLVM's version of GoogleTest
entry: scripts/forbid_llvm_googletest.py
language: python
files: ^.*/BUILD$
pass_filenames: false
- repo: https://github.com/PyCQA/flake8
rev: 10f4af6dbcf93456ba7df762278ae61ba3120dc6 # frozen: 6.1.0
hooks:
- id: flake8
- repo: https://github.com/pre-commit/mirrors-mypy
rev: '4daa14b20c0f48f472528c2b5f5bca28a18a7ce0' # frozen: v1.7.1
hooks:
- id: mypy
# Use setup.cfg to match the command line.
args:
- --config-file=setup.cfg
# This should match the requirements added in the WORKSPACE pip_install.
additional_dependencies:
- gql >= 2.0.0, < 3.0.0
- PyGitHub
# Exclusions are:
# - p#### scripts because they're not tested or maintained.
# - lit.cfg.py because it has multiple copies, breaking mypy.
# - Unit tests because they sometimes violate typing, such as by
# assigning a mock to a function.
exclude: |
(?x)^(
proposals/(?!scripts/).*|
.*/lit\.cfg\.py|
.*_test\.py
)$
- repo: https://github.com/codespell-project/codespell
rev: 6e41aba91fb32e9feb741a6258eefeb9c6e4a482 # frozen: v2.2.6
hooks:
- id: codespell
args: ['-I', '.codespell_ignore', '--uri-ignore-words-list', '*']
# Test data may contain intentional misspellings, as well as short,
# meaningless identifiers that codespell incorrectly identifies as
# typos but that we would want to detect in other contexts.
exclude: |
(?x)^(
.*/testdata/.*|
.*/fuzzer_corpus/.*
)$
- repo: https://github.com/google/pre-commit-tool-hooks
rev: e006c8ab09f96ec32ba728b488ea5d17e1f8f6c0 # frozen: v1.2.4
hooks:
- id: check-copyright
args:
- --copyright
- |+
Part of the Carbon Language project, under the Apache License v2.0 with LLVM
Exceptions. See /LICENSE for license information.
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- --custom_format
- '\.(carbon|proto|ypp)$'
- ''
- '// '
- ''
- --custom_format
- '\.(l|lpp|y)$'
- '/*'
- ''
- '*/'
- --custom_format
- '\.(plist)$'
- '<!--'
- ''
- '\-->'
- --custom_format
- '\.vim$'
- ''
- '" '
- ''
- --custom_format
- '\.scm$'
- ''
- '; '
- ''
- --custom_format
- '\.lua$'
- ''
- '-- '
- ''
- --custom_format
- '\.c$'
- ''
- '// '
- ''
exclude: |
(?x)^(
.bazelversion|
.github/pull_request_template.md|
compile_flags.txt|
github_tools/requirements.txt|
third_party/.*|
.*\.def|
.*\.svg|
.*/fuzzer_corpus/.*|
.*/testdata/.*\.golden
)$
- id: check-links
# This excludes third-party code, and patches to third-party code.
exclude: |
(?x)^(
MODULE.bazel.lock|
bazel/bazel_clang_tidy/.*\.patch|
bazel/google_benchmark/.*\.patch|
bazel/libpfm/.*\.patch|
bazel/llvm_project/.*\.patch|
third_party/examples/.*/carbon/.*|
)$