LLVM API Documentation
00001 //===-- MCJIT.cpp - MC-based Just-in-Time Compiler ------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 00010 #include "MCJIT.h" 00011 #include "llvm/ExecutionEngine/GenericValue.h" 00012 #include "llvm/ExecutionEngine/JITEventListener.h" 00013 #include "llvm/ExecutionEngine/JITMemoryManager.h" 00014 #include "llvm/ExecutionEngine/MCJIT.h" 00015 #include "llvm/ExecutionEngine/ObjectBuffer.h" 00016 #include "llvm/ExecutionEngine/ObjectImage.h" 00017 #include "llvm/ExecutionEngine/SectionMemoryManager.h" 00018 #include "llvm/IR/DataLayout.h" 00019 #include "llvm/IR/DerivedTypes.h" 00020 #include "llvm/IR/Function.h" 00021 #include "llvm/IR/Mangler.h" 00022 #include "llvm/IR/Module.h" 00023 #include "llvm/MC/MCAsmInfo.h" 00024 #include "llvm/Object/Archive.h" 00025 #include "llvm/PassManager.h" 00026 #include "llvm/Support/DynamicLibrary.h" 00027 #include "llvm/Support/ErrorHandling.h" 00028 #include "llvm/Support/MemoryBuffer.h" 00029 #include "llvm/Support/MutexGuard.h" 00030 #include "llvm/Target/TargetLowering.h" 00031 #include "llvm/Target/TargetSubtargetInfo.h" 00032 00033 using namespace llvm; 00034 00035 namespace { 00036 00037 static struct RegisterJIT { 00038 RegisterJIT() { MCJIT::Register(); } 00039 } JITRegistrator; 00040 00041 } 00042 00043 extern "C" void LLVMLinkInMCJIT() { 00044 } 00045 00046 ExecutionEngine *MCJIT::createJIT(std::unique_ptr<Module> M, 00047 std::string *ErrorStr, 00048 RTDyldMemoryManager *MemMgr, 00049 std::unique_ptr<TargetMachine> TM) { 00050 // Try to register the program as a source of symbols to resolve against. 00051 // 00052 // FIXME: Don't do this here. 00053 sys::DynamicLibrary::LoadLibraryPermanently(nullptr, nullptr); 00054 00055 return new MCJIT(std::move(M), std::move(TM), 00056 MemMgr ? MemMgr : new SectionMemoryManager()); 00057 } 00058 00059 MCJIT::MCJIT(std::unique_ptr<Module> M, std::unique_ptr<TargetMachine> tm, 00060 RTDyldMemoryManager *MM) 00061 : ExecutionEngine(std::move(M)), TM(std::move(tm)), Ctx(nullptr), 00062 MemMgr(this, MM), Dyld(&MemMgr), ObjCache(nullptr) { 00063 // FIXME: We are managing our modules, so we do not want the base class 00064 // ExecutionEngine to manage them as well. To avoid double destruction 00065 // of the first (and only) module added in ExecutionEngine constructor 00066 // we remove it from EE and will destruct it ourselves. 00067 // 00068 // It may make sense to move our module manager (based on SmallStPtr) back 00069 // into EE if the JIT and Interpreter can live with it. 00070 // If so, additional functions: addModule, removeModule, FindFunctionNamed, 00071 // runStaticConstructorsDestructors could be moved back to EE as well. 00072 // 00073 std::unique_ptr<Module> First = std::move(Modules[0]); 00074 Modules.clear(); 00075 00076 OwnedModules.addModule(std::move(First)); 00077 setDataLayout(TM->getSubtargetImpl()->getDataLayout()); 00078 } 00079 00080 MCJIT::~MCJIT() { 00081 MutexGuard locked(lock); 00082 00083 Dyld.deregisterEHFrames(); 00084 00085 for (auto &Obj : LoadedObjects) 00086 if (Obj) 00087 NotifyFreeingObject(*Obj); 00088 00089 Archives.clear(); 00090 } 00091 00092 void MCJIT::addModule(std::unique_ptr<Module> M) { 00093 MutexGuard locked(lock); 00094 OwnedModules.addModule(std::move(M)); 00095 } 00096 00097 bool MCJIT::removeModule(Module *M) { 00098 MutexGuard locked(lock); 00099 return OwnedModules.removeModule(M); 00100 } 00101 00102 void MCJIT::addObjectFile(std::unique_ptr<object::ObjectFile> Obj) { 00103 std::unique_ptr<ObjectImage> LoadedObject = Dyld.loadObject(std::move(Obj)); 00104 if (!LoadedObject || Dyld.hasError()) 00105 report_fatal_error(Dyld.getErrorString()); 00106 00107 NotifyObjectEmitted(*LoadedObject); 00108 00109 LoadedObjects.push_back(std::move(LoadedObject)); 00110 } 00111 00112 void MCJIT::addObjectFile(object::OwningBinary<object::ObjectFile> Obj) { 00113 addObjectFile(std::move(Obj.getBinary())); 00114 Buffers.push_back(std::move(Obj.getBuffer())); 00115 } 00116 00117 void MCJIT::addArchive(object::OwningBinary<object::Archive> A) { 00118 Archives.push_back(std::move(A)); 00119 } 00120 00121 void MCJIT::setObjectCache(ObjectCache* NewCache) { 00122 MutexGuard locked(lock); 00123 ObjCache = NewCache; 00124 } 00125 00126 std::unique_ptr<ObjectBufferStream> MCJIT::emitObject(Module *M) { 00127 MutexGuard locked(lock); 00128 00129 // This must be a module which has already been added but not loaded to this 00130 // MCJIT instance, since these conditions are tested by our caller, 00131 // generateCodeForModule. 00132 00133 PassManager PM; 00134 00135 M->setDataLayout(TM->getSubtargetImpl()->getDataLayout()); 00136 PM.add(new DataLayoutPass()); 00137 00138 // The RuntimeDyld will take ownership of this shortly 00139 std::unique_ptr<ObjectBufferStream> CompiledObject(new ObjectBufferStream()); 00140 00141 // Turn the machine code intermediate representation into bytes in memory 00142 // that may be executed. 00143 if (TM->addPassesToEmitMC(PM, Ctx, CompiledObject->getOStream(), 00144 !getVerifyModules())) { 00145 report_fatal_error("Target does not support MC emission!"); 00146 } 00147 00148 // Initialize passes. 00149 PM.run(*M); 00150 // Flush the output buffer to get the generated code into memory 00151 CompiledObject->flush(); 00152 00153 // If we have an object cache, tell it about the new object. 00154 // Note that we're using the compiled image, not the loaded image (as below). 00155 if (ObjCache) { 00156 // MemoryBuffer is a thin wrapper around the actual memory, so it's OK 00157 // to create a temporary object here and delete it after the call. 00158 MemoryBufferRef MB = CompiledObject->getMemBuffer(); 00159 ObjCache->notifyObjectCompiled(M, MB); 00160 } 00161 00162 return CompiledObject; 00163 } 00164 00165 void MCJIT::generateCodeForModule(Module *M) { 00166 // Get a thread lock to make sure we aren't trying to load multiple times 00167 MutexGuard locked(lock); 00168 00169 // This must be a module which has already been added to this MCJIT instance. 00170 assert(OwnedModules.ownsModule(M) && 00171 "MCJIT::generateCodeForModule: Unknown module."); 00172 00173 // Re-compilation is not supported 00174 if (OwnedModules.hasModuleBeenLoaded(M)) 00175 return; 00176 00177 std::unique_ptr<ObjectBuffer> ObjectToLoad; 00178 // Try to load the pre-compiled object from cache if possible 00179 if (ObjCache) { 00180 if (std::unique_ptr<MemoryBuffer> PreCompiledObject = 00181 ObjCache->getObject(M)) 00182 ObjectToLoad = 00183 llvm::make_unique<ObjectBuffer>(std::move(PreCompiledObject)); 00184 } 00185 00186 // If the cache did not contain a suitable object, compile the object 00187 if (!ObjectToLoad) { 00188 ObjectToLoad = emitObject(M); 00189 assert(ObjectToLoad && "Compilation did not produce an object."); 00190 } 00191 00192 // Load the object into the dynamic linker. 00193 // MCJIT now owns the ObjectImage pointer (via its LoadedObjects list). 00194 std::unique_ptr<ObjectImage> LoadedObject = 00195 Dyld.loadObject(std::move(ObjectToLoad)); 00196 if (!LoadedObject) 00197 report_fatal_error(Dyld.getErrorString()); 00198 00199 // FIXME: Make this optional, maybe even move it to a JIT event listener 00200 LoadedObject->registerWithDebugger(); 00201 00202 NotifyObjectEmitted(*LoadedObject); 00203 00204 LoadedObjects.push_back(std::move(LoadedObject)); 00205 00206 OwnedModules.markModuleAsLoaded(M); 00207 } 00208 00209 void MCJIT::finalizeLoadedModules() { 00210 MutexGuard locked(lock); 00211 00212 // Resolve any outstanding relocations. 00213 Dyld.resolveRelocations(); 00214 00215 OwnedModules.markAllLoadedModulesAsFinalized(); 00216 00217 // Register EH frame data for any module we own which has been loaded 00218 Dyld.registerEHFrames(); 00219 00220 // Set page permissions. 00221 MemMgr.finalizeMemory(); 00222 } 00223 00224 // FIXME: Rename this. 00225 void MCJIT::finalizeObject() { 00226 MutexGuard locked(lock); 00227 00228 // Generate code for module is going to move objects out of the 'added' list, 00229 // so we need to copy that out before using it: 00230 SmallVector<Module*, 16> ModsToAdd; 00231 for (auto M : OwnedModules.added()) 00232 ModsToAdd.push_back(M); 00233 00234 for (auto M : ModsToAdd) 00235 generateCodeForModule(M); 00236 00237 finalizeLoadedModules(); 00238 } 00239 00240 void MCJIT::finalizeModule(Module *M) { 00241 MutexGuard locked(lock); 00242 00243 // This must be a module which has already been added to this MCJIT instance. 00244 assert(OwnedModules.ownsModule(M) && "MCJIT::finalizeModule: Unknown module."); 00245 00246 // If the module hasn't been compiled, just do that. 00247 if (!OwnedModules.hasModuleBeenLoaded(M)) 00248 generateCodeForModule(M); 00249 00250 finalizeLoadedModules(); 00251 } 00252 00253 uint64_t MCJIT::getExistingSymbolAddress(const std::string &Name) { 00254 Mangler Mang(TM->getSubtargetImpl()->getDataLayout()); 00255 SmallString<128> FullName; 00256 Mang.getNameWithPrefix(FullName, Name); 00257 return Dyld.getSymbolLoadAddress(FullName); 00258 } 00259 00260 Module *MCJIT::findModuleForSymbol(const std::string &Name, 00261 bool CheckFunctionsOnly) { 00262 MutexGuard locked(lock); 00263 00264 // If it hasn't already been generated, see if it's in one of our modules. 00265 for (ModulePtrSet::iterator I = OwnedModules.begin_added(), 00266 E = OwnedModules.end_added(); 00267 I != E; ++I) { 00268 Module *M = *I; 00269 Function *F = M->getFunction(Name); 00270 if (F && !F->isDeclaration()) 00271 return M; 00272 if (!CheckFunctionsOnly) { 00273 GlobalVariable *G = M->getGlobalVariable(Name); 00274 if (G && !G->isDeclaration()) 00275 return M; 00276 // FIXME: Do we need to worry about global aliases? 00277 } 00278 } 00279 // We didn't find the symbol in any of our modules. 00280 return nullptr; 00281 } 00282 00283 uint64_t MCJIT::getSymbolAddress(const std::string &Name, 00284 bool CheckFunctionsOnly) 00285 { 00286 MutexGuard locked(lock); 00287 00288 // First, check to see if we already have this symbol. 00289 uint64_t Addr = getExistingSymbolAddress(Name); 00290 if (Addr) 00291 return Addr; 00292 00293 for (object::OwningBinary<object::Archive> &OB : Archives) { 00294 object::Archive *A = OB.getBinary().get(); 00295 // Look for our symbols in each Archive 00296 object::Archive::child_iterator ChildIt = A->findSym(Name); 00297 if (ChildIt != A->child_end()) { 00298 // FIXME: Support nested archives? 00299 ErrorOr<std::unique_ptr<object::Binary>> ChildBinOrErr = 00300 ChildIt->getAsBinary(); 00301 if (ChildBinOrErr.getError()) 00302 continue; 00303 std::unique_ptr<object::Binary> &ChildBin = ChildBinOrErr.get(); 00304 if (ChildBin->isObject()) { 00305 std::unique_ptr<object::ObjectFile> OF( 00306 static_cast<object::ObjectFile *>(ChildBin.release())); 00307 // This causes the object file to be loaded. 00308 addObjectFile(std::move(OF)); 00309 // The address should be here now. 00310 Addr = getExistingSymbolAddress(Name); 00311 if (Addr) 00312 return Addr; 00313 } 00314 } 00315 } 00316 00317 // If it hasn't already been generated, see if it's in one of our modules. 00318 Module *M = findModuleForSymbol(Name, CheckFunctionsOnly); 00319 if (M) { 00320 generateCodeForModule(M); 00321 00322 // Check the RuntimeDyld table again, it should be there now. 00323 return getExistingSymbolAddress(Name); 00324 } 00325 00326 // If a LazyFunctionCreator is installed, use it to get/create the function. 00327 // FIXME: Should we instead have a LazySymbolCreator callback? 00328 if (LazyFunctionCreator) 00329 Addr = (uint64_t)LazyFunctionCreator(Name); 00330 00331 return Addr; 00332 } 00333 00334 uint64_t MCJIT::getGlobalValueAddress(const std::string &Name) { 00335 MutexGuard locked(lock); 00336 uint64_t Result = getSymbolAddress(Name, false); 00337 if (Result != 0) 00338 finalizeLoadedModules(); 00339 return Result; 00340 } 00341 00342 uint64_t MCJIT::getFunctionAddress(const std::string &Name) { 00343 MutexGuard locked(lock); 00344 uint64_t Result = getSymbolAddress(Name, true); 00345 if (Result != 0) 00346 finalizeLoadedModules(); 00347 return Result; 00348 } 00349 00350 // Deprecated. Use getFunctionAddress instead. 00351 void *MCJIT::getPointerToFunction(Function *F) { 00352 MutexGuard locked(lock); 00353 00354 if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) { 00355 bool AbortOnFailure = !F->hasExternalWeakLinkage(); 00356 void *Addr = getPointerToNamedFunction(F->getName(), AbortOnFailure); 00357 addGlobalMapping(F, Addr); 00358 return Addr; 00359 } 00360 00361 Module *M = F->getParent(); 00362 bool HasBeenAddedButNotLoaded = OwnedModules.hasModuleBeenAddedButNotLoaded(M); 00363 00364 // Make sure the relevant module has been compiled and loaded. 00365 if (HasBeenAddedButNotLoaded) 00366 generateCodeForModule(M); 00367 else if (!OwnedModules.hasModuleBeenLoaded(M)) 00368 // If this function doesn't belong to one of our modules, we're done. 00369 return nullptr; 00370 00371 // FIXME: Should the Dyld be retaining module information? Probably not. 00372 // 00373 // This is the accessor for the target address, so make sure to check the 00374 // load address of the symbol, not the local address. 00375 Mangler Mang(TM->getSubtargetImpl()->getDataLayout()); 00376 SmallString<128> Name; 00377 TM->getNameWithPrefix(Name, F, Mang); 00378 return (void*)Dyld.getSymbolLoadAddress(Name); 00379 } 00380 00381 void MCJIT::runStaticConstructorsDestructorsInModulePtrSet( 00382 bool isDtors, ModulePtrSet::iterator I, ModulePtrSet::iterator E) { 00383 for (; I != E; ++I) { 00384 ExecutionEngine::runStaticConstructorsDestructors(**I, isDtors); 00385 } 00386 } 00387 00388 void MCJIT::runStaticConstructorsDestructors(bool isDtors) { 00389 // Execute global ctors/dtors for each module in the program. 00390 runStaticConstructorsDestructorsInModulePtrSet( 00391 isDtors, OwnedModules.begin_added(), OwnedModules.end_added()); 00392 runStaticConstructorsDestructorsInModulePtrSet( 00393 isDtors, OwnedModules.begin_loaded(), OwnedModules.end_loaded()); 00394 runStaticConstructorsDestructorsInModulePtrSet( 00395 isDtors, OwnedModules.begin_finalized(), OwnedModules.end_finalized()); 00396 } 00397 00398 Function *MCJIT::FindFunctionNamedInModulePtrSet(const char *FnName, 00399 ModulePtrSet::iterator I, 00400 ModulePtrSet::iterator E) { 00401 for (; I != E; ++I) { 00402 if (Function *F = (*I)->getFunction(FnName)) 00403 return F; 00404 } 00405 return nullptr; 00406 } 00407 00408 Function *MCJIT::FindFunctionNamed(const char *FnName) { 00409 Function *F = FindFunctionNamedInModulePtrSet( 00410 FnName, OwnedModules.begin_added(), OwnedModules.end_added()); 00411 if (!F) 00412 F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_loaded(), 00413 OwnedModules.end_loaded()); 00414 if (!F) 00415 F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_finalized(), 00416 OwnedModules.end_finalized()); 00417 return F; 00418 } 00419 00420 GenericValue MCJIT::runFunction(Function *F, 00421 const std::vector<GenericValue> &ArgValues) { 00422 assert(F && "Function *F was null at entry to run()"); 00423 00424 void *FPtr = getPointerToFunction(F); 00425 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction"); 00426 FunctionType *FTy = F->getFunctionType(); 00427 Type *RetTy = FTy->getReturnType(); 00428 00429 assert((FTy->getNumParams() == ArgValues.size() || 00430 (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) && 00431 "Wrong number of arguments passed into function!"); 00432 assert(FTy->getNumParams() == ArgValues.size() && 00433 "This doesn't support passing arguments through varargs (yet)!"); 00434 00435 // Handle some common cases first. These cases correspond to common `main' 00436 // prototypes. 00437 if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) { 00438 switch (ArgValues.size()) { 00439 case 3: 00440 if (FTy->getParamType(0)->isIntegerTy(32) && 00441 FTy->getParamType(1)->isPointerTy() && 00442 FTy->getParamType(2)->isPointerTy()) { 00443 int (*PF)(int, char **, const char **) = 00444 (int(*)(int, char **, const char **))(intptr_t)FPtr; 00445 00446 // Call the function. 00447 GenericValue rv; 00448 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(), 00449 (char **)GVTOP(ArgValues[1]), 00450 (const char **)GVTOP(ArgValues[2]))); 00451 return rv; 00452 } 00453 break; 00454 case 2: 00455 if (FTy->getParamType(0)->isIntegerTy(32) && 00456 FTy->getParamType(1)->isPointerTy()) { 00457 int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr; 00458 00459 // Call the function. 00460 GenericValue rv; 00461 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(), 00462 (char **)GVTOP(ArgValues[1]))); 00463 return rv; 00464 } 00465 break; 00466 case 1: 00467 if (FTy->getNumParams() == 1 && 00468 FTy->getParamType(0)->isIntegerTy(32)) { 00469 GenericValue rv; 00470 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr; 00471 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue())); 00472 return rv; 00473 } 00474 break; 00475 } 00476 } 00477 00478 // Handle cases where no arguments are passed first. 00479 if (ArgValues.empty()) { 00480 GenericValue rv; 00481 switch (RetTy->getTypeID()) { 00482 default: llvm_unreachable("Unknown return type for function call!"); 00483 case Type::IntegerTyID: { 00484 unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth(); 00485 if (BitWidth == 1) 00486 rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)()); 00487 else if (BitWidth <= 8) 00488 rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)()); 00489 else if (BitWidth <= 16) 00490 rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)()); 00491 else if (BitWidth <= 32) 00492 rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)()); 00493 else if (BitWidth <= 64) 00494 rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)()); 00495 else 00496 llvm_unreachable("Integer types > 64 bits not supported"); 00497 return rv; 00498 } 00499 case Type::VoidTyID: 00500 rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)()); 00501 return rv; 00502 case Type::FloatTyID: 00503 rv.FloatVal = ((float(*)())(intptr_t)FPtr)(); 00504 return rv; 00505 case Type::DoubleTyID: 00506 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)(); 00507 return rv; 00508 case Type::X86_FP80TyID: 00509 case Type::FP128TyID: 00510 case Type::PPC_FP128TyID: 00511 llvm_unreachable("long double not supported yet"); 00512 case Type::PointerTyID: 00513 return PTOGV(((void*(*)())(intptr_t)FPtr)()); 00514 } 00515 } 00516 00517 llvm_unreachable("Full-featured argument passing not supported yet!"); 00518 } 00519 00520 void *MCJIT::getPointerToNamedFunction(StringRef Name, bool AbortOnFailure) { 00521 if (!isSymbolSearchingDisabled()) { 00522 void *ptr = MemMgr.getPointerToNamedFunction(Name, false); 00523 if (ptr) 00524 return ptr; 00525 } 00526 00527 /// If a LazyFunctionCreator is installed, use it to get/create the function. 00528 if (LazyFunctionCreator) 00529 if (void *RP = LazyFunctionCreator(Name)) 00530 return RP; 00531 00532 if (AbortOnFailure) { 00533 report_fatal_error("Program used external function '"+Name+ 00534 "' which could not be resolved!"); 00535 } 00536 return nullptr; 00537 } 00538 00539 void MCJIT::RegisterJITEventListener(JITEventListener *L) { 00540 if (!L) 00541 return; 00542 MutexGuard locked(lock); 00543 EventListeners.push_back(L); 00544 } 00545 void MCJIT::UnregisterJITEventListener(JITEventListener *L) { 00546 if (!L) 00547 return; 00548 MutexGuard locked(lock); 00549 auto I = std::find(EventListeners.rbegin(), EventListeners.rend(), L); 00550 if (I != EventListeners.rend()) { 00551 std::swap(*I, EventListeners.back()); 00552 EventListeners.pop_back(); 00553 } 00554 } 00555 void MCJIT::NotifyObjectEmitted(const ObjectImage& Obj) { 00556 MutexGuard locked(lock); 00557 MemMgr.notifyObjectLoaded(this, &Obj); 00558 for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) { 00559 EventListeners[I]->NotifyObjectEmitted(Obj); 00560 } 00561 } 00562 void MCJIT::NotifyFreeingObject(const ObjectImage& Obj) { 00563 MutexGuard locked(lock); 00564 for (JITEventListener *L : EventListeners) 00565 L->NotifyFreeingObject(Obj); 00566 } 00567 00568 uint64_t LinkingMemoryManager::getSymbolAddress(const std::string &Name) { 00569 uint64_t Result = ParentEngine->getSymbolAddress(Name, false); 00570 // If the symbols wasn't found and it begins with an underscore, try again 00571 // without the underscore. 00572 if (!Result && Name[0] == '_') 00573 Result = ParentEngine->getSymbolAddress(Name.substr(1), false); 00574 if (Result) 00575 return Result; 00576 if (ParentEngine->isSymbolSearchingDisabled()) 00577 return 0; 00578 return ClientMM->getSymbolAddress(Name); 00579 }