Skip to content

Commit 2993e3b

Browse files
authored
Merge pull request #70712 from eeckstein/fix-static-array-storage
IRGen: get metadata symbol of class __StaticArrayStorage the correct way
2 parents 2622fa7 + ded1b6e commit 2993e3b

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

lib/IRGen/GenConstant.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include "StructLayout.h"
2828
#include "Callee.h"
2929
#include "ConstantBuilder.h"
30+
#include "DebugTypeInfo.h"
31+
#include "swift/IRGen/Linking.h"
3032
#include "swift/Basic/Range.h"
3133
#include "swift/SIL/SILModule.h"
3234
#include "llvm/Support/BLAKE3.h"
@@ -438,11 +440,12 @@ llvm::Constant *irgen::emitConstantObject(IRGenModule &IGM, ObjectInst *OI,
438440
IGM.swiftImmortalRefCount = var;
439441
}
440442
if (!IGM.swiftStaticArrayMetadata) {
441-
// type metadata for class __StaticArrayStorage
442-
auto *var = new llvm::GlobalVariable(IGM.Module, IGM.TypeMetadataStructTy,
443-
/*constant*/ true, llvm::GlobalValue::ExternalLinkage,
444-
/*initializer*/ nullptr, "$ss20__StaticArrayStorageCN");
445-
IGM.swiftStaticArrayMetadata = var;
443+
auto *classDecl = IGM.getStaticArrayStorageDecl();
444+
assert(classDecl && "no __StaticArrayStorage in stdlib");
445+
CanType classTy = CanType(ClassType::get(classDecl, Type(), IGM.Context));
446+
LinkEntity entity = LinkEntity::forTypeMetadata(classTy, TypeMetadataAddress::AddressPoint);
447+
auto *metatype = IGM.getAddrOfLLVMVariable(entity, NotForDefinition, DebugTypeInfo());
448+
IGM.swiftStaticArrayMetadata = cast<llvm::GlobalVariable>(metatype);
446449
}
447450
elements[0].add(llvm::ConstantStruct::get(ObjectHeaderTy, {
448451
IGM.swiftStaticArrayMetadata,

lib/IRGen/IRGenModule.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2032,8 +2032,23 @@ bool IRGenModule::canMakeStaticObjectsReadOnly() {
20322032
if (!Triple.isOSDarwin())
20332033
return false;
20342034

2035-
return getAvailabilityContext().isContainedIn(
2036-
Context.getStaticReadOnlyArraysAvailability());
2035+
if (!getAvailabilityContext().isContainedIn(Context.getStaticReadOnlyArraysAvailability()))
2036+
return false;
2037+
2038+
if (!getStaticArrayStorageDecl())
2039+
return false;
2040+
2041+
return true;
2042+
}
2043+
2044+
ClassDecl *IRGenModule::getStaticArrayStorageDecl() {
2045+
SmallVector<ValueDecl *, 1> results;
2046+
Context.lookupInSwiftModule("__StaticArrayStorage", results);
2047+
2048+
if (results.size() != 1)
2049+
return nullptr;
2050+
2051+
return dyn_cast<ClassDecl>(results[0]);
20372052
}
20382053

20392054
void IRGenerator::addGenModule(SourceFile *SF, IRGenModule *IGM) {

lib/IRGen/IRGenModule.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,8 @@ class IRGenModule {
927927

928928
bool canMakeStaticObjectsReadOnly();
929929

930+
ClassDecl *getStaticArrayStorageDecl();
931+
930932
bool canUseObjCSymbolicReferences();
931933

932934
Size getAtomicBoolSize() const { return AtomicBoolSize; }

0 commit comments

Comments
 (0)