Skip to content

Conversation

nerfur
Copy link
Contributor

@nerfur nerfur commented Jul 20, 2025

Adding Loongarch64 to OpenBSD parts

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@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 Jul 20, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 20, 2025

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-driver

Author: Slava "nerfur" Voronzoff (nerfur)

Changes

Adding Loongarch64 to OpenBSD parts


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

4 Files Affected:

  • (modified) clang/lib/Basic/Targets.cpp (+3)
  • (modified) clang/lib/Basic/Targets/OSTargets.h (+1)
  • (modified) clang/lib/Driver/ToolChains/OpenBSD.cpp (+6)
  • (modified) clang/test/Driver/openbsd.c (+13)
diff --git a/clang/lib/Basic/Targets.cpp b/clang/lib/Basic/Targets.cpp
index 5c2af9b080b83..e3f9760ac7ce3 100644
--- a/clang/lib/Basic/Targets.cpp
+++ b/clang/lib/Basic/Targets.cpp
@@ -757,6 +757,9 @@ std::unique_ptr<TargetInfo> AllocateTarget(const llvm::Triple &Triple,
     case llvm::Triple::FreeBSD:
       return std::make_unique<FreeBSDTargetInfo<LoongArch64TargetInfo>>(Triple,
                                                                         Opts);
+    case llvm::Triple::OpenBSD:
+      return std::make_unique<OpenBSDTargetInfo<LoongArch64TargetInfo>>(Triple,
+                                                                        Opts);
     default:
       return std::make_unique<LoongArch64TargetInfo>(Triple, Opts);
     }
diff --git a/clang/lib/Basic/Targets/OSTargets.h b/clang/lib/Basic/Targets/OSTargets.h
index 42cff6540c5e3..7f55b1dcd42f4 100644
--- a/clang/lib/Basic/Targets/OSTargets.h
+++ b/clang/lib/Basic/Targets/OSTargets.h
@@ -497,6 +497,7 @@ class LLVM_LIBRARY_VISIBILITY OpenBSDTargetInfo : public OSTargetInfo<Target> {
       this->MCountName = "_mcount";
       break;
     case llvm::Triple::riscv64:
+    case llvm::Triple::loongarch64:
       break;
     }
   }
diff --git a/clang/lib/Driver/ToolChains/OpenBSD.cpp b/clang/lib/Driver/ToolChains/OpenBSD.cpp
index 79b1b6960da1f..05d9242a080a9 100644
--- a/clang/lib/Driver/ToolChains/OpenBSD.cpp
+++ b/clang/lib/Driver/ToolChains/OpenBSD.cpp
@@ -167,6 +167,12 @@ void openbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
       CmdArgs.push_back("--no-relax");
   }
 
+  if (Triple.isLoongArch64()) {
+    CmdArgs.push_back("-X");
+    if (Args.hasArg(options::OPT_mno_relax))
+      CmdArgs.push_back("--no-relax");
+  }
+  
   assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
   if (Output.isFilename()) {
     CmdArgs.push_back("-o");
diff --git a/clang/test/Driver/openbsd.c b/clang/test/Driver/openbsd.c
index 6639e9d2d9d67..0c76249143214 100644
--- a/clang/test/Driver/openbsd.c
+++ b/clang/test/Driver/openbsd.c
@@ -29,6 +29,19 @@
 // CHECK-MIPS64-CPU: "-target-cpu" "mips3"
 // CHECK-MIPS64EL-CPU: "-target-cpu" "mips3"
 
+// Check that LoongArch passes the correct linker emulation.
+//
+// RUN: %clang --target=loongarch64-unknown-openbsd -### %s %s 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-LA64-LD %s
+// CHECK-LA64-LD: ld{{.*}}" {{.*}} "-m" "elf64loongarch"
+//
+// Check options passed to the linker on LoongArch
+//
+// RUN: %clang --target=loongarch64-unknown-openbsd -mno-relax -### %s %s 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-LA64-LD-OPTS %s
+// CHECK-LA64-LD-OPTS: ld{{.*}}" {{.*}} "-X" "--no-relax"
+//
+
 // Check that the new linker flags are passed to OpenBSD
 // RUN: %clang --target=i686-pc-openbsd -r -### %s 2>&1 \
 // RUN:   | FileCheck --check-prefix=CHECK-LD-R %s

if (Args.hasArg(options::OPT_mno_relax))
CmdArgs.push_back("--no-relax");
}

Copy link
Contributor

Choose a reason for hiding this comment

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

You can merge this check with the RISC-V check above this.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also there is a space you added.

@brad0
Copy link
Contributor

brad0 commented Jul 20, 2025

Are you really working on Loongarch64 support?

@@ -497,6 +497,7 @@ class LLVM_LIBRARY_VISIBILITY OpenBSDTargetInfo : public OSTargetInfo<Target> {
this->MCountName = "_mcount";
break;
case llvm::Triple::riscv64:
case llvm::Triple::loongarch64:
break;
Copy link
Contributor

Choose a reason for hiding this comment

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

Also make sure this is sorted, loongarch64 before riscv64.

@nerfur
Copy link
Contributor Author

nerfur commented Jul 21, 2025

Are you really working on Loongarch64 support?

Yes, I was trying to find some people to "sponsor" it (as much as I can do it as simple person), but decided that I can at least start, especially looking at experience of some freebsd guys (where I can start, what to add first, etc)

I will check your other suggestions, thanks

@nerfur
Copy link
Contributor Author

nerfur commented Jul 22, 2025

Did I get your suggestions right?

@brad0
Copy link
Contributor

brad0 commented Jul 22, 2025

Clean up the space added on line 169 in OpenBSD.cpp.

@brad0 brad0 merged commit 5eecbc0 into llvm:main Jul 22, 2025
5 checks passed
Copy link

@nerfur Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

tru pushed a commit to llvmbot/llvm-project that referenced this pull request Jul 28, 2025
Adding Loongarch64 to OpenBSD parts

(cherry picked from commit 5eecbc0)
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Jul 28, 2025
Adding Loongarch64 to OpenBSD parts
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.

3 participants