[llvm] r292430 - Cloning: Copy comdats when cloning globals.
Peter Collingbourne via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 18 12:02:31 PST 2017
Author: pcc
Date: Wed Jan 18 14:02:31 2017
New Revision: 292430
URL: http://llvm.org/viewvc/llvm-project?rev=292430&view=rev
Log:
Cloning: Copy comdats when cloning globals.
Differential Revision: https://reviews.llvm.org/D28838
Modified:
llvm/trunk/lib/Transforms/Utils/CloneModule.cpp
llvm/trunk/unittests/Transforms/Utils/Cloning.cpp
Modified: llvm/trunk/lib/Transforms/Utils/CloneModule.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/CloneModule.cpp?rev=292430&r1=292429&r2=292430&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/CloneModule.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/CloneModule.cpp Wed Jan 18 14:02:31 2017
@@ -20,6 +20,15 @@
#include "llvm-c/Core.h"
using namespace llvm;
+static void copyComdat(GlobalObject *Dst, const GlobalObject *Src) {
+ const Comdat *SC = Src->getComdat();
+ if (!SC)
+ return;
+ Comdat *DC = Dst->getParent()->getOrInsertComdat(SC->getName());
+ DC->setSelectionKind(SC->getSelectionKind());
+ Dst->setComdat(DC);
+}
+
/// This is not as easy as it might seem because we have to worry about making
/// copies of global variables and functions, and making their (initializers and
/// references, respectively) refer to the right globals.
@@ -124,6 +133,8 @@ std::unique_ptr<Module> llvm::CloneModul
I->getAllMetadata(MDs);
for (auto MD : MDs)
GV->addMetadata(MD.first, *MapMetadata(MD.second, VMap));
+
+ copyComdat(GV, &*I);
}
// Similarly, copy over function bodies now...
@@ -153,6 +164,8 @@ std::unique_ptr<Module> llvm::CloneModul
if (I.hasPersonalityFn())
F->setPersonalityFn(MapValue(I.getPersonalityFn(), VMap));
+
+ copyComdat(F, &I);
}
// And aliases
Modified: llvm/trunk/unittests/Transforms/Utils/Cloning.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Transforms/Utils/Cloning.cpp?rev=292430&r1=292429&r2=292430&view=diff
==============================================================================
--- llvm/trunk/unittests/Transforms/Utils/Cloning.cpp (original)
+++ llvm/trunk/unittests/Transforms/Utils/Cloning.cpp Wed Jan 18 14:02:31 2017
@@ -405,10 +405,14 @@ protected:
void SetupModule() { OldM = new Module("", C); }
void CreateOldModule() {
+ auto *CD = OldM->getOrInsertComdat("comdat");
+ CD->setSelectionKind(Comdat::ExactMatch);
+
auto GV = new GlobalVariable(
*OldM, Type::getInt32Ty(C), false, GlobalValue::ExternalLinkage,
ConstantInt::get(Type::getInt32Ty(C), 1), "gv");
GV->addMetadata(LLVMContext::MD_type, *MDNode::get(C, {}));
+ GV->setComdat(CD);
DIBuilder DBuilder(*OldM);
IRBuilder<> IBuilder(C);
@@ -419,6 +423,7 @@ protected:
auto *F =
Function::Create(FuncType, GlobalValue::PrivateLinkage, "f", OldM);
F->setPersonalityFn(PersFn);
+ F->setComdat(CD);
// Create debug info
auto *File = DBuilder.createFile("filename.c", "/file/dir/");
@@ -472,4 +477,15 @@ TEST_F(CloneModule, GlobalMetadata) {
GlobalVariable *NewGV = NewM->getGlobalVariable("gv");
EXPECT_NE(nullptr, NewGV->getMetadata(LLVMContext::MD_type));
}
+
+TEST_F(CloneModule, Comdat) {
+ GlobalVariable *NewGV = NewM->getGlobalVariable("gv");
+ auto *CD = NewGV->getComdat();
+ ASSERT_NE(nullptr, CD);
+ EXPECT_EQ("comdat", CD->getName());
+ EXPECT_EQ(Comdat::ExactMatch, CD->getSelectionKind());
+
+ Function *NewF = NewM->getFunction("f");
+ EXPECT_EQ(CD, NewF->getComdat());
+}
}
More information about the llvm-commits
mailing list