@@ -2482,7 +2482,7 @@ Address IRGenModule::getAddrOfSILGlobalVariable(SILGlobalVariable *var,
24822482 return Address (addr, alignment);
24832483 }
24842484
2485- LinkEntity entity = LinkEntity::forSILGlobalVariable (var);
2485+ LinkEntity entity = LinkEntity::forSILGlobalVariable (var, * this );
24862486 ResilienceExpansion expansion = getResilienceExpansionForLayout (var);
24872487
24882488 llvm::Type *storageType;
@@ -2528,28 +2528,33 @@ 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- auto gvar = Module.getGlobalVariable (var->getName (), /* allowInternal*/ true );
2531+ LinkInfo link = LinkInfo::get (*this , entity, forDefinition);
2532+ auto gvar = Module.getGlobalVariable (link.getName (), /* allowInternal*/ true );
25322533 if (gvar) {
25332534 if (forDefinition)
25342535 updateLinkageForDefinition (*this , gvar, entity);
25352536 } else {
2536- LinkInfo link = LinkInfo::get (*this , entity, forDefinition);
25372537 llvm::Type *storageTypeWithContainer = storageType;
25382538 if (var->isInitializedObject ()) {
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);
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+ }
25532558 } else {
25542559 StringRef name;
25552560 Optional<SILLocation> loc;
@@ -2574,7 +2579,7 @@ Address IRGenModule::getAddrOfSILGlobalVariable(SILGlobalVariable *var,
25742579 gvar->setComdat (nullptr );
25752580 }
25762581 llvm::Constant *addr = gvar;
2577- if (var->isInitializedObject ()) {
2582+ if (var->isInitializedObject () && ! canMakeStaticObjectsReadOnly () ) {
25782583 // Project out the object from the container.
25792584 llvm::Constant *Indices[2 ] = {
25802585 llvm::ConstantExpr::getIntegerValue (Int32Ty, APInt (32 , 0 )),
0 commit comments