From 620ac3520e6ff67afd8f382b3e1753955e5d3e4f Mon Sep 17 00:00:00 2001 From: Udit Agarwal Date: Fri, 15 Nov 2024 10:13:57 -0800 Subject: [PATCH] [Clang] Link ITT libraries in device code by default (#16094) Fixes: CMPLRLLVM-63157 **Problem** Consider the following case: ``` clang++ -fsycl -c testFile.cpp -o obj1.o clang++ -fsycl -c testFile2.cpp -o obj2.o -fsycl-instrument-device-code clang++ -fsycl obj1.o obj2.o -o test.exe // test.exe fails with: JIT session error: Symbols not found: [ __itt_offload_wi_finish_wrapper, __itt_offload_wi_start_wrapper ] ``` This issue was observed while using MKL static libraries built with `-fsycl-instrument-device-code` with the latest compiler that does not link ITT annotations by default. With this change, we link in ITT libraries by default to stays ABI compliant with the previous release. During device code linking, if the device code is not instrumented with ITT annotations, this library will be omitted. Note that, even with this change, we are not instrumenting device code with ITT annotations by default. --- clang/lib/Driver/ToolChains/SYCL.cpp | 5 ++++- clang/test/Driver/sycl-instrumentation-old-model.c | 8 ++++++-- clang/test/Driver/sycl-instrumentation.c | 8 ++++++-- clang/test/Driver/sycl-offload-new-driver.c | 2 +- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/clang/lib/Driver/ToolChains/SYCL.cpp b/clang/lib/Driver/ToolChains/SYCL.cpp index 1d903aa1fc17c..6831938fd4c6d 100644 --- a/clang/lib/Driver/ToolChains/SYCL.cpp +++ b/clang/lib/Driver/ToolChains/SYCL.cpp @@ -650,8 +650,11 @@ SYCL::getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple, addLibraries(SYCLDeviceBfloat16FallbackLib); } + // Link in ITT annotations library unless fsycl-no-instrument-device-code + // is specified. This ensures that we are ABI-compatible with the + // instrumented device code, which was the default not so long ago. if (Args.hasFlag(options::OPT_fsycl_instrument_device_code, - options::OPT_fno_sycl_instrument_device_code, false)) + options::OPT_fno_sycl_instrument_device_code, true)) addLibraries(SYCLDeviceAnnotationLibs); #if !defined(_WIN32) diff --git a/clang/test/Driver/sycl-instrumentation-old-model.c b/clang/test/Driver/sycl-instrumentation-old-model.c index 3e07d6d1ca298..bf1471fad84ef 100644 --- a/clang/test/Driver/sycl-instrumentation-old-model.c +++ b/clang/test/Driver/sycl-instrumentation-old-model.c @@ -20,12 +20,16 @@ // CHECK-SPIRV-SAME: "{{.*}}libsycl-itt-stubs.bc" // CHECK-HOST-NOT: "-cc1"{{.*}} "-fsycl-is-host"{{.*}} "-fsycl-instrument-device-code" -// ITT annotations in device code are disabled by default. +// ITT annotations in device code are disabled by default. However, for SYCL offloading, +// we still link ITT annotations libraries to ensure ABI compatibility with previous release. // RUN: %clangxx -fsycl --no-offload-new-driver -fsycl-targets=spir64 -### %s 2>&1 \ -// RUN: | FileCheck -check-prefixes=CHECK-NONPASSED %s +// RUN: | FileCheck -check-prefixes=CHECK-ITT-LINK-ONLY %s // RUN: %clangxx -fsycl --no-offload-new-driver -fsycl-targets=nvptx64-nvidia-cuda -nocudalib -### %s 2>&1 \ // RUN: | FileCheck -check-prefixes=CHECK-NONPASSED %s +// CHECK-ITT-LINK-ONLY-NOT: "-fsycl-instrument-device-code" +// CHECK-ITT-LINK-ONLY: llvm-link{{.*}} {{.*}}libsycl-itt-{{.*}} + // RUN: %clangxx -fsycl --no-offload-new-driver -fno-sycl-instrument-device-code -fsycl-targets=spir64 -### %s 2>&1 \ // RUN: | FileCheck -check-prefixes=CHECK-NONPASSED %s // RUN: %clangxx -fsycl --no-offload-new-driver -fsycl-targets=nvptx64-nvidia-cuda -fno-sycl-instrument-device-code -nocudalib -### %s 2>&1 \ diff --git a/clang/test/Driver/sycl-instrumentation.c b/clang/test/Driver/sycl-instrumentation.c index ccb3d857d46af..c2dbf8b6b83f7 100644 --- a/clang/test/Driver/sycl-instrumentation.c +++ b/clang/test/Driver/sycl-instrumentation.c @@ -19,12 +19,16 @@ // CHECK-SPIRV-SAME: libsycl-itt-compiler-wrappers.new.o // CHECK-SPIRV-SAME: libsycl-itt-stubs.new.o -// ITT annotations in device code are disabled by default. +// ITT annotations in device code are disabled by default. However, for SYCL offloading, +// we still link ITT annotations libraries to ensure ABI compatibility with previous release. // RUN: %clangxx -fsycl --offload-new-driver -fsycl-targets=spir64 -### %s 2>&1 \ -// RUN: | FileCheck -check-prefixes=CHECK-NONPASSED %s +// RUN: | FileCheck -check-prefixes=CHECK-ITT-LINK-ONLY %s // RUN: %clangxx -fsycl --offload-new-driver -fsycl-targets=nvptx64-nvidia-cuda -nocudalib -### %s 2>&1 \ // RUN: | FileCheck -check-prefixes=CHECK-NONPASSED %s +// CHECK-ITT-LINK-ONLY-NOT: "-fsycl-instrument-device-code" +// CHECK-ITT-LINK-ONLY: clang-linker-wrapper{{.*}} {{.*}}libsycl-itt-{{.*}} + // RUN: %clangxx -fsycl --offload-new-driver -fno-sycl-instrument-device-code -fsycl-targets=spir64 -### %s 2>&1 \ // RUN: | FileCheck -check-prefixes=CHECK-NONPASSED %s // RUN: %clangxx -fsycl --offload-new-driver -fsycl-targets=nvptx64-nvidia-cuda -fno-sycl-instrument-device-code -nocudalib -### %s 2>&1 \ diff --git a/clang/test/Driver/sycl-offload-new-driver.c b/clang/test/Driver/sycl-offload-new-driver.c index b6732b3e9312e..dd656192b80f3 100644 --- a/clang/test/Driver/sycl-offload-new-driver.c +++ b/clang/test/Driver/sycl-offload-new-driver.c @@ -34,7 +34,7 @@ // RUN: %clangxx --target=x86_64-unknown-linux-gnu -fsycl --offload-new-driver \ // RUN: --sysroot=%S/Inputs/SYCL -### %s 2>&1 \ // RUN: | FileCheck -check-prefix WRAPPER_OPTIONS %s -// WRAPPER_OPTIONS: clang-linker-wrapper{{.*}} "-sycl-device-libraries=libsycl-crt.new.o,libsycl-complex.new.o,libsycl-complex-fp64.new.o,libsycl-cmath.new.o,libsycl-cmath-fp64.new.o,libsycl-imf.new.o,libsycl-imf-fp64.new.o,libsycl-imf-bf16.new.o,libsycl-fallback-cassert.new.o,libsycl-fallback-cstring.new.o,libsycl-fallback-complex.new.o,libsycl-fallback-complex-fp64.new.o,libsycl-fallback-cmath.new.o,libsycl-fallback-cmath-fp64.new.o,libsycl-fallback-imf.new.o,libsycl-fallback-imf-fp64.new.o,libsycl-fallback-imf-bf16.new.o" +// WRAPPER_OPTIONS: clang-linker-wrapper{{.*}} "-sycl-device-libraries=libsycl-crt.new.o,libsycl-complex.new.o,libsycl-complex-fp64.new.o,libsycl-cmath.new.o,libsycl-cmath-fp64.new.o,libsycl-imf.new.o,libsycl-imf-fp64.new.o,libsycl-imf-bf16.new.o,libsycl-fallback-cassert.new.o,libsycl-fallback-cstring.new.o,libsycl-fallback-complex.new.o,libsycl-fallback-complex-fp64.new.o,libsycl-fallback-cmath.new.o,libsycl-fallback-cmath-fp64.new.o,libsycl-fallback-imf.new.o,libsycl-fallback-imf-fp64.new.o,libsycl-fallback-imf-bf16.new.o,libsycl-itt-user-wrappers.new.o,libsycl-itt-compiler-wrappers.new.o,libsycl-itt-stubs.new.o" // WRAPPER_OPTIONS-SAME: "-sycl-device-library-location={{.*}}/lib" /// Verify phases used to generate SPIR-V instead of LLVM-IR