Index: lib/Transforms/IPO/Internalize.cpp =================================================================== --- lib/Transforms/IPO/Internalize.cpp +++ lib/Transforms/IPO/Internalize.cpp @@ -119,10 +119,6 @@ if (ExternalComdats.count(C)) return false; - // If a comdat is not externally visible we can drop it. - if (auto GO = dyn_cast(&GV)) - GO->setComdat(nullptr); - if (GV.hasLocalLinkage()) return false; } else { Index: test/Transforms/Internalize/comdat.ll =================================================================== --- test/Transforms/Internalize/comdat.ll +++ test/Transforms/Internalize/comdat.ll @@ -8,7 +8,7 @@ ; CHECK: @c1_c = global i32 0, comdat($c1) @c1_c = global i32 0, comdat($c1) -; CHECK: @c2_b = internal global i32 0{{$}} +; CHECK: @c2_b = internal global i32 0, comdat($c2) @c2_b = global i32 0, comdat($c2) ; CHECK: @c3 = global i32 0, comdat{{$}} @@ -36,12 +36,12 @@ ret void } -; CHECK: define internal void @c2() { +; CHECK: define internal void @c2() comdat { define internal void @c2() comdat { ret void } -; CHECK: define internal void @c2_a() { +; CHECK: define internal void @c2_a() comdat($c2) { define void @c2_a() comdat($c2) { ret void } Index: test/Transforms/Internalize/preserve-comdat-members.ll =================================================================== --- /dev/null +++ test/Transforms/Internalize/preserve-comdat-members.ll @@ -0,0 +1,19 @@ +; Running internalize followed by globaldce should preserve unused globals that share a COMDAT with used globals +; RUN: opt < %s -internalize -internalize-public-api-list c1 -globaldce -S | FileCheck %s + +$c1 = comdat any + +; Since both used_global and and unused_fn share a COMDAT, they should both be kept, +; keeping the !associated metadata pointing at unused_fn +; CHECK: @used_global = internal global i32 0, comdat($c1), !associated !0 +@used_global = global i32 0, comdat($c1), !associated !0 + +@llvm.used = appending global [1 x i32*] [i32* @used_global], section "llvm.metadata" + +; CHECK: define internal void @unused_fn() comdat($c1) { +define void @unused_fn() comdat($c1) { + ret void +} + +; CHECK: !0 = !{void ()* @unused_fn} +!0 = !{void ()* @unused_fn} \ No newline at end of file