Skip to content

ApplyIRLinkage cleanups #21639

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 6 commits into from
Jan 4, 2019
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
24 changes: 14 additions & 10 deletions include/swift/IRGen/Linking.h
Original file line number Diff line number Diff line change
Expand Up @@ -1043,31 +1043,35 @@ struct IRLinkage {
llvm::GlobalValue::DLLStorageClassTypes DLLStorage;

static const IRLinkage InternalLinkOnceODR;
static const IRLinkage InternalWeakODR;
static const IRLinkage Internal;

static const IRLinkage ExternalImport;
static const IRLinkage ExternalExport;
};

class ApplyIRLinkage {
IRLinkage IRL;
public:
ApplyIRLinkage(IRLinkage IRL) : IRL(IRL) {}
void to(llvm::GlobalValue *GV) const {
llvm::Module *M = GV->getParent();
const llvm::Triple Triple(M->getTargetTriple());

GV->setLinkage(IRL.Linkage);
GV->setVisibility(IRL.Visibility);
GV->setDLLStorageClass(IRL.DLLStorage);

if (IRL.Linkage == llvm::GlobalValue::LinkOnceODRLinkage ||
IRL.Linkage == llvm::GlobalValue::WeakODRLinkage) {
llvm::Module *M = GV->getParent();
const llvm::Triple Triple(M->getTargetTriple());
if (Triple.isOSBinFormatCOFF() && !Triple.isOSCygMing())
GV->setDLLStorageClass(IRL.DLLStorage);

// TODO: BFD and gold do not handle COMDATs properly
if (Triple.isOSBinFormatELF())
return;
// TODO: BFD and gold do not handle COMDATs properly
if (Triple.isOSBinFormatELF())
return;

if (IRL.Linkage == llvm::GlobalValue::LinkOnceODRLinkage ||
IRL.Linkage == llvm::GlobalValue::WeakODRLinkage)
if (Triple.supportsCOMDAT())
if (llvm::GlobalObject *GO = dyn_cast<llvm::GlobalObject>(GV))
GO->setComdat(M->getOrInsertComdat(GV->getName()));
}
}
};

Expand Down
5 changes: 2 additions & 3 deletions lib/IRGen/GenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1410,9 +1410,8 @@ void IRGenModule::emitVTableStubs() {

if (F.getEffectiveSymbolLinkage() == SILLinkage::Hidden)
alias->setVisibility(llvm::GlobalValue::HiddenVisibility);

if (useDllStorage())
alias->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
else
ApplyIRLinkage(IRLinkage::ExternalExport).to(alias);
}
}

Expand Down
6 changes: 3 additions & 3 deletions lib/IRGen/GenFunc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
#include "swift/AST/PrettyStackTrace.h"
#include "swift/AST/SubstitutionMap.h"
#include "swift/AST/Types.h"
#include "swift/IRGen/Linking.h"
#include "clang/AST/ASTContext.h"
#include "clang/CodeGen/CodeGenABITypes.h"
#include "llvm/IR/Constants.h"
Expand Down Expand Up @@ -1621,9 +1622,8 @@ void irgen::emitBlockHeader(IRGenFunction &IGF,
auto NSConcreteStackBlock =
IGF.IGM.getModule()->getOrInsertGlobal("_NSConcreteStackBlock",
IGF.IGM.ObjCClassStructTy);
if (IGF.IGM.useDllStorage())
cast<llvm::GlobalVariable>(NSConcreteStackBlock)
->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
ApplyIRLinkage(IRLinkage::ExternalImport)
.to(cast<llvm::GlobalVariable>(NSConcreteStackBlock));

//
// Set the flags.
Expand Down
5 changes: 1 addition & 4 deletions lib/IRGen/GenReflection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,7 @@ llvm::Constant *IRGenModule::getMangledAssociatedConformance(
llvm::GlobalValue::LinkOnceODRLinkage,
nullptr,
symbolName);
ApplyIRLinkage({llvm::GlobalValue::LinkOnceODRLinkage,
llvm::GlobalValue::HiddenVisibility,
llvm::GlobalValue::DefaultStorageClass})
.to(var);
ApplyIRLinkage(IRLinkage::InternalLinkOnceODR).to(var);
var->setAlignment(2);
setTrueConstGlobal(var);
var->setSection(getReflectionTypeRefSectionName());
Expand Down
32 changes: 9 additions & 23 deletions lib/IRGen/IRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -659,8 +659,8 @@ IRGenModule::createStringConstant(StringRef Str,
return NAME; \
NAME = Module.getOrInsertGlobal(SYM, FullTypeMetadataStructTy); \
if (useDllStorage() && !isStandardLibrary()) \
cast<llvm::GlobalVariable>(NAME)->setDLLStorageClass( \
llvm::GlobalValue::DLLImportStorageClass); \
ApplyIRLinkage(IRLinkage::ExternalImport) \
.to(cast<llvm::GlobalVariable>(NAME)); \
return NAME; \
}

Expand All @@ -681,9 +681,8 @@ llvm::Constant *IRGenModule::getObjCEmptyCachePtr() {
// struct objc_cache _objc_empty_cache;
ObjCEmptyCachePtr = Module.getOrInsertGlobal("_objc_empty_cache",
OpaquePtrTy->getElementType());
if (useDllStorage())
cast<llvm::GlobalVariable>(ObjCEmptyCachePtr)
->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
ApplyIRLinkage(IRLinkage::ExternalImport)
.to(cast<llvm::GlobalVariable>(ObjCEmptyCachePtr));
} else {
// FIXME: Remove even the null value per rdar://problem/18801263
ObjCEmptyCachePtr = llvm::ConstantPointerNull::get(OpaquePtrTy);
Expand Down Expand Up @@ -714,9 +713,8 @@ Address IRGenModule::getAddrOfObjCISAMask() {
assert(TargetInfo.hasISAMasking());
if (!ObjCISAMaskPtr) {
ObjCISAMaskPtr = Module.getOrInsertGlobal("swift_isaMask", IntPtrTy);
if (useDllStorage())
cast<llvm::GlobalVariable>(ObjCISAMaskPtr)
->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
ApplyIRLinkage(IRLinkage::ExternalImport)
.to(cast<llvm::GlobalVariable>(ObjCISAMaskPtr));
}
return Address(ObjCISAMaskPtr, getPointerAlignment());
}
Expand Down Expand Up @@ -960,11 +958,7 @@ void IRGenModule::addLinkLibrary(const LinkLibrary &linkLib) {
encodeForceLoadSymbolName(buf, linkLib.getName());
auto ForceImportThunk =
Module.getOrInsertFunction(buf, llvm::FunctionType::get(VoidTy, false));
ApplyIRLinkage({llvm::GlobalValue::ExternalLinkage,
llvm::GlobalValue::DefaultVisibility,
useDllStorage()
? llvm::GlobalValue::DLLImportStorageClass
: llvm::GlobalValue::DefaultStorageClass})
ApplyIRLinkage(IRLinkage::ExternalImport)
.to(cast<llvm::GlobalValue>(ForceImportThunk));

buf += "_$";
Expand All @@ -975,10 +969,7 @@ void IRGenModule::addLinkLibrary(const LinkLibrary &linkLib) {
/*isConstant=*/true,
llvm::GlobalValue::WeakODRLinkage,
ForceImportThunk, buf.str());
ApplyIRLinkage({llvm::GlobalValue::WeakODRLinkage,
llvm::GlobalValue::HiddenVisibility,
llvm::GlobalValue::DefaultStorageClass})
.to(ref);
ApplyIRLinkage(IRLinkage::InternalWeakODR).to(ref);
auto casted = llvm::ConstantExpr::getBitCast(ref, Int8PtrTy);
LLVMUsed.push_back(casted);
}
Expand Down Expand Up @@ -1089,12 +1080,7 @@ void IRGenModule::emitAutolinkInfo() {
llvm::Function::Create(llvm::FunctionType::get(VoidTy, false),
llvm::GlobalValue::ExternalLinkage, buf,
&Module);
ApplyIRLinkage({llvm::GlobalValue::ExternalLinkage,
llvm::GlobalValue::DefaultVisibility,
useDllStorage()
? llvm::GlobalValue::DLLExportStorageClass
: llvm::GlobalValue::DefaultStorageClass})
.to(ForceImportThunk);
ApplyIRLinkage(IRLinkage::ExternalExport).to(ForceImportThunk);

auto BB = llvm::BasicBlock::Create(getLLVMContext(), "", ForceImportThunk);
llvm::IRBuilder<> IRB(BB);
Expand Down
18 changes: 18 additions & 0 deletions lib/IRGen/Linking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,30 @@ const IRLinkage IRLinkage::InternalLinkOnceODR = {
llvm::GlobalValue::DefaultStorageClass,
};

const IRLinkage IRLinkage::InternalWeakODR = {
llvm::GlobalValue::WeakODRLinkage,
llvm::GlobalValue::HiddenVisibility,
llvm::GlobalValue::DefaultStorageClass,
};

const IRLinkage IRLinkage::Internal = {
llvm::GlobalValue::InternalLinkage,
llvm::GlobalValue::DefaultVisibility,
llvm::GlobalValue::DefaultStorageClass,
};

const IRLinkage IRLinkage::ExternalImport = {
llvm::GlobalValue::ExternalLinkage,
llvm::GlobalValue::DefaultVisibility,
llvm::GlobalValue::DLLImportStorageClass,
};

const IRLinkage IRLinkage::ExternalExport = {
llvm::GlobalValue::ExternalLinkage,
llvm::GlobalValue::DefaultVisibility,
llvm::GlobalValue::DLLExportStorageClass,
};

bool swift::irgen::useDllStorage(const llvm::Triple &triple) {
return triple.isOSBinFormatCOFF() && !triple.isOSCygMing();
}
Expand Down
5 changes: 1 addition & 4 deletions lib/IRGen/MetadataRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,7 @@ llvm::Constant *IRGenModule::getAddrOfStringForMetadataRef(
nullptr,
symbolName);

ApplyIRLinkage({llvm::GlobalValue::LinkOnceODRLinkage,
llvm::GlobalValue::HiddenVisibility,
llvm::GlobalValue::DefaultStorageClass})
.to(var);
ApplyIRLinkage(IRLinkage::InternalLinkOnceODR).to(var);
if (alignment)
var->setAlignment(alignment);
setTrueConstGlobal(var);
Expand Down