Store a DataLayout in Module.

Now that DataLayout is not a pass, store one in Module.

Since the C API expects to be able to get a char* to the datalayout description,
we have to keep a std::string somewhere. This patch keeps it in Module and also
uses it to represent modules without a DataLayout.

Once DataLayout is mandatory, we should probably move the string to DataLayout
itself since it won't be necessary anymore to represent the special case of a
module without a DataLayout.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@202190 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Rafael Espindola 2014-02-25 20:01:08 +00:00
parent dbaa6ab8b5
commit aab87fe0ec
9 changed files with 62 additions and 24 deletions

View File

@ -16,6 +16,7 @@
#define LLVM_IR_MODULE_H
#include "llvm/ADT/OwningPtr.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalAlias.h"
#include "llvm/IR/GlobalVariable.h"
@ -199,9 +200,16 @@ private:
OwningPtr<GVMaterializer> Materializer; ///< Used to materialize GlobalValues
std::string ModuleID; ///< Human readable identifier for the module
std::string TargetTriple; ///< Platform target triple Module compiled on
std::string DataLayout; ///< Target data description
void *NamedMDSymTab; ///< NamedMDNode names.
// We need to keep the string because the C API expects us to own the string
// representation.
// Since we have it, we also use an empty string to represent a module without
// a DataLayout. If it has a DataLayout, these variables are in sync and the
// string is just a cache of getDataLayout()->getStringRepresentation().
std::string DataLayoutStr;
DataLayout DL;
friend class Constant;
/// @}
@ -222,10 +230,12 @@ public:
/// @returns the module identifier as a string
const std::string &getModuleIdentifier() const { return ModuleID; }
/// Get the data layout string for the module's target platform. This encodes
/// the type sizes and alignments expected by this module.
/// @returns the data layout as a string
const std::string &getDataLayout() const { return DataLayout; }
/// Get the data layout string for the module's target platform. This is
/// equivalent to getDataLayout()->getStringRepresentation().
const std::string &getDataLayoutStr() const { return DataLayoutStr; }
/// Get the data layout for the module's target platform.
const DataLayout *getDataLayout() const;
/// Get the target triple which is a string describing the target host.
/// @returns a string containing the target triple.
@ -247,7 +257,8 @@ public:
void setModuleIdentifier(StringRef ID) { ModuleID = ID; }
/// Set the data layout
void setDataLayout(StringRef DL) { DataLayout = DL; }
void setDataLayout(StringRef Desc);
void setDataLayout(const DataLayout *Other);
/// Set the target triple.
void setTargetTriple(StringRef T) { TargetTriple = T; }

View File

@ -530,9 +530,9 @@ static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE,
if (!M->getTargetTriple().empty())
WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(),
0/*TODO*/, Stream);
if (!M->getDataLayout().empty())
WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, M->getDataLayout(),
0/*TODO*/, Stream);
const std::string &DL = M->getDataLayoutStr();
if (!DL.empty())
WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, DL, 0 /*TODO*/, Stream);
if (!M->getModuleInlineAsm().empty())
WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(),
0/*TODO*/, Stream);

View File

@ -1252,8 +1252,9 @@ void AssemblyWriter::printModule(const Module *M) {
M->getModuleIdentifier().find('\n') == std::string::npos)
Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
if (!M->getDataLayout().empty())
Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
const std::string &DL = M->getDataLayoutStr();
if (!DL.empty())
Out << "target datalayout = \"" << DL << "\"\n";
if (!M->getTargetTriple().empty())
Out << "target triple = \"" << M->getTargetTriple() << "\"\n";

View File

@ -107,7 +107,7 @@ void LLVMDisposeModule(LLVMModuleRef M) {
/*--.. Data layout .........................................................--*/
const char * LLVMGetDataLayout(LLVMModuleRef M) {
return unwrap(M)->getDataLayout().c_str();
return unwrap(M)->getDataLayoutStr().c_str();
}
void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {

View File

@ -344,7 +344,13 @@ void DataLayout::parseSpecifier(StringRef Desc) {
}
}
DataLayout::DataLayout(const Module *M) { init(M->getDataLayout()); }
DataLayout::DataLayout(const Module *M) {
const DataLayout *Other = M->getDataLayout();
if (Other)
*this = *Other;
else
init("");
}
void
DataLayout::setAlignment(AlignTypeEnum align_type, unsigned abi_align,

View File

@ -42,8 +42,8 @@ template class llvm::SymbolTableListTraits<GlobalAlias, Module>;
// Primitive Module methods.
//
Module::Module(StringRef MID, LLVMContext& C)
: Context(C), Materializer(NULL), ModuleID(MID) {
Module::Module(StringRef MID, LLVMContext &C)
: Context(C), Materializer(NULL), ModuleID(MID), DL("") {
ValSymTab = new ValueSymbolTable();
NamedMDSymTab = new StringMap<NamedMDNode *>();
Context.addModule(this);
@ -338,6 +338,30 @@ void Module::addModuleFlag(MDNode *Node) {
getOrInsertModuleFlagsMetadata()->addOperand(Node);
}
void Module::setDataLayout(StringRef Desc) {
if (Desc.empty()) {
DataLayoutStr = "";
} else {
DL.init(Desc);
DataLayoutStr = DL.getStringRepresentation();
}
}
void Module::setDataLayout(const DataLayout *Other) {
if (!Other) {
DataLayoutStr = "";
} else {
DL = *Other;
DataLayoutStr = DL.getStringRepresentation();
}
}
const DataLayout *Module::getDataLayout() const {
if (DataLayoutStr.empty())
return 0;
return &DL;
}
//===----------------------------------------------------------------------===//
// Methods to control the materialization of GlobalValues in the Module.
//

View File

@ -1200,14 +1200,14 @@ bool ModuleLinker::run() {
// Inherit the target data from the source module if the destination module
// doesn't have one already.
if (DstM->getDataLayout().empty() && !SrcM->getDataLayout().empty())
if (!DstM->getDataLayout() && SrcM->getDataLayout())
DstM->setDataLayout(SrcM->getDataLayout());
// Copy the target triple from the source to dest if the dest's is empty.
if (DstM->getTargetTriple().empty() && !SrcM->getTargetTriple().empty())
DstM->setTargetTriple(SrcM->getTargetTriple());
if (!SrcM->getDataLayout().empty() && !DstM->getDataLayout().empty() &&
if (SrcM->getDataLayout() && DstM->getDataLayout() &&
SrcM->getDataLayout() != DstM->getDataLayout()) {
if (!SuppressWarnings) {
errs() << "WARNING: Linking two modules of different data layouts!\n";

View File

@ -1,8 +1,7 @@
; RUN: llc < %s
; PR1462
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-
v64:64:64-v128:128:128-a0:0:64"
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64"
target triple = "x86_64-unknown-linux-gnu"
define hidden i128 @__addvti3(i128 %a1, i128 %b2) {

View File

@ -429,11 +429,8 @@ int main(int argc, char **argv) {
Passes.add(TLI);
// Add an appropriate DataLayout instance for this module.
DataLayout *DL = 0;
const std::string &ModuleDataLayout = M.get()->getDataLayout();
if (!ModuleDataLayout.empty())
DL = new DataLayout(ModuleDataLayout);
else if (!DefaultDataLayout.empty())
const DataLayout *DL = M.get()->getDataLayout();
if (!DL && !DefaultDataLayout.empty())
DL = new DataLayout(DefaultDataLayout);
if (DL)