Skip to content

IRGen: enable the dynamic cast optimization on Windows #42286

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 1 commit into from
Apr 11, 2022
Merged
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
16 changes: 14 additions & 2 deletions lib/IRGen/GenCast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "llvm/IR/Module.h"

#include "swift/AST/ExistentialLayout.h"
#include "swift/AST/IRGenOptions.h"
#include "swift/SIL/DynamicCasts.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILModule.h"
Expand Down Expand Up @@ -1061,8 +1062,8 @@ llvm::Value *irgen::emitFastClassCastIfPossible(IRGenFunction &IGF,
llvm::Value *instance,
CanType sourceFormalType,
CanType targetFormalType) {
if (!doesCastPreserveOwnershipForTypes(IGF.IGM.getSILModule(), sourceFormalType,
targetFormalType)) {
if (!doesCastPreserveOwnershipForTypes(IGF.IGM.getSILModule(),
sourceFormalType, targetFormalType)) {
return nullptr;
}

Expand All @@ -1089,6 +1090,17 @@ llvm::Value *irgen::emitFastClassCastIfPossible(IRGenFunction &IGF,

// Get the metadata pointer of the destination class type.
llvm::Value *destMetadata = IGF.IGM.getAddrOfTypeMetadata(targetFormalType);
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems that we should no longer call getAddrOfTypeMetadata for lazy initialized metadata. It looks like dead code to me.

Copy link
Member Author

Choose a reason for hiding this comment

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

No, it is not dead code - it is simply unnecessary work. It is the "else" case that is being hoisted and done unconditionally. I can sink it into an else clause.

if (IGF.IGM.IRGen.Opts.LazyInitializeClassMetadata) {
llvm::Function *accessor =
IGF.IGM.getAddrOfTypeMetadataAccessFunction(targetFormalType,
NotForDefinition);
auto request = DynamicMetadataRequest(MetadataState::Complete);
// We know that we are not in a gneric class context, so we can safely
// determine that the call here does not need to take that into account.
auto response =
IGF.emitGenericTypeMetadataAccessFunctionCall(accessor, {}, request);
destMetadata = response.getMetadata();
}
llvm::Value *lhs = IGF.Builder.CreateBitCast(destMetadata, IGF.IGM.Int8PtrTy);

// Load the isa pointer.
Expand Down