-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Add swift_getTypeByMangledNameInContextInMetadataState such that we can #28337
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
Changes from all commits
0d324d2
44b99b7
cf20dd4
84bb456
2715f13
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2156,16 +2156,29 @@ static bool shouldAccessByMangledName(IRGenModule &IGM, CanType type) { | |
|
||
} | ||
|
||
static bool canIssueIncompleteMetadataRequests(IRGenModule &IGM) { | ||
// We can only answer blocking complete metadata requests with the <=5.1 | ||
// runtime ABI entry points. | ||
auto &context = IGM.getSwiftModule()->getASTContext(); | ||
auto deploymentAvailability = | ||
AvailabilityContext::forDeploymentTarget(context); | ||
return deploymentAvailability.isContainedIn( | ||
context.getTypesInAbstractMetadataStateAvailability()); | ||
} | ||
|
||
/// Emit a call to a type metadata accessor using a mangled name. | ||
static MetadataResponse | ||
emitMetadataAccessByMangledName(IRGenFunction &IGF, CanType type, | ||
DynamicMetadataRequest request) { | ||
// TODO: We can only answer blocking complete metadata requests with the | ||
// <=5.1 runtime ABI entry points. | ||
assert(request.isStaticallyBlockingComplete() | ||
&& "can only form complete metadata by mangled name"); | ||
|
||
auto &IGM = IGF.IGM; | ||
|
||
// We can only answer blocking complete metadata requests with the <=5.1 | ||
// runtime ABI entry points. | ||
assert((request.isStaticallyBlockingComplete() || | ||
(request.isStaticallyAbstract() && | ||
canIssueIncompleteMetadataRequests(IGM))) && | ||
"can only form complete metadata by mangled name"); | ||
|
||
llvm::Constant *mangledString; | ||
unsigned mangledStringSize; | ||
std::tie(mangledString, mangledStringSize) = | ||
|
@@ -2198,11 +2211,15 @@ emitMetadataAccessByMangledName(IRGenFunction &IGF, CanType type, | |
} | ||
|
||
// Get or create a shared helper function to do the instantiation. | ||
auto instantiationFnName = | ||
request.isStaticallyAbstract() | ||
? "__swift_instantiateConcreteTypeFromMangledNameAbstract" | ||
: "__swift_instantiateConcreteTypeFromMangledName"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for implementing this, Arnold! I wonder whether, if we're targeting Swift >= 5.2, we could only emit |
||
auto instantiationFn = cast<llvm::Function>( | ||
IGM.getModule() | ||
->getOrInsertFunction("__swift_instantiateConcreteTypeFromMangledName", | ||
IGF.IGM.TypeMetadataPtrTy, cache->getType()) | ||
.getCallee()); | ||
IGM.getModule() | ||
->getOrInsertFunction(instantiationFnName, IGF.IGM.TypeMetadataPtrTy, | ||
cache->getType()) | ||
.getCallee()); | ||
if (instantiationFn->empty()) { | ||
ApplyIRLinkage(IRLinkage::InternalLinkOnceODR) | ||
.to(instantiationFn); | ||
|
@@ -2212,7 +2229,7 @@ emitMetadataAccessByMangledName(IRGenFunction &IGF, CanType type, | |
llvm::Attribute::NoInline); | ||
IGM.setHasFramePointer(instantiationFn, false); | ||
|
||
[&IGM, instantiationFn]{ | ||
[&IGM, instantiationFn, request]{ | ||
IRGenFunction subIGF(IGM, instantiationFn); | ||
|
||
auto params = subIGF.collectParameters(); | ||
|
@@ -2279,15 +2296,26 @@ emitMetadataAccessByMangledName(IRGenFunction &IGF, CanType type, | |
auto stringAddr = subIGF.Builder.CreateAdd(stringAddrBase, | ||
stringAddrOffset); | ||
stringAddr = subIGF.Builder.CreateIntToPtr(stringAddr, IGM.Int8PtrTy); | ||
|
||
auto call = | ||
subIGF.Builder.CreateCall(IGM.getGetTypeByMangledNameInContextFn(), | ||
{stringAddr, | ||
size, | ||
// TODO: Use mangled name lookup in generic | ||
// contexts? | ||
llvm::ConstantPointerNull::get(IGM.TypeContextDescriptorPtrTy), | ||
llvm::ConstantPointerNull::get(IGM.Int8PtrPtrTy)}); | ||
|
||
llvm::CallInst *call; | ||
if (request.isStaticallyAbstract()) { | ||
call = subIGF.Builder.CreateCall( | ||
IGM.getGetTypeByMangledNameInContextInMetadataStateFn(), | ||
{llvm::ConstantInt::get(IGM.SizeTy, (size_t)MetadataState::Abstract), | ||
stringAddr, size, | ||
// TODO: Use mangled name lookup in generic | ||
// contexts? | ||
llvm::ConstantPointerNull::get(IGM.TypeContextDescriptorPtrTy), | ||
llvm::ConstantPointerNull::get(IGM.Int8PtrPtrTy)}); | ||
} else { | ||
call = subIGF.Builder.CreateCall( | ||
IGM.getGetTypeByMangledNameInContextFn(), | ||
{stringAddr, size, | ||
// TODO: Use mangled name lookup in generic | ||
// contexts? | ||
llvm::ConstantPointerNull::get(IGM.TypeContextDescriptorPtrTy), | ||
llvm::ConstantPointerNull::get(IGM.Int8PtrPtrTy)}); | ||
} | ||
call->setDoesNotThrow(); | ||
call->setDoesNotAccessMemory(); | ||
call->setCallingConv(IGM.SwiftCC); | ||
|
@@ -2339,14 +2367,13 @@ emitCallToTypeMetadataAccessFunction(IRGenFunction &IGF, CanType type, | |
// single access by mangled name instead, if we're asking for complete | ||
// metadata. | ||
// | ||
// TODO: The getTypeByMangledNameInContext entry point in Swift <=5.1 can | ||
// only answer requests for complete metadata. We could introduce new | ||
// entry points that could answer all metadata requests. | ||
if (request.isStaticallyBlockingComplete() | ||
&& shouldAccessByMangledName(IGF.IGM, type)) { | ||
if ((request.isStaticallyBlockingComplete() || | ||
(request.isStaticallyAbstract() && | ||
canIssueIncompleteMetadataRequests(IGF.IGM))) && | ||
shouldAccessByMangledName(IGF.IGM, type)) { | ||
return emitMetadataAccessByMangledName(IGF, type, request); | ||
} | ||
|
||
llvm::Constant *accessor = | ||
getOrCreateTypeMetadataAccessFunction(IGF.IGM, type); | ||
llvm::CallInst *call = IGF.Builder.CreateCall(accessor, { request.get(IGF) }); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// RUN: %target-swift-frontend -target x86_64-apple-macosx10.99 -emit-ir %S/../Inputs/conditional_conformance_basic_conformances.swift | %FileCheck %S/../Inputs/conditional_conformance_basic_conformances.swift --check-prefix=TYPEBYNAME | ||
|
||
// Too many pointer-sized integers in the IR | ||
// REQUIRES: PTRSIZE=64 | ||
// REQUIRES: OS=macosx | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd also be good to add the
swift_getTypeByMangledNameInEnvironmentInMetadataState
variant, so we have it available for future use to instantiate metadata in generic environments that don't have an associated type context.