[llvm] f069327 - CloneModule: Handling cloning ifuncs

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 23 09:38:25 PST 2022


Author: Matt Arsenault
Date: 2022-11-23T12:22:06-05:00
New Revision: f0693277c7cbe982bcfa5d1a8a9540807f6ec314

URL: https://github.com/llvm/llvm-project/commit/f0693277c7cbe982bcfa5d1a8a9540807f6ec314
DIFF: https://github.com/llvm/llvm-project/commit/f0693277c7cbe982bcfa5d1a8a9540807f6ec314.diff

LOG: CloneModule: Handling cloning ifuncs

This is tested in a future llvm-reduce patch.

Added: 
    

Modified: 
    llvm/lib/Transforms/Utils/CloneModule.cpp
    llvm/unittests/Transforms/Utils/CloningTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/CloneModule.cpp b/llvm/lib/Transforms/Utils/CloneModule.cpp
index 55cda0f11e472..55e051298a9a3 100644
--- a/llvm/lib/Transforms/Utils/CloneModule.cpp
+++ b/llvm/lib/Transforms/Utils/CloneModule.cpp
@@ -109,6 +109,15 @@ std::unique_ptr<Module> llvm::CloneModule(
     VMap[&I] = GA;
   }
 
+  for (const GlobalIFunc &I : M.ifuncs()) {
+    // Defer setting the resolver function until after functions are cloned.
+    auto *GI =
+        GlobalIFunc::create(I.getValueType(), I.getAddressSpace(),
+                            I.getLinkage(), I.getName(), nullptr, New.get());
+    GI->copyAttributesFrom(&I);
+    VMap[&I] = GI;
+  }
+
   // Now that all of the things that global variable initializer can refer to
   // have been created, loop through and copy the global variable referrers
   // over...  We also set the attributes on the global now.
@@ -184,6 +193,12 @@ std::unique_ptr<Module> llvm::CloneModule(
       GA->setAliasee(MapValue(C, VMap));
   }
 
+  for (const GlobalIFunc &I : M.ifuncs()) {
+    GlobalIFunc *GI = cast<GlobalIFunc>(VMap[&I]);
+    if (const Constant *Resolver = I.getResolver())
+      GI->setResolver(MapValue(Resolver, VMap));
+  }
+
   // And named metadata....
   for (const NamedMDNode &NMD : M.named_metadata()) {
     NamedMDNode *NewNMD = New->getOrInsertNamedMetadata(NMD.getName());

diff  --git a/llvm/unittests/Transforms/Utils/CloningTest.cpp b/llvm/unittests/Transforms/Utils/CloningTest.cpp
index 186714a318c83..9b4a6fbe81100 100644
--- a/llvm/unittests/Transforms/Utils/CloningTest.cpp
+++ b/llvm/unittests/Transforms/Utils/CloningTest.cpp
@@ -922,6 +922,23 @@ class CloneModule : public ::testing::Test {
     GV->addMetadata(LLVMContext::MD_type, *MDNode::get(C, {}));
     GV->setComdat(CD);
 
+    // Add ifuncs
+    {
+      const unsigned AddrSpace = 123;
+      auto *FuncPtrTy = Type::getInt8Ty(C)->getPointerTo(123);
+      auto *FuncTy = FunctionType::get(FuncPtrTy, false);
+
+      auto *ResolverF = Function::Create(FuncTy, GlobalValue::PrivateLinkage,
+                                         AddrSpace, "resolver", OldM);
+      BasicBlock *ResolverBody = BasicBlock::Create(C, "", ResolverF);
+      ReturnInst::Create(C, ConstantPointerNull::get(FuncPtrTy), ResolverBody);
+
+      GlobalIFunc *GI = GlobalIFunc::create(FuncTy, AddrSpace,
+                                            GlobalValue::LinkOnceODRLinkage,
+                                            "an_ifunc", ResolverF, OldM);
+      GI->setVisibility(GlobalValue::ProtectedVisibility);
+    }
+
     {
       // Add an empty compile unit first that isn't otherwise referenced, to
       // confirm that compile units get cloned in the correct order.
@@ -1087,4 +1104,19 @@ TEST_F(CloneModule, Comdat) {
   Function *NewF = NewM->getFunction("f");
   EXPECT_EQ(CD, NewF->getComdat());
 }
+
+TEST_F(CloneModule, IFunc) {
+  ASSERT_EQ(1u, NewM->ifunc_size());
+
+  const GlobalIFunc &IFunc = *NewM->ifunc_begin();
+  EXPECT_EQ("an_ifunc", IFunc.getName());
+  EXPECT_EQ(GlobalValue::LinkOnceODRLinkage, IFunc.getLinkage());
+  EXPECT_EQ(GlobalValue::ProtectedVisibility, IFunc.getVisibility());
+  EXPECT_EQ(123u, IFunc.getAddressSpace());
+
+  const Function *Resolver = IFunc.getResolverFunction();
+  ASSERT_NE(nullptr, Resolver);
+  EXPECT_EQ("resolver", Resolver->getName());
+  EXPECT_EQ(GlobalValue::PrivateLinkage, Resolver->getLinkage());
+}
 }


        


More information about the llvm-commits mailing list