[llvm] [Exegesis][RISCV] Add RISCV support for llvm-exegesis (PR #89047)
Aiden Grossman via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 18 02:21:12 PDT 2024
================
@@ -0,0 +1,66 @@
+//===-- RISCVCounters.cpp ---------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "RISCVCounters.h"
+
+namespace llvm {
+namespace exegesis {
+
+// This implementation of RISCV target for Exegesis doesn't use libpfm
+// and provides manual implementation of performance counters.
+
+inline uint64_t getRISCVCpuCyclesCount() {
+#ifdef __riscv
+ uint64_t Counter;
+ asm("csrr %0, cycle" : "=r"(Counter)::"memory");
+ return Counter;
+#else
+ return 0;
+#endif
+}
+
+class RISCVCpuCyclesCounter : public pfm::CounterGroup {
+ uint64_t StartValue;
+ uint64_t EndValue;
+ uint64_t MeasurementCycles;
+
+public:
+ explicit RISCVCpuCyclesCounter(pfm::PerfEvent &&Event);
+
+ void start() override { StartValue = getRISCVCpuCyclesCount(); }
+
+ void stop() override { EndValue = getRISCVCpuCyclesCount(); }
+
+ Expected<llvm::SmallVector<int64_t, 4>>
+ readOrError(StringRef FunctionBytes) const override;
+};
+
+RISCVCpuCyclesCounter::RISCVCpuCyclesCounter(pfm::PerfEvent &&Event)
+ : CounterGroup(std::move(Event), {}) {
+ StartValue = getRISCVCpuCyclesCount();
+ EndValue = getRISCVCpuCyclesCount();
+ MeasurementCycles = EndValue - StartValue;
+ if (MeasurementCycles == 0) {
+ report_fatal_error("MeasurementCycles == 0, "
+ "performance counters are not configured.");
+ }
+ StartValue = EndValue = 0;
+}
+
+Expected<SmallVector<int64_t, 4>>
+RISCVCpuCyclesCounter::readOrError(StringRef FunctionBytes) const {
+ uint64_t Counter = EndValue - StartValue - MeasurementCycles;
----------------
boomanaiden154 wrote:
I would warn against subtracting `MeasurementCycles` from the counter value. There are going to be quite a few overheads that aren't really avoidable within `llvm-exegesis`, and trying to subtract them out in places like this doesn't really seem ideal to me. To subtract out all differences during a measurement, we have the `middle-half-duplicate` and `middle-half-loop` repetition modes that are able to systematically subtract out overheads and anecdotally work quite well.
https://github.com/llvm/llvm-project/pull/89047
More information about the llvm-commits
mailing list