-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[clang][LoongArch] Add OHOS target #127555
base: main
Are you sure you want to change the base?
Conversation
@llvm/pr-subscribers-clang-driver @llvm/pr-subscribers-clang Author: None (Ami-zhang) ChangesAdd support for OHOS on LoongArch. This patch is taken from part of the Full diff: https://github.com/llvm/llvm-project/pull/127555.diff 4 Files Affected:
diff --git a/clang/lib/Basic/Targets.cpp b/clang/lib/Basic/Targets.cpp
index 281aebdb1c35d..f345e3596df39 100644
--- a/clang/lib/Basic/Targets.cpp
+++ b/clang/lib/Basic/Targets.cpp
@@ -741,16 +741,32 @@ std::unique_ptr<TargetInfo> AllocateTarget(const llvm::Triple &Triple,
case llvm::Triple::loongarch32:
switch (os) {
case llvm::Triple::Linux:
- return std::make_unique<LinuxTargetInfo<LoongArch32TargetInfo>>(Triple,
- Opts);
+ // OHOS_LOCAL begin
+ switch (Triple.getEnvironment()) {
+ default:
+ return std::make_unique<LinuxTargetInfo<LoongArch32TargetInfo>>(Triple,
+ Opts);
+ case llvm::Triple::OpenHOS:
+ return std::make_unique<OHOSTargetInfo<LoongArch32TargetInfo>>(Triple,
+ Opts);
+ }
+ // OHOS_LOCAL end
default:
return std::make_unique<LoongArch32TargetInfo>(Triple, Opts);
}
case llvm::Triple::loongarch64:
switch (os) {
case llvm::Triple::Linux:
- return std::make_unique<LinuxTargetInfo<LoongArch64TargetInfo>>(Triple,
- Opts);
+ // OHOS_LOCAL begin
+ switch (Triple.getEnvironment()) {
+ default:
+ return std::make_unique<LinuxTargetInfo<LoongArch64TargetInfo>>(Triple,
+ Opts);
+ case llvm::Triple::OpenHOS:
+ return std::make_unique<OHOSTargetInfo<LoongArch64TargetInfo>>(Triple,
+ Opts);
+ }
+ // OHOS_LOCAL end
case llvm::Triple::FreeBSD:
return std::make_unique<FreeBSDTargetInfo<LoongArch64TargetInfo>>(Triple,
Opts);
diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp
index f56eeda3cb5f6..0b560be1d28e7 100644
--- a/clang/lib/Driver/ToolChains/Gnu.cpp
+++ b/clang/lib/Driver/ToolChains/Gnu.cpp
@@ -2458,7 +2458,8 @@ void Generic_GCC::GCCInstallationDetector::AddDefaultGCCPrefixes(
static const char *const LoongArch64LibDirs[] = {"/lib64", "/lib"};
static const char *const LoongArch64Triples[] = {
- "loongarch64-linux-gnu", "loongarch64-unknown-linux-gnu"};
+ "loongarch64-linux-gnu", "loongarch64-unknown-linux-gnu",
+ "loongarch64-linux-ohos"}; // OHOS_LOCAL
static const char *const M68kLibDirs[] = {"/lib"};
static const char *const M68kTriples[] = {"m68k-unknown-linux-gnu",
diff --git a/clang/lib/Driver/ToolChains/OHOS.cpp b/clang/lib/Driver/ToolChains/OHOS.cpp
index 6e1a09ae908b2..cd4351a2c614d 100644
--- a/clang/lib/Driver/ToolChains/OHOS.cpp
+++ b/clang/lib/Driver/ToolChains/OHOS.cpp
@@ -111,6 +111,10 @@ std::string OHOS::getMultiarchTriple(const llvm::Triple &T) const {
return "x86_64-linux-ohos";
case llvm::Triple::aarch64:
return "aarch64-linux-ohos";
+ // OHOS_LOCAL begin
+ case llvm::Triple::loongarch64:
+ return "loongarch64-linux-ohos";
+ // OHOS_LOCAL end
}
return T.str();
}
@@ -368,7 +372,14 @@ void OHOS::addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const {
CmdArgs.push_back("-z");
CmdArgs.push_back("relro");
CmdArgs.push_back("-z");
- CmdArgs.push_back("max-page-size=4096");
+ // OHOS_LOCAL begin
+ // LoongArch needs page size 16K
+ if (getArch() == llvm::Triple::loongarch64) {
+ CmdArgs.push_back("max-page-size=16384");
+ } else {
+ CmdArgs.push_back("max-page-size=4096");
+ }
+ // OHOS_LOCAL end
// .gnu.hash section is not compatible with the MIPS target
if (getArch() != llvm::Triple::mipsel)
CmdArgs.push_back("--hash-style=both");
diff --git a/clang/test/Preprocessor/ohos.c b/clang/test/Preprocessor/ohos.c
index 0c435c7ed5ab4..7017c9847ccae 100644
--- a/clang/test/Preprocessor/ohos.c
+++ b/clang/test/Preprocessor/ohos.c
@@ -3,6 +3,7 @@
// RUN: %clang_cc1 -x c++ -E -dM -ffreestanding -triple=riscv64-linux-ohos < /dev/null | FileCheck %s -match-full-lines -check-prefix=RISCV64-OHOS-CXX
// RUN: %clang_cc1 -x c++ -E -dM -ffreestanding -triple=mipsel-linux-ohos < /dev/null | FileCheck %s -match-full-lines -check-prefix=MIPSEL-OHOS-CXX
// 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
+// RUN: %clang_cc1 -x c++ -E -dM -ffreestanding -triple=loongarch64-linux-ohos < /dev/null | FileCheck %s -match-full-lines -check-prefix=LOONGARCH64-OHOS-CXX
// RUN: %clang_cc1 -E -dM -ffreestanding -triple=arm-linux-ohos < /dev/null | FileCheck %s -check-prefix=OHOS-DEFS
// ARM-OHOS-CXX: #define __STDCPP_DEFAULT_NEW_ALIGNMENT__ 8U
@@ -10,6 +11,7 @@
// RISCV64-OHOS-CXX: #define __STDCPP_DEFAULT_NEW_ALIGNMENT__ 16UL
// MIPSEL-OHOS-CXX: #define __STDCPP_DEFAULT_NEW_ALIGNMENT__ 8U
// X86_64-OHOS-CXX: #define __STDCPP_DEFAULT_NEW_ALIGNMENT__ 16UL
+// LOONGARCH64-OHOS-CXX: #define __STDCPP_DEFAULT_NEW_ALIGNMENT__ 16UL
// OHOS-DEFS: __OHOS_FAMILY__
// OHOS-DEFS: __OHOS__
// OHOS-DEFS-NOT: __OHOS__
|
Remove all of the OHOS_LOCAL begin / end comment markers. |
You have to push any new changes first. |
Done. |
That looks better. |
@@ -3,13 +3,15 @@ | |||
// RUN: %clang_cc1 -x c++ -E -dM -ffreestanding -triple=riscv64-linux-ohos < /dev/null | FileCheck %s -match-full-lines -check-prefix=RISCV64-OHOS-CXX | |||
// RUN: %clang_cc1 -x c++ -E -dM -ffreestanding -triple=mipsel-linux-ohos < /dev/null | FileCheck %s -match-full-lines -check-prefix=MIPSEL-OHOS-CXX | |||
// 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 | |||
// RUN: %clang_cc1 -x c++ -E -dM -ffreestanding -triple=loongarch64-linux-ohos < /dev/null | FileCheck %s -match-full-lines -check-prefix=LOONGARCH64-OHOS-CXX |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should test loongarch32 here too.
@@ -111,6 +111,8 @@ std::string OHOS::getMultiarchTriple(const llvm::Triple &T) const { | |||
return "x86_64-linux-ohos"; | |||
case llvm::Triple::aarch64: | |||
return "aarch64-linux-ohos"; | |||
case llvm::Triple::loongarch64: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No loongarch32 addition here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah.
Just on loongarch64.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM for the LoongArch change. We'll see if @kpdev has any objections later.
clang/lib/Driver/ToolChains/OHOS.cpp
Outdated
if (getArch() == llvm::Triple::loongarch64) { | ||
CmdArgs.push_back("max-page-size=16384"); | ||
} else { | ||
CmdArgs.push_back("max-page-size=4096"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (getArch() == llvm::Triple::loongarch64) { | |
CmdArgs.push_back("max-page-size=16384"); | |
} else { | |
CmdArgs.push_back("max-page-size=4096"); | |
} | |
CmdArgs.push_back(getArch() == llvm::Triple::loongarch64 | |
? "max-page-size=16384" | |
: "max-page-size=4096"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, Thanks.
clang/lib/Driver/ToolChains/OHOS.cpp
Outdated
@@ -368,7 +370,12 @@ void OHOS::addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const { | |||
CmdArgs.push_back("-z"); | |||
CmdArgs.push_back("relro"); | |||
CmdArgs.push_back("-z"); | |||
CmdArgs.push_back("max-page-size=4096"); | |||
// LoongArch needs page size 16K |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment is unnecessary as the code explains itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should add more tests like clang/test/Driver/loongarch-toolchain.c
and https://reviews.llvm.org/D55029.
clang/lib/Basic/Targets.cpp
Outdated
case llvm::Triple::OpenHOS: | ||
return std::make_unique<OHOSTargetInfo<LoongArch32TargetInfo>>(Triple, | ||
Opts); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this patch should only focus on LoongArch64. LoongArch32 could be added in future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay.
clang/lib/Driver/ToolChains/Gnu.cpp
Outdated
@@ -2458,7 +2458,8 @@ void Generic_GCC::GCCInstallationDetector::AddDefaultGCCPrefixes( | |||
|
|||
static const char *const LoongArch64LibDirs[] = {"/lib64", "/lib"}; | |||
static const char *const LoongArch64Triples[] = { | |||
"loongarch64-linux-gnu", "loongarch64-unknown-linux-gnu"}; | |||
"loongarch64-linux-gnu", "loongarch64-unknown-linux-gnu", | |||
"loongarch64-linux-ohos"}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the ohos triple is added to Gnu.cpp
. Seems that aarch64 and x86-64 don't add them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep it consistent with other architectures and do not add it for now.
Removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove new entries. There is a comment to discourage new entries.
@@ -368,7 +370,9 @@ void OHOS::addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const { | |||
CmdArgs.push_back("-z"); | |||
CmdArgs.push_back("relro"); | |||
CmdArgs.push_back("-z"); | |||
CmdArgs.push_back("max-page-size=4096"); | |||
CmdArgs.push_back(getArch() == llvm::Triple::loongarch64 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be tested
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a test in the latest patch. Thanks!
@@ -0,0 +1,6 @@ | |||
/// Check the max-page-size for OHOS LoongArch. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rename the test like ohos-pagesize.c. Maybe add one other arch and check that the value is 4k.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay.
LGTM also. Thank you for the patch |
clang/test/Driver/ohos-pagesize.c
Outdated
@@ -0,0 +1,16 @@ | |||
/// Check the max-page-size for OHOS aarch64/loongarch64/riscv64/x86_64. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't add a new file for a minor property (though important to test) like page-size. Just reuse ohos.c
Many driver tests are probably not organized in the best way. linux-cross.cpp could give inspiration.
You usually need to rg max-page-size clang/test/Driver
, read multiple tests , and decide how to write the test in the most elegant way:)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed the new file and added the page-size check in ohos.c, following the style of similar tests. Thanks!
Add support for OHOS on loongarch64.
|
||
// CHECK-MAXPAGESIZE-4KB: "-z" "max-page-size=4096" | ||
// CHECK-MAXPAGESIZE-16KB: "-z" "max-page-size=16384" | ||
// CHECK-NO-MAXPAGESIZE-NOT: "-z" "max-page-size=4096" "-z" "max-page-size=16384" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems this is not a good check. This check will always success no matter what the output is. I think the gnu
checks could be removed.
Could you add some tests in ohos.c with the |
Add support for OHOS on loongarch64.