Skip to content

Commit 9628b7d

Browse files
committed
support amswap[__db].{b/h} and amadd[__db].{b/h} instructions for LoongArch.
1 parent 6360652 commit 9628b7d

File tree

14 files changed

+1787
-14
lines changed

14 files changed

+1787
-14
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5394,6 +5394,10 @@ def mfrecipe : Flag<["-"], "mfrecipe">, Group<m_loongarch_Features_Group>,
53945394
HelpText<"Enable frecipe.{s/d} and frsqrte.{s/d}">;
53955395
def mno_frecipe : Flag<["-"], "mno-frecipe">, Group<m_loongarch_Features_Group>,
53965396
HelpText<"Disable frecipe.{s/d} and frsqrte.{s/d}">;
5397+
def mlam_bh : Flag<["-"], "mlam-bh">, Group<m_loongarch_Features_Group>,
5398+
HelpText<"Enable amswap_[db].{b/h} and amadd_[db].{b/h}">;
5399+
def mno_lam_bh : Flag<["-"], "mno-lam-bh">, Group<m_loongarch_Features_Group>,
5400+
HelpText<"Disable amswap_[db].{b/h} and amadd_[db].{b/h}">;
53975401
def mannotate_tablejump : Flag<["-"], "mannotate-tablejump">, Group<m_loongarch_Features_Group>,
53985402
HelpText<"Enable annotate table jump instruction to correlate it with the jump table.">;
53995403
def mno_annotate_tablejump : Flag<["-"], "mno-annotate-tablejump">, 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)
208+
if (HasFeatureFrecipe || HasFeatureLAM_BH)
209209
Builder.defineMacro("__loongarch_arch",
210210
Twine('"') + "la64v1.1" + Twine('"'));
211211
else
@@ -236,6 +236,9 @@ void LoongArchTargetInfo::getTargetDefines(const LangOptions &Opts,
236236
if (HasFeatureFrecipe)
237237
Builder.defineMacro("__loongarch_frecipe", Twine(1));
238238

239+
if (HasFeatureLAM_BH)
240+
Builder.defineMacro("__loongarch_lam_bh", Twine(1));
241+
239242
StringRef ABI = getABI();
240243
if (ABI == "lp64d" || ABI == "lp64f" || ABI == "lp64s")
241244
Builder.defineMacro("__loongarch_lp64");
@@ -312,6 +315,8 @@ bool LoongArchTargetInfo::handleTargetFeatures(
312315
HasUnalignedAccess = false;
313316
else if (Feature == "+frecipe")
314317
HasFeatureFrecipe = true;
318+
else if (Feature == "+lam-bh")
319+
HasFeatureLAM_BH = true;
315320
}
316321
return true;
317322
}

clang/lib/Basic/Targets/LoongArch.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class LLVM_LIBRARY_VISIBILITY LoongArchTargetInfo : public TargetInfo {
3030
bool HasFeatureLSX;
3131
bool HasFeatureLASX;
3232
bool HasFeatureFrecipe;
33+
bool HasFeatureLAM_BH;
3334

3435
public:
3536
LoongArchTargetInfo(const llvm::Triple &Triple, const TargetOptions &)
@@ -39,6 +40,7 @@ class LLVM_LIBRARY_VISIBILITY LoongArchTargetInfo : public TargetInfo {
3940
HasFeatureLSX = false;
4041
HasFeatureLASX = false;
4142
HasFeatureFrecipe = false;
43+
HasFeatureLAM_BH = false;
4244
LongDoubleWidth = 128;
4345
LongDoubleAlign = 128;
4446
LongDoubleFormat = &llvm::APFloat::IEEEquad();

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,15 @@ void loongarch::getLoongArchTargetFeatures(const Driver &D,
260260
else
261261
Features.push_back("-frecipe");
262262
}
263+
264+
// Select lam-bh feature determined by -m[no-]lam-bh.
265+
if (const Arg *A =
266+
Args.getLastArg(options::OPT_mlam_bh, options::OPT_mno_lam_bh)) {
267+
if (A->getOption().matches(options::OPT_mlam_bh))
268+
Features.push_back("+lam-bh");
269+
else
270+
Features.push_back("-lam-bh");
271+
}
263272
}
264273

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

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"
42+
// CC1-LA64V1P1: "-target-feature" "+64bit" "-target-feature" "+d" "-target-feature" "+lsx" "-target-feature" "+ual" "-target-feature" "+frecipe" "-target-feature" "+lam-bh"
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"
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"
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,+lsx,+ual"
56-
// IR-LA664: attributes #[[#]] ={{.*}}"target-cpu"="la664" {{.*}}"target-features"="+64bit,+d,+f,+frecipe,+lasx,+lsx,+ual"
55+
// IR-LA64V1P1: attributes #[[#]] ={{.*}}"target-cpu"="loongarch64" {{.*}}"target-features"="+64bit,+d,+frecipe,+lam-bh,+lsx,+ual"
56+
// IR-LA664: attributes #[[#]] ={{.*}}"target-cpu"="la664" {{.*}}"target-features"="+64bit,+d,+f,+frecipe,+lam-bh,+lasx,+lsx,+ual"
5757

5858
int foo(void) {
5959
return 3;

clang/test/Driver/loongarch-mlam-bh.c

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

clang/test/Preprocessor/init-loongarch.c

Lines changed: 17 additions & 6 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}.
801+
/// Check __loongarch_arch{_tune/_frecipe/_lam_bh}.
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,26 +823,37 @@
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 -DARCH=la64v1.1 -DTUNE=loongarch64 %s
826+
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,FRECIPE,LAM-BH -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-prefix=ARCH-TUNE -DARCH=la64v1.0 -DTUNE=loongarch64 %s
828+
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,LAM-BH -DARCH=la64v1.1 -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 -DARCH=loongarch64 -DTUNE=loongarch64 %s
830+
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,FRECIPE,LAM-BH -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.1 -DTUNE=loongarch64 %s
835+
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=la64v1.0 -Xclang -target-feature -Xclang +lam-bh | \
836+
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,LAM-BH -DARCH=la64v1.1 -DTUNE=loongarch64 %s
837+
// 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 -DARCH=la64v1.1 -DTUNE=loongarch64 %s
839+
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=loongarch64 -Xclang -target-feature -Xclang +lam-bh | \
840+
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,LAM-BH -DARCH=loongarch64 -DTUNE=loongarch64 %s
841+
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=loongarch64 -Xclang -target-feature -Xclang +lsx -Xclang -target-feature -Xclang +lam-bh | \
842+
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,LAM-BH -DARCH=la64v1.1 -DTUNE=loongarch64 %s
843+
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=la64v1.1 -Xclang -target-feature -Xclang -frecipe -Xclang -target-feature -Xclang -lam-bh | \
844+
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE -DARCH=la64v1.0 -DTUNE=loongarch64 %s
835845
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=la664 | \
836-
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,FRECIPE -DARCH=la664 -DTUNE=la664 %s
846+
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,FRECIPE,LAM-BH -DARCH=la664 -DTUNE=la664 %s
837847
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -mtune=la664 | \
838848
// RUN: FileCheck --match-full-lines --check-prefix=ARCH-TUNE -DARCH=la64v1.0 -DTUNE=la664 %s
839849
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=loongarch64 -mtune=la664 | \
840850
// RUN: FileCheck --match-full-lines --check-prefix=ARCH-TUNE -DARCH=loongarch64 -DTUNE=la664 %s
841851
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=la664 -mtune=loongarch64 | \
842-
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,FRECIPE -DARCH=la664 -DTUNE=loongarch64 %s
852+
// RUN: FileCheck --match-full-lines --check-prefixes=ARCH-TUNE,FRECIPE,LAM-BH -DARCH=la664 -DTUNE=loongarch64 %s
843853

844854
// ARCH-TUNE: #define __loongarch_arch "[[ARCH]]"
845855
// FRECIPE: #define __loongarch_frecipe 1
856+
// LAM-BH: #define __loongarch_lam_bh 1
846857
// ARCH-TUNE: #define __loongarch_tune "[[TUNE]]"
847858

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

llvm/include/llvm/TargetParser/LoongArchTargetParser.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ LOONGARCH_FEATURE("+lbt", FK_LBT)
1111
LOONGARCH_FEATURE("+lvz", FK_LVZ)
1212
LOONGARCH_FEATURE("+ual", FK_UAL)
1313
LOONGARCH_FEATURE("+frecipe", FK_FRECIPE)
14+
LOONGARCH_FEATURE("+lam-bh", FK_LAM_BH)
1415

1516
#undef LOONGARCH_FEATURE
1617

@@ -20,6 +21,6 @@ LOONGARCH_FEATURE("+frecipe", FK_FRECIPE)
2021

2122
LOONGARCH_ARCH("loongarch64", AK_LOONGARCH64, FK_64BIT | FK_FP32 | FK_FP64 | FK_UAL)
2223
LOONGARCH_ARCH("la464", AK_LA464, FK_64BIT | FK_FP32 | FK_FP64 | FK_LSX | FK_LASX | FK_UAL)
23-
LOONGARCH_ARCH("la664", AK_LA664, FK_64BIT | FK_FP32 | FK_FP64 | FK_LSX | FK_LASX | FK_UAL | FK_FRECIPE)
24+
LOONGARCH_ARCH("la664", AK_LA664, FK_64BIT | FK_FP32 | FK_FP64 | FK_LSX | FK_LASX | FK_UAL | FK_FRECIPE | FK_LAM_BH)
2425

2526
#undef LOONGARCH_ARCH

llvm/include/llvm/TargetParser/LoongArchTargetParser.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ enum FeatureKind : uint32_t {
4949

5050
// Floating-point approximate reciprocal instructions are available.
5151
FK_FRECIPE = 1 << 9,
52+
53+
// Atomic memory swap and add instructions for byte and half word are
54+
// available.
55+
FK_LAM_BH = 1 << 10,
5256
};
5357

5458
struct FeatureInfo {

llvm/lib/Target/LoongArch/LoongArch.td

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ def FeatureFrecipe
112112
"Support frecipe.{s/d} and frsqrte.{s/d} instructions.">;
113113
def HasFrecipe : Predicate<"Subtarget->hasFrecipe()">;
114114

115+
// Atomic memory swap and add instructions for byte and half word
116+
def FeatureLAM_BH
117+
: SubtargetFeature<"lam-bh", "HasLAM_BH", "true",
118+
"Support amswap[_db].{b/h} and amadd[_db].{b/h} instructions.">;
119+
def HasLAM_BH : Predicate<"Subtarget->hasLAM_BH()">;
120+
115121
def TunePreferWInst
116122
: SubtargetFeature<"prefer-w-inst", "PreferWInst", "true",
117123
"Prefer instructions with W suffix">;
@@ -151,7 +157,8 @@ def : ProcessorModel<"la664", NoSchedModel, [Feature64Bit,
151157
FeatureExtLASX,
152158
FeatureExtLVZ,
153159
FeatureExtLBT,
154-
FeatureFrecipe]>;
160+
FeatureFrecipe,
161+
FeatureLAM_BH]>;
155162

156163
//===----------------------------------------------------------------------===//
157164
// Define the LoongArch target.

llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5735,6 +5735,12 @@ LoongArchTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
57355735
AI->getOperation() == AtomicRMWInst::USubSat)
57365736
return AtomicExpansionKind::CmpXChg;
57375737

5738+
if (Subtarget.hasLAM_BH() && Subtarget.is64Bit() &&
5739+
(AI->getOperation() == AtomicRMWInst::Xchg ||
5740+
AI->getOperation() == AtomicRMWInst::Add)) {
5741+
return AtomicExpansionKind::None;
5742+
}
5743+
57385744
unsigned Size = AI->getType()->getPrimitiveSizeInBits();
57395745
if (Size == 8 || Size == 16)
57405746
return AtomicExpansionKind::MaskedIntrinsic;

llvm/lib/Target/LoongArch/LoongArchInstrInfo.td

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,8 +2088,26 @@ multiclass binary_atomic_op_wd<string inst, string op, string signed = ""> {
20882088
(!cast<Instruction>(inst#"__DB_D"#signed) GPR:$rk, GPR:$rj)>;
20892089
}
20902090

2091+
// Atomic operation for byte and half word
2092+
multiclass binary_atomic_op_bh<string inst, string op> {
2093+
def : Pat<(!cast<PatFrag>(op#"_i8_monotonic") GPR:$rj, GPR:$rk),
2094+
(!cast<Instruction>(inst#"_B") GPR:$rk, GPR:$rj)>;
2095+
def : Pat<(!cast<PatFrag>(op#"_i16_monotonic") GPR:$rj, GPR:$rk),
2096+
(!cast<Instruction>(inst#"_H") GPR:$rk, GPR:$rj)>;
2097+
2098+
def : Pat<(!cast<PatFrag>(op#"_i8") GPR:$rj, GPR:$rk),
2099+
(!cast<Instruction>(inst#"__DB_B") GPR:$rk, GPR:$rj)>;
2100+
def : Pat<(!cast<PatFrag>(op#"_i16") GPR:$rj, GPR:$rk),
2101+
(!cast<Instruction>(inst#"__DB_H") GPR:$rk, GPR:$rj)>;
2102+
}
2103+
20912104
let Predicates = [IsLA64] in {
20922105

2106+
let Predicates = [ HasLAM_BH ] in {
2107+
defm : binary_atomic_op_bh<"AMSWAP", "atomic_swap">;
2108+
defm : binary_atomic_op_bh<"AMADD", "atomic_load_add">;
2109+
}
2110+
20932111
defm : binary_atomic_op_wd<"AMSWAP", "atomic_swap">;
20942112
defm : binary_atomic_op_wd<"AMADD", "atomic_load_add">;
20952113
defm : binary_atomic_op_wd<"AMAND", "atomic_load_and">;

llvm/lib/TargetParser/LoongArchTargetParser.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ bool LoongArch::getArchFeatures(StringRef Arch,
5050
Features.push_back("+d");
5151
Features.push_back("+lsx");
5252
Features.push_back("+ual");
53-
if (Arch == "la64v1.1")
53+
if (Arch == "la64v1.1") {
5454
Features.push_back("+frecipe");
55+
Features.push_back("+lam-bh");
56+
}
5557
return true;
5658
}
5759

0 commit comments

Comments
 (0)