Skip to content

Commit 75f907b

Browse files
ylzsxAmi-zhang
authored andcommitted
[LoongArch][clang] Add support for option -msimd= and macro __loongarch_simd_width. (llvm#97984)
(cherry picked from commit 626c7ce) Change-Id: If6cbc4a15eb2445a79c3f0ed3e8bfadcb1ce91ae
1 parent 2a30bd5 commit 75f907b

File tree

6 files changed

+172
-2
lines changed

6 files changed

+172
-2
lines changed

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,8 @@ def err_drv_loongarch_wrong_fpu_width_for_lasx : Error<
738738
"wrong fpu width; LASX depends on 64-bit FPU.">;
739739
def err_drv_loongarch_invalid_simd_option_combination : Error<
740740
"invalid option combination; LASX depends on LSX.">;
741+
def err_drv_loongarch_invalid_msimd_EQ : Error<
742+
"invalid argument '%0' to -msimd=; must be one of: none, lsx, lasx">;
741743

742744
def err_drv_expand_response_file : Error<
743745
"failed to expand response file: %0">;

clang/include/clang/Driver/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4200,6 +4200,9 @@ def mlasx : Flag<["-"], "mlasx">, Group<m_loongarch_Features_Group>,
42004200
HelpText<"Enable Loongson Advanced SIMD Extension (LASX).">;
42014201
def mno_lasx : Flag<["-"], "mno-lasx">, Group<m_loongarch_Features_Group>,
42024202
HelpText<"Disable Loongson Advanced SIMD Extension (LASX).">;
4203+
def msimd_EQ : Joined<["-"], "msimd=">, Group<m_loongarch_Features_Group>,
4204+
Flags<[TargetSpecific]>,
4205+
HelpText<"Select the SIMD extension(s) to be enabled in LoongArch either 'none', 'lsx', 'lasx'.">;
42034206
def mnop_mcount : Flag<["-"], "mnop-mcount">, HelpText<"Generate mcount/__fentry__ calls as nops. To activate they need to be patched in.">,
42044207
Flags<[CC1Option]>, Group<m_Group>,
42054208
MarshallingInfoFlag<CodeGenOpts<"MNopMCount">>;

clang/lib/Basic/Targets/LoongArch.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,14 @@ void LoongArchTargetInfo::getTargetDefines(const LangOptions &Opts,
208208
TuneCPU = ArchName;
209209
Builder.defineMacro("__loongarch_tune", Twine('"') + TuneCPU + Twine('"'));
210210

211-
if (HasFeatureLSX)
211+
if (HasFeatureLASX) {
212+
Builder.defineMacro("__loongarch_simd_width", "256");
212213
Builder.defineMacro("__loongarch_sx", Twine(1));
213-
if (HasFeatureLASX)
214214
Builder.defineMacro("__loongarch_asx", Twine(1));
215+
} else if (HasFeatureLSX) {
216+
Builder.defineMacro("__loongarch_simd_width", "128");
217+
Builder.defineMacro("__loongarch_sx", Twine(1));
218+
}
215219

216220
StringRef ABI = getABI();
217221
if (ABI == "lp64d" || ABI == "lp64f" || ABI == "lp64s")

clang/lib/Driver/ToolChains/Arch/LoongArch.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,35 @@ void loongarch::getLoongArchTargetFeatures(const Driver &D,
207207
} else /*-mno-lasx*/
208208
Features.push_back("-lasx");
209209
}
210+
211+
// Select lsx/lasx feature determined by -msimd=.
212+
// Option -msimd= has lower priority than -m[no-]lsx and -m[no-]lasx.
213+
if (const Arg *A = Args.getLastArg(options::OPT_msimd_EQ)) {
214+
StringRef MSIMD = A->getValue();
215+
if (MSIMD == "lsx") {
216+
// Option -msimd=lsx depends on 64-bit FPU.
217+
// -m*-float and -mfpu=none/0/32 conflict with -mlsx.
218+
if (llvm::find(Features, "-d") != Features.end())
219+
D.Diag(diag::err_drv_loongarch_wrong_fpu_width) << /*LSX*/ 0;
220+
// The previous option does not contain feature -lsx.
221+
else if (llvm::find(Features, "-lsx") == Features.end())
222+
Features.push_back("+lsx");
223+
} else if (MSIMD == "lasx") {
224+
// Option -msimd=lasx depends on 64-bit FPU and LSX.
225+
// -m*-float and -mfpu=none/0/32 conflict with -mlsx.
226+
if (llvm::find(Features, "-d") != Features.end())
227+
D.Diag(diag::err_drv_loongarch_wrong_fpu_width) << /*LASX*/ 1;
228+
else if (llvm::find(Features, "-lsx") != Features.end())
229+
D.Diag(diag::err_drv_loongarch_invalid_simd_option_combination);
230+
// The previous option does not contain feature -lasx.
231+
else if (llvm::find(Features, "-lasx") == Features.end()) {
232+
Features.push_back("+lsx");
233+
Features.push_back("+lasx");
234+
}
235+
} else if (MSIMD != "none") {
236+
D.Diag(diag::err_drv_loongarch_invalid_msimd_EQ) << MSIMD;
237+
}
238+
}
210239
}
211240

212241
std::string loongarch::postProcessTargetCPUString(const std::string &CPU,

clang/test/Driver/loongarch-msimd.c

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/// Test -msimd options.
2+
3+
/// COM: -msimd=none
4+
// RUN: %clang --target=loongarch64 -mlasx -msimd=none -fsyntax-only %s -### 2>&1 | \
5+
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
6+
// RUN: FileCheck %s --check-prefixes=LSX,LASX
7+
// RUN: %clang --target=loongarch64 -mlasx -mlsx -msimd=none -fsyntax-only %s -### 2>&1 | \
8+
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
9+
// RUN: FileCheck %s --check-prefixes=LSX,LASX
10+
11+
// RUN: %clang --target=loongarch64 -msimd=none -fsyntax-only %s -### 2>&1 | \
12+
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
13+
// RUN: FileCheck %s --check-prefixes=NOLSX,NOLASX
14+
// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -msimd=none -fsyntax-only %s -### 2>&1 | \
15+
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
16+
// RUN: FileCheck %s --check-prefixes=NOLSX,NOLASX
17+
// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -mlsx -mno-lsx -msimd=none -fsyntax-only %s -### 2>&1 | \
18+
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
19+
// RUN: FileCheck %s --check-prefixes=NOLSX,NOLASX
20+
// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -mno-lsx -msimd=none -fsyntax-only %s -### 2>&1 | \
21+
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
22+
// RUN: FileCheck %s --check-prefixes=NOLSX,NOLASX
23+
// RUN: %clang --target=loongarch64 -mlsx -mno-lsx -msimd=none -fsyntax-only %s -### 2>&1 | \
24+
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
25+
// RUN: FileCheck %s --check-prefixes=NOLSX,NOLASX
26+
// RUN: %clang --target=loongarch64 -mno-lasx -msimd=none -fsyntax-only %s -### 2>&1 | \
27+
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
28+
// RUN: FileCheck %s --check-prefixes=NOLSX,NOLASX
29+
// RUN: %clang --target=loongarch64 -mno-lasx -mlsx -mno-lsx -msimd=none -fsyntax-only %s -### 2>&1 | \
30+
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
31+
// RUN: FileCheck %s --check-prefixes=NOLSX,NOLASX
32+
// RUN: %clang --target=loongarch64 -mno-lasx -mno-lsx -msimd=none -fsyntax-only %s -### 2>&1 | \
33+
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
34+
// RUN: FileCheck %s --check-prefixes=NOLSX,NOLASX
35+
// RUN: %clang --target=loongarch64 -mno-lsx -msimd=none -fsyntax-only %s -### 2>&1 | \
36+
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
37+
// RUN: FileCheck %s --check-prefixes=NOLSX,NOLASX
38+
39+
// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -mlsx -msimd=none -fsyntax-only %s -### 2>&1 | \
40+
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
41+
// RUN: FileCheck %s --check-prefixes=LSX,NOLASX
42+
// RUN: %clang --target=loongarch64 -mno-lasx -mlsx -msimd=none -fsyntax-only %s -### 2>&1 | \
43+
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
44+
// RUN: FileCheck %s --check-prefixes=LSX,NOLASX
45+
// RUN: %clang --target=loongarch64 -mlsx -msimd=none -fsyntax-only %s -### 2>&1 | \
46+
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
47+
// RUN: FileCheck %s --check-prefixes=LSX,NOLASX
48+
49+
50+
/// COM: -msimd=lsx
51+
// RUN: %clang --target=loongarch64 -mlasx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
52+
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
53+
// RUN: FileCheck %s --check-prefixes=LSX,LASX
54+
// RUN: %clang --target=loongarch64 -mlasx -mlsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
55+
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
56+
// RUN: FileCheck %s --check-prefixes=LSX,LASX
57+
58+
// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -mno-lsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
59+
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
60+
// RUN: FileCheck %s --check-prefixes=NOLSX,NOLASX
61+
// RUN: %clang --target=loongarch64 -mlsx -mno-lsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
62+
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
63+
// RUN: FileCheck %s --check-prefixes=NOLSX,NOLASX
64+
// RUN: %clang --target=loongarch64 -mno-lasx -mlsx -mno-lsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
65+
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
66+
// RUN: FileCheck %s --check-prefixes=NOLSX,NOLASX
67+
// RUN: %clang --target=loongarch64 -mno-lasx -mno-lsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
68+
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
69+
// RUN: FileCheck %s --check-prefixes=NOLSX,NOLASX
70+
// RUN: %clang --target=loongarch64 -mno-lsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
71+
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
72+
// RUN: FileCheck %s --check-prefixes=NOLSX,NOLASX
73+
// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -mlsx -mno-lsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
74+
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
75+
// RUN: FileCheck %s --check-prefixes=NOLSX,NOLASX
76+
77+
// RUN: %clang --target=loongarch64 -msimd=lsx -fsyntax-only %s -### 2>&1 | \
78+
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
79+
// RUN: FileCheck %s --check-prefixes=LSX,NOLASX
80+
// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
81+
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
82+
// RUN: FileCheck %s --check-prefixes=LSX,NOLASX
83+
// RUN: %clang --target=loongarch64 -mlsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
84+
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
85+
// RUN: FileCheck %s --check-prefixes=LSX,NOLASX
86+
// RUN: %clang --target=loongarch64 -mno-lasx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
87+
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
88+
// RUN: FileCheck %s --check-prefixes=LSX,NOLASX
89+
// RUN: %clang --target=loongarch64 -mno-lasx -mlsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
90+
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
91+
// RUN: FileCheck %s --check-prefixes=LSX,NOLASX
92+
93+
94+
/// COM: -msimd=lasx
95+
// RUN: %clang --target=loongarch64 -msimd=lasx -fsyntax-only %s -### 2>&1 | \
96+
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
97+
// RUN: FileCheck %s --check-prefixes=LSX,LASX
98+
// RUN: %clang --target=loongarch64 -mlasx -msimd=lasx -fsyntax-only %s -### 2>&1 | \
99+
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
100+
// RUN: FileCheck %s --check-prefixes=LSX,LASX
101+
// RUN: %clang --target=loongarch64 -mlasx -mlsx -msimd=lasx -fsyntax-only %s -### 2>&1 | \
102+
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
103+
// RUN: FileCheck %s --check-prefixes=LSX,LASX
104+
// RUN: %clang --target=loongarch64 -mlsx -msimd=lasx -fsyntax-only %s -### 2>&1 | \
105+
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
106+
// RUN: FileCheck %s --check-prefixes=LSX,LASX
107+
108+
// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -msimd=lasx -fsyntax-only %s -### 2>&1 | \
109+
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
110+
// RUN: FileCheck %s --check-prefixes=NOLSX,NOLASX
111+
// RUN: %clang --target=loongarch64 -mno-lasx -msimd=lasx -fsyntax-only %s -### 2>&1 | \
112+
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
113+
// RUN: FileCheck %s --check-prefixes=NOLSX,NOLASX
114+
115+
// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -mlsx -msimd=lasx -fsyntax-only %s -### 2>&1 | \
116+
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
117+
// RUN: FileCheck %s --check-prefixes=LSX,NOLASX
118+
// RUN: %clang --target=loongarch64 -mno-lasx -mlsx -msimd=lasx -fsyntax-only %s -### 2>&1 | \
119+
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
120+
// RUN: FileCheck %s --check-prefixes=LSX,NOLASX
121+
// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -mlsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
122+
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
123+
// RUN: FileCheck %s --check-prefixes=LSX,NOLASX
124+
125+
126+
// LSX: "-target-feature" "+lsx"
127+
// LASX: "-target-feature" "+lasx"
128+
// NOLSX-NOT: "-target-feature" "+lsx"
129+
// NOLASX-NOT: "-target-feature" "+lasx"

clang/test/Preprocessor/init-loongarch.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,7 @@
817817
// RUN: %clang --target=loongarch64 -mno-lasx -mlsx -x c -E -dM %s -o - \
818818
// RUN: | FileCheck --match-full-lines --check-prefix=MLSX %s
819819
// MLSX-NOT: #define __loongarch_asx
820+
// MLSX: #define __loongarch_simd_width 128
820821
// MLSX: #define __loongarch_sx 1
821822

822823
// RUN: %clang --target=loongarch64 -mlasx -x c -E -dM %s -o - \
@@ -828,6 +829,7 @@
828829
// RUN: %clang --target=loongarch64 -mlasx -mlsx -x c -E -dM %s -o - \
829830
// RUN: | FileCheck --match-full-lines --check-prefix=MLASX %s
830831
// MLASX: #define __loongarch_asx 1
832+
// MLASX: #define __loongarch_simd_width 256
831833
// MLASX: #define __loongarch_sx 1
832834

833835
// RUN: %clang --target=loongarch64 -mno-lsx -x c -E -dM %s -o - \
@@ -841,4 +843,5 @@
841843
// RUN: %clang --target=loongarch64 -mno-lasx -x c -E -dM %s -o - \
842844
// RUN: | FileCheck --match-full-lines --check-prefix=MNO-LSX %s
843845
// MNO-LSX-NOT: #define __loongarch_asx
846+
// MNO-LSX-NOT: #define __loongarch_simd_width
844847
// MNO-LSX-NOT: #define __loongarch_sx

0 commit comments

Comments
 (0)