Skip to content

Commit

Permalink
[RISCV][llvm-exegesis] Add default Pfm cycle counter. (llvm#121866)
Browse files Browse the repository at this point in the history
Also tested with Ubuntu on SiFive's HiFive Premier P550 board. Curiously
latency is reporting ~1.5 on basic scalar arithmetic, scalar mul is
~3.5, and div is ~36.5. This 0.5 cycles higher than I expect.
  • Loading branch information
topperc authored Jan 7, 2025
1 parent 6192faf commit afa8aee
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions llvm/lib/Target/RISCV/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ tablegen(LLVM RISCVGenRegisterBank.inc -gen-register-bank)
tablegen(LLVM RISCVGenRegisterInfo.inc -gen-register-info)
tablegen(LLVM RISCVGenSearchableTables.inc -gen-searchable-tables)
tablegen(LLVM RISCVGenSubtargetInfo.inc -gen-subtarget)
tablegen(LLVM RISCVGenExegesis.inc -gen-exegesis)

set(LLVM_TARGET_DEFINITIONS RISCVGISel.td)
tablegen(LLVM RISCVGenGlobalISel.inc -gen-global-isel)
Expand Down
6 changes: 6 additions & 0 deletions llvm/lib/Target/RISCV/RISCV.td
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ include "RISCVSchedXiangShanNanHu.td"

include "RISCVProcessors.td"

//===----------------------------------------------------------------------===//
// Pfm Counters
//===----------------------------------------------------------------------===//

include "RISCVPfmCounters.td"

//===----------------------------------------------------------------------===//
// Define the RISC-V target.
//===----------------------------------------------------------------------===//
Expand Down
18 changes: 18 additions & 0 deletions llvm/lib/Target/RISCV/RISCVPfmCounters.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===---- RISCVPfmCounters.td - RISC-V Hardware Counters ---*- tablegen -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This describes the available hardware counters for RISC-V.
//
//===----------------------------------------------------------------------===//

def CpuCyclesPfmCounter : PfmCounter<"CYCLES">;

def DefaultPfmCounters : ProcPfmCounters {
let CycleCounter = CpuCyclesPfmCounter;
}
def : PfmCountersDefaultBinding<DefaultPfmCounters>;
5 changes: 3 additions & 2 deletions llvm/tools/llvm-exegesis/lib/RISCV/Target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
namespace llvm {
namespace exegesis {

#include "RISCVGenExegesis.inc"

namespace {

// Stores constant value to a general-purpose (integer) register.
Expand Down Expand Up @@ -132,8 +134,7 @@ class ExegesisRISCVTarget : public ExegesisTarget {
};

ExegesisRISCVTarget::ExegesisRISCVTarget()
: ExegesisTarget(ArrayRef<CpuAndPfmCounters>{},
RISCV_MC::isOpcodeAvailable) {}
: ExegesisTarget(RISCVCpuPfmCounters, RISCV_MC::isOpcodeAvailable) {}

bool ExegesisRISCVTarget::matchesArch(Triple::ArchType Arch) const {
return Arch == Triple::riscv32 || Arch == Triple::riscv64;
Expand Down
9 changes: 9 additions & 0 deletions llvm/unittests/tools/llvm-exegesis/RISCV/TargetTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ TEST_F(RISCVTargetTest, SetRegToConstant) {
EXPECT_THAT(Insts, Not(IsEmpty()));
}

TEST_F(RISCVTargetTest, DefaultPfmCounters) {
const std::string Expected = "CYCLES";
EXPECT_EQ(State.getExegesisTarget().getPfmCounters("").CycleCounter,
Expected);
EXPECT_EQ(
State.getExegesisTarget().getPfmCounters("unknown_cpu").CycleCounter,
Expected);
}

} // namespace
} // namespace exegesis
} // namespace llvm

0 comments on commit afa8aee

Please sign in to comment.