[PATCH] D152148: llvm-extract: Replace IFuncs with declarations

Christian Ulmann via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 5 05:48:49 PDT 2023


Dinistro created this revision.
Herald added subscribers: ormris, hiraditya.
Herald added a project: All.
Dinistro requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This commit ensures that llvm-extract does not copy all IFuncs into the
resulting modules. Before this change, ifuncs were not modified which
could cause the emission unexpected IR files.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D152148

Files:
  llvm/lib/Transforms/IPO/ExtractGV.cpp
  llvm/test/tools/llvm-extract/delete-ifunc.ll


Index: llvm/test/tools/llvm-extract/delete-ifunc.ll
===================================================================
--- /dev/null
+++ llvm/test/tools/llvm-extract/delete-ifunc.ll
@@ -0,0 +1,22 @@
+; RUN: llvm-extract -func foo -S %s | FileCheck %s
+
+; llvm-extract should not copy ifuncs into the new module, so make sure they
+; are turned into declarations.
+
+; CHECK: define void @foo() {
+; CHECK: call void @ifunc()
+define void @foo() {
+  call void @ifunc()
+  ret void
+}
+
+define void @ifunc_impl() {
+  ret void
+}
+
+; CHECK: declare void @ifunc()
+ at ifunc = ifunc void (), ptr @ifunc_resolver
+
+define internal ptr @ifunc_resolver() {
+  ret ptr @ifunc_impl
+}
Index: llvm/lib/Transforms/IPO/ExtractGV.cpp
===================================================================
--- llvm/lib/Transforms/IPO/ExtractGV.cpp
+++ llvm/lib/Transforms/IPO/ExtractGV.cpp
@@ -36,7 +36,7 @@
   }
 
   // Map linkonce* to weak* so that llvm doesn't drop this GV.
-  switch(GV.getLinkage()) {
+  switch (GV.getLinkage()) {
   default:
     llvm_unreachable("Unexpected linkage");
   case GlobalValue::LinkOnceAnyLinkage:
@@ -48,10 +48,9 @@
   }
 }
 
-
-    /// If deleteS is true, this pass deletes the specified global values.
-    /// Otherwise, it deletes as much of the module as possible, except for the
-    /// global values specified.
+/// If deleteS is true, this pass deletes the specified global values.
+/// Otherwise, it deletes as much of the module as possible, except for the
+/// global values specified.
 ExtractGVPass::ExtractGVPass(std::vector<GlobalValue *> &GVs, bool deleteS,
                              bool keepConstInit)
     : Named(GVs.begin(), GVs.end()), deleteStuff(deleteS),
@@ -129,5 +128,22 @@
     }
   }
 
+  // Visit the IFuncs.
+  for (GlobalIFunc &IF : llvm::make_early_inc_range(M.ifuncs())) {
+    bool Delete = deleteStuff == (bool)Named.count(&IF);
+    makeVisible(IF, Delete);
+
+    if (!Delete)
+      continue;
+
+    auto *FuncType = dyn_cast<FunctionType>(IF.getValueType());
+    IF.removeFromParent();
+    llvm::Value *Declaration =
+        Function::Create(FuncType, GlobalValue::ExternalLinkage,
+                         IF.getAddressSpace(), IF.getName(), &M);
+    IF.replaceAllUsesWith(Declaration);
+    delete &IF;
+  }
+
   return PreservedAnalyses::none();
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D152148.528385.patch
Type: text/x-patch
Size: 2328 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230605/e0844c58/attachment.bin>


More information about the llvm-commits mailing list