Skip to content

Commit 2d65097

Browse files
authored
[RISCV] Unify all the code that adds unaligned-scalar/vector-mem to Features vector. (#94660)
Instead of having multiple places insert into the Features vector independently, check all the conditions in one place. This avoids a subtle ordering requirement that -mstrict-align processing had to be done after the others.
1 parent 7e7c29b commit 2d65097

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

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

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,6 @@ static void getRISCFeaturesFromMcpu(const Driver &D, const Arg *A,
6767
D.Diag(clang::diag::err_drv_unsupported_option_argument)
6868
<< A->getSpelling() << Mcpu;
6969
}
70-
71-
if (llvm::RISCV::hasFastUnalignedAccess(Mcpu)) {
72-
Features.push_back("+unaligned-scalar-mem");
73-
Features.push_back("+unaligned-vector-mem");
74-
}
7570
}
7671

7772
void riscv::getRISCVTargetFeatures(const Driver &D, const llvm::Triple &Triple,
@@ -82,6 +77,8 @@ void riscv::getRISCVTargetFeatures(const Driver &D, const llvm::Triple &Triple,
8277
if (!getArchFeatures(D, MArch, Features, Args))
8378
return;
8479

80+
bool CPUFastUnaligned = false;
81+
8582
// If users give march and mcpu, get std extension feature from MArch
8683
// and other features (ex. mirco architecture feature) from mcpu
8784
if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
@@ -90,6 +87,9 @@ void riscv::getRISCVTargetFeatures(const Driver &D, const llvm::Triple &Triple,
9087
CPU = llvm::sys::getHostCPUName();
9188

9289
getRISCFeaturesFromMcpu(D, A, Triple, CPU, Features);
90+
91+
if (llvm::RISCV::hasFastUnalignedAccess(CPU))
92+
CPUFastUnaligned = true;
9393
}
9494

9595
// Handle features corresponding to "-ffixed-X" options
@@ -169,18 +169,23 @@ void riscv::getRISCVTargetFeatures(const Driver &D, const llvm::Triple &Triple,
169169
Features.push_back("-relax");
170170
}
171171

172-
// Android requires fast unaligned access on RISCV64.
173-
if (Triple.isAndroid()) {
172+
// If -mstrict-align or -mno-strict-align is passed, use it. Otherwise, the
173+
// unaligned-*-mem is enabled if the CPU supports it or the target is
174+
// Android.
175+
if (const Arg *A = Args.getLastArg(options::OPT_mno_strict_align,
176+
options::OPT_mstrict_align)) {
177+
if (A->getOption().matches(options::OPT_mno_strict_align)) {
178+
Features.push_back("+unaligned-scalar-mem");
179+
Features.push_back("+unaligned-vector-mem");
180+
} else {
181+
Features.push_back("-unaligned-scalar-mem");
182+
Features.push_back("-unaligned-vector-mem");
183+
}
184+
} else if (CPUFastUnaligned || Triple.isAndroid()) {
174185
Features.push_back("+unaligned-scalar-mem");
175186
Features.push_back("+unaligned-vector-mem");
176187
}
177188

178-
// -mstrict-align is default, unless -mno-strict-align is specified.
179-
AddTargetFeature(Args, Features, options::OPT_mno_strict_align,
180-
options::OPT_mstrict_align, "unaligned-scalar-mem");
181-
AddTargetFeature(Args, Features, options::OPT_mno_strict_align,
182-
options::OPT_mstrict_align, "unaligned-vector-mem");
183-
184189
// Now add any that the user explicitly requested on the command line,
185190
// which may override the defaults.
186191
handleTargetFeaturesGroup(D, Triple, Args, Features,

0 commit comments

Comments
 (0)