Skip to content

Commit bfb6c82

Browse files
authored
Merge pull request #85560 from slavapestov/fix-ridiculous-irgen-thing
IRGen: Remove unsafe usage of static variable
2 parents 7d6acae + ca0f310 commit bfb6c82

File tree

5 files changed

+33
-30
lines changed

5 files changed

+33
-30
lines changed

include/swift/IRGen/Linking.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,10 @@ class LinkEntity {
465465
// These are both type kinds and protocol-conformance kinds.
466466
// TYPE KINDS: BEGIN {{
467467

468+
/// A SIL differentiability witness. The pointer is a
469+
/// SILDifferentiabilityWitness*.
470+
DifferentiabilityWitness,
471+
468472
/// A lazy protocol witness accessor function. The pointer is a
469473
/// canonical TypeBase*, and the secondary pointer is a
470474
/// ProtocolConformance*.
@@ -475,10 +479,6 @@ class LinkEntity {
475479
/// ProtocolConformance*.
476480
ProtocolWitnessTableLazyCacheVariable,
477481

478-
/// A SIL differentiability witness. The pointer is a
479-
/// SILDifferentiabilityWitness*.
480-
DifferentiabilityWitness,
481-
482482
// Everything following this is a type kind.
483483

484484
/// A value witness for a type.

lib/IRGen/GenDecl.cpp

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

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

40594058
// Adjust the linkage for the well-known VWTs that are strongly defined
40604059
// in the runtime.
40614060
//
4062-
// We special case the "AnyFunctionType" here as this type is referened
4061+
// We special case the "AnyFunctionType" here as this type is referenced
40634062
// inside the standard library with the definition being in the runtime
40644063
// preventing the normal detection from identifying that this is module
40654064
// local.
40664065
//
40674066
// If we are statically linking the standard library, we need to internalise
40684067
// the symbols.
4069-
if (getSwiftModule()->isStdlibModule() ||
4070-
(Context.getStdlibModule() &&
4071-
Context.getStdlibModule()->isStaticLibrary()))
4072-
if (entity.isTypeKind() &&
4073-
(IsWellKnownBuiltinOrStructralType(entity.getType()) ||
4074-
entity.getType() == kAnyFunctionType))
4075-
if (auto *GV = dyn_cast<llvm::GlobalValue>(var))
4076-
if (GV->hasDLLImportStorageClass())
4077-
ApplyIRLinkage({llvm::GlobalValue::ExternalLinkage,
4078-
llvm::GlobalValue::DefaultVisibility,
4079-
llvm::GlobalValue::DefaultStorageClass})
4080-
.to(GV);
4068+
if (auto *GV = dyn_cast<llvm::GlobalValue>(var)) {
4069+
if (GV->hasDLLImportStorageClass()) {
4070+
if (getSwiftModule()->isStdlibModule() ||
4071+
(Context.getStdlibModule() &&
4072+
Context.getStdlibModule()->isStaticLibrary())) {
4073+
if (entity.isTypeKind() &&
4074+
(isWellKnownBuiltinOrStructuralType(entity.getType()) ||
4075+
isZeroParamFunctionType(entity.getType()))) {
4076+
ApplyIRLinkage({llvm::GlobalValue::ExternalLinkage,
4077+
llvm::GlobalValue::DefaultVisibility,
4078+
llvm::GlobalValue::DefaultStorageClass})
4079+
.to(GV);
4080+
}
4081+
}
4082+
}
4083+
}
40814084

40824085
// Install the concrete definition if we have one.
40834086
if (definition && definition.hasInit()) {

lib/IRGen/IRGenModule.cpp

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

1419-
bool IRGenModule::IsWellKnownBuiltinOrStructralType(CanType T) const {
1419+
bool IRGenModule::isWellKnownBuiltinOrStructuralType(CanType T) const {
14201420
if (T == Context.TheEmptyTupleType || T == Context.TheNativeObjectType ||
14211421
T == Context.TheBridgeObjectType || T == Context.TheRawPointerType ||
14221422
T == Context.getAnyObjectType())
14231423
return true;
14241424

1425-
if (auto IntTy = dyn_cast_or_null<BuiltinIntegerType>(T)) {
1425+
if (auto IntTy = dyn_cast<BuiltinIntegerType>(T)) {
14261426
auto Width = IntTy->getWidth();
14271427
if (Width.isPointerWidth())
14281428
return true;

lib/IRGen/IRGenModule.h

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

12041204
ClassMetadataStrategy getClassMetadataStrategy(const ClassDecl *theClass);
12051205

1206-
bool IsWellKnownBuiltinOrStructralType(CanType type) const;
1206+
bool isWellKnownBuiltinOrStructuralType(CanType type) const;
12071207

12081208
private:
12091209
TypeConverter &Types;

lib/IRGen/MetadataRequest.cpp

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

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

0 commit comments

Comments
 (0)