Skip to content

Commit fa079e4

Browse files
committed
[RFC][RISCV] Support RISC-V Profiles in -march option
This PR implements the draft riscv-non-isa/riscv-toolchain-conventions#36. Currently, we replace specified profile in `-march` with standard arch string. We may need to pass it to backend so that we can emit an ELF attr proposed by riscv-non-isa/riscv-elf-psabi-doc#409.
1 parent 9d3fbf9 commit fa079e4

File tree

7 files changed

+271
-1
lines changed

7 files changed

+271
-1
lines changed

clang/test/Driver/riscv-profiles.c

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// RUN: %clang -### -c %s 2>&1 -march=rvi20u32 | FileCheck -check-prefix=RVI20U32 %s
2+
// RVI20U32: "-target-cpu" "generic-rv32"
3+
// RVI20U32: "-target-feature" "-a"
4+
// RVI20U32: "-target-feature" "-c"
5+
// RVI20U32: "-target-feature" "-d"
6+
// RVI20U32: "-target-feature" "-f"
7+
// RVI20U32: "-target-feature" "-m"
8+
// RVI20U32: "-target-feature" "+rvi20u32"
9+
// RVI20U32: "-target-abi" "ilp32"
10+
11+
// RUN: %clang -### -c %s 2>&1 -march=rvi20u64 | FileCheck -check-prefix=RVI20U64 %s
12+
// RVI20U64: "-target-cpu" "generic-rv64"
13+
// RVI20U64: "-target-feature" "-a"
14+
// RVI20U64: "-target-feature" "-c"
15+
// RVI20U64: "-target-feature" "-d"
16+
// RVI20U64: "-target-feature" "-f"
17+
// RVI20U64: "-target-feature" "-m"
18+
// RVI20U64: "-target-feature" "+rvi20u64"
19+
// RVI20U64: "-target-abi" "lp64"
20+
21+
// RUN: %clang -### -c %s 2>&1 -march=rva20u64 | FileCheck -check-prefix=RVA20U64 %s
22+
// RVA20U64: "-target-cpu" "generic-rv64"
23+
// RVA20U64: "-target-feature" "+m"
24+
// RVA20U64: "-target-feature" "+a"
25+
// RVA20U64: "-target-feature" "+f"
26+
// RVA20U64: "-target-feature" "+d"
27+
// RVA20U64: "-target-feature" "+c"
28+
// RVA20U64: "-target-feature" "+zicsr"
29+
// RVA20U64: "-target-feature" "+rva20u64"
30+
// RVA20U64: "-target-abi" "lp64d"
31+
32+
// RUN: %clang -### -c %s 2>&1 -march=rva20s64 | FileCheck -check-prefix=RVA20S64 %s
33+
// RVA20S64: "-target-cpu" "generic-rv64"
34+
// RVA20S64: "-target-feature" "+m"
35+
// RVA20S64: "-target-feature" "+a"
36+
// RVA20S64: "-target-feature" "+f"
37+
// RVA20S64: "-target-feature" "+d"
38+
// RVA20S64: "-target-feature" "+c"
39+
// RVA20S64: "-target-feature" "+zicsr"
40+
// RVA20S64: "-target-feature" "+zifencei"
41+
// RVA20S64: "-target-feature" "+rva20s64"
42+
// RVA20S64: "-target-abi" "lp64d"
43+
44+
// RUN: %clang -### -c %s 2>&1 -march=rva22u64 | FileCheck -check-prefix=RVA22U64 %s
45+
// RVA22U64: "-target-cpu" "generic-rv64"
46+
// RVA22U64: "-target-feature" "+m"
47+
// RVA22U64: "-target-feature" "+a"
48+
// RVA22U64: "-target-feature" "+f"
49+
// RVA22U64: "-target-feature" "+d"
50+
// RVA22U64: "-target-feature" "+c"
51+
// RVA22U64: "-target-feature" "+zicbom"
52+
// RVA22U64: "-target-feature" "+zicbop"
53+
// RVA22U64: "-target-feature" "+zicboz"
54+
// RVA22U64: "-target-feature" "+zicsr"
55+
// RVA22U64: "-target-feature" "+zihintpause"
56+
// RVA22U64: "-target-feature" "+zfhmin"
57+
// RVA22U64: "-target-feature" "+zba"
58+
// RVA22U64: "-target-feature" "+zbb"
59+
// RVA22U64: "-target-feature" "+zbs"
60+
// RVA22U64: "-target-feature" "+zkt"
61+
// RVA22U64: "-target-feature" "+rva22u64"
62+
// RVA22U64: "-target-abi" "lp64d"
63+
64+
// RUN: %clang -### -c %s 2>&1 -march=rva22s64 | FileCheck -check-prefix=RVA22S64 %s
65+
// RVA22S64: "-target-cpu" "generic-rv64"
66+
// RVA22S64: "-target-feature" "+m"
67+
// RVA22S64: "-target-feature" "+a"
68+
// RVA22S64: "-target-feature" "+f"
69+
// RVA22S64: "-target-feature" "+d"
70+
// RVA22S64: "-target-feature" "+c"
71+
// RVA22S64: "-target-feature" "+zicbom"
72+
// RVA22S64: "-target-feature" "+zicbop"
73+
// RVA22S64: "-target-feature" "+zicboz"
74+
// RVA22S64: "-target-feature" "+zicsr"
75+
// RVA22S64: "-target-feature" "+zifencei"
76+
// RVA22S64: "-target-feature" "+zihintpause"
77+
// RVA22S64: "-target-feature" "+zfhmin"
78+
// RVA22S64: "-target-feature" "+zba"
79+
// RVA22S64: "-target-feature" "+zbb"
80+
// RVA22S64: "-target-feature" "+zbs"
81+
// RVA22S64: "-target-feature" "+zkt"
82+
// RVA22S64: "-target-feature" "+svinval"
83+
// RVA22S64: "-target-feature" "+svpbmt"
84+
// RVA22S64: "-target-feature" "+rva22s64"
85+
// RVA22S64: "-target-abi" "lp64d"
86+
87+
// RUN: %clang -### -c %s 2>&1 -march=rva22u64_zfa | FileCheck -check-prefix=PROFILE-WITH-ADDITIONAL %s
88+
// PROFILE-WITH-ADDITIONAL: "-target-cpu" "generic-rv64"
89+
// PROFILE-WITH-ADDITIONAL: "-target-feature" "+m"
90+
// PROFILE-WITH-ADDITIONAL: "-target-feature" "+a"
91+
// PROFILE-WITH-ADDITIONAL: "-target-feature" "+f"
92+
// PROFILE-WITH-ADDITIONAL: "-target-feature" "+d"
93+
// PROFILE-WITH-ADDITIONAL: "-target-feature" "+c"
94+
// PROFILE-WITH-ADDITIONAL: "-target-feature" "+zicbom"
95+
// PROFILE-WITH-ADDITIONAL: "-target-feature" "+zicbop"
96+
// PROFILE-WITH-ADDITIONAL: "-target-feature" "+zicboz"
97+
// PROFILE-WITH-ADDITIONAL: "-target-feature" "+zicsr"
98+
// PROFILE-WITH-ADDITIONAL: "-target-feature" "+zihintpause"
99+
// PROFILE-WITH-ADDITIONAL: "-target-feature" "+zfa"
100+
// PROFILE-WITH-ADDITIONAL: "-target-feature" "+zfhmin"
101+
// PROFILE-WITH-ADDITIONAL: "-target-feature" "+zba"
102+
// PROFILE-WITH-ADDITIONAL: "-target-feature" "+zbb"
103+
// PROFILE-WITH-ADDITIONAL: "-target-feature" "+zbs"
104+
// PROFILE-WITH-ADDITIONAL: "-target-feature" "+zkt"
105+
// PROFILE-WITH-ADDITIONAL: "-target-feature" "+rva22u64"
106+
// PROFILE-WITH-ADDITIONAL: "-target-abi" "lp64d"
107+
108+
// RUN: not %clang -### -c %s 2>&1 -march=rva19u64_zfa | FileCheck -check-prefix=INVALID-PROFILE %s
109+
// INVALID-PROFILE: error: invalid arch name 'rva19u64_zfa', unsupported profile
110+
111+
// RUN: not %clang -### -c %s 2>&1 -march=rva22u64zfa | FileCheck -check-prefix=INVALID-ADDITIONAL %s
112+
// INVALID-ADDITIONAL: error: invalid arch name 'rva22u64zfa', additional extensions must be after separator '_'

llvm/include/llvm/Support/RISCVISAInfo.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class RISCVISAInfo {
8080
unsigned getMaxVLen() const { return 65536; }
8181
unsigned getMaxELen() const { return MaxELen; }
8282
unsigned getMaxELenFp() const { return MaxELenFp; }
83+
std::string getProfile() const { return Profile; }
8384

8485
bool hasExtension(StringRef Ext) const;
8586
std::string toString() const;
@@ -97,12 +98,13 @@ class RISCVISAInfo {
9798

9899
private:
99100
RISCVISAInfo(unsigned XLen)
100-
: XLen(XLen), FLen(0), MinVLen(0), MaxELen(0), MaxELenFp(0) {}
101+
: XLen(XLen), FLen(0), MinVLen(0), MaxELen(0), MaxELenFp(0), Profile() {}
101102

102103
unsigned XLen;
103104
unsigned FLen;
104105
unsigned MinVLen;
105106
unsigned MaxELen, MaxELenFp;
107+
std::string Profile;
106108

107109
OrderedExtensionMap Exts;
108110

llvm/lib/Support/RISCVISAInfo.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ struct RISCVSupportedExtension {
3939
return StringRef(Name) < StringRef(RHS.Name);
4040
}
4141
};
42+
struct RISCVProfile {
43+
const char *Name;
44+
const char *MArch;
45+
};
4246

4347
} // end anonymous namespace
4448

@@ -206,6 +210,17 @@ static const RISCVSupportedExtension SupportedExperimentalExtensions[] = {
206210
{"zvfbfwma", RISCVExtensionVersion{0, 8}},
207211
};
208212

213+
static const RISCVProfile SupportedProfiles[] = {
214+
{"rvi20u32", "rv32i"},
215+
{"rvi20u64", "rv64i"},
216+
{"rva20u64", "rv64imafdc_zicsr"},
217+
{"rva20s64", "rv64imafdc_zicsr_zifencei"},
218+
{"rva22u64", "rv64imafdc_zicsr_zihintpause_zba_zbb_zbs_zicbom_zicbop_"
219+
"zicboz_zfhmin_zkt"},
220+
{"rva22s64", "rv64imafdc_zicsr_zifencei_zihintpause_zba_zbb_zbs_zicbom_"
221+
"zicbop_zicboz_zfhmin_zkt_svpbmt_svinval"},
222+
};
223+
209224
static void verifyTables() {
210225
#ifndef NDEBUG
211226
static std::atomic<bool> TableChecked(false);
@@ -494,6 +509,10 @@ void RISCVISAInfo::toFeatures(
494509
Features.push_back(StrAlloc(Twine("-experimental-") + Ext.Name));
495510
}
496511
}
512+
513+
// Add profile feature.
514+
if (!Profile.empty())
515+
Features.push_back(StrAlloc(Twine("+") + Profile));
497516
}
498517

499518
// Extensions may have a version number, and may be separated by
@@ -710,6 +729,36 @@ RISCVISAInfo::parseArchString(StringRef Arch, bool EnableExperimentalExtension,
710729
"string must be lowercase");
711730
}
712731

732+
bool IsProfile = Arch.starts_with("rvi") || Arch.starts_with("rva") ||
733+
Arch.starts_with("rvb") || Arch.starts_with("rvm");
734+
std::string NewArch;
735+
std::string ProfileName;
736+
if (IsProfile) {
737+
const RISCVProfile *FoundProfile = nullptr;
738+
for (const RISCVProfile &Profile : SupportedProfiles) {
739+
if (Arch.starts_with(Profile.Name)) {
740+
FoundProfile = &Profile;
741+
break;
742+
}
743+
}
744+
745+
if (!FoundProfile)
746+
return createStringError(errc::invalid_argument, "unsupported profile");
747+
748+
ProfileName = FoundProfile->Name;
749+
NewArch = FoundProfile->MArch;
750+
751+
StringRef ArchWithoutProfile = Arch.substr(ProfileName.size());
752+
if (!ArchWithoutProfile.empty()) {
753+
if (!ArchWithoutProfile.starts_with("_"))
754+
return createStringError(
755+
errc::invalid_argument,
756+
"additional extensions must be after separator '_'");
757+
NewArch = NewArch + ArchWithoutProfile.str();
758+
}
759+
Arch = NewArch;
760+
}
761+
713762
bool HasRV64 = Arch.starts_with("rv64");
714763
// ISA string must begin with rv32 or rv64.
715764
if (!(Arch.starts_with("rv32") || HasRV64) || (Arch.size() < 5)) {
@@ -720,6 +769,8 @@ RISCVISAInfo::parseArchString(StringRef Arch, bool EnableExperimentalExtension,
720769

721770
unsigned XLen = HasRV64 ? 64 : 32;
722771
std::unique_ptr<RISCVISAInfo> ISAInfo(new RISCVISAInfo(XLen));
772+
if (!ProfileName.empty())
773+
ISAInfo->Profile = ProfileName;
723774

724775
// The canonical order specified in ISA manual.
725776
// Ref: Table 22.1 in RISC-V User-Level ISA V2.2

llvm/lib/Target/RISCV/RISCV.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ include "RISCVSchedSyntacoreSCR1.td"
4444

4545
include "RISCVProcessors.td"
4646

47+
//===----------------------------------------------------------------------===//
48+
// RISC-V profiles supported.
49+
//===----------------------------------------------------------------------===//
50+
51+
include "RISCVProfiles.td"
52+
4753
//===----------------------------------------------------------------------===//
4854
// Define the RISC-V target.
4955
//===----------------------------------------------------------------------===//
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//===------ RISCVProfiles.td - RISC-V Profiles -------------*- tablegen -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
class RISCVProfile<string name, list<SubtargetFeature> features>
10+
: SubtargetFeature<name, "RISCVProfile", NAME,
11+
"RISC-V " # name # " profile",
12+
features>;
13+
14+
def RVI20U32 : RISCVProfile<"rvi20u32", [Feature32Bit]>;
15+
16+
def RVI20U64 : RISCVProfile<"rvi20u64", [Feature64Bit]>;
17+
18+
def RVA20U64 : RISCVProfile<"rva20u64", [Feature64Bit,
19+
FeatureStdExtM,
20+
FeatureStdExtA,
21+
FeatureStdExtF,
22+
FeatureStdExtD,
23+
FeatureStdExtC,
24+
FeatureStdExtZicsr]>;
25+
26+
def RVA20S64 : RISCVProfile<"rva20s64", [Feature64Bit,
27+
FeatureStdExtM,
28+
FeatureStdExtA,
29+
FeatureStdExtF,
30+
FeatureStdExtD,
31+
FeatureStdExtC,
32+
FeatureStdExtZicsr,
33+
FeatureStdExtZifencei]>;
34+
35+
def RVA22U64 : RISCVProfile<"rva22u64", [Feature64Bit,
36+
FeatureStdExtM,
37+
FeatureStdExtA,
38+
FeatureStdExtF,
39+
FeatureStdExtD,
40+
FeatureStdExtC,
41+
FeatureStdExtZba,
42+
FeatureStdExtZbb,
43+
FeatureStdExtZbs,
44+
FeatureStdExtZfhmin,
45+
FeatureStdExtZicbom,
46+
FeatureStdExtZicbop,
47+
FeatureStdExtZicboz,
48+
FeatureStdExtZkt,
49+
FeatureStdExtZicsr,
50+
FeatureStdExtZihintpause]>;
51+
52+
def RVA22S64 : RISCVProfile<"rva22s64", [Feature64Bit,
53+
FeatureStdExtM,
54+
FeatureStdExtA,
55+
FeatureStdExtF,
56+
FeatureStdExtD,
57+
FeatureStdExtC,
58+
FeatureStdExtZba,
59+
FeatureStdExtZbb,
60+
FeatureStdExtZbs,
61+
FeatureStdExtZfhmin,
62+
FeatureStdExtZicbom,
63+
FeatureStdExtZicbop,
64+
FeatureStdExtZicboz,
65+
FeatureStdExtZkt,
66+
FeatureStdExtZicsr,
67+
FeatureStdExtZifencei,
68+
FeatureStdExtZihintpause,
69+
FeatureStdExtSvpbmt,
70+
FeatureStdExtSvinval]>;

llvm/lib/Target/RISCV/RISCVSubtarget.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ struct RISCVTuneInfo {
5353
#include "RISCVGenSearchableTables.inc"
5454
} // namespace RISCVTuneInfoTable
5555

56+
enum RISCVProfileEnum : uint8_t {
57+
Unspecified,
58+
RVA20S64,
59+
RVA20U64,
60+
RVA22S64,
61+
RVA22U64,
62+
RVI20U32,
63+
RVI20U64,
64+
};
65+
5666
class RISCVSubtarget : public RISCVGenSubtargetInfo {
5767
public:
5868
// clang-format off
@@ -67,6 +77,8 @@ class RISCVSubtarget : public RISCVGenSubtargetInfo {
6777

6878
RISCVProcFamilyEnum RISCVProcFamily = Others;
6979

80+
RISCVProfileEnum RISCVProfile = Unspecified;
81+
7082
#define GET_SUBTARGETINFO_MACRO(ATTRIBUTE, DEFAULT, GETTER) \
7183
bool ATTRIBUTE = DEFAULT;
7284
#include "RISCVGenSubtargetInfo.inc"
@@ -135,6 +147,8 @@ class RISCVSubtarget : public RISCVGenSubtargetInfo {
135147
/// initializeProperties().
136148
RISCVProcFamilyEnum getProcFamily() const { return RISCVProcFamily; }
137149

150+
RISCVProfileEnum getRISCVProfile() const { return RISCVProfile; }
151+
138152
#define GET_SUBTARGETINFO_MACRO(ATTRIBUTE, DEFAULT, GETTER) \
139153
bool GETTER() const { return ATTRIBUTE; }
140154
#include "RISCVGenSubtargetInfo.inc"

llvm/test/CodeGen/RISCV/attributes.ll

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,14 @@
189189
; RUN: llc -mtriple=riscv64 -mattr=+experimental-zacas %s -o - | FileCheck --check-prefix=RV64ZACAS %s
190190
; RUN: llc -mtriple=riscv64 -mattr=+experimental-zicfilp %s -o - | FileCheck --check-prefix=RV64ZICFILP %s
191191

192+
; Tests for profile features.
193+
; RUN: llc -mtriple=riscv32 -mattr=+rvi20u32 %s -o - | FileCheck --check-prefix=RVI20U32 %s
194+
; RUN: llc -mtriple=riscv64 -mattr=+rvi20u64 %s -o - | FileCheck --check-prefix=RVI20U64 %s
195+
; RUN: llc -mtriple=riscv64 -mattr=+rva20u64 %s -o - | FileCheck --check-prefix=RVA20U64 %s
196+
; RUN: llc -mtriple=riscv64 -mattr=+rva20s64 %s -o - | FileCheck --check-prefix=RVA20S64 %s
197+
; RUN: llc -mtriple=riscv64 -mattr=+rva22u64 %s -o - | FileCheck --check-prefix=RVA22U64 %s
198+
; RUN: llc -mtriple=riscv64 -mattr=+rva22s64 %s -o - | FileCheck --check-prefix=RVA22S64 %s
199+
192200
; CHECK: .attribute 4, 16
193201

194202
; RV32M: .attribute 5, "rv32i2p1_m2p0"
@@ -378,6 +386,13 @@
378386
; RV64ZACAS: .attribute 5, "rv64i2p1_a2p1_zacas1p0"
379387
; RV64ZICFILP: .attribute 5, "rv64i2p1_zicfilp0p4"
380388

389+
; RVI20U32: .attribute 5, "rv32i2p1"
390+
; RVI20U64: .attribute 5, "rv64i2p1"
391+
; RVA20U64: .attribute 5, "rv64i2p1_m2p0_a2p1_f2p2_d2p2_c2p0_zicsr2p0"
392+
; RVA20S64: .attribute 5, "rv64i2p1_m2p0_a2p1_f2p2_d2p2_c2p0_zicsr2p0_zifencei2p0"
393+
; RVA22U64: .attribute 5, "rv64i2p1_m2p0_a2p1_f2p2_d2p2_c2p0_zicbom1p0_zicbop1p0_zicboz1p0_zicsr2p0_zihintpause2p0_zfhmin1p0_zba1p0_zbb1p0_zbs1p0_zkt1p0"
394+
; RVA22S64: .attribute 5, "rv64i2p1_m2p0_a2p1_f2p2_d2p2_c2p0_zicbom1p0_zicbop1p0_zicboz1p0_zicsr2p0_zifencei2p0_zihintpause2p0_zfhmin1p0_zba1p0_zbb1p0_zbs1p0_zkt1p0_svinval1p0_svpbmt1p0"
395+
381396
define i32 @addi(i32 %a) {
382397
%1 = add i32 %a, 1
383398
ret i32 %1

0 commit comments

Comments
 (0)