Skip to content

Commit 0b79254

Browse files
Merge pull request #2408 from swiftwasm/katei/async-await
[Experimental] async/await support
2 parents e189dbb + b565d02 commit 0b79254

File tree

10 files changed

+51
-23
lines changed

10 files changed

+51
-23
lines changed

cmake/caches/Runtime-WASI-wasm32.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ set(SWIFTWASM_DISABLE_REFLECTION_TEST YES CACHE BOOL "")
1818
# stdlib configurations
1919
set(SWIFT_BUILD_STATIC_STDLIB YES CACHE BOOL "")
2020
set(SWIFT_BUILD_DYNAMIC_STDLIB NO CACHE BOOL "")
21-
set(SWIFT_ENABLE_EXPERIMENTAL_CONCURRENCY NO CACHE BOOL "")
21+
set(SWIFT_ENABLE_EXPERIMENTAL_CONCURRENCY YES CACHE BOOL "")
2222
# TODO(katei): This should get turned off, as this is not an ABI stable platform.
2323
# But current CMake build system doesn't support SWIFT_STDLIB_STABLE_ABI=NO
2424
set(SWIFT_STDLIB_STABLE_ABI YES CACHE BOOL "")

include/swift/Runtime/HeapObject.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ void swift_slowDealloc(void *ptr, size_t bytes, size_t alignMask);
148148
/// It may also prove worthwhile to have this use a custom CC
149149
/// which preserves a larger set of registers.
150150
SWIFT_RUNTIME_EXPORT
151+
SWIFT_CC(swift)
151152
HeapObject *swift_retain(HeapObject *object);
152153

153154
SWIFT_RUNTIME_EXPORT

include/swift/Runtime/RuntimeFunctions.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ FUNCTION(CopyPOD, swift_copyPOD, C_CC, AlwaysAvailable,
155155
ATTRS(NoUnwind))
156156

157157
// void *swift_retain(void *ptr);
158-
FUNCTION(NativeStrongRetain, swift_retain, C_CC, AlwaysAvailable,
158+
FUNCTION(NativeStrongRetain, swift_retain, SwiftCC, AlwaysAvailable,
159159
RETURNS(RefCountedPtrTy),
160160
ARGS(RefCountedPtrTy),
161161
ATTRS(NoUnwind, FirstParamReturned))

lib/IRGen/GenCall.cpp

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1956,27 +1956,42 @@ std::pair<llvm::Value *, llvm::Value *> irgen::getAsyncFunctionAndSize(
19561956
{ // thin
19571957
IGF.Builder.emitBlock(thinBlock);
19581958
auto *ptr = functionPointer.getRawPointer();
1959-
if (auto authInfo = functionPointer.getAuthInfo()) {
1960-
ptr = emitPointerAuthAuth(IGF, ptr, authInfo);
1961-
}
1962-
auto *afpPtr =
1963-
IGF.Builder.CreateBitCast(ptr, IGF.IGM.AsyncFunctionPointerPtrTy);
1964-
if (emitFunction) {
1965-
llvm::Value *addrPtr = IGF.Builder.CreateStructGEP(afpPtr, 0);
1966-
auto *uncastFnPtr = IGF.emitLoadOfRelativePointer(
1967-
Address(addrPtr, IGF.IGM.getPointerAlignment()), /*isFar*/ false,
1968-
/*expectedType*/ functionPointer.getFunctionType()->getPointerTo());
1969-
auto *fnPtr = IGF.Builder.CreateBitCast(uncastFnPtr, IGF.IGM.Int8PtrTy);
1959+
1960+
// If function pointer is a direct pointer to a function, it could not be
1961+
// an async function pointer. And additive operation to function address is
1962+
// illegal on some archs like wasm32, so emit undefs instead.
1963+
if (isa<llvm::Function>(ptr)) {
1964+
if (emitFunction) {
1965+
auto *undef = llvm::UndefValue::get(IGF.IGM.Int8PtrTy);
1966+
fnPhiValues.push_back({thinBlock, undef});
1967+
}
1968+
if (emitSize) {
1969+
auto *undef = llvm::UndefValue::get(IGF.IGM.Int32Ty);
1970+
sizePhiValues.push_back({thinBlock, undef});
1971+
}
1972+
} else {
19701973
if (auto authInfo = functionPointer.getAuthInfo()) {
1971-
fnPtr = emitPointerAuthSign(IGF, fnPtr, authInfo);
1974+
ptr = emitPointerAuthAuth(IGF, ptr, authInfo);
1975+
}
1976+
auto *afpPtr =
1977+
IGF.Builder.CreateBitCast(ptr, IGF.IGM.AsyncFunctionPointerPtrTy);
1978+
if (emitFunction) {
1979+
llvm::Value *addrPtr = IGF.Builder.CreateStructGEP(afpPtr, 0);
1980+
auto *uncastFnPtr = IGF.emitLoadOfRelativePointer(
1981+
Address(addrPtr, IGF.IGM.getPointerAlignment()), /*isFar*/ false,
1982+
/*expectedType*/ functionPointer.getFunctionType()->getPointerTo());
1983+
auto *fnPtr = IGF.Builder.CreateBitCast(uncastFnPtr, IGF.IGM.Int8PtrTy);
1984+
if (auto authInfo = functionPointer.getAuthInfo()) {
1985+
fnPtr = emitPointerAuthSign(IGF, fnPtr, authInfo);
1986+
}
1987+
fnPhiValues.push_back({thinBlock, fnPtr});
1988+
}
1989+
if (emitSize) {
1990+
auto *sizePtr = IGF.Builder.CreateStructGEP(afpPtr, 1);
1991+
auto *size =
1992+
IGF.Builder.CreateLoad(sizePtr, IGF.IGM.getPointerAlignment());
1993+
sizePhiValues.push_back({thinBlock, size});
19721994
}
1973-
fnPhiValues.push_back({thinBlock, fnPtr});
1974-
}
1975-
if (emitSize) {
1976-
auto *sizePtr = IGF.Builder.CreateStructGEP(afpPtr, 1);
1977-
auto *size =
1978-
IGF.Builder.CreateLoad(sizePtr, IGF.IGM.getPointerAlignment());
1979-
sizePhiValues.push_back({thinBlock, size});
19801995
}
19811996
IGF.Builder.CreateBr(joinBlock);
19821997
}

lib/IRGen/IRGenFunction.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,14 @@ IRGenFunction::emitLoadOfRelativePointer(Address addr, bool isFar,
318318
if (!isFar) {
319319
value = Builder.CreateSExt(value, IGM.IntPtrTy);
320320
}
321+
322+
// FIXME: add absolute pointer mode for IRGen
323+
if (IGM.TargetInfo.OutputObjectFormat == llvm::Triple::Wasm) {
324+
auto *uncastPointer = Builder.CreateIntToPtr(value, IGM.Int8PtrTy);
325+
auto uncastPointerAddress = Address(uncastPointer, IGM.getPointerAlignment());
326+
auto pointer = Builder.CreateBitCast(uncastPointerAddress, expectedType);
327+
return pointer.getAddress();
328+
}
321329
auto *addrInt = Builder.CreatePtrToInt(addr.getAddress(), IGM.IntPtrTy);
322330
auto *uncastPointerInt = Builder.CreateAdd(addrInt, value);
323331
auto *uncastPointer = Builder.CreateIntToPtr(uncastPointerInt, IGM.Int8PtrTy);

stdlib/public/Concurrency/GlobalExecutor.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@
5656
#include "swift/Runtime/Concurrency.h"
5757
#include "TaskPrivate.h"
5858

59+
#if !SWIFT_CONCURRENCY_COOPERATIVE_GLOBAL_EXECUTOR
5960
#include <dispatch/dispatch.h>
61+
#endif
6062

6163
using namespace swift;
6264

stdlib/public/Concurrency/Mutex.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "swift/Runtime/MutexSingleThreaded.h"
2121
#endif
2222

23+
__attribute__ ((weak))
2324
SWIFT_NORETURN void swift::fatalError(uint32_t flags, const char *format, ...) {
2425
abort();
2526
}

stdlib/public/Concurrency/TaskGroup.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ extension Task.Group {
267267
@_silgen_name("swift_retain")
268268
func _swiftRetain(
269269
_ task: Builtin.NativeObject
270-
)
270+
) -> UInt
271271

272272
@_silgen_name("swift_task_group_add_pending")
273273
func _taskGroupAddPendingTask(

stdlib/public/runtime/HeapObject.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ static HeapObject *_swift_retain_(HeapObject *object) {
341341
return object;
342342
}
343343

344+
SWIFT_CC(swift)
344345
HeapObject *swift::swift_retain(HeapObject *object) {
345346
#ifdef SWIFT_STDLIB_SINGLE_THREADED_RUNTIME
346347
return swift_nonatomic_retain(object);

utils/webassembly/build-presets.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ skip-build-benchmarks
99
llvm-targets-to-build=X86;AArch64;WebAssembly
1010
swift-darwin-supported-archs=%(HOST_ARCHITECTURE)s
1111
compiler-vendor=swiftwasm
12-
enable-experimental-concurrency=0
12+
enable-experimental-concurrency=1
1313

1414
[preset: webassembly-install]
1515

0 commit comments

Comments
 (0)