Skip to content

Commit 3393cfd

Browse files
committed
[MCJIT] Fix PR20656 by teaching MCJIT to honor ExecutionEngine's global mapping.
This is important for users of the C API who can't supply custom symbol resolvers yet. llvm-svn: 243589
1 parent e0d68e3 commit 3393cfd

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

llvm/lib/ExecutionEngine/ExecutionEngine.cpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,17 @@ uint64_t ExecutionEngineState::RemoveMapping(StringRef Name) {
189189
}
190190

191191
std::string ExecutionEngine::getMangledName(const GlobalValue *GV) {
192+
assert(GV->hasName() && "Global must have name.");
193+
192194
MutexGuard locked(lock);
193-
Mangler Mang;
194195
SmallString<128> FullName;
195-
Mang.getNameWithPrefix(FullName, GV, false);
196+
197+
const DataLayout &DL =
198+
GV->getParent()->getDataLayout().isDefault()
199+
? getDataLayout()
200+
: GV->getParent()->getDataLayout();
201+
202+
Mangler::getNameWithPrefix(FullName, GV->getName(), DL);
196203
return FullName.str();
197204
}
198205

llvm/lib/ExecutionEngine/MCJIT/MCJIT.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,12 @@ void MCJIT::finalizeModule(Module *M) {
270270
RuntimeDyld::SymbolInfo MCJIT::findExistingSymbol(const std::string &Name) {
271271
SmallString<128> FullName;
272272
Mangler::getNameWithPrefix(FullName, Name, getDataLayout());
273+
274+
if (void *Addr = getPointerToGlobalIfAvailable(FullName))
275+
return RuntimeDyld::SymbolInfo(static_cast<uint64_t>(
276+
reinterpret_cast<uintptr_t>(Addr)),
277+
JITSymbolFlags::Exported);
278+
273279
return Dyld.getSymbol(FullName);
274280
}
275281

llvm/unittests/ExecutionEngine/MCJIT/MCJITCAPITest.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -488,3 +488,36 @@ TEST_F(MCJITCAPITest, yield) {
488488
EXPECT_TRUE(didCallYield);
489489
}
490490

491+
static int localTestFunc() {
492+
return 42;
493+
}
494+
495+
TEST_F(MCJITCAPITest, addGlobalMapping) {
496+
SKIP_UNSUPPORTED_PLATFORM;
497+
498+
Module = LLVMModuleCreateWithName("testModule");
499+
LLVMTypeRef FunctionType = LLVMFunctionType(LLVMInt32Type(), NULL, 0, 0);
500+
LLVMValueRef MappedFn = LLVMAddFunction(Module, "mapped_fn", FunctionType);
501+
502+
Function = LLVMAddFunction(Module, "test_fn", FunctionType);
503+
LLVMBasicBlockRef Entry = LLVMAppendBasicBlock(Function, "");
504+
LLVMBuilderRef Builder = LLVMCreateBuilder();
505+
LLVMPositionBuilderAtEnd(Builder, Entry);
506+
LLVMValueRef RetVal = LLVMBuildCall(Builder, MappedFn, NULL, 0, "");
507+
LLVMBuildRet(Builder, RetVal);
508+
509+
LLVMVerifyModule(Module, LLVMAbortProcessAction, &Error);
510+
LLVMDisposeMessage(Error);
511+
512+
buildMCJITOptions();
513+
buildMCJITEngine();
514+
515+
LLVMAddGlobalMapping(Engine, MappedFn, reinterpret_cast<void*>(&localTestFunc));
516+
517+
buildAndRunPasses();
518+
519+
uint64_t raw = LLVMGetFunctionAddress(Engine, "test_fn");
520+
int (*usable)() = (int (*)()) raw;
521+
522+
EXPECT_EQ(42, usable());
523+
}

0 commit comments

Comments
 (0)