Revert "Remove access to the DataLayout in the TargetMachine"
authorMehdi Amini <mehdi.amini@apple.com>
Fri, 24 Jul 2015 03:36:55 +0000 (03:36 +0000)
committerMehdi Amini <mehdi.amini@apple.com>
Fri, 24 Jul 2015 03:36:55 +0000 (03:36 +0000)
This reverts commit 0f720d984f419c747709462f7476dff962c0bc41.

It breaks clang too badly, I need to prepare a proper patch for clang
first.

From: Mehdi Amini <mehdi.amini@apple.com>

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@243089 91177308-0d34-0410-b5e6-96231b3b80d8

17 files changed:
examples/Kaleidoscope/Orc/fully_lazy/toy.cpp
examples/Kaleidoscope/Orc/initial/toy.cpp
examples/Kaleidoscope/Orc/lazy_codegen/toy.cpp
examples/Kaleidoscope/Orc/lazy_irgen/toy.cpp
include/llvm/Target/TargetMachine.h
lib/CodeGen/AsmPrinter/AsmPrinter.cpp
lib/ExecutionEngine/MCJIT/MCJIT.cpp
lib/ExecutionEngine/Orc/OrcMCJITReplacement.h
lib/LTO/LTOCodeGenerator.cpp
lib/LTO/LTOModule.cpp
lib/Target/Sparc/SparcISelLowering.cpp
lib/Target/SystemZ/SystemZISelLowering.cpp
lib/Target/TargetMachineC.cpp
lib/Target/X86/X86ISelLowering.cpp
tools/llc/llc.cpp
tools/lli/OrcLazyJIT.cpp
tools/lli/OrcLazyJIT.h

index b68f807e3e7af29289e154baf9a565d82fad6dbc..c9b2c6af56588b625f3c2e68fb1579fb71b476e8 100644 (file)
@@ -717,7 +717,7 @@ public:
       M(new Module(GenerateUniqueName("jit_module_"),
                    Session.getLLVMContext())),
       Builder(Session.getLLVMContext()) {
-    M->setDataLayout(Session.getTarget().createDataLayout());
+    M->setDataLayout(*Session.getTarget().getDataLayout());
   }
 
   SessionContext& getSession() { return Session; }
@@ -1179,7 +1179,7 @@ public:
     {
       raw_string_ostream MangledNameStream(MangledName);
       Mangler::getNameWithPrefix(MangledNameStream, Name,
-                                 Session.getTarget().createDataLayout());
+                                 *Session.getTarget().getDataLayout());
     }
     return MangledName;
   }
index 91290157ace30f3d34c4e2150abd998eeb55d454..7e99c0f5ba545d64a80e6d5ccd8e795d4d0961fc 100644 (file)
@@ -716,7 +716,7 @@ public:
       M(new Module(GenerateUniqueName("jit_module_"),
                    Session.getLLVMContext())),
       Builder(Session.getLLVMContext()) {
-    M->setDataLayout(Session.getTarget().createDataLayout());
+    M->setDataLayout(*Session.getTarget().getDataLayout());
   }
 
   SessionContext& getSession() { return Session; }
@@ -1160,7 +1160,7 @@ public:
   typedef CompileLayerT::ModuleSetHandleT ModuleHandleT;
 
   KaleidoscopeJIT(SessionContext &Session)
-      : DL(Session.getTarget().createDataLayout()),
+      : DL(*Session.getTarget().getDataLayout()),
         CompileLayer(ObjectLayer, SimpleCompiler(Session.getTarget())) {}
 
   std::string mangle(const std::string &Name) {
@@ -1201,7 +1201,7 @@ public:
   }
 
 private:
-  const DataLayout DL;
+  const DataLayout &DL;
   ObjLayerT ObjectLayer;
   CompileLayerT CompileLayer;
 };
index 17f9dbb6dfa81184276ebb45dccac9e0fdd5c141..4b4c191171b48a7549cff880ccce15ee8ab925b0 100644 (file)
@@ -716,7 +716,7 @@ public:
       M(new Module(GenerateUniqueName("jit_module_"),
                    Session.getLLVMContext())),
       Builder(Session.getLLVMContext()) {
-    M->setDataLayout(Session.getTarget().createDataLayout());
+    M->setDataLayout(*Session.getTarget().getDataLayout());
   }
 
   SessionContext& getSession() { return Session; }
@@ -1162,7 +1162,7 @@ public:
   typedef LazyEmitLayerT::ModuleSetHandleT ModuleHandleT;
 
   KaleidoscopeJIT(SessionContext &Session)
-      : DL(Session.getTarget().createDataLayout()),
+      : DL(*Session.getTarget().getDataLayout()),
         CompileLayer(ObjectLayer, SimpleCompiler(Session.getTarget())),
         LazyEmitLayer(CompileLayer) {}
 
@@ -1204,7 +1204,7 @@ public:
   }
 
 private:
-  const DataLayout DL;
+  const DataLayout &DL;
   ObjLayerT ObjectLayer;
   CompileLayerT CompileLayer;
   LazyEmitLayerT LazyEmitLayer;
index 7373c99d543170c6df71b9cef3b7e4e5b3dec143..ca34de7e22449044c8c83a827468d12a8a55fece 100644 (file)
@@ -716,7 +716,7 @@ public:
       M(new Module(GenerateUniqueName("jit_module_"),
                    Session.getLLVMContext())),
       Builder(Session.getLLVMContext()) {
-    M->setDataLayout(Session.getTarget().createDataLayout());
+    M->setDataLayout(*Session.getTarget().getDataLayout());
   }
 
   SessionContext& getSession() { return Session; }
@@ -1170,7 +1170,7 @@ public:
     {
       raw_string_ostream MangledNameStream(MangledName);
       Mangler::getNameWithPrefix(MangledNameStream, Name,
-                                 Session.getTarget().createDataLayout());
+                                 *Session.getTarget().getDataLayout());
     }
     return MangledName;
   }
index bc8537c0daa74acd48956045f7d33b40e26325e4..06a2b13836ed6548561733fab16b490dcb8cd33c 100644 (file)
@@ -76,12 +76,7 @@ protected: // Can only create subclasses.
   /// The Target that this machine was created for.
   const Target &TheTarget;
 
-  /// 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.
+  /// For ABI type size and alignment.
   const DataLayout DL;
 
   /// Triple string, CPU name, and target feature strings the TargetMachine
@@ -130,13 +125,9 @@ public:
     return *static_cast<const STC*>(getSubtargetImpl(F));
   }
 
-  /// 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(); }
+  /// This method returns a pointer to the DataLayout for the target. It should
+  /// be unchanging for every subtarget.
+  const DataLayout *getDataLayout() const { return &DL; }
 
   /// \brief Reset the target options based on the function's attributes.
   // FIXME: Remove TargetOptions that affect per-function code generation
index 8a0854628a681548e9febd9d4112a1544f5f9bf2..ecdc7fa8b35ed840d9324c903b75243b2ad5e351 100644 (file)
@@ -139,9 +139,9 @@ const DataLayout &AsmPrinter::getDataLayout() const {
   return MMI->getModule()->getDataLayout();
 }
 
-// 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(); }
+unsigned AsmPrinter::getPointerSize() const {
+  return TM.getDataLayout()->getPointerSize();
+}
 
 const MCSubtargetInfo &AsmPrinter::getSubtargetInfo() const {
   assert(MF && "getSubtargetInfo requires a valid MachineFunction!");
index 492478da89f246cf7dd6ee944d8b074d3b98156e..5f4641515ce5641eec064edd86f5b62b971108e9 100644 (file)
@@ -68,7 +68,7 @@ MCJIT::createJIT(std::unique_ptr<Module> M,
 MCJIT::MCJIT(std::unique_ptr<Module> M, std::unique_ptr<TargetMachine> TM,
              std::shared_ptr<MCJITMemoryManager> MemMgr,
              std::shared_ptr<RuntimeDyld::SymbolResolver> Resolver)
-    : ExecutionEngine(TM->createDataLayout(), std::move(M)), TM(std::move(TM)),
+    : ExecutionEngine(*TM->getDataLayout(), std::move(M)), TM(std::move(TM)),
       Ctx(nullptr), MemMgr(std::move(MemMgr)),
       Resolver(*this, std::move(Resolver)), Dyld(*this->MemMgr, this->Resolver),
       ObjCache(nullptr) {
index 951993f75e4ceb2a0f173f579ce50e230079899d..0efbcbd915930d5f9e5c01fd04d5c22bc2fa5473 100644 (file)
@@ -140,7 +140,7 @@ public:
       std::shared_ptr<MCJITMemoryManager> MemMgr,
       std::shared_ptr<RuntimeDyld::SymbolResolver> ClientResolver,
       std::unique_ptr<TargetMachine> TM)
-      : ExecutionEngine(TM->createDataLayout()), TM(std::move(TM)),
+      : ExecutionEngine(*TM->getDataLayout()), TM(std::move(TM)),
         MemMgr(*this, std::move(MemMgr)), Resolver(*this),
         ClientResolver(std::move(ClientResolver)), NotifyObjectLoaded(*this),
         NotifyFinalized(*this),
index daaf22329223c32907b4b5f3472b5c1b154f31b3..149ec6a4f372549aacb65c4ea6ee6ae40c53c5ef 100644 (file)
@@ -521,7 +521,7 @@ bool LTOCodeGenerator::optimize(bool DisableInline,
   legacy::PassManager passes;
 
   // Add an appropriate DataLayout instance for this module...
-  mergedModule->setDataLayout(TargetMach->createDataLayout());
+  mergedModule->setDataLayout(*TargetMach->getDataLayout());
 
   passes.add(
       createTargetTransformInfoWrapperPass(TargetMach->getTargetIRAnalysis()));
index ec8991ed236531e5dfd3b2f641d904a2fd2f8c38..53ed4175f8e35463e9826858ff84efdf5046a6d8 100644 (file)
@@ -232,7 +232,7 @@ LTOModule *LTOModule::makeLTOModule(MemoryBufferRef Buffer,
 
   TargetMachine *target = march->createTargetMachine(TripleStr, CPU, FeatureStr,
                                                      options);
-  M->setDataLayout(target->createDataLayout());
+  M->setDataLayout(*target->getDataLayout());
 
   std::unique_ptr<object::IRObjectFile> IRObj(
       new object::IRObjectFile(Buffer, std::move(M)));
index 67918a7c319a58a8c7a8a3c31baf1f5c9adbf327..4879d4ee79e57e0cb2b0a6d98144a453f18569cb 100644 (file)
@@ -1370,7 +1370,7 @@ static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
 SparcTargetLowering::SparcTargetLowering(TargetMachine &TM,
                                          const SparcSubtarget &STI)
     : TargetLowering(TM), Subtarget(&STI) {
-  MVT PtrVT = MVT::getIntegerVT(8 * TM.getPointerSize());
+  auto &DL = *TM.getDataLayout();
 
   // Set up the register classes.
   addRegisterClass(MVT::i32, &SP::IntRegsRegClass);
@@ -1396,10 +1396,10 @@ SparcTargetLowering::SparcTargetLowering(TargetMachine &TM,
   setTruncStoreAction(MVT::f128, MVT::f64, Expand);
 
   // Custom legalize GlobalAddress nodes into LO/HI parts.
-  setOperationAction(ISD::GlobalAddress, PtrVT, Custom);
-  setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom);
-  setOperationAction(ISD::ConstantPool, PtrVT, Custom);
-  setOperationAction(ISD::BlockAddress, PtrVT, Custom);
+  setOperationAction(ISD::GlobalAddress, getPointerTy(DL), Custom);
+  setOperationAction(ISD::GlobalTLSAddress, getPointerTy(DL), Custom);
+  setOperationAction(ISD::ConstantPool, getPointerTy(DL), Custom);
+  setOperationAction(ISD::BlockAddress, getPointerTy(DL), Custom);
 
   // Sparc doesn't have sext_inreg, replace them with shl/sra
   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
index 3c5d185ffe89899aa609161004fe2a1bff00f6a1..056ee02dcc2110582fd5f5c6582769bd199e7e64 100644 (file)
@@ -84,7 +84,8 @@ static MachineOperand earlyUseOperand(MachineOperand Op) {
 SystemZTargetLowering::SystemZTargetLowering(const TargetMachine &TM,
                                              const SystemZSubtarget &STI)
     : TargetLowering(TM), Subtarget(STI) {
-  MVT PtrVT = MVT::getIntegerVT(8 * TM.getPointerSize());
+  auto &DL = *TM.getDataLayout();
+  MVT PtrVT = getPointerTy(DL);
 
   // Set up the register classes.
   if (Subtarget.hasHighWord())
index eae23e6e67fead4788cfe5e26c0f90ced1368bda..719923558de489f0431347404b9343a2f7567ee7 100644 (file)
 
 using namespace llvm;
 
-
-// The TargetMachine uses to offer access to a DataLayout member. This is reflected
-// in the C API. For backward compatibility reason, this structure allows to keep
-// a DataLayout member accessible to C client that have a handle to a
-// LLVMTargetMachineRef.
-struct LLVMOpaqueTargetMachine {
-  std::unique_ptr<TargetMachine> Machine;
-  DataLayout DL;
-};
-
-
 inline TargetMachine *unwrap(LLVMTargetMachineRef P) {
-  return P->Machine.get();
+  return reinterpret_cast<TargetMachine*>(P);
 }
 inline Target *unwrap(LLVMTargetRef P) {
   return reinterpret_cast<Target*>(P);
 }
 inline LLVMTargetMachineRef wrap(const TargetMachine *P) {
-  return new LLVMOpaqueTargetMachine{ std::unique_ptr<TargetMachine>(const_cast<TargetMachine*>(P)),  P->createDataLayout() };
+  return
+    reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine*>(P));
 }
 inline LLVMTargetRef wrap(const Target * P) {
   return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P));
@@ -157,7 +147,7 @@ LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T,
 
 
 void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) {
-  delete T;
+  delete unwrap(T);
 }
 
 LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) {
@@ -180,9 +170,8 @@ char* LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T) {
   return strdup(StringRep.c_str());
 }
 
-/// @deprecated: see "struct LLVMOpaqueTargetMachine" description above
 LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T) {
-  return wrap(&T->DL);
+  return wrap(unwrap(T)->getDataLayout());
 }
 
 void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T,
@@ -201,7 +190,14 @@ static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
 
   std::string error;
 
-  Mod->setDataLayout(TM->createDataLayout());
+  const DataLayout *td = TM->getDataLayout();
+
+  if (!td) {
+    error = "No DataLayout in TargetMachine";
+    *ErrorMessage = strdup(error.c_str());
+    return true;
+  }
+  Mod->setDataLayout(*td);
 
   TargetMachine::CodeGenFileType ft;
   switch (codegen) {
index ff0daf9eb714f91d9a137a26ee5474c19da309ef..4c09eaf12c8e4882f1f8adcd05dd10ef1d8920fd 100644 (file)
@@ -76,7 +76,7 @@ X86TargetLowering::X86TargetLowering(const X86TargetMachine &TM,
     : TargetLowering(TM), Subtarget(&STI) {
   X86ScalarSSEf64 = Subtarget->hasSSE2();
   X86ScalarSSEf32 = Subtarget->hasSSE1();
-  MVT PtrVT = MVT::getIntegerVT(8 * TM.getPointerSize());
+  TD = TM.getDataLayout();
 
   // Set up the TargetLowering object.
   static const MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
@@ -505,7 +505,7 @@ X86TargetLowering::X86TargetLowering(const X86TargetMachine &TM,
   setOperationAction(ISD::STACKSAVE,          MVT::Other, Expand);
   setOperationAction(ISD::STACKRESTORE,       MVT::Other, Expand);
 
-  setOperationAction(ISD::DYNAMIC_STACKALLOC, PtrVT, Custom);
+  setOperationAction(ISD::DYNAMIC_STACKALLOC, getPointerTy(*TD), Custom);
 
   // GC_TRANSITION_START and GC_TRANSITION_END need custom lowering.
   setOperationAction(ISD::GC_TRANSITION_START, MVT::Other, Custom);
@@ -16515,11 +16515,9 @@ SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
 
         for (FunctionType::param_iterator I = FTy->param_begin(),
              E = FTy->param_end(); I != E; ++I, ++Idx)
-          if (Attrs.hasAttribute(Idx, Attribute::InReg)) {
-            auto &DL = DAG.getDataLayout();
+          if (Attrs.hasAttribute(Idx, Attribute::InReg))
             // FIXME: should only count parameters that are lowered to integers.
-            InRegCount += (DL.getTypeSizeInBits(*I) + 31) / 32;
-          }
+            InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
 
         if (InRegCount > 2) {
           report_fatal_error("Nest register in use - reduce number of inreg"
index c51c012391b2e6fe422df4cc797653779a55cce9..e33cd795d3ae2d01b2fe708ad6e8c70d6779d16a 100644 (file)
@@ -312,7 +312,8 @@ static int compileModule(char **argv, LLVMContext &Context) {
   PM.add(new TargetLibraryInfoWrapperPass(TLII));
 
   // Add the target data from the target machine, if it exists, or the module.
-  M->setDataLayout(Target->createDataLayout());
+  if (const DataLayout *DL = Target->getDataLayout())
+    M->setDataLayout(*DL);
 
   // Override function attributes based on CPUStr, FeaturesStr, and command line
   // flags.
index 718b3903822215cd2f942e988768a439b0841d11..ae276e6cda8fbef4778cc82261978c429460e530 100644 (file)
@@ -136,8 +136,7 @@ int llvm::runOrcLazyJIT(std::unique_ptr<Module> M, int ArgC, char* ArgV[]) {
   }
 
   // Everything looks good. Build the JIT.
-  auto &DL = M->getDataLayout();
-  OrcLazyJIT J(std::move(TM), DL, Context, CallbackMgrBuilder);
+  OrcLazyJIT J(std::move(TM), Context, CallbackMgrBuilder);
 
   // Add the module, look up main and run it.
   auto MainHandle = J.addModule(std::move(M));
index 5019523c7f0e0d71f600364f885e76d6525a7147..fe86adbf95162f672398fceedbffafd45787c6a3 100644 (file)
@@ -46,17 +46,16 @@ public:
     CallbackManagerBuilder;
 
   static CallbackManagerBuilder createCallbackManagerBuilder(Triple T);
-  const DataLayout &DL;
-
-  OrcLazyJIT(std::unique_ptr<TargetMachine> 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(std::unique_ptr<TargetMachine> 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); }) {}
 
   ~OrcLazyJIT() {
     // Run any destructors registered with __cxa_atexit.
@@ -74,7 +73,7 @@ public:
   ModuleHandleT addModule(std::unique_ptr<Module> M) {
     // Attach a data-layout if one isn't already present.
     if (M->getDataLayout().isDefault())
-      M->setDataLayout(DL);
+      M->setDataLayout(*TM->getDataLayout());
 
     // Record the static constructors and destructors. We have to do this before
     // we hand over ownership of the module to the JIT.
@@ -132,11 +131,12 @@ public:
   }
 
 private:
+
   std::string mangle(const std::string &Name) {
     std::string MangledName;
     {
       raw_string_ostream MangledNameStream(MangledName);
-      Mangler::getNameWithPrefix(MangledNameStream, Name, DL);
+      Mangler::getNameWithPrefix(MangledNameStream, Name, *TM->getDataLayout());
     }
     return MangledName;
   }