Skip to content

Commit 04e49d8

Browse files
authored
[OpenMP] Fix for --opaque-offload-linker. Default remains --no-opaque-offload-linker. (llvm#1348)
Fixed lit test.
2 parents 82b836e + 3f06cc7 commit 04e49d8

File tree

5 files changed

+336
-582
lines changed

5 files changed

+336
-582
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3822,10 +3822,17 @@ defm openmp_optimistic_collapse : BoolFOption<"openmp-optimistic-collapse",
38223822
NegFlag<SetFalse>, BothFlags<[NoArgumentUnused, HelpHidden], []>>;
38233823
def static_openmp: Flag<["-"], "static-openmp">,
38243824
HelpText<"Use the static host OpenMP runtime while linking.">;
3825-
def opaque_offload_linker : Flag<["--"], "opaque-offload-linker">, Visibility<[ClangOption, CC1Option]>, Group<f_Group>,
3826-
HelpText<"Build/link omp offload binary, construct opaque cmd list instead of single clang-linker-wrapper cmd.">;
3827-
def no_opaque_offload_linker : Flag<["--"], "no-opaque-offload-linker">, Visibility<[ClangOption, CC1Option]>, Group<f_Group>,
3828-
HelpText<"Build/link omp offload binary, using single clang-linker-wrapper cmd.">;
3825+
def opaque_offload_linker
3826+
: Flag<["--"], "opaque-offload-linker">,
3827+
Visibility<[ClangOption, FlangOption]>,
3828+
Group<f_Group>,
3829+
HelpText<"Build/link omp offload binary, construct opaque cmd list "
3830+
"instead of single clang-linker-wrapper cmd.">;
3831+
def no_opaque_offload_linker : Flag<["--"], "no-opaque-offload-linker">,
3832+
Visibility<[ClangOption, FlangOption]>,
3833+
Group<f_Group>,
3834+
HelpText<"Build/link omp offload binary, using "
3835+
"single clang-linker-wrapper cmd.">;
38293836
def fopenmp_new_driver : Flag<["-"], "fopenmp-new-driver">, Flags<[HelpHidden]>,
38303837
HelpText<"Use the new driver for OpenMP offloading.">;
38313838
def fno_openmp_new_driver : Flag<["-"], "fno-openmp-new-driver">,

clang/lib/Driver/ToolChains/AMDGPUOpenMP.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,11 @@ const char *amdgpu::dlr::getLinkCommandArgs(
269269
<< "libomptarget-amdgpu.bc";
270270
}
271271

272-
// Add the generic set of libraries, OpenMP subset only
273-
BCLibs.append(amdgpu::dlr::getCommonDeviceLibNames(
274-
C.getArgs(), TC.getSanitizerArgs(C.getArgs()), C.getDriver(),
275-
GPUArch.str(), /* isOpenMP=*/true, RocmInstallation));
272+
if (!Args.hasArg(options::OPT_no_offloadlib))
273+
// Add the generic set of libraries, OpenMP subset only
274+
BCLibs.append(amdgpu::dlr::getCommonDeviceLibNames(
275+
C.getArgs(), TC.getSanitizerArgs(C.getArgs()), C.getDriver(),
276+
GPUArch.str(), /* isOpenMP=*/true, RocmInstallation));
276277
}
277278

278279
llvm::for_each(BCLibs, [&](auto BCLib) {

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9370,6 +9370,24 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
93709370
const InputInfoList &Inputs,
93719371
const ArgList &Args,
93729372
const char *LinkingOutput) const {
9373+
bool isAMDGPU = false;
9374+
auto offloadTC = C.getOffloadToolChains(Action::OFK_OpenMP);
9375+
const auto OpenMPTCs = llvm::make_range(offloadTC.first, offloadTC.second);
9376+
const ToolChain *OTC;
9377+
for (auto &I : OpenMPTCs) {
9378+
OTC = I.second;
9379+
if (OTC->getTriple().isAMDGPU()) {
9380+
isAMDGPU = true;
9381+
break;
9382+
}
9383+
}
9384+
if (isAMDGPU && Args.hasFlag(options::OPT_opaque_offload_linker,
9385+
options::OPT_no_opaque_offload_linker, false)) {
9386+
ConstructOpaqueJob(C, JA, Output, Inputs, Args, OTC->getTriple(),
9387+
LinkingOutput);
9388+
return;
9389+
}
9390+
93739391
using namespace options;
93749392

93759393
// A list of permitted options that will be forwarded to the embedded device

clang/test/Driver/clang-offload-wrapper.c

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,37 @@
2727
// ELF-WARNING: is not an ELF image, so notes cannot be added to it.
2828
// CHECK-IR: target triple = "x86_64-pc-linux-gnu"
2929

30-
// CHECK-IR-DAG: [[ENTTY:%.+]] = type { ptr, ptr, i{{32|64}}, i32, i32 }
30+
// CHECK-IR-DAG: [[ENTTY:%.+]] = type { i64, i16, i16, i32, ptr, ptr, i64, i64, ptr }
3131
// CHECK-IR-DAG: [[IMAGETY:%.+]] = type { ptr, ptr, ptr, ptr }
3232
// CHECK-IR-DAG: [[DESCTY:%.+]] = type { i32, ptr, ptr, ptr }
33-
34-
// CHECK-IR: [[ENTBEGIN:@.+]] = external hidden constant [[ENTTY]]
35-
// CHECK-IR: [[ENTEND:@.+]] = external hidden constant [[ENTTY]]
36-
37-
// CHECK-IR: [[DUMMY:@.+]] = weak hidden constant [0 x [[ENTTY]]] zeroinitializer, section "omp_offloading_entries"
33+
//
34+
// CHECK-IR: [[ENTBEGIN:@.+]] = external hidden constant [0 x [[ENTTY]]]
35+
// CHECK-IR: [[ENTEND:@.+]] = external hidden constant [0 x [[ENTTY]]]
36+
// CHECK-IR: [[DUMMY:@.+]] = internal constant [0 x [[ENTTY]]] zeroinitializer, section "llvm_offload_entries", align 8
37+
// CHECK-IR: @llvm.compiler.used = appending global [1 x ptr] [ptr [[DUMMY]]], section "llvm.metadata"
3838

3939
// CHECK-IR: [[BIN:@.+]] = internal unnamed_addr constant [[[SIZE:[0-9]+]] x i8] c"\10\FF\10\AD{{.*}}"
40-
41-
// CHECK-IR: [[IMAGES:@.+]] = internal unnamed_addr constant [1 x [[IMAGETY]]] [[[IMAGETY]] { ptr [[BIN]], ptr getelementptr ([[[SIZE]] x i8], ptr [[BIN]], i64 0, i64 160), ptr [[ENTBEGIN]], ptr [[ENTEND]] }]
42-
40+
ffloading.device_image = internal unnamed_addr constant [[[SIZE]] x i8] c"\10\FF\10\AD\01\00\00\0
41+
// CHECK-IR: [[IMAGES:@.+]] = internal unnamed_addr constant [1 x %__tgt_device_image] [%__tgt_device_image { ptr getelementptr ([[[SIZE]] x i8], ptr [[BIN]], i64 0, i64 136), ptr getelementptr ([[[SIZE]] x i8], ptr [[BIN]], i64 0, i64 159), ptr [[ENTBEGIN]], ptr [[ENTEND]] }]
4342
// CHECK-IR: [[DESC:@.+]] = internal constant [[DESCTY]] { i32 1, ptr [[IMAGES]], ptr [[ENTBEGIN]], ptr [[ENTEND]] }
43+
// CHECK-IR: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 101, ptr [[REGFN:@.+]], ptr null }]
4444

45-
// CHECK-IR: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 1, ptr [[REGFN:@.+]], ptr null }]
46-
47-
// CHECK-IR: define internal void [[REGFN]]()
45+
// CHECK-IR: define internal void [[REGFN]]() section ".text.startup" {
4846
// CHECK-IR: call void @__tgt_register_lib(ptr [[DESC]])
47+
// CHECK-IR: %0 = call i32 @atexit(ptr @.omp_offloading.descriptor_unreg)
4948
// CHECK-IR: ret void
49+
// CHECK-IR: }
5050

5151
// CHECK-IR: declare void @__tgt_register_lib(ptr)
5252

53+
// CHECK-IR: declare i32 @atexit(ptr)
54+
55+
// CHECK-IR: define internal void [[DESC]]_unreg() section ".text.startup" {
5356
// CHECK-IR: call void @__tgt_unregister_lib(ptr [[DESC]])
5457
// CHECK-IR: ret void
58+
// CHECK-IR: }
5559

56-
// CHECK-IR: declare void @__tgt_unregister_lib(ptr)
60+
// CHECK_IR: declare void @__tgt_unregister_lib(ptr)
5761

5862
// Check that clang-offload-wrapper adds LLVMOMPOFFLOAD notes
5963
// into the ELF offload images:

0 commit comments

Comments
 (0)