Skip to content

Commit c034865

Browse files
Ami-zhangyuxuanchen1997
authored andcommitted
[LoongArch] Support -march=la64v1.0 and -march=la64v1.1 (#100057)
Summary: The newly added strings `la64v1.0` and `la64v1.1` in `-march` are as described in LoongArch toolchains conventions (see [1]). The target-cpu/feature attributes are forwarded to compiler when specifying particular `-march` parameter. The default cpu `loongarch64` is returned when archname is `la64v1.0` or `la64v1.1`. In addition, this commit adds `la64v1.0`/`la64v1.1` to "__loongarch_arch" and adds definition for macro "__loongarch_frecipe". [1]: https://github.com/loongson/la-toolchain-conventions Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60251223
1 parent df03a8a commit c034865

File tree

6 files changed

+88
-7
lines changed

6 files changed

+88
-7
lines changed

clang/lib/Basic/Targets/LoongArch.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,24 @@ void LoongArchTargetInfo::getTargetDefines(const LangOptions &Opts,
200200

201201
// Define __loongarch_arch.
202202
StringRef ArchName = getCPU();
203-
Builder.defineMacro("__loongarch_arch", Twine('"') + ArchName + Twine('"'));
203+
if (ArchName == "loongarch64") {
204+
if (HasFeatureLSX) {
205+
// TODO: As more features of the V1.1 ISA are supported, a unified "v1.1"
206+
// arch feature set will be used to include all sub-features belonging to
207+
// the V1.1 ISA version.
208+
if (HasFeatureFrecipe)
209+
Builder.defineMacro("__loongarch_arch",
210+
Twine('"') + "la64v1.1" + Twine('"'));
211+
else
212+
Builder.defineMacro("__loongarch_arch",
213+
Twine('"') + "la64v1.0" + Twine('"'));
214+
} else {
215+
Builder.defineMacro("__loongarch_arch",
216+
Twine('"') + ArchName + Twine('"'));
217+
}
218+
} else {
219+
Builder.defineMacro("__loongarch_arch", Twine('"') + ArchName + Twine('"'));
220+
}
204221

205222
// Define __loongarch_tune.
206223
StringRef TuneCPU = getTargetOpts().TuneCPU;
@@ -216,6 +233,8 @@ void LoongArchTargetInfo::getTargetDefines(const LangOptions &Opts,
216233
Builder.defineMacro("__loongarch_simd_width", "128");
217234
Builder.defineMacro("__loongarch_sx", Twine(1));
218235
}
236+
if (HasFeatureFrecipe)
237+
Builder.defineMacro("__loongarch_frecipe", Twine(1));
219238

220239
StringRef ABI = getABI();
221240
if (ABI == "lp64d" || ABI == "lp64f" || ABI == "lp64s")
@@ -291,6 +310,8 @@ bool LoongArchTargetInfo::handleTargetFeatures(
291310
HasFeatureLASX = true;
292311
else if (Feature == "-ual")
293312
HasUnalignedAccess = false;
313+
else if (Feature == "+frecipe")
314+
HasFeatureFrecipe = true;
294315
}
295316
return true;
296317
}

clang/lib/Basic/Targets/LoongArch.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class LLVM_LIBRARY_VISIBILITY LoongArchTargetInfo : public TargetInfo {
2929
bool HasFeatureF;
3030
bool HasFeatureLSX;
3131
bool HasFeatureLASX;
32+
bool HasFeatureFrecipe;
3233

3334
public:
3435
LoongArchTargetInfo(const llvm::Triple &Triple, const TargetOptions &)
@@ -37,6 +38,7 @@ class LLVM_LIBRARY_VISIBILITY LoongArchTargetInfo : public TargetInfo {
3738
HasFeatureF = false;
3839
HasFeatureLSX = false;
3940
HasFeatureLASX = false;
41+
HasFeatureFrecipe = false;
4042
LongDoubleWidth = 128;
4143
LongDoubleAlign = 128;
4244
LongDoubleFormat = &llvm::APFloat::IEEEquad();

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,14 @@ std::string loongarch::postProcessTargetCPUString(const std::string &CPU,
267267
std::string loongarch::getLoongArchTargetCPU(const llvm::opt::ArgList &Args,
268268
const llvm::Triple &Triple) {
269269
std::string CPU;
270+
std::string Arch;
270271
// If we have -march, use that.
271-
if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
272-
CPU = A->getValue();
272+
if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
273+
Arch = A->getValue();
274+
if (Arch == "la64v1.0" || Arch == "la64v1.1")
275+
CPU = llvm::LoongArch::getDefaultArch(Triple.isLoongArch64());
276+
else
277+
CPU = Arch;
278+
}
273279
return postProcessTargetCPUString(CPU, Triple);
274280
}

clang/test/Driver/loongarch-march.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22
// RUN: FileCheck %s --check-prefix=CC1-LOONGARCH64
33
// RUN: %clang --target=loongarch64 -march=la464 -fsyntax-only %s -### 2>&1 | \
44
// RUN: FileCheck %s --check-prefix=CC1-LA464
5+
// RUN: %clang --target=loongarch64 -march=la64v1.0 -fsyntax-only %s -### 2>&1 | \
6+
// RUN: FileCheck %s --check-prefix=CC1-LA64V1P0
7+
// RUN: %clang --target=loongarch64 -march=la64v1.1 -fsyntax-only %s -### 2>&1 | \
8+
// RUN: FileCheck %s --check-prefix=CC1-LA64V1P1
59
// RUN: %clang --target=loongarch64 -march=loongarch64 -S -emit-llvm %s -o - | \
610
// RUN: FileCheck %s --check-prefix=IR-LOONGARCH64
711
// RUN: %clang --target=loongarch64 -march=la464 -S -emit-llvm %s -o - | \
812
// RUN: FileCheck %s --check-prefix=IR-LA464
13+
// RUN: %clang --target=loongarch64 -march=la64v1.0 -S -emit-llvm %s -o - | \
14+
// RUN: FileCheck %s --check-prefix=IR-LA64V1P0
15+
// RUN: %clang --target=loongarch64 -march=la64v1.1 -S -emit-llvm %s -o - | \
16+
// RUN: FileCheck %s --check-prefix=IR-LA64V1P1
917

1018
// CC1-LOONGARCH64: "-target-cpu" "loongarch64"
1119
// CC1-LOONGARCH64-NOT: "-target-feature"
@@ -19,8 +27,22 @@
1927
// CC1-LA464-NOT: "-target-feature"
2028
// CC1-LA464: "-target-abi" "lp64d"
2129

30+
// CC1-LA64V1P0: "-target-cpu" "loongarch64"
31+
// CC1-LA64V1P0-NOT: "-target-feature"
32+
// CC1-LA64V1P0: "-target-feature" "+64bit" "-target-feature" "+d" "-target-feature" "+lsx" "-target-feature" "+ual"
33+
// CC1-LA64V1P0-NOT: "-target-feature"
34+
// CC1-LA64V1P0: "-target-abi" "lp64d"
35+
36+
// CC1-LA64V1P1: "-target-cpu" "loongarch64"
37+
// CC1-LA64V1P1-NOT: "-target-feature"
38+
// CC1-LA64V1P1: "-target-feature" "+64bit" "-target-feature" "+d" "-target-feature" "+lsx" "-target-feature" "+ual" "-target-feature" "+frecipe"
39+
// CC1-LA64V1P1-NOT: "-target-feature"
40+
// CC1-LA64V1P1: "-target-abi" "lp64d"
41+
2242
// IR-LOONGARCH64: attributes #[[#]] ={{.*}}"target-cpu"="loongarch64" {{.*}}"target-features"="+64bit,+d,+f,+ual"
2343
// IR-LA464: attributes #[[#]] ={{.*}}"target-cpu"="la464" {{.*}}"target-features"="+64bit,+d,+f,+lasx,+lsx,+ual"
44+
// IR-LA64V1P0: attributes #[[#]] ={{.*}}"target-cpu"="loongarch64" {{.*}}"target-features"="+64bit,+d,+lsx,+ual"
45+
// IR-LA64V1P1: attributes #[[#]] ={{.*}}"target-cpu"="loongarch64" {{.*}}"target-features"="+64bit,+d,+frecipe,+lsx,+ual"
2446

2547
int foo(void) {
2648
return 3;

clang/test/Preprocessor/init-loongarch.c

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -798,24 +798,43 @@
798798
// LA64-FPU0-LP64S-NOT: #define __loongarch_single_float
799799
// LA64-FPU0-LP64S: #define __loongarch_soft_float 1
800800

801-
/// Check __loongarch_arch and __loongarch_tune.
801+
/// Check __loongarch_arch{_tune/_frecipe}.
802802

803803
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - | \
804-
// RUN: FileCheck --match-full-lines --check-prefix=ARCH-TUNE -DARCH=loongarch64 -DTUNE=loongarch64 %s
804+
// RUN: FileCheck --match-full-lines --check-prefix=ARCH-TUNE -DARCH=la64v1.0 -DTUNE=loongarch64 %s
805805
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=loongarch64 | \
806806
// RUN: FileCheck --match-full-lines --check-prefix=ARCH-TUNE -DARCH=loongarch64 -DTUNE=loongarch64 %s
807807
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=la464 | \
808808
// RUN: FileCheck --match-full-lines --check-prefix=ARCH-TUNE -DARCH=la464 -DTUNE=la464 %s
809809
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -mtune=loongarch64 | \
810-
// RUN: FileCheck --match-full-lines --check-prefix=ARCH-TUNE -DARCH=loongarch64 -DTUNE=loongarch64 %s
810+
// RUN: FileCheck --match-full-lines --check-prefix=ARCH-TUNE -DARCH=la64v1.0 -DTUNE=loongarch64 %s
811811
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -mtune=la464 | \
812-
// RUN: FileCheck --match-full-lines --check-prefix=ARCH-TUNE -DARCH=loongarch64 -DTUNE=la464 %s
812+
// RUN: FileCheck --match-full-lines --check-prefix=ARCH-TUNE -DARCH=la64v1.0 -DTUNE=la464 %s
813813
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=loongarch64 -mtune=la464 | \
814814
// RUN: FileCheck --match-full-lines --check-prefix=ARCH-TUNE -DARCH=loongarch64 -DTUNE=la464 %s
815815
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=la464 -mtune=loongarch64 | \
816816
// RUN: FileCheck --match-full-lines --check-prefix=ARCH-TUNE -DARCH=la464 -DTUNE=loongarch64 %s
817+
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=la64v1.0 | \
818+
// RUN: FileCheck --match-full-lines --check-prefix=ARCH-TUNE -DARCH=la64v1.0 -DTUNE=loongarch64 %s
819+
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=la64v1.0 -Xclang -target-feature -Xclang -lsx | \
820+
// RUN: FileCheck --match-full-lines --check-prefix=ARCH-TUNE -DARCH=loongarch64 -DTUNE=loongarch64 %s
821+
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=la64v1.0 -Xclang -target-feature -Xclang +frecipe | \
822+
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,FRECIPE -DARCH=la64v1.1 -DTUNE=loongarch64 %s
823+
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=loongarch64 -Xclang -target-feature -Xclang +lsx | \
824+
// RUN: FileCheck --match-full-lines --check-prefix=ARCH-TUNE -DARCH=la64v1.0 -DTUNE=loongarch64 %s
825+
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=la64v1.1 | \
826+
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,FRECIPE -DARCH=la64v1.1 -DTUNE=loongarch64 %s
827+
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=la64v1.1 -Xclang -target-feature -Xclang -frecipe | \
828+
// RUN: FileCheck --match-full-lines --check-prefix=ARCH-TUNE -DARCH=la64v1.0 -DTUNE=loongarch64 %s
829+
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=la64v1.1 -Xclang -target-feature -Xclang -lsx | \
830+
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,FRECIPE -DARCH=loongarch64 -DTUNE=loongarch64 %s
831+
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=loongarch64 -Xclang -target-feature -Xclang +frecipe | \
832+
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,FRECIPE -DARCH=loongarch64 -DTUNE=loongarch64 %s
833+
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=loongarch64 -Xclang -target-feature -Xclang +lsx -Xclang -target-feature -Xclang +frecipe | \
834+
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,FRECIPE -DARCH=la64v1.1 -DTUNE=loongarch64 %s
817835

818836
// ARCH-TUNE: #define __loongarch_arch "[[ARCH]]"
837+
// FRECIPE: #define __loongarch_frecipe 1
819838
// ARCH-TUNE: #define __loongarch_tune "[[TUNE]]"
820839

821840
// RUN: %clang --target=loongarch64 -mlsx -x c -E -dM %s -o - \

llvm/lib/TargetParser/LoongArchTargetParser.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ bool LoongArch::getArchFeatures(StringRef Arch,
4444
return true;
4545
}
4646
}
47+
48+
if (Arch == "la64v1.0" || Arch == "la64v1.1") {
49+
Features.push_back("+64bit");
50+
Features.push_back("+d");
51+
Features.push_back("+lsx");
52+
Features.push_back("+ual");
53+
if (Arch == "la64v1.1")
54+
Features.push_back("+frecipe");
55+
return true;
56+
}
57+
4758
return false;
4859
}
4960

0 commit comments

Comments
 (0)