Skip to content

[5.5] async function pointer support for Windows #40147

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 3 commits into from
Nov 15, 2021
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
4 changes: 3 additions & 1 deletion include/swift/AST/IRGenOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ class IRGenOptions {
/// Used on Windows to avoid cross-module references.
unsigned LazyInitializeClassMetadata : 1;
unsigned LazyInitializeProtocolConformances : 1;
unsigned IndirectAsyncFunctionPointer : 1;

/// Normally if the -read-legacy-type-info flag is not specified, we look for
/// a file named "legacy-<arch>.yaml" in SearchPathOpts.RuntimeLibraryPath.
Expand Down Expand Up @@ -406,7 +407,8 @@ class IRGenOptions {
ValueNames(false), EnableReflectionMetadata(true),
EnableReflectionNames(true), EnableAnonymousContextMangledNames(false),
ForcePublicLinkage(false), LazyInitializeClassMetadata(false),
LazyInitializeProtocolConformances(false), DisableLegacyTypeInfo(false),
LazyInitializeProtocolConformances(false),
IndirectAsyncFunctionPointer(false), DisableLegacyTypeInfo(false),
PrespecializeGenericMetadata(false), UseIncrementalLLVMCodeGen(true),
UseSwiftCall(false), UseTypeLayoutValueHandling(true),
GenerateProfile(false), EnableDynamicReplacementChaining(false),
Expand Down
5 changes: 5 additions & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1700,6 +1700,11 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
// witness.
Opts.LazyInitializeProtocolConformances = Triple.isOSBinFormatCOFF();

// PE/COFF cannot deal with the cross-module reference to the
// AsyncFunctionPointer data block. Force the use of indirect
// AsyncFunctionPointer access.
Opts.IndirectAsyncFunctionPointer = Triple.isOSBinFormatCOFF();

if (Args.hasArg(OPT_disable_legacy_type_info)) {
Opts.DisableLegacyTypeInfo = true;
}
Expand Down
50 changes: 47 additions & 3 deletions lib/IRGen/GenCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1977,6 +1977,46 @@ void IRGenFunction::emitAllExtractValues(llvm::Value *value,
out.add(Builder.CreateExtractValue(value, i));
}

namespace {
// TODO(compnerd) analyze if this should be out-lined via a runtime call rather
// than be open-coded. This needs to account for the fact that we are able to
// statically optimize this often times due to CVP changing the select to a
// `select i1 true, ...`.
llvm::Value *emitIndirectAsyncFunctionPointer(IRGenFunction &IGF,
llvm::Value *pointer) {
llvm::IntegerType *IntPtrTy = IGF.IGM.IntPtrTy;
llvm::Type *AsyncFunctionPointerPtrTy = IGF.IGM.AsyncFunctionPointerPtrTy;
llvm::Constant *Zero =
llvm::Constant::getIntegerValue(IntPtrTy, APInt(IntPtrTy->getBitWidth(),
0));
llvm::Constant *One =
llvm::Constant::getIntegerValue(IntPtrTy, APInt(IntPtrTy->getBitWidth(),
1));
llvm::Constant *NegativeOne =
llvm::Constant::getIntegerValue(IntPtrTy, APInt(IntPtrTy->getBitWidth(),
-2));
swift::irgen::Alignment PointerAlignment = IGF.IGM.getPointerAlignment();

llvm::Value *PtrToInt = IGF.Builder.CreatePtrToInt(pointer, IntPtrTy);
llvm::Value *And = IGF.Builder.CreateAnd(PtrToInt, One);
llvm::Value *ICmp = IGF.Builder.CreateICmpEQ(And, Zero);

llvm::Value *BitCast =
IGF.Builder.CreateBitCast(pointer, AsyncFunctionPointerPtrTy);

llvm::Value *UntaggedPointer = IGF.Builder.CreateAnd(PtrToInt, NegativeOne);
llvm::Value *IntToPtr =
IGF.Builder.CreateIntToPtr(UntaggedPointer,
AsyncFunctionPointerPtrTy->getPointerTo());
llvm::Value *Load = IGF.Builder.CreateLoad(IntToPtr, PointerAlignment);

// (select (icmp eq, (and (ptrtoint %AsyncFunctionPointer), 1), 0),
// (%AsyncFunctionPointer),
// (inttoptr (and (ptrtoint %AsyncFunctionPointer), -2)))
return IGF.Builder.CreateSelect(ICmp, BitCast, Load);
}
}

std::pair<llvm::Value *, llvm::Value *> irgen::getAsyncFunctionAndSize(
IRGenFunction &IGF, SILFunctionTypeRepresentation representation,
FunctionPointer functionPointer, llvm::Value *thickContext,
Expand All @@ -1998,9 +2038,11 @@ std::pair<llvm::Value *, llvm::Value *> irgen::getAsyncFunctionAndSize(
if (auto authInfo = functionPointer.getAuthInfo()) {
ptr = emitPointerAuthAuth(IGF, ptr, authInfo);
}
auto *afpPtr =
IGF.Builder.CreateBitCast(ptr, IGF.IGM.AsyncFunctionPointerPtrTy);
afpPtrValue = afpPtr;
afpPtrValue =
(IGF.IGM.getOptions().IndirectAsyncFunctionPointer)
? emitIndirectAsyncFunctionPointer(IGF, ptr)
: IGF.Builder.CreateBitCast(ptr,
IGF.IGM.AsyncFunctionPointerPtrTy);
}
return *afpPtrValue;
};
Expand Down Expand Up @@ -4800,6 +4842,8 @@ llvm::Value *FunctionPointer::getPointer(IRGenFunction &IGF) const {
auto *fnPtr = Value;
if (auto authInfo = AuthInfo) {
fnPtr = emitPointerAuthAuth(IGF, fnPtr, authInfo);
if (IGF.IGM.getOptions().IndirectAsyncFunctionPointer)
fnPtr = emitIndirectAsyncFunctionPointer(IGF, fnPtr);
}
auto *descriptorPtr =
IGF.Builder.CreateBitCast(fnPtr, IGF.IGM.AsyncFunctionPointerPtrTy);
Expand Down
40 changes: 37 additions & 3 deletions lib/IRGen/GenThunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,34 @@ void IRGenModule::emitDispatchThunk(SILDeclRef declRef) {

llvm::Constant *
IRGenModule::getAddrOfAsyncFunctionPointer(LinkEntity entity) {
return getAddrOfLLVMVariable(
LinkEntity::forAsyncFunctionPointer(entity),
NotForDefinition, DebugTypeInfo());
llvm::Constant *Pointer =
getAddrOfLLVMVariable(LinkEntity::forAsyncFunctionPointer(entity),
NotForDefinition, DebugTypeInfo());
if (!getOptions().IndirectAsyncFunctionPointer)
return Pointer;

// When the symbol does not have DLL Import storage, we must directly address
// it. Otherwise, we will form an invalid reference.
if (!Pointer->isDLLImportDependent())
return Pointer;

llvm::Constant *PointerPointer =
getOrCreateGOTEquivalent(Pointer,
LinkEntity::forAsyncFunctionPointer(entity));
llvm::Constant *PointerPointerConstant =
llvm::ConstantExpr::getPtrToInt(PointerPointer, IntPtrTy);
llvm::Constant *Marker =
llvm::Constant::getIntegerValue(IntPtrTy, APInt(IntPtrTy->getBitWidth(),
1));
// TODO(compnerd) ensure that the pointer alignment guarantees that bit-0 is
// cleared. We cannot use an `getOr` here as it does not form a relocatable
// expression.
llvm::Constant *Address =
llvm::ConstantExpr::getAdd(PointerPointerConstant, Marker);

IndirectAsyncFunctionPointers[entity] = Address;
return llvm::ConstantExpr::getIntToPtr(Address,
AsyncFunctionPointerTy->getPointerTo());
}

llvm::Constant *
Expand All @@ -374,6 +399,15 @@ IRGenModule::getSILFunctionForAsyncFunctionPointer(llvm::Constant *afp) {
return entity.getSILFunction();
}
}
for (auto &entry : IndirectAsyncFunctionPointers) {
if (entry.getSecond() == afp) {
auto entity = entry.getFirst();
assert(getOptions().IndirectAsyncFunctionPointer &&
"indirect async function found for non-indirect async function"
" target?");
return entity.getSILFunction();
}
}
return nullptr;
}

Expand Down
1 change: 1 addition & 0 deletions lib/IRGen/IRGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,7 @@ class IRGenModule {
LinkEntity entity);

llvm::DenseMap<LinkEntity, llvm::Constant*> GlobalVars;
llvm::DenseMap<LinkEntity, llvm::Constant*> IndirectAsyncFunctionPointers;
llvm::DenseMap<LinkEntity, llvm::Constant*> GlobalGOTEquivalents;
llvm::DenseMap<LinkEntity, llvm::Function*> GlobalFuncs;
llvm::DenseSet<const clang::Decl *> GlobalClangDecls;
Expand Down
33 changes: 33 additions & 0 deletions test/IRGen/async-inheritance.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// RUN: %empty-directory(%t)
// RUN: %target-build-swift-dylib(%t/%target-library-name(L)) -Xfrontend -disable-availability-checking -module-name L -emit-module -emit-module-path %t/L.swiftmodule %s -DL
// RUN: %target-build-swift -Xfrontend -disable-availability-checking -I%t -L%t -lL -parse-as-library %s -module-name E -o %t/E %target-rpath(%t)
// RUN: %target-codesign %t/E
// RUN: %target-run %t/E %t/%target-library-name(L) | %FileCheck %s

// REQUIRES: concurrency
// REQUIRES: concurrency_runtime
// REQUIRES: executable_test

// UNSUPPORTED: back_deployment_runtime

#if L
open class C {
public init() {}
open func f() async {
print("\(#function)")
}
}
#else
import L
class D: C {
}

@main
struct S {
public static func main() async {
await D().f()
}
}
#endif

// CHECK: f()
8 changes: 6 additions & 2 deletions test/IRGen/async/class_resilience.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -emit-module -enable-experimental-concurrency -disable-availability-checking -enable-library-evolution -emit-module-path=%t/resilient_class.swiftmodule -module-name=resilient_class %S/Inputs/resilient_class.swift
// RUN: %target-swift-frontend -I %t -emit-ir -enable-experimental-concurrency -disable-availability-checking -enable-library-evolution %s | %FileCheck --check-prefix=CHECK --check-prefix=CHECK-%target-cpu %s
// RUN: %target-swift-frontend -I %t -emit-ir -enable-experimental-concurrency -disable-availability-checking -enable-library-evolution %s | %FileCheck -check-prefix CHECK -check-prefix CHECK-%target-cpu -check-prefix CHECK-%target-import-type %s
// REQUIRES: concurrency

import resilient_class
Expand Down Expand Up @@ -42,7 +42,11 @@ open class MyBaseClass<T> {
// CHECK-SAME: %swift.async_func_pointer* @"$s16class_resilience9MyDerivedC4waitSiyYaF010resilient_A09BaseClassCADxyYaFTVTu"

// CHECK-LABEL: define {{(dllexport )?}}{{(protected )?}}swift{{(tail)?}}cc void @"$s16class_resilience14callsAwaitableyx010resilient_A09BaseClassCyxGYalF"(%swift.opaque* noalias nocapture %0, %swift.context* swiftasync %1{{.*}})
// CHECK: %swift.async_func_pointer* @"$s15resilient_class9BaseClassC4waitxyYaFTjTu"
// CHECK-DIRECT: %swift.async_func_pointer* @"$s15resilient_class9BaseClassC4waitxyYaFTjTu"
// CHECK-INDIRECT: [[LOAD:%[0-9]+]] = load %swift.async_func_pointer*, %swift.async_func_pointer** inttoptr (i64 and (i64 add (i64 ptrtoint (%swift.async_func_pointer** @"\01__imp_$s15resilient_class9BaseClassC4waitxyYaFTjTu" to i64), i64 1), i64 -2) to %swift.async_func_pointer**), align {{4|8}}
// CHECK-INDIRECT-NEXT: select i1 icmp eq (i64 and (i64 add (i64 ptrtoint (%swift.async_func_pointer** @"\01__imp_$s15resilient_class9BaseClassC4waitxyYaFTjTu" to i64), i64 1), i64 1), i64 0),
// CHECK-INDIRECT-SAME: %swift.async_func_pointer* inttoptr (i64 add (i64 ptrtoint (%swift.async_func_pointer** @"\01__imp_$s15resilient_class9BaseClassC4waitxyYaFTjTu" to i64), i64 1) to %swift.async_func_pointer*),
// CHECK-INDIRECT-SAME: %swift.async_func_pointer* [[LOAD]]
// CHECK: ret void
public func callsAwaitable<T>(_ c: BaseClass<T>) async -> T {
return await c.wait()
Expand Down
8 changes: 6 additions & 2 deletions test/IRGen/async/protocol_resilience.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -emit-module -enable-experimental-concurrency -disable-availability-checking -g -enable-library-evolution -emit-module-path=%t/resilient_protocol.swiftmodule -module-name=resilient_protocol %S/Inputs/resilient_protocol.swift
// RUN: %target-swift-frontend -I %t -emit-ir -enable-experimental-concurrency -disable-availability-checking -g -enable-library-evolution %s | %FileCheck --check-prefix=CHECK --check-prefix=CHECK-%target-cpu %s
// RUN: %target-swift-frontend -I %t -emit-ir -enable-experimental-concurrency -disable-availability-checking -g -enable-library-evolution %s | %FileCheck -check-prefix CHECK -check-prefix CHECK-%target-cpu -check-prefix CHECK-%target-import-type %s
// REQUIRES: concurrency

import resilient_protocol
Expand All @@ -27,7 +27,11 @@ public protocol MyAwaitable {
// CHECK-SAME: %swift.async_func_pointer* @"$s19protocol_resilience19ConformsToAwaitableVyxG010resilient_A00E0AaeFP4wait6ResultQzyYaFTWTu"

// CHECK-LABEL: define {{(dllexport )?}}{{(protected )?}}swift{{(tail)?}}cc void @"$s19protocol_resilience14callsAwaitabley6ResultQzxYa010resilient_A00D0RzlF"(%swift.opaque* noalias nocapture %0, %swift.context* swiftasync %1, %swift.opaque* noalias nocapture %2, %swift.type* %T, i8** %T.Awaitable)
// CHECK: %swift.async_func_pointer* @"$s18resilient_protocol9AwaitableP4wait6ResultQzyYaFTjTu"
// CHECK-DIRECT: %swift.async_func_pointer* @"$s18resilient_protocol9AwaitableP4wait6ResultQzyYaFTjTu"
// CHECK-INDIRECT: [[LOAD:%[0-9]+]] = load %swift.async_func_pointer*, %swift.async_func_pointer** inttoptr (i64 and (i64 add (i64 ptrtoint (%swift.async_func_pointer** @"\01__imp_$s18resilient_protocol9AwaitableP4wait6ResultQzyYaFTjTu" to i64), i64 1), i64 -2) to %swift.async_func_pointer**), align {{4|8}}
// CHECK-INDIRECT: select i1 icmp eq (i64 and (i64 add (i64 ptrtoint (%swift.async_func_pointer** @"\01__imp_$s18resilient_protocol9AwaitableP4wait6ResultQzyYaFTjTu" to i64), i64 1), i64 1), i64 0),
// CHECK-INDIRECT-SAME: %swift.async_func_pointer* inttoptr (i64 add (i64 ptrtoint (%swift.async_func_pointer** @"\01__imp_$s18resilient_protocol9AwaitableP4wait6ResultQzyYaFTjTu" to i64), i64 1) to %swift.async_func_pointer*),
// CHECK-INDIRECT-SAME: %swift.async_func_pointer* [[LOAD]]
// CHECK: ret void
public func callsAwaitable<T : Awaitable>(_ t: T) async -> T.Result {
return await t.wait()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
// REQUIRES: concurrency
// REQUIRES: concurrency_runtime
// UNSUPPORTED: back_deployment_runtime
// XFAIL: windows

import _Concurrency
import NonresilientClass
Expand Down