Skip to content

Commit ca0f310

Browse files
committed
IRGen: Remove unsafe usage of static variable
We cannot use 'static' linkage for something that points into the ASTContext, because there might be more than one ASTContext in a single process. Also, fix the spelling mistake in a related function name.
1 parent 6c1a2df commit ca0f310

File tree

4 files changed

+29
-26
lines changed

4 files changed

+29
-26
lines changed

lib/IRGen/GenDecl.cpp

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4023,38 +4023,41 @@ IRGenModule::getAddrOfLLVMVariable(LinkEntity entity,
40234023
auto var = createVariable(*this, link, definitionType,
40244024
entity.getAlignment(*this), DbgTy);
40254025

4026-
// @escaping () -> ()
4027-
// NOTE: we explicitly desugar the `Void` type for the return as the test
4028-
// suite makes assumptions that it can emit the value witness table without a
4029-
// standard library for the target. `Context.getVoidType()` will attempt to
4030-
// lookup the `Decl` before returning the canonical type. To workaround this
4031-
// dependency, we simply desugar the `Void` return type to `()`.
4032-
static CanType kAnyFunctionType =
4033-
FunctionType::get({}, Context.TheEmptyTupleType,
4034-
ASTExtInfo{})->getCanonicalType();
4026+
auto isZeroParamFunctionType = [this](Type t) -> bool {
4027+
if (auto *funcTy = t->getAs<FunctionType>()) {
4028+
return (funcTy->getParams().size() == 0 &&
4029+
funcTy->getResult()->isEqual(Context.TheEmptyTupleType));
4030+
}
4031+
4032+
return false;
4033+
};
40354034

40364035
// Adjust the linkage for the well-known VWTs that are strongly defined
40374036
// in the runtime.
40384037
//
4039-
// We special case the "AnyFunctionType" here as this type is referened
4038+
// We special case the "AnyFunctionType" here as this type is referenced
40404039
// inside the standard library with the definition being in the runtime
40414040
// preventing the normal detection from identifying that this is module
40424041
// local.
40434042
//
40444043
// If we are statically linking the standard library, we need to internalise
40454044
// the symbols.
4046-
if (getSwiftModule()->isStdlibModule() ||
4047-
(Context.getStdlibModule() &&
4048-
Context.getStdlibModule()->isStaticLibrary()))
4049-
if (entity.isTypeKind() &&
4050-
(IsWellKnownBuiltinOrStructralType(entity.getType()) ||
4051-
entity.getType() == kAnyFunctionType))
4052-
if (auto *GV = dyn_cast<llvm::GlobalValue>(var))
4053-
if (GV->hasDLLImportStorageClass())
4054-
ApplyIRLinkage({llvm::GlobalValue::ExternalLinkage,
4055-
llvm::GlobalValue::DefaultVisibility,
4056-
llvm::GlobalValue::DefaultStorageClass})
4057-
.to(GV);
4045+
if (auto *GV = dyn_cast<llvm::GlobalValue>(var)) {
4046+
if (GV->hasDLLImportStorageClass()) {
4047+
if (getSwiftModule()->isStdlibModule() ||
4048+
(Context.getStdlibModule() &&
4049+
Context.getStdlibModule()->isStaticLibrary())) {
4050+
if (entity.isTypeKind() &&
4051+
(isWellKnownBuiltinOrStructuralType(entity.getType()) ||
4052+
isZeroParamFunctionType(entity.getType()))) {
4053+
ApplyIRLinkage({llvm::GlobalValue::ExternalLinkage,
4054+
llvm::GlobalValue::DefaultVisibility,
4055+
llvm::GlobalValue::DefaultStorageClass})
4056+
.to(GV);
4057+
}
4058+
}
4059+
}
4060+
}
40584061

40594062
// Install the concrete definition if we have one.
40604063
if (definition && definition.hasInit()) {

lib/IRGen/IRGenModule.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,13 +1411,13 @@ llvm::Module *IRGenModule::getModule() const {
14111411
return ClangCodeGen->GetModule();
14121412
}
14131413

1414-
bool IRGenModule::IsWellKnownBuiltinOrStructralType(CanType T) const {
1414+
bool IRGenModule::isWellKnownBuiltinOrStructuralType(CanType T) const {
14151415
if (T == Context.TheEmptyTupleType || T == Context.TheNativeObjectType ||
14161416
T == Context.TheBridgeObjectType || T == Context.TheRawPointerType ||
14171417
T == Context.getAnyObjectType())
14181418
return true;
14191419

1420-
if (auto IntTy = dyn_cast_or_null<BuiltinIntegerType>(T)) {
1420+
if (auto IntTy = dyn_cast<BuiltinIntegerType>(T)) {
14211421
auto Width = IntTy->getWidth();
14221422
if (Width.isPointerWidth())
14231423
return true;

lib/IRGen/IRGenModule.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1196,7 +1196,7 @@ class IRGenModule {
11961196

11971197
ClassMetadataStrategy getClassMetadataStrategy(const ClassDecl *theClass);
11981198

1199-
bool IsWellKnownBuiltinOrStructralType(CanType type) const;
1199+
bool isWellKnownBuiltinOrStructuralType(CanType type) const;
12001200

12011201
private:
12021202
TypeConverter &Types;

lib/IRGen/MetadataRequest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3664,7 +3664,7 @@ namespace {
36643664
//
36653665
// TODO: If a nominal type is in the same source file as we're currently
36663666
// emitting, we would be able to see its value witness table.
3667-
if (IGF.IGM.IsWellKnownBuiltinOrStructralType(t))
3667+
if (IGF.IGM.isWellKnownBuiltinOrStructuralType(t))
36683668
return emitFromValueWitnessTable(t);
36693669

36703670
// If the type is a singleton aggregate, the field's layout is equivalent

0 commit comments

Comments
 (0)