Skip to content

Commit 8c1ca6a

Browse files
committed
[clang] Add driver support for riscv64-mti toolchains
1 parent 513b1ae commit 8c1ca6a

File tree

4 files changed

+67
-8
lines changed

4 files changed

+67
-8
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,12 +337,14 @@ std::string riscv::getRISCVArch(const llvm::opt::ArgList &Args,
337337
// - On `riscv{XLEN}-unknown-elf` we default to `rv{XLEN}imac`
338338
// - On all other OSs we use `rv{XLEN}imafdc` (equivalent to `rv{XLEN}gc`)
339339
if (Triple.isRISCV32()) {
340-
if (Triple.getOS() == llvm::Triple::UnknownOS)
340+
if (Triple.getOS() == llvm::Triple::UnknownOS &&
341+
Triple.getVendor() != llvm::Triple::MipsTechnologies)
341342
return "rv32imac";
342343
return "rv32imafdc";
343344
}
344345

345-
if (Triple.getOS() == llvm::Triple::UnknownOS)
346+
if (Triple.getOS() == llvm::Triple::UnknownOS &&
347+
Triple.getVendor() != llvm::Triple::MipsTechnologies)
346348
return "rv64imac";
347349
if (Triple.isAndroid())
348350
return "rv64imafdcv_zba_zbb_zbs";

clang/lib/Driver/ToolChains/Gnu.cpp

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,11 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
402402
CmdArgs.push_back(Arch == llvm::Triple::aarch64_be ? "-EB" : "-EL");
403403
}
404404

405+
if (Triple.isRISCV() &&
406+
Triple.getVendor() == llvm::Triple::MipsTechnologies) {
407+
CmdArgs.push_back("-EL");
408+
}
409+
405410
// Most Android ARM64 targets should enable the linker fix for erratum
406411
// 843419. Only non-Cortex-A53 devices are allowed to skip this flag.
407412
if (Arch == llvm::Triple::aarch64 && (isAndroid || isOHOSFamily)) {
@@ -765,7 +770,8 @@ void tools::gnutools::Assembler::ConstructJob(Compilation &C,
765770
}
766771
case llvm::Triple::riscv32:
767772
case llvm::Triple::riscv64: {
768-
StringRef ABIName = riscv::getRISCVABI(Args, getToolChain().getTriple());
773+
const llvm::Triple &Triple = getToolChain().getTriple();
774+
StringRef ABIName = riscv::getRISCVABI(Args, Triple);
769775
CmdArgs.push_back("-mabi");
770776
CmdArgs.push_back(ABIName.data());
771777
std::string MArchName =
@@ -774,6 +780,10 @@ void tools::gnutools::Assembler::ConstructJob(Compilation &C,
774780
CmdArgs.push_back(Args.MakeArgString(MArchName));
775781
if (!Args.hasFlag(options::OPT_mrelax, options::OPT_mno_relax, true))
776782
Args.addOptOutFlag(CmdArgs, options::OPT_mrelax, options::OPT_mno_relax);
783+
784+
if (Triple.getVendor() == llvm::Triple::MipsTechnologies)
785+
CmdArgs.push_back("-EL");
786+
777787
break;
778788
}
779789
case llvm::Triple::sparc:
@@ -1824,9 +1834,19 @@ static void findRISCVBareMetalMultilibs(const Driver &D,
18241834
.flag(Twine("-march=", Element.march).str())
18251835
.flag(Twine("-mabi=", Element.mabi).str()));
18261836
}
1837+
SmallVector<MultilibBuilder, 2> Endian;
1838+
bool IsMIPS = TargetTriple.getVendor() == llvm::Triple::MipsTechnologies;
1839+
if (IsMIPS) {
1840+
Endian.push_back(
1841+
MultilibBuilder("/riscv").flag("-EL").flag("-EB", /*Disallow=*/true));
1842+
Endian.push_back(
1843+
MultilibBuilder("/riscveb").flag("-EB").flag("-EL", /*Disallow=*/true));
1844+
}
18271845
MultilibSet RISCVMultilibs =
18281846
MultilibSetBuilder()
18291847
.Either(Ms)
1848+
.Either(Endian)
1849+
.Either(ArrayRef<MultilibBuilder>(Ms))
18301850
.makeMultilibSet()
18311851
.FilterOut(NonExistent)
18321852
.setFilePathsCallback([](const Multilib &M) {
@@ -1850,6 +1870,8 @@ static void findRISCVBareMetalMultilibs(const Driver &D,
18501870
}
18511871
}
18521872

1873+
addMultilibFlag(IsMIPS, "-EL", Flags);
1874+
18531875
if (selectRISCVMultilib(D, RISCVMultilibs, MArch, Flags,
18541876
Result.SelectedMultilibs))
18551877
Result.Multilibs = RISCVMultilibs;
@@ -1874,15 +1896,26 @@ static void findRISCVMultilibs(const Driver &D,
18741896
MultilibBuilder("lib64/lp64f").flag("-m64").flag("-mabi=lp64f");
18751897
MultilibBuilder Lp64d =
18761898
MultilibBuilder("lib64/lp64d").flag("-m64").flag("-mabi=lp64d");
1899+
1900+
SmallVector<MultilibBuilder, 2> Endian;
1901+
if (TargetTriple.getVendor() == llvm::Triple::MipsTechnologies) {
1902+
Endian.push_back(
1903+
MultilibBuilder("/riscv").flag("-EL").flag("-EB", /*Disallow=*/true));
1904+
Endian.push_back(
1905+
MultilibBuilder("/riscveb").flag("-EB").flag("-EL", /*Disallow=*/true));
1906+
}
1907+
18771908
MultilibSet RISCVMultilibs =
18781909
MultilibSetBuilder()
1910+
.Either(Endian)
18791911
.Either({Ilp32, Ilp32f, Ilp32d, Lp64, Lp64f, Lp64d})
18801912
.makeMultilibSet()
18811913
.FilterOut(NonExistent);
18821914

18831915
Multilib::flags_list Flags;
18841916
bool IsRV64 = TargetTriple.getArch() == llvm::Triple::riscv64;
18851917
StringRef ABIName = tools::riscv::getRISCVABI(Args, TargetTriple);
1918+
bool IsMIPS = TargetTriple.getVendor() == llvm::Triple::MipsTechnologies;
18861919

18871920
addMultilibFlag(!IsRV64, "-m32", Flags);
18881921
addMultilibFlag(IsRV64, "-m64", Flags);
@@ -1892,6 +1925,7 @@ static void findRISCVMultilibs(const Driver &D,
18921925
addMultilibFlag(ABIName == "lp64", "-mabi=lp64", Flags);
18931926
addMultilibFlag(ABIName == "lp64f", "-mabi=lp64f", Flags);
18941927
addMultilibFlag(ABIName == "lp64d", "-mabi=lp64d", Flags);
1928+
addMultilibFlag(IsMIPS, "-EL", Flags);
18951929

18961930
if (RISCVMultilibs.select(D, Flags, Result.SelectedMultilibs))
18971931
Result.Multilibs = RISCVMultilibs;
@@ -2516,8 +2550,8 @@ void Generic_GCC::GCCInstallationDetector::AddDefaultGCCPrefixes(
25162550
static const char *const RISCV32Triples[] = {"riscv32-unknown-linux-gnu",
25172551
"riscv32-unknown-elf"};
25182552
static const char *const RISCV64LibDirs[] = {"/lib64", "/lib"};
2519-
static const char *const RISCV64Triples[] = {"riscv64-unknown-linux-gnu",
2520-
"riscv64-unknown-elf"};
2553+
static const char *const RISCV64Triples[] = {
2554+
"riscv64-unknown-linux-gnu", "riscv64-unknown-elf", "riscv64-mti-elf"};
25212555

25222556
static const char *const SPARCv8LibDirs[] = {"/lib32", "/lib"};
25232557
static const char *const SPARCv8Triples[] = {"sparc-linux-gnu",

clang/lib/Driver/ToolChains/Linux.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,14 @@ Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
283283
const bool IsHexagon = Arch == llvm::Triple::hexagon;
284284
const bool IsRISCV = Triple.isRISCV();
285285
const bool IsCSKY = Triple.isCSKY();
286+
const bool IsMipsSysRoot =
287+
IsMips ||
288+
(IsRISCV && Triple.getVendor() == llvm::Triple::MipsTechnologies);
286289

287290
if (IsCSKY && !SelectedMultilibs.empty())
288291
SysRoot = SysRoot + SelectedMultilibs.back().osSuffix();
289292

290-
if ((IsMips || IsCSKY) && !SysRoot.empty())
293+
if ((IsMipsSysRoot || IsCSKY) && !SysRoot.empty())
291294
ExtraOpts.push_back("--sysroot=" + SysRoot);
292295

293296
// Do not use 'gnu' hash style for Mips targets because .gnu.hash
@@ -414,7 +417,12 @@ std::string Linux::computeSysRoot() const {
414417
return std::string();
415418
}
416419

417-
if (!GCCInstallation.isValid() || !getTriple().isMIPS())
420+
const bool IsMipsSysRoot =
421+
getTriple().isMIPS() ||
422+
(getTriple().isRISCV() &&
423+
getTriple().getVendor() == llvm::Triple::MipsTechnologies);
424+
425+
if (!GCCInstallation.isValid() || !IsMipsSysRoot)
418426
return std::string();
419427

420428
// Standalone MIPS toolchains use different names for sysroot folder
@@ -424,8 +432,19 @@ std::string Linux::computeSysRoot() const {
424432
const StringRef InstallDir = GCCInstallation.getInstallPath();
425433
const StringRef TripleStr = GCCInstallation.getTriple().str();
426434
const Multilib &Multilib = GCCInstallation.getMultilib();
435+
std::string Path;
436+
if (getTriple().isRISCV()) {
437+
Path =
438+
(InstallDir + "/../../../../sysroot" + Multilib.osSuffix() + "/../..")
439+
.str();
440+
441+
if (getVFS().exists(Path))
442+
return Path;
443+
444+
return std::string();
445+
}
427446

428-
std::string Path =
447+
Path =
429448
(InstallDir + "/../../../../" + TripleStr + "/libc" + Multilib.osSuffix())
430449
.str();
431450

clang/lib/Driver/ToolChains/RISCVToolchain.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ void RISCV::Linker::ConstructJob(Compilation &C, const JobAction &JA,
173173
}
174174
CmdArgs.push_back("-X");
175175

176+
if (ToolChain.getTriple().getVendor() == llvm::Triple::MipsTechnologies)
177+
CmdArgs.push_back("-EL");
178+
176179
std::string Linker = getToolChain().GetLinkerPath();
177180

178181
bool WantCRTs =
@@ -229,4 +232,5 @@ void RISCV::Linker::ConstructJob(Compilation &C, const JobAction &JA,
229232
JA, *this, ResponseFileSupport::AtFileCurCP(), Args.MakeArgString(Linker),
230233
CmdArgs, Inputs, Output));
231234
}
235+
232236
// RISCV tools end.

0 commit comments

Comments
 (0)