Skip to content

IRGen: ensure that helper functions are COMDAT'ed #20059

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
Nov 6, 2018
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
55 changes: 40 additions & 15 deletions include/swift/IRGen/Linking.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include "swift/SIL/SILModule.h"
#include "llvm/ADT/DenseMapInfo.h"
#include "llvm/IR/GlobalValue.h"
#include "llvm/IR/GlobalObject.h"
#include "llvm/IR/Module.h"

namespace llvm {
class Triple;
Expand Down Expand Up @@ -935,14 +937,43 @@ class LinkEntity {
#undef LINKENTITY_SET_FIELD
};

struct IRLinkage {
llvm::GlobalValue::LinkageTypes Linkage;
llvm::GlobalValue::VisibilityTypes Visibility;
llvm::GlobalValue::DLLStorageClassTypes DLLStorage;
};

class ApplyIRLinkage {
IRLinkage IRL;
public:
ApplyIRLinkage(IRLinkage IRL) : IRL(IRL) {}
void to(llvm::GlobalValue *GV) const {
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());

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

if (Triple.supportsCOMDAT())
if (llvm::GlobalObject *GO = dyn_cast<llvm::GlobalObject>(GV))
GO->setComdat(M->getOrInsertComdat(GV->getName()));
}
}
};

/// Encapsulated information about the linkage of an entity.
class LinkInfo {
LinkInfo() = default;

llvm::SmallString<32> Name;
llvm::GlobalValue::LinkageTypes Linkage;
llvm::GlobalValue::VisibilityTypes Visibility;
llvm::GlobalValue::DLLStorageClassTypes DLLStorageClass;
IRLinkage IRL;
ForDefinition_t ForDefinition;

public:
Expand All @@ -962,25 +993,19 @@ class LinkInfo {
return Name.str();
}
llvm::GlobalValue::LinkageTypes getLinkage() const {
return Linkage;
return IRL.Linkage;
}
llvm::GlobalValue::VisibilityTypes getVisibility() const {
return Visibility;
return IRL.Visibility;
}
llvm::GlobalValue::DLLStorageClassTypes getDLLStorage() const {
return DLLStorageClass;
return IRL.DLLStorage;
}

bool isForDefinition() const {
return ForDefinition;
}
bool isUsed() const {
return ForDefinition && isUsed(Linkage, Visibility, DLLStorageClass);
}
bool isForDefinition() const { return ForDefinition; }
bool isUsed() const { return ForDefinition && isUsed(IRL); }

static bool isUsed(llvm::GlobalValue::LinkageTypes Linkage,
llvm::GlobalValue::VisibilityTypes Visibility,
llvm::GlobalValue::DLLStorageClassTypes DLLStorage);
static bool isUsed(IRLinkage IRL);
};

StringRef encodeForceLoadSymbolName(llvm::SmallVectorImpl<char> &buf,
Expand Down
97 changes: 42 additions & 55 deletions lib/IRGen/GenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -917,8 +917,10 @@ llvm::Constant *IRGenModule::getAddrOfAssociatedTypeGenericParamRef(

auto var = B.finishAndCreateGlobal(symbolName, Alignment(4),
/*constant*/ true);
var->setLinkage(llvm::GlobalValue::LinkOnceODRLinkage);
var->setVisibility(llvm::GlobalValue::HiddenVisibility);
ApplyIRLinkage({llvm::GlobalValue::LinkOnceODRLinkage,
llvm::GlobalValue::HiddenVisibility,
llvm::GlobalValue::DefaultStorageClass})
.to(var);
setTrueConstGlobal(var);
return var;
}
Expand Down Expand Up @@ -1348,16 +1350,14 @@ void IRGenModule::emitVTableStubs() {
}
}

static std::tuple<llvm::GlobalValue::LinkageTypes,
llvm::GlobalValue::VisibilityTypes,
llvm::GlobalValue::DLLStorageClassTypes>
static IRLinkage
getIRLinkage(const UniversalLinkageInfo &info, SILLinkage linkage,
ForDefinition_t isDefinition,
bool isWeakImported) {
#define RESULT(LINKAGE, VISIBILITY, DLL_STORAGE) \
std::make_tuple(llvm::GlobalValue::LINKAGE##Linkage, \
llvm::GlobalValue::VISIBILITY##Visibility, \
llvm::GlobalValue::DLL_STORAGE##StorageClass)
IRLinkage{llvm::GlobalValue::LINKAGE##Linkage, \
llvm::GlobalValue::VISIBILITY##Visibility, \
llvm::GlobalValue::DLL_STORAGE##StorageClass}

// Use protected visibility for public symbols we define on ELF. ld.so
// doesn't support relative relocations at load time, which interferes with
Expand All @@ -1375,8 +1375,8 @@ getIRLinkage(const UniversalLinkageInfo &info, SILLinkage linkage,

switch (linkage) {
case SILLinkage::Public:
return std::make_tuple(llvm::GlobalValue::ExternalLinkage,
PublicDefinitionVisibility, ExportedStorage);
return {llvm::GlobalValue::ExternalLinkage, PublicDefinitionVisibility,
ExportedStorage};

case SILLinkage::Shared:
case SILLinkage::SharedExternal:
Expand All @@ -1394,34 +1394,25 @@ getIRLinkage(const UniversalLinkageInfo &info, SILLinkage linkage,
auto visibility = info.shouldAllPrivateDeclsBeVisibleFromOtherFiles()
? llvm::GlobalValue::HiddenVisibility
: llvm::GlobalValue::DefaultVisibility;
return std::make_tuple(linkage, visibility,
llvm::GlobalValue::DefaultStorageClass);
return {linkage, visibility, llvm::GlobalValue::DefaultStorageClass};
}

case SILLinkage::PublicExternal: {
if (isDefinition) {
return std::make_tuple(llvm::GlobalValue::AvailableExternallyLinkage,
llvm::GlobalValue::DefaultVisibility,
llvm::GlobalValue::DefaultStorageClass);
}
if (isDefinition)
return RESULT(AvailableExternally, Default, Default);

auto linkage = isWeakImported ? llvm::GlobalValue::ExternalWeakLinkage
: llvm::GlobalValue::ExternalLinkage;
return std::make_tuple(linkage, llvm::GlobalValue::DefaultVisibility,
ImportedStorage);
return {linkage, llvm::GlobalValue::DefaultVisibility, ImportedStorage};
}

case SILLinkage::HiddenExternal:
case SILLinkage::PrivateExternal:
if (isDefinition) {
return std::make_tuple(llvm::GlobalValue::AvailableExternallyLinkage,
llvm::GlobalValue::HiddenVisibility,
llvm::GlobalValue::DefaultStorageClass);
}
if (isDefinition)
return RESULT(AvailableExternally, Hidden, Default);

return std::make_tuple(llvm::GlobalValue::ExternalLinkage,
llvm::GlobalValue::DefaultVisibility,
ImportedStorage);
return {llvm::GlobalValue::ExternalLinkage,
llvm::GlobalValue::DefaultVisibility, ImportedStorage};

}

Expand All @@ -1436,22 +1427,18 @@ void irgen::updateLinkageForDefinition(IRGenModule &IGM,
// TODO: there are probably cases where we can avoid redoing the
// entire linkage computation.
UniversalLinkageInfo linkInfo(IGM);
auto linkage =
auto IRL =
getIRLinkage(linkInfo, entity.getLinkage(ForDefinition),
ForDefinition, entity.isWeakImported(IGM.getSwiftModule()));
global->setLinkage(std::get<0>(linkage));
global->setVisibility(std::get<1>(linkage));
global->setDLLStorageClass(std::get<2>(linkage));
ApplyIRLinkage(IRL).to(global);

// Everything externally visible is considered used in Swift.
// That mostly means we need to be good at not marking things external.
//
// Exclude "main", because it should naturally be used, and because adding it
// to llvm.used leaves a dangling use when the REPL attempts to discard
// intermediate mains.
if (LinkInfo::isUsed(std::get<0>(linkage), std::get<1>(linkage),
std::get<2>(linkage)) &&
global->getName() != SWIFT_ENTRY_POINT_FUNCTION)
if (LinkInfo::isUsed(IRL) && global->getName() != SWIFT_ENTRY_POINT_FUNCTION)
IGM.addUsedGlobal(global);
}

Expand All @@ -1477,9 +1464,8 @@ LinkInfo LinkInfo::get(const UniversalLinkageInfo &linkInfo,
ForDefinition_t(swiftModule->isStdlibModule() || isDefinition);

entity.mangle(result.Name);
std::tie(result.Linkage, result.Visibility, result.DLLStorageClass) =
getIRLinkage(linkInfo, entity.getLinkage(isStdlibOrDefinition),
isDefinition, entity.isWeakImported(swiftModule));
result.IRL = getIRLinkage(linkInfo, entity.getLinkage(isStdlibOrDefinition),
isDefinition, entity.isWeakImported(swiftModule));
result.ForDefinition = isDefinition;
return result;
}
Expand All @@ -1490,8 +1476,7 @@ LinkInfo LinkInfo::get(const UniversalLinkageInfo &linkInfo, StringRef name,
LinkInfo result;

result.Name += name;
std::tie(result.Linkage, result.Visibility, result.DLLStorageClass) =
getIRLinkage(linkInfo, linkage, isDefinition, isWeakImported);
result.IRL = getIRLinkage(linkInfo, linkage, isDefinition, isWeakImported);
result.ForDefinition = isDefinition;
return result;
}
Expand Down Expand Up @@ -1523,6 +1508,7 @@ llvm::Function *irgen::createFunction(IRGenModule &IGM,

llvm::Function *fn =
llvm::Function::Create(signature.getType(), linkInfo.getLinkage(), name);
// TODO(compnerd) apply COMDAT to definitions
fn->setVisibility(linkInfo.getVisibility());
fn->setDLLStorageClass(linkInfo.getDLLStorage());
fn->setCallingConv(signature.getCallingConv());
Expand All @@ -1531,7 +1517,7 @@ llvm::Function *irgen::createFunction(IRGenModule &IGM,
IGM.Module.getFunctionList().insert(insertBefore->getIterator(), fn);
} else {
IGM.Module.getFunctionList().push_back(fn);
}
}

llvm::AttrBuilder initialAttrs;
IGM.constructInitialFnAttributes(initialAttrs, FuncOptMode);
Expand All @@ -1556,16 +1542,14 @@ llvm::Function *irgen::createFunction(IRGenModule &IGM,
return fn;
}

bool LinkInfo::isUsed(llvm::GlobalValue::LinkageTypes Linkage,
llvm::GlobalValue::VisibilityTypes Visibility,
llvm::GlobalValue::DLLStorageClassTypes DLLStorage) {
bool LinkInfo::isUsed(IRLinkage IRL) {
// Everything externally visible is considered used in Swift.
// That mostly means we need to be good at not marking things external.
return Linkage == llvm::GlobalValue::ExternalLinkage &&
(Visibility == llvm::GlobalValue::DefaultVisibility ||
Visibility == llvm::GlobalValue::ProtectedVisibility) &&
(DLLStorage == llvm::GlobalValue::DefaultStorageClass ||
DLLStorage == llvm::GlobalValue::DLLExportStorageClass);
return IRL.Linkage == llvm::GlobalValue::ExternalLinkage &&
(IRL.Visibility == llvm::GlobalValue::DefaultVisibility ||
IRL.Visibility == llvm::GlobalValue::ProtectedVisibility) &&
(IRL.DLLStorage == llvm::GlobalValue::DefaultStorageClass ||
IRL.DLLStorage == llvm::GlobalValue::DLLExportStorageClass);
}

/// Get or create an LLVM global variable with these linkage rules.
Expand All @@ -1591,8 +1575,10 @@ llvm::GlobalVariable *swift::irgen::createVariable(
auto var = new llvm::GlobalVariable(IGM.Module, storageType,
/*constant*/ false, linkInfo.getLinkage(),
/*initializer*/ nullptr, name);
var->setVisibility(linkInfo.getVisibility());
var->setDLLStorageClass(linkInfo.getDLLStorage());
ApplyIRLinkage({linkInfo.getLinkage(),
linkInfo.getVisibility(),
linkInfo.getDLLStorage()})
.to(var);
var->setAlignment(alignment.getValue());

// Everything externally visible is considered used in Swift.
Expand Down Expand Up @@ -2868,8 +2854,8 @@ llvm::GlobalValue *IRGenModule::defineAlias(LinkEntity entity,
auto *alias = llvm::GlobalAlias::create(
ptrTy->getElementType(), ptrTy->getAddressSpace(), link.getLinkage(),
link.getName(), definition, &Module);
alias->setVisibility(link.getVisibility());
alias->setDLLStorageClass(link.getDLLStorage());
ApplyIRLinkage({link.getLinkage(), link.getVisibility(), link.getDLLStorage()})
.to(alias);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pattern — repeated over and over again — where we create a declaration and then immediately override half the values from it with the values of an IRLinkage is quite unfortunate. I like the idea of encapsulating IRLinkage; perhaps it ought to have functions on it to actually create these globals?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disliked that we were setting the properties for the linkage everywhere but didn't manage to do it very uniformly. So, the application of the IRLinkage at least ensures we set the properties more uniformly. But, I can try to sink this into createVariable and createFunction. I'm happy to try to do that in a follow up.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with doing it in multiple stages. Some other suggestions:

  • As mentioned, please add functions to create variables and functions.
  • Please look around IRGen for other places that create variables and functions that could use this. LLVM's API for creating globals is really a mess, so it shouldn't be hard to create something that's friendlier and therefore encourages people to use it (and thus implicitly get the correctness benefits).
  • Please add static factories (like shared() or internal()) for the common patterns.
  • Consider incorporating unnamed_addr into this, and maybe an optional section name. But constant probably ought to be a mandatory argument to the function to create a global, not part of the linkage.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, unnamed_addr and dso_local are other attributes that I think that we should grow into this. But, this unblocks Windows builds and improves things.


if (link.isUsed()) {
addUsedGlobal(alias);
Expand Down Expand Up @@ -3797,9 +3783,10 @@ static llvm::Function *shouldDefineHelper(IRGenModule &IGM,
if (!def) return nullptr;
if (!def->empty()) return nullptr;

def->setLinkage(llvm::Function::LinkOnceODRLinkage);
def->setVisibility(llvm::Function::HiddenVisibility);
def->setDLLStorageClass(llvm::GlobalVariable::DefaultStorageClass);
ApplyIRLinkage({llvm::GlobalValue::LinkOnceODRLinkage,
llvm::GlobalValue::HiddenVisibility,
llvm::GlobalValue::DefaultStorageClass})
.to(def);
def->setDoesNotThrow();
def->setCallingConv(IGM.DefaultCC);
if (setIsNoInline)
Expand Down
5 changes: 4 additions & 1 deletion lib/IRGen/GenEnum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1473,7 +1473,10 @@ namespace {
auto func =
llvm::Function::Create(consumeTy, llvm::GlobalValue::LinkOnceODRLinkage,
llvm::StringRef(name), IGM.getModule());
func->setVisibility(llvm::GlobalValue::HiddenVisibility);
ApplyIRLinkage({llvm::GlobalValue::LinkOnceODRLinkage,
llvm::GlobalValue::HiddenVisibility,
llvm::GlobalValue::DefaultStorageClass})
.to(func);
func->setAttributes(IGM.constructInitialAttributes());
func->setDoesNotThrow();
func->setCallingConv(IGM.DefaultCC);
Expand Down
6 changes: 4 additions & 2 deletions lib/IRGen/GenObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -963,8 +963,10 @@ static llvm::Constant *findSwiftAsObjCThunk(IRGenModule &IGM, SILDeclRef ref) {
SILFunction *SILFn = IGM.getSILModule().lookUpFunction(ref);
assert(SILFn && "no IR function for swift-as-objc thunk");
auto fn = IGM.getAddrOfSILFunction(SILFn, NotForDefinition);
fn->setVisibility(llvm::GlobalValue::DefaultVisibility);
fn->setLinkage(llvm::GlobalValue::InternalLinkage);
ApplyIRLinkage({llvm::GlobalValue::InternalLinkage,
llvm::GlobalValue::DefaultVisibility,
llvm::GlobalValue::DefaultStorageClass})
.to(fn);
fn->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);

return llvm::ConstantExpr::getBitCast(fn, IGM.Int8PtrTy);
Expand Down
5 changes: 4 additions & 1 deletion lib/IRGen/GenReflection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,10 @@ void IRGenModule::emitReflectionMetadataVersion() {
llvm::GlobalValue::LinkOnceODRLinkage,
Init,
"__swift_reflection_version");
Version->setVisibility(llvm::GlobalValue::HiddenVisibility);
ApplyIRLinkage({llvm::GlobalValue::LinkOnceODRLinkage,
llvm::GlobalValue::HiddenVisibility,
llvm::GlobalValue::DefaultStorageClass})
.to(Version);
addUsedGlobal(Version);
}

Expand Down
5 changes: 4 additions & 1 deletion lib/IRGen/MetadataRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,10 @@ llvm::Constant *IRGenModule::getAddrOfStringForTypeRef(
llvm::GlobalValue::LinkOnceODRLinkage,
nullptr,
symbolName);
var->setVisibility(llvm::GlobalValue::HiddenVisibility);
ApplyIRLinkage({llvm::GlobalValue::LinkOnceODRLinkage,
llvm::GlobalValue::HiddenVisibility,
llvm::GlobalValue::DefaultStorageClass})
.to(var);
var->setAlignment(2);
setTrueConstGlobal(var);
var->setSection(getReflectionTypeRefSectionName());
Expand Down