Skip to content

Commit 078ae8c

Browse files
authored
[Offloading][NFC] Move creation of offloading entries from OpenMP (#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.
1 parent 42c25fd commit 078ae8c

File tree

10 files changed

+139
-76
lines changed

10 files changed

+139
-76
lines changed

clang/lib/CodeGen/CGCUDANV.cpp

+14-14
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "clang/Basic/Cuda.h"
2020
#include "clang/CodeGen/CodeGenABITypes.h"
2121
#include "clang/CodeGen/ConstantInitBuilder.h"
22+
#include "llvm/Frontend/Offloading/Utility.h"
2223
#include "llvm/IR/BasicBlock.h"
2324
#include "llvm/IR/Constants.h"
2425
#include "llvm/IR/DerivedTypes.h"
@@ -1129,33 +1130,32 @@ void CGNVCUDARuntime::transformManagedVars() {
11291130
// registered. The linker will provide a pointer to this section so we can
11301131
// register the symbols with the linked device image.
11311132
void CGNVCUDARuntime::createOffloadingEntries() {
1132-
llvm::OpenMPIRBuilder OMPBuilder(CGM.getModule());
1133-
OMPBuilder.initialize();
1134-
11351133
StringRef Section = CGM.getLangOpts().HIP ? "hip_offloading_entries"
11361134
: "cuda_offloading_entries";
1135+
llvm::Module &M = CGM.getModule();
11371136
for (KernelInfo &I : EmittedKernels)
1138-
OMPBuilder.emitOffloadingEntry(KernelHandles[I.Kernel->getName()],
1139-
getDeviceSideName(cast<NamedDecl>(I.D)), 0,
1140-
DeviceVarFlags::OffloadGlobalEntry, Section);
1137+
llvm::offloading::emitOffloadingEntry(
1138+
M, KernelHandles[I.Kernel->getName()],
1139+
getDeviceSideName(cast<NamedDecl>(I.D)), 0,
1140+
DeviceVarFlags::OffloadGlobalEntry, Section);
11411141

11421142
for (VarInfo &I : DeviceVars) {
11431143
uint64_t VarSize =
11441144
CGM.getDataLayout().getTypeAllocSize(I.Var->getValueType());
11451145
if (I.Flags.getKind() == DeviceVarFlags::Variable) {
1146-
OMPBuilder.emitOffloadingEntry(
1147-
I.Var, getDeviceSideName(I.D), VarSize,
1146+
llvm::offloading::emitOffloadingEntry(
1147+
M, I.Var, getDeviceSideName(I.D), VarSize,
11481148
I.Flags.isManaged() ? DeviceVarFlags::OffloadGlobalManagedEntry
11491149
: DeviceVarFlags::OffloadGlobalEntry,
11501150
Section);
11511151
} else if (I.Flags.getKind() == DeviceVarFlags::Surface) {
1152-
OMPBuilder.emitOffloadingEntry(I.Var, getDeviceSideName(I.D), VarSize,
1153-
DeviceVarFlags::OffloadGlobalSurfaceEntry,
1154-
Section);
1152+
llvm::offloading::emitOffloadingEntry(
1153+
M, I.Var, getDeviceSideName(I.D), VarSize,
1154+
DeviceVarFlags::OffloadGlobalSurfaceEntry, Section);
11551155
} else if (I.Flags.getKind() == DeviceVarFlags::Texture) {
1156-
OMPBuilder.emitOffloadingEntry(I.Var, getDeviceSideName(I.D), VarSize,
1157-
DeviceVarFlags::OffloadGlobalTextureEntry,
1158-
Section);
1156+
llvm::offloading::emitOffloadingEntry(
1157+
M, I.Var, getDeviceSideName(I.D), VarSize,
1158+
DeviceVarFlags::OffloadGlobalTextureEntry, Section);
11591159
}
11601160
}
11611161
}

clang/lib/CodeGen/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ set(LLVM_LINK_COMPONENTS
1111
Extensions
1212
FrontendHLSL
1313
FrontendOpenMP
14+
FrontendOffloading
1415
HIPStdPar
1516
IPO
1617
IRPrinter
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===- Utility.h - Collection of geneirc offloading utilities -------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "llvm/IR/Module.h"
10+
#include "llvm/Object/OffloadBinary.h"
11+
12+
namespace llvm {
13+
namespace offloading {
14+
15+
/// Create an offloading section struct used to register this global at
16+
/// runtime.
17+
///
18+
/// Type struct __tgt_offload_entry{
19+
/// void *addr; // Pointer to the offload entry info.
20+
/// // (function or global)
21+
/// char *name; // Name of the function or global.
22+
/// size_t size; // Size of the entry info (0 if it a function).
23+
/// int32_t flags;
24+
/// int32_t reserved;
25+
/// };
26+
///
27+
/// \param M The module to be used
28+
/// \param Addr The pointer to the global being registered.
29+
/// \param Name The symbol name associated with the global.
30+
/// \param Size The size in bytes of the global (0 for functions).
31+
/// \param Flags Flags associated with the entry.
32+
/// \param SectionName The section this entry will be placed at.
33+
void emitOffloadingEntry(Module &M, Constant *Addr, StringRef Name,
34+
uint64_t Size, int32_t Flags, StringRef SectionName);
35+
36+
} // namespace offloading
37+
} // namespace llvm

llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h

-21
Original file line numberDiff line numberDiff line change
@@ -1358,27 +1358,6 @@ class OpenMPIRBuilder {
13581358
/// Value.
13591359
GlobalValue *createGlobalFlag(unsigned Value, StringRef Name);
13601360

1361-
/// Create an offloading section struct used to register this global at
1362-
/// runtime.
1363-
///
1364-
/// Type struct __tgt_offload_entry{
1365-
/// void *addr; // Pointer to the offload entry info.
1366-
/// // (function or global)
1367-
/// char *name; // Name of the function or global.
1368-
/// size_t size; // Size of the entry info (0 if it a function).
1369-
/// int32_t flags;
1370-
/// int32_t reserved;
1371-
/// };
1372-
///
1373-
/// \param Addr The pointer to the global being registered.
1374-
/// \param Name The symbol name associated with the global.
1375-
/// \param Size The size in bytes of the global (0 for functions).
1376-
/// \param Flags Flags associated with the entry.
1377-
/// \param SectionName The section this entry will be placed at.
1378-
void emitOffloadingEntry(Constant *Addr, StringRef Name, uint64_t Size,
1379-
int32_t Flags,
1380-
StringRef SectionName = "omp_offloading_entries");
1381-
13821361
/// Generate control flow and cleanup for cancellation.
13831362
///
13841363
/// \param CancelFlag Flag indicating if the cancellation is performed.

llvm/include/llvm/Frontend/OpenMP/OMPKinds.def

-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@ __OMP_ARRAY_TYPE(Int32Arr3, Int32, 3)
8888
OMP_STRUCT_TYPE(VarName, "struct." #Name, Packed, __VA_ARGS__)
8989

9090
__OMP_STRUCT_TYPE(Ident, ident_t, false, Int32, Int32, Int32, Int32, Int8Ptr)
91-
__OMP_STRUCT_TYPE(OffloadEntry, __tgt_offload_entry, false, Int8Ptr, Int8Ptr, SizeTy,
92-
Int32, Int32)
9391
__OMP_STRUCT_TYPE(KernelArgs, __tgt_kernel_arguments, false, Int32, Int32, VoidPtrPtr,
9492
VoidPtrPtr, Int64Ptr, Int64Ptr, VoidPtrPtr, VoidPtrPtr,
9593
Int64, Int64, Int32Arr3Ty, Int32Arr3Ty, Int32)

llvm/lib/Frontend/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
add_subdirectory(HLSL)
22
add_subdirectory(OpenACC)
33
add_subdirectory(OpenMP)
4+
add_subdirectory(Offloading)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
add_llvm_component_library(LLVMFrontendOffloading
2+
Utility.cpp
3+
4+
ADDITIONAL_HEADER_DIRS
5+
${LLVM_MAIN_INCLUDE_DIR}/llvm/Frontend
6+
7+
DEPENDS
8+
intrinsics_gen
9+
10+
LINK_COMPONENTS
11+
Core
12+
Support
13+
TransformUtils
14+
)
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//===- Utility.cpp ------ Collection of geneirc offloading utilities ------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "llvm/Frontend/Offloading/Utility.h"
10+
#include "llvm/IR/Constants.h"
11+
#include "llvm/IR/GlobalValue.h"
12+
#include "llvm/IR/GlobalVariable.h"
13+
#include "llvm/IR/Value.h"
14+
15+
using namespace llvm;
16+
using namespace llvm::offloading;
17+
18+
// TODO: Export this to the linker wrapper code registration.
19+
static StructType *getEntryTy(Module &M) {
20+
LLVMContext &C = M.getContext();
21+
StructType *EntryTy =
22+
StructType::getTypeByName(C, "struct.__tgt_offload_entry");
23+
if (!EntryTy)
24+
EntryTy = StructType::create("struct.__tgt_offload_entry",
25+
Type::getInt8PtrTy(C), Type::getInt8PtrTy(C),
26+
M.getDataLayout().getIntPtrType(C),
27+
Type::getInt32Ty(C), Type::getInt32Ty(C));
28+
return EntryTy;
29+
}
30+
31+
// TODO: Rework this interface to be more generic.
32+
void offloading::emitOffloadingEntry(Module &M, Constant *Addr, StringRef Name,
33+
uint64_t Size, int32_t Flags,
34+
StringRef SectionName) {
35+
Type *Int8PtrTy = Type::getInt8PtrTy(M.getContext());
36+
Type *Int32Ty = Type::getInt32Ty(M.getContext());
37+
Type *SizeTy = M.getDataLayout().getIntPtrType(M.getContext());
38+
39+
Constant *AddrName = ConstantDataArray::getString(M.getContext(), Name);
40+
41+
// Create the constant string used to look up the symbol in the device.
42+
auto *Str =
43+
new llvm::GlobalVariable(M, AddrName->getType(), /*isConstant=*/true,
44+
llvm::GlobalValue::InternalLinkage, AddrName,
45+
".omp_offloading.entry_name");
46+
Str->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
47+
48+
// Construct the offloading entry.
49+
Constant *EntryData[] = {
50+
ConstantExpr::getPointerBitCastOrAddrSpaceCast(Addr, Int8PtrTy),
51+
ConstantExpr::getPointerBitCastOrAddrSpaceCast(Str, Int8PtrTy),
52+
ConstantInt::get(SizeTy, Size),
53+
ConstantInt::get(Int32Ty, Flags),
54+
ConstantInt::get(Int32Ty, 0),
55+
};
56+
Constant *EntryInitializer = ConstantStruct::get(getEntryTy(M), EntryData);
57+
58+
auto *Entry = new GlobalVariable(
59+
M, getEntryTy(M),
60+
/*isConstant=*/true, GlobalValue::WeakAnyLinkage, EntryInitializer,
61+
".omp_offloading.entry." + Name, nullptr, GlobalValue::NotThreadLocal,
62+
M.getDataLayout().getDefaultGlobalsAddressSpace());
63+
64+
// The entry has to be created in the section the linker expects it to be.
65+
Entry->setSection(SectionName);
66+
Entry->setAlignment(Align(1));
67+
}

llvm/lib/Frontend/OpenMP/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ add_llvm_component_library(LLVMFrontendOpenMP
2020
MC
2121
Scalar
2222
BitReader
23+
FrontendOffloading
2324
)

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

+4-39
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "llvm/Analysis/ScalarEvolution.h"
2424
#include "llvm/Analysis/TargetLibraryInfo.h"
2525
#include "llvm/Bitcode/BitcodeReader.h"
26+
#include "llvm/Frontend/Offloading/Utility.h"
2627
#include "llvm/Frontend/OpenMP/OMPGridValues.h"
2728
#include "llvm/IR/Attributes.h"
2829
#include "llvm/IR/BasicBlock.h"
@@ -958,44 +959,6 @@ OpenMPIRBuilder::createCancel(const LocationDescription &Loc,
958959
return Builder.saveIP();
959960
}
960961

961-
void OpenMPIRBuilder::emitOffloadingEntry(Constant *Addr, StringRef Name,
962-
uint64_t Size, int32_t Flags,
963-
StringRef SectionName) {
964-
Type *Int8PtrTy = Type::getInt8PtrTy(M.getContext());
965-
Type *Int32Ty = Type::getInt32Ty(M.getContext());
966-
Type *SizeTy = M.getDataLayout().getIntPtrType(M.getContext());
967-
968-
Constant *AddrName = ConstantDataArray::getString(M.getContext(), Name);
969-
970-
// Create the constant string used to look up the symbol in the device.
971-
auto *Str =
972-
new llvm::GlobalVariable(M, AddrName->getType(), /*isConstant=*/true,
973-
llvm::GlobalValue::InternalLinkage, AddrName,
974-
".omp_offloading.entry_name");
975-
Str->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
976-
977-
// Construct the offloading entry.
978-
Constant *EntryData[] = {
979-
ConstantExpr::getPointerBitCastOrAddrSpaceCast(Addr, Int8PtrTy),
980-
ConstantExpr::getPointerBitCastOrAddrSpaceCast(Str, Int8PtrTy),
981-
ConstantInt::get(SizeTy, Size),
982-
ConstantInt::get(Int32Ty, Flags),
983-
ConstantInt::get(Int32Ty, 0),
984-
};
985-
Constant *EntryInitializer =
986-
ConstantStruct::get(OpenMPIRBuilder::OffloadEntry, EntryData);
987-
988-
auto *Entry = new GlobalVariable(
989-
M, OpenMPIRBuilder::OffloadEntry,
990-
/* isConstant = */ true, GlobalValue::WeakAnyLinkage, EntryInitializer,
991-
".omp_offloading.entry." + Name, nullptr, GlobalValue::NotThreadLocal,
992-
M.getDataLayout().getDefaultGlobalsAddressSpace());
993-
994-
// The entry has to be created in the section the linker expects it to be.
995-
Entry->setSection(SectionName);
996-
Entry->setAlignment(Align(1));
997-
}
998-
999962
OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::emitTargetKernel(
1000963
const LocationDescription &Loc, InsertPointTy AllocaIP, Value *&Return,
1001964
Value *Ident, Value *DeviceID, Value *NumTeams, Value *NumThreads,
@@ -5928,7 +5891,9 @@ void OpenMPIRBuilder::createOffloadEntry(Constant *ID, Constant *Addr,
59285891
GlobalValue::LinkageTypes,
59295892
StringRef Name) {
59305893
if (!Config.isGPU()) {
5931-
emitOffloadingEntry(ID, Name.empty() ? Addr->getName() : Name, Size, Flags);
5894+
llvm::offloading::emitOffloadingEntry(
5895+
M, ID, Name.empty() ? Addr->getName() : Name, Size, Flags,
5896+
"omp_offloading_entries");
59325897
return;
59335898
}
59345899
// TODO: Add support for global variables on the device after declare target

0 commit comments

Comments
 (0)