[PATCH] D65130: [clang][OpenMP] Add clang-offload-wrapper tool
sdmitriev abandoned this revision. sdmitriev added a comment. This patch is no longer relevant since it was agreed to split https://reviews.llvm.org/D64943 into pieces in a different way. CHANGES SINCE LAST ACTION https://reviews.llvm.org/D65130/new/ https://reviews.llvm.org/D65130 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D65130: [clang][OpenMP] Add clang-offload-wrapper tool
sdmitriev updated this revision to Diff 219750. sdmitriev added a comment. Rebase CHANGES SINCE LAST ACTION https://reviews.llvm.org/D65130/new/ https://reviews.llvm.org/D65130 Files: clang/test/Driver/clang-offload-wrapper.c clang/tools/CMakeLists.txt clang/tools/clang-offload-wrapper/CMakeLists.txt clang/tools/clang-offload-wrapper/ClangOffloadWrapper.cpp Index: clang/tools/clang-offload-wrapper/ClangOffloadWrapper.cpp === --- /dev/null +++ clang/tools/clang-offload-wrapper/ClangOffloadWrapper.cpp @@ -0,0 +1,360 @@ +//===-- clang-offload-wrapper/ClangOffloadWrapper.cpp ---*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// +/// +/// \file +/// Implementation of the offload wrapper tool. It takes offload target binaries +/// as input and creates wrapper bitcode from them which registers given +/// binaries in offload runtime. +/// +//===--===// + +#include "clang/Basic/Version.h" +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/Triple.h" +#include "llvm/Bitcode/BitcodeWriter.h" +#include "llvm/IR/Constants.h" +#include "llvm/IR/GlobalVariable.h" +#include "llvm/IR/IRBuilder.h" +#include "llvm/IR/LLVMContext.h" +#include "llvm/IR/Module.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/Errc.h" +#include "llvm/Support/Error.h" +#include "llvm/Support/ErrorOr.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/Signals.h" +#include "llvm/Support/ToolOutputFile.h" +#include "llvm/Support/WithColor.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Transforms/Utils/ModuleUtils.h" +#include +#include + +using namespace llvm; + +static cl::opt Help("h", cl::desc("Alias for -help"), cl::Hidden); + +// Mark all our options with this category, everything else (except for -version +// and -help) will be hidden. +static cl::OptionCategory +ClangOffloadWrapperCategory("clang-offload-wrapper options"); + +static cl::opt Output("o", cl::Required, + cl::desc("Output filename"), + cl::value_desc("filename"), + cl::cat(ClangOffloadWrapperCategory)); + +static cl::list Inputs(cl::Positional, cl::OneOrMore, +cl::desc(""), +cl::cat(ClangOffloadWrapperCategory)); + +static cl::opt Target("target", cl::Required, + cl::desc("Target triple"), + cl::value_desc("triple"), + cl::cat(ClangOffloadWrapperCategory)); + +namespace { + +class BinaryWrapper { + LLVMContext C; + Module M; + + StructType *EntryTy = nullptr; + StructType *ImageTy = nullptr; + StructType *DescTy = nullptr; + +private: + IntegerType *getSizeTTy() { +switch (M.getDataLayout().getPointerTypeSize(Type::getInt8PtrTy(C))) { +case 4u: + return Type::getInt32Ty(C); +case 8u: + return Type::getInt64Ty(C); +} +llvm_unreachable("unsupported pointer type size"); + } + + // struct __tgt_offload_entry { + // void *addr; + // char *name; + // size_t size; + // int32_t flags; + // int32_t reserved; + // }; + StructType *getEntryTy() { +if (!EntryTy) + EntryTy = StructType::create("__tgt_offload_entry", Type::getInt8PtrTy(C), + Type::getInt8PtrTy(C), getSizeTTy(), + Type::getInt32Ty(C), Type::getInt32Ty(C)); +return EntryTy; + } + + PointerType *getEntryPtrTy() { return PointerType::getUnqual(getEntryTy()); } + + // struct __tgt_device_image { + // void *ImageStart; + // void *ImageEnd; + // __tgt_offload_entry *EntriesBegin; + // __tgt_offload_entry *EntriesEnd; + // }; + StructType *getDeviceImageTy() { +if (!ImageTy) + ImageTy = StructType::create("__tgt_device_image", Type::getInt8PtrTy(C), + Type::getInt8PtrTy(C), getEntryPtrTy(), + getEntryPtrTy()); +return ImageTy; + } + + PointerType *getDeviceImagePtrTy() { +return PointerType::getUnqual(getDeviceImageTy()); + } + + // struct __tgt_bin_desc { + // int32_t NumDeviceImages; + // __tgt_device_image *DeviceImages; + // __tgt_offload_entry *HostEntriesBegin; + // __tgt_offload_entry *HostEntriesEnd; + // }; + StructType *getBinDescTy() { +if (!DescTy) + DescTy = StructType::create("__tgt_bin_desc", Type::getInt32Ty(C), + getDeviceImagePtrTy(), getEntryPtrTy(), +
[PATCH] D65130: [clang][OpenMP] Add clang-offload-wrapper tool
sdmitriev updated this revision to Diff 217682. sdmitriev added a comment. Rebased and synced up changes with https://reviews.llvm.org/D64943 CHANGES SINCE LAST ACTION https://reviews.llvm.org/D65130/new/ https://reviews.llvm.org/D65130 Files: clang/test/Driver/clang-offload-wrapper.c clang/tools/CMakeLists.txt clang/tools/clang-offload-wrapper/CMakeLists.txt clang/tools/clang-offload-wrapper/ClangOffloadWrapper.cpp Index: clang/tools/clang-offload-wrapper/ClangOffloadWrapper.cpp === --- /dev/null +++ clang/tools/clang-offload-wrapper/ClangOffloadWrapper.cpp @@ -0,0 +1,360 @@ +//===-- clang-offload-wrapper/ClangOffloadWrapper.cpp ---*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// +/// +/// \file +/// Implememtation of the offload wrapper tool. It takes offload target binaries +/// as input and creates wrapper bitcode from them which registers given +/// binaries in offload runtime. +/// +//===--===// + +#include "clang/Basic/Version.h" +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/Triple.h" +#include "llvm/Bitcode/BitcodeWriter.h" +#include "llvm/IR/Constants.h" +#include "llvm/IR/GlobalVariable.h" +#include "llvm/IR/IRBuilder.h" +#include "llvm/IR/LLVMContext.h" +#include "llvm/IR/Module.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/Errc.h" +#include "llvm/Support/Error.h" +#include "llvm/Support/ErrorOr.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/Signals.h" +#include "llvm/Support/ToolOutputFile.h" +#include "llvm/Support/WithColor.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Transforms/Utils/ModuleUtils.h" +#include +#include + +using namespace llvm; + +static cl::opt Help("h", cl::desc("Alias for -help"), cl::Hidden); + +// Mark all our options with this category, everything else (except for -version +// and -help) will be hidden. +static cl::OptionCategory +ClangOffloadWrapperCategory("clang-offload-wrapper options"); + +static cl::opt Output("o", cl::Required, + cl::desc("Output filename"), + cl::value_desc("filename"), + cl::cat(ClangOffloadWrapperCategory)); + +static cl::list Inputs(cl::Positional, cl::OneOrMore, +cl::desc(""), +cl::cat(ClangOffloadWrapperCategory)); + +static cl::opt Target("target", cl::Required, + cl::desc("Target triple"), + cl::value_desc("triple"), + cl::cat(ClangOffloadWrapperCategory)); + +namespace { + +class BinaryWrapper { + LLVMContext C; + Module M; + + StructType *EntryTy = nullptr; + StructType *ImageTy = nullptr; + StructType *DescTy = nullptr; + +private: + IntegerType *getSizeTTy() { +switch (M.getDataLayout().getPointerTypeSize(Type::getInt8PtrTy(C))) { +case 4u: + return Type::getInt32Ty(C); +case 8u: + return Type::getInt64Ty(C); +} +llvm_unreachable("unsupported pointer type size"); + } + + // struct __tgt_offload_entry { + // void *addr; + // char *name; + // size_t size; + // int32_t flags; + // int32_t reserved; + // }; + StructType *getEntryTy() { +if (!EntryTy) + EntryTy = StructType::create("__tgt_offload_entry", Type::getInt8PtrTy(C), + Type::getInt8PtrTy(C), getSizeTTy(), + Type::getInt32Ty(C), Type::getInt32Ty(C)); +return EntryTy; + } + + PointerType *getEntryPtrTy() { return PointerType::getUnqual(getEntryTy()); } + + // struct __tgt_device_image { + // void *ImageStart; + // void *ImageEnd; + // __tgt_offload_entry *EntriesBegin; + // __tgt_offload_entry *EntriesEnd; + // }; + StructType *getDeviceImageTy() { +if (!ImageTy) + ImageTy = StructType::create("__tgt_device_image", Type::getInt8PtrTy(C), + Type::getInt8PtrTy(C), getEntryPtrTy(), + getEntryPtrTy()); +return ImageTy; + } + + PointerType *getDeviceImagePtrTy() { +return PointerType::getUnqual(getDeviceImageTy()); + } + + // struct __tgt_bin_desc { + // int32_t NumDeviceImages; + // __tgt_device_image *DeviceImages; + // __tgt_offload_entry *HostEntriesBegin; + // __tgt_offload_entry *HostEntriesEnd; + // }; + StructType *getBinDescTy() { +if (!DescTy) + DescTy = StructType::create("__tgt_bin_desc", Type::getInt32Ty(C), + getDeviceImagePtrTy(),
[PATCH] D65130: [clang][OpenMP] Add clang-offload-wrapper tool
sdmitriev updated this revision to Diff 213481. sdmitriev added a comment. Rebase. CHANGES SINCE LAST ACTION https://reviews.llvm.org/D65130/new/ https://reviews.llvm.org/D65130 Files: clang/test/Driver/clang-offload-wrapper.ll clang/tools/CMakeLists.txt clang/tools/clang-offload-wrapper/CMakeLists.txt clang/tools/clang-offload-wrapper/ClangOffloadWrapper.cpp Index: clang/tools/clang-offload-wrapper/ClangOffloadWrapper.cpp === --- /dev/null +++ clang/tools/clang-offload-wrapper/ClangOffloadWrapper.cpp @@ -0,0 +1,290 @@ +//===-- clang-offload-wrapper/ClangOffloadWrapper.cpp ---*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// +/// +/// \file +/// Implememtation of the offload wrapper tool. It takes offload target binaries +/// as input and creates wrapper bitcode from them which registers given +/// binaries in offload runtime. +/// +//===--===// + +#include "clang/Basic/Version.h" +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/Triple.h" +#include "llvm/Bitcode/BitcodeWriter.h" +#include "llvm/IR/Constants.h" +#include "llvm/IR/GlobalVariable.h" +#include "llvm/IR/IRBuilder.h" +#include "llvm/IR/LLVMContext.h" +#include "llvm/IR/Module.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/Error.h" +#include "llvm/Support/ErrorOr.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/Signals.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Transforms/Utils/ModuleUtils.h" +#include +#include +#include + +using namespace llvm; + +static cl::opt Help("h", cl::desc("Alias for -help"), cl::Hidden); + +// Mark all our options with this category, everything else (except for -version +// and -help) will be hidden. +static cl::OptionCategory +ClangOffloadWrapperCategory("clang-offload-wrapper options"); + +static cl::opt Output("o", cl::Required, + cl::desc("Output filename"), + cl::value_desc("filename"), + cl::cat(ClangOffloadWrapperCategory)); + +static cl::list Inputs(cl::Positional, cl::OneOrMore, +cl::desc(""), +cl::cat(ClangOffloadWrapperCategory)); + +static cl::opt Target("target", cl::Required, + cl::desc("Target triple"), + cl::value_desc("triple"), + cl::cat(ClangOffloadWrapperCategory)); + +namespace { + +class BinaryWrapper { + LLVMContext C; + Module M; + + StructType *EntryTy = nullptr; + StructType *ImageTy = nullptr; + StructType *DescTy = nullptr; + + using MemoryBuffersVector = SmallVectorImpl>; + +private: + IntegerType *getSizeTTy() { +auto PtrSize = M.getDataLayout().getPointerTypeSize(Type::getInt8PtrTy(C)); +return PtrSize == 8 ? Type::getInt64Ty(C) : Type::getInt32Ty(C); + } + + // struct __tgt_offload_entry { + // void *addr; + // char *name; + // size_t size; + // int32_t flags; + // int32_t reserved; + // }; + StructType *getEntryTy() { +if (!EntryTy) + EntryTy = StructType::create("__tgt_offload_entry", Type::getInt8PtrTy(C), + Type::getInt8PtrTy(C), getSizeTTy(), + Type::getInt32Ty(C), Type::getInt32Ty(C)); +return EntryTy; + } + + PointerType *getEntryPtrTy() { return PointerType::getUnqual(getEntryTy()); } + + // struct __tgt_device_image { + // void *ImageStart; + // void *ImageEnd; + // __tgt_offload_entry *EntriesBegin; + // __tgt_offload_entry *EntriesEnd; + // }; + StructType *getDeviceImageTy() { +if (!ImageTy) + ImageTy = StructType::create("__tgt_device_image", Type::getInt8PtrTy(C), + Type::getInt8PtrTy(C), getEntryPtrTy(), + getEntryPtrTy()); +return ImageTy; + } + + PointerType *getDeviceImagePtrTy() { +return PointerType::getUnqual(getDeviceImageTy()); + } + + // struct __tgt_bin_desc { + // int32_t NumDeviceImages; + // __tgt_device_image *DeviceImages; + // __tgt_offload_entry *HostEntriesBegin; + // __tgt_offload_entry *HostEntriesEnd; + // }; + StructType *getBinDescTy() { +if (!DescTy) + DescTy = StructType::create("__tgt_bin_desc", Type::getInt32Ty(C), + getDeviceImagePtrTy(), getEntryPtrTy(), + getEntryPtrTy()); +return DescTy; + } + + PointerType *getBinDescPtrTy() { +return PointerType::getUnqual(getBinDescTy()); + } + +
[PATCH] D65130: [clang][OpenMP] Add clang-offload-wrapper tool
sdmitriev added a comment. Any feedback? Repository: rC Clang CHANGES SINCE LAST ACTION https://reviews.llvm.org/D65130/new/ https://reviews.llvm.org/D65130 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D65130: [clang][OpenMP] Add clang-offload-wrapper tool
sdmitriev created this revision. sdmitriev added reviewers: hfinkel, ABataev. Herald added subscribers: cfe-commits, guansong, mgorny. Herald added a reviewer: jdoerfert. Herald added a project: clang. This is the first set of changes for eliminating OpenMP linker script that adds new clang-offload-wrapper tool (please see https://reviews.llvm.org/D64943 for more details). This tool takes device images as input and produces a wrapper bit-code file which contains device images as data and offload registration code that registers the binaries in OpenMP offloading runtime at startup. Repository: rC Clang https://reviews.llvm.org/D65130 Files: clang/test/Driver/clang-offload-wrapper.ll clang/tools/CMakeLists.txt clang/tools/clang-offload-wrapper/CMakeLists.txt clang/tools/clang-offload-wrapper/ClangOffloadWrapper.cpp Index: clang/tools/clang-offload-wrapper/ClangOffloadWrapper.cpp === --- /dev/null +++ clang/tools/clang-offload-wrapper/ClangOffloadWrapper.cpp @@ -0,0 +1,290 @@ +//===-- clang-offload-wrapper/ClangOffloadWrapper.cpp ---*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// +/// +/// \file +/// Implememtation of the offload wrapper tool. It takes offload target binaries +/// as input and creates wrapper bitcode from them which registers given +/// binaries in offload runtime. +/// +//===--===// + +#include "clang/Basic/Version.h" +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/Triple.h" +#include "llvm/Bitcode/BitcodeWriter.h" +#include "llvm/IR/Constants.h" +#include "llvm/IR/GlobalVariable.h" +#include "llvm/IR/IRBuilder.h" +#include "llvm/IR/LLVMContext.h" +#include "llvm/IR/Module.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/Error.h" +#include "llvm/Support/ErrorOr.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/Signals.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Transforms/Utils/ModuleUtils.h" +#include +#include +#include + +using namespace llvm; + +static cl::opt Help("h", cl::desc("Alias for -help"), cl::Hidden); + +// Mark all our options with this category, everything else (except for -version +// and -help) will be hidden. +static cl::OptionCategory +ClangOffloadWrapperCategory("clang-offload-wrapper options"); + +static cl::opt Output("o", cl::Required, + cl::desc("Output filename"), + cl::value_desc("filename"), + cl::cat(ClangOffloadWrapperCategory)); + +static cl::list Inputs(cl::Positional, cl::OneOrMore, +cl::desc(""), +cl::cat(ClangOffloadWrapperCategory)); + +static cl::opt Target("target", cl::Required, + cl::desc("Target triple"), + cl::value_desc("triple"), + cl::cat(ClangOffloadWrapperCategory)); + +namespace { + +class BinaryWrapper { + LLVMContext C; + Module M; + + StructType *EntryTy = nullptr; + StructType *ImageTy = nullptr; + StructType *DescTy = nullptr; + + using MemoryBuffersVector = SmallVectorImpl>; + +private: + IntegerType *getSizeTTy() { +auto PtrSize = M.getDataLayout().getPointerTypeSize(Type::getInt8PtrTy(C)); +return PtrSize == 8 ? Type::getInt64Ty(C) : Type::getInt32Ty(C); + } + + // struct __tgt_offload_entry { + // void *addr; + // char *name; + // size_t size; + // int32_t flags; + // int32_t reserved; + // }; + StructType *getEntryTy() { +if (!EntryTy) + EntryTy = StructType::create("__tgt_offload_entry", Type::getInt8PtrTy(C), + Type::getInt8PtrTy(C), getSizeTTy(), + Type::getInt32Ty(C), Type::getInt32Ty(C)); +return EntryTy; + } + + PointerType *getEntryPtrTy() { return PointerType::getUnqual(getEntryTy()); } + + // struct __tgt_device_image { + // void *ImageStart; + // void *ImageEnd; + // __tgt_offload_entry *EntriesBegin; + // __tgt_offload_entry *EntriesEnd; + // }; + StructType *getDeviceImageTy() { +if (!ImageTy) + ImageTy = StructType::create("__tgt_device_image", Type::getInt8PtrTy(C), + Type::getInt8PtrTy(C), getEntryPtrTy(), + getEntryPtrTy()); +return ImageTy; + } + + PointerType *getDeviceImagePtrTy() { +return PointerType::getUnqual(getDeviceImageTy()); + } + + // struct __tgt_bin_desc { + // int32_t NumDeviceImages; + // __tgt_device_image *DeviceImages; + //