Index: clang/test/Driver/clang-offload-wrapper.c =================================================================== --- clang/test/Driver/clang-offload-wrapper.c +++ clang/test/Driver/clang-offload-wrapper.c @@ -22,6 +22,7 @@ // RUN: clang-offload-wrapper -target=x86_64-pc-linux-gnu -o %t.wrapper.bc %t.tgt // RUN: llvm-dis %t.wrapper.bc -o - | FileCheck %s --check-prefix CHECK-IR +// CHECK-IR: target datalayout = // CHECK-IR: target triple = "x86_64-pc-linux-gnu" // CHECK-IR-DAG: [[ENTTY:%.+]] = type { i8*, i8*, i{{32|64}}, i32, i32 } Index: clang/tools/clang-offload-wrapper/CMakeLists.txt =================================================================== --- clang/tools/clang-offload-wrapper/CMakeLists.txt +++ clang/tools/clang-offload-wrapper/CMakeLists.txt @@ -1,4 +1,15 @@ -set(LLVM_LINK_COMPONENTS BitWriter Core Support TransformUtils) +set(LLVM_LINK_COMPONENTS + AllTargetsAsmParsers + AllTargetsCodeGens + AllTargetsDescs + AllTargetsInfos + BitWriter + Core + MC + Support + Target + TransformUtils + ) if(NOT CLANG_BUILT_STANDALONE) set(tablegen_deps intrinsics_gen) Index: clang/tools/clang-offload-wrapper/ClangOffloadWrapper.cpp =================================================================== --- clang/tools/clang-offload-wrapper/ClangOffloadWrapper.cpp +++ clang/tools/clang-offload-wrapper/ClangOffloadWrapper.cpp @@ -29,9 +29,13 @@ #include "llvm/Support/ErrorOr.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Signals.h" +#include "llvm/Support/TargetRegistry.h" +#include "llvm/Support/TargetSelect.h" #include "llvm/Support/ToolOutputFile.h" #include "llvm/Support/WithColor.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/Target/TargetMachine.h" +#include "llvm/Target/TargetOptions.h" #include "llvm/Transforms/Utils/ModuleUtils.h" #include #include @@ -55,9 +59,9 @@ cl::cat(ClangOffloadWrapperCategory)); static cl::opt - Target("target", cl::Required, - cl::desc("Target triple for the output module"), - cl::value_desc("triple"), cl::cat(ClangOffloadWrapperCategory)); +TargetTriple("target", cl::Required, + cl::desc("Target triple for the output module"), + cl::value_desc("triple"), cl::cat(ClangOffloadWrapperCategory)); namespace { @@ -287,8 +291,9 @@ } public: - BinaryWrapper(StringRef Target) : M("offload.wrapper.object", C) { - M.setTargetTriple(Target); + BinaryWrapper(const TargetMachine *TM) : M("offload.wrapper.object", C) { + M.setTargetTriple(TM->getTargetTriple().getTriple()); + M.setDataLayout(TM->createDataLayout()); } const Module &wrapBinaries(ArrayRef> Binaries) { @@ -321,16 +326,33 @@ return 0; } + InitializeAllTargets(); + InitializeAllTargetMCs(); + auto reportError = [argv](Error E) { logAllUnhandledErrors(std::move(E), WithColor::error(errs(), argv[0])); }; - if (Triple(Target).getArch() == Triple::UnknownArch) { - reportError(createStringError( - errc::invalid_argument, "'" + Target + "': unsupported target triple")); + if (Triple(TargetTriple).getArch() == Triple::UnknownArch) { + reportError( + createStringError(errc::invalid_argument, + "'" + TargetTriple + "': unsupported target triple")); return 1; } + // Lookup and create target machine. We need it for setting data layout for + // the wrapper module. + std::string Error; + const Target *TheTarget = TargetRegistry::lookupTarget(TargetTriple, Error); + if (!TheTarget) { + reportError(createStringError(errc::invalid_argument, Error)); + return 1; + } + + std::unique_ptr TM(TheTarget->createTargetMachine( + TargetTriple, "", "", TargetOptions(), None)); + assert(TM && "Could not allocate target machine!"); + // Read device binaries. SmallVector, 4u> Buffers; SmallVector, 4u> Images; @@ -357,7 +379,7 @@ } // Create a wrapper for device binaries and write its bitcode to the file. - WriteBitcodeToFile(BinaryWrapper(Target).wrapBinaries( + WriteBitcodeToFile(BinaryWrapper(TM.get()).wrapBinaries( makeArrayRef(Images.data(), Images.size())), Out.os()); if (Out.os().has_error()) {