Skip to content

Commit e53bc2f

Browse files
Ami-zhangcaiwei
andcommitted
[clang][LoongArch] Add OHOS target
Add support for OHOS on LoongArch. This patch is taken from part of the https://gitee.com/openharmony/third_party_llvm-project/pulls/554, the original author is @caiwei. Co-authored-by: caiwei <[email protected]>
1 parent 09d1414 commit e53bc2f

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

clang/lib/Basic/Targets.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -741,16 +741,32 @@ std::unique_ptr<TargetInfo> AllocateTarget(const llvm::Triple &Triple,
741741
case llvm::Triple::loongarch32:
742742
switch (os) {
743743
case llvm::Triple::Linux:
744-
return std::make_unique<LinuxTargetInfo<LoongArch32TargetInfo>>(Triple,
745-
Opts);
744+
// OHOS_LOCAL begin
745+
switch (Triple.getEnvironment()) {
746+
default:
747+
return std::make_unique<LinuxTargetInfo<LoongArch32TargetInfo>>(Triple,
748+
Opts);
749+
case llvm::Triple::OpenHOS:
750+
return std::make_unique<OHOSTargetInfo<LoongArch32TargetInfo>>(Triple,
751+
Opts);
752+
}
753+
// OHOS_LOCAL end
746754
default:
747755
return std::make_unique<LoongArch32TargetInfo>(Triple, Opts);
748756
}
749757
case llvm::Triple::loongarch64:
750758
switch (os) {
751759
case llvm::Triple::Linux:
752-
return std::make_unique<LinuxTargetInfo<LoongArch64TargetInfo>>(Triple,
753-
Opts);
760+
// OHOS_LOCAL begin
761+
switch (Triple.getEnvironment()) {
762+
default:
763+
return std::make_unique<LinuxTargetInfo<LoongArch64TargetInfo>>(Triple,
764+
Opts);
765+
case llvm::Triple::OpenHOS:
766+
return std::make_unique<OHOSTargetInfo<LoongArch64TargetInfo>>(Triple,
767+
Opts);
768+
}
769+
// OHOS_LOCAL end
754770
case llvm::Triple::FreeBSD:
755771
return std::make_unique<FreeBSDTargetInfo<LoongArch64TargetInfo>>(Triple,
756772
Opts);

clang/lib/Driver/ToolChains/Gnu.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2458,7 +2458,8 @@ void Generic_GCC::GCCInstallationDetector::AddDefaultGCCPrefixes(
24582458

24592459
static const char *const LoongArch64LibDirs[] = {"/lib64", "/lib"};
24602460
static const char *const LoongArch64Triples[] = {
2461-
"loongarch64-linux-gnu", "loongarch64-unknown-linux-gnu"};
2461+
"loongarch64-linux-gnu", "loongarch64-unknown-linux-gnu",
2462+
"loongarch64-linux-ohos"}; // OHOS_LOCAL
24622463

24632464
static const char *const M68kLibDirs[] = {"/lib"};
24642465
static const char *const M68kTriples[] = {"m68k-unknown-linux-gnu",

clang/lib/Driver/ToolChains/OHOS.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ std::string OHOS::getMultiarchTriple(const llvm::Triple &T) const {
111111
return "x86_64-linux-ohos";
112112
case llvm::Triple::aarch64:
113113
return "aarch64-linux-ohos";
114+
// OHOS_LOCAL begin
115+
case llvm::Triple::loongarch64:
116+
return "loongarch64-linux-ohos";
117+
// OHOS_LOCAL end
114118
}
115119
return T.str();
116120
}
@@ -368,7 +372,14 @@ void OHOS::addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const {
368372
CmdArgs.push_back("-z");
369373
CmdArgs.push_back("relro");
370374
CmdArgs.push_back("-z");
371-
CmdArgs.push_back("max-page-size=4096");
375+
// OHOS_LOCAL begin
376+
// LoongArch needs page size 16K
377+
if (getArch() == llvm::Triple::loongarch64) {
378+
CmdArgs.push_back("max-page-size=16384");
379+
} else {
380+
CmdArgs.push_back("max-page-size=4096");
381+
}
382+
// OHOS_LOCAL end
372383
// .gnu.hash section is not compatible with the MIPS target
373384
if (getArch() != llvm::Triple::mipsel)
374385
CmdArgs.push_back("--hash-style=both");

clang/test/Preprocessor/ohos.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
// RUN: %clang_cc1 -x c++ -E -dM -ffreestanding -triple=riscv64-linux-ohos < /dev/null | FileCheck %s -match-full-lines -check-prefix=RISCV64-OHOS-CXX
44
// RUN: %clang_cc1 -x c++ -E -dM -ffreestanding -triple=mipsel-linux-ohos < /dev/null | FileCheck %s -match-full-lines -check-prefix=MIPSEL-OHOS-CXX
55
// RUN: %clang_cc1 -x c++ -E -dM -ffreestanding -triple=x86_64-linux-ohos < /dev/null | FileCheck %s -match-full-lines -check-prefix=X86_64-OHOS-CXX
6+
// RUN: %clang_cc1 -x c++ -E -dM -ffreestanding -triple=loongarch64-linux-ohos < /dev/null | FileCheck %s -match-full-lines -check-prefix=LOONGARCH64-OHOS-CXX
67
// RUN: %clang_cc1 -E -dM -ffreestanding -triple=arm-linux-ohos < /dev/null | FileCheck %s -check-prefix=OHOS-DEFS
78

89
// ARM-OHOS-CXX: #define __STDCPP_DEFAULT_NEW_ALIGNMENT__ 8U
910
// ARM64-OHOS-CXX: #define __STDCPP_DEFAULT_NEW_ALIGNMENT__ 16UL
1011
// RISCV64-OHOS-CXX: #define __STDCPP_DEFAULT_NEW_ALIGNMENT__ 16UL
1112
// MIPSEL-OHOS-CXX: #define __STDCPP_DEFAULT_NEW_ALIGNMENT__ 8U
1213
// X86_64-OHOS-CXX: #define __STDCPP_DEFAULT_NEW_ALIGNMENT__ 16UL
14+
// LOONGARCH64-OHOS-CXX: #define __STDCPP_DEFAULT_NEW_ALIGNMENT__ 16UL
1315
// OHOS-DEFS: __OHOS_FAMILY__
1416
// OHOS-DEFS: __OHOS__
1517
// OHOS-DEFS-NOT: __OHOS__

0 commit comments

Comments
 (0)