summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorRafael Espindola <rafael.espindola@gmail.com>2015-01-21 20:37:18 +0000
committerRafael Espindola <rafael.espindola@gmail.com>2015-01-21 20:37:18 +0000
commit71c1f3b6570c651f5fd16ee5711ae385bc7dd79e (patch)
tree971aae2899a1467b65f4b32622de1a9d5baee763 /lib
parentee2fee9f4322bc03c5cbe56dab0fbe90257d3cd8 (diff)
Revert r225705 from the branch.
There were a few followup commits to this one, but only this one made it to the branch. Also, there are still some open issues (pr22285), so it is probably better to leave this out of 3.6. original message: -------------------------------------------------------------------------- [patch][pr19848] Produce explicit comdats in clang. The llvm IR until recently had no support for comdats. This was a problem when targeting C++ on ELF/COFF as just using weak linkage would cause quite a bit of dead bits to remain on the executable (unless -ffunction-sections, -fdata-sections and --gc-sections were used). To fix the problem, llvm's codegen will just assume that any weak or linkonce that is not in an explicit comdat should be output in one with the same name as the global. This unfortunately breaks cases like pr19848 where a weak symbol is not xpected to be part of any comdat. Now that we have explicit comdats in the IR, we can finally get both cases right. This first patch just makes clang give explicit comdats to GlobalValues where t is allowed to. A followup patch to llvm will then stop implicitly producing comdats. ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/cfe/branches/release_36@226699 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib')
-rw-r--r--lib/CodeGen/CGDecl.cpp3
-rw-r--r--lib/CodeGen/CGDeclCXX.cpp10
-rw-r--r--lib/CodeGen/CodeGenModule.cpp31
-rw-r--r--lib/CodeGen/ItaniumCXXABI.cpp7
-rw-r--r--lib/CodeGen/MicrosoftCXXABI.cpp12
5 files changed, 22 insertions, 41 deletions
diff --git a/lib/CodeGen/CGDecl.cpp b/lib/CodeGen/CGDecl.cpp
index 766d2aa6ff..15a1a7fb5f 100644
--- a/lib/CodeGen/CGDecl.cpp
+++ b/lib/CodeGen/CGDecl.cpp
@@ -206,9 +206,6 @@ llvm::Constant *CodeGenModule::getOrCreateStaticVarDecl(
GV->setAlignment(getContext().getDeclAlign(&D).getQuantity());
setGlobalVisibility(GV, &D);
- if (supportsCOMDAT() && GV->isWeakForLinker())
- GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
-
if (D.getTLSKind())
setTLSMode(GV, D);
diff --git a/lib/CodeGen/CGDeclCXX.cpp b/lib/CodeGen/CGDeclCXX.cpp
index 3b379b7d25..19e4bdd00c 100644
--- a/lib/CodeGen/CGDeclCXX.cpp
+++ b/lib/CodeGen/CGDeclCXX.cpp
@@ -267,7 +267,15 @@ void CodeGenModule::EmitPointerToInitFunc(const VarDecl *D,
addUsedGlobal(PtrArray);
// If the GV is already in a comdat group, then we have to join it.
- if (llvm::Comdat *C = GV->getComdat())
+ llvm::Comdat *C = GV->getComdat();
+
+ // LinkOnce and Weak linkage are lowered down to a single-member comdat group.
+ // Make an explicit group so we can join it.
+ if (!C && (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage())) {
+ C = TheModule.getOrInsertComdat(GV->getName());
+ GV->setComdat(C);
+ }
+ if (C)
PtrArray->setComdat(C);
}
diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp
index 8981bfe89c..1b07160908 100644
--- a/lib/CodeGen/CodeGenModule.cpp
+++ b/lib/CodeGen/CodeGenModule.cpp
@@ -1928,31 +1928,6 @@ void CodeGenModule::MaybeHandleStaticInExternC(const SomeDecl *D,
R.first->second = nullptr;
}
-static bool shouldBeInCOMDAT(CodeGenModule &CGM, const Decl &D) {
- if (!CGM.supportsCOMDAT())
- return false;
-
- if (D.hasAttr<SelectAnyAttr>())
- return true;
-
- GVALinkage Linkage;
- if (auto *VD = dyn_cast<VarDecl>(&D))
- Linkage = CGM.getContext().GetGVALinkageForVariable(VD);
- else
- Linkage = CGM.getContext().GetGVALinkageForFunction(cast<FunctionDecl>(&D));
-
- switch (Linkage) {
- case GVA_Internal:
- case GVA_AvailableExternally:
- case GVA_StrongExternal:
- return false;
- case GVA_DiscardableODR:
- case GVA_StrongODR:
- return true;
- }
- llvm_unreachable("No such linkage");
-}
-
void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) {
llvm::Constant *Init = nullptr;
QualType ASTTy = D->getType();
@@ -2096,9 +2071,6 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) {
setTLSMode(GV, *D);
}
- if (shouldBeInCOMDAT(*this, *D))
- GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
-
// Emit the initializer function if necessary.
if (NeedsGlobalCtor || NeedsGlobalDtor)
EmitCXXGlobalVarDeclInitFunc(D, GV, NeedsGlobalCtor);
@@ -2433,9 +2405,6 @@ void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD,
MaybeHandleStaticInExternC(D, Fn);
- if (shouldBeInCOMDAT(*this, *D))
- Fn->setComdat(TheModule.getOrInsertComdat(Fn->getName()));
-
CodeGenFunction(*this).GenerateCode(D, Fn, FI);
setFunctionDefinitionAttributes(D, Fn);
diff --git a/lib/CodeGen/ItaniumCXXABI.cpp b/lib/CodeGen/ItaniumCXXABI.cpp
index f2ffabcf3e..deebab8583 100644
--- a/lib/CodeGen/ItaniumCXXABI.cpp
+++ b/lib/CodeGen/ItaniumCXXABI.cpp
@@ -1711,12 +1711,11 @@ void ItaniumCXXABI::EmitGuardedInit(CodeGenFunction &CGF,
// The ABI says: It is suggested that it be emitted in the same COMDAT group
// as the associated data object
- llvm::Comdat *C = var->getComdat();
- if (!D.isLocalVarDecl() && C) {
+ if (!D.isLocalVarDecl() && var->isWeakForLinker() && CGM.supportsCOMDAT()) {
+ llvm::Comdat *C = CGM.getModule().getOrInsertComdat(var->getName());
guard->setComdat(C);
+ var->setComdat(C);
CGF.CurFn->setComdat(C);
- } else if (CGM.supportsCOMDAT() && guard->isWeakForLinker()) {
- guard->setComdat(CGM.getModule().getOrInsertComdat(guard->getName()));
}
CGM.setStaticLocalDeclGuardAddress(&D, guard);
diff --git a/lib/CodeGen/MicrosoftCXXABI.cpp b/lib/CodeGen/MicrosoftCXXABI.cpp
index c80db7d18a..06c913e1ce 100644
--- a/lib/CodeGen/MicrosoftCXXABI.cpp
+++ b/lib/CodeGen/MicrosoftCXXABI.cpp
@@ -1829,10 +1829,18 @@ void MicrosoftCXXABI::EmitThreadLocalInitFuncs(
llvm::Function *F = CXXThreadLocalInits[I];
// If the GV is already in a comdat group, then we have to join it.
- if (llvm::Comdat *C = GV->getComdat())
+ llvm::Comdat *C = GV->getComdat();
+
+ // LinkOnce and Weak linkage are lowered down to a single-member comdat
+ // group.
+ // Make an explicit group so we can join it.
+ if (!C && (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage())) {
+ C = CGM.getModule().getOrInsertComdat(GV->getName());
+ GV->setComdat(C);
AddToXDU(F)->setComdat(C);
- else
+ } else {
NonComdatInits.push_back(F);
+ }
}
if (!NonComdatInits.empty()) {