[clang] 60e5cfe - [Clang][LoongArch] Define more LoongArch specific built-in macros

via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 10 01:30:59 PST 2022


Author: Weining Lu
Date: 2022-11-10T17:27:29+08:00
New Revision: 60e5cfe2a4eba3bf0a642aeeb53a3f37dfed5fdb

URL: https://github.com/llvm/llvm-project/commit/60e5cfe2a4eba3bf0a642aeeb53a3f37dfed5fdb
DIFF: https://github.com/llvm/llvm-project/commit/60e5cfe2a4eba3bf0a642aeeb53a3f37dfed5fdb.diff

LOG: [Clang][LoongArch] Define more LoongArch specific built-in macros

Define below macros according to LoongArch toolchain conventions [1].

* `__loongarch_grlen`
* `__loongarch_frlen`
* `__loongarch_lp64`
* `__loongarch_hard_float`
* `__loongarch_soft_float`
* `__loongarch_single_float`
* `__loongarch_double_float`

Note:
1. `__loongarch__` has been defined in earlier patch.
2. `__loongarch_arch` is not defined because I don't know how `TargetInfo` can get the arch name specified by `-march`.
3. `__loongarch_tune` will be defined in future.

[1]: https://loongson.github.io/LoongArch-Documentation/LoongArch-toolchain-conventions-EN.html

Depends on D136146

Differential Revision: https://reviews.llvm.org/D136413

Added: 
    

Modified: 
    clang/lib/Basic/Targets/LoongArch.cpp
    clang/lib/Basic/Targets/LoongArch.h
    clang/test/Preprocessor/init-loongarch.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/Basic/Targets/LoongArch.cpp b/clang/lib/Basic/Targets/LoongArch.cpp
index 12d53a9aff43b..ca7f6e519bd5a 100644
--- a/clang/lib/Basic/Targets/LoongArch.cpp
+++ b/clang/lib/Basic/Targets/LoongArch.cpp
@@ -131,10 +131,50 @@ LoongArchTargetInfo::convertConstraint(const char *&Constraint) const {
 void LoongArchTargetInfo::getTargetDefines(const LangOptions &Opts,
                                            MacroBuilder &Builder) const {
   Builder.defineMacro("__loongarch__");
-  // TODO: Define more macros.
+  unsigned GRLen = getRegisterWidth();
+  Builder.defineMacro("__loongarch_grlen", Twine(GRLen));
+  if (GRLen == 64)
+    Builder.defineMacro("__loongarch64");
+
+  if (HasFeatureD)
+    Builder.defineMacro("__loongarch_frlen", "64");
+  else if (HasFeatureF)
+    Builder.defineMacro("__loongarch_frlen", "32");
+  else
+    Builder.defineMacro("__loongarch_frlen", "0");
+
+  // TODO: define __loongarch_arch and __loongarch_tune.
+
+  StringRef ABI = getABI();
+  if (ABI == "lp64d" || ABI == "lp64f" || ABI == "lp64s")
+    Builder.defineMacro("__loongarch_lp64");
+
+  if (ABI == "lp64d" || ABI == "ilp32d") {
+    Builder.defineMacro("__loongarch_hard_float");
+    Builder.defineMacro("__loongarch_double_float");
+  } else if (ABI == "lp64f" || ABI == "ilp32f") {
+    Builder.defineMacro("__loongarch_hard_float");
+    Builder.defineMacro("__loongarch_single_float");
+  } else if (ABI == "lp64s" || ABI == "ilp32s") {
+    Builder.defineMacro("__loongarch_soft_float");
+  }
 }
 
 ArrayRef<Builtin::Info> LoongArchTargetInfo::getTargetBuiltins() const {
   // TODO: To be implemented in future.
   return {};
 }
+
+bool LoongArchTargetInfo::handleTargetFeatures(
+    std::vector<std::string> &Features, DiagnosticsEngine &Diags) {
+  for (const auto &Feature : Features) {
+    if (Feature == "+d" || Feature == "+f") {
+      // "d" implies "f".
+      HasFeatureF = true;
+      if (Feature == "+d") {
+        HasFeatureD = true;
+      }
+    }
+  }
+  return true;
+}

diff  --git a/clang/lib/Basic/Targets/LoongArch.h b/clang/lib/Basic/Targets/LoongArch.h
index 09d3290362f65..69bcec18659d5 100644
--- a/clang/lib/Basic/Targets/LoongArch.h
+++ b/clang/lib/Basic/Targets/LoongArch.h
@@ -24,10 +24,14 @@ namespace targets {
 class LLVM_LIBRARY_VISIBILITY LoongArchTargetInfo : public TargetInfo {
 protected:
   std::string ABI;
+  bool HasFeatureD;
+  bool HasFeatureF;
 
 public:
   LoongArchTargetInfo(const llvm::Triple &Triple, const TargetOptions &)
       : TargetInfo(Triple) {
+    HasFeatureD = false;
+    HasFeatureF = false;
     LongDoubleWidth = 128;
     LongDoubleAlign = 128;
     LongDoubleFormat = &llvm::APFloat::IEEEquad();
@@ -58,6 +62,9 @@ class LLVM_LIBRARY_VISIBILITY LoongArchTargetInfo : public TargetInfo {
   std::string convertConstraint(const char *&Constraint) const override;
 
   bool hasBitIntType() const override { return true; }
+
+  bool handleTargetFeatures(std::vector<std::string> &Features,
+                            DiagnosticsEngine &Diags) override;
 };
 
 class LLVM_LIBRARY_VISIBILITY LoongArch32TargetInfo

diff  --git a/clang/test/Preprocessor/init-loongarch.c b/clang/test/Preprocessor/init-loongarch.c
index c26cc4f741582..b7b84d42d4ec7 100644
--- a/clang/test/Preprocessor/init-loongarch.c
+++ b/clang/test/Preprocessor/init-loongarch.c
@@ -322,6 +322,7 @@
 // LA32-LINUX: #define __gnu_linux__ 1
 // LA32-LINUX: #define __linux 1
 // LA32-LINUX: #define __linux__ 1
+// LA32-NOT: #define __loongarch64 1
 // LA32: #define __loongarch__ 1
 // LA32-LINUX: #define __unix 1
 // LA32-LINUX: #define __unix__ 1
@@ -634,8 +635,149 @@
 // LA64-LINUX: #define __gnu_linux__ 1
 // LA64-LINUX: #define __linux 1
 // LA64-LINUX: #define __linux__ 1
+// LA64: #define __loongarch64 1
 // LA64: #define __loongarch__ 1
 // LA64-LINUX: #define __unix 1
 // LA64-LINUX: #define __unix__ 1
 // LA64-LINUX: #define linux 1
 // LA64-LINUX: #define unix 1
+
+
+/// Check various macros prefixed with "__loongarch_" in 
diff erent cases.
+/// "__loongarch__"" is not listed here as it has been checked above.
+
+// RUN: %clang --target=loongarch32 -mfpu=64 -mabi=ilp32d -x c -E -dM %s -o - \
+// RUN:   | grep __loongarch_ | FileCheck --check-prefix=LA32-FPU64-ILP32D %s
+// RUN: %clang --target=loongarch32 -mdouble-float -x c -E -dM %s -o - \
+// RUN:   | grep __loongarch_ | FileCheck --check-prefix=LA32-FPU64-ILP32D %s
+// LA32-FPU64-ILP32D: __loongarch_double_float 1
+// LA32-FPU64-ILP32D-NEXT: __loongarch_frlen 64
+// LA32-FPU64-ILP32D-NEXT: __loongarch_grlen 32
+// LA32-FPU64-ILP32D-NEXT: __loongarch_hard_float 1
+// LA32-FPU64-ILP32D-NOT: __loongarch_lp64
+// LA32-FPU64-ILP32D-NOT: __loongarch_single_float
+// LA32-FPU64-ILP32D-NOT: __loongarch_soft_float
+
+// RUN: %clang --target=loongarch32 -mfpu=64 -mabi=ilp32f -x c -E -dM %s -o - \
+// RUN:   | grep __loongarch_ | FileCheck --check-prefix=LA32-FPU64-ILP32F %s
+// LA32-FPU64-ILP32F-NOT: __loongarch_double_float
+// LA32-FPU64-ILP32F: __loongarch_frlen 64
+// LA32-FPU64-ILP32F-NEXT: __loongarch_grlen 32
+// LA32-FPU64-ILP32F-NEXT: __loongarch_hard_float 1
+// LA32-FPU64-ILP32F-NOT: __loongarch_lp64
+// LA32-FPU64-ILP32F-NEXT: __loongarch_single_float 1
+// LA32-FPU64-ILP32F-NOT: __loongarch_soft_float
+
+// RUN: %clang --target=loongarch32 -mfpu=64 -mabi=ilp32s -x c -E -dM %s -o - \
+// RUN:   | grep __loongarch_ | FileCheck --check-prefix=LA32-FPU64-ILP32S %s
+// LA32-FPU64-ILP32S-NOT: __loongarch_double_float
+// LA32-FPU64-ILP32S: __loongarch_frlen 64
+// LA32-FPU64-ILP32S-NEXT: __loongarch_grlen 32
+// LA32-FPU64-ILP32S-NOT: __loongarch_hard_float
+// LA32-FPU64-ILP32S-NOT: __loongarch_lp64
+// LA32-FPU64-ILP32S-NOT: __loongarch_single_float
+// LA32-FPU64-ILP32S-NEXT: __loongarch_soft_float 1
+
+// RUN: %clang --target=loongarch32 -mfpu=32 -mabi=ilp32f -x c -E -dM %s -o - \
+// RUN:   | grep __loongarch_ | FileCheck --check-prefix=LA32-FPU32-ILP32F %s
+// RUN: %clang --target=loongarch32 -msingle-float -x c -E -dM %s -o - \
+// RUN:   | grep __loongarch_ | FileCheck --check-prefix=LA32-FPU32-ILP32F %s
+// LA32-FPU32-ILP32F-NOT: __loongarch_double_float
+// LA32-FPU32-ILP32F: __loongarch_frlen 32
+// LA32-FPU32-ILP32F-NEXT: __loongarch_grlen 32
+// LA32-FPU32-ILP32F-NEXT: __loongarch_hard_float 1
+// LA32-FPU32-ILP32F-NOT: __loongarch_lp64
+// LA32-FPU32-ILP32F-NEXT: __loongarch_single_float 1
+// LA32-FPU32-ILP32F-NOT: __loongarch_soft_float
+
+// RUN: %clang --target=loongarch32 -mfpu=32 -mabi=ilp32s -x c -E -dM %s -o - \
+// RUN:   | grep __loongarch_ | FileCheck --check-prefix=LA32-FPU32-ILP32S %s
+// LA32-FPU32-ILP32S-NOT: __loongarch_double_float
+// LA32-FPU32-ILP32S: __loongarch_frlen 32
+// LA32-FPU32-ILP32S-NEXT: __loongarch_grlen 32
+// LA32-FPU32-ILP32S-NOT: __loongarch_hard_float
+// LA32-FPU32-ILP32S-NOT: __loongarch_lp64
+// LA32-FPU32-ILP32S-NOT: __loongarch_single_float
+// LA32-FPU32-ILP32S-NEXT: __loongarch_soft_float 1
+
+// RUN: %clang --target=loongarch32 -mfpu=0 -mabi=ilp32s -x c -E -dM %s -o - \
+// RUN:   | grep __loongarch_ | FileCheck --check-prefix=LA32-FPU0-ILP32S %s
+// RUN: %clang --target=loongarch32 -mfpu=none -mabi=ilp32s -x c -E -dM %s -o - \
+// RUN:   | grep __loongarch_ | FileCheck --check-prefix=LA32-FPU0-ILP32S %s
+// RUN: %clang --target=loongarch32 -msoft-float -x c -E -dM %s -o - \
+// RUN:   | grep __loongarch_ | FileCheck --check-prefix=LA32-FPU0-ILP32S %s
+// LA32-FPU0-ILP32S-NOT: __loongarch_double_float
+// LA32-FPU0-ILP32S: __loongarch_frlen 0
+// LA32-FPU0-ILP32S-NEXT: __loongarch_grlen 32
+// LA32-FPU0-ILP32S-NOT: __loongarch_hard_float
+// LA32-FPU0-ILP32S-NOT: __loongarch_lp64
+// LA32-FPU0-ILP32S-NOT: __loongarch_single_float
+// LA32-FPU0-ILP32S-NEXT: __loongarch_soft_float 1
+
+// RUN: %clang --target=loongarch64 -mfpu=64 -mabi=lp64d -x c -E -dM %s -o - \
+// RUN:   | grep __loongarch_ | FileCheck --check-prefix=LA64-FPU64-LP64D %s
+// RUN: %clang --target=loongarch64 -mdouble-float -x c -E -dM %s -o - \
+// RUN:   | grep __loongarch_ | FileCheck --check-prefix=LA64-FPU64-LP64D %s
+// LA64-FPU64-LP64D: __loongarch_double_float 1
+// LA64-FPU64-LP64D-NEXT: __loongarch_frlen 64
+// LA64-FPU64-LP64D-NEXT: __loongarch_grlen 64
+// LA64-FPU64-LP64D-NEXT: __loongarch_hard_float 1
+// LA64-FPU64-LP64D-NEXT: __loongarch_lp64 1
+// LA64-FPU64-LP64D-NOT: __loongarch_single_float
+// LA64-FPU64-LP64D-NOT: __loongarch_soft_float
+
+// RUN: %clang --target=loongarch64 -mfpu=64 -mabi=lp64f -x c -E -dM %s -o - \
+// RUN:   | grep __loongarch_ | FileCheck --check-prefix=LA64-FPU64-LP64F %s
+// LA64-FPU64-LP64F-NOT: __loongarch_double_float
+// LA64-FPU64-LP64F: __loongarch_frlen 64
+// LA64-FPU64-LP64F-NEXT: __loongarch_grlen 64
+// LA64-FPU64-LP64F-NEXT: __loongarch_hard_float 1
+// LA64-FPU64-LP64F-NEXT: __loongarch_lp64 1
+// LA64-FPU64-LP64F-NEXT: __loongarch_single_float 1
+// LA64-FPU64-LP64F-NOT: __loongarch_soft_float
+
+// RUN: %clang --target=loongarch64 -mfpu=64 -mabi=lp64s -x c -E -dM %s -o - \
+// RUN:   | grep __loongarch_ | FileCheck --check-prefix=LA64-FPU64-LP64S %s
+// LA64-FPU64-LP64S-NOT: __loongarch_double_float
+// LA64-FPU64-LP64S: __loongarch_frlen 64
+// LA64-FPU64-LP64S-NEXT: __loongarch_grlen 64
+// LA64-FPU64-LP64S-NOT: __loongarch_hard_float
+// LA64-FPU64-LP64S-NEXT: __loongarch_lp64 1
+// LA64-FPU64-LP64S-NOT: __loongarch_single_float
+// LA64-FPU64-LP64S-NEXT: __loongarch_soft_float 1
+
+// RUN: %clang --target=loongarch64 -mfpu=32 -mabi=lp64f -x c -E -dM %s -o - \
+// RUN:   | grep __loongarch_ | FileCheck --check-prefix=LA64-FPU32-LP64F %s
+// RUN: %clang --target=loongarch64 -msingle-float -x c -E -dM %s -o - \
+// RUN:   | grep __loongarch_ | FileCheck --check-prefix=LA64-FPU32-LP64F %s
+// LA64-FPU32-LP64F-NOT: __loongarch_double_float
+// LA64-FPU32-LP64F: __loongarch_frlen 32
+// LA64-FPU32-LP64F-NEXT: __loongarch_grlen 64
+// LA64-FPU32-LP64F-NEXT: __loongarch_hard_float 1
+// LA64-FPU32-LP64F-NEXT: __loongarch_lp64 1
+// LA64-FPU32-LP64F-NEXT: __loongarch_single_float 1
+// LA64-FPU32-LP64F-NOT: __loongarch_soft_float
+
+// RUN: %clang --target=loongarch64 -mfpu=32 -mabi=lp64s -x c -E -dM %s -o - \
+// RUN:   | grep __loongarch_ | FileCheck --check-prefix=LA64-FPU32-LP64S %s
+// LA64-FPU32-LP64S-NOT: __loongarch_double_float
+// LA64-FPU32-LP64S: __loongarch_frlen 32
+// LA64-FPU32-LP64S-NEXT: __loongarch_grlen 64
+// LA64-FPU32-LP64S-NOT: __loongarch_hard_float
+// LA64-FPU32-LP64S-NEXT: __loongarch_lp64 1
+// LA64-FPU32-LP64S-NOT: __loongarch_single_float
+// LA64-FPU32-LP64S-NEXT: __loongarch_soft_float 1
+
+// RUN: %clang --target=loongarch64 -mfpu=0 -mabi=lp64s -x c -E -dM %s -o - \
+// RUN:   | grep __loongarch_ | FileCheck --check-prefix=LA64-FPU0-LP64S %s
+// RUN: %clang --target=loongarch64 -mfpu=none -mabi=lp64s -x c -E -dM %s -o - \
+// RUN:   | grep __loongarch_ | FileCheck --check-prefix=LA64-FPU0-LP64S %s
+// RUN: %clang --target=loongarch64 -msoft-float -x c -E -dM %s -o - \
+// RUN:   | grep __loongarch_ | FileCheck --check-prefix=LA64-FPU0-LP64S %s
+// LA64-FPU0-LP64S-NOT: __loongarch_double_float
+// LA64-FPU0-LP64S: __loongarch_frlen 0
+// LA64-FPU0-LP64S-NEXT: __loongarch_grlen 64
+// LA64-FPU0-LP64S-NOT: __loongarch_hard_float
+// LA64-FPU0-LP64S-NEXT: __loongarch_lp64 1
+// LA64-FPU0-LP64S-NOT: __loongarch_single_float
+// LA64-FPU0-LP64S-NEXT: __loongarch_soft_float 1


        


More information about the cfe-commits mailing list