diff options
-rw-r--r-- | llvm/lib/Linker/LinkModules.cpp | 16 | ||||
-rw-r--r-- | llvm/test/Linker/Inputs/funcimport_comdat.ll | 4 | ||||
-rw-r--r-- | llvm/test/Linker/funcimport_comdat.ll | 28 |
3 files changed, 46 insertions, 2 deletions
diff --git a/llvm/lib/Linker/LinkModules.cpp b/llvm/lib/Linker/LinkModules.cpp index a9b8ef2470b..4bb4b3f4f23 100644 --- a/llvm/lib/Linker/LinkModules.cpp +++ b/llvm/lib/Linker/LinkModules.cpp @@ -722,9 +722,21 @@ void ThinLTOGlobalProcessing::processGlobalForThinLTO(GlobalValue &GV) { GV.setVisibility(GlobalValue::HiddenVisibility); if (isModuleExporting()) NewExportedValues.insert(&GV); - return; + } else + GV.setLinkage(getLinkage(&GV)); + + // Remove functions imported as available externally defs from comdats, + // as this is a declaration for the linker, and will be dropped eventually. + // It is illegal for comdats to contain declarations. + auto *GO = dyn_cast_or_null<GlobalObject>(&GV); + if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) { + // The IRMover should not have placed any imported declarations in + // a comdat, so the only declaration that should be in a comdat + // at this point would be a definition imported as available_externally. + assert(GO->hasAvailableExternallyLinkage() && + "Expected comdat on definition (possibly available external)"); + GO->setComdat(nullptr); } - GV.setLinkage(getLinkage(&GV)); } void ThinLTOGlobalProcessing::processGlobalsForThinLTO() { diff --git a/llvm/test/Linker/Inputs/funcimport_comdat.ll b/llvm/test/Linker/Inputs/funcimport_comdat.ll new file mode 100644 index 00000000000..9a4b9d728d4 --- /dev/null +++ b/llvm/test/Linker/Inputs/funcimport_comdat.ll @@ -0,0 +1,4 @@ +define i32 @main() #0 { +entry: + ret i32 0 +} diff --git a/llvm/test/Linker/funcimport_comdat.ll b/llvm/test/Linker/funcimport_comdat.ll new file mode 100644 index 00000000000..22276b0e9a0 --- /dev/null +++ b/llvm/test/Linker/funcimport_comdat.ll @@ -0,0 +1,28 @@ +; Do setup work for all below tests: generate bitcode and combined index +; RUN: llvm-as -function-summary %s -o %t.bc +; RUN: llvm-as -function-summary %p/Inputs/funcimport_comdat.ll -o %t2.bc +; RUN: llvm-lto -thinlto -o %t3 %t.bc %t2.bc + +; Ensure linking of comdat containing external linkage global and function +; removes the imported available_externally defs from comdat. +; RUN: llvm-link %t2.bc -functionindex=%t3.thinlto.bc -import=comdat1_func1:%t.bc -S | FileCheck %s --check-prefix=IMPORTCOMDAT +; IMPORTCOMDAT-NOT: $comdat1 = comdat any +; IMPORTCOMDAT-NOT: comdat($comdat1) + +; Ensure linking of comdat containing internal linkage function with alias +; removes the imported and promoted available_externally defs from comdat. +; RUN: llvm-link %t2.bc -functionindex=%t3.thinlto.bc -import=comdat2_func1:%t.bc -S | FileCheck %s --check-prefix=IMPORTCOMDAT2 +; IMPORTCOMDAT2-NOT: $comdat2 = comdat any +; IMPORTCOMDAT2-NOT: comdat($comdat2) + +$comdat1 = comdat any +@comdat1_glob = global i32 0, comdat($comdat1) +define void @comdat1_func1() comdat($comdat1) { + ret void +} + +$comdat2 = comdat any +@comdat2_alias = alias void (), void ()* @comdat2_func1 +define internal void @comdat2_func1() comdat($comdat2) { + ret void +} |