Skip to content

Commit b8804cd

Browse files
authored
Merge pull request #59431 from eeckstein/read-only-static-arrays
IRGen: generate static arrays in read-only data sections.
2 parents afa599c + aca0d83 commit b8804cd

File tree

20 files changed

+284
-38
lines changed

20 files changed

+284
-38
lines changed

docs/ABI/Mangling.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ Entities
349349
entity-spec ::= 'fE' // ivar destroyer; untyped
350350
entity-spec ::= 'fe' // ivar initializer; untyped
351351
entity-spec ::= 'Tv' NATURAL // outlined global variable (from context function)
352+
entity-spec ::= 'Tv' NATURAL 'r' // outlined global read-only object
352353
entity-spec ::= 'Te' bridge-spec // outlined objective c method call
353354

354355
entity-spec ::= decl-name label-list function-signature generic-signature? 'F' // function

include/swift/AST/ASTContext.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,10 @@ 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+
883887
/// Get the runtime availability of features introduced in the Swift 5.2
884888
/// compiler for the target platform.
885889
AvailabilityContext getSwift52Availability();

include/swift/Demangling/DemangleNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ NODE(OutlinedAssignWithTake)
279279
NODE(OutlinedAssignWithCopy)
280280
NODE(OutlinedDestroy)
281281
NODE(OutlinedVariable)
282+
NODE(OutlinedReadOnlyObject)
282283
NODE(AssocTypePath)
283284
NODE(LabelList)
284285
NODE(ModuleDescriptor)

include/swift/IRGen/Linking.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,9 @@ 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+
367370
// These next few are protocol-conformance kinds.
368371

369372
/// A direct protocol witness table. The secondary pointer is a
@@ -993,13 +996,7 @@ class LinkEntity {
993996
return entity;
994997
}
995998

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-
}
999+
static LinkEntity forSILGlobalVariable(SILGlobalVariable *G, IRGenModule &IGM);
10031000

10041001
static LinkEntity
10051002
forDifferentiabilityWitness(const SILDifferentiabilityWitness *witness) {
@@ -1420,7 +1417,8 @@ class LinkEntity {
14201417
}
14211418

14221419
SILGlobalVariable *getSILGlobalVariable() const {
1423-
assert(getKind() == Kind::SILGlobalVariable);
1420+
assert(getKind() == Kind::SILGlobalVariable ||
1421+
getKind() == Kind::ReadOnlyGlobalObject);
14241422
return reinterpret_cast<SILGlobalVariable*>(Pointer);
14251423
}
14261424

lib/AST/Availability.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,13 @@ 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+
402409
AvailabilityContext ASTContext::getSwift52Availability() {
403410
auto target = LangOpts.Target;
404411

lib/Demangling/Demangler.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ bool swift::Demangle::isFunctionAttr(Node::Kind kind) {
131131
case Node::Kind::PartialApplyForwarder:
132132
case Node::Kind::PartialApplyObjCForwarder:
133133
case Node::Kind::OutlinedVariable:
134+
case Node::Kind::OutlinedReadOnlyObject:
134135
case Node::Kind::OutlinedBridgedMethod:
135136
case Node::Kind::MergedFunction:
136137
case Node::Kind::DistributedThunk:
@@ -2615,6 +2616,8 @@ NodePointer Demangler::demangleThunkOrSpecialization() {
26152616
int Idx = demangleIndex();
26162617
if (Idx < 0)
26172618
return nullptr;
2619+
if (nextChar() == 'r')
2620+
return createNode(Node::Kind::OutlinedReadOnlyObject, Idx);
26182621
return createNode(Node::Kind::OutlinedVariable, Idx);
26192622
}
26202623
case 'e': {

lib/Demangling/NodePrinter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@ class NodePrinter {
547547
case Node::Kind::OutlinedAssignWithCopy:
548548
case Node::Kind::OutlinedDestroy:
549549
case Node::Kind::OutlinedVariable:
550+
case Node::Kind::OutlinedReadOnlyObject:
550551
case Node::Kind::AssocTypePath:
551552
case Node::Kind::ModuleDescriptor:
552553
case Node::Kind::AnonymousDescriptor:
@@ -1265,6 +1266,9 @@ NodePointer NodePrinter::print(NodePointer Node, unsigned depth,
12651266
case Node::Kind::OutlinedVariable:
12661267
Printer << "outlined variable #" << Node->getIndex() << " of ";
12671268
return nullptr;
1269+
case Node::Kind::OutlinedReadOnlyObject:
1270+
Printer << "outlined read-only object #" << Node->getIndex() << " of ";
1271+
return nullptr;
12681272
case Node::Kind::Directness:
12691273
Printer << toString(Directness(Node->getIndex())) << " ";
12701274
return nullptr;

lib/Demangling/OldRemangler.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2467,6 +2467,11 @@ ManglingError Remangler::mangleOutlinedVariable(Node *node, unsigned depth) {
24672467
return mangleSingleChildNode(node, depth + 1);
24682468
}
24692469

2470+
ManglingError Remangler::mangleOutlinedReadOnlyObject(Node *node, unsigned depth) {
2471+
Buffer << "Tv" << node->getIndex() << 'r';
2472+
return mangleSingleChildNode(node, depth + 1);
2473+
}
2474+
24702475
ManglingError Remangler::mangleOutlinedBridgedMethod(Node *node,
24712476
unsigned depth) {
24722477
Buffer << "Te" << node->getText();

lib/Demangling/Remangler.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,6 +1628,7 @@ ManglingError Remangler::mangleGlobal(Node *node, unsigned depth) {
16281628
case Node::Kind::GenericPartialSpecializationNotReAbstracted:
16291629
case Node::Kind::OutlinedBridgedMethod:
16301630
case Node::Kind::OutlinedVariable:
1631+
case Node::Kind::OutlinedReadOnlyObject:
16311632
case Node::Kind::ObjCAttribute:
16321633
case Node::Kind::NonObjCAttribute:
16331634
case Node::Kind::DynamicAttribute:
@@ -3153,6 +3154,13 @@ ManglingError Remangler::mangleOutlinedVariable(Node *node, unsigned depth) {
31533154
return ManglingError::Success;
31543155
}
31553156

3157+
ManglingError Remangler::mangleOutlinedReadOnlyObject(Node *node, unsigned depth) {
3158+
Buffer << "Tv";
3159+
mangleIndex(node->getIndex());
3160+
Buffer << 'r';
3161+
return ManglingError::Success;
3162+
}
3163+
31563164
ManglingError Remangler::mangleOutlinedBridgedMethod(Node *node,
31573165
unsigned depth) {
31583166
Buffer << "Te";

lib/IRGen/GenConstant.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,27 @@ llvm::Constant *irgen::emitConstantObject(IRGenModule &IGM, ObjectInst *OI,
278278
}
279279
}
280280
// Construct the object header.
281-
llvm::Type *ObjectHeaderTy = sTy->getElementType(0);
282-
assert(ObjectHeaderTy->isStructTy());
283-
elts[0] = llvm::Constant::getNullValue(ObjectHeaderTy);
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+
}
284302
insertPadding(elts, sTy);
285303
return llvm::ConstantStruct::get(sTy, elts);
286304
}

0 commit comments

Comments
 (0)