Files
carbon-lang/bazel/libpfm/0001-Introduce-a-simple-native-Bazel-build.patch
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

134 lines
4.1 KiB
Diff

From 04fb28b5673d29a8c38519845c87f4c00c76e9cf Mon Sep 17 00:00:00 2001
From: Chandler Carruth <chandlerc@gmail.com>
Date: Sat, 13 Jan 2024 02:15:19 -0800
Subject: [PATCH] Introduce a simple native Bazel build.
---
BUILD.bazel | 84 +++++++++++++++++++++++++++++++++++++++++++++++++
MODULE.bazel | 10 ++++++
WORKSPACE.bazel | 5 +++
3 files changed, 99 insertions(+)
create mode 100644 BUILD.bazel
create mode 100644 MODULE.bazel
create mode 100644 WORKSPACE.bazel
diff --git a/BUILD.bazel b/BUILD.bazel
new file mode 100644
index 0000000..427c854
--- /dev/null
+++ b/BUILD.bazel
@@ -0,0 +1,84 @@
+# 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
+
+load("@rules_cc//cc:defs.bzl", "cc_library")
+
+package(default_visibility = ["//visibility:public"])
+
+aarch64_srcs = [
+ "lib/pfmlib_arm_perf_event.c",
+ "lib/pfmlib_arm.c",
+ "lib/pfmlib_arm_armv8.c",
+ "lib/pfmlib_arm_armv9.c",
+ "lib/pfmlib_tx2_unc_perf_event.c",
+ "lib/pfmlib_kunpeng_unc_perf_event.c",
+ "lib/pfmlib_arm_priv.h",
+ "lib/events/arm_cortex_a57_events.h",
+ "lib/events/arm_cortex_a53_events.h",
+ "lib/events/arm_xgene_events.h",
+ "lib/events/arm_cavium_tx2_events.h",
+ "lib/events/arm_marvell_tx2_unc_events.h",
+ "lib/events/arm_fujitsu_a64fx_events.h",
+ "lib/events/arm_neoverse_n1_events.h",
+ "lib/events/arm_neoverse_n2_events.h",
+ "lib/events/arm_neoverse_v1_events.h",
+ "lib/events/arm_neoverse_v2_events.h",
+ "lib/events/arm_hisilicon_kunpeng_events.h",
+ "lib/events/arm_hisilicon_kunpeng_unc_events.h",
+]
+
+x86_64_srcs = [
+ "lib/pfmlib_amd64_priv.h",
+] + glob(
+ [
+ "lib/pfmlib_amd64*.c",
+ "lib/pfmlib_intel*.c",
+ "lib/pfmlib_intel*_priv.h",
+ "lib/events/amd64_events_*.h",
+ "lib/events/intel_*_events.h",
+ ],
+ exclude = [
+ # 32-bit CPUs
+ "lib/pfmlib_intel_coreduo.c",
+ "lib/pfmlib_intel_p6.c",
+ ],
+)
+
+cc_library(
+ name = "libpfm",
+ srcs = [
+ "lib/events/perf_events.h",
+ "lib/pfmlib_common.c",
+ "lib/pfmlib_perf_event.c",
+ "lib/pfmlib_perf_event_pmu.c",
+ "lib/pfmlib_perf_event_priv.h",
+ "lib/pfmlib_perf_event_raw.c",
+ "lib/pfmlib_priv.h",
+ ] + select({
+ "@platforms//cpu:aarch64": aarch64_srcs,
+ "@platforms//cpu:x86_64": x86_64_srcs,
+ }),
+ hdrs = glob(["include/perfmon/*.h"]),
+ copts = [
+ "-DHAS_OPENAT",
+ "-D_REENTRANT",
+ "-I.",
+ "-fvisibility=hidden",
+ ] + select({
+ "@platforms//cpu:x86_64": [
+ "-DCONFIG_PFMLIB_ARCH_X86",
+ "-DCONFIG_PFMLIB_ARCH_X86_64",
+ ],
+ "//conditions:default": [],
+ }),
+ strip_include_prefix = "include",
+ target_compatible_with = select({
+ # This library only makes sense on Linux, and we only include support
+ # for building on AArch64 and x86-64. Other CPUs can be added to this
+ # list if build support is added for them.
+ "@platforms//cpu:aarch64": ["@platforms//os:linux"],
+ "@platforms//cpu:x86_64": ["@platforms//os:linux"],
+ "//conditions:default": ["@platforms//:incompatible"],
+ }),
+)
diff --git a/MODULE.bazel b/MODULE.bazel
new file mode 100644
index 0000000..c901cbe
--- /dev/null
+++ b/MODULE.bazel
@@ -0,0 +1,10 @@
+# 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
+
+"""Bazel modules."""
+
+module(name = "libpfm")
+
+bazel_dep(name = "rules_cc", version = "0.0.9")
+bazel_dep(name = "platforms", version = "0.0.8")
diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel
new file mode 100644
index 0000000..9aad57c
--- /dev/null
+++ b/WORKSPACE.bazel
@@ -0,0 +1,5 @@
+# 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
+
+# See `MODULE.bazel` for details.
--
2.43.0