Skip to content

Commit b985794

Browse files
committed
Prepare IRGen and LLVM passes to use the new preserve_most calling convention, but do not enable it yet.
The convention should be enabled once we can properly build the runtime library using Siwft's own clang/llvm binaries.
1 parent 12f3825 commit b985794

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

include/swift/Runtime/Config.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@
104104
#define SWIFT_CC_DefaultCC_IMPL SWIFT_CC_c
105105
#define SWIFT_LLVM_CC_DefaultCC llvm::CallingConv::C
106106

107+
#define SWIFT_LLVM_CC_RegisterPreservingCC llvm::CallingConv::PreserveMost
108+
109+
107110
// If defined, it indicates that runtime function wrappers
108111
// should be used on all platforms, even they do not support
109112
// the new calling convention which requires this.
@@ -113,13 +116,19 @@
113116
// supported by the current target.
114117
// TODO: Define it once the runtime calling convention support has
115118
// been integrated into clang and llvm.
116-
//#define RT_USE_RegisterPreservingCC
119+
#define SWIFT_RT_USE_RegisterPreservingCC 0
120+
121+
#if __has_attribute(preserve_most)
122+
#define SWIFT_BACKEND_SUPPORTS_RegisterPreservingCC 1
123+
#else
124+
#define SWIFT_BACKEND_SUPPORTS_RegisterPreservingCC 0
125+
#endif
126+
117127

118128
// RegisterPreservingCC is a dedicated runtime calling convention to be used
119129
// when calling the most popular runtime functions.
120-
#if defined(RT_USE_RegisterPreservingCC) && __has_attribute(preserve_most) && \
121-
(defined(__aarch64__) || defined(__x86_64__))
122-
130+
#if SWIFT_RT_USE_RegisterPreservingCC && \
131+
SWIFT_BACKEND_SUPPORTS_RegisterPreservingCC && defined(__aarch64__)
123132
// Targets supporting the dedicated runtime convention should use it.
124133
// If a runtime function is using this calling convention, it can
125134
// be invoked only by means of a wrapper, which performs an indirect
@@ -134,7 +143,6 @@
134143
SWIFT_CC_preserve_most
135144
#define SWIFT_CC_RegisterPreservingCC_IMPL \
136145
SWIFT_CC_preserve_most
137-
#define SWIFT_LLVM_CC_RegisterPreservingCC llvm::CallingConv::PreserveMost
138146

139147
// Indicate that wrappers should be used, because it is required
140148
// for the calling convention to get around dynamic linking issues.
@@ -147,7 +155,6 @@
147155
// No wrappers are required in this case by the calling convention.
148156
#define SWIFT_CC_RegisterPreservingCC SWIFT_CC_c
149157
#define SWIFT_CC_RegisterPreservingCC_IMPL SWIFT_CC_c
150-
#define SWIFT_LLVM_CC_RegisterPreservingCC llvm::CallingConv::C
151158

152159
#endif
153160

@@ -171,6 +178,7 @@
171178
#define SWIFT_RT_ENTRY_REF_AS_STR(Name) "_" #Name
172179

173180
#if defined(SWIFT_RT_USE_WRAPPERS_ALWAYS)
181+
#undef SWIFT_RT_USE_WRAPPERS
174182
#define SWIFT_RT_USE_WRAPPERS
175183
#endif
176184

lib/IRGen/IRGenModule.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -375,11 +375,13 @@ IRGenModule::IRGenModule(IRGenModuleDispatcher &dispatcher, SourceFile *SF,
375375

376376
// Only use the new calling conventions on platforms that support it.
377377
auto Arch = Triple.getArch();
378-
if (Arch == llvm::Triple::ArchType::x86_64 ||
379-
Arch == llvm::Triple::ArchType::aarch64)
378+
if (SWIFT_RT_USE_RegisterPreservingCC &&
379+
Arch == llvm::Triple::ArchType::aarch64) {
380380
RegisterPreservingCC = SWIFT_LLVM_CC(RegisterPreservingCC);
381-
else
381+
}
382+
else {
382383
RegisterPreservingCC = DefaultCC;
384+
}
383385

384386
ABITypes = new CodeGenABITypes(clangASTContext, Module);
385387

lib/LLVMPasses/ARCEntryPointBuilder.h

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "llvm/IR/IRBuilder.h"
2020
#include "llvm/IR/Module.h"
2121
#include "llvm/ADT/APInt.h"
22+
#include "llvm/ADT/Triple.h"
2223

2324
namespace swift {
2425

@@ -86,10 +87,26 @@ class ARCEntryPointBuilder {
8687
public:
8788
ARCEntryPointBuilder(Function &F)
8889
: B(&*F.begin()), Retain(), ObjectPtrTy(),
89-
DefaultCC(SWIFT_LLVM_CC(DefaultCC)),
90-
RegisterPreservingCC(SWIFT_LLVM_CC(RegisterPreservingCC)) {
91-
//TODO: If the target does not support the new calling convention,
92-
//set RegisterPreservingCC to use a standard C calling convention.
90+
DefaultCC(SWIFT_LLVM_CC(DefaultCC)) {
91+
//If the target does not support the new calling convention,
92+
//set RegisterPreservingCC to use a default calling convention.
93+
RegisterPreservingCC = DefaultCC;
94+
95+
// Check if the register preserving calling convention
96+
// is supported by the backend and should be used
97+
// for the deployment target provided for this compilation.
98+
if (SWIFT_RT_USE_RegisterPreservingCC) {
99+
bool ShouldUseRegisterPreservingCC = false;
100+
auto &TargeTriple = F.getParent()->getTargetTriple();
101+
llvm::Triple Triple(TargeTriple);
102+
auto Arch = Triple.getArch();
103+
if (Arch == llvm::Triple::ArchType::aarch64) {
104+
ShouldUseRegisterPreservingCC = true;
105+
}
106+
107+
if (ShouldUseRegisterPreservingCC)
108+
RegisterPreservingCC = SWIFT_LLVM_CC(RegisterPreservingCC);
109+
}
93110
}
94111

95112
~ARCEntryPointBuilder() = default;

0 commit comments

Comments
 (0)