Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

Ami-zhang
Copy link
Contributor

@Ami-zhang Ami-zhang commented Feb 18, 2025

Add support for OHOS on loongarch64.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Feb 18, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 18, 2025

@llvm/pr-subscribers-clang-driver

@llvm/pr-subscribers-clang

Author: None (Ami-zhang)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/127555.diff

4 Files Affected:

  • (modified) clang/lib/Basic/Targets.cpp (+20-4)
  • (modified) clang/lib/Driver/ToolChains/Gnu.cpp (+2-1)
  • (modified) clang/lib/Driver/ToolChains/OHOS.cpp (+12-1)
  • (modified) clang/test/Preprocessor/ohos.c (+2)
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__

@brad0
Copy link
Contributor

brad0 commented Feb 18, 2025

Remove all of the OHOS_LOCAL begin / end comment markers.

@Ami-zhang Ami-zhang requested a review from brad0 February 18, 2025 07:21
@brad0
Copy link
Contributor

brad0 commented Feb 18, 2025

You have to push any new changes first.

@Ami-zhang
Copy link
Contributor Author

Remove all of the OHOS_LOCAL begin / end comment markers.

Done.
Thank you for your review.

@brad0
Copy link
Contributor

brad0 commented Feb 18, 2025

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
Copy link
Contributor

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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No loongarch32 addition here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah.
Just on loongarch64.

Copy link
Contributor

@SixWeining SixWeining left a 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.

Comment on lines 374 to 378
if (getArch() == llvm::Triple::loongarch64) {
CmdArgs.push_back("max-page-size=16384");
} else {
CmdArgs.push_back("max-page-size=4096");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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");

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, Thanks.

@@ -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
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Contributor

@SixWeining SixWeining left a 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.

Comment on lines 748 to 751
case llvm::Triple::OpenHOS:
return std::make_unique<OHOSTargetInfo<LoongArch32TargetInfo>>(Triple,
Opts);
}
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay.

@@ -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"};
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Member

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be tested

Copy link
Contributor Author

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.
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay.

@kpdev
Copy link
Member

kpdev commented Feb 19, 2025

LGTM for the LoongArch change. We'll see if @kpdev has any objections later.

LGTM also. Thank you for the patch

@@ -0,0 +1,16 @@
/// Check the max-page-size for OHOS aarch64/loongarch64/riscv64/x86_64.
Copy link
Member

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:)

Copy link
Contributor Author

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"
Copy link
Contributor

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.

@SixWeining
Copy link
Contributor

Could you add some tests in ohos.c with the --sysroot option specified?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants