Skip to content

Commit 08165c4

Browse files
authored
[RISCV] Add searchable table for tune information (#66193)
There are many information that can be used for tuning, like alignments, cache line size, etc. But we can't make all of them `SubtargetFeature` because some of them are not with enumerable value, for example, `PrefetchDistance` used by `LoopDataPrefetch`. In this patch, a searchable table `RISCVTuneInfoTable` is added, in which each entry contains the CPU name and all tune information defined in `RISCVTuneInfo`. Each field of `RISCVTuneInfo` should have a default value and processor definitions can override the default value via `let` statements. We don't need to define a `RISCVTuneInfo` for each processor and it will use the default value (which is for `generic`) if no `RISCVTuneInfo` defined. For processors in the same series, a subclass can inherit from `RISCVTuneInfo` and override the fields. And we can also override the fields in processor definitions if there are some differences in the same processor series. When initilizing `RISCVSubtarget`, we will use `TuneCPU` as the key to serach the tune info table. So, the behavior here is if we don't specify the tune CPU, we will use specified `CPU`, which is expected I think. This patch almost undoes 61ab106, in which I added tune features of preferred function/loop alignments. More tune information can be added in the future.
1 parent 6417ce4 commit 08165c4

File tree

6 files changed

+54
-22
lines changed

6 files changed

+54
-22
lines changed

llvm/lib/Target/RISCV/RISCVFeatures.td

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -950,12 +950,3 @@ def FeatureTaggedGlobals : SubtargetFeature<"tagged-globals",
950950
"AllowTaggedGlobals",
951951
"true", "Use an instruction sequence for taking the address of a global "
952952
"that allows a memory tag in the upper address bits">;
953-
954-
foreach align = [2, 4, 8, 16, 32, 64] in {
955-
def TunePrefFunctionAlignment # align :
956-
SubtargetFeature<"pref-func-align-" # align, "PrefFunctionAlignment",
957-
"Align(" # align # ")", "Set preferred function alignment to " # align # " bytes">;
958-
def TunePrefLoopAlignment # align :
959-
SubtargetFeature<"pref-loop-align-" # align, "PrefLoopAlignment",
960-
"Align(" # align # ")", "Set preferred loop alignment to " # align # " bytes">;
961-
}

llvm/lib/Target/RISCV/RISCVProcessors.td

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@
1010
// RISC-V processors supported.
1111
//===----------------------------------------------------------------------===//
1212

13+
class RISCVTuneInfo {
14+
bits<8> PrefFunctionAlignment = 1;
15+
bits<8> PrefLoopAlignment = 1;
16+
}
17+
18+
def RISCVTuneInfoTable : GenericTable {
19+
let FilterClass = "RISCVTuneInfo";
20+
let CppTypeName = "RISCVTuneInfo";
21+
let Fields = ["Name", "PrefFunctionAlignment", "PrefLoopAlignment"];
22+
}
23+
24+
def getRISCVTuneInfo : SearchIndex {
25+
let Table = RISCVTuneInfoTable;
26+
let Key = ["Name"];
27+
}
28+
29+
class GenericTuneInfo: RISCVTuneInfo;
30+
1331
class RISCVProcessorModel<string n,
1432
SchedMachineModel m,
1533
list<SubtargetFeature> f,
@@ -27,13 +45,15 @@ class RISCVTuneProcessorModel<string n,
2745

2846
def GENERIC_RV32 : RISCVProcessorModel<"generic-rv32",
2947
NoSchedModel,
30-
[Feature32Bit]>;
48+
[Feature32Bit]>,
49+
GenericTuneInfo;
3150
def GENERIC_RV64 : RISCVProcessorModel<"generic-rv64",
3251
NoSchedModel,
33-
[Feature64Bit]>;
52+
[Feature64Bit]>,
53+
GenericTuneInfo;
3454
// Support generic for compatibility with other targets. The triple will be used
3555
// to change to the appropriate rv32/rv64 version.
36-
def : ProcessorModel<"generic", NoSchedModel, []>;
56+
def : ProcessorModel<"generic", NoSchedModel, []>, GenericTuneInfo;
3757

3858
def ROCKET_RV32 : RISCVProcessorModel<"rocket-rv32",
3959
RocketModel,

llvm/lib/Target/RISCV/RISCVSubtarget.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ using namespace llvm;
2929
#define GET_SUBTARGETINFO_CTOR
3030
#include "RISCVGenSubtargetInfo.inc"
3131

32+
namespace llvm::RISCVTuneInfoTable {
33+
34+
#define GET_RISCVTuneInfoTable_IMPL
35+
#include "RISCVGenSearchableTables.inc"
36+
} // namespace llvm::RISCVTuneInfoTable
37+
3238
static cl::opt<bool> EnableSubRegLiveness("riscv-enable-subreg-liveness",
3339
cl::init(true), cl::Hidden);
3440

@@ -65,6 +71,12 @@ RISCVSubtarget::initializeSubtargetDependencies(const Triple &TT, StringRef CPU,
6571
if (TuneCPU.empty())
6672
TuneCPU = CPU;
6773

74+
TuneInfo = RISCVTuneInfoTable::getRISCVTuneInfo(TuneCPU);
75+
// If there is no TuneInfo for this CPU, we fail back to generic.
76+
if (!TuneInfo)
77+
TuneInfo = RISCVTuneInfoTable::getRISCVTuneInfo("generic");
78+
assert(TuneInfo && "TuneInfo shouldn't be nullptr!");
79+
6880
ParseSubtargetFeatures(CPU, TuneCPU, FS);
6981
TargetABI = RISCVABI::computeTargetABI(TT, getFeatureBits(), ABIName);
7082
RISCVFeatures::validate(TT, getFeatureBits());

llvm/lib/Target/RISCV/RISCVSubtarget.h

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@
3232
namespace llvm {
3333
class StringRef;
3434

35+
namespace RISCVTuneInfoTable {
36+
37+
struct RISCVTuneInfo {
38+
const char *Name;
39+
uint8_t PrefFunctionAlignment;
40+
uint8_t PrefLoopAlignment;
41+
};
42+
43+
#define GET_RISCVTuneInfoTable_DECL
44+
#include "RISCVGenSearchableTables.inc"
45+
} // namespace RISCVTuneInfoTable
46+
3547
class RISCVSubtarget : public RISCVGenSubtargetInfo {
3648
public:
3749
enum RISCVProcFamilyEnum : uint8_t {
@@ -54,8 +66,7 @@ class RISCVSubtarget : public RISCVGenSubtargetInfo {
5466
uint8_t MaxInterleaveFactor = 2;
5567
RISCVABI::ABI TargetABI = RISCVABI::ABI_Unknown;
5668
std::bitset<RISCV::NUM_TARGET_REGS> UserReservedRegister;
57-
Align PrefFunctionAlignment;
58-
Align PrefLoopAlignment;
69+
const RISCVTuneInfoTable::RISCVTuneInfo *TuneInfo;
5970

6071
RISCVFrameLowering FrameLowering;
6172
RISCVInstrInfo InstrInfo;
@@ -96,8 +107,12 @@ class RISCVSubtarget : public RISCVGenSubtargetInfo {
96107
}
97108
bool enableMachineScheduler() const override { return true; }
98109

99-
Align getPrefFunctionAlignment() const { return PrefFunctionAlignment; }
100-
Align getPrefLoopAlignment() const { return PrefLoopAlignment; }
110+
Align getPrefFunctionAlignment() const {
111+
return Align(TuneInfo->PrefFunctionAlignment);
112+
}
113+
Align getPrefLoopAlignment() const {
114+
return Align(TuneInfo->PrefLoopAlignment);
115+
}
101116

102117
/// Returns RISC-V processor family.
103118
/// Avoid this function! CPU specifics should be kept local to this class

llvm/test/CodeGen/RISCV/align-loops.ll

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
; RUN: llc < %s -mtriple=riscv64 | FileCheck %s
22
; RUN: llc < %s -mtriple=riscv64 -align-loops=16 | FileCheck %s -check-prefix=ALIGN_16
33
; RUN: llc < %s -mtriple=riscv64 -align-loops=32 | FileCheck %s -check-prefix=ALIGN_32
4-
; RUN: llc < %s -mtriple=riscv64 -mattr=+pref-loop-align-16 | FileCheck %s -check-prefix=ALIGN_16
5-
; RUN: llc < %s -mtriple=riscv64 -mattr=+pref-loop-align-32 | FileCheck %s -check-prefix=ALIGN_32
64

75
declare void @foo()
86

llvm/test/CodeGen/RISCV/align.ll

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
; RUN: | FileCheck %s -check-prefix=RV32I
33
; RUN: llc -mtriple=riscv32 -mattr=+c -verify-machineinstrs < %s \
44
; RUN: | FileCheck %s -check-prefix=RV32C
5-
; RUN: llc -mtriple=riscv32 -mattr=+pref-func-align-32 -verify-machineinstrs < %s \
6-
; RUN: | FileCheck %s -check-prefix=ALIGN-32
75
; RUN: llc -filetype=obj -mtriple=riscv32 < %s -o %t
86
; RUN: llvm-readelf -S %t | FileCheck %s --check-prefixes=SEC,SEC-I
97
; RUN: llc -filetype=obj -mtriple=riscv32 -mattr=+c < %s -o %t
@@ -18,8 +16,6 @@ define void @foo() {
1816
;RV32I: foo:
1917
;RV32C: .p2align 1
2018
;RV32C: foo:
21-
;ALIGN-32: .p2align 5
22-
;ALIGN-32: foo:
2319
entry:
2420
ret void
2521
}

0 commit comments

Comments
 (0)