https://github.com/frobtech created
https://github.com/llvm/llvm-project/pull/240709
This adds the `+outline-atomics-preserve-most` subtarget feature, the
`--aarch64-outline-atomics-preserve-most` backend flag, and the Clang
driver flags `-moutline-atomics-preserve-most` and
`-mno-outline-atomics-preserve-most` for AArch64 targets.
Out-of-line atomic helper calls emitted by `-moutline-atomics`
(`__aarch64_{cas,swp,ldadd,ldclr,ldeor,ldset}*`) are currently lowered
as ordinary AAPCS64 calls, clobbering all caller-saved GPRs and
SIMD/SVE registers at every atomic operation. However,
`compiler-rt`'s `lse.S` implementation (as well as custom
runtime/kernel implementations using the same ABI) links statically
with hidden visibility and modifies only its argument and return
registers, `x15`-`x17`, and `nzcv`.
A single call-only calling convention,
`aarch64_outline_atomics_preservemost`, models that contract across
all 1-, 2-, 4-, 8-, and 16-byte helpers: the callee preserves all
registers except the argument/return registers (`x0`-`x4`), scratch
registers (`x15`-`x17`), and `nzcv`.
`LibcallLoweringInfo::setLibcallImplCallingConv` allows
`AArch64Subtarget::initLibcallLoweringInfo` to apply this
per-subtarget override without duplicating `RuntimeLibcallImpl` table
entries.
Because `libgcc`'s `__aarch64_cas16_*` implementation additionally
writes `x13` and `x14`, this convention remains opt-in rather than the
default for targets that may link against `libgcc`. The feature is
marked `InlineIgnore` so LTO can inline across translation units
compiled with different settings, and is suppressed on Arm64EC where
calls may traverse emulation thunks.
Assisted-by: Gemini
>From f9a60b39d06c4266f70f9872074c4da384874094 Mon Sep 17 00:00:00 2001
From: Roland McGrath <[email protected]>
Date: Mon, 9 Nov 2026 20:08:27 -0800
Subject: [PATCH] [AArch64] Add preserve-most calling convention for outline
atomics
This adds the `+outline-atomics-preserve-most` subtarget feature, the
`--aarch64-outline-atomics-preserve-most` backend flag, and the Clang
driver flags `-moutline-atomics-preserve-most` and
`-mno-outline-atomics-preserve-most` for AArch64 targets.
Out-of-line atomic helper calls emitted by `-moutline-atomics`
(`__aarch64_{cas,swp,ldadd,ldclr,ldeor,ldset}*`) are currently lowered
as ordinary AAPCS64 calls, clobbering all caller-saved GPRs and
SIMD/SVE registers at every atomic operation. However,
`compiler-rt`'s `lse.S` implementation (as well as custom
runtime/kernel implementations using the same ABI) links statically
with hidden visibility and modifies only its argument and return
registers, `x15`-`x17`, and `nzcv`.
A single call-only calling convention,
`aarch64_outline_atomics_preservemost`, models that contract across
all 1-, 2-, 4-, 8-, and 16-byte helpers: the callee preserves all
registers except the argument/return registers (`x0`-`x4`), scratch
registers (`x15`-`x17`), and `nzcv`.
`LibcallLoweringInfo::setLibcallImplCallingConv` allows
`AArch64Subtarget::initLibcallLoweringInfo` to apply this
per-subtarget override without duplicating `RuntimeLibcallImpl` table
entries.
Because `libgcc`'s `__aarch64_cas16_*` implementation additionally
writes `x13` and `x14`, this convention remains opt-in rather than the
default for targets that may link against `libgcc`. The feature is
marked `InlineIgnore` so LTO can inline across translation units
compiled with different settings, and is suppressed on Arm64EC where
calls may traverse emulation thunks.
Assisted-by: Gemini
---
clang/docs/ReleaseNotes.md | 2 +
clang/include/clang/Options/Options.td | 12 ++
clang/lib/Driver/ToolChains/CommonArgs.cpp | 16 ++
.../AArch64/outline-atomics-preserve-most.c | 19 +++
clang/test/Driver/aarch64-features.c | 18 +++
.../test/Driver/unsupported-outline-atomics.c | 8 +
llvm/docs/ReleaseNotes.md | 2 +
.../llvm/Analysis/LibcallLoweringInfo.h | 18 ++-
llvm/include/llvm/AsmParser/LLToken.h | 1 +
llvm/include/llvm/IR/CallingConv.h | 4 +
llvm/lib/AsmParser/LLLexer.cpp | 1 +
llvm/lib/AsmParser/LLParser.cpp | 4 +
llvm/lib/IR/AsmWriter.cpp | 3 +
llvm/lib/IR/Function.cpp | 1 +
.../AArch64/AArch64CallingConvention.td | 9 ++
llvm/lib/Target/AArch64/AArch64Features.td | 7 +
.../Target/AArch64/AArch64ISelLowering.cpp | 1 +
llvm/lib/Target/AArch64/AArch64Options.td | 2 +
.../Target/AArch64/AArch64RegisterInfo.cpp | 10 ++
llvm/lib/Target/AArch64/AArch64Subtarget.cpp | 34 +++++
llvm/lib/Target/AArch64/AArch64Subtarget.h | 3 +
.../aarch64-outline-atomics-preserve-most.ll | 22 +++
...line-atomics-preserve-most-callee-error.ll | 7 +
.../outline-atomics-preserve-most-sve.ll | 28 ++++
.../AArch64/outline-atomics-preserve-most.ll | 142 ++++++++++++++++++
.../AArch64/outline-atomics-preserve-most.ll | 88 +++++++++++
26 files changed, 461 insertions(+), 1 deletion(-)
create mode 100644 clang/test/CodeGen/AArch64/outline-atomics-preserve-most.c
create mode 100644 llvm/test/Bitcode/aarch64-outline-atomics-preserve-most.ll
create mode 100644
llvm/test/CodeGen/AArch64/outline-atomics-preserve-most-callee-error.ll
create mode 100644
llvm/test/CodeGen/AArch64/outline-atomics-preserve-most-sve.ll
create mode 100644 llvm/test/CodeGen/AArch64/outline-atomics-preserve-most.ll
create mode 100644 llvm/test/ThinLTO/AArch64/outline-atomics-preserve-most.ll
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 9c36e1e1679457..fc88a9fee80370 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1347,6 +1347,8 @@ documentation](CIR/index.md) for more information
regarding ClangIR's status and
`x86_64-unknown-uefi`, it follows the Microsoft ABI conventions (LLP64,
Microsoft C++ ABI, `char *` based `va_list`) and links with `lld-link`.
+- Added `-moutline-atomics-preserve-most` and
`-mno-outline-atomics-preserve-most` for AArch64 targets to control whether
out-of-line atomic helper calls preserve all registers except argument/return
registers (`x0`-`x4`), scratch registers (`x15`-`x17`), and `NZCV`.
+
#### Android Support
- Enabled PAC and BTI by default for AArch64 Android targets.
diff --git a/clang/include/clang/Options/Options.td
b/clang/include/clang/Options/Options.td
index d281f6552b4dbf..7df0f134e41b44 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -6388,6 +6388,18 @@ def moutline_atomics : Flag<["-"], "moutline-atomics">,
Group<f_clang_Group>,
def mno_outline_atomics : Flag<["-"], "mno-outline-atomics">,
Group<f_clang_Group>,
Visibility<[ClangOption, CC1Option, FlangOption]>,
HelpText<"Don't generate local calls to out-of-line atomic operations">;
+def moutline_atomics_preserve_most
+ : Flag<["-"], "moutline-atomics-preserve-most">,
+ Group<f_clang_Group>,
+ Visibility<[ClangOption, FlangOption]>,
+ HelpText<"Use a preserve-most calling convention for out-of-line atomic "
+ "helper calls on AArch64">;
+def mno_outline_atomics_preserve_most
+ : Flag<["-"], "mno-outline-atomics-preserve-most">,
+ Group<f_clang_Group>,
+ Visibility<[ClangOption, FlangOption]>,
+ HelpText<"Don't use a preserve-most calling convention for out-of-line "
+ "atomic helper calls on AArch64">;
def mno_implicit_float : Flag<["-"], "mno-implicit-float">, Group<m_Group>,
HelpText<"Don't generate implicit floating point or vector instructions">;
def mimplicit_float : Flag<["-"], "mimplicit-float">, Group<m_Group>,
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp
b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index db5250db329a9f..6b48129c8d2760 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -3067,6 +3067,22 @@ void tools::addOutlineAtomicsArgs(const Driver &D, const
ToolChain &TC,
CmdArgs.push_back("-target-feature");
CmdArgs.push_back("+outline-atomics");
}
+
+ if (Arg *A =
+ Args.getLastArg(options::OPT_moutline_atomics_preserve_most,
+ options::OPT_mno_outline_atomics_preserve_most)) {
+ if (!Triple.isAArch64()) {
+ D.Diag(diag::warn_drv_moutline_atomics_unsupported_opt)
+ << Triple.getArchName() << A->getOption().getName();
+ } else if (A->getOption().matches(
+ options::OPT_moutline_atomics_preserve_most)) {
+ CmdArgs.push_back("-target-feature");
+ CmdArgs.push_back("+outline-atomics-preserve-most");
+ } else {
+ CmdArgs.push_back("-target-feature");
+ CmdArgs.push_back("-outline-atomics-preserve-most");
+ }
+ }
}
void tools::addOffloadCompressArgs(const llvm::opt::ArgList &TCArgs,
diff --git a/clang/test/CodeGen/AArch64/outline-atomics-preserve-most.c
b/clang/test/CodeGen/AArch64/outline-atomics-preserve-most.c
new file mode 100644
index 00000000000000..37d110a48a2f9c
--- /dev/null
+++ b/clang/test/CodeGen/AArch64/outline-atomics-preserve-most.c
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 -triple aarch64-unknown-linux-gnu -O2 -target-feature
+outline-atomics -target-feature +outline-atomics-preserve-most -emit-llvm -o -
%s | FileCheck %s --check-prefix=IR
+// RUN: %clang_cc1 -triple aarch64-unknown-linux-gnu -O2 -target-feature
+outline-atomics -target-feature +outline-atomics-preserve-most -S -o - %s |
FileCheck %s --check-prefix=ASM
+// RUN: %clang_cc1 -triple aarch64-unknown-linux-gnu -O2 -target-feature
+outline-atomics -mllvm --aarch64-outline-atomics-preserve-most -S -o - %s |
FileCheck %s --check-prefix=ASM
+// RUN: %clang_cc1 -triple aarch64-unknown-fuchsia -O2 -target-feature
+outline-atomics -target-feature +outline-atomics-preserve-most -emit-llvm -o -
%s | FileCheck %s --check-prefix=IR
+// RUN: %clang_cc1 -triple aarch64-unknown-fuchsia -O2 -target-feature
+outline-atomics -target-feature +outline-atomics-preserve-most -S -o - %s |
FileCheck %s --check-prefix=ASM
+// RUN: %clang_cc1 -triple aarch64-unknown-fuchsia -O2 -target-feature
+outline-atomics -mllvm --aarch64-outline-atomics-preserve-most -S -o - %s |
FileCheck %s --check-prefix=ASM
+
+int fetch_add_with_live_vals(_Atomic int *p, int v, int a, int b, int c, int d,
+ int e, int f) {
+ // ASM-LABEL: fetch_add_with_live_vals:
+ // ASM-NOT: stp x19
+ // ASM-NOT: stp x20
+ // ASM: bl __aarch64_ldadd4_acq_rel
+ // ASM: ret
+ __c11_atomic_fetch_add(p, v, __ATOMIC_ACQ_REL);
+ return a + b + c + d + e + f;
+}
+
+// IR: attributes #{{[0-9]+}} = {
{{.*}}"target-features"="{{.*}}+outline-atomics,+outline-atomics-preserve-most{{.*}}"
diff --git a/clang/test/Driver/aarch64-features.c
b/clang/test/Driver/aarch64-features.c
index d2dc5a20119ee6..9a2aae82e0a072 100644
--- a/clang/test/Driver/aarch64-features.c
+++ b/clang/test/Driver/aarch64-features.c
@@ -87,6 +87,24 @@
// RUN: %clang --target=aarch64-unknown-managarm-mlibc -rtlib=libgcc \
// RUN: -### -c %s 2>&1 | FileCheck -check-prefix=CHECK-OUTLINE-ATOMICS-ON %s
+// RUN: %clang --target=aarch64-linux-gnu -rtlib=compiler-rt
-moutline-atomics-preserve-most \
+// RUN: -### -c %s 2>&1 | FileCheck \
+// RUN:
-check-prefixes=CHECK-OUTLINE-ATOMICS-ON,CHECK-OUTLINE-ATOMICS-PRESERVE-MOST %s
+
+// RUN: %clang --target=aarch64-linux-gnu -moutline-atomics
-moutline-atomics-preserve-most \
+// RUN: -mno-outline-atomics-preserve-most -### -c %s 2>&1 | FileCheck \
+// RUN:
-check-prefixes=CHECK-OUTLINE-ATOMICS-ON,CHECK-NO-OUTLINE-ATOMICS-PRESERVE-MOST
%s
+
+// RUN: %clang --target=aarch64-unknown-fuchsia
-moutline-atomics-preserve-most \
+// RUN: -### -c %s 2>&1 | FileCheck \
+// RUN:
-check-prefixes=CHECK-OUTLINE-ATOMICS-ON,CHECK-OUTLINE-ATOMICS-PRESERVE-MOST %s
+
+// RUN: %clang --target=aarch64-unknown-fuchsia
-moutline-atomics-preserve-most \
+// RUN: -mno-outline-atomics-preserve-most -### -c %s 2>&1 | FileCheck \
+// RUN:
-check-prefixes=CHECK-OUTLINE-ATOMICS-ON,CHECK-NO-OUTLINE-ATOMICS-PRESERVE-MOST
%s
+
// CHECK-OUTLINE-ATOMICS-ON: "-target-feature" "+outline-atomics"
// CHECK-OUTLINE-ATOMICS-OFF-NOT: "-target-feature" "+outline-atomics"
// CHECK-NO-OUTLINE-ATOMICS: "-target-feature" "-outline-atomics"
+// CHECK-OUTLINE-ATOMICS-PRESERVE-MOST: "-target-feature"
"+outline-atomics-preserve-most"
+// CHECK-NO-OUTLINE-ATOMICS-PRESERVE-MOST: "-target-feature"
"-outline-atomics-preserve-most"
diff --git a/clang/test/Driver/unsupported-outline-atomics.c
b/clang/test/Driver/unsupported-outline-atomics.c
index 2e88528c2ec603..2cba7fb0d1dfbf 100644
--- a/clang/test/Driver/unsupported-outline-atomics.c
+++ b/clang/test/Driver/unsupported-outline-atomics.c
@@ -13,3 +13,11 @@
// RUN: %clang -target riscv64 -mno-outline-atomics -S %s -### 2>&1 |
FileCheck %s -check-prefix=CHECK-NO-OUTLINE-ATOMICS-RISCV
// CHECK-NO-OUTLINE-ATOMICS-RISCV: warning: 'riscv64' does not support
'-mno-outline-atomics'; flag ignored [-Woption-ignored]
// CHECK-NO-OUTLINE-ATOMICS-RISCV-NOT: "-target-feature" "-outline-atomics"
+
+// RUN: %clang -target x86_64 -moutline-atomics-preserve-most -S %s -### 2>&1
| FileCheck %s -check-prefix=CHECK-PRESERVE-MOST-X86
+// CHECK-PRESERVE-MOST-X86: warning: 'x86_64' does not support
'-moutline-atomics-preserve-most'; flag ignored [-Woption-ignored]
+// CHECK-PRESERVE-MOST-X86-NOT: "-target-feature"
"+outline-atomics-preserve-most"
+
+// RUN: %clang -target x86_64 -mno-outline-atomics-preserve-most -S %s -###
2>&1 | FileCheck %s -check-prefix=CHECK-NO-PRESERVE-MOST-X86
+// CHECK-NO-PRESERVE-MOST-X86: warning: 'x86_64' does not support
'-mno-outline-atomics-preserve-most'; flag ignored [-Woption-ignored]
+// CHECK-NO-PRESERVE-MOST-X86-NOT: "-target-feature"
"-outline-atomics-preserve-most"
diff --git a/llvm/docs/ReleaseNotes.md b/llvm/docs/ReleaseNotes.md
index 687025c9582219..0d64f1374edafc 100644
--- a/llvm/docs/ReleaseNotes.md
+++ b/llvm/docs/ReleaseNotes.md
@@ -303,6 +303,8 @@ Makes programs 10x faster by doing Special New Thing.
ARM64 ABI (calling convention, callee saved registers, stack probing and SEH
unwind information) and produces PE/COFF objects.
+* Added the `+outline-atomics-preserve-most` subtarget feature (and
`--aarch64-outline-atomics-preserve-most` backend option) to lower AArch64
out-of-line atomic helper calls using `aarch64_outline_atomics_preservemost`,
preserving all registers except `x0`-`x4`, `x15`-`x17`, and `NZCV`.
+
### Changes to the AMDGPU Backend
* Replaced `xnack` and `sramecc` target features with `amdgpu.xnack`
diff --git a/llvm/include/llvm/Analysis/LibcallLoweringInfo.h
b/llvm/include/llvm/Analysis/LibcallLoweringInfo.h
index 2461bc7723ca10..22679bae055baa 100644
--- a/llvm/include/llvm/Analysis/LibcallLoweringInfo.h
+++ b/llvm/include/llvm/Analysis/LibcallLoweringInfo.h
@@ -20,6 +20,7 @@
#include "llvm/Analysis/RuntimeLibcallInfo.h"
#include "llvm/IR/RuntimeLibcalls.h"
#include "llvm/Pass.h"
+#include <cassert>
namespace llvm {
@@ -31,6 +32,8 @@ class LibcallLoweringInfo {
/// Stores the implementation choice for each libcall.
RTLIB::LibcallImpl LibcallImpls[RTLIB::UNKNOWN_LIBCALL + 1] = {
RTLIB::Unsupported};
+ /// Stores context-specific (e.g. subtarget) calling convention overrides.
+ DenseMap<unsigned, CallingConv::ID> CallingConvOverrides;
public:
/// Callback applying the caller's context-specific (e.g. subtarget) libcall
@@ -80,14 +83,27 @@ class LibcallLoweringInfo {
LibcallImpls[Call] = Impl;
}
+ /// Override the CallingConv that should be used for \p Call in this lowering
+ /// context.
+ void setLibcallImplCallingConv(RTLIB::LibcallImpl Call, CallingConv::ID CC) {
+ assert(Call != RTLIB::Unsupported &&
+ "Cannot override calling convention for unsupported libcall");
+ CallingConvOverrides[Call] = CC;
+ }
+
// FIXME: Remove this wrapper in favor of directly using
// getLibcallImplCallingConv
CallingConv::ID getLibcallCallingConv(RTLIB::Libcall Call) const {
- return RTLCI.LibcallImplCallingConvs[LibcallImpls[Call]];
+ return getLibcallImplCallingConv(LibcallImpls[Call]);
}
/// Get the CallingConv that should be used for the specified libcall.
CallingConv::ID getLibcallImplCallingConv(RTLIB::LibcallImpl Call) const {
+ if (!CallingConvOverrides.empty()) {
+ auto It = CallingConvOverrides.find(Call);
+ if (It != CallingConvOverrides.end())
+ return It->second;
+ }
return RTLCI.LibcallImplCallingConvs[Call];
}
diff --git a/llvm/include/llvm/AsmParser/LLToken.h
b/llvm/include/llvm/AsmParser/LLToken.h
index 78e20282c84593..2d99e78ceac75e 100644
--- a/llvm/include/llvm/AsmParser/LLToken.h
+++ b/llvm/include/llvm/AsmParser/LLToken.h
@@ -152,6 +152,7 @@ enum Kind {
kw_aarch64_sme_preservemost_from_x0,
kw_aarch64_sme_preservemost_from_x1,
kw_aarch64_sme_preservemost_from_x2,
+ kw_aarch64_outline_atomics_preservemost,
kw_msp430_intrcc,
kw_avr_intrcc,
kw_avr_signalcc,
diff --git a/llvm/include/llvm/IR/CallingConv.h
b/llvm/include/llvm/IR/CallingConv.h
index 249c512dda5324..413c5879d5204e 100644
--- a/llvm/include/llvm/IR/CallingConv.h
+++ b/llvm/include/llvm/IR/CallingConv.h
@@ -297,6 +297,10 @@ namespace CallingConv {
/// stateless compartment.
CHERIoT_LibraryCall = 127,
+ /// Callee preserves all registers except X0-X4, X15-X17, and NZCV (used by
+ /// AArch64 out-of-line atomic helper calls).
+ AArch64_Outline_Atomics_PreserveMost = 128,
+
/// The highest possible ID. Must be some 2^k - 1.
MaxID = 1023
};
diff --git a/llvm/lib/AsmParser/LLLexer.cpp b/llvm/lib/AsmParser/LLLexer.cpp
index 36fcb4a7df7f25..417fed434228f8 100644
--- a/llvm/lib/AsmParser/LLLexer.cpp
+++ b/llvm/lib/AsmParser/LLLexer.cpp
@@ -663,6 +663,7 @@ lltok::Kind LLLexer::LexIdentifier() {
KEYWORD(aarch64_sme_preservemost_from_x0);
KEYWORD(aarch64_sme_preservemost_from_x1);
KEYWORD(aarch64_sme_preservemost_from_x2);
+ KEYWORD(aarch64_outline_atomics_preservemost);
KEYWORD(msp430_intrcc);
KEYWORD(avr_intrcc);
KEYWORD(avr_signalcc);
diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp
index 6be0e010a235c5..2ef79cb079168b 100644
--- a/llvm/lib/AsmParser/LLParser.cpp
+++ b/llvm/lib/AsmParser/LLParser.cpp
@@ -2344,6 +2344,7 @@ void LLParser::parseOptionalDLLStorageClass(unsigned
&Res) {
/// ::= 'aarch64_sme_preservemost_from_x0'
/// ::= 'aarch64_sme_preservemost_from_x1'
/// ::= 'aarch64_sme_preservemost_from_x2'
+/// ::= 'aarch64_outline_atomics_preservemost'
/// ::= 'msp430_intrcc'
/// ::= 'avr_intrcc'
/// ::= 'avr_signalcc'
@@ -2410,6 +2411,9 @@ bool LLParser::parseOptionalCallingConv(unsigned &CC) {
case lltok::kw_aarch64_sme_preservemost_from_x2:
CC = CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2;
break;
+ case lltok::kw_aarch64_outline_atomics_preservemost:
+ CC = CallingConv::AArch64_Outline_Atomics_PreserveMost;
+ break;
case lltok::kw_msp430_intrcc: CC = CallingConv::MSP430_INTR; break;
case lltok::kw_avr_intrcc: CC = CallingConv::AVR_INTR; break;
case lltok::kw_avr_signalcc: CC = CallingConv::AVR_SIGNAL; break;
diff --git a/llvm/lib/IR/AsmWriter.cpp b/llvm/lib/IR/AsmWriter.cpp
index b47458c28e4994..9dc739a9e352c9 100644
--- a/llvm/lib/IR/AsmWriter.cpp
+++ b/llvm/lib/IR/AsmWriter.cpp
@@ -361,6 +361,9 @@ static void printCallingConv(unsigned cc, raw_ostream &Out)
{
case CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2:
Out << "aarch64_sme_preservemost_from_x2";
break;
+ case CallingConv::AArch64_Outline_Atomics_PreserveMost:
+ Out << "aarch64_outline_atomics_preservemost";
+ break;
case CallingConv::MSP430_INTR: Out << "msp430_intrcc"; break;
case CallingConv::AVR_INTR: Out << "avr_intrcc "; break;
case CallingConv::AVR_SIGNAL: Out << "avr_signalcc "; break;
diff --git a/llvm/lib/IR/Function.cpp b/llvm/lib/IR/Function.cpp
index c11e5f2ae12209..9fd405c6aed2a9 100644
--- a/llvm/lib/IR/Function.cpp
+++ b/llvm/lib/IR/Function.cpp
@@ -1214,6 +1214,7 @@ bool
llvm::CallingConv::supportsNonVoidReturnType(CallingConv::ID CC) {
case CallingConv::ARM64EC_Thunk_Native:
case CallingConv::RISCV_VectorCall:
case CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X1:
+ case CallingConv::AArch64_Outline_Atomics_PreserveMost:
case CallingConv::RISCV_VLSCall_32:
case CallingConv::RISCV_VLSCall_64:
case CallingConv::RISCV_VLSCall_128:
diff --git a/llvm/lib/Target/AArch64/AArch64CallingConvention.td
b/llvm/lib/Target/AArch64/AArch64CallingConvention.td
index 5ad6045c09f509..8136d7af287786 100644
--- a/llvm/lib/Target/AArch64/AArch64CallingConvention.td
+++ b/llvm/lib/Target/AArch64/AArch64CallingConvention.td
@@ -702,6 +702,15 @@ def
CSR_AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2
(sequence "X%u",19, 28),
LR, FP)>;
+// Out-of-line atomic helpers in compiler-rt (__aarch64_{cas,swp,ld*}*)
+// preserve all registers except argument/return registers (X0-X4),
+// scratch registers (X15-X17), and NZCV.
+def CSR_AArch64_Outline_Atomics_PreserveMost
+ : CalleeSavedRegs<(add (sequence "Z%u", 0, 31),
+ (sequence "P%u", 0, 15),
+ (sub (sequence "X%u", 5, 28), X15, X16, X17),
+ LR, FP)>;
+
// The SMSTART/SMSTOP instructions preserve only GPR registers.
def CSR_AArch64_SMStartStop : CalleeSavedRegs<(add (sequence "X%u", 0, 28),
LR, FP)>;
diff --git a/llvm/lib/Target/AArch64/AArch64Features.td
b/llvm/lib/Target/AArch64/AArch64Features.td
index 07d01fae8856d5..1897e3280ce974 100644
--- a/llvm/lib/Target/AArch64/AArch64Features.td
+++ b/llvm/lib/Target/AArch64/AArch64Features.td
@@ -665,6 +665,13 @@ def FeatureSMEF16F8F16 :
ExtensionWithMArch<"sme-f16f8f16", "SME_F16F8F16", "FEA
def FeatureOutlineAtomics : SubtargetFeature<"outline-atomics",
"OutlineAtomics", "true",
"Enable out of line atomics to support LSE instructions">;
+def FeatureOutlineAtomicsPreserveMost
+ : SubtargetFeature<"outline-atomics-preserve-most",
+ "OutlineAtomicsPreserveMost", "true",
+ "Use a preserve-most calling convention for out-of-line
"
+ "atomic helper calls",
+ [], InlineIgnore>;
+
def FeatureFMV : SubtargetFeature<"fmv", "HasFMV", "true",
"Enable Function Multi Versioning support.">;
diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
index fbc7fdfc63c331..1c3b9d82ebf526 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
@@ -9285,6 +9285,7 @@ CCAssignFn
*AArch64TargetLowering::CCAssignFnForCall(CallingConv::ID CC,
case CallingConv::SwiftTail:
case CallingConv::Tail:
case CallingConv::GRAAL:
+ case CallingConv::AArch64_Outline_Atomics_PreserveMost:
if (Subtarget->isTargetWindowsOrUEFI()) {
if (IsVarArg) {
if (Subtarget->isWindowsArm64EC())
diff --git a/llvm/lib/Target/AArch64/AArch64Options.td
b/llvm/lib/Target/AArch64/AArch64Options.td
index 3cfea0ad209fc4..b8d05bb416beb2 100644
--- a/llvm/lib/Target/AArch64/AArch64Options.td
+++ b/llvm/lib/Target/AArch64/AArch64Options.td
@@ -232,6 +232,8 @@ defm : BoolField<"aarch64-use-tbi", "false",
"Assume that top byte of an address is ignored">;
defm : OptionalBoolField<"sve-use-scalar-inc-vl",
"Prefer add+cnt over addvl/inc/dec">;
+defm : OptionalBoolField<"aarch64-outline-atomics-preserve-most",
+ "Use a preserve-most calling convention for out-of-line atomic helper
calls">;
defm : ValueField<"sve-vscale-for-tuning", "std::optional<unsigned>",
"std::nullopt",
"Force a vscale for tuning factor for SVE">;
diff --git a/llvm/lib/Target/AArch64/AArch64RegisterInfo.cpp
b/llvm/lib/Target/AArch64/AArch64RegisterInfo.cpp
index d52841923cea86..303b02e72be9d5 100644
--- a/llvm/lib/Target/AArch64/AArch64RegisterInfo.cpp
+++ b/llvm/lib/Target/AArch64/AArch64RegisterInfo.cpp
@@ -166,6 +166,11 @@ AArch64RegisterInfo::getCalleeSavedRegs(const
MachineFunction *MF) const {
"only supported to improve calls to SME ACLE __arm_sme_state "
"and is not intended to be used beyond that scope.");
+ case CallingConv::AArch64_Outline_Atomics_PreserveMost:
+ report_fatal_error(
+ "Calling convention AArch64_Outline_Atomics_PreserveMost is only "
+ "supported for calls to out-of-line atomic helper routines.");
+
case CallingConv::Win64:
if (Darwin)
return CSR_Darwin_AArch64_AAPCS_Win64_SaveList;
@@ -250,6 +255,8 @@ AArch64RegisterInfo::getDarwinCallPreservedMask(const
MachineFunction &MF,
return CSR_AArch64_SME_ABI_Support_Routines_PreserveMost_From_X1_RegMask;
if (CC == CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2)
return CSR_AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2_RegMask;
+ if (CC == CallingConv::AArch64_Outline_Atomics_PreserveMost)
+ return CSR_AArch64_Outline_Atomics_PreserveMost_RegMask;
if (CC == CallingConv::CFGuard_Check)
report_fatal_error(
"Calling convention CFGuard_Check is unsupported on Darwin.");
@@ -298,6 +305,8 @@ AArch64RegisterInfo::getCallPreservedMask(const
MachineFunction &MF,
return CSR_AArch64_SME_ABI_Support_Routines_PreserveMost_From_X1_RegMask;
if (CC == CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2)
return CSR_AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2_RegMask;
+ if (CC == CallingConv::AArch64_Outline_Atomics_PreserveMost)
+ return CSR_AArch64_Outline_Atomics_PreserveMost_RegMask;
if (CC == CallingConv::CFGuard_Check)
return CSR_Win_AArch64_CFGuard_Check_RegMask;
if (MF.getSubtarget<AArch64Subtarget>().getTargetLowering()
@@ -738,6 +747,7 @@ bool AArch64RegisterInfo::isArgumentRegister(const
MachineFunction &MF,
case CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0:
case CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X1:
case CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2:
+ case CallingConv::AArch64_Outline_Atomics_PreserveMost:
if (STI.isTargetWindowsOrUEFI())
return HasReg(CC_AArch64_Win64PCS_ArgRegs, Reg);
return HasReg(CC_AArch64_AAPCS_ArgRegs, Reg);
diff --git a/llvm/lib/Target/AArch64/AArch64Subtarget.cpp
b/llvm/lib/Target/AArch64/AArch64Subtarget.cpp
index 0adee4dc05611b..3e31ef423599a0 100644
--- a/llvm/lib/Target/AArch64/AArch64Subtarget.cpp
+++ b/llvm/lib/Target/AArch64/AArch64Subtarget.cpp
@@ -20,6 +20,7 @@
#include "GISel/AArch64LegalizerInfo.h"
#include "GISel/AArch64RegisterBankInfo.h"
#include "MCTargetDesc/AArch64AddressingModes.h"
+#include "llvm/Analysis/LibcallLoweringInfo.h"
#include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
#include "llvm/CodeGen/MachineFrameInfo.h"
#include "llvm/CodeGen/MachineScheduler.h"
@@ -545,6 +546,39 @@ bool AArch64Subtarget::useScalarIncVL() const {
return valueOr(CLOpts.sve_use_scalar_inc_vl, hasSVE2() || hasSME());
}
+bool AArch64Subtarget::useOutlineAtomicsPreserveMost() const {
+ if (isWindowsArm64EC())
+ return false;
+ return valueOr(CLOpts.outline_atomics_preserve_most,
+ OutlineAtomicsPreserveMost);
+}
+
+void AArch64Subtarget::initLibcallLoweringInfo(
+ LibcallLoweringInfo &Info) const {
+ if (!useOutlineAtomicsPreserveMost())
+ return;
+
+#define LCALLS(A, B)
\
+ RTLIB::A##B##_RELAX, RTLIB::A##B##_ACQ, RTLIB::A##B##_REL,
\
+ RTLIB::A##B##_ACQ_REL
+#define LCALL4(A) LCALLS(A, 1), LCALLS(A, 2), LCALLS(A, 4), LCALLS(A, 8)
+ static constexpr RTLIB::Libcall OutlineAtomicCalls[] = {
+ LCALL4(OUTLINE_ATOMIC_CAS), LCALLS(OUTLINE_ATOMIC_CAS, 16),
+ LCALL4(OUTLINE_ATOMIC_SWP), LCALL4(OUTLINE_ATOMIC_LDADD),
+ LCALL4(OUTLINE_ATOMIC_LDSET), LCALL4(OUTLINE_ATOMIC_LDCLR),
+ LCALL4(OUTLINE_ATOMIC_LDEOR),
+ };
+#undef LCALL4
+#undef LCALLS
+
+ for (RTLIB::Libcall LC : OutlineAtomicCalls) {
+ RTLIB::LibcallImpl Impl = Info.getLibcallImpl(LC);
+ if (Impl != RTLIB::Unsupported)
+ Info.setLibcallImplCallingConv(
+ Impl, CallingConv::AArch64_Outline_Atomics_PreserveMost);
+ }
+}
+
// If return address signing is enabled, tail calls are emitted as follows:
//
// ```
diff --git a/llvm/lib/Target/AArch64/AArch64Subtarget.h
b/llvm/lib/Target/AArch64/AArch64Subtarget.h
index 17cf9f5d247657..70f5246270d517 100644
--- a/llvm/lib/Target/AArch64/AArch64Subtarget.h
+++ b/llvm/lib/Target/AArch64/AArch64Subtarget.h
@@ -178,6 +178,8 @@ class AArch64Subtarget final : public
AArch64GenSubtargetInfo {
InstructionSelector *getInstructionSelector() const override;
const LegalizerInfo *getLegalizerInfo() const override;
const RegisterBankInfo *getRegBankInfo() const override;
+ void initLibcallLoweringInfo(LibcallLoweringInfo &Info) const override;
+ bool useOutlineAtomicsPreserveMost() const;
const Triple &getTargetTriple() const { return TargetTriple; }
bool enableMachineScheduler() const override { return true; }
bool enablePostRAScheduler() const override { return usePostRAScheduler(); }
@@ -435,6 +437,7 @@ class AArch64Subtarget final : public
AArch64GenSubtargetInfo {
case CallingConv::Fast:
case CallingConv::Swift:
case CallingConv::SwiftTail:
+ case CallingConv::AArch64_Outline_Atomics_PreserveMost:
return isTargetWindowsOrUEFI();
case CallingConv::PreserveNone:
return IsVarArg && isTargetWindowsOrUEFI();
diff --git a/llvm/test/Bitcode/aarch64-outline-atomics-preserve-most.ll
b/llvm/test/Bitcode/aarch64-outline-atomics-preserve-most.ll
new file mode 100644
index 00000000000000..3ca73f560e0ba7
--- /dev/null
+++ b/llvm/test/Bitcode/aarch64-outline-atomics-preserve-most.ll
@@ -0,0 +1,22 @@
+; RUN: llvm-as %s -o - -f | llvm-dis | FileCheck %s
+; RUN: llvm-as %s -o - -f | verify-uselistorder
+
+declare aarch64_outline_atomics_preservemost i32 @helper_scalar(i32, ptr)
+; CHECK: declare aarch64_outline_atomics_preservemost i32 @helper_scalar(i32,
ptr)
+
+declare aarch64_outline_atomics_preservemost i128 @helper_cas16(i128, i128,
ptr)
+; CHECK: declare aarch64_outline_atomics_preservemost i128 @helper_cas16(i128,
i128, ptr)
+
+define i32 @call_helper_scalar(i32 %v, ptr %p) {
+; CHECK-LABEL: define i32 @call_helper_scalar(
+; CHECK: %r = call aarch64_outline_atomics_preservemost i32 @helper_scalar(i32
%v, ptr %p)
+ %r = call aarch64_outline_atomics_preservemost i32 @helper_scalar(i32 %v,
ptr %p)
+ ret i32 %r
+}
+
+define i128 @call_helper_cas16(i128 %e, i128 %d, ptr %p) {
+; CHECK-LABEL: define i128 @call_helper_cas16(
+; CHECK: %r = call aarch64_outline_atomics_preservemost i128
@helper_cas16(i128 %e, i128 %d, ptr %p)
+ %r = call aarch64_outline_atomics_preservemost i128 @helper_cas16(i128 %e,
i128 %d, ptr %p)
+ ret i128 %r
+}
diff --git
a/llvm/test/CodeGen/AArch64/outline-atomics-preserve-most-callee-error.ll
b/llvm/test/CodeGen/AArch64/outline-atomics-preserve-most-callee-error.ll
new file mode 100644
index 00000000000000..bf1ad50a60df65
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/outline-atomics-preserve-most-callee-error.ll
@@ -0,0 +1,7 @@
+; RUN: not --crash llc -mtriple=aarch64-unknown-linux-gnu -o - < %s 2>&1 |
FileCheck %s
+; RUN: not --crash llc -mtriple=aarch64-unknown-fuchsia -o - < %s 2>&1 |
FileCheck %s
+
+; CHECK: LLVM ERROR: Calling convention AArch64_Outline_Atomics_PreserveMost
is only supported for calls to out-of-line atomic helper routines.
+define aarch64_outline_atomics_preservemost void @bad_callee() {
+ unreachable
+}
diff --git a/llvm/test/CodeGen/AArch64/outline-atomics-preserve-most-sve.ll
b/llvm/test/CodeGen/AArch64/outline-atomics-preserve-most-sve.ll
new file mode 100644
index 00000000000000..06fe8e84bf35a4
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/outline-atomics-preserve-most-sve.ll
@@ -0,0 +1,28 @@
+; RUN: llc -mtriple=aarch64-unknown-linux-gnu -mattr=+sve,+outline-atomics
-verify-machineinstrs < %s | FileCheck %s --check-prefix=DEFAULT
+; RUN: llc -mtriple=aarch64-unknown-linux-gnu
-mattr=+sve,+outline-atomics,+outline-atomics-preserve-most
-verify-machineinstrs < %s | FileCheck %s --check-prefix=PRESERVE
+; RUN: llc -mtriple=aarch64-unknown-fuchsia -mattr=+sve,+outline-atomics
-verify-machineinstrs < %s | FileCheck %s --check-prefix=DEFAULT
+; RUN: llc -mtriple=aarch64-unknown-fuchsia
-mattr=+sve,+outline-atomics,+outline-atomics-preserve-most
-verify-machineinstrs < %s | FileCheck %s --check-prefix=PRESERVE
+
+; Verify that SVE scalable vector (z0-z31) and predicate (p0-p15) registers are
+; preserved across outline-atomic libcalls without scalable stack spills.
+define <vscale x 4 x i32> @ldset8_preserves_sve(ptr %p, i64 %v, <vscale x 4 x
i32> %sv0, <vscale x 4 x i32> %sv1, <vscale x 4 x i1> %pg) nounwind {
+; DEFAULT-LABEL: ldset8_preserves_sve:
+; DEFAULT: addvl sp, sp,
+; DEFAULT: str z{{[0-9]+}},
+; DEFAULT: bl __aarch64_ldset8_acq_rel
+;
+; PRESERVE-LABEL: ldset8_preserves_sve:
+; PRESERVE-NOT: addvl
+; PRESERVE-NOT: str z
+; PRESERVE-NOT: str p
+; PRESERVE: bl __aarch64_ldset8_acq_rel
+; PRESERVE-NOT: ldr z
+; PRESERVE-NOT: ldr p
+; PRESERVE: add z0.s, p0/m, z0.s, z1.s
+; PRESERVE: ret
+ %old = atomicrmw or ptr %p, i64 %v acq_rel
+ %res = call <vscale x 4 x i32> @llvm.aarch64.sve.add.nxv4i32(<vscale x 4 x
i1> %pg, <vscale x 4 x i32> %sv0, <vscale x 4 x i32> %sv1)
+ ret <vscale x 4 x i32> %res
+}
+
+declare <vscale x 4 x i32> @llvm.aarch64.sve.add.nxv4i32(<vscale x 4 x i1>,
<vscale x 4 x i32>, <vscale x 4 x i32>)
diff --git a/llvm/test/CodeGen/AArch64/outline-atomics-preserve-most.ll
b/llvm/test/CodeGen/AArch64/outline-atomics-preserve-most.ll
new file mode 100644
index 00000000000000..053a7bd72eff11
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/outline-atomics-preserve-most.ll
@@ -0,0 +1,142 @@
+; RUN: llc -mtriple=aarch64-unknown-linux-gnu -mattr=+outline-atomics
-verify-machineinstrs < %s | FileCheck %s --check-prefix=DEFAULT
+; RUN: llc -mtriple=aarch64-unknown-linux-gnu
-mattr=+outline-atomics,+outline-atomics-preserve-most -verify-machineinstrs <
%s | FileCheck %s --check-prefix=PRESERVE
+; RUN: llc -mtriple=aarch64-unknown-linux-gnu -mattr=+outline-atomics
--aarch64-outline-atomics-preserve-most -verify-machineinstrs < %s | FileCheck
%s --check-prefix=PRESERVE
+; RUN: llc -mtriple=aarch64-unknown-linux-gnu
-mattr=+outline-atomics,+outline-atomics-preserve-most -global-isel
-global-isel-abort=1 -verify-machineinstrs < %s | FileCheck %s
--check-prefix=GISEL
+; RUN: llc -mtriple=aarch64-unknown-linux-gnu -O0
-mattr=+outline-atomics,+outline-atomics-preserve-most -verify-machineinstrs <
%s | FileCheck %s --check-prefix=O0
+; RUN: llc -mtriple=aarch64-unknown-linux-gnu -O0 -global-isel=0
-mattr=+outline-atomics,+outline-atomics-preserve-most -verify-machineinstrs <
%s | FileCheck %s --check-prefix=O0
+; RUN: llc -mtriple=aarch64-unknown-linux-gnu
-mattr=+outline-atomics,+outline-atomics-preserve-most
-stop-after=finalize-isel < %s | FileCheck %s --check-prefix=CSRMASK
+; RUN: llc -mtriple=aarch64-unknown-fuchsia -mattr=+outline-atomics
-verify-machineinstrs < %s | FileCheck %s --check-prefix=DEFAULT
+; RUN: llc -mtriple=aarch64-unknown-fuchsia
-mattr=+outline-atomics,+outline-atomics-preserve-most -verify-machineinstrs <
%s | FileCheck %s --check-prefix=PRESERVE
+; RUN: llc -mtriple=aarch64-unknown-fuchsia -mattr=+outline-atomics
--aarch64-outline-atomics-preserve-most -verify-machineinstrs < %s | FileCheck
%s --check-prefix=PRESERVE
+; RUN: llc -mtriple=aarch64-unknown-fuchsia
-mattr=+outline-atomics,+outline-atomics-preserve-most -global-isel
-global-isel-abort=1 -verify-machineinstrs < %s | FileCheck %s
--check-prefix=GISEL
+; RUN: llc -mtriple=aarch64-unknown-fuchsia -O0
-mattr=+outline-atomics,+outline-atomics-preserve-most -verify-machineinstrs <
%s | FileCheck %s --check-prefix=O0
+; RUN: llc -mtriple=aarch64-unknown-fuchsia -O0 -global-isel=0
-mattr=+outline-atomics,+outline-atomics-preserve-most -verify-machineinstrs <
%s | FileCheck %s --check-prefix=O0
+; RUN: llc -mtriple=aarch64-unknown-fuchsia
-mattr=+outline-atomics,+outline-atomics-preserve-most
-stop-after=finalize-isel < %s | FileCheck %s --check-prefix=CSRMASK
+; RUN: llc -mtriple=aarch64-apple-darwin
-mattr=+outline-atomics,+outline-atomics-preserve-most
-stop-after=finalize-isel < %s | FileCheck %s --check-prefix=CSRMASK
+; RUN: llc -mtriple=arm64ec-pc-windows-msvc
-mattr=+outline-atomics,+outline-atomics-preserve-most
-stop-after=finalize-isel < %s | FileCheck %s --check-prefix=ARM64EC
+
+; Verify that live GPRs are held in preserved x5-x14 across 1/2/4/8-byte
+; outline-atomic libcalls without callee-saved stack spills.
+define i64 @ldadd4_preserves_gprs(ptr %p, i32 %v, i64 %a, i64 %b, i64 %c, i64
%d, i64 %e, i64 %f) nounwind {
+; DEFAULT-LABEL: ldadd4_preserves_gprs:
+; DEFAULT: stp x24, x23,
+; DEFAULT: stp x22, x21,
+; DEFAULT: stp x20, x19,
+; DEFAULT: bl __aarch64_ldadd4_acq_rel
+;
+; PRESERVE-LABEL: ldadd4_preserves_gprs:
+; PRESERVE-NOT: stp x
+; PRESERVE: bl __aarch64_ldadd4_acq_rel
+; PRESERVE-NOT: ldp x
+; PRESERVE: ret
+;
+; GISEL-LABEL: ldadd4_preserves_gprs:
+; GISEL-NOT: stp x
+; GISEL: bl __aarch64_ldadd4_acq_rel
+; GISEL: ret
+;
+; CSRMASK-LABEL: name: ldadd4_preserves_gprs
+; CSRMASK: BL &__aarch64_ldadd4_acq_rel,
csr_aarch64_outline_atomics_preservemost
+; ARM64EC-LABEL: name: '#ldadd4_preserves_gprs'
+; ARM64EC: BL &"#__aarch64_ldadd4_acq_rel", csr_aarch64_aapcs
+ %old = atomicrmw add ptr %p, i32 %v acq_rel
+ %after = load volatile i32, ptr %p
+ %sum0 = add i32 %old, %after
+ %sum0z = zext i32 %sum0 to i64
+ %s1 = add i64 %a, %b
+ %s2 = add i64 %c, %d
+ %s3 = add i64 %e, %f
+ %s4 = add i64 %s1, %s2
+ %s5 = add i64 %s3, %sum0z
+ %res = add i64 %s4, %s5
+ ret i64 %res
+}
+
+; Verify that 16-byte CAS uses the same calling convention, keeping live values
+; in x5-x14 without callee-saved stack spills while treating x0-x4 as
clobbered.
+define i128 @cas16_preserves_gprs(ptr %p, i128 %exp, i128 %des, i64 %extra)
nounwind {
+; DEFAULT-LABEL: cas16_preserves_gprs:
+; DEFAULT: stp {{.*}}x19
+; DEFAULT: bl __aarch64_cas16_acq_rel
+;
+; PRESERVE-LABEL: cas16_preserves_gprs:
+; PRESERVE-NOT: x19
+; PRESERVE-NOT: x20
+; PRESERVE: bl __aarch64_cas16_acq_rel
+; PRESERVE: ret
+;
+; GISEL-LABEL: cas16_preserves_gprs:
+; GISEL-NOT: x19
+; GISEL-NOT: x20
+; GISEL: bl __aarch64_cas16_acq_rel
+; GISEL: ret
+;
+; CSRMASK-LABEL: name: cas16_preserves_gprs
+; CSRMASK: BL &__aarch64_cas16_acq_rel,
csr_aarch64_outline_atomics_preservemost
+; ARM64EC-LABEL: name: '#cas16_preserves_gprs'
+; ARM64EC: BL &"#__aarch64_cas16_acq_rel", csr_aarch64_aapcs
+ %pair = cmpxchg ptr %p, i128 %exp, i128 %des acq_rel acquire
+ %old = extractvalue { i128, i1 } %pair, 0
+ %ext = zext i64 %extra to i128
+ %sum1 = add i128 %old, %des
+ %sum2 = add i128 %sum1, %ext
+ store volatile i128 %sum2, ptr %p
+ ret i128 %sum2
+}
+
+; Verify that NEON 128-bit vector registers (q0-q31) are preserved without
spills.
+define <4 x i32> @swp8_preserves_simd(ptr %p, i64 %v, <4 x i32> %vec0, <4 x
i32> %vec1) nounwind {
+; DEFAULT-LABEL: swp8_preserves_simd:
+; DEFAULT: stp q{{[0-9]+}}, q{{[0-9]+}}, [sp
+; DEFAULT: bl __aarch64_swp8_rel
+;
+; PRESERVE-LABEL: swp8_preserves_simd:
+; PRESERVE-NOT: stp q
+; PRESERVE-NOT: str q
+; PRESERVE: bl __aarch64_swp8_rel
+; PRESERVE-NOT: ldp q
+; PRESERVE-NOT: ldr q
+; PRESERVE: add v0.4s, v0.4s, v1.4s
+; PRESERVE: ret
+ %old = atomicrmw xchg ptr %p, i64 %v release
+ %res = add <4 x i32> %vec0, %vec1
+ ret <4 x i32> %res
+}
+
+; Verify the exact register boundaries:
+; - x4 (argument register) and x15-x17 (scratch) and NZCV are clobbered;
+; - x5 and x14 are preserved in place across the call.
+define i64 @exact_x4_x5_x14_x15_x16_x17_and_nzcv_boundary(ptr %p, i32 %v, i64
%x, i64 %y) nounwind {
+; PRESERVE-LABEL: exact_x4_x5_x14_x15_x16_x17_and_nzcv_boundary:
+; PRESERVE-NOT: mov {{x[0-9]+}}, x5
+; PRESERVE-NOT: mov {{x[0-9]+}}, x14
+; PRESERVE-DAG: mov [[SAVEX4:x[6-9]|x1[0-3]]], x4
+; PRESERVE-DAG: mov [[SAVEX15:x[6-9]|x1[0-3]]], x15
+; PRESERVE-DAG: mov [[SAVEX16:x[6-9]|x1[0-3]]], x16
+; PRESERVE-DAG: mov [[SAVEX17:x[6-9]|x1[0-3]]], x17
+; PRESERVE: bl __aarch64_ldclr4_relax
+; PRESERVE-DAG: mov x4, [[SAVEX4]]
+; PRESERVE-DAG: mov x15, [[SAVEX15]]
+; PRESERVE-DAG: mov x16, [[SAVEX16]]
+; PRESERVE-DAG: mov x17, [[SAVEX17]]
+; PRESERVE-DAG: cmp {{x[0-9]+}}, {{x[0-9]+}}
+; PRESERVE-DAG: csel x0,
+; PRESERVE-NOT: mov x5,
+; PRESERVE-NOT: mov x14,
+; PRESERVE: ret
+;
+; O0-LABEL: exact_x4_x5_x14_x15_x16_x17_and_nzcv_boundary:
+; O0: bl __aarch64_ldclr4_relax
+ %cmp = icmp eq i64 %x, %y
+ %r4 = call i64 asm sideeffect "", "={x4}"()
+ %r5 = call i64 asm sideeffect "", "={x5}"()
+ %r14 = call i64 asm sideeffect "", "={x14}"()
+ %r15 = call i64 asm sideeffect "", "={x15}"()
+ %r16 = call i64 asm sideeffect "", "={x16}"()
+ %r17 = call i64 asm sideeffect "", "={x17}"()
+ %old = atomicrmw and ptr %p, i32 %v monotonic
+ call void asm sideeffect "", "{x4},{x5},{x14},{x15},{x16},{x17}"(i64 %r4,
i64 %r5, i64 %r14, i64 %r15, i64 %r16, i64 %r17)
+ %old64 = zext i32 %old to i64
+ %sel = select i1 %cmp, i64 %x, i64 %old64
+ ret i64 %sel
+}
diff --git a/llvm/test/ThinLTO/AArch64/outline-atomics-preserve-most.ll
b/llvm/test/ThinLTO/AArch64/outline-atomics-preserve-most.ll
new file mode 100644
index 00000000000000..6c5f9b668d354d
--- /dev/null
+++ b/llvm/test/ThinLTO/AArch64/outline-atomics-preserve-most.ll
@@ -0,0 +1,88 @@
+;; Verify that +outline-atomics-preserve-most is InlineIgnore across ThinLTO
+;; module boundaries and lowers inlined atomicrmw operations using the
+;; preserve-most calling convention on both Linux and Fuchsia targets (via
+;; either function attributes or the backend flag).
+
+; RUN: split-file %s %t
+; RUN: sed -e "s,TRIPLE,aarch64-unknown-linux-gnu,g" %t/caller.ll | opt
-module-summary -o %t/linux-caller.bc
+; RUN: sed -e "s,TRIPLE,aarch64-unknown-linux-gnu,g" %t/callee.ll | opt
-module-summary -o %t/linux-callee.bc
+; RUN: llvm-lto2 run %t/linux-caller.bc %t/linux-callee.bc -o %t/linux-attr \
+; RUN: -r=%t/linux-caller.bc,caller,px \
+; RUN: -r=%t/linux-caller.bc,callee_atomic,l \
+; RUN: -r=%t/linux-callee.bc,callee_atomic,pl \
+; RUN: -r=%t/linux-callee.bc,callee_with_live_vals,
+; RUN: llvm-objdump -d -r %t/linux-attr.1 | FileCheck %s
+
+; RUN: llvm-lto2 run %t/linux-callee.bc -aarch64-outline-atomics-preserve-most
-o %t/linux-flag \
+; RUN: -r=%t/linux-callee.bc,callee_with_live_vals,px \
+; RUN: -r=%t/linux-callee.bc,callee_atomic,pl
+; RUN: llvm-objdump -d -r %t/linux-flag.1 | FileCheck %s --check-prefix=FLAG
+
+; RUN: sed -e "s,TRIPLE,aarch64-unknown-fuchsia,g" %t/caller.ll | opt
-module-summary -o %t/fuchsia-caller.bc
+; RUN: sed -e "s,TRIPLE,aarch64-unknown-fuchsia,g" %t/callee.ll | opt
-module-summary -o %t/fuchsia-callee.bc
+; RUN: llvm-lto2 run %t/fuchsia-caller.bc %t/fuchsia-callee.bc -o
%t/fuchsia-attr \
+; RUN: -r=%t/fuchsia-caller.bc,caller,px \
+; RUN: -r=%t/fuchsia-caller.bc,callee_atomic,l \
+; RUN: -r=%t/fuchsia-callee.bc,callee_atomic,pl \
+; RUN: -r=%t/fuchsia-callee.bc,callee_with_live_vals,
+; RUN: llvm-objdump -d -r %t/fuchsia-attr.1 | FileCheck %s
+
+; RUN: llvm-lto2 run %t/fuchsia-callee.bc
-aarch64-outline-atomics-preserve-most -o %t/fuchsia-flag \
+; RUN: -r=%t/fuchsia-callee.bc,callee_with_live_vals,px \
+; RUN: -r=%t/fuchsia-callee.bc,callee_atomic,pl
+; RUN: llvm-objdump -d -r %t/fuchsia-flag.1 | FileCheck %s --check-prefix=FLAG
+
+; CHECK-LABEL: <caller>:
+; CHECK-NOT: stp x19
+; CHECK-NOT: stp x20
+; CHECK: bl {{.*}} <caller+{{.*}}>
+; CHECK-NEXT: R_AARCH64_CALL26 __aarch64_ldadd4_acq_rel
+; CHECK-NOT: ldp x19
+; CHECK: ret
+
+; FLAG-LABEL: <callee_with_live_vals>:
+; FLAG-NOT: stp x19
+; FLAG-NOT: stp x20
+; FLAG: bl {{.*}} <callee_with_live_vals+{{.*}}>
+; FLAG-NEXT: R_AARCH64_CALL26 __aarch64_ldadd4_acq_rel
+; FLAG-NOT: ldp x19
+; FLAG: ret
+
+;--- caller.ll
+target datalayout =
"e-m:e-p270:32:32-p271:32:32-p272:64:64-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128-Fn32"
+target triple = "TRIPLE"
+
+declare i32 @callee_atomic(ptr, i32)
+
+define i64 @caller(ptr %p, i32 %v, i64 %a, i64 %b, i64 %c, i64 %d) #0 {
+ %old = call i32 @callee_atomic(ptr %p, i32 %v)
+ %old64 = zext i32 %old to i64
+ %s1 = add i64 %a, %b
+ %s2 = add i64 %c, %d
+ %s3 = add i64 %s1, %s2
+ %res = add i64 %s3, %old64
+ ret i64 %res
+}
+
+attributes #0 = { nounwind
"target-features"="+outline-atomics,+outline-atomics-preserve-most" }
+
+;--- callee.ll
+target datalayout =
"e-m:e-p270:32:32-p271:32:32-p272:64:64-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128-Fn32"
+target triple = "TRIPLE"
+
+define i32 @callee_atomic(ptr %p, i32 %v) #0 {
+ %old = atomicrmw add ptr %p, i32 %v acq_rel
+ ret i32 %old
+}
+
+define i64 @callee_with_live_vals(ptr %p, i32 %v, i64 %a, i64 %b, i64 %c, i64
%d) #0 {
+ %old = atomicrmw add ptr %p, i32 %v acq_rel
+ %old64 = zext i32 %old to i64
+ %s1 = add i64 %a, %b
+ %s2 = add i64 %c, %d
+ %s3 = add i64 %s1, %s2
+ %res = add i64 %s3, %old64
+ ret i64 %res
+}
+
+attributes #0 = { nounwind "target-features"="+outline-atomics" }
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits