Index: include/llvm/ExecutionEngine/ExecutionEngine.h =================================================================== --- include/llvm/ExecutionEngine/ExecutionEngine.h +++ include/llvm/ExecutionEngine/ExecutionEngine.h @@ -165,6 +165,8 @@ /// Add a Module to the list of modules that we can JIT from. virtual void addModule(std::unique_ptr M) { + if(!DL) + DL = &M->getDataLayout(); Modules.push_back(std::move(M)); } Index: include/llvm/Target/TargetMachine.h =================================================================== --- include/llvm/Target/TargetMachine.h +++ include/llvm/Target/TargetMachine.h @@ -76,7 +76,12 @@ /// The Target that this machine was created for. const Target &TheTarget; - /// For ABI type size and alignment. + /// DataLayout for the target: keep ABI type size and alignment. + /// + /// The DataLayout is created based on the string representation provided + /// during construction. It is kept here only to avoid reparsing the string + /// but should not really be used during compilation, because it has an + /// internal cache that is context specific. const DataLayout DL; /// Triple string, CPU name, and target feature strings the TargetMachine @@ -125,9 +130,13 @@ return *static_cast(getSubtargetImpl(F)); } - /// This method returns a pointer to the DataLayout for the target. It should - /// be unchanging for every subtarget. - const DataLayout *getDataLayout() const { return &DL; } + /// Create a DataLayout. + const DataLayout createDataLayout() const { return DL; } + + /// Get the pointer size for this target. + /// + /// This is the only time the DataLayout in the TargetMachine is used. + unsigned getPointerSize() const { return DL.getPointerSize(); } /// \brief Reset the target options based on the function's attributes. // FIXME: Remove TargetOptions that affect per-function code generation Index: lib/CodeGen/AsmPrinter/AsmPrinter.cpp =================================================================== --- lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ lib/CodeGen/AsmPrinter/AsmPrinter.cpp @@ -139,9 +139,9 @@ return MMI->getModule()->getDataLayout(); } -unsigned AsmPrinter::getPointerSize() const { - return TM.getDataLayout()->getPointerSize(); -} +// Do not use the cached DataLayout because some client use it without a Module +// (llmv-dsymutil, llvm-dwarfdump). +unsigned AsmPrinter::getPointerSize() const { return TM.getPointerSize(); } const MCSubtargetInfo &AsmPrinter::getSubtargetInfo() const { assert(MF && "getSubtargetInfo requires a valid MachineFunction!"); Index: lib/ExecutionEngine/ExecutionEngine.cpp =================================================================== --- lib/ExecutionEngine/ExecutionEngine.cpp +++ lib/ExecutionEngine/ExecutionEngine.cpp @@ -62,7 +62,7 @@ void JITEventListener::anchor() {} ExecutionEngine::ExecutionEngine(std::unique_ptr M) - : LazyFunctionCreator(nullptr) { + : DL(&M->getDataLayout()), LazyFunctionCreator(nullptr) { CompilingLazily = false; GVCompilationDisabled = false; SymbolSearchingDisabled = false; @@ -434,6 +434,8 @@ !FTy->getReturnType()->isVoidTy()) report_fatal_error("Invalid return type of main() supplied"); + setDataLayout(&Fn->getParent()->getDataLayout()); + ArgvArray CArgv; ArgvArray CEnv; if (NumArgs) { Index: lib/ExecutionEngine/MCJIT/MCJIT.cpp =================================================================== --- lib/ExecutionEngine/MCJIT/MCJIT.cpp +++ lib/ExecutionEngine/MCJIT/MCJIT.cpp @@ -85,7 +85,6 @@ Modules.clear(); OwnedModules.addModule(std::move(First)); - setDataLayout(TM->getDataLayout()); RegisterJITEventListener(JITEventListener::createGDBRegistrationListener()); } @@ -193,7 +192,8 @@ if (ObjCache) ObjectToLoad = ObjCache->getObject(M); - M->setDataLayout(*TM->getDataLayout()); + M->setDataLayout(TM->createDataLayout()); + setDataLayout(&M->getDataLayout()); // If the cache did not contain a suitable object, compile the object if (!ObjectToLoad) { @@ -265,7 +265,7 @@ RuntimeDyld::SymbolInfo MCJIT::findExistingSymbol(const std::string &Name) { SmallString<128> FullName; - Mangler::getNameWithPrefix(FullName, Name, *TM->getDataLayout()); + Mangler::getNameWithPrefix(FullName, Name, *getDataLayout()); return Dyld.getSymbol(FullName); } Index: lib/ExecutionEngine/Orc/OrcMCJITReplacement.h =================================================================== --- lib/ExecutionEngine/Orc/OrcMCJITReplacement.h +++ lib/ExecutionEngine/Orc/OrcMCJITReplacement.h @@ -137,16 +137,16 @@ } OrcMCJITReplacement( - std::shared_ptr MemMgr, - std::shared_ptr ClientResolver, - std::unique_ptr TM) - : TM(std::move(TM)), MemMgr(*this, std::move(MemMgr)), - Resolver(*this), ClientResolver(std::move(ClientResolver)), - NotifyObjectLoaded(*this), NotifyFinalized(*this), + std::shared_ptr MemMgr, + std::shared_ptr ClientResolver, + std::unique_ptr TM) + : TM(std::move(TM)), + MemMgr(*this, std::move(MemMgr)), Resolver(*this), + ClientResolver(std::move(ClientResolver)), NotifyObjectLoaded(*this), + NotifyFinalized(*this), ObjectLayer(NotifyObjectLoaded, NotifyFinalized), CompileLayer(ObjectLayer, SimpleCompiler(*this->TM)), LazyEmitLayer(CompileLayer) { - setDataLayout(this->TM->getDataLayout()); } void addModule(std::unique_ptr M) override { @@ -154,7 +154,9 @@ // If this module doesn't have a DataLayout attached then attach the // default. if (M->getDataLayout().isDefault()) - M->setDataLayout(*getDataLayout()); + M->setDataLayout(TM->createDataLayout()); + if(!getDataLayout()) + setDataLayout(&M->getDataLayout()); Modules.push_back(std::move(M)); std::vector Ms; @@ -310,7 +312,7 @@ std::string MangledName; { raw_string_ostream MangledNameStream(MangledName); - Mang.getNameWithPrefix(MangledNameStream, Name, *TM->getDataLayout()); + Mang.getNameWithPrefix(MangledNameStream, Name, *getDataLayout()); } return MangledName; } Index: lib/ExecutionEngine/Orc/OrcMCJITReplacement.cpp =================================================================== --- lib/ExecutionEngine/Orc/OrcMCJITReplacement.cpp +++ lib/ExecutionEngine/Orc/OrcMCJITReplacement.cpp @@ -27,6 +27,7 @@ OrcMCJITReplacement::runFunction(Function *F, ArrayRef ArgValues) { assert(F && "Function *F was null at entry to run()"); + setDataLayout(&F->getParent()->getDataLayout()); void *FPtr = getPointerToFunction(F); assert(FPtr && "Pointer to fn's code was null after getPointerToFunction"); Index: lib/LTO/LTOCodeGenerator.cpp =================================================================== --- lib/LTO/LTOCodeGenerator.cpp +++ lib/LTO/LTOCodeGenerator.cpp @@ -521,7 +521,7 @@ legacy::PassManager passes; // Add an appropriate DataLayout instance for this module... - mergedModule->setDataLayout(*TargetMach->getDataLayout()); + mergedModule->setDataLayout(TargetMach->createDataLayout()); passes.add( createTargetTransformInfoWrapperPass(TargetMach->getTargetIRAnalysis())); Index: lib/LTO/LTOModule.cpp =================================================================== --- lib/LTO/LTOModule.cpp +++ lib/LTO/LTOModule.cpp @@ -232,7 +232,7 @@ TargetMachine *target = march->createTargetMachine(TripleStr, CPU, FeatureStr, options); - M->setDataLayout(*target->getDataLayout()); + M->setDataLayout(target->createDataLayout()); std::unique_ptr IRObj( new object::IRObjectFile(Buffer, std::move(M))); Index: lib/Target/Sparc/SparcISelLowering.cpp =================================================================== --- lib/Target/Sparc/SparcISelLowering.cpp +++ lib/Target/Sparc/SparcISelLowering.cpp @@ -1370,7 +1370,7 @@ SparcTargetLowering::SparcTargetLowering(TargetMachine &TM, const SparcSubtarget &STI) : TargetLowering(TM), Subtarget(&STI) { - auto &DL = *TM.getDataLayout(); + MVT PtrVT = MVT::getIntegerVT(8 * TM.getPointerSize()); // Set up the register classes. addRegisterClass(MVT::i32, &SP::IntRegsRegClass); @@ -1396,10 +1396,10 @@ setTruncStoreAction(MVT::f128, MVT::f64, Expand); // Custom legalize GlobalAddress nodes into LO/HI parts. - setOperationAction(ISD::GlobalAddress, getPointerTy(DL), Custom); - setOperationAction(ISD::GlobalTLSAddress, getPointerTy(DL), Custom); - setOperationAction(ISD::ConstantPool, getPointerTy(DL), Custom); - setOperationAction(ISD::BlockAddress, getPointerTy(DL), Custom); + setOperationAction(ISD::GlobalAddress, PtrVT, Custom); + setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom); + setOperationAction(ISD::ConstantPool, PtrVT, Custom); + setOperationAction(ISD::BlockAddress, PtrVT, Custom); // Sparc doesn't have sext_inreg, replace them with shl/sra setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand); Index: lib/Target/SystemZ/SystemZISelLowering.cpp =================================================================== --- lib/Target/SystemZ/SystemZISelLowering.cpp +++ lib/Target/SystemZ/SystemZISelLowering.cpp @@ -84,8 +84,7 @@ SystemZTargetLowering::SystemZTargetLowering(const TargetMachine &TM, const SystemZSubtarget &STI) : TargetLowering(TM), Subtarget(STI) { - auto &DL = *TM.getDataLayout(); - MVT PtrVT = getPointerTy(DL); + MVT PtrVT = MVT::getIntegerVT(8 * TM.getPointerSize()); // Set up the register classes. if (Subtarget.hasHighWord()) Index: lib/Target/TargetMachineC.cpp =================================================================== --- lib/Target/TargetMachineC.cpp +++ lib/Target/TargetMachineC.cpp @@ -171,7 +171,7 @@ } LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T) { - return wrap(unwrap(T)->getDataLayout()); + return nullptr; } void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T, @@ -190,14 +190,7 @@ std::string error; - const DataLayout *td = TM->getDataLayout(); - - if (!td) { - error = "No DataLayout in TargetMachine"; - *ErrorMessage = strdup(error.c_str()); - return true; - } - Mod->setDataLayout(*td); + Mod->setDataLayout(TM->createDataLayout()); TargetMachine::CodeGenFileType ft; switch (codegen) { Index: lib/Target/X86/X86ISelLowering.cpp =================================================================== --- lib/Target/X86/X86ISelLowering.cpp +++ lib/Target/X86/X86ISelLowering.cpp @@ -76,7 +76,7 @@ : TargetLowering(TM), Subtarget(&STI) { X86ScalarSSEf64 = Subtarget->hasSSE2(); X86ScalarSSEf32 = Subtarget->hasSSE1(); - auto &DL = *TM.getDataLayout(); + MVT PtrVT = MVT::getIntegerVT(8 * TM.getPointerSize()); // Set up the TargetLowering object. static const MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 }; @@ -505,7 +505,7 @@ setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); - setOperationAction(ISD::DYNAMIC_STACKALLOC, getPointerTy(DL), Custom); + setOperationAction(ISD::DYNAMIC_STACKALLOC, PtrVT, Custom); // GC_TRANSITION_START and GC_TRANSITION_END need custom lowering. setOperationAction(ISD::GC_TRANSITION_START, MVT::Other, Custom); Index: tools/llc/llc.cpp =================================================================== --- tools/llc/llc.cpp +++ tools/llc/llc.cpp @@ -312,8 +312,7 @@ PM.add(new TargetLibraryInfoWrapperPass(TLII)); // Add the target data from the target machine, if it exists, or the module. - if (const DataLayout *DL = Target->getDataLayout()) - M->setDataLayout(*DL); + M->setDataLayout(Target->createDataLayout()); // Override function attributes based on CPUStr, FeaturesStr, and command line // flags. Index: tools/lli/OrcLazyJIT.h =================================================================== --- tools/lli/OrcLazyJIT.h +++ tools/lli/OrcLazyJIT.h @@ -46,16 +46,17 @@ CallbackManagerBuilder; static CallbackManagerBuilder createCallbackManagerBuilder(Triple T); - - OrcLazyJIT(std::unique_ptr TM, LLVMContext &Context, - CallbackManagerBuilder &BuildCallbackMgr) - : TM(std::move(TM)), - ObjectLayer(), - CompileLayer(ObjectLayer, orc::SimpleCompiler(*this->TM)), - IRDumpLayer(CompileLayer, createDebugDumper()), - CCMgr(BuildCallbackMgr(IRDumpLayer, CCMgrMemMgr, Context)), - CODLayer(IRDumpLayer, *CCMgr, false), - CXXRuntimeOverrides([this](const std::string &S) { return mangle(S); }) {} + const DataLayout &DL; + + OrcLazyJIT(std::unique_ptr TM, const DataLayout &DL, + LLVMContext &Context, CallbackManagerBuilder &BuildCallbackMgr) + : DL(DL), TM(std::move(TM)), ObjectLayer(), + CompileLayer(ObjectLayer, orc::SimpleCompiler(*this->TM)), + IRDumpLayer(CompileLayer, createDebugDumper()), + CCMgr(BuildCallbackMgr(IRDumpLayer, CCMgrMemMgr, Context)), + CODLayer(IRDumpLayer, *CCMgr, false), + CXXRuntimeOverrides( + [this](const std::string &S) { return mangle(S); }) {} ~OrcLazyJIT() { // Run any destructors registered with __cxa_atexit. @@ -73,7 +74,7 @@ ModuleHandleT addModule(std::unique_ptr M) { // Attach a data-layout if one isn't already present. if (M->getDataLayout().isDefault()) - M->setDataLayout(*TM->getDataLayout()); + M->setDataLayout(DL); // Record the static constructors and destructors. We have to do this before // we hand over ownership of the module to the JIT. @@ -131,12 +132,11 @@ } private: - std::string mangle(const std::string &Name) { std::string MangledName; { raw_string_ostream MangledNameStream(MangledName); - Mangler::getNameWithPrefix(MangledNameStream, Name, *TM->getDataLayout()); + Mangler::getNameWithPrefix(MangledNameStream, Name, DL); } return MangledName; } Index: tools/lli/OrcLazyJIT.cpp =================================================================== --- tools/lli/OrcLazyJIT.cpp +++ tools/lli/OrcLazyJIT.cpp @@ -136,7 +136,8 @@ } // Everything looks good. Build the JIT. - OrcLazyJIT J(std::move(TM), Context, CallbackMgrBuilder); + auto &DL = M->getDataLayout(); + OrcLazyJIT J(std::move(TM), DL, Context, CallbackMgrBuilder); // Add the module, look up main and run it. auto MainHandle = J.addModule(std::move(M));