Skip to content

Commit 8bdabe8

Browse files
committed
IRGen: Add LinkEntity::getDeclContextForEmission()
This replaces getSourceFileForEmission().
1 parent 7fb6233 commit 8bdabe8

File tree

3 files changed

+20
-54
lines changed

3 files changed

+20
-54
lines changed

include/swift/IRGen/Linking.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,9 +1137,9 @@ class LinkEntity {
11371137
/// Determine whether this entity will be weak-imported.
11381138
bool isWeakImported(ModuleDecl *module) const;
11391139

1140-
/// Return the source file whose codegen should trigger emission of this
1141-
/// link entity, if one can be identified.
1142-
const SourceFile *getSourceFileForEmission() const;
1140+
/// Return the module scope context whose codegen should trigger emission
1141+
/// of this link entity, if one can be identified.
1142+
DeclContext *getDeclContextForEmission() const;
11431143

11441144
/// Get the preferred alignment for the definition of this entity.
11451145
Alignment getAlignment(IRGenModule &IGM) const;

lib/IRGen/GenDecl.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2947,9 +2947,11 @@ IRGenModule::getAddrOfLLVMVariableOrGOTEquivalent(LinkEntity entity) {
29472947
// If the entity will be emitted as part of the current source file
29482948
// (if we know what that is), then we can reference it directly.
29492949
if (CurSourceFile
2950-
&& !isa<ClangModuleUnit>(CurSourceFile)
2951-
&& CurSourceFile == entity.getSourceFileForEmission())
2952-
return direct();
2950+
&& !isa<ClangModuleUnit>(CurSourceFile)) {
2951+
if (auto *dc = entity.getDeclContextForEmission())
2952+
if (CurSourceFile == dc->getParentSourceFile())
2953+
return direct();
2954+
}
29532955

29542956
// TODO: If we know the target entry is going to be linked into the same
29552957
// binary, then we ought to be able to directly relative-reference the

lib/IRGen/Linking.cpp

Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,20 +1071,7 @@ bool LinkEntity::isWeakImported(ModuleDecl *module) const {
10711071
llvm_unreachable("Bad link entity kind");
10721072
}
10731073

1074-
const SourceFile *LinkEntity::getSourceFileForEmission() const {
1075-
const SourceFile *sf;
1076-
1077-
// Shared-linkage entities don't get emitted with any particular file.
1078-
if (hasSharedVisibility(getLinkage(NotForDefinition)))
1079-
return nullptr;
1080-
1081-
auto getSourceFileForDeclContext =
1082-
[](const DeclContext *dc) -> const SourceFile * {
1083-
if (!dc)
1084-
return nullptr;
1085-
return dc->getParentSourceFile();
1086-
};
1087-
1074+
DeclContext *LinkEntity::getDeclContextForEmission() const {
10881075
switch (getKind()) {
10891076
case Kind::DispatchThunk:
10901077
case Kind::DispatchThunkInitializer:
@@ -1122,60 +1109,39 @@ const SourceFile *LinkEntity::getSourceFileForEmission() const {
11221109
case Kind::OpaqueTypeDescriptorAccessorImpl:
11231110
case Kind::OpaqueTypeDescriptorAccessorKey:
11241111
case Kind::OpaqueTypeDescriptorAccessorVar:
1125-
sf = getSourceFileForDeclContext(getDecl()->getDeclContext());
1126-
if (!sf)
1127-
return nullptr;
1128-
break;
1112+
return getDecl()->getDeclContext();
11291113

11301114
case Kind::SILFunction:
11311115
case Kind::DynamicallyReplaceableFunctionVariable:
11321116
case Kind::DynamicallyReplaceableFunctionKey:
1133-
sf = getSourceFileForDeclContext(getSILFunction()->getDeclContext());
1134-
if (!sf)
1135-
return nullptr;
1136-
break;
1117+
return getSILFunction()->getDeclContext();
11371118

11381119
case Kind::SILGlobalVariable:
1139-
if (auto decl = getSILGlobalVariable()->getDecl()) {
1140-
sf = getSourceFileForDeclContext(decl->getDeclContext());
1141-
if (!sf)
1142-
return nullptr;
1143-
} else {
1144-
return nullptr;
1145-
}
1146-
break;
1120+
if (auto decl = getSILGlobalVariable()->getDecl())
1121+
return decl->getDeclContext();
1122+
1123+
return nullptr;
11471124

11481125
case Kind::ProtocolWitnessTable:
11491126
case Kind::ProtocolConformanceDescriptor:
1150-
sf = getSourceFileForDeclContext(
1151-
getRootProtocolConformance()->getDeclContext());
1152-
if (!sf)
1153-
return nullptr;
1154-
break;
1127+
return getRootProtocolConformance()->getDeclContext();
11551128

11561129
case Kind::ProtocolWitnessTablePattern:
11571130
case Kind::GenericProtocolWitnessTableInstantiationFunction:
11581131
case Kind::AssociatedTypeWitnessTableAccessFunction:
11591132
case Kind::ReflectionAssociatedTypeDescriptor:
11601133
case Kind::ProtocolWitnessTableLazyCacheVariable:
11611134
case Kind::ProtocolWitnessTableLazyAccessFunction:
1162-
sf = getSourceFileForDeclContext(
1163-
getProtocolConformance()->getRootConformance()->getDeclContext());
1164-
if (!sf)
1165-
return nullptr;
1166-
break;
1135+
return getRootProtocolConformance()->getDeclContext();
11671136

11681137
case Kind::TypeMetadata: {
11691138
auto ty = getType();
11701139
// Only fully concrete nominal type metadata gets emitted eagerly.
11711140
auto nom = ty->getAnyNominal();
1172-
if (!nom || nom->isGenericContext())
1173-
return nullptr;
1141+
if (nom)
1142+
return nom->getDeclContext();
11741143

1175-
sf = getSourceFileForDeclContext(nom);
1176-
if (!sf)
1177-
return nullptr;
1178-
break;
1144+
return nullptr;
11791145
}
11801146

11811147
// Always shared linkage
@@ -1197,6 +1163,4 @@ const SourceFile *LinkEntity::getSourceFileForEmission() const {
11971163
case Kind::DifferentiabilityWitness:
11981164
return nullptr;
11991165
}
1200-
1201-
return sf;
12021166
}

0 commit comments

Comments
 (0)