Skip to content

[embedded] Avoid using swiftcc on targets that don't support it #69822

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
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
35 changes: 25 additions & 10 deletions lib/IRGen/IRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,15 @@ IRGenModule::IRGenModule(IRGenerator &irgen,
C_CC = getOptions().PlatformCCallingConvention;
// TODO: use "tinycc" on platforms that support it
DefaultCC = SWIFT_DEFAULT_LLVM_CC;
SwiftCC = llvm::CallingConv::Swift;

bool isSwiftCCSupported =
clangASTContext.getTargetInfo().checkCallingConvention(clang::CC_Swift)
== clang::TargetInfo::CCCR_OK;
if (isSwiftCCSupported) {
SwiftCC = llvm::CallingConv::Swift;
} else {
SwiftCC = DefaultCC;
}

bool isAsyncCCSupported =
clangASTContext.getTargetInfo().checkCallingConvention(clang::CC_SwiftAsync)
Expand Down Expand Up @@ -601,15 +609,22 @@ IRGenModule::IRGenModule(IRGenerator &irgen,
AtomicBoolSize = Size(ClangASTContext->getTypeSize(atomicBoolTy));
AtomicBoolAlign = Alignment(ClangASTContext->getTypeSize(atomicBoolTy));
}
// On WebAssembly, tail optional arguments are not allowed because Wasm requires
// callee and caller signature to be the same. So LLVM adds dummy arguments for
// `swiftself` and `swifterror`. If there is `swiftself` but is no `swifterror` in
// a swiftcc function or invocation, then LLVM adds dummy `swifterror` parameter or
// argument. To count up how many dummy arguments should be added, we need to mark
// it as `swifterror` even though it's not in register.
ShouldUseSwiftError =
clang::CodeGen::swiftcall::isSwiftErrorLoweredInRegister(
ClangCodeGen->CGM()) || TargetInfo.OutputObjectFormat == llvm::Triple::Wasm;
if (TargetInfo.OutputObjectFormat == llvm::Triple::Wasm) {
// On WebAssembly, tail optional arguments are not allowed because Wasm
// requires callee and caller signature to be the same. So LLVM adds dummy
// arguments for `swiftself` and `swifterror`. If there is `swiftself` but
// is no `swifterror` in a swiftcc function or invocation, then LLVM adds
// dummy `swifterror` parameter or argument. To count up how many dummy
// arguments should be added, we need to mark it as `swifterror` even though
// it's not in register.
ShouldUseSwiftError = true;
} else if (!isSwiftCCSupported) {
ShouldUseSwiftError = false;
} else {
ShouldUseSwiftError =
clang::CodeGen::swiftcall::isSwiftErrorLoweredInRegister(
ClangCodeGen->CGM());
}

#ifndef NDEBUG
sanityCheckStdlib(*this);
Expand Down
16 changes: 14 additions & 2 deletions stdlib/public/SwiftShims/swift/shims/EmbeddedShims.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,20 @@
extern "C" {
#endif

typedef void __attribute__((swiftcall)) (*HeapObjectDestroyer)(
__attribute__((swift_context)) void *object);
// FIXME: Replace with __has_feature(swiftcc) once that's added to Clang.
#if __has_feature(swiftasynccc)
#define SWIFT_CC_swift __attribute__((swiftcall))
#define SWIFT_CONTEXT __attribute__((swift_context))
#define SWIFT_ERROR_RESULT __attribute__((swift_error_result))
#define SWIFT_INDIRECT_RESULT __attribute__((swift_indirect_result))
#else
#define SWIFT_CC_swift
#define SWIFT_CONTEXT
#define SWIFT_ERROR_RESULT
#define SWIFT_INDIRECT_RESULT
#endif

typedef void SWIFT_CC_swift (*HeapObjectDestroyer)(SWIFT_CONTEXT void *object);

static inline void _swift_embedded_invoke_heap_object_destroy(void *object) {
void *metadata = *(void **)object;
Expand Down