Skip to content

Commit 7d7cf1f

Browse files
authored
Merge pull request #69822 from kubamracek/embedded-fallback-to-c-calling-conv
[embedded] Avoid using swiftcc on targets that don't support it
2 parents 26a04fe + df09cf1 commit 7d7cf1f

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

lib/IRGen/IRGenModule.cpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,15 @@ IRGenModule::IRGenModule(IRGenerator &irgen,
572572
C_CC = getOptions().PlatformCCallingConvention;
573573
// TODO: use "tinycc" on platforms that support it
574574
DefaultCC = SWIFT_DEFAULT_LLVM_CC;
575-
SwiftCC = llvm::CallingConv::Swift;
575+
576+
bool isSwiftCCSupported =
577+
clangASTContext.getTargetInfo().checkCallingConvention(clang::CC_Swift)
578+
== clang::TargetInfo::CCCR_OK;
579+
if (isSwiftCCSupported) {
580+
SwiftCC = llvm::CallingConv::Swift;
581+
} else {
582+
SwiftCC = DefaultCC;
583+
}
576584

577585
bool isAsyncCCSupported =
578586
clangASTContext.getTargetInfo().checkCallingConvention(clang::CC_SwiftAsync)
@@ -601,15 +609,22 @@ IRGenModule::IRGenModule(IRGenerator &irgen,
601609
AtomicBoolSize = Size(ClangASTContext->getTypeSize(atomicBoolTy));
602610
AtomicBoolAlign = Alignment(ClangASTContext->getTypeSize(atomicBoolTy));
603611
}
604-
// On WebAssembly, tail optional arguments are not allowed because Wasm requires
605-
// callee and caller signature to be the same. So LLVM adds dummy arguments for
606-
// `swiftself` and `swifterror`. If there is `swiftself` but is no `swifterror` in
607-
// a swiftcc function or invocation, then LLVM adds dummy `swifterror` parameter or
608-
// argument. To count up how many dummy arguments should be added, we need to mark
609-
// it as `swifterror` even though it's not in register.
610-
ShouldUseSwiftError =
611-
clang::CodeGen::swiftcall::isSwiftErrorLoweredInRegister(
612-
ClangCodeGen->CGM()) || TargetInfo.OutputObjectFormat == llvm::Triple::Wasm;
612+
if (TargetInfo.OutputObjectFormat == llvm::Triple::Wasm) {
613+
// On WebAssembly, tail optional arguments are not allowed because Wasm
614+
// requires callee and caller signature to be the same. So LLVM adds dummy
615+
// arguments for `swiftself` and `swifterror`. If there is `swiftself` but
616+
// is no `swifterror` in a swiftcc function or invocation, then LLVM adds
617+
// dummy `swifterror` parameter or argument. To count up how many dummy
618+
// arguments should be added, we need to mark it as `swifterror` even though
619+
// it's not in register.
620+
ShouldUseSwiftError = true;
621+
} else if (!isSwiftCCSupported) {
622+
ShouldUseSwiftError = false;
623+
} else {
624+
ShouldUseSwiftError =
625+
clang::CodeGen::swiftcall::isSwiftErrorLoweredInRegister(
626+
ClangCodeGen->CGM());
627+
}
613628

614629
#ifndef NDEBUG
615630
sanityCheckStdlib(*this);

stdlib/public/SwiftShims/swift/shims/EmbeddedShims.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,20 @@
2828
extern "C" {
2929
#endif
3030

31-
typedef void __attribute__((swiftcall)) (*HeapObjectDestroyer)(
32-
__attribute__((swift_context)) void *object);
31+
// FIXME: Replace with __has_feature(swiftcc) once that's added to Clang.
32+
#if __has_feature(swiftasynccc)
33+
#define SWIFT_CC_swift __attribute__((swiftcall))
34+
#define SWIFT_CONTEXT __attribute__((swift_context))
35+
#define SWIFT_ERROR_RESULT __attribute__((swift_error_result))
36+
#define SWIFT_INDIRECT_RESULT __attribute__((swift_indirect_result))
37+
#else
38+
#define SWIFT_CC_swift
39+
#define SWIFT_CONTEXT
40+
#define SWIFT_ERROR_RESULT
41+
#define SWIFT_INDIRECT_RESULT
42+
#endif
43+
44+
typedef void SWIFT_CC_swift (*HeapObjectDestroyer)(SWIFT_CONTEXT void *object);
3345

3446
static inline void _swift_embedded_invoke_heap_object_destroy(void *object) {
3547
void *metadata = *(void **)object;

0 commit comments

Comments
 (0)