Skip to content

Commit 9fa9d9a

Browse files
authored
[llvm][frontend][offloading] Move clang-linker-wrapper/OffloadWrapper.* to llvm/Frontend/Offloading (#78057)
This patch moves `clang/tools/clang-linker-wrapper/OffloadWrapper.*` to `llvm/Frontend/Offloading` allowing them to be re-utilized by other projects. Additionally, it makes minor modifications to the API to make it more flexible. Concretely: - The `wrap*` methods now have additional arguments `EntryArray`, `Suffix` and `EmitSurfacesAndTextures` to specify some additional options. - The `EntryArray` is now constructed by the caller. 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 `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 function `getOffloadingEntryInitializer` was added to help create the `EntryArray`, as it returns the constant initializer and not a global variable.
1 parent 8f3bdea commit 9fa9d9a

File tree

9 files changed

+162
-94
lines changed

9 files changed

+162
-94
lines changed

clang/test/Driver/linker-wrapper-image.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,17 @@
4545
// RUN: clang-linker-wrapper --print-wrapped-module --dry-run --host-triple=x86_64-unknown-windows-gnu \
4646
// RUN: --linker-path=/usr/bin/ld -- %t.o -o a.out 2>&1 | FileCheck %s --check-prefixes=CUDA,CUDA-COFF
4747

48-
// CUDA: @.fatbin_image = internal constant [0 x i8] zeroinitializer, section ".nv_fatbin"
49-
// CUDA-NEXT: @.fatbin_wrapper = internal constant %fatbin_wrapper { i32 1180844977, i32 1, ptr @.fatbin_image, ptr null }, section ".nvFatBinSegment", align 8
50-
// CUDA-NEXT: @.cuda.binary_handle = internal global ptr null
51-
5248
// CUDA-ELF: @__start_cuda_offloading_entries = external hidden constant [0 x %struct.__tgt_offload_entry]
5349
// CUDA-ELF-NEXT: @__stop_cuda_offloading_entries = external hidden constant [0 x %struct.__tgt_offload_entry]
5450
// CUDA-ELF-NEXT: @__dummy.cuda_offloading_entries = hidden constant [0 x %struct.__tgt_offload_entry] zeroinitializer, section "cuda_offloading_entries"
5551

5652
// CUDA-COFF: @__start_cuda_offloading_entries = hidden constant [0 x %struct.__tgt_offload_entry] zeroinitializer, section "cuda_offloading_entries$OA"
5753
// CUDA-COFF-NEXT: @__stop_cuda_offloading_entries = hidden constant [0 x %struct.__tgt_offload_entry] zeroinitializer, section "cuda_offloading_entries$OZ"
5854

55+
// CUDA: @.fatbin_image = internal constant [0 x i8] zeroinitializer, section ".nv_fatbin"
56+
// CUDA-NEXT: @.fatbin_wrapper = internal constant %fatbin_wrapper { i32 1180844977, i32 1, ptr @.fatbin_image, ptr null }, section ".nvFatBinSegment", align 8
57+
// CUDA-NEXT: @.cuda.binary_handle = internal global ptr null
58+
5959
// CUDA: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 1, ptr @.cuda.fatbin_reg, ptr null }]
6060

6161
// CUDA: define internal void @.cuda.fatbin_reg() section ".text.startup" {
@@ -145,17 +145,17 @@
145145
// RUN: clang-linker-wrapper --print-wrapped-module --dry-run --host-triple=x86_64-unknown-windows-gnu \
146146
// RUN: --linker-path=/usr/bin/ld -- %t.o -o a.out 2>&1 | FileCheck %s --check-prefixes=HIP,HIP-COFF
147147

148-
// HIP: @.fatbin_image = internal constant [0 x i8] zeroinitializer, section ".hip_fatbin"
149-
// HIP-NEXT: @.fatbin_wrapper = internal constant %fatbin_wrapper { i32 1212764230, i32 1, ptr @.fatbin_image, ptr null }, section ".hipFatBinSegment", align 8
150-
// HIP-NEXT: @.hip.binary_handle = internal global ptr null
151-
152148
// HIP-ELF: @__start_hip_offloading_entries = external hidden constant [0 x %struct.__tgt_offload_entry]
153149
// HIP-ELF-NEXT: @__stop_hip_offloading_entries = external hidden constant [0 x %struct.__tgt_offload_entry]
154150
// HIP-ELF-NEXT: @__dummy.hip_offloading_entries = hidden constant [0 x %struct.__tgt_offload_entry] zeroinitializer, section "hip_offloading_entries"
155151

156152
// HIP-COFF: @__start_hip_offloading_entries = hidden constant [0 x %struct.__tgt_offload_entry] zeroinitializer, section "hip_offloading_entries$OA"
157153
// HIP-COFF-NEXT: @__stop_hip_offloading_entries = hidden constant [0 x %struct.__tgt_offload_entry] zeroinitializer, section "hip_offloading_entries$OZ"
158154

155+
// HIP: @.fatbin_image = internal constant [0 x i8] zeroinitializer, section ".hip_fatbin"
156+
// HIP-NEXT: @.fatbin_wrapper = internal constant %fatbin_wrapper { i32 1212764230, i32 1, ptr @.fatbin_image, ptr null }, section ".hipFatBinSegment", align 8
157+
// HIP-NEXT: @.hip.binary_handle = internal global ptr null
158+
159159
// HIP: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 1, ptr @.hip.fatbin_reg, ptr null }]
160160

161161
// HIP: define internal void @.hip.fatbin_reg() section ".text.startup" {

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: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
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"
22+
#include "llvm/Frontend/Offloading/Utility.h"
2223
#include "llvm/IR/Constants.h"
2324
#include "llvm/IR/DiagnosticPrinter.h"
2425
#include "llvm/IR/Module.h"
@@ -906,15 +907,21 @@ wrapDeviceImages(ArrayRef<std::unique_ptr<MemoryBuffer>> Buffers,
906907

907908
switch (Kind) {
908909
case OFK_OpenMP:
909-
if (Error Err = wrapOpenMPBinaries(M, BuffersToWrap))
910+
if (Error Err = offloading::wrapOpenMPBinaries(
911+
M, BuffersToWrap,
912+
offloading::getOffloadEntryArray(M, "omp_offloading_entries")))
910913
return std::move(Err);
911914
break;
912915
case OFK_Cuda:
913-
if (Error Err = wrapCudaBinary(M, BuffersToWrap.front()))
916+
if (Error Err = offloading::wrapCudaBinary(
917+
M, BuffersToWrap.front(),
918+
offloading::getOffloadEntryArray(M, "cuda_offloading_entries")))
914919
return std::move(Err);
915920
break;
916921
case OFK_HIP:
917-
if (Error Err = wrapHIPBinary(M, BuffersToWrap.front()))
922+
if (Error Err = offloading::wrapHIPBinary(
923+
M, BuffersToWrap.front(),
924+
offloading::getOffloadEntryArray(M, "hip_offloading_entries")))
918925
return std::move(Err);
919926
break;
920927
default:

clang/tools/clang-linker-wrapper/OffloadWrapper.h

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//===- OffloadWrapper.h --r-------------------------------------*- C++ -*-===//
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+
#ifndef LLVM_FRONTEND_OFFLOADING_OFFLOADWRAPPER_H
10+
#define LLVM_FRONTEND_OFFLOADING_OFFLOADWRAPPER_H
11+
12+
#include "llvm/ADT/ArrayRef.h"
13+
#include "llvm/IR/Module.h"
14+
15+
namespace llvm {
16+
namespace offloading {
17+
using EntryArrayTy = std::pair<GlobalVariable *, GlobalVariable *>;
18+
/// Wraps the input device images into the module \p M as global symbols and
19+
/// registers the images with the OpenMP Offloading runtime libomptarget.
20+
/// \param EntryArray Optional pair pointing to the `__start` and `__stop`
21+
/// symbols holding the `__tgt_offload_entry` array.
22+
/// \param Suffix An optional suffix appended to the emitted symbols.
23+
llvm::Error wrapOpenMPBinaries(llvm::Module &M,
24+
llvm::ArrayRef<llvm::ArrayRef<char>> Images,
25+
EntryArrayTy EntryArray,
26+
llvm::StringRef Suffix = "");
27+
28+
/// Wraps the input fatbinary image into the module \p M as global symbols and
29+
/// registers the images with the CUDA runtime.
30+
/// \param EntryArray Optional pair pointing to the `__start` and `__stop`
31+
/// symbols holding the `__tgt_offload_entry` array.
32+
/// \param Suffix An optional suffix appended to the emitted symbols.
33+
/// \param EmitSurfacesAndTextures Whether to emit surface and textures
34+
/// registration code. It defaults to false.
35+
llvm::Error wrapCudaBinary(llvm::Module &M, llvm::ArrayRef<char> Images,
36+
EntryArrayTy EntryArray, llvm::StringRef Suffix = "",
37+
bool EmitSurfacesAndTextures = true);
38+
39+
/// Wraps the input bundled image into the module \p M as global symbols and
40+
/// registers the images with the HIP runtime.
41+
/// \param EntryArray Optional pair pointing to the `__start` and `__stop`
42+
/// symbols holding the `__tgt_offload_entry` array.
43+
/// \param Suffix An optional suffix appended to the emitted symbols.
44+
/// \param EmitSurfacesAndTextures Whether to emit surface and textures
45+
/// registration code. It defaults to false.
46+
llvm::Error wrapHIPBinary(llvm::Module &M, llvm::ArrayRef<char> Images,
47+
EntryArrayTy EntryArray, llvm::StringRef Suffix = "",
48+
bool EmitSurfacesAndTextures = true);
49+
} // namespace offloading
50+
} // namespace llvm
51+
52+
#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)