Skip to content

[LoongArch][clang] Add support for option -msimd= and macro __loongarch_simd_width. #97984

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

Merged
merged 1 commit into from
Jul 9, 2024

Conversation

ylzsx
Copy link
Contributor

@ylzsx ylzsx commented Jul 8, 2024

No description provided.

Copy link

github-actions bot commented Jul 8, 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" backend:loongarch labels Jul 8, 2024
@llvmbot
Copy link
Member

llvmbot commented Jul 8, 2024

@llvm/pr-subscribers-backend-loongarch

@llvm/pr-subscribers-clang-driver

Author: Zhaoxin Yang (ylzsx)

Changes

…rch_simd_width`.


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

6 Files Affected:

  • (modified) clang/include/clang/Basic/DiagnosticDriverKinds.td (+2)
  • (modified) clang/include/clang/Driver/Options.td (+3)
  • (modified) clang/lib/Basic/Targets/LoongArch.cpp (+6-2)
  • (modified) clang/lib/Driver/ToolChains/Arch/LoongArch.cpp (+29)
  • (added) clang/test/Driver/loongarch-msimd.c (+129)
  • (modified) clang/test/Preprocessor/init-loongarch.c (+3)
diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index a62bdc21298eee..243d88d53d6647 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -791,6 +791,8 @@ def err_drv_loongarch_wrong_fpu_width : Error<
   "wrong fpu width; %select{LSX|LASX}0 depends on 64-bit FPU">;
 def err_drv_loongarch_invalid_simd_option_combination : Error<
   "invalid option combination; LASX depends on LSX">;
+def err_drv_loongarch_invalid_msimd_EQ : Error<
+  "invalid argument '%0' to -msimd=; must be one of: none, lsx, lasx">;
 
 def err_drv_expand_response_file : Error<
   "failed to expand response file: %0">;
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 58ca6f2bea9e44..707ce8ce78d0b0 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -5252,6 +5252,9 @@ def mlasx : Flag<["-"], "mlasx">, Group<m_loongarch_Features_Group>,
   HelpText<"Enable Loongson Advanced SIMD Extension (LASX).">;
 def mno_lasx : Flag<["-"], "mno-lasx">, Group<m_loongarch_Features_Group>,
   HelpText<"Disable Loongson Advanced SIMD Extension (LASX).">;
+def msimd_EQ : Joined<["-"], "msimd=">, Group<m_loongarch_Features_Group>,
+  Flags<[TargetSpecific]>,
+  HelpText<"Select the SIMD extension(s) to be enabled in LoongArch either 'none', 'lsx', 'lasx'.">;
 def mnop_mcount : Flag<["-"], "mnop-mcount">, HelpText<"Generate mcount/__fentry__ calls as nops. To activate they need to be patched in.">,
   Visibility<[ClangOption, CC1Option]>, Group<m_Group>,
   MarshallingInfoFlag<CodeGenOpts<"MNopMCount">>;
diff --git a/clang/lib/Basic/Targets/LoongArch.cpp b/clang/lib/Basic/Targets/LoongArch.cpp
index 280bd1d8033cc6..75f71a337b7a49 100644
--- a/clang/lib/Basic/Targets/LoongArch.cpp
+++ b/clang/lib/Basic/Targets/LoongArch.cpp
@@ -208,10 +208,14 @@ void LoongArchTargetInfo::getTargetDefines(const LangOptions &Opts,
     TuneCPU = ArchName;
   Builder.defineMacro("__loongarch_tune", Twine('"') + TuneCPU + Twine('"'));
 
-  if (HasFeatureLSX)
+  if (HasFeatureLASX) {
+    Builder.defineMacro("__loongarch_simd_width", "256");
     Builder.defineMacro("__loongarch_sx", Twine(1));
-  if (HasFeatureLASX)
     Builder.defineMacro("__loongarch_asx", Twine(1));
+  } else if (HasFeatureLSX) {
+    Builder.defineMacro("__loongarch_simd_width", "128");
+    Builder.defineMacro("__loongarch_sx", Twine(1));
+  }
 
   StringRef ABI = getABI();
   if (ABI == "lp64d" || ABI == "lp64f" || ABI == "lp64s")
diff --git a/clang/lib/Driver/ToolChains/Arch/LoongArch.cpp b/clang/lib/Driver/ToolChains/Arch/LoongArch.cpp
index 9ea4cc3f7cb95b..4a2b9efc9ffadb 100644
--- a/clang/lib/Driver/ToolChains/Arch/LoongArch.cpp
+++ b/clang/lib/Driver/ToolChains/Arch/LoongArch.cpp
@@ -206,6 +206,35 @@ void loongarch::getLoongArchTargetFeatures(const Driver &D,
     } else /*-mno-lasx*/
       Features.push_back("-lasx");
   }
+
+  // Select lsx/lasx feature determined by -msimd=.
+  // Option -msimd= has lower priority than -m[no-]lsx and -m[no-]lasx.
+  if (const Arg *A = Args.getLastArg(options::OPT_msimd_EQ)) {
+    StringRef MSIMD = A->getValue();
+    if (MSIMD == "lsx") {
+      // Option -msimd=lsx depends on 64-bit FPU.
+      // -m*-float and -mfpu=none/0/32 conflict with -mlsx.
+      if (llvm::find(Features, "-d") != Features.end())
+        D.Diag(diag::err_drv_loongarch_wrong_fpu_width) << /*LSX*/ 0;
+      // The previous option does not contain feature -lsx.
+      else if (llvm::find(Features, "-lsx") == Features.end())
+        Features.push_back("+lsx");
+    } else if (MSIMD == "lasx") {
+      // Option -msimd=lasx depends on 64-bit FPU and LSX.
+      // -m*-float and -mfpu=none/0/32 conflict with -mlsx.
+      if (llvm::find(Features, "-d") != Features.end())
+        D.Diag(diag::err_drv_loongarch_wrong_fpu_width) << /*LASX*/ 1;
+      else if (llvm::find(Features, "-lsx") != Features.end())
+        D.Diag(diag::err_drv_loongarch_invalid_simd_option_combination);
+      // The previous option does not contain feature -lasx.
+      else if (llvm::find(Features, "-lasx") == Features.end()) {
+        Features.push_back("+lsx");
+        Features.push_back("+lasx");
+      }
+    } else if (MSIMD != "none") {
+      D.Diag(diag::err_drv_loongarch_invalid_msimd_EQ) << MSIMD;
+    }
+  }
 }
 
 std::string loongarch::postProcessTargetCPUString(const std::string &CPU,
diff --git a/clang/test/Driver/loongarch-msimd.c b/clang/test/Driver/loongarch-msimd.c
new file mode 100644
index 00000000000000..984f3e8bf2bfc3
--- /dev/null
+++ b/clang/test/Driver/loongarch-msimd.c
@@ -0,0 +1,129 @@
+/// Test -msimd options.
+
+/// COM: -msimd=none
+// RUN: %clang --target=loongarch64 -mlasx -msimd=none -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,LASX
+// RUN: %clang --target=loongarch64 -mlasx -mlsx -msimd=none -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,LASX
+
+// RUN: %clang --target=loongarch64 -msimd=none -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=NOLSX,NOLASX
+// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -msimd=none -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=NOLSX,NOLASX
+// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -mlsx -mno-lsx -msimd=none -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=NOLSX,NOLASX
+// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -mno-lsx -msimd=none -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=NOLSX,NOLASX
+// RUN: %clang --target=loongarch64 -mlsx -mno-lsx -msimd=none -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=NOLSX,NOLASX
+// RUN: %clang --target=loongarch64 -mno-lasx -msimd=none -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=NOLSX,NOLASX
+// RUN: %clang --target=loongarch64 -mno-lasx -mlsx -mno-lsx -msimd=none -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=NOLSX,NOLASX
+// RUN: %clang --target=loongarch64 -mno-lasx -mno-lsx -msimd=none -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=NOLSX,NOLASX
+// RUN: %clang --target=loongarch64 -mno-lsx -msimd=none -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=NOLSX,NOLASX
+
+// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -mlsx -msimd=none -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,NOLASX
+// RUN: %clang --target=loongarch64 -mno-lasx -mlsx -msimd=none -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,NOLASX
+// RUN: %clang --target=loongarch64 -mlsx -msimd=none -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,NOLASX
+
+
+/// COM: -msimd=lsx
+// RUN: %clang --target=loongarch64 -mlasx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,LASX
+// RUN: %clang --target=loongarch64 -mlasx -mlsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,LASX
+
+// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -mno-lsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=NOLSX,NOLASX
+// RUN: %clang --target=loongarch64 -mlsx -mno-lsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=NOLSX,NOLASX
+// RUN: %clang --target=loongarch64 -mno-lasx -mlsx -mno-lsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=NOLSX,NOLASX
+// RUN: %clang --target=loongarch64 -mno-lasx -mno-lsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=NOLSX,NOLASX
+// RUN: %clang --target=loongarch64 -mno-lsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=NOLSX,NOLASX
+// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -mlsx -mno-lsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=NOLSX,NOLASX
+
+// RUN: %clang --target=loongarch64 -msimd=lsx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,NOLASX
+// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,NOLASX
+// RUN: %clang --target=loongarch64 -mlsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,NOLASX
+// RUN: %clang --target=loongarch64 -mno-lasx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,NOLASX
+// RUN: %clang --target=loongarch64 -mno-lasx -mlsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,NOLASX
+
+
+/// COM: -msimd=lasx
+// RUN: %clang --target=loongarch64 -msimd=lasx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,LASX
+// RUN: %clang --target=loongarch64 -mlasx -msimd=lasx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,LASX
+// RUN: %clang --target=loongarch64 -mlasx -mlsx -msimd=lasx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,LASX
+// RUN: %clang --target=loongarch64 -mlsx -msimd=lasx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,LASX
+
+// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -msimd=lasx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=NOLSX,NOLASX
+// RUN: %clang --target=loongarch64 -mno-lasx -msimd=lasx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=NOLSX,NOLASX
+
+// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -mlsx -msimd=lasx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,NOLASX
+// RUN: %clang --target=loongarch64 -mno-lasx -mlsx -msimd=lasx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,NOLASX
+// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -mlsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,NOLASX
+
+
+// LSX: "-target-feature" "+lsx"
+// LASX: "-target-feature" "+lasx"
+// NOLSX-NOT: "-target-feature" "+lsx"
+// NOLASX-NOT: "-target-feature" "+lasx"
diff --git a/clang/test/Preprocessor/init-loongarch.c b/clang/test/Preprocessor/init-loongarch.c
index 10a4cc7e66e830..182f904b765927 100644
--- a/clang/test/Preprocessor/init-loongarch.c
+++ b/clang/test/Preprocessor/init-loongarch.c
@@ -827,6 +827,7 @@
 // RUN: %clang --target=loongarch64 -mno-lasx -mlsx -x c -E -dM %s -o - \
 // RUN:   | FileCheck --match-full-lines --check-prefix=MLSX %s
 // MLSX-NOT: #define __loongarch_asx
+// MLSX: #define __loongarch_simd_width 128
 // MLSX: #define __loongarch_sx 1
 
 // RUN: %clang --target=loongarch64 -mlasx -x c -E -dM %s -o - \
@@ -838,6 +839,7 @@
 // RUN: %clang --target=loongarch64 -mlasx -mlsx -x c -E -dM %s -o - \
 // RUN:   | FileCheck --match-full-lines --check-prefix=MLASX %s
 // MLASX: #define __loongarch_asx 1
+// MLASX: #define __loongarch_simd_width 256
 // MLASX: #define __loongarch_sx 1
 
 // RUN: %clang --target=loongarch64 -mno-lsx -x c -E -dM %s -o - \
@@ -851,4 +853,5 @@
 // RUN: %clang --target=loongarch64 -mno-lasx -x c -E -dM %s -o - \
 // RUN:   | FileCheck --match-full-lines --check-prefix=MNO-LSX %s
 // MNO-LSX-NOT: #define __loongarch_asx
+// MNO-LSX-NOT: #define __loongarch_simd_width
 // MNO-LSX-NOT: #define __loongarch_sx

@llvmbot
Copy link
Member

llvmbot commented Jul 8, 2024

@llvm/pr-subscribers-clang

Author: Zhaoxin Yang (ylzsx)

Changes

…rch_simd_width`.


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

6 Files Affected:

  • (modified) clang/include/clang/Basic/DiagnosticDriverKinds.td (+2)
  • (modified) clang/include/clang/Driver/Options.td (+3)
  • (modified) clang/lib/Basic/Targets/LoongArch.cpp (+6-2)
  • (modified) clang/lib/Driver/ToolChains/Arch/LoongArch.cpp (+29)
  • (added) clang/test/Driver/loongarch-msimd.c (+129)
  • (modified) clang/test/Preprocessor/init-loongarch.c (+3)
diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index a62bdc21298eee..243d88d53d6647 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -791,6 +791,8 @@ def err_drv_loongarch_wrong_fpu_width : Error<
   "wrong fpu width; %select{LSX|LASX}0 depends on 64-bit FPU">;
 def err_drv_loongarch_invalid_simd_option_combination : Error<
   "invalid option combination; LASX depends on LSX">;
+def err_drv_loongarch_invalid_msimd_EQ : Error<
+  "invalid argument '%0' to -msimd=; must be one of: none, lsx, lasx">;
 
 def err_drv_expand_response_file : Error<
   "failed to expand response file: %0">;
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 58ca6f2bea9e44..707ce8ce78d0b0 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -5252,6 +5252,9 @@ def mlasx : Flag<["-"], "mlasx">, Group<m_loongarch_Features_Group>,
   HelpText<"Enable Loongson Advanced SIMD Extension (LASX).">;
 def mno_lasx : Flag<["-"], "mno-lasx">, Group<m_loongarch_Features_Group>,
   HelpText<"Disable Loongson Advanced SIMD Extension (LASX).">;
+def msimd_EQ : Joined<["-"], "msimd=">, Group<m_loongarch_Features_Group>,
+  Flags<[TargetSpecific]>,
+  HelpText<"Select the SIMD extension(s) to be enabled in LoongArch either 'none', 'lsx', 'lasx'.">;
 def mnop_mcount : Flag<["-"], "mnop-mcount">, HelpText<"Generate mcount/__fentry__ calls as nops. To activate they need to be patched in.">,
   Visibility<[ClangOption, CC1Option]>, Group<m_Group>,
   MarshallingInfoFlag<CodeGenOpts<"MNopMCount">>;
diff --git a/clang/lib/Basic/Targets/LoongArch.cpp b/clang/lib/Basic/Targets/LoongArch.cpp
index 280bd1d8033cc6..75f71a337b7a49 100644
--- a/clang/lib/Basic/Targets/LoongArch.cpp
+++ b/clang/lib/Basic/Targets/LoongArch.cpp
@@ -208,10 +208,14 @@ void LoongArchTargetInfo::getTargetDefines(const LangOptions &Opts,
     TuneCPU = ArchName;
   Builder.defineMacro("__loongarch_tune", Twine('"') + TuneCPU + Twine('"'));
 
-  if (HasFeatureLSX)
+  if (HasFeatureLASX) {
+    Builder.defineMacro("__loongarch_simd_width", "256");
     Builder.defineMacro("__loongarch_sx", Twine(1));
-  if (HasFeatureLASX)
     Builder.defineMacro("__loongarch_asx", Twine(1));
+  } else if (HasFeatureLSX) {
+    Builder.defineMacro("__loongarch_simd_width", "128");
+    Builder.defineMacro("__loongarch_sx", Twine(1));
+  }
 
   StringRef ABI = getABI();
   if (ABI == "lp64d" || ABI == "lp64f" || ABI == "lp64s")
diff --git a/clang/lib/Driver/ToolChains/Arch/LoongArch.cpp b/clang/lib/Driver/ToolChains/Arch/LoongArch.cpp
index 9ea4cc3f7cb95b..4a2b9efc9ffadb 100644
--- a/clang/lib/Driver/ToolChains/Arch/LoongArch.cpp
+++ b/clang/lib/Driver/ToolChains/Arch/LoongArch.cpp
@@ -206,6 +206,35 @@ void loongarch::getLoongArchTargetFeatures(const Driver &D,
     } else /*-mno-lasx*/
       Features.push_back("-lasx");
   }
+
+  // Select lsx/lasx feature determined by -msimd=.
+  // Option -msimd= has lower priority than -m[no-]lsx and -m[no-]lasx.
+  if (const Arg *A = Args.getLastArg(options::OPT_msimd_EQ)) {
+    StringRef MSIMD = A->getValue();
+    if (MSIMD == "lsx") {
+      // Option -msimd=lsx depends on 64-bit FPU.
+      // -m*-float and -mfpu=none/0/32 conflict with -mlsx.
+      if (llvm::find(Features, "-d") != Features.end())
+        D.Diag(diag::err_drv_loongarch_wrong_fpu_width) << /*LSX*/ 0;
+      // The previous option does not contain feature -lsx.
+      else if (llvm::find(Features, "-lsx") == Features.end())
+        Features.push_back("+lsx");
+    } else if (MSIMD == "lasx") {
+      // Option -msimd=lasx depends on 64-bit FPU and LSX.
+      // -m*-float and -mfpu=none/0/32 conflict with -mlsx.
+      if (llvm::find(Features, "-d") != Features.end())
+        D.Diag(diag::err_drv_loongarch_wrong_fpu_width) << /*LASX*/ 1;
+      else if (llvm::find(Features, "-lsx") != Features.end())
+        D.Diag(diag::err_drv_loongarch_invalid_simd_option_combination);
+      // The previous option does not contain feature -lasx.
+      else if (llvm::find(Features, "-lasx") == Features.end()) {
+        Features.push_back("+lsx");
+        Features.push_back("+lasx");
+      }
+    } else if (MSIMD != "none") {
+      D.Diag(diag::err_drv_loongarch_invalid_msimd_EQ) << MSIMD;
+    }
+  }
 }
 
 std::string loongarch::postProcessTargetCPUString(const std::string &CPU,
diff --git a/clang/test/Driver/loongarch-msimd.c b/clang/test/Driver/loongarch-msimd.c
new file mode 100644
index 00000000000000..984f3e8bf2bfc3
--- /dev/null
+++ b/clang/test/Driver/loongarch-msimd.c
@@ -0,0 +1,129 @@
+/// Test -msimd options.
+
+/// COM: -msimd=none
+// RUN: %clang --target=loongarch64 -mlasx -msimd=none -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,LASX
+// RUN: %clang --target=loongarch64 -mlasx -mlsx -msimd=none -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,LASX
+
+// RUN: %clang --target=loongarch64 -msimd=none -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=NOLSX,NOLASX
+// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -msimd=none -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=NOLSX,NOLASX
+// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -mlsx -mno-lsx -msimd=none -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=NOLSX,NOLASX
+// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -mno-lsx -msimd=none -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=NOLSX,NOLASX
+// RUN: %clang --target=loongarch64 -mlsx -mno-lsx -msimd=none -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=NOLSX,NOLASX
+// RUN: %clang --target=loongarch64 -mno-lasx -msimd=none -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=NOLSX,NOLASX
+// RUN: %clang --target=loongarch64 -mno-lasx -mlsx -mno-lsx -msimd=none -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=NOLSX,NOLASX
+// RUN: %clang --target=loongarch64 -mno-lasx -mno-lsx -msimd=none -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=NOLSX,NOLASX
+// RUN: %clang --target=loongarch64 -mno-lsx -msimd=none -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=NOLSX,NOLASX
+
+// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -mlsx -msimd=none -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,NOLASX
+// RUN: %clang --target=loongarch64 -mno-lasx -mlsx -msimd=none -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,NOLASX
+// RUN: %clang --target=loongarch64 -mlsx -msimd=none -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,NOLASX
+
+
+/// COM: -msimd=lsx
+// RUN: %clang --target=loongarch64 -mlasx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,LASX
+// RUN: %clang --target=loongarch64 -mlasx -mlsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,LASX
+
+// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -mno-lsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=NOLSX,NOLASX
+// RUN: %clang --target=loongarch64 -mlsx -mno-lsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=NOLSX,NOLASX
+// RUN: %clang --target=loongarch64 -mno-lasx -mlsx -mno-lsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=NOLSX,NOLASX
+// RUN: %clang --target=loongarch64 -mno-lasx -mno-lsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=NOLSX,NOLASX
+// RUN: %clang --target=loongarch64 -mno-lsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=NOLSX,NOLASX
+// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -mlsx -mno-lsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=NOLSX,NOLASX
+
+// RUN: %clang --target=loongarch64 -msimd=lsx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,NOLASX
+// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,NOLASX
+// RUN: %clang --target=loongarch64 -mlsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,NOLASX
+// RUN: %clang --target=loongarch64 -mno-lasx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,NOLASX
+// RUN: %clang --target=loongarch64 -mno-lasx -mlsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,NOLASX
+
+
+/// COM: -msimd=lasx
+// RUN: %clang --target=loongarch64 -msimd=lasx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,LASX
+// RUN: %clang --target=loongarch64 -mlasx -msimd=lasx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,LASX
+// RUN: %clang --target=loongarch64 -mlasx -mlsx -msimd=lasx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,LASX
+// RUN: %clang --target=loongarch64 -mlsx -msimd=lasx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,LASX
+
+// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -msimd=lasx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=NOLSX,NOLASX
+// RUN: %clang --target=loongarch64 -mno-lasx -msimd=lasx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=NOLSX,NOLASX
+
+// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -mlsx -msimd=lasx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,NOLASX
+// RUN: %clang --target=loongarch64 -mno-lasx -mlsx -msimd=lasx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,NOLASX
+// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -mlsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
+// RUN:   grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
+// RUN:   FileCheck %s --check-prefixes=LSX,NOLASX
+
+
+// LSX: "-target-feature" "+lsx"
+// LASX: "-target-feature" "+lasx"
+// NOLSX-NOT: "-target-feature" "+lsx"
+// NOLASX-NOT: "-target-feature" "+lasx"
diff --git a/clang/test/Preprocessor/init-loongarch.c b/clang/test/Preprocessor/init-loongarch.c
index 10a4cc7e66e830..182f904b765927 100644
--- a/clang/test/Preprocessor/init-loongarch.c
+++ b/clang/test/Preprocessor/init-loongarch.c
@@ -827,6 +827,7 @@
 // RUN: %clang --target=loongarch64 -mno-lasx -mlsx -x c -E -dM %s -o - \
 // RUN:   | FileCheck --match-full-lines --check-prefix=MLSX %s
 // MLSX-NOT: #define __loongarch_asx
+// MLSX: #define __loongarch_simd_width 128
 // MLSX: #define __loongarch_sx 1
 
 // RUN: %clang --target=loongarch64 -mlasx -x c -E -dM %s -o - \
@@ -838,6 +839,7 @@
 // RUN: %clang --target=loongarch64 -mlasx -mlsx -x c -E -dM %s -o - \
 // RUN:   | FileCheck --match-full-lines --check-prefix=MLASX %s
 // MLASX: #define __loongarch_asx 1
+// MLASX: #define __loongarch_simd_width 256
 // MLASX: #define __loongarch_sx 1
 
 // RUN: %clang --target=loongarch64 -mno-lsx -x c -E -dM %s -o - \
@@ -851,4 +853,5 @@
 // RUN: %clang --target=loongarch64 -mno-lasx -x c -E -dM %s -o - \
 // RUN:   | FileCheck --match-full-lines --check-prefix=MNO-LSX %s
 // MNO-LSX-NOT: #define __loongarch_asx
+// MNO-LSX-NOT: #define __loongarch_simd_width
 // MNO-LSX-NOT: #define __loongarch_sx

@ylzsx ylzsx changed the title [LoongArch][clang] Add support for option -msimd and macro ` loonga… [LoongArch][clang] Add support for option -msimd and macro __loongarch_simd_width. Jul 8, 2024
@SixWeining SixWeining changed the title [LoongArch][clang] Add support for option -msimd and macro __loongarch_simd_width. [LoongArch][clang] Add support for option -msimd= and macro __loongarch_simd_width. Jul 8, 2024
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 626c7ce into llvm:main Jul 9, 2024
12 checks passed
Copy link

github-actions bot commented Jul 9, 2024

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

@ylzsx ylzsx deleted the yangzhaoxin branch July 9, 2024 06:19
@jakeegan
Copy link
Member

jakeegan commented Jul 10, 2024

Hi, this test is failing on the AIX bot
https://lab.llvm.org/buildbot/#/builders/64/builds/250/steps/6/logs/FAIL__Clang__loongarch-msimd_c

The reason is that -o is not a supported option with grep on AIX. Would you be able to adapt the test to avoid piping and use input redirection (so that the builtin grep is used, for example here: https://github.com/llvm/llvm-project/pull/83184/files. I'm not sure if this works with grep though) or use another command like sed?

@ylzsx
Copy link
Contributor Author

ylzsx commented Jul 11, 2024

Hi, this test is failing on the AIX bot https://lab.llvm.org/buildbot/#/builders/64/builds/250/steps/6/logs/FAIL__Clang__loongarch-msimd_c

The reason is that -o is not a supported option with grep on AIX. Would you be able to adapt the test to avoid piping and use input redirection (so that the builtin grep is used, for example here: https://github.com/llvm/llvm-project/pull/83184/files. I'm not sure if this works with grep though) or use another command like sed?

@jakeegan Already fixed, see #98442.

@jakeegan
Copy link
Member

@ylzsx Thank you!

aaryanshukla pushed a commit to aaryanshukla/llvm-project that referenced this pull request Jul 14, 2024
qiaojbao pushed a commit to GPUOpen-Drivers/llvm-project that referenced this pull request Aug 13, 2024
…f2b3d50fe

Local branch amd-gfx 1d2f2b3 Manual merge remote-tracking branch llvm.org/main into amd-gfx
Remote branch main 626c7ce [LoongArch][clang] Add support for option `-msimd=` and macro `__loongarch_simd_width`. (llvm#97984)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:loongarch 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.

4 participants