Skip to content

Commit fad7a36

Browse files
committed
[llvm][frontend][offloading] Move clang-linker-wrapper/OffloadWrapper.* to llvm/Frontend/Offloading
This patch moves `clang/tools/clang-linker-wrapper/OffloadWrapper.*` to `llvm/Frontend/Offloading` allowing them to be reutilized by other projects. Additionally, it makes minor modifications to the API to make it more flexible. Concretely: - The `wrap*` methods are moved to the `OffloadWrapper` class. - The `OffloadWrapper` includes `Suffix` and `EmitSurfacesAndTextures` fields to specify some additional options. - The `Suffix` field is used when emitting the descriptor, registration methods, etc, to make them more readable. It is empty by default. - The `EmitSurfacesAndTextures` field controls whether to emit surface and texture registration code, as those functions were removed from `CUDART` in CUDA 12. It is true by default. - The `wrap*` methods now have an optional field to specify the `EntryArray`; this change is needed to enable JIT compilation, as ORC doesn't fully support `__start_` and `__stop_` symbols. Thus, to JIT the code, the `EntryArray` has to be constructed explicitly in the IR. - The function `getOffloadingEntryInitializer` was added to help create the `EntryArray`, as it returns the constant initializer and not a global variable.
1 parent f56a739 commit fad7a36

File tree

7 files changed

+156
-68
lines changed

7 files changed

+156
-68
lines changed

clang/tools/clang-linker-wrapper/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ endif()
2828

2929
add_clang_tool(clang-linker-wrapper
3030
ClangLinkerWrapper.cpp
31-
OffloadWrapper.cpp
3231

3332
DEPENDS
3433
${tablegen_deps}

clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
//
1515
//===---------------------------------------------------------------------===//
1616

17-
#include "OffloadWrapper.h"
1817
#include "clang/Basic/Version.h"
1918
#include "llvm/BinaryFormat/Magic.h"
2019
#include "llvm/Bitcode/BitcodeWriter.h"
2120
#include "llvm/CodeGen/CommandFlags.h"
21+
#include "llvm/Frontend/Offloading/OffloadWrapper.h"
2222
#include "llvm/IR/Constants.h"
2323
#include "llvm/IR/DiagnosticPrinter.h"
2424
#include "llvm/IR/Module.h"
@@ -906,15 +906,18 @@ wrapDeviceImages(ArrayRef<std::unique_ptr<MemoryBuffer>> Buffers,
906906

907907
switch (Kind) {
908908
case OFK_OpenMP:
909-
if (Error Err = wrapOpenMPBinaries(M, BuffersToWrap))
909+
if (Error Err =
910+
offloading::OffloadWrapper().wrapOpenMPBinaries(M, BuffersToWrap))
910911
return std::move(Err);
911912
break;
912913
case OFK_Cuda:
913-
if (Error Err = wrapCudaBinary(M, BuffersToWrap.front()))
914+
if (Error Err = offloading::OffloadWrapper().wrapCudaBinary(
915+
M, BuffersToWrap.front()))
914916
return std::move(Err);
915917
break;
916918
case OFK_HIP:
917-
if (Error Err = wrapHIPBinary(M, BuffersToWrap.front()))
919+
if (Error Err = offloading::OffloadWrapper().wrapHIPBinary(
920+
M, BuffersToWrap.front()))
918921
return std::move(Err);
919922
break;
920923
default:

llvm/include/llvm/Frontend/Offloading/OffloadWrapper.h

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,57 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef LLVM_CLANG_TOOLS_CLANG_LINKER_WRAPPER_OFFLOAD_WRAPPER_H
10-
#define LLVM_CLANG_TOOLS_CLANG_LINKER_WRAPPER_OFFLOAD_WRAPPER_H
9+
#ifndef LLVM_FRONTEND_OFFLOADING_OFFLOADWRAPPER_H
10+
#define LLVM_FRONTEND_OFFLOADING_OFFLOADWRAPPER_H
1111

1212
#include "llvm/ADT/ArrayRef.h"
1313
#include "llvm/IR/Module.h"
1414

15-
/// Wraps the input device images into the module \p M as global symbols and
16-
/// registers the images with the OpenMP Offloading runtime libomptarget.
17-
llvm::Error wrapOpenMPBinaries(llvm::Module &M,
18-
llvm::ArrayRef<llvm::ArrayRef<char>> Images);
15+
namespace llvm {
16+
namespace offloading {
17+
/// Class for embedding and registering offloading images and related objects in
18+
/// a Module.
19+
class OffloadWrapper {
20+
public:
21+
using EntryArrayTy = std::pair<GlobalVariable *, GlobalVariable *>;
1922

20-
/// Wraps the input fatbinary image into the module \p M as global symbols and
21-
/// registers the images with the CUDA runtime.
22-
llvm::Error wrapCudaBinary(llvm::Module &M, llvm::ArrayRef<char> Images);
23+
OffloadWrapper(const Twine &Suffix = "", bool EmitSurfacesAndTextures = true)
24+
: Suffix(Suffix.str()), EmitSurfacesAndTextures(EmitSurfacesAndTextures) {
25+
}
2326

24-
/// Wraps the input bundled image into the module \p M as global symbols and
25-
/// registers the images with the HIP runtime.
26-
llvm::Error wrapHIPBinary(llvm::Module &M, llvm::ArrayRef<char> Images);
27+
/// Wraps the input device images into the module \p M as global symbols and
28+
/// registers the images with the OpenMP Offloading runtime libomptarget.
29+
/// \param EntryArray Optional pair pointing to the `__start` and `__stop`
30+
/// symbols holding the `__tgt_offload_entry` array.
31+
llvm::Error wrapOpenMPBinaries(
32+
llvm::Module &M, llvm::ArrayRef<llvm::ArrayRef<char>> Images,
33+
std::optional<EntryArrayTy> EntryArray = std::nullopt) const;
2734

28-
#endif
35+
/// Wraps the input fatbinary image into the module \p M as global symbols and
36+
/// registers the images with the CUDA runtime.
37+
/// \param EntryArray Optional pair pointing to the `__start` and `__stop`
38+
/// symbols holding the `__tgt_offload_entry` array.
39+
llvm::Error
40+
wrapCudaBinary(llvm::Module &M, llvm::ArrayRef<char> Images,
41+
std::optional<EntryArrayTy> EntryArray = std::nullopt) const;
42+
43+
/// Wraps the input bundled image into the module \p M as global symbols and
44+
/// registers the images with the HIP runtime.
45+
/// \param EntryArray Optional pair pointing to the `__start` and `__stop`
46+
/// symbols holding the `__tgt_offload_entry` array.
47+
llvm::Error
48+
wrapHIPBinary(llvm::Module &M, llvm::ArrayRef<char> Images,
49+
std::optional<EntryArrayTy> EntryArray = std::nullopt) const;
50+
51+
protected:
52+
/// Suffix used when emitting symbols. It defaults to the empty string.
53+
std::string Suffix;
54+
55+
/// Whether to emit surface and textures registration code. It defaults to
56+
/// false.
57+
bool EmitSurfacesAndTextures;
58+
};
59+
} // namespace offloading
60+
} // namespace llvm
61+
62+
#endif // LLVM_FRONTEND_OFFLOADING_OFFLOADWRAPPER_H

llvm/include/llvm/Frontend/Offloading/Utility.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ StructType *getEntryTy(Module &M);
6161
void emitOffloadingEntry(Module &M, Constant *Addr, StringRef Name,
6262
uint64_t Size, int32_t Flags, int32_t Data,
6363
StringRef SectionName);
64+
/// Create a constant struct initializer used to register this global at
65+
/// runtime.
66+
/// \return the constant struct and the global variable holding the symbol name.
67+
std::pair<Constant *, GlobalVariable *>
68+
getOffloadingEntryInitializer(Module &M, Constant *Addr, StringRef Name,
69+
uint64_t Size, int32_t Flags, int32_t Data);
6470

6571
/// Creates a pair of globals used to iterate the array of offloading entries by
6672
/// accessing the section variables provided by the linker.

llvm/lib/Frontend/Offloading/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
add_llvm_component_library(LLVMFrontendOffloading
22
Utility.cpp
3+
OffloadWrapper.cpp
34

45
ADDITIONAL_HEADER_DIRS
56
${LLVM_MAIN_INCLUDE_DIR}/llvm/Frontend
@@ -9,6 +10,7 @@ add_llvm_component_library(LLVMFrontendOffloading
910

1011
LINK_COMPONENTS
1112
Core
13+
BinaryFormat
1214
Support
1315
TransformUtils
1416
TargetParser

0 commit comments

Comments
 (0)