Skip to content

[embedded] Use __has_feature(swiftcc) to detect Swift calling convention #72345

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
8 changes: 6 additions & 2 deletions include/swift/Runtime/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,12 @@ extern uintptr_t __COMPATIBILITY_LIBRARIES_CANNOT_CHECK_THE_IS_SWIFT_BIT_DIRECTL

// SWIFT_CC(swiftasync) is the Swift async calling convention.
// We assume that it supports mandatory tail call elimination.
#if __has_feature(swiftasynccc) && __has_attribute(swiftasynccall)
#define SWIFT_CC_swiftasync __attribute__((swiftasynccall))
#if __has_attribute(swiftasynccall)
# if __has_feature(swiftasynccc) || __has_extension(swiftasynccc)
# define SWIFT_CC_swiftasync __attribute__((swiftasynccall))
# else
# define SWIFT_CC_swiftasync SWIFT_CC_swift
# endif
#else
#define SWIFT_CC_swiftasync SWIFT_CC_swift
#endif
Expand Down
5 changes: 3 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,9 @@
extern "C" {
#endif

// FIXME: Replace with __has_feature(swiftcc) once that's added to Clang.
#if __has_feature(swiftasynccc)
// TODO: __has_feature(swiftasynccc) is just for older clang. Remove this
// when we no longer support older clang.
#if __has_extension(swiftcc) || __has_feature(swiftasynccc)
#define SWIFT_CC_swift __attribute__((swiftcall))
#define SWIFT_CONTEXT __attribute__((swift_context))
#define SWIFT_ERROR_RESULT __attribute__((swift_error_result))
Expand Down
35 changes: 35 additions & 0 deletions test/embedded/classes-wasm.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// RUN: %empty-directory(%t)
// RUN: split-file %s %t
// RUN: %swift-frontend -c %t/check.swift -parse-as-library -target wasm32-unknown-none-wasm \
// RUN: -enable-experimental-feature Embedded -Xcc -fdeclspec -disable-stack-protector \
// RUN: -o %t/check.o
// RUN: %clang -target wasm32-unknown-none-wasm %t/check.o %t/rt.c -nostdlib -o %t/check.wasm
// RUN: %target-run %t/check.wasm
// REQUIRES: executable_test
// REQUIRES: CPU=wasm32
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you point me to a CI job that actually runs these executable WASM tests?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasm CI runs executable tests on WasmKit runtime: https://ci.swift.org/job/swift-PR-Linux-crosscompile-wasm/31


//--- rt.c

#include <stddef.h>
#include <stdint.h>

void free(void *ptr) {}

int posix_memalign(void **memptr, size_t alignment, size_t size) {
uintptr_t mem = __builtin_wasm_memory_grow(0, (size + 0xffff) / 0x10000);
if (mem == -1) {
return -1;
}
*memptr = (void *)(mem * 0x10000);
*memptr = (void *)(((uintptr_t)*memptr + alignment - 1) & -alignment);
return 0;
}

//--- check.swift

class Foo {}

@_cdecl("_start")
func main() {
_ = Foo()
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def build(self, host_target):
# Test configuration
self.cmake_options.define('SWIFT_INCLUDE_TESTS:BOOL', 'TRUE')
self.cmake_options.define('SWIFT_ENABLE_SOURCEKIT_TESTS:BOOL', 'FALSE')
lit_test_paths = ['IRGen', 'stdlib', 'Concurrency/Runtime']
lit_test_paths = ['IRGen', 'stdlib', 'Concurrency/Runtime', 'embedded']
lit_test_paths = [os.path.join(
self.build_dir, 'test-wasi-wasm32', path) for path in lit_test_paths]
self.cmake_options.define('SWIFT_LIT_TEST_PATHS:STRING',
Expand Down