Skip to content

Commit

Permalink
[SYCL][Driver][thinLTO] Don't pass -emit-only-kernels-as-entry-points…
Browse files Browse the repository at this point in the history
… to sycl-post-link in early splitting (#14991)

In early splitting with thinLTO, we could be generating an `.o` to be
linked in with other files later. Right now, passing
`-emit-only-kernels-as-entry-points` to `sycl-post-link` can cause
functions to get dropped even though they are used by some other `.o`.

We will need to prune non-entry points inside the thinLTO processing
inside `clang-linker-wrapper`, but that's not implemented yet.

---------

Signed-off-by: Sarnie, Nick <nick.sarnie@intel.com>
  • Loading branch information
sarnex authored Aug 8, 2024
1 parent 121a7d5 commit d893994
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
38 changes: 27 additions & 11 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10741,6 +10741,32 @@ static void getNonTripleBasedSYCLPostLinkOpts(const ToolChain &TC,
addArgs(PostLinkArgs, TCArgs, {"-support-dynamic-linking"});
}

// On Intel targets we don't need non-kernel functions as entry points,
// because it only increases amount of code for device compiler to handle,
// without any actual benefits.
// TODO: Try to extend this feature for non-Intel GPUs.
static bool shouldEmitOnlyKernelsAsEntryPoints(const ToolChain &TC,
const llvm::opt::ArgList &TCArgs,
llvm::Triple Triple) {
if (TCArgs.hasFlag(options::OPT_fno_sycl_remove_unused_external_funcs,
options::OPT_fsycl_remove_unused_external_funcs, false))
return false;
if (isSYCLNativeCPU(TC))
return false;
// When supporting dynamic linking, non-kernels in a device image can be
// called.
if (supportDynamicLinking(TCArgs))
return false;
if (Triple.isNVPTX() || Triple.isAMDGPU())
return false;
bool IsUsingLTO = TC.getDriver().isUsingLTO(/*IsDeviceOffloadAction=*/true);
auto LTOMode = TC.getDriver().getLTOMode(/*IsDeviceOffloadAction=*/true);
// With thinLTO, final entry point handing is done in clang-linker-wrapper
if (IsUsingLTO && LTOMode == LTOK_Thin)
return false;
return true;
}

// Add any sycl-post-link options that rely on a specific Triple in addition
// to user supplied options. This function is invoked only for the old
// offloading model. For the new offloading model, a slightly modified version
Expand Down Expand Up @@ -10778,17 +10804,7 @@ static void getTripleBasedSYCLPostLinkOpts(const ToolChain &TC,
(Triple.getArchName() != "spir64_fpga"))
addArgs(PostLinkArgs, TCArgs, {"-split=auto"});

// On Intel targets we don't need non-kernel functions as entry points,
// because it only increases amount of code for device compiler to handle,
// without any actual benefits.
// TODO: Try to extend this feature for non-Intel GPUs.
if ((!TCArgs.hasFlag(options::OPT_fno_sycl_remove_unused_external_funcs,
options::OPT_fsycl_remove_unused_external_funcs,
false) &&
!isSYCLNativeCPU(TC)) &&
// When supporting dynamic linking, non-kernels in a device image can be
// called.
!supportDynamicLinking(TCArgs) && !Triple.isNVPTX() && !Triple.isAMDGPU())
if (shouldEmitOnlyKernelsAsEntryPoints(TC, TCArgs, Triple))
addArgs(PostLinkArgs, TCArgs, {"-emit-only-kernels-as-entry-points"});

if (!Triple.isAMDGCN())
Expand Down
5 changes: 3 additions & 2 deletions clang/test/Driver/sycl-lto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
// CHECK_SPLIT_ERROR: '-fsycl-device-code-split=off' is not supported when '-foffload-lto=thin' is set with '-fsycl'

// Verify there's no error and we see the expected cc1 flags and tool invocations with the new offload driver.
// RUN: %clangxx -fsycl --offload-new-driver -foffload-lto=thin %s -### 2>&1 | FileCheck -check-prefix=CHECK_SUPPORTED %s
// RUN: %clangxx -fsycl --offload-new-driver -foffload-lto=thin %s -### 2>&1 | \
// RUN: FileCheck -check-prefix=CHECK_SUPPORTED -implicit-check-not=-emit-only-kernels-as-entry-points %s
// CHECK_SUPPORTED: clang{{.*}} "-cc1" "-triple" "spir64-unknown-unknown" {{.*}} "-flto=thin" "-flto-unit"
// CHECK_SUPPORTED: sycl-post-link{{.*}}
// CHECK_SUPPORTED: sycl-post-link
// CHECK_SUPPORTED-NOT: -properties
// CHECK_SUPPORTED-NEXT: file-table-tform{{.*}}
// CHECK_SUPPORTED-NEXT: llvm-foreach{{.*}} "--" {{.*}}clang{{.*}} "-fsycl-is-device"{{.*}} "-flto=thin" "-flto-unit"
Expand Down

0 comments on commit d893994

Please sign in to comment.