title: LLVM clone function pass to different module
body: <p>I'm creating LLVM pass, loaded in opt as library, which takes function from hardcoded module and clones its function to the input module.</p>
  
  <pre><code>bool MyPass::runOnModule(llvm::Module &amp;M)
  {
    SMDiagnostic error;
    LLVMContext context;
    StringRef ImplAssembly = R"(
      define void @foo() {
        ret void
      }
    )";
    auto InjectedModule = parseAssemblyString(ImplAssembly, error, context);
  
    auto* ImplFunction = InjectedModule-&gt;getFunction("foo");
    auto DeclFunction = Function::Create(ImplFunction-&gt;getFunctionType(), ImplFunction-&gt;getLinkage(), "foo", M);
  
    VMap[ImplFunction] = DeclFunction;
  
    SmallVector&lt;ReturnInst*, 8&gt; Returns;
    CloneFunctionInto(DeclFunction, ImplFunction, VMap, false, Returns);
    return true;
  }
  </code></pre>
  
  <p>Even though the pass successfully clones the function, opt throws EXC_BAD_ACCESS at LookupBucketFor function.</p>
  
  <pre><code>if (LLVM_LIKELY(KeyInfoT::isEqual(Val, ThisBucket-&gt;getFirst())))
  </code></pre>
  
  <p>I want to know, whether the way of cloning functions is correct. If it is, then find the reason behind the crashes.</p>