Skip to content

Commit 75d6795

Browse files
authored
[RISCV][Clang][TargetParser] Support getting feature unaligned-scalar-mem from mcpu. (#71513)
This patch reference ac1ffd3 to suppot a soft coding way to identify whether a cpu has a feature `unaligned-scalar-mem` by `RISCVProcessors.td`. This patch does not provide test case since there is no risc-v cpu support `unaligned-scalar-mem` in llvm upstream now.
1 parent f0cdf4b commit 75d6795

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

clang/lib/Driver/ToolChains/Arch/RISCV.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ static void getRISCFeaturesFromMcpu(const Driver &D, const Arg *A,
6363
D.Diag(clang::diag::err_drv_unsupported_option_argument)
6464
<< A->getSpelling() << Mcpu;
6565
}
66+
67+
if (llvm::RISCV::hasFastUnalignedAccess(Mcpu))
68+
Features.push_back("+unaligned-scalar-mem");
6669
}
6770

6871
void riscv::getRISCVTargetFeatures(const Driver &D, const llvm::Triple &Triple,

llvm/include/llvm/TargetParser/RISCVTargetParser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ bool parseTuneCPU(StringRef CPU, bool IsRV64);
3131
StringRef getMArchFromMcpu(StringRef CPU);
3232
void fillValidCPUArchList(SmallVectorImpl<StringRef> &Values, bool IsRV64);
3333
void fillValidTuneCPUArchList(SmallVectorImpl<StringRef> &Values, bool IsRV64);
34+
bool hasFastUnalignedAccess(StringRef CPU);
3435

3536
} // namespace RISCV
3637
} // namespace llvm

llvm/lib/TargetParser/RISCVTargetParser.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,21 @@ namespace llvm {
2020
namespace RISCV {
2121

2222
enum CPUKind : unsigned {
23-
#define PROC(ENUM, NAME, DEFAULT_MARCH) CK_##ENUM,
23+
#define PROC(ENUM, NAME, DEFAULT_MARCH, FAST_UNALIGN) CK_##ENUM,
2424
#define TUNE_PROC(ENUM, NAME) CK_##ENUM,
2525
#include "llvm/TargetParser/RISCVTargetParserDef.inc"
2626
};
2727

2828
struct CPUInfo {
2929
StringLiteral Name;
3030
StringLiteral DefaultMarch;
31+
bool FastUnalignedAccess;
3132
bool is64Bit() const { return DefaultMarch.starts_with("rv64"); }
3233
};
3334

3435
constexpr CPUInfo RISCVCPUInfo[] = {
35-
#define PROC(ENUM, NAME, DEFAULT_MARCH) \
36-
{NAME, DEFAULT_MARCH},
36+
#define PROC(ENUM, NAME, DEFAULT_MARCH, FAST_UNALIGN) \
37+
{NAME, DEFAULT_MARCH, FAST_UNALIGN},
3738
#include "llvm/TargetParser/RISCVTargetParserDef.inc"
3839
};
3940

@@ -44,6 +45,11 @@ static const CPUInfo *getCPUInfoByName(StringRef CPU) {
4445
return nullptr;
4546
}
4647

48+
bool hasFastUnalignedAccess(StringRef CPU) {
49+
const CPUInfo *Info = getCPUInfoByName(CPU);
50+
return Info && Info->FastUnalignedAccess;
51+
}
52+
4753
bool parseCPU(StringRef CPU, bool IsRV64) {
4854
const CPUInfo *Info = getCPUInfoByName(CPU);
4955

llvm/utils/TableGen/RISCVTargetDefEmitter.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static std::string getMArch(const Record &Rec) {
4949

5050
static void EmitRISCVTargetDef(RecordKeeper &RK, raw_ostream &OS) {
5151
OS << "#ifndef PROC\n"
52-
<< "#define PROC(ENUM, NAME, DEFAULT_MARCH)\n"
52+
<< "#define PROC(ENUM, NAME, DEFAULT_MARCH, FAST_UNALIGNED_ACCESS)\n"
5353
<< "#endif\n\n";
5454

5555
// Iterate on all definition records.
@@ -60,9 +60,14 @@ static void EmitRISCVTargetDef(RecordKeeper &RK, raw_ostream &OS) {
6060
if (MArch.empty())
6161
MArch = getMArch(*Rec);
6262

63+
const bool FastUnalignedAccess =
64+
any_of(Rec->getValueAsListOfDefs("Features"), [&](auto &Feature) {
65+
return Feature->getValueAsString("Name") == "unaligned-scalar-mem";
66+
});
67+
6368
OS << "PROC(" << Rec->getName() << ", "
6469
<< "{\"" << Rec->getValueAsString("Name") << "\"}, "
65-
<< "{\"" << MArch << "\"})\n";
70+
<< "{\"" << MArch << "\"}, " << FastUnalignedAccess << ")\n";
6671
}
6772
OS << "\n#undef PROC\n";
6873
OS << "\n";

0 commit comments

Comments
 (0)