Skip to content

Commit 88daeba

Browse files
committed
Support amcas[_db].{b/h/w/d} instructions.
1 parent 2ed8c5d commit 88daeba

File tree

15 files changed

+5542
-20
lines changed

15 files changed

+5542
-20
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5413,6 +5413,10 @@ def mlam_bh : Flag<["-"], "mlam-bh">, Group<m_loongarch_Features_Group>,
54135413
HelpText<"Enable amswap[_db].{b/h} and amadd[_db].{b/h}">;
54145414
def mno_lam_bh : Flag<["-"], "mno-lam-bh">, Group<m_loongarch_Features_Group>,
54155415
HelpText<"Disable amswap[_db].{b/h} and amadd[_db].{b/h}">;
5416+
def mlamcas : Flag<["-"], "mlamcas">, Group<m_loongarch_Features_Group>,
5417+
HelpText<"Enable amcas[_db].{b/h/w/d}">;
5418+
def mno_lamcas : Flag<["-"], "mno-lamcas">, Group<m_loongarch_Features_Group>,
5419+
HelpText<"Disable amcas[_db].{b/h/w/d}">;
54165420
def mld_seq_sa : Flag<["-"], "mld-seq-sa">, Group<m_loongarch_Features_Group>,
54175421
HelpText<"Do not generate load-load barrier instructions (dbar 0x700)">;
54185422
def mno_ld_seq_sa : Flag<["-"], "mno-ld-seq-sa">, Group<m_loongarch_Features_Group>,

clang/lib/Basic/Targets/LoongArch.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ void LoongArchTargetInfo::getTargetDefines(const LangOptions &Opts,
205205
// TODO: As more features of the V1.1 ISA are supported, a unified "v1.1"
206206
// arch feature set will be used to include all sub-features belonging to
207207
// the V1.1 ISA version.
208-
if (HasFeatureFrecipe && HasFeatureLAM_BH && HasFeatureLD_SEQ_SA)
208+
if (HasFeatureFrecipe && HasFeatureLAM_BH && HasFeatureLAMCAS && HasFeatureLD_SEQ_SA)
209209
Builder.defineMacro("__loongarch_arch",
210210
Twine('"') + "la64v1.1" + Twine('"'));
211211
else
@@ -239,6 +239,9 @@ void LoongArchTargetInfo::getTargetDefines(const LangOptions &Opts,
239239
if (HasFeatureLAM_BH)
240240
Builder.defineMacro("__loongarch_lam_bh", Twine(1));
241241

242+
if (HasFeatureLAMCAS)
243+
Builder.defineMacro("__loongarch_lamcas", Twine(1));
244+
242245
if (HasFeatureLD_SEQ_SA)
243246
Builder.defineMacro("__loongarch_ld_seq_sa", Twine(1));
244247

@@ -320,6 +323,8 @@ bool LoongArchTargetInfo::handleTargetFeatures(
320323
HasFeatureFrecipe = true;
321324
else if (Feature == "+lam-bh")
322325
HasFeatureLAM_BH = true;
326+
else if (Feature == "+lamcas")
327+
HasFeatureLAMCAS = true;
323328
else if (Feature == "+ld-seq-sa")
324329
HasFeatureLD_SEQ_SA = true;
325330
}

clang/lib/Basic/Targets/LoongArch.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class LLVM_LIBRARY_VISIBILITY LoongArchTargetInfo : public TargetInfo {
3131
bool HasFeatureLASX;
3232
bool HasFeatureFrecipe;
3333
bool HasFeatureLAM_BH;
34+
bool HasFeatureLAMCAS;
3435
bool HasFeatureLD_SEQ_SA;
3536

3637
public:
@@ -42,6 +43,7 @@ class LLVM_LIBRARY_VISIBILITY LoongArchTargetInfo : public TargetInfo {
4243
HasFeatureLASX = false;
4344
HasFeatureFrecipe = false;
4445
HasFeatureLAM_BH = false;
46+
HasFeatureLAMCAS = false;
4547
HasFeatureLD_SEQ_SA = false;
4648
LongDoubleWidth = 128;
4749
LongDoubleAlign = 128;

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,15 @@ void loongarch::getLoongArchTargetFeatures(const Driver &D,
275275
Features.push_back("-lam-bh");
276276
}
277277

278+
// Select lamcas feature determined by -m[no-]lamcas.
279+
if (const Arg *A =
280+
Args.getLastArg(options::OPT_mlamcas, options::OPT_mno_lamcas)) {
281+
if (A->getOption().matches(options::OPT_mlamcas))
282+
Features.push_back("+lamcas");
283+
else
284+
Features.push_back("-lamcas");
285+
}
286+
278287
// Select ld-seq-sa feature determined by -m[no-]ld-seq-sa.
279288
if (const Arg *A = Args.getLastArg(options::OPT_mld_seq_sa,
280289
options::OPT_mno_ld_seq_sa)) {

clang/test/Driver/loongarch-march.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,21 @@
3939

4040
// CC1-LA64V1P1: "-target-cpu" "loongarch64"
4141
// CC1-LA64V1P1-NOT: "-target-feature"
42-
// CC1-LA64V1P1: "-target-feature" "+64bit" "-target-feature" "+d" "-target-feature" "+lsx" "-target-feature" "+ual" "-target-feature" "+frecipe" "-target-feature" "+lam-bh" "-target-feature" "+ld-seq-sa"
42+
// CC1-LA64V1P1: "-target-feature" "+64bit" "-target-feature" "+d" "-target-feature" "+lsx" "-target-feature" "+ual" "-target-feature" "+frecipe" "-target-feature" "+lam-bh" "-target-feature" "+lamcas" "-target-feature" "+ld-seq-sa"
4343
// CC1-LA64V1P1-NOT: "-target-feature"
4444
// CC1-LA64V1P1: "-target-abi" "lp64d"
4545

4646
// CC1-LA664: "-target-cpu" "la664"
4747
// CC1-LA664-NOT: "-target-feature"
48-
// CC1-LA664: "-target-feature" "+64bit" "-target-feature" "+f" "-target-feature" "+d" "-target-feature" "+lsx" "-target-feature" "+lasx" "-target-feature" "+ual" "-target-feature" "+frecipe" "-target-feature" "+lam-bh" "-target-feature" "+ld-seq-sa"
48+
// CC1-LA664: "-target-feature" "+64bit" "-target-feature" "+f" "-target-feature" "+d" "-target-feature" "+lsx" "-target-feature" "+lasx" "-target-feature" "+ual" "-target-feature" "+frecipe" "-target-feature" "+lam-bh" "-target-feature" "+lamcas" "-target-feature" "+ld-seq-sa"
4949
// CC1-LA664-NOT: "-target-feature"
5050
// CC1-LA664: "-target-abi" "lp64d"
5151

5252
// IR-LOONGARCH64: attributes #[[#]] ={{.*}}"target-cpu"="loongarch64" {{.*}}"target-features"="+64bit,+d,+f,+ual"
5353
// IR-LA464: attributes #[[#]] ={{.*}}"target-cpu"="la464" {{.*}}"target-features"="+64bit,+d,+f,+lasx,+lsx,+ual"
5454
// IR-LA64V1P0: attributes #[[#]] ={{.*}}"target-cpu"="loongarch64" {{.*}}"target-features"="+64bit,+d,+lsx,+ual"
55-
// IR-LA64V1P1: attributes #[[#]] ={{.*}}"target-cpu"="loongarch64" {{.*}}"target-features"="+64bit,+d,+frecipe,+lam-bh,+ld-seq-sa,+lsx,+ual"
56-
// IR-LA664: attributes #[[#]] ={{.*}}"target-cpu"="la664" {{.*}}"target-features"="+64bit,+d,+f,+frecipe,+lam-bh,+lasx,+ld-seq-sa,+lsx,+ual"
55+
// IR-LA64V1P1: attributes #[[#]] ={{.*}}"target-cpu"="loongarch64" {{.*}}"target-features"="+64bit,+d,+frecipe,+lam-bh,+lamcas,+ld-seq-sa,+lsx,+ual"
56+
// IR-LA664: attributes #[[#]] ={{.*}}"target-cpu"="la664" {{.*}}"target-features"="+64bit,+d,+f,+frecipe,+lam-bh,+lamcas,+ld-seq-sa,+lasx,+lsx,+ual"
5757

5858
int foo(void) {
5959
return 3;

clang/test/Driver/loongarch-mlamcas.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/// Test -m[no]lamcas options.
2+
3+
// RUN: %clang --target=loongarch64 -mlamcas -fsyntax-only %s -### 2>&1 | \
4+
// RUN: FileCheck %s --check-prefix=CC1-LAMCAS
5+
// RUN: %clang --target=loongarch64 -mno-lamcas -fsyntax-only %s -### 2>&1 | \
6+
// RUN: FileCheck %s --check-prefix=CC1-NO-LAMCAS
7+
// RUN: %clang --target=loongarch64 -mno-lamcas -mlamcas -fsyntax-only %s -### 2>&1 | \
8+
// RUN: FileCheck %s --check-prefix=CC1-LAMCAS
9+
// RUN: %clang --target=loongarch64 -mlamcas -mno-lamcas -fsyntax-only %s -### 2>&1 | \
10+
// RUN: FileCheck %s --check-prefix=CC1-NO-LAMCAS
11+
12+
// RUN: %clang --target=loongarch64 -mlamcas -S -emit-llvm %s -o - | \
13+
// RUN: FileCheck %s --check-prefix=IR-LAMCAS
14+
// RUN: %clang --target=loongarch64 -mno-lamcas -S -emit-llvm %s -o - | \
15+
// RUN: FileCheck %s --check-prefix=IR-NO-LAMCAS
16+
// RUN: %clang --target=loongarch64 -mno-lamcas -mlamcas -S -emit-llvm %s -o - | \
17+
// RUN: FileCheck %s --check-prefix=IR-LAMCAS
18+
// RUN: %clang --target=loongarch64 -mlamcas -mno-lamcas -S -emit-llvm %s -o - | \
19+
// RUN: FileCheck %s --check-prefix=IR-NO-LAMCAS
20+
21+
22+
// CC1-LAMCAS: "-target-feature" "+lamcas"
23+
// CC1-NO-LAMCAS: "-target-feature" "-lamcas"
24+
25+
// IR-LAMCAS: attributes #[[#]] ={{.*}}"target-features"="{{(.*,)?}}+lamcas{{(,.*)?}}"
26+
// IR-NO-LAMCAS: attributes #[[#]] ={{.*}}"target-features"="{{(.*,)?}}-lamcas{{(,.*)?}}"
27+
28+
int foo(void) {
29+
return 42;
30+
}

clang/test/Preprocessor/init-loongarch.c

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

801-
/// Check __loongarch_arch{_tune/_frecipe/_lam_bh/_ld_seq_sa}.
801+
/// Check __loongarch_arch{_tune/_frecipe/_lam_bh/_lamcas/_ld_seq_sa}.
802802

803803
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - | \
804804
// RUN: FileCheck --match-full-lines --check-prefix=ARCH-TUNE -DARCH=la64v1.0 -DTUNE=loongarch64 %s
@@ -823,23 +823,31 @@
823823
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=loongarch64 -Xclang -target-feature -Xclang +lsx | \
824824
// RUN: FileCheck --match-full-lines --check-prefix=ARCH-TUNE -DARCH=la64v1.0 -DTUNE=loongarch64 %s
825825
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=la64v1.1 | \
826-
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,FRECIPE,LAM-BH,LD-SEQ-SA -DARCH=la64v1.1 -DTUNE=loongarch64 %s
826+
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,FRECIPE,LAM-BH,LAMCAS,LD-SEQ-SA -DARCH=la64v1.1 -DTUNE=loongarch64 %s
827827
// 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-prefixes=ARCH-TUNE,LAM-BH,LD-SEQ-SA -DARCH=la64v1.0 -DTUNE=loongarch64 %s
828+
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,LAM-BH,LAMCAS,lD-SEQ-SA -DARCH=la64v1.0 -DTUNE=loongarch64 %s
829829
// 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,LAM-BH,LD-SEQ-SA -DARCH=loongarch64 -DTUNE=loongarch64 %s
830+
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,FRECIPE,LAM-BH,LAMCAS,LD-SEQ-SA -DARCH=loongarch64 -DTUNE=loongarch64 %s
831831
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=loongarch64 -Xclang -target-feature -Xclang +frecipe | \
832832
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,FRECIPE -DARCH=loongarch64 -DTUNE=loongarch64 %s
833833
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=loongarch64 -Xclang -target-feature -Xclang +lsx -Xclang -target-feature -Xclang +frecipe | \
834834
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,FRECIPE -DARCH=la64v1.0 -DTUNE=loongarch64 %s
835835
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=la64v1.0 -Xclang -target-feature -Xclang +lam-bh | \
836836
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,LAM-BH -DARCH=la64v1.0 -DTUNE=loongarch64 %s
837837
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=la64v1.1 -Xclang -target-feature -Xclang -lam-bh | \
838-
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,FRECIPE,LD-SEQ-SA -DARCH=la64v1.0 -DTUNE=loongarch64 %s
838+
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,FRECIPE,LAMCAS,LD-SEQ-SA -DARCH=la64v1.0 -DTUNE=loongarch64 %s
839839
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=loongarch64 -Xclang -target-feature -Xclang +lam-bh | \
840840
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,LAM-BH -DARCH=loongarch64 -DTUNE=loongarch64 %s
841841
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=loongarch64 -Xclang -target-feature -Xclang +lsx -Xclang -target-feature -Xclang +lam-bh | \
842842
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,LAM-BH -DARCH=la64v1.0 -DTUNE=loongarch64 %s
843+
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=la64v1.0 -Xclang -target-feature -Xclang +lamcas | \
844+
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,LAMCAS -DARCH=la64v1.0 -DTUNE=loongarch64 %s
845+
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=la64v1.1 -Xclang -target-feature -Xclang -lamcas | \
846+
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,FRECIPE,LAM-BH,LD-SEQ-SA -DARCH=la64v1.0 -DTUNE=loongarch64 %s
847+
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=loongarch64 -Xclang -target-feature -Xclang +lamcas | \
848+
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,LAMCAS -DARCH=loongarch64 -DTUNE=loongarch64 %s
849+
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=loongarch64 -Xclang -target-feature -Xclang +lsx -Xclang -target-feature -Xclang +lamcas | \
850+
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,LAMCAS -DARCH=la64v1.0 -DTUNE=loongarch64 %s
843851
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=la64v1.0 -Xclang -target-feature -Xclang +ld-seq-sa | \
844852
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,LD-SEQ-SA -DARCH=la64v1.0 -DTUNE=loongarch64 %s
845853
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=la64v1.1 -Xclang -target-feature -Xclang -ld-seq-sa | \
@@ -848,20 +856,21 @@
848856
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,LD-SEQ-SA -DARCH=loongarch64 -DTUNE=loongarch64 %s
849857
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=loongarch64 -Xclang -target-feature -Xclang +lsx -Xclang -target-feature -Xclang +ld-seq-sa | \
850858
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,LD-SEQ-SA -DARCH=la64v1.0 -DTUNE=loongarch64 %s
851-
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=la64v1.0 -Xclang -target-feature -Xclang +frecipe -Xclang -target-feature -Xclang +lam-bh -Xclang -target-feature -Xclang +ld-seq-sa | \
859+
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=la64v1.0 -Xclang -target-feature -Xclang +frecipe -Xclang -target-feature -Xclang +lam-bh -Xclang -target-feature -Xclang +lamcas -Xclang -target-feature -Xclang +ld-seq-sa | \
852860
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE -DARCH=la64v1.1 -DTUNE=loongarch64 %s
853861
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=la664 | \
854-
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,FRECIPE,LAM-BH,LD-SEQ-SA -DARCH=la664 -DTUNE=la664 %s
862+
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,FRECIPE,LAM-BH,LAMCAS,LD-SEQ-SA -DARCH=la664 -DTUNE=la664 %s
855863
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -mtune=la664 | \
856864
// RUN: FileCheck --match-full-lines --check-prefix=ARCH-TUNE -DARCH=la64v1.0 -DTUNE=la664 %s
857865
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=loongarch64 -mtune=la664 | \
858866
// RUN: FileCheck --match-full-lines --check-prefix=ARCH-TUNE -DARCH=loongarch64 -DTUNE=la664 %s
859867
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=la664 -mtune=loongarch64 | \
860-
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,FRECIPE,LAM-BH,LD-SEQ-SA -DARCH=la664 -DTUNE=loongarch64 %s
868+
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,FRECIPE,LAM-BH,LAMCAS,LD-SEQ-SA -DARCH=la664 -DTUNE=loongarch64 %s
861869

862870
// ARCH-TUNE: #define __loongarch_arch "[[ARCH]]"
863871
// FRECIPE: #define __loongarch_frecipe 1
864872
// LAM-BH: #define __loongarch_lam_bh 1
873+
// LAMCAS: #define __loongarch_lamcas 1
865874
// LD-SEQ-SA: #define __loongarch_ld_seq_sa 1
866875
// ARCH-TUNE: #define __loongarch_tune "[[TUNE]]"
867876

llvm/include/llvm/TargetParser/LoongArchTargetParser.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ LOONGARCH_FEATURE("+lvz", FK_LVZ)
1212
LOONGARCH_FEATURE("+ual", FK_UAL)
1313
LOONGARCH_FEATURE("+frecipe", FK_FRECIPE)
1414
LOONGARCH_FEATURE("+lam-bh", FK_LAM_BH)
15+
LOONGARCH_FEATURE("+lamcas", FK_LAMCAS)
1516
LOONGARCH_FEATURE("+ld-seq-sa", FK_LD_SEQ_SA)
1617

1718
#undef LOONGARCH_FEATURE
@@ -22,6 +23,6 @@ LOONGARCH_FEATURE("+ld-seq-sa", FK_LD_SEQ_SA)
2223

2324
LOONGARCH_ARCH("loongarch64", AK_LOONGARCH64, FK_64BIT | FK_FP32 | FK_FP64 | FK_UAL)
2425
LOONGARCH_ARCH("la464", AK_LA464, FK_64BIT | FK_FP32 | FK_FP64 | FK_LSX | FK_LASX | FK_UAL)
25-
LOONGARCH_ARCH("la664", AK_LA664, FK_64BIT | FK_FP32 | FK_FP64 | FK_LSX | FK_LASX | FK_UAL | FK_FRECIPE | FK_LAM_BH | FK_LD_SEQ_SA)
26+
LOONGARCH_ARCH("la664", AK_LA664, FK_64BIT | FK_FP32 | FK_FP64 | FK_LSX | FK_LASX | FK_UAL | FK_FRECIPE | FK_LAM_BH | FK_LAMCAS | FK_LD_SEQ_SA)
2627

2728
#undef LOONGARCH_ARCH

llvm/include/llvm/TargetParser/LoongArchTargetParser.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ enum FeatureKind : uint32_t {
5454
// available.
5555
FK_LAM_BH = 1 << 10,
5656

57+
// Atomic memory compare and swap instructions for byte, half word, word and
58+
// double word are available.
59+
FK_LAMCAS = 1 << 11,
60+
5761
// Do not generate load-load barrier instructions (dbar 0x700).
5862
FK_LD_SEQ_SA = 1 << 12,
5963

llvm/lib/Target/LoongArch/LoongArch.td

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ def FeatureLAM_BH
118118
"Support amswap[_db].{b/h} and amadd[_db].{b/h} instructions.">;
119119
def HasLAM_BH : Predicate<"Subtarget->hasLAM_BH()">;
120120

121+
// Atomic memory compare and swap instructions for byte, half word, word and double word
122+
def FeatureLAMCAS
123+
: SubtargetFeature<"lamcas", "HasLAMCAS", "true",
124+
"Support amcas[_db].{b/h/w/d}.">;
125+
def HasLAMCAS : Predicate<"Subtarget->hasLAMCAS()">;
126+
121127
def FeatureLD_SEQ_SA
122128
: SubtargetFeature<"ld-seq-sa", "HasLD_SEQ_SA", "true",
123129
"Don't use load-load barrier (dbar 0x700).">;
@@ -165,7 +171,8 @@ def : ProcessorModel<"la664", NoSchedModel, [Feature64Bit,
165171
FeatureExtLVZ,
166172
FeatureExtLBT,
167173
FeatureFrecipe,
168-
FeatureLAM_BH]>;
174+
FeatureLAM_BH,
175+
FeatureLAMCAS]>;
169176

170177
//===----------------------------------------------------------------------===//
171178
// Define the LoongArch target.

llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,10 @@ LoongArchTargetLowering::LoongArchTargetLowering(const TargetMachine &TM,
371371
setPrefFunctionAlignment(Subtarget.getPrefFunctionAlignment());
372372
setPrefLoopAlignment(Subtarget.getPrefLoopAlignment());
373373
setMaxBytesForAlignment(Subtarget.getMaxBytesForAlignment());
374+
375+
// cmpxchg sizes down to 8 bits become legal if LAMCAS is available.
376+
if (Subtarget.hasLAMCAS())
377+
setMinCmpXchgSizeInBits(8);
374378
}
375379

376380
bool LoongArchTargetLowering::isOffsetFoldingLegal(
@@ -5765,6 +5769,10 @@ LoongArchTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
57655769
}
57665770

57675771
unsigned Size = AI->getType()->getPrimitiveSizeInBits();
5772+
if (Subtarget.hasLAMCAS() &&
5773+
(AI->getOperation() == AtomicRMWInst::Nand || Size < 32))
5774+
return AtomicExpansionKind::CmpXChg;
5775+
57685776
if (Size == 8 || Size == 16)
57695777
return AtomicExpansionKind::MaskedIntrinsic;
57705778
return AtomicExpansionKind::None;
@@ -5819,6 +5827,10 @@ getIntrinsicForMaskedAtomicRMWBinOp(unsigned GRLen,
58195827
TargetLowering::AtomicExpansionKind
58205828
LoongArchTargetLowering::shouldExpandAtomicCmpXchgInIR(
58215829
AtomicCmpXchgInst *CI) const {
5830+
5831+
if (Subtarget.hasLAMCAS())
5832+
return AtomicExpansionKind::None;
5833+
58225834
unsigned Size = CI->getCompareOperand()->getType()->getPrimitiveSizeInBits();
58235835
if (Size == 8 || Size == 16)
58245836
return AtomicExpansionKind::MaskedIntrinsic;
@@ -6314,8 +6326,8 @@ bool LoongArchTargetLowering::hasAndNotCompare(SDValue Y) const {
63146326
}
63156327

63166328
ISD::NodeType LoongArchTargetLowering::getExtendForAtomicCmpSwapArg() const {
6317-
// TODO: LAMCAS will use amcas{_DB,}.[bhwd] which does not require extension.
6318-
return ISD::SIGN_EXTEND;
6329+
// LAMCAS will use amcas[_DB].{b/h/w/d} which does not require extension.
6330+
return Subtarget.hasLAMCAS() ? ISD::ANY_EXTEND : ISD::SIGN_EXTEND;
63196331
}
63206332

63216333
bool LoongArchTargetLowering::shouldSignExtendTypeInLibCall(

llvm/lib/Target/LoongArch/LoongArchInstrInfo.td

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -719,9 +719,9 @@ class AM_3R<bits<32> op>
719719
}
720720

721721
class AMCAS_3R<bits<32> op>
722-
: Fmt3R<op, (outs GPR:$rd_wb), (ins GPR:$rd, GPR:$rk, GPRMemAtomic:$rj),
722+
: Fmt3R<op, (outs GPR:$dst), (ins GPR:$rd, GPR:$rk, GPRMemAtomic:$rj),
723723
"$rd, $rk, $rj"> {
724-
let Constraints = "@earlyclobber $rd_wb, $rd_wb = $rd";
724+
let Constraints = "@earlyclobber $dst, $dst = $rd";
725725
let IsAMCAS = 1;
726726
}
727727
} // hasSideEffects = 0, mayLoad = 1, mayStore = 1,
@@ -2119,6 +2119,26 @@ def : Pat<(atomic_load_sub_i16 GPR:$rj, GPR:$rk),
21192119
(AMADD__DB_H (SUB_W R0, GPR:$rk), GPR:$rj)>;
21202120
} // Predicates = [ IsLA64, HasLAM_BH ]
21212121

2122+
let Predicates = [ HasLAMCAS, IsLA64 ] in {
2123+
2124+
def : Pat<(atomic_cmp_swap_i8_monotonic GPR:$addr, GPR:$cmp, GPR:$new),
2125+
(AMCAS_B GPR:$cmp, GPR:$new, GPR:$addr)>;
2126+
def : Pat<(atomic_cmp_swap_i16_monotonic GPR:$addr, GPR:$cmp, GPR:$new),
2127+
(AMCAS_H GPR:$cmp, GPR:$new, GPR:$addr)>;
2128+
def : Pat<(atomic_cmp_swap_i32_monotonic GPR:$addr, GPR:$cmp, GPR:$new),
2129+
(AMCAS_W GPR:$cmp, GPR:$new, GPR:$addr)>;
2130+
def : Pat<(atomic_cmp_swap_i64_monotonic GPR:$addr, GPR:$cmp, GPR:$new),
2131+
(AMCAS_D GPR:$cmp, GPR:$new, GPR:$addr)>;
2132+
2133+
def : Pat<(atomic_cmp_swap_i8 GPR:$addr, GPR:$cmp, GPR:$new),
2134+
(AMCAS__DB_B GPR:$cmp, GPR:$new, GPR:$addr)>;
2135+
def : Pat<(atomic_cmp_swap_i16 GPR:$addr, GPR:$cmp, GPR:$new),
2136+
(AMCAS__DB_H GPR:$cmp, GPR:$new, GPR:$addr)>;
2137+
def : Pat<(atomic_cmp_swap_i32 GPR:$addr, GPR:$cmp, GPR:$new),
2138+
(AMCAS__DB_W GPR:$cmp, GPR:$new, GPR:$addr)>;
2139+
def : Pat<(atomic_cmp_swap_i64 GPR:$addr, GPR:$cmp, GPR:$new),
2140+
(AMCAS__DB_D GPR:$cmp, GPR:$new, GPR:$addr)>;
2141+
}
21222142

21232143
let Predicates = [IsLA64] in {
21242144

llvm/lib/TargetParser/LoongArchTargetParser.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ bool LoongArch::getArchFeatures(StringRef Arch,
5353
if (Arch == "la64v1.1") {
5454
Features.push_back("+frecipe");
5555
Features.push_back("+lam-bh");
56+
Features.push_back("+lamcas");
5657
Features.push_back("+ld-seq-sa");
5758
}
5859
return true;

0 commit comments

Comments
 (0)