Skip to content

IRGen: get metadata symbol of class __StaticArrayStorage the correct way #70712

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
Jan 5, 2024
Merged
Show file tree
Hide file tree
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
13 changes: 8 additions & 5 deletions lib/IRGen/GenConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include "StructLayout.h"
#include "Callee.h"
#include "ConstantBuilder.h"
#include "DebugTypeInfo.h"
#include "swift/IRGen/Linking.h"
#include "swift/Basic/Range.h"
#include "swift/SIL/SILModule.h"
#include "llvm/Support/BLAKE3.h"
Expand Down Expand Up @@ -438,11 +440,12 @@ llvm::Constant *irgen::emitConstantObject(IRGenModule &IGM, ObjectInst *OI,
IGM.swiftImmortalRefCount = var;
}
if (!IGM.swiftStaticArrayMetadata) {
// type metadata for class __StaticArrayStorage
auto *var = new llvm::GlobalVariable(IGM.Module, IGM.TypeMetadataStructTy,
/*constant*/ true, llvm::GlobalValue::ExternalLinkage,
/*initializer*/ nullptr, "$ss20__StaticArrayStorageCN");
IGM.swiftStaticArrayMetadata = var;
auto *classDecl = IGM.getStaticArrayStorageDecl();
assert(classDecl && "no __StaticArrayStorage in stdlib");
CanType classTy = CanType(ClassType::get(classDecl, Type(), IGM.Context));
LinkEntity entity = LinkEntity::forTypeMetadata(classTy, TypeMetadataAddress::AddressPoint);
auto *metatype = IGM.getAddrOfLLVMVariable(entity, NotForDefinition, DebugTypeInfo());
IGM.swiftStaticArrayMetadata = cast<llvm::GlobalVariable>(metatype);
}
elements[0].add(llvm::ConstantStruct::get(ObjectHeaderTy, {
IGM.swiftStaticArrayMetadata,
Expand Down
19 changes: 17 additions & 2 deletions lib/IRGen/IRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2046,8 +2046,23 @@ bool IRGenModule::canMakeStaticObjectsReadOnly() {
if (!Triple.isOSDarwin())
return false;

return getAvailabilityContext().isContainedIn(
Context.getStaticReadOnlyArraysAvailability());
if (!getAvailabilityContext().isContainedIn(Context.getStaticReadOnlyArraysAvailability()))
return false;

if (!getStaticArrayStorageDecl())
return false;

return true;
}

ClassDecl *IRGenModule::getStaticArrayStorageDecl() {
SmallVector<ValueDecl *, 1> results;
Context.lookupInSwiftModule("__StaticArrayStorage", results);

if (results.size() != 1)
return nullptr;

return dyn_cast<ClassDecl>(results[0]);
}

void IRGenerator::addGenModule(SourceFile *SF, IRGenModule *IGM) {
Expand Down
2 changes: 2 additions & 0 deletions lib/IRGen/IRGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,8 @@ class IRGenModule {

bool canMakeStaticObjectsReadOnly();

ClassDecl *getStaticArrayStorageDecl();

bool canUseObjCSymbolicReferences();

Size getAtomicBoolSize() const { return AtomicBoolSize; }
Expand Down