Skip to content

[runtime] Always use SwiftCC #13311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 7 additions & 17 deletions include/swift/Runtime/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,6 @@
#endif
#endif

/// Does the current Swift platform use LLVM's intrinsic "swiftcall"
/// calling convention for Swift functions?
#ifndef SWIFT_USE_SWIFTCALL
// Clang doesn't support mangling functions with the swiftcall attribute
// on Windows and crashes during compilation: http://bugs.llvm.org/show_bug.cgi?id=32000
#if (__has_attribute(swiftcall) || defined(__linux__)) && !defined(_WIN32)
#define SWIFT_USE_SWIFTCALL 1
#else
#define SWIFT_USE_SWIFTCALL 0
#endif
#endif

/// Does the current Swift platform allow information other than the
/// class pointer to be stored in the isa field? If so, when deriving
/// the class pointer of an object, we must apply a
Expand Down Expand Up @@ -105,7 +93,11 @@
#define SWIFT_CC_preserve_all __attribute__((preserve_all))
#define SWIFT_CC_c

#if SWIFT_USE_SWIFTCALL
// Define SWIFT_CC_swift in terms of the Swift CC for runtime functions.
// Functions outside the stdlib or runtime that include this file may be built
// with a compiler that doesn't support swiftcall; don't define these macros
// in that case so any incorrect usage is caught.
#if __has_attribute(swiftcall)
#define SWIFT_CC_swift __attribute__((swiftcall))
#define SWIFT_CONTEXT __attribute__((swift_context))
#define SWIFT_ERROR_RESULT __attribute__((swift_error_result))
Expand All @@ -117,6 +109,8 @@
#define SWIFT_INDIRECT_RESULT
#endif

#define SWIFT_CC_SwiftCC SWIFT_CC_swift

// Map a logical calling convention (e.g. RegisterPreservingCC) to LLVM calling
// convention.
#define SWIFT_LLVM_CC(CC) SWIFT_LLVM_CC_##CC
Expand All @@ -133,11 +127,7 @@

#define SWIFT_LLVM_CC_RegisterPreservingCC llvm::CallingConv::PreserveMost

#if SWIFT_USE_SWIFTCALL
#define SWIFT_LLVM_CC_SwiftCC llvm::CallingConv::Swift
#else
#define SWIFT_LLVM_CC_SwiftCC llvm::CallingConv::C
#endif

// If defined, it indicates that runtime function wrappers
// should be used on all platforms, even they do not support
Expand Down
6 changes: 2 additions & 4 deletions lib/IRGen/GenCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ static void addInoutParameterAttributes(IRGenModule &IGM,

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

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

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

llvm::AttrBuilder b;
Expand Down
3 changes: 1 addition & 2 deletions lib/IRGen/IRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,7 @@ IRGenModule::IRGenModule(IRGenerator &irgen,
else
RegisterPreservingCC = DefaultCC;

SwiftCC = SWIFT_LLVM_CC(SwiftCC);
UseSwiftCC = (SwiftCC == llvm::CallingConv::Swift);
SwiftCC = llvm::CallingConv::Swift;

if (IRGen.Opts.DebugInfoKind > IRGenDebugInfoKind::None)
DebugInfo = IRGenDebugInfo::createIRGenDebugInfo(IRGen.Opts, *CI, *this,
Expand Down
1 change: 0 additions & 1 deletion lib/IRGen/IRGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,6 @@ class IRGenModule {
llvm::CallingConv::ID DefaultCC; /// default calling convention
llvm::CallingConv::ID RegisterPreservingCC; /// lightweight calling convention
llvm::CallingConv::ID SwiftCC; /// swift calling convention
bool UseSwiftCC;

Signature getAssociatedTypeMetadataAccessFunctionSignature();
Signature getAssociatedTypeWitnessTableAccessFunctionSignature();
Expand Down
16 changes: 16 additions & 0 deletions stdlib/public/runtime/HeapObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@

using namespace swift;

// Check to make sure the runtime is being built with a compiler that
// supports the Swift calling convention.
//
// If the Swift calling convention is not in use, functions such as
// swift_allocBox and swift_makeBoxUnique that rely on their return value
// being passed in a register to be compatible with Swift may miscompile on
// some platforms and silently fail.
#if !__has_attribute(swiftcall)
#error "The runtime must be built with a compiler that supports swiftcall."
#endif

// Check that the user isn't manually disabling SWIFTCALL.
#if defined(SWIFT_USE_SWIFTCALL) && !SWIFT_USE_SWIFTCALL
#error "SWIFT_USE_SWIFTCALL=0 is not supported; swiftcall must always be used."
#endif

/// Returns true if the pointer passed to a native retain or release is valid.
/// If false, the operation should immediately return.
static inline bool isValidPointerForNativeRetain(const void *p) {
Expand Down