diff --git a/lib/ExecutionEngine/ExecutionEngine.cpp b/lib/ExecutionEngine/ExecutionEngine.cpp index 8be399e510e..12e0e6aee6e 100644 --- a/lib/ExecutionEngine/ExecutionEngine.cpp +++ b/lib/ExecutionEngine/ExecutionEngine.cpp @@ -142,7 +142,8 @@ bool ExecutionEngine::removeModule(Module *M) { Function *ExecutionEngine::FindFunctionNamed(const char *FnName) { for (unsigned i = 0, e = Modules.size(); i != e; ++i) { - if (Function *F = Modules[i]->getFunction(FnName)) + Function *F = Modules[i]->getFunction(FnName); + if (F && !F->isDeclaration()) return F; } return nullptr; diff --git a/lib/ExecutionEngine/MCJIT/MCJIT.cpp b/lib/ExecutionEngine/MCJIT/MCJIT.cpp index f41f6b4fcf6..b0cfb2c827f 100644 --- a/lib/ExecutionEngine/MCJIT/MCJIT.cpp +++ b/lib/ExecutionEngine/MCJIT/MCJIT.cpp @@ -408,7 +408,8 @@ Function *MCJIT::FindFunctionNamedInModulePtrSet(const char *FnName, ModulePtrSet::iterator I, ModulePtrSet::iterator E) { for (; I != E; ++I) { - if (Function *F = (*I)->getFunction(FnName)) + Function *F = (*I)->getFunction(FnName); + if (F && !F->isDeclaration()) return F; } return nullptr; diff --git a/unittests/ExecutionEngine/MCJIT/MCJITMultipleModuleTest.cpp b/unittests/ExecutionEngine/MCJIT/MCJITMultipleModuleTest.cpp index b0d1bb39c7f..da6e25a3d51 100644 --- a/unittests/ExecutionEngine/MCJIT/MCJITMultipleModuleTest.cpp +++ b/unittests/ExecutionEngine/MCJIT/MCJITMultipleModuleTest.cpp @@ -392,4 +392,23 @@ TEST_F(MCJITMultipleModuleTest, cross_module_dependency_case3) { ptr = TheJIT->getFunctionAddress(FB2->getName().str()); checkAccumulate(ptr); } + +// Test that FindFunctionNamed finds the definition of +// a function in the correct module. We check two functions +// in two different modules, to make sure that for at least +// one of them MCJIT had to ignore the extern declaration. +TEST_F(MCJITMultipleModuleTest, FindFunctionNamed_test) { + SKIP_UNSUPPORTED_PLATFORM; + + std::unique_ptr A, B; + Function *FA, *FB1, *FB2; + createCrossModuleRecursiveCase(A, FA, B, FB1, FB2); + + createJIT(std::move(A)); + TheJIT->addModule(std::move(B)); + + EXPECT_EQ(FA, TheJIT->FindFunctionNamed(FA->getName().data())); + EXPECT_EQ(FB1, TheJIT->FindFunctionNamed(FB1->getName().data())); +} + }