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 FreeBSD targets #119191

Merged
merged 1 commit into from
Dec 13, 2024
Merged

Conversation

hitmoon
Copy link
Contributor

@hitmoon hitmoon commented Dec 9, 2024

Add support for freebsd on loongarch

Copy link

github-actions bot commented Dec 9, 2024

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 Dec 9, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 9, 2024

@llvm/pr-subscribers-clang-driver

@llvm/pr-subscribers-clang

Author: None (hitmoon)

Changes

Add support for freebsd on loongarch


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

4 Files Affected:

  • (modified) clang/lib/Basic/Targets.cpp (+6)
  • (modified) clang/lib/Basic/Targets/OSTargets.h (+3)
  • (modified) clang/lib/Driver/ToolChains/FreeBSD.cpp (+14)
  • (modified) clang/test/Driver/freebsd.c (+9)
diff --git a/clang/lib/Basic/Targets.cpp b/clang/lib/Basic/Targets.cpp
index 0021d33c45d7c9..98d151cfe4312f 100644
--- a/clang/lib/Basic/Targets.cpp
+++ b/clang/lib/Basic/Targets.cpp
@@ -726,6 +726,9 @@ std::unique_ptr<TargetInfo> AllocateTarget(const llvm::Triple &Triple,
     case llvm::Triple::Linux:
         return std::make_unique<LinuxTargetInfo<LoongArch32TargetInfo>>(Triple,
                                                                         Opts);
+    case llvm::Triple::FreeBSD:
+        return std::make_unique<FreeBSDTargetInfo<LoongArch32TargetInfo>>(Triple,
+                                                                          Opts);
     default:
         return std::make_unique<LoongArch32TargetInfo>(Triple, Opts);
     }
@@ -734,6 +737,9 @@ std::unique_ptr<TargetInfo> AllocateTarget(const llvm::Triple &Triple,
     case llvm::Triple::Linux:
         return std::make_unique<LinuxTargetInfo<LoongArch64TargetInfo>>(Triple,
                                                                         Opts);
+    case llvm::Triple::FreeBSD:
+        return std::make_unique<FreeBSDTargetInfo<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 75f53e96ce28f6..a39d8526e958f3 100644
--- a/clang/lib/Basic/Targets/OSTargets.h
+++ b/clang/lib/Basic/Targets/OSTargets.h
@@ -231,6 +231,9 @@ class LLVM_LIBRARY_VISIBILITY FreeBSDTargetInfo : public OSTargetInfo<Target> {
     case llvm::Triple::riscv32:
     case llvm::Triple::riscv64:
       break;
+    case llvm::Triple::loongarch32:
+    case llvm::Triple::loongarch64:
+      break;
     }
   }
 };
diff --git a/clang/lib/Driver/ToolChains/FreeBSD.cpp b/clang/lib/Driver/ToolChains/FreeBSD.cpp
index 3d744bc087f467..be44fc4fe1a84d 100644
--- a/clang/lib/Driver/ToolChains/FreeBSD.cpp
+++ b/clang/lib/Driver/ToolChains/FreeBSD.cpp
@@ -213,6 +213,14 @@ void freebsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
     CmdArgs.push_back("-m");
     CmdArgs.push_back("elf64lriscv");
     break;
+  case llvm::Triple::loongarch32:
+    CmdArgs.push_back("-m");
+    CmdArgs.push_back("elf32loongarch");
+    break;
+  case llvm::Triple::loongarch64:
+    CmdArgs.push_back("-m");
+    CmdArgs.push_back("elf64loongarch");
+    break;
   default:
     break;
   }
@@ -223,6 +231,12 @@ void freebsd::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");
+  }
+
   if (Arg *A = Args.getLastArg(options::OPT_G)) {
     if (ToolChain.getTriple().isMIPS()) {
       StringRef v = A->getValue();
diff --git a/clang/test/Driver/freebsd.c b/clang/test/Driver/freebsd.c
index 10fe155fee8744..c99beade607343 100644
--- a/clang/test/Driver/freebsd.c
+++ b/clang/test/Driver/freebsd.c
@@ -77,6 +77,15 @@
 // RUN:   | FileCheck --check-prefix=CHECK-RV64I-LD %s
 // CHECK-RV64I-LD: ld{{.*}}" {{.*}} "-m" "elf64lriscv"
 //
+// Check that LOONGARCH passes the correct linker emulation.
+//
+// RUN: %clang --target=loongarch32-freebsd -### %s %s 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-LOONG32-LD %s
+// CHECK-LOONG32-LD: ld{{.*}}" {{.*}} "-m" "elf32loongarch"
+// RUN: %clang --target=loongarch64-freebsd -### %s %s 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-LOONG64-LD %s
+// CHECK-LOONG64-LD: ld{{.*}}" {{.*}} "-m" "elf64loongarch"
+//
 // Check that the new linker flags are passed to FreeBSD
 // RUN: %clang --target=x86_64-pc-freebsd10.0 -m32 %s \
 // RUN:   --sysroot=%S/Inputs/multiarch_freebsd64_tree -### 2>&1 \

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.

Can FreeBSD be built with this PR?

Copy link

github-actions bot commented Dec 10, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@hitmoon
Copy link
Contributor Author

hitmoon commented Dec 10, 2024

Can FreeBSD be built with this PR?

Yes, tested by building kernel-toolchain

@hitmoon hitmoon force-pushed the llvm_loong_freebsd branch 2 times, most recently from 1f33d76 to 975aed8 Compare December 11, 2024 05:46
@SixWeining
Copy link
Contributor

LGTM from the LoongArch side. It would be better a FreeBSD expert could have a look. But if there is no obvious objections I'd like to merge and mention it in LLVM20's release notes. Thanks! @hitmoon Looking forward to hear more news about the FreeBSD-LoongArch porting progress.

@hitmoon
Copy link
Contributor Author

hitmoon commented Dec 11, 2024

LGTM from the LoongArch side. It would be better a FreeBSD expert could have a look. But if there is no obvious objections I'd like to merge and mention it in LLVM20's release notes. Thanks! @hitmoon Looking forward to hear more news about the FreeBSD-LoongArch porting progress.

@SixWeining Thanks for the review !

@xen0n
Copy link
Contributor

xen0n commented Dec 11, 2024

Also a [Clang] tag could be prepended to the PR title to make it clearer what part gets changed.

@yushanwei
Copy link
Contributor

Also a [Clang] tag could be prepended to the PR title to make it clearer what part gets changed.

Not only clang, all of FreeBSD on LoongArch

Co-authored-by: yu shan wei <[email protected]>
Signed-off-by: xiaoqiang zhao <[email protected]>
@hitmoon hitmoon changed the title [LoongArch] Add FreeBSD targets [clang][LoongArch] Add FreeBSD targets Dec 12, 2024
@hitmoon
Copy link
Contributor Author

hitmoon commented Dec 12, 2024

clang tag added

@xen0n
Copy link
Contributor

xen0n commented Dec 12, 2024

Also a [Clang] tag could be prepended to the PR title to make it clearer what part gets changed.

Not only clang, all of FreeBSD on LoongArch

It's not about what can get built after the change, but what part of LLVM project this change applies to, which is Clang. Look at the diff -- all touched files reside in clang/ directory, that's it.

@@ -231,6 +231,9 @@ class LLVM_LIBRARY_VISIBILITY FreeBSDTargetInfo : public OSTargetInfo<Target> {
case llvm::Triple::riscv32:
case llvm::Triple::riscv64:
break;
case llvm::Triple::loongarch32:
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm of two minds about adding loongarch32 triples. It's harmless and parallels riscv, but the chances of a new 32-bit port being importing into the FreeBSD are close to zero.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@brooksdavis Thanks!
after have reading Future of 32-bit platform support in FreeBSD , I think It's reasonable to exclude 32 bit from LoongArch support.
I also hope to hear some comments/suggestions from @emaste

Copy link
Contributor

Choose a reason for hiding this comment

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

@brooksdavis 32-bit RISC-V support was removed for FreeBSD. I just noticed this addition and think it should be removed.

Copy link
Collaborator

@DimitryAndric DimitryAndric left a comment

Choose a reason for hiding this comment

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

I don't have any experience with LoongArch, but this diff looks fine to me.

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

@SixWeining SixWeining merged commit 3b10e31 into llvm:main Dec 13, 2024
7 checks passed
Copy link

@hitmoon 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!

@llvm-ci
Copy link
Collaborator

llvm-ci commented Dec 13, 2024

LLVM Buildbot has detected a new failure on builder llvm-clang-aarch64-darwin running on doug-worker-4 while building clang at step 6 "test-build-unified-tree-check-all".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/190/builds/11277

Here is the relevant piece of the build log for the reference
Step 6 (test-build-unified-tree-check-all) failure: test (failure)
...
PASS: lld :: MachO/arm64-objc-stubs-fix.s (79801 of 80343)
PASS: lld :: MachO/arm64-objc-stubs.s (79802 of 80343)
PASS: lld :: MachO/arm64-reloc-got-load.s (79803 of 80343)
PASS: lld :: MachO/arm64-reloc-pointer-to-got.s (79804 of 80343)
PASS: lld :: MachO/arm64-reloc-tlv-load.s (79805 of 80343)
PASS: lld :: MachO/arm64-relocs.s (79806 of 80343)
PASS: lld :: MachO/arm64-stubs.s (79807 of 80343)
PASS: lit :: shtest-define.py (79808 of 80343)
PASS: lld :: ELF/aarch64-thunk-reuse2.s (79809 of 80343)
UNRESOLVED: lld :: MachO/arm64-thunk-visibility.s (79810 of 80343)
******************** TEST 'lld :: MachO/arm64-thunk-visibility.s' FAILED ********************
Exception during script execution:
Traceback (most recent call last):
  File "/Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/llvm/utils/lit/lit/worker.py", line 76, in _execute_test_handle_errors
    result = test.config.test_format.execute(test, lit_config)
  File "/Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/llvm/utils/lit/lit/formats/shtest.py", line 29, in execute
    return lit.TestRunner.executeShTest(
  File "/Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/llvm/utils/lit/lit/TestRunner.py", line 2326, in executeShTest
    return _runShTest(test, litConfig, useExternalSh, script, tmpBase)
  File "/Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/llvm/utils/lit/lit/TestRunner.py", line 2270, in _runShTest
    res = runOnce(execdir)
  File "/Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/llvm/utils/lit/lit/TestRunner.py", line 2244, in runOnce
    res = executeScript(test, litConfig, tmpBase, scriptCopy, execdir)
  File "/Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/llvm/utils/lit/lit/TestRunner.py", line 1210, in executeScript
    f = open(script, mode, **open_kwargs)
OSError: [Errno 28] No space left on device: '/Users/buildbot/buildbot-root/aarch64-darwin/build/tools/lld/test/MachO/Output/arm64-thunk-visibility.s.script'


********************
UNRESOLVED: lld :: MachO/arm64-thunks.s (79811 of 80343)
******************** TEST 'lld :: MachO/arm64-thunks.s' FAILED ********************
Exception during script execution:
Traceback (most recent call last):
  File "/Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/llvm/utils/lit/lit/worker.py", line 76, in _execute_test_handle_errors
    result = test.config.test_format.execute(test, lit_config)
  File "/Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/llvm/utils/lit/lit/formats/shtest.py", line 29, in execute
    return lit.TestRunner.executeShTest(
  File "/Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/llvm/utils/lit/lit/TestRunner.py", line 2326, in executeShTest
    return _runShTest(test, litConfig, useExternalSh, script, tmpBase)
  File "/Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/llvm/utils/lit/lit/TestRunner.py", line 2270, in _runShTest
    res = runOnce(execdir)
  File "/Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/llvm/utils/lit/lit/TestRunner.py", line 2244, in runOnce
    res = executeScript(test, litConfig, tmpBase, scriptCopy, execdir)
  File "/Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/llvm/utils/lit/lit/TestRunner.py", line 1210, in executeScript
    f = open(script, mode, **open_kwargs)
OSError: [Errno 28] No space left on device: '/Users/buildbot/buildbot-root/aarch64-darwin/build/tools/lld/test/MachO/Output/arm64-thunks.s.script'


********************

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.

9 participants