Skip to content

Commit a8609a5

Browse files
authored
[New offload][llc] Pass -relocation-model=pic option to llc when building shared libraries (#13687)
When user is building a shared library using '-shared' option, the llc step run after wrapping device code needs to be run with '-relocation-model=pic' option. This PR adds the above option for the new offloading model. One other change has been added to address test failures resulting from adding a new SYCL E2E test: - Wrapper logic needs to use the 'arch' to specify the target and not use the entire triple. This is required for successful parsing by SYCL runtime. Thanks --------- Signed-off-by: Arvind Sudarsanam <[email protected]>
1 parent ab6cc47 commit a8609a5

File tree

7 files changed

+39
-2
lines changed

7 files changed

+39
-2
lines changed

clang/test/Driver/linker-wrapper-sycl.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@
1010
// CHK-CMDS-NEXT: offload-wrapper: input: [[LLVMSPIRVOUT:.*]].table, output: [[WRAPPEROUT:.*]].bc
1111
// CHK-CMDS-NEXT: "{{.*}}llc" -filetype=obj -o [[LLCOUT:.*]].o [[WRAPPEROUT]].bc
1212
// CHK-CMDS-NEXT: "{{.*}}/ld" -- HOST_LINKER_FLAGS -dynamic-linker HOST_DYN_LIB -o a.out [[LLCOUT]].o HOST_LIB_PATH HOST_STAT_LIB {{.*}}test-sycl.o
13+
14+
/// check for PIC for device wrap compilation when using -shared
15+
// RUN: clang-linker-wrapper -sycl-device-library-location=%S/Inputs -sycl-device-libraries=libsycl-crt.new.o,libsycl-complex.new.o -sycl-post-link-options="SYCL_POST_LINK_OPTIONS" -llvm-spirv-options="LLVM_SPIRV_OPTIONS" "--host-triple=x86_64-unknown-linux-gnu" "--triple=spir64" "--linker-path=/usr/bin/ld" -shared "--" HOST_LINKER_FLAGS "-dynamic-linker" HOST_DYN_LIB "-o" "a.out" HOST_LIB_PATH HOST_STAT_LIB %S/Inputs/test-sycl.o --dry-run 2>&1 | FileCheck -check-prefix=CHK-SHARED %s
16+
// CHK-SHARED: "{{.*}}llc"{{.*}} -relocation-model=pic

clang/test/Driver/sycl-linker-wrapper-image.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ int main() {
3131
// CHECK-DAG: %__sycl.tgt_device_image = type { i16, i8, i8, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr }
3232
// CHECK-DAG: %__sycl.tgt_bin_desc = type { i16, i16, ptr, ptr, ptr }
3333

34-
// CHECK-DAG: @.sycl_offloading.target.0 = internal unnamed_addr constant [23 x i8] c"spir64-unknown-unknown\00"
34+
// CHECK-DAG: @.sycl_offloading.target.0 = internal unnamed_addr constant [7 x i8] c"spir64\00"
3535
// CHECK-DAG: @.sycl_offloading.opts.compile.0 = internal unnamed_addr constant [1 x i8] zeroinitializer
3636
// CHECK-DAG: @.sycl_offloading.opts.link.0 = internal unnamed_addr constant [1 x i8] zeroinitializer
3737
// CHECK-DAG: @prop = internal unnamed_addr constant [17 x i8] c"DeviceLibReqMask\00"

clang/test/Driver/sycl-offload-new-driver.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,9 @@
7777
// DEVICE_ONLY: 2: compiler, {1}, ir, (device-sycl)
7878
// DEVICE_ONLY: 3: backend, {2}, ir, (device-sycl)
7979
// DEVICE_ONLY: 4: offload, "device-sycl (spir64-unknown-unknown)" {3}, none
80+
81+
/// check for -shared transmission to clang-linker-wrapper tool
82+
// RUN: %clangxx -### -fsycl --offload-new-driver -target x86_64-unknown-linux-gnu \
83+
// RUN: -shared %s 2>&1 \
84+
// RUN: | FileCheck -check-prefix=CHECK_SHARED %s
85+
// CHECK_SHARED: clang-linker-wrapper{{.*}} "-shared"

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -767,8 +767,13 @@ Expected<StringRef> wrapSYCLBinariesFromFile(StringRef InputFile,
767767
inconvertibleErrorCode(),
768768
"can't wrap SYCL image. -triple argument is missed.");
769769

770+
// SYCL runtime currently works for spir64 target triple and not for
771+
// spir64-unknown-unknown.
772+
// TODO: Fix SYCL runtime to accept both triple
773+
llvm::Triple T(Target);
774+
StringRef A(T.getArchName());
770775
for (offloading::SYCLImage &Image : Images)
771-
Image.Target = Target;
776+
Image.Target = A;
772777

773778
LLVMContext C;
774779
Module M("offload.wrapper.object", C);
@@ -814,6 +819,9 @@ static Expected<StringRef> runCompile(StringRef &InputFile,
814819

815820
SmallVector<StringRef, 8> CmdArgs;
816821
CmdArgs.push_back(*LLCPath);
822+
// Checking for '-shared' linker option
823+
if (Args.hasArg(OPT_shared))
824+
CmdArgs.push_back("-relocation-model=pic");
817825
CmdArgs.push_back("-filetype=obj");
818826
CmdArgs.push_back("-o");
819827
CmdArgs.push_back(*OutputFileOrErr);

clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ def relocatable : Flag<["--", "-"], "relocatable">,
135135
HelpText<"Link device code to create a relocatable offloading application">;
136136
def r : Flag<["-"], "r">, Alias<relocatable>;
137137

138+
def shared : Flag<["--", "-"], "shared">,
139+
HelpText<"Link device code to create a shared library">;
140+
138141
// link.exe-style linker options.
139142
def out : Joined<["/", "-", "/?", "-?"], "out:">, Flags<[HelpHidden]>;
140143
def libpath : Joined<["/", "-", "/?", "-?"], "libpath:">, Flags<[HelpHidden]>;

sycl/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,8 @@ set( SYCL_TOOLCHAIN_DEPLOY_COMPONENTS
414414
clang-offload-bundler
415415
clang-offload-deps
416416
clang-offload-extract
417+
clang-offload-packager
418+
clang-linker-wrapper
417419
file-table-tform
418420
llc
419421
llvm-ar
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// REQUIRES: opencl, cpu, linux
2+
// This test checks for correct behavior for shared library builds when new
3+
// offload driver is enabled. Currently, new offload model supports only JIT.
4+
// TODO: Expand the test once AOT support for new offload model is ready.
5+
//
6+
// RUN: %clangxx -fsycl -fsycl-targets=spir64 --offload-new-driver -DBUILD_LIB -fPIC -shared %s -o %T/lib%basename_t.so
7+
8+
// RUN: %clangxx -fsycl -fsycl-targets=spir64 --offload-new-driver -DFOO_FIRST -L%T %s -o %t.out -l%basename_t -Wl,-rpath=%T
9+
// RUN: %{run} %t.out
10+
11+
// RUN: %clangxx -fsycl -fsycl-targets=spir64 --offload-new-driver -L%T %s -o %t.out -l%basename_t -Wl,-rpath=%T
12+
// RUN: %{run} %t.out
13+
14+
#include "use_when_link.cpp"

0 commit comments

Comments
 (0)