diff --git a/llvm/lib/Transforms/Utils/SymbolRewriter.cpp b/llvm/lib/Transforms/Utils/SymbolRewriter.cpp index c93cdc4b52e2..d36283ea3ae1 100644 --- a/llvm/lib/Transforms/Utils/SymbolRewriter.cpp +++ b/llvm/lib/Transforms/Utils/SymbolRewriter.cpp @@ -79,6 +79,19 @@ static cl::list RewriteMapFiles("rewrite-map-file", namespace llvm { namespace SymbolRewriter { +void rewriteComdat(Module &M, GlobalObject *GO, const std::string &Source, + const std::string &Target) { + if (Comdat *CD = GO->getComdat()) { + auto &Comdats = M.getComdatSymbolTable(); + + Comdat *C = M.getOrInsertComdat(Target); + C->setSelectionKind(CD->getSelectionKind()); + GO->setComdat(C); + + Comdats.erase(Comdats.find(Source)); + } +} + template class ExplicitRewriteDescriptor : public RewriteDescriptor { @@ -102,10 +115,14 @@ template ::performOnModule(Module &M) { bool Changed = false; if (ValueType *S = (M.*Get)(Source)) { + if (GlobalObject *GO = dyn_cast(S)) + rewriteComdat(M, GO, Source, Target); + if (Value *T = (M.*Get)(Target)) S->setValueName(T->getValueName()); else S->setName(Target); + Changed = true; } return Changed; @@ -148,6 +165,9 @@ performOnModule(Module &M) { if (C.getName() == Name) continue; + if (GlobalObject *GO = dyn_cast(&C)) + rewriteComdat(M, GO, C.getName(), Name); + if (Value *V = (M.*Get)(Name)) C.setValueName(V->getValueName()); else diff --git a/llvm/test/SymbolRewriter/rewrite.ll b/llvm/test/SymbolRewriter/rewrite.ll index 716fff9f284c..e8a0db6d606c 100644 --- a/llvm/test/SymbolRewriter/rewrite.ll +++ b/llvm/test/SymbolRewriter/rewrite.ll @@ -28,12 +28,40 @@ entry: ret void } +$source_comdat_function = comdat any +define dllexport void @source_comdat_function() comdat($source_comdat_function) { +entry: + ret void +} + +$source_comdat_function_1 = comdat exactmatch +define dllexport void @source_comdat_function_1() comdat($source_comdat_function_1) { +entry: + ret void +} + +$source_comdat_variable = comdat largest +@source_comdat_variable = global i32 32, comdat($source_comdat_variable) + +$source_comdat_variable_1 = comdat noduplicates +@source_comdat_variable_1 = global i32 64, comdat($source_comdat_variable_1) + +; CHECK: $target_comdat_function = comdat any +; CHECK: $target_comdat_function_1 = comdat exactmatch +; CHECK: $target_comdat_variable = comdat largest +; CHECK: $target_comdat_variable_1 = comdat noduplicates + ; CHECK: @target_variable = external global i32 ; CHECK-NOT: @source_variable = external global i32 ; CHECK: @target_pattern_variable = external global i32 ; CHECK-NOT: @source_pattern_variable = external global i32 ; CHECK: @target_pattern_multiple_variable_matches = external global i32 ; CHECK-NOT: @source_pattern_multiple_variable_matches = external global i32 +; CHECK: @target_comdat_variable = global i32 32, comdat +; CHECK-NOT: @source_comdat_variable = global i32 32, comdat +; CHECK: @target_comdat_variable_1 = global i32 64, comdat +; CHECK-NOT: @source_comdat_variable_1 = global i32 64, comdat + ; CHECK: declare void @target_function() ; CHECK-NOT: declare void @source_function() ; CHECK: declare void @target_pattern_function() @@ -57,3 +85,8 @@ entry: ; CHECK: ret i32 %res ; CHECK: } +; CHECK: define dllexport void @target_comdat_function() comdat +; CHECK-NOT: define dllexport void @source_comdat_function() comdat +; CHECK: define dllexport void @target_comdat_function_1() comdat +; CHECK-NOT: define dllexport void @source_comdat_function_1() comdat + diff --git a/llvm/test/SymbolRewriter/rewrite.map b/llvm/test/SymbolRewriter/rewrite.map index ef6dfc8ca2b9..8094939d088d 100644 --- a/llvm/test/SymbolRewriter/rewrite.map +++ b/llvm/test/SymbolRewriter/rewrite.map @@ -44,3 +44,23 @@ global alias: { target: _ZN1SD1Ev, } +function: { + source: source_comdat_function, + target: target_comdat_function, +} + +function: { + source: source_comdat_function_(.*), + transform: target_comdat_function_\1, +} + +global variable: { + source: source_comdat_variable, + target: target_comdat_variable, +} + +global variable: { + source: source_comdat_variable_(.*), + transform: target_comdat_variable_\1, +} +