Skip to content

Commit 626c7ce

Browse files
authored
[LoongArch][clang] Add support for option -msimd= and macro __loongarch_simd_width. (#97984)
1 parent bb8998d commit 626c7ce

File tree

6 files changed

+172
-2
lines changed

6 files changed

+172
-2
lines changed

Diff for: clang/include/clang/Basic/DiagnosticDriverKinds.td

+2
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,8 @@ def err_drv_loongarch_wrong_fpu_width : Error<
791791
"wrong fpu width; %select{LSX|LASX}0 depends on 64-bit FPU">;
792792
def err_drv_loongarch_invalid_simd_option_combination : Error<
793793
"invalid option combination; LASX depends on LSX">;
794+
def err_drv_loongarch_invalid_msimd_EQ : Error<
795+
"invalid argument '%0' to -msimd=; must be one of: none, lsx, lasx">;
794796

795797
def err_drv_expand_response_file : Error<
796798
"failed to expand response file: %0">;

Diff for: clang/include/clang/Driver/Options.td

+3
Original file line numberDiff line numberDiff line change
@@ -5252,6 +5252,9 @@ def mlasx : Flag<["-"], "mlasx">, Group<m_loongarch_Features_Group>,
52525252
HelpText<"Enable Loongson Advanced SIMD Extension (LASX).">;
52535253
def mno_lasx : Flag<["-"], "mno-lasx">, Group<m_loongarch_Features_Group>,
52545254
HelpText<"Disable Loongson Advanced SIMD Extension (LASX).">;
5255+
def msimd_EQ : Joined<["-"], "msimd=">, Group<m_loongarch_Features_Group>,
5256+
Flags<[TargetSpecific]>,
5257+
HelpText<"Select the SIMD extension(s) to be enabled in LoongArch either 'none', 'lsx', 'lasx'.">;
52555258
def mnop_mcount : Flag<["-"], "mnop-mcount">, HelpText<"Generate mcount/__fentry__ calls as nops. To activate they need to be patched in.">,
52565259
Visibility<[ClangOption, CC1Option]>, Group<m_Group>,
52575260
MarshallingInfoFlag<CodeGenOpts<"MNopMCount">>;

Diff for: clang/lib/Basic/Targets/LoongArch.cpp

+6-2
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")

Diff for: clang/lib/Driver/ToolChains/Arch/LoongArch.cpp

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

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

Diff for: clang/test/Driver/loongarch-msimd.c

+129
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"

Diff for: clang/test/Preprocessor/init-loongarch.c

+3
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,7 @@
827827
// RUN: %clang --target=loongarch64 -mno-lasx -mlsx -x c -E -dM %s -o - \
828828
// RUN: | FileCheck --match-full-lines --check-prefix=MLSX %s
829829
// MLSX-NOT: #define __loongarch_asx
830+
// MLSX: #define __loongarch_simd_width 128
830831
// MLSX: #define __loongarch_sx 1
831832

832833
// RUN: %clang --target=loongarch64 -mlasx -x c -E -dM %s -o - \
@@ -838,6 +839,7 @@
838839
// RUN: %clang --target=loongarch64 -mlasx -mlsx -x c -E -dM %s -o - \
839840
// RUN: | FileCheck --match-full-lines --check-prefix=MLASX %s
840841
// MLASX: #define __loongarch_asx 1
842+
// MLASX: #define __loongarch_simd_width 256
841843
// MLASX: #define __loongarch_sx 1
842844

843845
// RUN: %clang --target=loongarch64 -mno-lsx -x c -E -dM %s -o - \
@@ -851,4 +853,5 @@
851853
// RUN: %clang --target=loongarch64 -mno-lasx -x c -E -dM %s -o - \
852854
// RUN: | FileCheck --match-full-lines --check-prefix=MNO-LSX %s
853855
// MNO-LSX-NOT: #define __loongarch_asx
856+
// MNO-LSX-NOT: #define __loongarch_simd_width
854857
// MNO-LSX-NOT: #define __loongarch_sx

0 commit comments

Comments
 (0)