forked from llvm/llvm-project
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Offloading][NFC] Move creation of offloading entries from OpenMP (ll…
…vm#70116) Summary: This patch is a first step to remove dependencies on the OpenMPIRBuilder for creating generic offloading entries. This patch changes no functionality and merely moves the code around. In the future the interface will be changed to allow for more code re-use in the registration and creation of offloading entries as well as a more generic interface for CUDA, HIP, OpenMP, and SYCL(?). Doing this as a first step to reduce the noise involved in the functional changes.
- Loading branch information
Showing
10 changed files
with
139 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//===- Utility.h - Collection of geneirc offloading utilities -------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "llvm/IR/Module.h" | ||
#include "llvm/Object/OffloadBinary.h" | ||
|
||
namespace llvm { | ||
namespace offloading { | ||
|
||
/// Create an offloading section struct used to register this global at | ||
/// runtime. | ||
/// | ||
/// Type struct __tgt_offload_entry{ | ||
/// void *addr; // Pointer to the offload entry info. | ||
/// // (function or global) | ||
/// char *name; // Name of the function or global. | ||
/// size_t size; // Size of the entry info (0 if it a function). | ||
/// int32_t flags; | ||
/// int32_t reserved; | ||
/// }; | ||
/// | ||
/// \param M The module to be used | ||
/// \param Addr The pointer to the global being registered. | ||
/// \param Name The symbol name associated with the global. | ||
/// \param Size The size in bytes of the global (0 for functions). | ||
/// \param Flags Flags associated with the entry. | ||
/// \param SectionName The section this entry will be placed at. | ||
void emitOffloadingEntry(Module &M, Constant *Addr, StringRef Name, | ||
uint64_t Size, int32_t Flags, StringRef SectionName); | ||
|
||
} // namespace offloading | ||
} // namespace llvm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
add_subdirectory(HLSL) | ||
add_subdirectory(OpenACC) | ||
add_subdirectory(OpenMP) | ||
add_subdirectory(Offloading) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
add_llvm_component_library(LLVMFrontendOffloading | ||
Utility.cpp | ||
|
||
ADDITIONAL_HEADER_DIRS | ||
${LLVM_MAIN_INCLUDE_DIR}/llvm/Frontend | ||
|
||
DEPENDS | ||
intrinsics_gen | ||
|
||
LINK_COMPONENTS | ||
Core | ||
Support | ||
TransformUtils | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
//===- Utility.cpp ------ Collection of geneirc offloading utilities ------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "llvm/Frontend/Offloading/Utility.h" | ||
#include "llvm/IR/Constants.h" | ||
#include "llvm/IR/GlobalValue.h" | ||
#include "llvm/IR/GlobalVariable.h" | ||
#include "llvm/IR/Value.h" | ||
|
||
using namespace llvm; | ||
using namespace llvm::offloading; | ||
|
||
// TODO: Export this to the linker wrapper code registration. | ||
static StructType *getEntryTy(Module &M) { | ||
LLVMContext &C = M.getContext(); | ||
StructType *EntryTy = | ||
StructType::getTypeByName(C, "struct.__tgt_offload_entry"); | ||
if (!EntryTy) | ||
EntryTy = StructType::create("struct.__tgt_offload_entry", | ||
Type::getInt8PtrTy(C), Type::getInt8PtrTy(C), | ||
M.getDataLayout().getIntPtrType(C), | ||
Type::getInt32Ty(C), Type::getInt32Ty(C)); | ||
return EntryTy; | ||
} | ||
|
||
// TODO: Rework this interface to be more generic. | ||
void offloading::emitOffloadingEntry(Module &M, Constant *Addr, StringRef Name, | ||
uint64_t Size, int32_t Flags, | ||
StringRef SectionName) { | ||
Type *Int8PtrTy = Type::getInt8PtrTy(M.getContext()); | ||
Type *Int32Ty = Type::getInt32Ty(M.getContext()); | ||
Type *SizeTy = M.getDataLayout().getIntPtrType(M.getContext()); | ||
|
||
Constant *AddrName = ConstantDataArray::getString(M.getContext(), Name); | ||
|
||
// Create the constant string used to look up the symbol in the device. | ||
auto *Str = | ||
new llvm::GlobalVariable(M, AddrName->getType(), /*isConstant=*/true, | ||
llvm::GlobalValue::InternalLinkage, AddrName, | ||
".omp_offloading.entry_name"); | ||
Str->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); | ||
|
||
// Construct the offloading entry. | ||
Constant *EntryData[] = { | ||
ConstantExpr::getPointerBitCastOrAddrSpaceCast(Addr, Int8PtrTy), | ||
ConstantExpr::getPointerBitCastOrAddrSpaceCast(Str, Int8PtrTy), | ||
ConstantInt::get(SizeTy, Size), | ||
ConstantInt::get(Int32Ty, Flags), | ||
ConstantInt::get(Int32Ty, 0), | ||
}; | ||
Constant *EntryInitializer = ConstantStruct::get(getEntryTy(M), EntryData); | ||
|
||
auto *Entry = new GlobalVariable( | ||
M, getEntryTy(M), | ||
/*isConstant=*/true, GlobalValue::WeakAnyLinkage, EntryInitializer, | ||
".omp_offloading.entry." + Name, nullptr, GlobalValue::NotThreadLocal, | ||
M.getDataLayout().getDefaultGlobalsAddressSpace()); | ||
|
||
// The entry has to be created in the section the linker expects it to be. | ||
Entry->setSection(SectionName); | ||
Entry->setAlignment(Align(1)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,5 @@ add_llvm_component_library(LLVMFrontendOpenMP | |
MC | ||
Scalar | ||
BitReader | ||
FrontendOffloading | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters