Skip to content

Commit df353ff

Browse files
committed
Revert "IRGen: generate static arrays in read-only data sections."
This reverts commit aca0d83. I need to fix a problem before this can actually land.
1 parent 22f90f6 commit df353ff

File tree

10 files changed

+35
-198
lines changed

10 files changed

+35
-198
lines changed

include/swift/AST/ASTContext.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -880,10 +880,6 @@ class ASTContext final {
880880
/// for extended existential types.
881881
AvailabilityContext getParameterizedExistentialRuntimeAvailability();
882882

883-
/// Get the runtime availability of immortal ref-count symbols, which are
884-
/// needed to place array buffers into constant data sections.
885-
AvailabilityContext getImmortalRefCountSymbolsAvailability();
886-
887883
/// Get the runtime availability of features introduced in the Swift 5.2
888884
/// compiler for the target platform.
889885
AvailabilityContext getSwift52Availability();

include/swift/IRGen/Linking.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -364,9 +364,6 @@ class LinkEntity {
364364
/// A SIL global variable. The pointer is a SILGlobalVariable*.
365365
SILGlobalVariable,
366366

367-
/// An outlined read-only global object. The pointer is a SILGlobalVariable*.
368-
ReadOnlyGlobalObject,
369-
370367
// These next few are protocol-conformance kinds.
371368

372369
/// A direct protocol witness table. The secondary pointer is a
@@ -996,7 +993,13 @@ class LinkEntity {
996993
return entity;
997994
}
998995

999-
static LinkEntity forSILGlobalVariable(SILGlobalVariable *G, IRGenModule &IGM);
996+
static LinkEntity forSILGlobalVariable(SILGlobalVariable *G) {
997+
LinkEntity entity;
998+
entity.Pointer = G;
999+
entity.SecondaryPointer = nullptr;
1000+
entity.Data = LINKENTITY_SET_FIELD(Kind, unsigned(Kind::SILGlobalVariable));
1001+
return entity;
1002+
}
10001003

10011004
static LinkEntity
10021005
forDifferentiabilityWitness(const SILDifferentiabilityWitness *witness) {
@@ -1417,8 +1420,7 @@ class LinkEntity {
14171420
}
14181421

14191422
SILGlobalVariable *getSILGlobalVariable() const {
1420-
assert(getKind() == Kind::SILGlobalVariable ||
1421-
getKind() == Kind::ReadOnlyGlobalObject);
1423+
assert(getKind() == Kind::SILGlobalVariable);
14221424
return reinterpret_cast<SILGlobalVariable*>(Pointer);
14231425
}
14241426

lib/AST/Availability.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -399,13 +399,6 @@ ASTContext::getParameterizedExistentialRuntimeAvailability() {
399399
return getSwift57Availability();
400400
}
401401

402-
AvailabilityContext
403-
ASTContext::getImmortalRefCountSymbolsAvailability() {
404-
// TODO: replace this with a concrete swift version once we have it.
405-
// rdar://94185998
406-
return getSwiftFutureAvailability();
407-
}
408-
409402
AvailabilityContext ASTContext::getSwift52Availability() {
410403
auto target = LangOpts.Target;
411404

lib/IRGen/GenConstant.cpp

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -278,27 +278,9 @@ llvm::Constant *irgen::emitConstantObject(IRGenModule &IGM, ObjectInst *OI,
278278
}
279279
}
280280
// Construct the object header.
281-
llvm::StructType *ObjectHeaderTy = cast<llvm::StructType>(sTy->getElementType(0));
282-
283-
if (IGM.canMakeStaticObjectsReadOnly()) {
284-
if (!IGM.swiftImmortalRefCount) {
285-
auto *var = new llvm::GlobalVariable(IGM.Module, IGM.Int8Ty,
286-
/*constant*/ true, llvm::GlobalValue::ExternalLinkage,
287-
/*initializer*/ nullptr, "_swiftImmortalRefCount");
288-
IGM.swiftImmortalRefCount = var;
289-
}
290-
if (!IGM.swiftStaticArrayMetadata) {
291-
auto *var = new llvm::GlobalVariable(IGM.Module, IGM.TypeMetadataStructTy,
292-
/*constant*/ true, llvm::GlobalValue::ExternalLinkage,
293-
/*initializer*/ nullptr, "_swiftStaticArrayMetadata");
294-
IGM.swiftStaticArrayMetadata = var;
295-
}
296-
elts[0] = llvm::ConstantStruct::get(ObjectHeaderTy, {
297-
IGM.swiftStaticArrayMetadata,
298-
llvm::ConstantExpr::getPtrToInt(IGM.swiftImmortalRefCount, IGM.IntPtrTy)});
299-
} else {
300-
elts[0] = llvm::Constant::getNullValue(ObjectHeaderTy);
301-
}
281+
llvm::Type *ObjectHeaderTy = sTy->getElementType(0);
282+
assert(ObjectHeaderTy->isStructTy());
283+
elts[0] = llvm::Constant::getNullValue(ObjectHeaderTy);
302284
insertPadding(elts, sTy);
303285
return llvm::ConstantStruct::get(sTy, elts);
304286
}

lib/IRGen/GenDecl.cpp

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2482,7 +2482,7 @@ Address IRGenModule::getAddrOfSILGlobalVariable(SILGlobalVariable *var,
24822482
return Address(addr, alignment);
24832483
}
24842484

2485-
LinkEntity entity = LinkEntity::forSILGlobalVariable(var, *this);
2485+
LinkEntity entity = LinkEntity::forSILGlobalVariable(var);
24862486
ResilienceExpansion expansion = getResilienceExpansionForLayout(var);
24872487

24882488
llvm::Type *storageType;
@@ -2528,33 +2528,28 @@ Address IRGenModule::getAddrOfSILGlobalVariable(SILGlobalVariable *var,
25282528

25292529
// Check whether we've created the global variable already.
25302530
// FIXME: We should integrate this into the LinkEntity cache more cleanly.
2531-
LinkInfo link = LinkInfo::get(*this, entity, forDefinition);
2532-
auto gvar = Module.getGlobalVariable(link.getName(), /*allowInternal*/ true);
2531+
auto gvar = Module.getGlobalVariable(var->getName(), /*allowInternal*/ true);
25332532
if (gvar) {
25342533
if (forDefinition)
25352534
updateLinkageForDefinition(*this, gvar, entity);
25362535
} else {
2536+
LinkInfo link = LinkInfo::get(*this, entity, forDefinition);
25372537
llvm::Type *storageTypeWithContainer = storageType;
25382538
if (var->isInitializedObject()) {
2539-
if (canMakeStaticObjectsReadOnly()) {
2540-
gvar = createVariable(*this, link, storageType, fixedAlignment);
2541-
gvar->setConstant(true);
2542-
} else {
2543-
// A statically initialized object must be placed into a container struct
2544-
// because the swift_initStaticObject needs a swift_once_t at offset -1:
2545-
// struct Container {
2546-
// swift_once_t token[fixedAlignment / sizeof(swift_once_t)];
2547-
// HeapObject object;
2548-
// };
2549-
std::string typeName = storageType->getStructName().str() + 'c';
2550-
assert(fixedAlignment >= getPointerAlignment());
2551-
unsigned numTokens = fixedAlignment.getValue() /
2552-
getPointerAlignment().getValue();
2553-
storageTypeWithContainer = llvm::StructType::create(getLLVMContext(),
2554-
{llvm::ArrayType::get(OnceTy, numTokens), storageType}, typeName);
2555-
gvar = createVariable(*this, link, storageTypeWithContainer,
2556-
fixedAlignment);
2557-
}
2539+
// A statically initialized object must be placed into a container struct
2540+
// because the swift_initStaticObject needs a swift_once_t at offset -1:
2541+
// struct Container {
2542+
// swift_once_t token[fixedAlignment / sizeof(swift_once_t)];
2543+
// HeapObject object;
2544+
// };
2545+
std::string typeName = storageType->getStructName().str() + 'c';
2546+
assert(fixedAlignment >= getPointerAlignment());
2547+
unsigned numTokens = fixedAlignment.getValue() /
2548+
getPointerAlignment().getValue();
2549+
storageTypeWithContainer = llvm::StructType::create(getLLVMContext(),
2550+
{llvm::ArrayType::get(OnceTy, numTokens), storageType}, typeName);
2551+
gvar = createVariable(*this, link, storageTypeWithContainer,
2552+
fixedAlignment);
25582553
} else {
25592554
StringRef name;
25602555
Optional<SILLocation> loc;
@@ -2579,7 +2574,7 @@ Address IRGenModule::getAddrOfSILGlobalVariable(SILGlobalVariable *var,
25792574
gvar->setComdat(nullptr);
25802575
}
25812576
llvm::Constant *addr = gvar;
2582-
if (var->isInitializedObject() && !canMakeStaticObjectsReadOnly()) {
2577+
if (var->isInitializedObject()) {
25832578
// Project out the object from the container.
25842579
llvm::Constant *Indices[2] = {
25852580
llvm::ConstantExpr::getIntegerValue(Int32Ty, APInt(32, 0)),

lib/IRGen/IRGenModule.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1785,16 +1785,6 @@ bool IRGenModule::shouldPrespecializeGenericMetadata() {
17851785
canPrespecializeTarget;
17861786
}
17871787

1788-
bool IRGenModule::canMakeStaticObjectsReadOnly() {
1789-
// TODO: Support constant static arrays on other platforms, too.
1790-
// See also the comment in GlobalObjects.cpp.
1791-
if (!Triple.isOSDarwin())
1792-
return false;
1793-
1794-
return getAvailabilityContext().isContainedIn(
1795-
Context.getImmortalRefCountSymbolsAvailability());
1796-
}
1797-
17981788
void IRGenerator::addGenModule(SourceFile *SF, IRGenModule *IGM) {
17991789
assert(GenModules.count(SF) == 0);
18001790
GenModules[SF] = IGM;

lib/IRGen/IRGenModule.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -763,9 +763,6 @@ class IRGenModule {
763763

764764
llvm::GlobalVariable *TheTrivialPropertyDescriptor = nullptr;
765765

766-
llvm::GlobalVariable *swiftImmortalRefCount = nullptr;
767-
llvm::GlobalVariable *swiftStaticArrayMetadata = nullptr;
768-
769766
/// Used to create unique names for class layout types with tail allocated
770767
/// elements.
771768
unsigned TailElemTypeID = 0;
@@ -889,8 +886,6 @@ class IRGenModule {
889886

890887
bool shouldPrespecializeGenericMetadata();
891888

892-
bool canMakeStaticObjectsReadOnly();
893-
894889
Size getAtomicBoolSize() const { return AtomicBoolSize; }
895890
Alignment getAtomicBoolAlignment() const { return AtomicBoolAlign; }
896891

lib/IRGen/IRGenSIL.cpp

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2765,7 +2765,7 @@ void IRGenSILFunction::visitGlobalValueInst(GlobalValueInst *i) {
27652765
NotForDefinition).getAddress();
27662766
// We don't need to initialize the global object if it's never used for
27672767
// something which can access the object header.
2768-
if (!hasOnlyProjections(i) && !IGM.canMakeStaticObjectsReadOnly()) {
2768+
if (!hasOnlyProjections(i)) {
27692769
auto ClassType = loweredTy.getASTType();
27702770
llvm::Value *Metadata =
27712771
emitClassHeapMetadataRef(*this, ClassType, MetadataValueType::TypeMetadata,
@@ -7099,11 +7099,8 @@ void IRGenModule::emitSILStaticInitializers() {
70997099
"cannot emit a static initializer for dynamically-sized global");
71007100
#endif
71017101

7102-
LinkEntity entity = LinkEntity::forSILGlobalVariable(&Global, *this);
7103-
std::string name = entity.mangleAsString();
7104-
71057102
auto *IRGlobal =
7106-
Module.getGlobalVariable(name, true /* = AllowLocal */);
7103+
Module.getGlobalVariable(Global.getName(), true /* = AllowLocal */);
71077104

71087105
// A check for multi-threaded compilation: Is this the llvm module where the
71097106
// global is defined and not only referenced (or not referenced at all).
@@ -7113,14 +7110,10 @@ void IRGenModule::emitSILStaticInitializers() {
71137110
if (auto *OI = dyn_cast<ObjectInst>(InitValue)) {
71147111
StructLayout *Layout = StaticObjectLayouts[&Global].get();
71157112
llvm::Constant *InitVal = emitConstantObject(*this, OI, Layout);
7116-
if (canMakeStaticObjectsReadOnly()) {
7117-
IRGlobal->setInitializer(InitVal);
7118-
} else {
7119-
auto *ContainerTy = cast<llvm::StructType>(IRGlobal->getValueType());
7120-
auto *zero = llvm::ConstantAggregateZero::get(ContainerTy->getElementType(0));
7121-
IRGlobal->setInitializer(llvm::ConstantStruct::get(ContainerTy,
7122-
{zero , InitVal}));
7123-
}
7113+
auto *ContainerTy = cast<llvm::StructType>(IRGlobal->getValueType());
7114+
auto *zero = llvm::ConstantAggregateZero::get(ContainerTy->getElementType(0));
7115+
IRGlobal->setInitializer(llvm::ConstantStruct::get(ContainerTy,
7116+
{zero , InitVal}));
71247117
continue;
71257118
}
71267119

lib/IRGen/Linking.cpp

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,6 @@ UniversalLinkageInfo::UniversalLinkageInfo(const llvm::Triple &triple,
9191
UseDLLStorage(useDllStorage(triple)), Internalize(isStaticLibrary),
9292
HasMultipleIGMs(hasMultipleIGMs), ForcePublicDecls(forcePublicDecls) {}
9393

94-
LinkEntity LinkEntity::forSILGlobalVariable(SILGlobalVariable *G,
95-
IRGenModule &IGM) {
96-
LinkEntity entity;
97-
entity.Pointer = G;
98-
entity.SecondaryPointer = nullptr;
99-
auto kind = (G->isInitializedObject() && IGM.canMakeStaticObjectsReadOnly() ?
100-
Kind::ReadOnlyGlobalObject : Kind::SILGlobalVariable);
101-
entity.Data = unsigned(kind) << KindShift;
102-
return entity;
103-
}
104-
105-
10694
/// Mangle this entity into the given buffer.
10795
void LinkEntity::mangle(SmallVectorImpl<char> &buffer) const {
10896
llvm::raw_svector_ostream stream(buffer);
@@ -466,9 +454,6 @@ std::string LinkEntity::mangleAsString() const {
466454
case Kind::SILGlobalVariable:
467455
return getSILGlobalVariable()->getName().str();
468456

469-
case Kind::ReadOnlyGlobalObject:
470-
return getSILGlobalVariable()->getName().str() + "r";
471-
472457
case Kind::ReflectionBuiltinDescriptor:
473458
return mangler.mangleReflectionBuiltinDescriptor(getType());
474459
case Kind::ReflectionFieldDescriptor:
@@ -802,7 +787,6 @@ SILLinkage LinkEntity::getLinkage(ForDefinition_t forDefinition) const {
802787
return getSILLinkage(getDeclLinkage(getDecl()), forDefinition);
803788

804789
case Kind::SILGlobalVariable:
805-
case Kind::ReadOnlyGlobalObject:
806790
return getSILGlobalVariable()->getLinkage();
807791

808792
case Kind::ReflectionBuiltinDescriptor:
@@ -898,7 +882,6 @@ bool LinkEntity::isContextDescriptor() const {
898882
case Kind::DefaultAssociatedConformanceAccessor:
899883
case Kind::SILFunction:
900884
case Kind::SILGlobalVariable:
901-
case Kind::ReadOnlyGlobalObject:
902885
case Kind::ProtocolWitnessTable:
903886
case Kind::ProtocolWitnessTablePattern:
904887
case Kind::GenericProtocolWitnessTableInstantiationFunction:
@@ -1146,7 +1129,6 @@ Alignment LinkEntity::getAlignment(IRGenModule &IGM) const {
11461129
bool LinkEntity::isWeakImported(ModuleDecl *module) const {
11471130
switch (getKind()) {
11481131
case Kind::SILGlobalVariable:
1149-
case Kind::ReadOnlyGlobalObject:
11501132
if (getSILGlobalVariable()->getDecl()) {
11511133
return getSILGlobalVariable()->getDecl()->isWeakImported(module);
11521134
}
@@ -1335,7 +1317,6 @@ DeclContext *LinkEntity::getDeclContextForEmission() const {
13351317
return getSILFunction()->getDeclContext();
13361318

13371319
case Kind::SILGlobalVariable:
1338-
case Kind::ReadOnlyGlobalObject:
13391320
if (auto decl = getSILGlobalVariable()->getDecl())
13401321
return decl->getDeclContext();
13411322

test/SILOptimizer/readonly_arrays.swift

Lines changed: 0 additions & 90 deletions
This file was deleted.

0 commit comments

Comments
 (0)