[llvm] 2544d91 - llvm-extract: Replace IFuncs with declarations

Christian Ulmann via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 6 00:18:57 PDT 2023


Author: Christian Ulmann
Date: 2023-06-06T07:18:33Z
New Revision: 2544d91956f9b7b955f13dc2ffb12da30c5f7f87

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

LOG: llvm-extract: Replace IFuncs with declarations

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.

Reviewed By: darthscsi

Differential Revision: https://reviews.llvm.org/D152148

Added: 
    llvm/test/tools/llvm-extract/delete-ifunc.ll

Modified: 
    llvm/lib/Transforms/IPO/ExtractGV.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/IPO/ExtractGV.cpp b/llvm/lib/Transforms/IPO/ExtractGV.cpp
index d5073eed2fef0..6414ea69c9f75 100644
--- a/llvm/lib/Transforms/IPO/ExtractGV.cpp
+++ b/llvm/lib/Transforms/IPO/ExtractGV.cpp
@@ -36,7 +36,7 @@ static void makeVisible(GlobalValue &GV, bool Delete) {
   }
 
   // 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 @@ static void makeVisible(GlobalValue &GV, bool Delete) {
   }
 }
 
-
-    /// 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 @@ PreservedAnalyses ExtractGVPass::run(Module &M, ModuleAnalysisManager &) {
     }
   }
 
+  // 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();
 }

diff  --git a/llvm/test/tools/llvm-extract/delete-ifunc.ll b/llvm/test/tools/llvm-extract/delete-ifunc.ll
new file mode 100644
index 0000000000000..0ffa337415326
--- /dev/null
+++ b/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
+}


        


More information about the llvm-commits mailing list