Skip to content

[clang][uefi] add arm, aarch64, x86, loongarch64, riscv64 targets #136247

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

RossComputerGuy
Copy link
Member

@RossComputerGuy RossComputerGuy commented Apr 18, 2025

Adds the rest of the targets which support UEFI according to the spec.

To test:

$ mkdir -p build
$ cd build
$ cmake ../llvm/ -DCMAKE_INSTALL_PREFIX=$(pwd)/install -DCMAKE_BUILD_TYPE=Debug -DLLVM_ENABLE_PROJECTS="clang;lld" -DLLVM_DEFAULT_TARGET_TRIPLE=x86_64-unknown-uefi-llvm -DLLVM_RUNTIME_TARGETS="x86_64-unknown-uefi-llvm;aarch64-unknown-uefi-llvm;loongarch64-unknown-uefi-llvm;riscv64-unknown-uefi-llvm" -DRUNTIMES_x86_64-unknown-uefi-llvm_LLVM_ENABLE_RUNTIMES="libc" -DRUNTIMES_x86_64-unknown-uefi-llvm_LLVM_LIBC_FULL_BUILD=true -DRUNTIMES_aarch64-unknown-uefi-llvm_LLVM_ENABLE_RUNTIMES="libc" -DRUNTIMES_aarch64-unknown-uefi-llvm_LLVM_LIBC_FULL_BUILD=true -DRUNTIMES_loongarch64-unknown-uefi-llvm_LLVM_ENABLE_RUNTIMES=libc -DRUNTIMES_loongarch64-unknown-uefi-llvm_LLVM_LIBC_FULL_BUILD=true -DRUNTIMES_riscv64-unknown-uefi-llvm_LLVM_ENABLE_RUNTIMES=libc -DRUNTIMES_riscv64-unknown-uefi-llvm_LLVM_LIBC_FULL_BUILD=true -G Ninja
$ ninja all install

Check these files exist:

  • build/install/lib/aarch64-unknown-uefi-llvm/libc.a
  • build/install/lib/x86_64-unknown-uefi-llvm/libc.a
  • build/install/lib/loongarch64-unknown-uefi-llvm/libc.a
  • build/install/lib/riscv64-unknown-uefi-llvm/libc.a

@RossComputerGuy RossComputerGuy requested a review from Prabhuk April 18, 2025 03:08
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Apr 18, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 18, 2025

@llvm/pr-subscribers-llvm-ir
@llvm/pr-subscribers-backend-aarch64
@llvm/pr-subscribers-clang-driver

@llvm/pr-subscribers-clang

Author: Tristan Ross (RossComputerGuy)

Changes

Adds the rest of the targets which support UEFI according to the spec.


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

3 Files Affected:

  • (modified) clang/include/clang/Basic/TargetOSMacros.def (+3)
  • (modified) clang/lib/Basic/Targets.cpp (+14)
  • (modified) clang/test/Preprocessor/init.c (+5)
diff --git a/clang/include/clang/Basic/TargetOSMacros.def b/clang/include/clang/Basic/TargetOSMacros.def
index 58dce330f9c8f..f4f3276ad1c25 100644
--- a/clang/include/clang/Basic/TargetOSMacros.def
+++ b/clang/include/clang/Basic/TargetOSMacros.def
@@ -53,4 +53,7 @@ TARGET_OS(TARGET_OS_NANO, Triple.isWatchOS())
 TARGET_OS(TARGET_IPHONE_SIMULATOR, Triple.isSimulatorEnvironment())
 TARGET_OS(TARGET_OS_UIKITFORMAC, Triple.isMacCatalystEnvironment())
 
+// UEFI target.
+TARGET_OS(TARGET_OS_UEFI, Triple.isUEFI())
+
 #undef TARGET_OS
diff --git a/clang/lib/Basic/Targets.cpp b/clang/lib/Basic/Targets.cpp
index c6d228fe98100..0502d99a20e4f 100644
--- a/clang/lib/Basic/Targets.cpp
+++ b/clang/lib/Basic/Targets.cpp
@@ -164,6 +164,11 @@ std::unique_ptr<TargetInfo> AllocateTarget(const llvm::Triple &Triple,
         return std::make_unique<OHOSTargetInfo<AArch64leTargetInfo>>(Triple,
                                                                      Opts);
       }
+
+    case llvm::Triple::UEFI:
+      return std::make_unique<UEFITargetInfo<AArch64leTargetInfo>>(Triple,
+                                                                   Opts);
+
     case llvm::Triple::NetBSD:
       return std::make_unique<NetBSDTargetInfo<AArch64leTargetInfo>>(Triple,
                                                                      Opts);
@@ -227,6 +232,8 @@ std::unique_ptr<TargetInfo> AllocateTarget(const llvm::Triple &Triple,
       return std::make_unique<HaikuTargetInfo<ARMleTargetInfo>>(Triple, Opts);
     case llvm::Triple::NaCl:
       return std::make_unique<NaClTargetInfo<ARMleTargetInfo>>(Triple, Opts);
+    case llvm::Triple::UEFI:
+      return std::make_unique<UEFITargetInfo<ARMleTargetInfo>>(Triple, Opts);
     case llvm::Triple::Win32:
       switch (Triple.getEnvironment()) {
       case llvm::Triple::Cygnus:
@@ -457,6 +464,8 @@ std::unique_ptr<TargetInfo> AllocateTarget(const llvm::Triple &Triple,
     case llvm::Triple::Haiku:
       return std::make_unique<HaikuTargetInfo<RISCV64TargetInfo>>(Triple,
                                                                   Opts);
+    case llvm::Triple::UEFI:
+      return std::make_unique<UEFITargetInfo<RISCV64TargetInfo>>(Triple, Opts);
     case llvm::Triple::Linux:
       switch (Triple.getEnvironment()) {
       default:
@@ -569,6 +578,8 @@ std::unique_ptr<TargetInfo> AllocateTarget(const llvm::Triple &Triple,
     case llvm::Triple::Solaris:
       return std::make_unique<SolarisTargetInfo<X86_32TargetInfo>>(Triple,
                                                                    Opts);
+    case llvm::Triple::UEFI:
+      return std::make_unique<UEFITargetInfo<X86_32TargetInfo>>(Triple, Opts);
     case llvm::Triple::Win32: {
       switch (Triple.getEnvironment()) {
       case llvm::Triple::Cygnus:
@@ -760,6 +771,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::UEFI:
+      return std::make_unique<UEFITargetInfo<LoongArch64TargetInfo>>(Triple,
+                                                                     Opts);
     default:
       return std::make_unique<LoongArch64TargetInfo>(Triple, Opts);
     }
diff --git a/clang/test/Preprocessor/init.c b/clang/test/Preprocessor/init.c
index 1ac325d444662..55a7340deba24 100644
--- a/clang/test/Preprocessor/init.c
+++ b/clang/test/Preprocessor/init.c
@@ -2835,6 +2835,11 @@
 // RISCV64-LINUX: #define unix 1
 
 // RUN: %clang_cc1 -dM -triple=x86_64-uefi -E /dev/null | FileCheck -match-full-lines -check-prefix UEFI %s
+// RUN: %clang_cc1 -dM -triple=armv7l-unknown-uefi -E < /dev/null | FileCheck -match-full-lines -check-prefix UEFI %s
+// RUN: %clang_cc1 -dM -triple=aarch64-unknown-uefi -E < /dev/null | FileCheck -match-full-lines -check-prefix UEFI %s
+// RUN: %clang_cc1 -dM -triple=loongarch64-unknown-uefi -E < /dev/null | FileCheck -match-full-lines -check-prefix UEFI %s
+// RUN: %clang_cc1 -dM -triple=riscv64-unknown-uefi -E < /dev/null | FileCheck -match-full-lines -check-prefix UEFI %s
+// RUN: %clang_cc1 -dM -triple=x86_64-unknown-uefi -E /dev/null | FileCheck -match-full-lines -check-prefix UEFI %s
 
 // UEFI: #define __UEFI__ 1
 

@llvmbot llvmbot added compiler-rt clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' compiler-rt:builtins labels Apr 18, 2025
@RossComputerGuy RossComputerGuy force-pushed the feat/uefi-clang-improve branch from 427b22a to b601bb7 Compare April 18, 2025 03:48
Copy link

github-actions bot commented Apr 18, 2025

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

@RossComputerGuy
Copy link
Member Author

2 targets have complications which require non-trivial backend work:

  • armv7l-unknown-uefi-llvm
  • i386-unknown-uefi-llvm

There's 3 options:

  1. Gut from this PR
  2. Keep as it
  3. Keep but mark as unsupported

@@ -352,6 +352,8 @@ static MCAsmInfo *createAArch64MCAsmInfo(const MCRegisterInfo &MRI,
MAI = new AArch64MCAsmInfoMicrosoftCOFF();
else if (TheTriple.isOSBinFormatCOFF())
MAI = new AArch64MCAsmInfoGNUCOFF();
else if (TheTriple.isUEFI())
MAI = new AArch64MCAsmInfoGNUCOFF();
Copy link
Member

Choose a reason for hiding this comment

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

We shouldn't be using GNU COFF for UEFI, we should be using Microsoft COFF.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, why does x86 use it?

} else if (TheTriple.isUEFI()) {
MAI = new X86MCAsmInfoGNUCOFF(TheTriple);

Is it because of the win64 calling convention?

@RossComputerGuy RossComputerGuy force-pushed the feat/uefi-clang-improve branch from bede2c6 to 31d6ee2 Compare April 20, 2025 03:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:AArch64 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 compiler-rt:builtins compiler-rt llvm:ir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants