[PATCH] D31722: [llvm-extract] Add option for recursive extraction

Keno Fischer via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 5 13:22:04 PDT 2017


loladiro created this revision.

Particularly, with --delete, this can be very useful for testing
new optimizations on some hotspots, without having to run it on the whole
application. E.g. as such:

  llvm-extract app.bc --recursive --rfunc .*hotspot.* > hotspot.bc
  llvm-extract app.bc --recursive --delete --rfunc .*hotspot.* > residual.bc
  llc -filetype=obj residual.bc > residual.o
  # Play with hotspot.bc here
  llc -filetype=obj hotspot.bc > hotspot.o
  cc -o app residual.o hotspot.o


https://reviews.llvm.org/D31722

Files:
  tools/llvm-extract/llvm-extract.cpp


Index: tools/llvm-extract/llvm-extract.cpp
===================================================================
--- tools/llvm-extract/llvm-extract.cpp
+++ tools/llvm-extract/llvm-extract.cpp
@@ -17,10 +17,11 @@
 #include "llvm/Bitcode/BitcodeWriterPass.h"
 #include "llvm/IR/DataLayout.h"
 #include "llvm/IR/IRPrintingPasses.h"
+#include "llvm/IR/Instructions.h"
 #include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/LegacyPassManager.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IRReader/IRReader.h"
-#include "llvm/IR/LegacyPassManager.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Error.h"
 #include "llvm/Support/FileSystem.h"
@@ -50,6 +51,10 @@
 static cl::opt<bool>
 DeleteFn("delete", cl::desc("Delete specified Globals from Module"));
 
+static cl::opt<bool>
+    Recursive("recursive",
+              cl::desc("Recursively extract all called functions"));
+
 // ExtractFuncs - The functions to extract from the module.
 static cl::list<std::string>
 ExtractFuncs("func", cl::desc("Specify function to extract"),
@@ -226,6 +231,32 @@
   // Use *argv instead of argv[0] to work around a wrong GCC warning.
   ExitOnError ExitOnErr(std::string(*argv) + ": error reading input: ");
 
+  if (Recursive) {
+    std::vector<llvm::Function *> Workqueue;
+    for (auto *GV : GVs) {
+      if (Function *F = dyn_cast<Function>(GV)) {
+        Workqueue.push_back(F);
+      }
+    }
+    while (!Workqueue.empty()) {
+      Function *F = &*Workqueue.back();
+      Workqueue.pop_back();
+      ExitOnErr(F->materialize());
+      for (auto &BB : *F) {
+        for (auto &I : BB) {
+          if (CallInst *CI = dyn_cast<CallInst>(&I)) {
+            if (Function *CF = CI->getCalledFunction()) {
+              if (!CF->isDeclaration() && !GVs.count(CF)) {
+                GVs.insert(CF);
+                Workqueue.push_back(CF);
+              }
+            }
+          }
+        }
+      }
+    }
+  }
+
   auto Materialize = [&](GlobalValue &GV) { ExitOnErr(GV.materialize()); };
 
   // Materialize requisite global values.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D31722.94274.patch
Type: text/x-patch
Size: 2056 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170405/db8e0391/attachment.bin>


More information about the llvm-commits mailing list