Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions clang/lib/Driver/ToolChains/Arch/RISCV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,3 +367,31 @@ std::string riscv::getRISCVTargetCPU(const llvm::opt::ArgList &Args,

return Triple.isRISCV64() ? "generic-rv64" : "generic-rv32";
}

void riscv::addMtuneWithFeatures(const Arg *A, const ArgList &Args,
ArgStringList &CmdArgs) {
// format: -mtune=<tune cpu>[:+a,-b,+c...]
StringRef MTune(A->getValue());
size_t ColonPos = MTune.find(':');
bool HasColon = ColonPos != StringRef::npos;

StringRef TuneCPU = MTune.take_front(ColonPos);
if (TuneCPU == "native")
CmdArgs.push_back(Args.MakeArgString(llvm::sys::getHostCPUName()));
else
CmdArgs.push_back(HasColon ? Args.MakeArgString(TuneCPU) : A->getValue());

StringRef FeatureStr = HasColon ? MTune.drop_front(ColonPos + 1) : "";
// TODO: Emit error
assert(!HasColon || !FeatureStr.empty());
if (FeatureStr.empty())
return;
StringRef Feature;
do {
std::tie(Feature, FeatureStr) = FeatureStr.split(',');
// TODO: Emit error
assert(Feature.starts_with('-') || Feature.starts_with('+'));
CmdArgs.push_back("-target-feature");
CmdArgs.push_back(Args.MakeArgString(Feature));
} while (!FeatureStr.empty());
}
3 changes: 3 additions & 0 deletions clang/lib/Driver/ToolChains/Arch/RISCV.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ std::string getRISCVArch(const llvm::opt::ArgList &Args,
const llvm::Triple &Triple);
std::string getRISCVTargetCPU(const llvm::opt::ArgList &Args,
const llvm::Triple &Triple);
void addMtuneWithFeatures(const llvm::opt::Arg *A,
const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &CmdArgs);
} // end namespace riscv
} // namespace tools
} // end namespace driver
Expand Down
5 changes: 1 addition & 4 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2046,10 +2046,7 @@ void Clang::AddRISCVTargetArgs(const ArgList &Args,

if (const Arg *A = Args.getLastArg(options::OPT_mtune_EQ)) {
CmdArgs.push_back("-tune-cpu");
if (strcmp(A->getValue(), "native") == 0)
CmdArgs.push_back(Args.MakeArgString(llvm::sys::getHostCPUName()));
else
CmdArgs.push_back(A->getValue());
riscv::addMtuneWithFeatures(A, Args, CmdArgs);
}

// Handle -mrvv-vector-bits=<bits>
Expand Down
5 changes: 5 additions & 0 deletions clang/test/Driver/riscv-cpus.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,11 @@
// RUN: %clang --target=riscv64 -### -c %s 2>&1 -mtune=native | FileCheck -check-prefix=MTUNE-NATIVE %s
// MTUNE-NATIVE-NOT: "-tune-cpu" "native"

// Checking `-mtune=<tune cpu>:+x,-y,+z...`
// RUN: %clang --target=riscv64 -### -c %s 2>&1 -mtune=sifive-e20:+xyz,-abc | FileCheck -check-prefix=MTUNE-WITH-FEATURES %s
// MTUNE-WITH-FEATURES: "-tune-cpu" "sifive-e20"
// MTUNE-WITH-FEATURES: "-target-feature" "+xyz" "-target-feature" "-abc"

// -mcpu with default -march
// RUN: %clang --target=riscv64 -### -c %s 2>&1 -mcpu=sifive-e20 | FileCheck -check-prefix=MCPU-SIFIVE-E20 %s
// MCPU-SIFIVE-E20: "-nostdsysteminc" "-target-cpu" "sifive-e20"
Expand Down