[PATCH] D31001: CloneModule: add an option for a custom LLVMContext

Alex Denisov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 15 14:40:10 PDT 2017


AlexDenisov created this revision.

The patch adds another parameter to the CloneModule family so that one can create a clone of a module using custom context.

Here is a bit of motivation and our use case: we create hundreds (sometimes thousands) of clones during program execution. If we create them using the context of an original module, then all those clones reside in memory until the very end of a program (when the 'global' context destructed).


https://reviews.llvm.org/D31001

Files:
  include/llvm/Transforms/Utils/Cloning.h
  lib/Transforms/Utils/CloneModule.cpp


Index: lib/Transforms/Utils/CloneModule.cpp
===================================================================
--- lib/Transforms/Utils/CloneModule.cpp
+++ lib/Transforms/Utils/CloneModule.cpp
@@ -48,9 +48,16 @@
 std::unique_ptr<Module> llvm::CloneModule(
     const Module *M, ValueToValueMapTy &VMap,
     function_ref<bool(const GlobalValue *)> ShouldCloneDefinition) {
+  return CloneModule(M, VMap, ShouldCloneDefinition, M->getContext());
+}
+
+std::unique_ptr<Module> llvm::CloneModule(
+    const Module *M, ValueToValueMapTy &VMap,
+    function_ref<bool(const GlobalValue *)> ShouldCloneDefinition,
+    LLVMContext &Context) {
   // First off, we need to create the new module.
   std::unique_ptr<Module> New =
-      llvm::make_unique<Module>(M->getModuleIdentifier(), M->getContext());
+      llvm::make_unique<Module>(M->getModuleIdentifier(), Context);
   New->setDataLayout(M->getDataLayout());
   New->setTargetTriple(M->getTargetTriple());
   New->setModuleInlineAsm(M->getModuleInlineAsm());
Index: include/llvm/Transforms/Utils/Cloning.h
===================================================================
--- include/llvm/Transforms/Utils/Cloning.h
+++ include/llvm/Transforms/Utils/Cloning.h
@@ -40,6 +40,7 @@
 class Function;
 class Instruction;
 class InvokeInst;
+class LLVMContext;
 class Loop;
 class LoopInfo;
 class Module;
@@ -58,6 +59,11 @@
 CloneModule(const Module *M, ValueToValueMapTy &VMap,
             function_ref<bool(const GlobalValue *)> ShouldCloneDefinition);
 
+std::unique_ptr<Module>
+CloneModule(const Module *M, ValueToValueMapTy &VMap,
+            function_ref<bool(const GlobalValue *)> ShouldCloneDefinition,
+            LLVMContext &Context);
+
 /// ClonedCodeInfo - This struct can be used to capture information about code
 /// being cloned, while it is being cloned.
 struct ClonedCodeInfo {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D31001.91937.patch
Type: text/x-patch
Size: 1851 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170315/59bb3e51/attachment.bin>


More information about the llvm-commits mailing list