Skip to content

Commit 3892c45

Browse files
committed
[slightly revise]
1 parent cb33ca6 commit 3892c45

File tree

3 files changed

+66
-84
lines changed

3 files changed

+66
-84
lines changed

driver/targetmachine.cpp

+25-29
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,20 @@ static llvm::cl::opt<bool, true> preserveDwarfLineSection(
5757
llvm::cl::init(false));
5858
#endif
5959

60-
const char *getABI(const llvm::Triple &triple, const llvm::SmallVector<llvm::StringRef, 8> &features) {
60+
// Returns true if 'feature' is enabled and false otherwise. Handles the
61+
// case where the feature is specified multiple times ('+m,-m'), and
62+
// takes the last occurrence.
63+
bool isFeatureEnabled(const llvm::SmallVectorImpl<llvm::StringRef> &features,
64+
llvm::StringRef feature) {
65+
for (auto it = features.rbegin(), end = features.rend(); it != end; ++it) {
66+
if (it->substr(1) == feature) {
67+
return (*it)[0] == '+';
68+
}
69+
}
70+
return false;
71+
};
72+
73+
const char *getABI(const llvm::Triple &triple, const llvm::SmallVectorImpl<llvm::StringRef> &features) {
6174
llvm::StringRef ABIName(opts::mABI);
6275
if (ABIName != "") {
6376
switch (triple.getArch()) {
@@ -111,17 +124,6 @@ const char *getABI(const llvm::Triple &triple, const llvm::SmallVector<llvm::Str
111124
ABIName.str().c_str());
112125
}
113126

114-
// checks if the features include ±<feature>
115-
auto hasFeature = [&features](llvm::StringRef feature) {
116-
for (int i = features.size() - 1; i >= 0; i--) {
117-
auto f = features[i];
118-
if (f.substr(1) == feature) {
119-
return f[0] == '+';
120-
}
121-
}
122-
return false;
123-
};
124-
125127
switch (triple.getArch()) {
126128
case llvm::Triple::mips64:
127129
case llvm::Triple::mips64el:
@@ -131,9 +133,9 @@ const char *getABI(const llvm::Triple &triple, const llvm::SmallVector<llvm::Str
131133
case llvm::Triple::ppc64le:
132134
return "elfv2";
133135
case llvm::Triple::riscv64:
134-
if (hasFeature("d"))
136+
if (isFeatureEnabled(features, "d"))
135137
return "lp64d";
136-
if (hasFeature("f"))
138+
if (isFeatureEnabled(features, "f"))
137139
return "lp64f";
138140
return "lp64";
139141
case llvm::Triple::riscv32:
@@ -445,13 +447,9 @@ createTargetMachine(const std::string targetTriple, const std::string arch,
445447

446448
// checks if the features include ±<feature>
447449
auto hasFeature = [&features](llvm::StringRef feature) {
448-
for (int i = features.size() - 1; i >= 0; i--) {
449-
auto f = features[i];
450-
if (f.substr(1) == feature) {
451-
return f[0] == '+';
452-
}
453-
}
454-
return false;
450+
return std::any_of(
451+
features.begin(), features.end(),
452+
[feature](llvm::StringRef f) { return f.substr(1) == feature; });
455453
};
456454

457455
// cmpxchg16b is not available on old 64bit CPUs. Enable code generation
@@ -462,14 +460,12 @@ createTargetMachine(const std::string targetTriple, const std::string arch,
462460

463461
// For a hosted RISC-V 64-bit target default to rv64gc if nothing has
464462
// been selected
465-
if (triple.getArch() == llvm::Triple::riscv64 &&
466-
triple.getOS() != llvm::Triple::UnknownOS &&
467-
features.empty()) {
468-
features.push_back("+m");
469-
features.push_back("+a");
470-
features.push_back("+f");
471-
features.push_back("+d");
472-
features.push_back("+c");
463+
if (triple.getArch() == llvm::Triple::riscv64 && features.empty()) {
464+
const llvm::StringRef os = triple.getOSName();
465+
const bool isFreeStanding = os.empty() || os == "unknown" || os == "none";
466+
if (!isFreeStanding) {
467+
features.insert(features.end(), {"+m", "+a", "+f", "+d", "+c"});
468+
}
473469
}
474470

475471
// Handle cases where LLVM picks wrong default relocModel

driver/targetmachine.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,7 @@ const llvm::Target *lookupTarget(const std::string &arch, llvm::Triple &triple,
7676
std::string &errorMsg);
7777

7878
const char *getABI(const llvm::Triple &triple,
79-
const llvm::SmallVector<llvm::StringRef, 8> &features);
79+
const llvm::SmallVectorImpl<llvm::StringRef> &features);
80+
81+
bool isFeatureEnabled(const llvm::SmallVectorImpl<llvm::StringRef> &features,
82+
llvm::StringRef feature);

driver/tool.cpp

+37-54
Original file line numberDiff line numberDiff line change
@@ -121,62 +121,45 @@ void appendTargetArgsForGcc(std::vector<std::string> &args) {
121121
}
122122
return;
123123

124-
case Triple::riscv64:
125-
{
126-
extern llvm::TargetMachine* gTargetMachine;
127-
auto featuresStr = gTargetMachine->getTargetFeatureString();
128-
llvm::SmallVector<llvm::StringRef, 8> features;
129-
featuresStr.split(features, ",", -1, false);
130-
131-
std::string mabi = getABI(triple, features);
132-
args.push_back("-mabi=" + mabi);
133-
134-
// Returns true if 'feature' is enabled and false otherwise. Handles the
135-
// case where the feature is specified multiple times ('+m,-m'), and
136-
// takes the last occurrence.
137-
auto hasFeature = [&features](llvm::StringRef feature) {
138-
for (int i = features.size() - 1; i >= 0; i--) {
139-
auto f = features[i];
140-
if (f.substr(1) == feature) {
141-
return f[0] == '+';
142-
}
143-
}
144-
return false;
145-
};
146-
147-
std::string march;
148-
if (triple.isArch64Bit())
149-
march = "rv64";
150-
else
151-
march = "rv32";
152-
bool m = hasFeature("m");
153-
bool a = hasFeature("a");
154-
bool f = hasFeature("f");
155-
bool d = hasFeature("d");
156-
bool c = hasFeature("c");
157-
bool g = false;
158-
159-
if (m && a && f && d) {
160-
march += "g";
161-
g = true;
162-
} else {
163-
march += "i";
164-
if (m)
165-
march += "m";
166-
if (a)
167-
march += "a";
168-
if (f)
169-
march += "f";
170-
if (d)
171-
march += "d";
172-
}
173-
if (c)
174-
march += "c";
175-
if (!g)
176-
march += "_zicsr_zifencei";
177-
args.push_back("-march=" + march);
124+
case Triple::riscv64: {
125+
extern llvm::TargetMachine* gTargetMachine;
126+
const auto featuresStr = gTargetMachine->getTargetFeatureString();
127+
llvm::SmallVector<llvm::StringRef, 8> features;
128+
featuresStr.split(features, ",", -1, false);
129+
130+
const std::string mabi = getABI(triple, features);
131+
args.push_back("-mabi=" + mabi);
132+
133+
std::string march = triple.isArch64Bit() ? "rv64" : "rv32";
134+
const bool m = isFeatureEnabled(features, "m");
135+
const bool a = isFeatureEnabled(features, "a");
136+
const bool f = isFeatureEnabled(features, "f");
137+
const bool d = isFeatureEnabled(features, "d");
138+
const bool c = isFeatureEnabled(features, "c");
139+
bool g = false;
140+
141+
if (m && a && f && d) {
142+
march += "g";
143+
g = true;
144+
} else {
145+
march += "i";
146+
if (m)
147+
march += "m";
148+
if (a)
149+
march += "a";
150+
if (f)
151+
march += "f";
152+
if (d)
153+
march += "d";
178154
}
155+
if (c)
156+
march += "c";
157+
if (!g)
158+
march += "_zicsr_zifencei";
159+
args.push_back("-march=" + march);
179160
return;
161+
}
162+
180163
default:
181164
break;
182165
}

0 commit comments

Comments
 (0)