Skip to content

Commit bf4d1e5

Browse files
committed
Always use SwiftCC and error if it’s unsupported in the runtime
1 parent f71456e commit bf4d1e5

File tree

5 files changed

+26
-29
lines changed

5 files changed

+26
-29
lines changed

include/swift/Runtime/Config.h

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,6 @@
3333
#endif
3434
#endif
3535

36-
/// Does the current Swift platform use LLVM's intrinsic "swiftcall"
37-
/// calling convention for Swift functions?
38-
#ifndef SWIFT_USE_SWIFTCALL
39-
// Clang doesn't support mangling functions with the swiftcall attribute
40-
// on Windows and crashes during compilation: http://bugs.llvm.org/show_bug.cgi?id=32000
41-
#if (__has_attribute(swiftcall) || defined(__linux__)) && !defined(_WIN32)
42-
#define SWIFT_USE_SWIFTCALL 1
43-
#else
44-
#define SWIFT_USE_SWIFTCALL 0
45-
#endif
46-
#endif
47-
4836
/// Does the current Swift platform allow information other than the
4937
/// class pointer to be stored in the isa field? If so, when deriving
5038
/// the class pointer of an object, we must apply a
@@ -105,18 +93,19 @@
10593
#define SWIFT_CC_preserve_all __attribute__((preserve_all))
10694
#define SWIFT_CC_c
10795

108-
#if SWIFT_USE_SWIFTCALL
96+
// Define SWIFT_CC_swift in terms of the Swift CC for runtime functions.
97+
// Functions outside the stdlib or runtime that include this file may be built
98+
// with a compiler that doesn't support swiftcall; don't define these macros
99+
// in that case so any incorrect usage is caught.
100+
#if __has_attribute(swiftcall)
109101
#define SWIFT_CC_swift __attribute__((swiftcall))
110102
#define SWIFT_CONTEXT __attribute__((swift_context))
111103
#define SWIFT_ERROR_RESULT __attribute__((swift_error_result))
112104
#define SWIFT_INDIRECT_RESULT __attribute__((swift_indirect_result))
113-
#else
114-
#define SWIFT_CC_swift
115-
#define SWIFT_CONTEXT
116-
#define SWIFT_ERROR_RESULT
117-
#define SWIFT_INDIRECT_RESULT
118105
#endif
119106

107+
#define SWIFT_CC_SwiftCC SWIFT_CC_swift
108+
120109
// Map a logical calling convention (e.g. RegisterPreservingCC) to LLVM calling
121110
// convention.
122111
#define SWIFT_LLVM_CC(CC) SWIFT_LLVM_CC_##CC
@@ -133,11 +122,7 @@
133122

134123
#define SWIFT_LLVM_CC_RegisterPreservingCC llvm::CallingConv::PreserveMost
135124

136-
#if SWIFT_USE_SWIFTCALL
137125
#define SWIFT_LLVM_CC_SwiftCC llvm::CallingConv::Swift
138-
#else
139-
#define SWIFT_LLVM_CC_SwiftCC llvm::CallingConv::C
140-
#endif
141126

142127
// If defined, it indicates that runtime function wrappers
143128
// should be used on all platforms, even they do not support

lib/IRGen/GenCall.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ static void addInoutParameterAttributes(IRGenModule &IGM,
108108

109109
static llvm::CallingConv::ID getFreestandingConvention(IRGenModule &IGM) {
110110
// TODO: use a custom CC that returns three scalars efficiently
111-
return SWIFT_LLVM_CC(SwiftCC);
111+
return IGM.SwiftCC;
112112
}
113113

114114
/// Expand the requirements of the given abstract calling convention
@@ -146,8 +146,6 @@ static void addIndirectResultAttributes(IRGenModule &IGM,
146146

147147
void IRGenModule::addSwiftSelfAttributes(llvm::AttributeList &attrs,
148148
unsigned argIndex) {
149-
if (!UseSwiftCC)
150-
return;
151149
llvm::AttrBuilder b;
152150
b.addAttribute(llvm::Attribute::SwiftSelf);
153151
attrs = attrs.addAttributes(this->LLVMContext,
@@ -160,7 +158,7 @@ void IRGenModule::addSwiftErrorAttributes(llvm::AttributeList &attrs,
160158
// We create a shadow stack location of the swifterror parameter for the
161159
// debugger on such platforms and so we can't mark the parameter with a
162160
// swifterror attribute.
163-
if (!UseSwiftCC || !this->IsSwiftErrorInRegister)
161+
if (!this->IsSwiftErrorInRegister)
164162
return;
165163

166164
llvm::AttrBuilder b;

lib/IRGen/IRGenModule.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,7 @@ IRGenModule::IRGenModule(IRGenerator &irgen,
397397
else
398398
RegisterPreservingCC = DefaultCC;
399399

400-
SwiftCC = SWIFT_LLVM_CC(SwiftCC);
401-
UseSwiftCC = (SwiftCC == llvm::CallingConv::Swift);
400+
SwiftCC = llvm::CallingConv::Swift;
402401

403402
if (IRGen.Opts.DebugInfoKind > IRGenDebugInfoKind::None)
404403
DebugInfo = IRGenDebugInfo::createIRGenDebugInfo(IRGen.Opts, *CI, *this,

lib/IRGen/IRGenModule.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,6 @@ class IRGenModule {
509509
llvm::CallingConv::ID DefaultCC; /// default calling convention
510510
llvm::CallingConv::ID RegisterPreservingCC; /// lightweight calling convention
511511
llvm::CallingConv::ID SwiftCC; /// swift calling convention
512-
bool UseSwiftCC;
513512

514513
Signature getAssociatedTypeMetadataAccessFunctionSignature();
515514
Signature getAssociatedTypeWitnessTableAccessFunctionSignature();

stdlib/public/runtime/HeapObject.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,22 @@
4646

4747
using namespace swift;
4848

49+
// Check to make sure the runtime is being built with a compiler that
50+
// supports the Swift calling convention.
51+
//
52+
// If the Swift calling convention is not in use, functions such as
53+
// swift_allocBox and swift_makeBoxUnique that rely on their return value
54+
// being passed in a register to be compatible with Swift may miscompile on
55+
// some platforms and silently fail.
56+
#if !__has_attribute(swiftcall)
57+
#error "The runtime must be built with a compiler that supports swiftcall."
58+
#endif
59+
60+
// Check that the user isn't manually disabling SWIFTCALL.
61+
#if defined(SWIFT_USE_SWIFTCALL) && !SWIFT_USE_SWIFTCALL
62+
#error "SWIFT_USE_SWIFTCALL=0 is not supported; swiftcall must always be used."
63+
#endif
64+
4965
/// Returns true if the pointer passed to a native retain or release is valid.
5066
/// If false, the operation should immediately return.
5167
static inline bool isValidPointerForNativeRetain(const void *p) {

0 commit comments

Comments
 (0)