Index: include/llvm-c/Target.h =================================================================== --- include/llvm-c/Target.h +++ include/llvm-c/Target.h @@ -183,6 +183,20 @@ /*===-- Target Data -------------------------------------------------------===*/ +/** + * Obtain the data layout for a module. + * + * @see Module::getDataLayout() + */ +LLVMTargetDataRef LLVMGetModuleDataLayout(LLVMModuleRef M); + +/** + * Set the data layout for a module. + * + * @see Module::setDataLayout() + */ +void LLVMSetModuleDataLayout(LLVMModuleRef M, LLVMTargetDataRef DL); + /** Creates target data from a target layout string. See the constructor llvm::DataLayout::DataLayout. */ LLVMTargetDataRef LLVMCreateTargetData(const char *StringRep); Index: include/llvm-c/TargetMachine.h =================================================================== --- include/llvm-c/TargetMachine.h +++ include/llvm-c/TargetMachine.h @@ -115,6 +115,9 @@ LLVMDisposeMessage. */ char *LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T); +/** Create a DataLayout based on the targetMachine. */ +LLVMTargetDataRef LLVMCreateTargetDataLayout(LLVMTargetMachineRef T); + /** Set the target machine's ASM verbosity. */ void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T, LLVMBool VerboseAsm); Index: lib/Target/Target.cpp =================================================================== --- lib/Target/Target.cpp +++ lib/Target/Target.cpp @@ -42,6 +42,14 @@ initializeTarget(*unwrap(R)); } +LLVMTargetDataRef LLVMGetModuleDataLayout(LLVMModuleRef M) { + return wrap(&unwrap(M)->getDataLayout()); +} + +void LLVMSetModuleDataLayout(LLVMModuleRef M, LLVMTargetDataRef DL) { + unwrap(M)->setDataLayout(*unwrap(DL)); +} + LLVMTargetDataRef LLVMCreateTargetData(const char *StringRep) { return wrap(new DataLayout(StringRep)); } Index: lib/Target/TargetMachineC.cpp =================================================================== --- lib/Target/TargetMachineC.cpp +++ lib/Target/TargetMachineC.cpp @@ -171,6 +171,10 @@ unwrap(T)->Options.MCOptions.AsmVerbose = VerboseAsm; } +LLVMTargetDataRef LLVMCreateTargetDataLayout(LLVMTargetMachineRef T) { + return wrap(new DataLayout(unwrap(T)->createDataLayout())); +} + static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M, raw_pwrite_stream &OS, LLVMCodeGenFileType codegen, Index: test/Bindings/llvm-c/echo.ll =================================================================== --- test/Bindings/llvm-c/echo.ll +++ test/Bindings/llvm-c/echo.ll @@ -2,6 +2,9 @@ ; RUN: llvm-as < %s | llvm-c-test --echo > %t.echo ; RUN: diff -w %t.orig %t.echo +target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-apple-macosx10.11.0" + %S = type { i64, %S* } define { i64, %S* } @unpackrepack(%S %s) { Index: tools/llvm-c-test/echo.cpp =================================================================== --- tools/llvm-c-test/echo.cpp +++ tools/llvm-c-test/echo.cpp @@ -16,6 +16,7 @@ //===----------------------------------------------------------------------===// #include "llvm-c-test.h" +#include "llvm-c/Target.h" #include "llvm/ADT/DenseMap.h" #include "llvm/Support/ErrorHandling.h" @@ -629,6 +630,11 @@ LLVMContextRef Ctx = LLVMContextCreate(); LLVMModuleRef M = LLVMModuleCreateWithNameInContext("", Ctx); + LLVMSetTarget(M, LLVMGetTarget(Src)); + LLVMSetModuleDataLayout(M, LLVMGetModuleDataLayout(Src)); + if (strcmp(LLVMGetDataLayoutStr(M), LLVMGetDataLayoutStr(Src))) + report_fatal_error("Inconsistent DataLayout string representation"); + clone_functions(Src, M); char *Str = LLVMPrintModuleToString(M); fputs(Str, stdout);