Skip to content

Commit a551703

Browse files
authored
[Offload] Fix the offloading wrapper when merged multiple times. (#79231)
Summary: The offloading wrapper is a object file that contains code necessary to register offloading entries for the given runtime. Currently, we expected only one of these to be present when we make the final executable. However, in the case of redistributable linking with `-r` we can end up with multiple of these being generated before finally creating the executable. This patch simply changes the defintiions of these globals to be mergable. This allows multiples of these to participate in a single link job. For ELF, we just make the dummy variable internal and used so it sets up the section as expected. For COFF we make the entries weak_odr so they merge to a single symbol
1 parent 0f8b529 commit a551703

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414

1515
// OPENMP-ELF: @__start_omp_offloading_entries = external hidden constant [0 x %struct.__tgt_offload_entry]
1616
// OPENMP-ELF-NEXT: @__stop_omp_offloading_entries = external hidden constant [0 x %struct.__tgt_offload_entry]
17-
// OPENMP-ELF-NEXT: @__dummy.omp_offloading_entries = hidden constant [0 x %struct.__tgt_offload_entry] zeroinitializer, section "omp_offloading_entries"
17+
// OPENMP-ELF-NEXT: @__dummy.omp_offloading_entries = internal constant [0 x %struct.__tgt_offload_entry] zeroinitializer, section "omp_offloading_entries"
1818

19-
// OPENMP-COFF: @__start_omp_offloading_entries = hidden constant [0 x %struct.__tgt_offload_entry] zeroinitializer, section "omp_offloading_entries$OA"
20-
// OPENMP-COFF-NEXT: @__stop_omp_offloading_entries = hidden constant [0 x %struct.__tgt_offload_entry] zeroinitializer, section "omp_offloading_entries$OZ"
19+
// OPENMP-COFF: @__start_omp_offloading_entries = weak_odr hidden constant [0 x %struct.__tgt_offload_entry] zeroinitializer, section "omp_offloading_entries$OA"
20+
// OPENMP-COFF-NEXT: @__stop_omp_offloading_entries = weak_odr hidden constant [0 x %struct.__tgt_offload_entry] zeroinitializer, section "omp_offloading_entries$OZ"
2121

2222
// OPENMP: @.omp_offloading.device_image = internal unnamed_addr constant [[[SIZE:[0-9]+]] x i8] c"\10\FF\10\AD{{.*}}", section ".llvm.offloading", align 8
2323
// OPENMP-NEXT: @.omp_offloading.device_images = internal unnamed_addr constant [1 x %__tgt_device_image] [%__tgt_device_image { ptr getelementptr inbounds ([[[BEGIN:[0-9]+]] x i8], ptr @.omp_offloading.device_image, i64 1, i64 0), ptr getelementptr inbounds ([[[END:[0-9]+]] x i8], ptr @.omp_offloading.device_image, i64 1, i64 0), ptr @__start_omp_offloading_entries, ptr @__stop_omp_offloading_entries }]
@@ -47,10 +47,10 @@
4747

4848
// CUDA-ELF: @__start_cuda_offloading_entries = external hidden constant [0 x %struct.__tgt_offload_entry]
4949
// CUDA-ELF-NEXT: @__stop_cuda_offloading_entries = external hidden constant [0 x %struct.__tgt_offload_entry]
50-
// CUDA-ELF-NEXT: @__dummy.cuda_offloading_entries = hidden constant [0 x %struct.__tgt_offload_entry] zeroinitializer, section "cuda_offloading_entries"
50+
// CUDA-ELF-NEXT: @__dummy.cuda_offloading_entries = internal constant [0 x %struct.__tgt_offload_entry] zeroinitializer, section "cuda_offloading_entries"
5151

52-
// CUDA-COFF: @__start_cuda_offloading_entries = hidden constant [0 x %struct.__tgt_offload_entry] zeroinitializer, section "cuda_offloading_entries$OA"
53-
// CUDA-COFF-NEXT: @__stop_cuda_offloading_entries = hidden constant [0 x %struct.__tgt_offload_entry] zeroinitializer, section "cuda_offloading_entries$OZ"
52+
// CUDA-COFF: @__start_cuda_offloading_entries = weak_odr hidden constant [0 x %struct.__tgt_offload_entry] zeroinitializer, section "cuda_offloading_entries$OA"
53+
// CUDA-COFF-NEXT: @__stop_cuda_offloading_entries = weak_odr hidden constant [0 x %struct.__tgt_offload_entry] zeroinitializer, section "cuda_offloading_entries$OZ"
5454

5555
// CUDA: @.fatbin_image = internal constant [0 x i8] zeroinitializer, section ".nv_fatbin"
5656
// CUDA-NEXT: @.fatbin_wrapper = internal constant %fatbin_wrapper { i32 1180844977, i32 1, ptr @.fatbin_image, ptr null }, section ".nvFatBinSegment", align 8
@@ -145,10 +145,10 @@
145145

146146
// HIP-ELF: @__start_hip_offloading_entries = external hidden constant [0 x %struct.__tgt_offload_entry]
147147
// HIP-ELF-NEXT: @__stop_hip_offloading_entries = external hidden constant [0 x %struct.__tgt_offload_entry]
148-
// HIP-ELF-NEXT: @__dummy.hip_offloading_entries = hidden constant [0 x %struct.__tgt_offload_entry] zeroinitializer, section "hip_offloading_entries"
148+
// HIP-ELF-NEXT: @__dummy.hip_offloading_entries = internal constant [0 x %struct.__tgt_offload_entry] zeroinitializer, section "hip_offloading_entries"
149149

150-
// HIP-COFF: @__start_hip_offloading_entries = hidden constant [0 x %struct.__tgt_offload_entry] zeroinitializer, section "hip_offloading_entries$OA"
151-
// HIP-COFF-NEXT: @__stop_hip_offloading_entries = hidden constant [0 x %struct.__tgt_offload_entry] zeroinitializer, section "hip_offloading_entries$OZ"
150+
// HIP-COFF: @__start_hip_offloading_entries = weak_odr hidden constant [0 x %struct.__tgt_offload_entry] zeroinitializer, section "hip_offloading_entries$OA"
151+
// HIP-COFF-NEXT: @__stop_hip_offloading_entries = weak_odr hidden constant [0 x %struct.__tgt_offload_entry] zeroinitializer, section "hip_offloading_entries$OZ"
152152

153153
// HIP: @.fatbin_image = internal constant [0 x i8] zeroinitializer, section ".hip_fatbin"
154154
// HIP-NEXT: @.fatbin_wrapper = internal constant %fatbin_wrapper { i32 1212764230, i32 1, ptr @.fatbin_image, ptr null }, section ".hipFatBinSegment", align 8

llvm/lib/Frontend/Offloading/Utility.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "llvm/IR/GlobalValue.h"
1212
#include "llvm/IR/GlobalVariable.h"
1313
#include "llvm/IR/Value.h"
14+
#include "llvm/Transforms/Utils/ModuleUtils.h"
1415

1516
using namespace llvm;
1617
using namespace llvm::offloading;
@@ -86,14 +87,16 @@ offloading::getOffloadEntryArray(Module &M, StringRef SectionName) {
8687
ConstantAggregateZero::get(ArrayType::get(getEntryTy(M), 0u));
8788
auto *EntryInit = Triple.isOSBinFormatCOFF() ? ZeroInitilaizer : nullptr;
8889
auto *EntryType = ArrayType::get(getEntryTy(M), 0);
90+
auto Linkage = Triple.isOSBinFormatCOFF() ? GlobalValue::WeakODRLinkage
91+
: GlobalValue::ExternalLinkage;
8992

90-
auto *EntriesB = new GlobalVariable(M, EntryType, /*isConstant=*/true,
91-
GlobalValue::ExternalLinkage, EntryInit,
92-
"__start_" + SectionName);
93+
auto *EntriesB =
94+
new GlobalVariable(M, EntryType, /*isConstant=*/true, Linkage, EntryInit,
95+
"__start_" + SectionName);
9396
EntriesB->setVisibility(GlobalValue::HiddenVisibility);
94-
auto *EntriesE = new GlobalVariable(M, EntryType, /*isConstant=*/true,
95-
GlobalValue::ExternalLinkage, EntryInit,
96-
"__stop_" + SectionName);
97+
auto *EntriesE =
98+
new GlobalVariable(M, EntryType, /*isConstant=*/true, Linkage, EntryInit,
99+
"__stop_" + SectionName);
97100
EntriesE->setVisibility(GlobalValue::HiddenVisibility);
98101

99102
if (Triple.isOSBinFormatELF()) {
@@ -102,10 +105,10 @@ offloading::getOffloadEntryArray(Module &M, StringRef SectionName) {
102105
// valid C-identifier is present. We define a dummy variable here to force
103106
// the linker to always provide these symbols.
104107
auto *DummyEntry = new GlobalVariable(
105-
M, ZeroInitilaizer->getType(), true, GlobalVariable::ExternalLinkage,
108+
M, ZeroInitilaizer->getType(), true, GlobalVariable::InternalLinkage,
106109
ZeroInitilaizer, "__dummy." + SectionName);
107110
DummyEntry->setSection(SectionName);
108-
DummyEntry->setVisibility(GlobalValue::HiddenVisibility);
111+
appendToCompilerUsed(M, DummyEntry);
109112
} else {
110113
// The COFF linker will merge sections containing a '$' together into a
111114
// single section. The order of entries in this section will be sorted

0 commit comments

Comments
 (0)