[clang] [clang][CodeGen] Add AS for Globals to SPIR & SPIRV datalayouts (PR #88455)

2024-04-14 Thread Arvind Sudarsanam via cfe-commits

asudarsa wrote:

Thanks @AlexVlx for this change. This should work fine for 
SPIRV-LLVM-Translator (and SPIR-V backend). Adding @michalpaszkowski for input 
from SPIR-V backend side. Recently, this restriction on LLVM IR input to our 
translator was docuemnted:
https://github.com/KhronosGroup/SPIRV-LLVM-Translator/blob/main/docs/SPIRVRepresentationInLLVM.rst#global-variables
_"A global variable resides in an address space, and the default address space 
in LLVM is zero. The SPIR-V storage class represented by the zero LLVM IR 
address spaces is Function. However, SPIR-V global variable declarations are 
OpVariable instructions whose Storage Class cannot be Function. This means that 
global variable declarations must always have an address space specified and 
that address space cannot be 0."_
So, your change will help to make the LLVM IR more suitable for the translator.

One quick pointer. I did notice a similar commit for the AMDGPU backend - 
https://reviews.llvm.org/D84345
Here, there are some updates to the llvm/lib/IR/AutoUpgrade.cpp. Do we need 
similar changes here?

Thanks

https://github.com/llvm/llvm-project/pull/88455
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [clang][SPIR-V] Add support for AMDGCN flavoured SPIRV (PR #89796)

2024-05-30 Thread Arvind Sudarsanam via cfe-commits

asudarsa wrote:

> I'm okay with the patch in general, but I'd like either @michalpaszkowski or 
> @VyacheslavLevytskyy to take a look. Just one question about doubling the 
> test scope for LLVM tests with spir64 target triple.

+1 on this. I will take a look before end of week on this. Thanks

https://github.com/llvm/llvm-project/pull/89796
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [clang][CodeGen] Add AS for Globals to SPIR & SPIRV datalayouts (PR #88455)

2024-04-15 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa approved this pull request.

Thanks for this change. LGTM

https://github.com/llvm/llvm-project/pull/88455
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-14 Thread Arvind Sudarsanam via cfe-commits


@@ -0,0 +1,530 @@
+//=-- clang-sycl-link-wrapper/ClangSYCLLinkWrapper.cpp - SYCL linker util --=//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===-===//
+//
+// This tool wraps around the sequence of steps required to link device code in
+// SYCL fat objects. SYCL device code linking requires a complex sequence of
+// steps that include linking of llvm bitcode files, linking device library
+// files with the fully linked source bitcode file(s), running several SYCL
+// specific post-link steps on the fully linked bitcode file(s), and finally
+// generating target-specific device code. This tool can be removed once SYCL
+// linking is ported to `ld.lld`.
+//
+//===-===//
+
+#include "clang/Basic/Version.h"
+
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/BinaryFormat/Magic.h"
+#include "llvm/Bitcode/BitcodeWriter.h"
+#include "llvm/CodeGen/CommandFlags.h"
+#include "llvm/IR/DiagnosticPrinter.h"
+#include "llvm/IRReader/IRReader.h"
+#include "llvm/LTO/LTO.h"
+#include "llvm/Object/Archive.h"
+#include "llvm/Object/ArchiveWriter.h"
+#include "llvm/Object/Binary.h"
+#include "llvm/Object/ELFObjectFile.h"
+#include "llvm/Object/IRObjectFile.h"
+#include "llvm/Object/ObjectFile.h"
+#include "llvm/Object/OffloadBinary.h"
+#include "llvm/Option/ArgList.h"
+#include "llvm/Option/OptTable.h"
+#include "llvm/Option/Option.h"
+#include "llvm/Remarks/HotnessThresholdParser.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/FileOutputBuffer.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/InitLLVM.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/Path.h"
+#include "llvm/Support/Program.h"
+#include "llvm/Support/Signals.h"
+#include "llvm/Support/StringSaver.h"
+#include "llvm/Support/TargetSelect.h"
+#include "llvm/Support/TimeProfiler.h"
+#include "llvm/Support/WithColor.h"
+
+using namespace llvm;
+using namespace llvm::opt;
+using namespace llvm::object;
+
+/// Save intermediary results.
+static bool SaveTemps = false;
+
+/// Print arguments without executing.
+static bool DryRun = false;
+
+/// Print verbose output.
+static bool Verbose = false;
+
+/// Filename of the output being created.
+static StringRef OutputFile;
+
+/// Directory to dump SPIR-V IR if requested by user.
+static SmallString<128> SPIRVDumpDir;
+
+static void printVersion(raw_ostream &OS) {
+  OS << clang::getClangToolFullVersion("clang-sycl-link-wrapper") << '\n';
+}
+
+/// The value of `argv[0]` when run.
+static const char *Executable;
+
+/// Temporary files to be cleaned up.
+static SmallVector> TempFiles;
+
+namespace {
+// Must not overlap with llvm::opt::DriverFlag.
+enum WrapperFlags { WrapperOnlyOption = (1 << 4) };
+
+enum ID {
+  OPT_INVALID = 0, // This is not an option ID.
+#define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
+#include "SYCLLinkOpts.inc"
+  LastOption
+#undef OPTION
+};
+
+#define PREFIX(NAME, VALUE)
\
+  static constexpr StringLiteral NAME##_init[] = VALUE;
\
+  static constexpr ArrayRef NAME(NAME##_init,   
\
+std::size(NAME##_init) - 1);
+#include "SYCLLinkOpts.inc"
+#undef PREFIX
+
+static constexpr OptTable::Info InfoTable[] = {

asudarsa wrote:

Most of the 'helper' routines getOptTable, createTempFile, etc are routines 
that have been borrowed from clang-linker-wrapper and clang-nvlink-wrapper. In 
an upcoming effort, we can try to create a single space for these routines to 
exist.

Thanks

https://github.com/llvm/llvm-project/pull/112245
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-14 Thread Arvind Sudarsanam via cfe-commits


@@ -0,0 +1,530 @@
+//=-- clang-sycl-link-wrapper/ClangSYCLLinkWrapper.cpp - SYCL linker util --=//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===-===//
+//
+// This tool wraps around the sequence of steps required to link device code in
+// SYCL fat objects. SYCL device code linking requires a complex sequence of
+// steps that include linking of llvm bitcode files, linking device library
+// files with the fully linked source bitcode file(s), running several SYCL
+// specific post-link steps on the fully linked bitcode file(s), and finally
+// generating target-specific device code. This tool can be removed once SYCL
+// linking is ported to `ld.lld`.
+//
+//===-===//
+
+#include "clang/Basic/Version.h"
+
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/BinaryFormat/Magic.h"
+#include "llvm/Bitcode/BitcodeWriter.h"
+#include "llvm/CodeGen/CommandFlags.h"
+#include "llvm/IR/DiagnosticPrinter.h"
+#include "llvm/IRReader/IRReader.h"
+#include "llvm/LTO/LTO.h"
+#include "llvm/Object/Archive.h"
+#include "llvm/Object/ArchiveWriter.h"
+#include "llvm/Object/Binary.h"
+#include "llvm/Object/ELFObjectFile.h"
+#include "llvm/Object/IRObjectFile.h"
+#include "llvm/Object/ObjectFile.h"
+#include "llvm/Object/OffloadBinary.h"
+#include "llvm/Option/ArgList.h"
+#include "llvm/Option/OptTable.h"
+#include "llvm/Option/Option.h"
+#include "llvm/Remarks/HotnessThresholdParser.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/FileOutputBuffer.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/InitLLVM.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/Path.h"
+#include "llvm/Support/Program.h"
+#include "llvm/Support/Signals.h"
+#include "llvm/Support/StringSaver.h"
+#include "llvm/Support/TargetSelect.h"
+#include "llvm/Support/TimeProfiler.h"
+#include "llvm/Support/WithColor.h"
+
+using namespace llvm;
+using namespace llvm::opt;
+using namespace llvm::object;
+
+/// Save intermediary results.
+static bool SaveTemps = false;
+
+/// Print arguments without executing.
+static bool DryRun = false;
+
+/// Print verbose output.
+static bool Verbose = false;
+
+/// Filename of the output being created.
+static StringRef OutputFile;
+
+/// Directory to dump SPIR-V IR if requested by user.
+static SmallString<128> SPIRVDumpDir;
+
+static void printVersion(raw_ostream &OS) {
+  OS << clang::getClangToolFullVersion("clang-sycl-link-wrapper") << '\n';
+}
+
+/// The value of `argv[0]` when run.
+static const char *Executable;
+
+/// Temporary files to be cleaned up.
+static SmallVector> TempFiles;
+
+namespace {
+// Must not overlap with llvm::opt::DriverFlag.
+enum WrapperFlags { WrapperOnlyOption = (1 << 4) };
+
+enum ID {
+  OPT_INVALID = 0, // This is not an option ID.
+#define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
+#include "SYCLLinkOpts.inc"
+  LastOption
+#undef OPTION
+};
+
+#define PREFIX(NAME, VALUE)
\
+  static constexpr StringLiteral NAME##_init[] = VALUE;
\
+  static constexpr ArrayRef NAME(NAME##_init,   
\
+std::size(NAME##_init) - 1);
+#include "SYCLLinkOpts.inc"
+#undef PREFIX
+
+static constexpr OptTable::Info InfoTable[] = {
+#define OPTION(...) LLVM_CONSTRUCT_OPT_INFO(__VA_ARGS__),
+#include "SYCLLinkOpts.inc"
+#undef OPTION
+};
+
+class WrapperOptTable : public opt::GenericOptTable {
+public:
+  WrapperOptTable() : opt::GenericOptTable(InfoTable) {}
+};
+
+const OptTable &getOptTable() {
+  static const WrapperOptTable *Table = []() {
+auto Result = std::make_unique();
+return Result.release();
+  }();
+  return *Table;
+}
+
+[[noreturn]] void reportError(Error E) {
+  outs().flush();
+  logAllUnhandledErrors(std::move(E), WithColor::error(errs(), Executable));
+  exit(EXIT_FAILURE);
+}
+
+std::string getMainExecutable(const char *Name) {
+  void *Ptr = (void *)(intptr_t)&getMainExecutable;
+  auto COWPath = sys::fs::getMainExecutable(Name, Ptr);
+  return sys::path::parent_path(COWPath).str();
+}
+
+Expected createTempFile(const ArgList &Args, const Twine &Prefix,
+   StringRef Extension) {
+  SmallString<128> OutputFile;
+  if (Args.hasArg(OPT_save_temps)) {
+// Generate a unique path name without creating a file
+sys::fs::createUniquePath(Prefix + "-%%." + Extension, OutputFile,
+  /*MakeAbsolute=*/false);
+  } else {
+if (std::error_code EC =
+sys::fs::createTemporaryFile(Prefix, Extension, OutputFile))
+  return createFileError(OutputFile, EC);
+  }
+
+  TempFiles.emplace_back(std::move(OutputFil

[clang] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-14 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa updated 
https://github.com/llvm/llvm-project/pull/112245

>From eff4a0300336c4c106e1d293b19e795f5ccbabc1 Mon Sep 17 00:00:00 2001
From: Arvind Sudarsanam 
Date: Fri, 27 Sep 2024 13:03:12 -0700
Subject: [PATCH 1/2] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link
 SYCL offloading device code

This PR is one of the many PRs in the SYCL upstreaming effort focusing on
device code linking during the SYCL offload compilation process.
RFC: 
https://discourse.llvm.org/t/rfc-offloading-design-for-sycl-offload-kind-and-spir-targets/74088

In this PR, we introduce a new tool that will be used to perform device code
linking for SYCL offload kind. It accepts SYCL device objects in LLVM IR bitcode
format and will generate a fully linked device object that can then be wrapped
and linked into the host object.

A primary use case for this tool is to perform device code linking for objects
with SYCL offload kind inside the clang-linker-wrapper.
It can also be invoked via clang driver as follows:

`clang --target=spirv64 --sycl-link input.bc`

Device code linking for SYCL offloading kind has a number of known quirks that
makes it difficult to use in a unified offloading setting.
Two of the primary issues are:
1. Several finalization steps are required to be run on the fully-linked LLVM
IR bitcode to gaurantee conformance to SYCL standards. This step is unique to
SYCL offloading compilation flow.
2. SPIR-V LLVM Translator tool is an extenal tool and hence SPIR-V IR code
generation cannot be done as part of LTO. This limitation will be lifted once
SPIR-V backend is available as a viable LLVM backend.

Hence, we introduce this new tool to provide a clean wrapper to perform SYCL
device linking.

Thanks

Signed-off-by: Arvind Sudarsanam 
---
 clang/docs/ClangSYCLLinkWrapper.rst   |  80 +++
 clang/docs/index.rst  |   1 +
 clang/include/clang/Driver/Options.td |   5 +-
 clang/lib/Driver/Driver.cpp   |   5 +
 clang/lib/Driver/ToolChains/SPIRV.cpp |  12 +
 clang/lib/Driver/ToolChains/SPIRV.h   |   5 +-
 clang/test/Driver/Inputs/libsycl-complex.bc   |   0
 clang/test/Driver/Inputs/libsycl-crt.bc   |   0
 .../Driver/clang-sycl-link-wrapper-test.cpp   |   9 +
 clang/test/Driver/sycl-link-spirv-target.cpp  |   7 +
 clang/tools/CMakeLists.txt|   1 +
 .../clang-sycl-link-wrapper/CMakeLists.txt|  28 +
 .../ClangSYCLLinkWrapper.cpp  | 530 ++
 .../clang-sycl-link-wrapper/SYCLLinkOpts.td   |  47 ++
 14 files changed, 727 insertions(+), 3 deletions(-)
 create mode 100644 clang/docs/ClangSYCLLinkWrapper.rst
 create mode 100644 clang/test/Driver/Inputs/libsycl-complex.bc
 create mode 100644 clang/test/Driver/Inputs/libsycl-crt.bc
 create mode 100644 clang/test/Driver/clang-sycl-link-wrapper-test.cpp
 create mode 100644 clang/test/Driver/sycl-link-spirv-target.cpp
 create mode 100644 clang/tools/clang-sycl-link-wrapper/CMakeLists.txt
 create mode 100644 clang/tools/clang-sycl-link-wrapper/ClangSYCLLinkWrapper.cpp
 create mode 100644 clang/tools/clang-sycl-link-wrapper/SYCLLinkOpts.td

diff --git a/clang/docs/ClangSYCLLinkWrapper.rst 
b/clang/docs/ClangSYCLLinkWrapper.rst
new file mode 100644
index 00..8ceb17f6af9d86
--- /dev/null
+++ b/clang/docs/ClangSYCLLinkWrapper.rst
@@ -0,0 +1,80 @@
+===
+Clang SYCL Link Wrapper
+===
+
+.. contents::
+   :local:
+
+.. _clang-sycl-link-wrapper:
+
+Introduction
+
+
+This tool works as a wrapper around the SYCL device code linking process.
+The purpose of this wrapper is to provide an interface to link SYCL device
+bitcode in LLVM IR format, SYCL device bitcode in SPIR-V IR format, and native
+binary objects, and then use the SPIR-V LLVM Translator tool on fully linked
+device objects to produce the final output.
+After the linking stage, the fully linked device code in LLVM IR format may
+undergo several SYCL-specific finalization steps before the SPIR-V code
+generation step.
+The wrapper will also support the Ahead-Of-Time (AOT) compilation flow. AOT
+compilation is the process of invoking the back-end at compile time to produce
+the final binary, as opposed to just-in-time (JIT) compilation when final code
+generation is deferred until application runtime.
+
+Device code linking for SYCL offloading has several known quirks that
+make it difficult to use in a unified offloading setting. Two of the primary
+issues are:
+1. Several finalization steps are required to be run on the fully linked LLVM
+IR bitcode to guarantee conformance to SYCL standards. This step is unique to
+the SYCL offloading compilation flow.
+2. The SPIR-V LLVM Translator tool is an external tool and hence SPIR-V IR code
+generation cannot be done as part of LTO. This limitation can be lifted once
+the SPIR-V backend is available as a viable LLVM backend.
+
+This tool has been proposed to work around these issu

[clang] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-14 Thread Arvind Sudarsanam via cfe-commits




asudarsa wrote:

Good point. I will create them inside the test case. I was trying to 'mimic' 
device library file usage. I suppose it's overkill. Thanks.

https://github.com/llvm/llvm-project/pull/112245
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-14 Thread Arvind Sudarsanam via cfe-commits


@@ -95,7 +95,19 @@ void SPIRV::Linker::ConstructJob(Compilation &C, const 
JobAction &JA,
   CmdArgs.push_back("-o");
   CmdArgs.push_back(Output.getFilename());
 
+  // Use of --sycl-link will call the clang-sycl-link-wrapper instead of
+  // the default linker (spirv-link).
+  if (Args.hasArg(options::OPT_sycl_link))
+Linker = ToolChain.GetProgramPath("clang-sycl-link-wrapper");

asudarsa wrote:

That sounds better. Will add the change. Thanks

https://github.com/llvm/llvm-project/pull/112245
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-14 Thread Arvind Sudarsanam via cfe-commits

asudarsa wrote:

Hi @jhuber6 

Thanks much for providing your feedback. I will address all your comments in an 
upcoming commit.

Sincerely

https://github.com/llvm/llvm-project/pull/112245
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-15 Thread Arvind Sudarsanam via cfe-commits


@@ -6728,7 +6728,10 @@ def fsycl : Flag<["-"], "fsycl">,
 def fno_sycl : Flag<["-"], "fno-sycl">,
   Visibility<[ClangOption, CLOption]>,
   Group, HelpText<"Disables SYCL kernels compilation for device">;
-
+def sycl_link : Flag<["--"], "sycl-link">, Flags<[HelpHidden]>,

asudarsa wrote:

We use this option to select between spirv-linker which is the standard linking 
tool for spir-v IR (but not mature enough for us to use it in our pipeline) and 
the clang-sycl-linker.

Thanks

https://github.com/llvm/llvm-project/pull/112245
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-15 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa edited 
https://github.com/llvm/llvm-project/pull/112245
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-15 Thread Arvind Sudarsanam via cfe-commits


@@ -95,7 +95,19 @@ void SPIRV::Linker::ConstructJob(Compilation &C, const 
JobAction &JA,
   CmdArgs.push_back("-o");
   CmdArgs.push_back(Output.getFilename());
 
+  // Use of --sycl-link will call the clang-sycl-link-wrapper instead of
+  // the default linker (spirv-link).
+  if (Args.hasArg(options::OPT_sycl_link))
+Linker = ToolChain.GetProgramPath("clang-sycl-link-wrapper");
   C.addCommand(std::make_unique(JA, *this, 
ResponseFileSupport::None(),
  Args.MakeArgString(Linker), CmdArgs,
  Inputs, Output));
 }
+
+SPIRVToolChain::SPIRVToolChain(const Driver &D, const llvm::Triple &Triple,
+   const ArgList &Args)
+: ToolChain(D, Triple, Args) {
+  NativeLLVMSupport = Args.hasArg(options::OPT_sycl_link);

asudarsa wrote:

We can eventually move this to SYCL toolchain when it is available upstream.

Thanks

https://github.com/llvm/llvm-project/pull/112245
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-15 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa updated 
https://github.com/llvm/llvm-project/pull/112245

>From eff4a0300336c4c106e1d293b19e795f5ccbabc1 Mon Sep 17 00:00:00 2001
From: Arvind Sudarsanam 
Date: Fri, 27 Sep 2024 13:03:12 -0700
Subject: [PATCH 1/4] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link
 SYCL offloading device code

This PR is one of the many PRs in the SYCL upstreaming effort focusing on
device code linking during the SYCL offload compilation process.
RFC: 
https://discourse.llvm.org/t/rfc-offloading-design-for-sycl-offload-kind-and-spir-targets/74088

In this PR, we introduce a new tool that will be used to perform device code
linking for SYCL offload kind. It accepts SYCL device objects in LLVM IR bitcode
format and will generate a fully linked device object that can then be wrapped
and linked into the host object.

A primary use case for this tool is to perform device code linking for objects
with SYCL offload kind inside the clang-linker-wrapper.
It can also be invoked via clang driver as follows:

`clang --target=spirv64 --sycl-link input.bc`

Device code linking for SYCL offloading kind has a number of known quirks that
makes it difficult to use in a unified offloading setting.
Two of the primary issues are:
1. Several finalization steps are required to be run on the fully-linked LLVM
IR bitcode to gaurantee conformance to SYCL standards. This step is unique to
SYCL offloading compilation flow.
2. SPIR-V LLVM Translator tool is an extenal tool and hence SPIR-V IR code
generation cannot be done as part of LTO. This limitation will be lifted once
SPIR-V backend is available as a viable LLVM backend.

Hence, we introduce this new tool to provide a clean wrapper to perform SYCL
device linking.

Thanks

Signed-off-by: Arvind Sudarsanam 
---
 clang/docs/ClangSYCLLinkWrapper.rst   |  80 +++
 clang/docs/index.rst  |   1 +
 clang/include/clang/Driver/Options.td |   5 +-
 clang/lib/Driver/Driver.cpp   |   5 +
 clang/lib/Driver/ToolChains/SPIRV.cpp |  12 +
 clang/lib/Driver/ToolChains/SPIRV.h   |   5 +-
 clang/test/Driver/Inputs/libsycl-complex.bc   |   0
 clang/test/Driver/Inputs/libsycl-crt.bc   |   0
 .../Driver/clang-sycl-link-wrapper-test.cpp   |   9 +
 clang/test/Driver/sycl-link-spirv-target.cpp  |   7 +
 clang/tools/CMakeLists.txt|   1 +
 .../clang-sycl-link-wrapper/CMakeLists.txt|  28 +
 .../ClangSYCLLinkWrapper.cpp  | 530 ++
 .../clang-sycl-link-wrapper/SYCLLinkOpts.td   |  47 ++
 14 files changed, 727 insertions(+), 3 deletions(-)
 create mode 100644 clang/docs/ClangSYCLLinkWrapper.rst
 create mode 100644 clang/test/Driver/Inputs/libsycl-complex.bc
 create mode 100644 clang/test/Driver/Inputs/libsycl-crt.bc
 create mode 100644 clang/test/Driver/clang-sycl-link-wrapper-test.cpp
 create mode 100644 clang/test/Driver/sycl-link-spirv-target.cpp
 create mode 100644 clang/tools/clang-sycl-link-wrapper/CMakeLists.txt
 create mode 100644 clang/tools/clang-sycl-link-wrapper/ClangSYCLLinkWrapper.cpp
 create mode 100644 clang/tools/clang-sycl-link-wrapper/SYCLLinkOpts.td

diff --git a/clang/docs/ClangSYCLLinkWrapper.rst 
b/clang/docs/ClangSYCLLinkWrapper.rst
new file mode 100644
index 00..8ceb17f6af9d86
--- /dev/null
+++ b/clang/docs/ClangSYCLLinkWrapper.rst
@@ -0,0 +1,80 @@
+===
+Clang SYCL Link Wrapper
+===
+
+.. contents::
+   :local:
+
+.. _clang-sycl-link-wrapper:
+
+Introduction
+
+
+This tool works as a wrapper around the SYCL device code linking process.
+The purpose of this wrapper is to provide an interface to link SYCL device
+bitcode in LLVM IR format, SYCL device bitcode in SPIR-V IR format, and native
+binary objects, and then use the SPIR-V LLVM Translator tool on fully linked
+device objects to produce the final output.
+After the linking stage, the fully linked device code in LLVM IR format may
+undergo several SYCL-specific finalization steps before the SPIR-V code
+generation step.
+The wrapper will also support the Ahead-Of-Time (AOT) compilation flow. AOT
+compilation is the process of invoking the back-end at compile time to produce
+the final binary, as opposed to just-in-time (JIT) compilation when final code
+generation is deferred until application runtime.
+
+Device code linking for SYCL offloading has several known quirks that
+make it difficult to use in a unified offloading setting. Two of the primary
+issues are:
+1. Several finalization steps are required to be run on the fully linked LLVM
+IR bitcode to guarantee conformance to SYCL standards. This step is unique to
+the SYCL offloading compilation flow.
+2. The SPIR-V LLVM Translator tool is an external tool and hence SPIR-V IR code
+generation cannot be done as part of LTO. This limitation can be lifted once
+the SPIR-V backend is available as a viable LLVM backend.
+
+This tool has been proposed to work around these issu

[clang] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-15 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa updated 
https://github.com/llvm/llvm-project/pull/112245

>From eff4a0300336c4c106e1d293b19e795f5ccbabc1 Mon Sep 17 00:00:00 2001
From: Arvind Sudarsanam 
Date: Fri, 27 Sep 2024 13:03:12 -0700
Subject: [PATCH 1/5] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link
 SYCL offloading device code

This PR is one of the many PRs in the SYCL upstreaming effort focusing on
device code linking during the SYCL offload compilation process.
RFC: 
https://discourse.llvm.org/t/rfc-offloading-design-for-sycl-offload-kind-and-spir-targets/74088

In this PR, we introduce a new tool that will be used to perform device code
linking for SYCL offload kind. It accepts SYCL device objects in LLVM IR bitcode
format and will generate a fully linked device object that can then be wrapped
and linked into the host object.

A primary use case for this tool is to perform device code linking for objects
with SYCL offload kind inside the clang-linker-wrapper.
It can also be invoked via clang driver as follows:

`clang --target=spirv64 --sycl-link input.bc`

Device code linking for SYCL offloading kind has a number of known quirks that
makes it difficult to use in a unified offloading setting.
Two of the primary issues are:
1. Several finalization steps are required to be run on the fully-linked LLVM
IR bitcode to gaurantee conformance to SYCL standards. This step is unique to
SYCL offloading compilation flow.
2. SPIR-V LLVM Translator tool is an extenal tool and hence SPIR-V IR code
generation cannot be done as part of LTO. This limitation will be lifted once
SPIR-V backend is available as a viable LLVM backend.

Hence, we introduce this new tool to provide a clean wrapper to perform SYCL
device linking.

Thanks

Signed-off-by: Arvind Sudarsanam 
---
 clang/docs/ClangSYCLLinkWrapper.rst   |  80 +++
 clang/docs/index.rst  |   1 +
 clang/include/clang/Driver/Options.td |   5 +-
 clang/lib/Driver/Driver.cpp   |   5 +
 clang/lib/Driver/ToolChains/SPIRV.cpp |  12 +
 clang/lib/Driver/ToolChains/SPIRV.h   |   5 +-
 clang/test/Driver/Inputs/libsycl-complex.bc   |   0
 clang/test/Driver/Inputs/libsycl-crt.bc   |   0
 .../Driver/clang-sycl-link-wrapper-test.cpp   |   9 +
 clang/test/Driver/sycl-link-spirv-target.cpp  |   7 +
 clang/tools/CMakeLists.txt|   1 +
 .../clang-sycl-link-wrapper/CMakeLists.txt|  28 +
 .../ClangSYCLLinkWrapper.cpp  | 530 ++
 .../clang-sycl-link-wrapper/SYCLLinkOpts.td   |  47 ++
 14 files changed, 727 insertions(+), 3 deletions(-)
 create mode 100644 clang/docs/ClangSYCLLinkWrapper.rst
 create mode 100644 clang/test/Driver/Inputs/libsycl-complex.bc
 create mode 100644 clang/test/Driver/Inputs/libsycl-crt.bc
 create mode 100644 clang/test/Driver/clang-sycl-link-wrapper-test.cpp
 create mode 100644 clang/test/Driver/sycl-link-spirv-target.cpp
 create mode 100644 clang/tools/clang-sycl-link-wrapper/CMakeLists.txt
 create mode 100644 clang/tools/clang-sycl-link-wrapper/ClangSYCLLinkWrapper.cpp
 create mode 100644 clang/tools/clang-sycl-link-wrapper/SYCLLinkOpts.td

diff --git a/clang/docs/ClangSYCLLinkWrapper.rst 
b/clang/docs/ClangSYCLLinkWrapper.rst
new file mode 100644
index 00..8ceb17f6af9d86
--- /dev/null
+++ b/clang/docs/ClangSYCLLinkWrapper.rst
@@ -0,0 +1,80 @@
+===
+Clang SYCL Link Wrapper
+===
+
+.. contents::
+   :local:
+
+.. _clang-sycl-link-wrapper:
+
+Introduction
+
+
+This tool works as a wrapper around the SYCL device code linking process.
+The purpose of this wrapper is to provide an interface to link SYCL device
+bitcode in LLVM IR format, SYCL device bitcode in SPIR-V IR format, and native
+binary objects, and then use the SPIR-V LLVM Translator tool on fully linked
+device objects to produce the final output.
+After the linking stage, the fully linked device code in LLVM IR format may
+undergo several SYCL-specific finalization steps before the SPIR-V code
+generation step.
+The wrapper will also support the Ahead-Of-Time (AOT) compilation flow. AOT
+compilation is the process of invoking the back-end at compile time to produce
+the final binary, as opposed to just-in-time (JIT) compilation when final code
+generation is deferred until application runtime.
+
+Device code linking for SYCL offloading has several known quirks that
+make it difficult to use in a unified offloading setting. Two of the primary
+issues are:
+1. Several finalization steps are required to be run on the fully linked LLVM
+IR bitcode to guarantee conformance to SYCL standards. This step is unique to
+the SYCL offloading compilation flow.
+2. The SPIR-V LLVM Translator tool is an external tool and hence SPIR-V IR code
+generation cannot be done as part of LTO. This limitation can be lifted once
+the SPIR-V backend is available as a viable LLVM backend.
+
+This tool has been proposed to work around these issu

[clang] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-15 Thread Arvind Sudarsanam via cfe-commits

asudarsa wrote:

Earlier test fail due to lack of new tool in clang/test/lit.cfg.py.
Added it now. 

Thanks

https://github.com/llvm/llvm-project/pull/112245
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-15 Thread Arvind Sudarsanam via cfe-commits


@@ -95,7 +95,19 @@ void SPIRV::Linker::ConstructJob(Compilation &C, const 
JobAction &JA,
   CmdArgs.push_back("-o");
   CmdArgs.push_back(Output.getFilename());
 
+  // Use of --sycl-link will call the clang-sycl-link-wrapper instead of
+  // the default linker (spirv-link).
+  if (Args.hasArg(options::OPT_sycl_link))
+Linker = ToolChain.GetProgramPath("clang-sycl-link-wrapper");

asudarsa wrote:

Made the change in 679436acf28a4da805dfbdf388ca1c9ce692ad4d

Thanks

https://github.com/llvm/llvm-project/pull/112245
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-15 Thread Arvind Sudarsanam via cfe-commits




asudarsa wrote:

Modified the test to just use empty files. Change can be found in 
307c74ea4f910a7698a6084db710ff7b699c5c93

Thanks

https://github.com/llvm/llvm-project/pull/112245
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-15 Thread Arvind Sudarsanam via cfe-commits


@@ -0,0 +1,528 @@
+//=-- clang-sycl-link-wrapper/ClangSYCLLinkWrapper.cpp - SYCL linker util --=//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===-===//
+//
+// This tool wraps around the sequence of steps required to link device code in
+// SYCL fat objects. SYCL device code linking requires a complex sequence of
+// steps that include linking of llvm bitcode files, linking device library
+// files with the fully linked source bitcode file(s), running several SYCL
+// specific post-link steps on the fully linked bitcode file(s), and finally
+// generating target-specific device code. This tool can be removed once SYCL
+// linking is ported to `ld.lld`.
+//
+//===-===//
+
+#include "clang/Basic/Version.h"
+
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/BinaryFormat/Magic.h"
+#include "llvm/Bitcode/BitcodeWriter.h"
+#include "llvm/CodeGen/CommandFlags.h"
+#include "llvm/IR/DiagnosticPrinter.h"
+#include "llvm/IRReader/IRReader.h"
+#include "llvm/LTO/LTO.h"
+#include "llvm/Object/Archive.h"
+#include "llvm/Object/ArchiveWriter.h"
+#include "llvm/Object/Binary.h"
+#include "llvm/Object/ELFObjectFile.h"
+#include "llvm/Object/IRObjectFile.h"
+#include "llvm/Object/ObjectFile.h"
+#include "llvm/Object/OffloadBinary.h"
+#include "llvm/Option/ArgList.h"
+#include "llvm/Option/OptTable.h"
+#include "llvm/Option/Option.h"
+#include "llvm/Remarks/HotnessThresholdParser.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/FileOutputBuffer.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/InitLLVM.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/Path.h"
+#include "llvm/Support/Program.h"
+#include "llvm/Support/Signals.h"
+#include "llvm/Support/StringSaver.h"
+#include "llvm/Support/TargetSelect.h"
+#include "llvm/Support/TimeProfiler.h"
+#include "llvm/Support/WithColor.h"
+
+using namespace llvm;
+using namespace llvm::opt;
+using namespace llvm::object;
+
+/// Save intermediary results.
+static bool SaveTemps = false;
+
+/// Print arguments without executing.
+static bool DryRun = false;
+
+/// Print verbose output.
+static bool Verbose = false;
+
+/// Filename of the output being created.
+static StringRef OutputFile;
+
+/// Directory to dump SPIR-V IR if requested by user.
+static SmallString<128> SPIRVDumpDir;
+
+static void printVersion(raw_ostream &OS) {
+  OS << clang::getClangToolFullVersion("clang-sycl-link-wrapper") << '\n';
+}
+
+/// The value of `argv[0]` when run.
+static const char *Executable;
+
+/// Temporary files to be cleaned up.
+static SmallVector> TempFiles;
+
+namespace {
+// Must not overlap with llvm::opt::DriverFlag.
+enum WrapperFlags { WrapperOnlyOption = (1 << 4) };
+
+enum ID {
+  OPT_INVALID = 0, // This is not an option ID.
+#define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
+#include "SYCLLinkOpts.inc"
+  LastOption
+#undef OPTION
+};
+
+#define PREFIX(NAME, VALUE)
\
+  static constexpr StringLiteral NAME##_init[] = VALUE;
\
+  static constexpr ArrayRef NAME(NAME##_init,   
\
+std::size(NAME##_init) - 1);
+#include "SYCLLinkOpts.inc"
+#undef PREFIX
+
+static constexpr OptTable::Info InfoTable[] = {
+#define OPTION(...) LLVM_CONSTRUCT_OPT_INFO(__VA_ARGS__),
+#include "SYCLLinkOpts.inc"
+#undef OPTION
+};
+
+class WrapperOptTable : public opt::GenericOptTable {
+public:
+  WrapperOptTable() : opt::GenericOptTable(InfoTable) {}
+};
+
+const OptTable &getOptTable() {
+  static const WrapperOptTable *Table = []() {
+auto Result = std::make_unique();
+return Result.release();
+  }();
+  return *Table;
+}
+
+[[noreturn]] void reportError(Error E) {
+  outs().flush();
+  logAllUnhandledErrors(std::move(E), WithColor::error(errs(), Executable));
+  exit(EXIT_FAILURE);
+}
+
+std::string getMainExecutable(const char *Name) {
+  void *Ptr = (void *)(intptr_t)&getMainExecutable;
+  auto COWPath = sys::fs::getMainExecutable(Name, Ptr);
+  return sys::path::parent_path(COWPath).str();
+}
+
+Expected createTempFile(const ArgList &Args, const Twine &Prefix,
+   StringRef Extension) {
+  SmallString<128> OutputFile;
+  if (Args.hasArg(OPT_save_temps)) {
+// Generate a unique path name without creating a file
+sys::fs::createUniquePath(Prefix + "-%%." + Extension, OutputFile,
+  /*MakeAbsolute=*/false);
+  } else {
+if (std::error_code EC =
+sys::fs::createTemporaryFile(Prefix, Extension, OutputFile))
+  return createFileError(OutputFile, EC);
+  }
+
+  TempFiles.emplace_back(std::move(OutputFil

[clang] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-15 Thread Arvind Sudarsanam via cfe-commits


@@ -0,0 +1,528 @@
+//=-- clang-sycl-link-wrapper/ClangSYCLLinkWrapper.cpp - SYCL linker util --=//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===-===//
+//
+// This tool wraps around the sequence of steps required to link device code in
+// SYCL fat objects. SYCL device code linking requires a complex sequence of
+// steps that include linking of llvm bitcode files, linking device library
+// files with the fully linked source bitcode file(s), running several SYCL
+// specific post-link steps on the fully linked bitcode file(s), and finally
+// generating target-specific device code. This tool can be removed once SYCL
+// linking is ported to `ld.lld`.
+//
+//===-===//
+
+#include "clang/Basic/Version.h"
+
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/BinaryFormat/Magic.h"
+#include "llvm/Bitcode/BitcodeWriter.h"
+#include "llvm/CodeGen/CommandFlags.h"
+#include "llvm/IR/DiagnosticPrinter.h"
+#include "llvm/IRReader/IRReader.h"
+#include "llvm/LTO/LTO.h"
+#include "llvm/Object/Archive.h"
+#include "llvm/Object/ArchiveWriter.h"
+#include "llvm/Object/Binary.h"
+#include "llvm/Object/ELFObjectFile.h"
+#include "llvm/Object/IRObjectFile.h"
+#include "llvm/Object/ObjectFile.h"
+#include "llvm/Object/OffloadBinary.h"
+#include "llvm/Option/ArgList.h"
+#include "llvm/Option/OptTable.h"
+#include "llvm/Option/Option.h"
+#include "llvm/Remarks/HotnessThresholdParser.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/FileOutputBuffer.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/InitLLVM.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/Path.h"
+#include "llvm/Support/Program.h"
+#include "llvm/Support/Signals.h"
+#include "llvm/Support/StringSaver.h"
+#include "llvm/Support/TargetSelect.h"
+#include "llvm/Support/TimeProfiler.h"
+#include "llvm/Support/WithColor.h"
+
+using namespace llvm;
+using namespace llvm::opt;
+using namespace llvm::object;
+
+/// Save intermediary results.
+static bool SaveTemps = false;
+
+/// Print arguments without executing.
+static bool DryRun = false;
+
+/// Print verbose output.
+static bool Verbose = false;
+
+/// Filename of the output being created.
+static StringRef OutputFile;
+
+/// Directory to dump SPIR-V IR if requested by user.
+static SmallString<128> SPIRVDumpDir;
+
+static void printVersion(raw_ostream &OS) {
+  OS << clang::getClangToolFullVersion("clang-sycl-link-wrapper") << '\n';
+}
+
+/// The value of `argv[0]` when run.
+static const char *Executable;
+
+/// Temporary files to be cleaned up.
+static SmallVector> TempFiles;
+
+namespace {
+// Must not overlap with llvm::opt::DriverFlag.
+enum WrapperFlags { WrapperOnlyOption = (1 << 4) };
+
+enum ID {
+  OPT_INVALID = 0, // This is not an option ID.
+#define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
+#include "SYCLLinkOpts.inc"
+  LastOption
+#undef OPTION
+};
+
+#define PREFIX(NAME, VALUE)
\
+  static constexpr StringLiteral NAME##_init[] = VALUE;
\
+  static constexpr ArrayRef NAME(NAME##_init,   
\
+std::size(NAME##_init) - 1);
+#include "SYCLLinkOpts.inc"
+#undef PREFIX
+
+static constexpr OptTable::Info InfoTable[] = {
+#define OPTION(...) LLVM_CONSTRUCT_OPT_INFO(__VA_ARGS__),
+#include "SYCLLinkOpts.inc"
+#undef OPTION
+};
+
+class WrapperOptTable : public opt::GenericOptTable {
+public:
+  WrapperOptTable() : opt::GenericOptTable(InfoTable) {}
+};
+
+const OptTable &getOptTable() {
+  static const WrapperOptTable *Table = []() {
+auto Result = std::make_unique();
+return Result.release();
+  }();
+  return *Table;
+}
+
+[[noreturn]] void reportError(Error E) {
+  outs().flush();
+  logAllUnhandledErrors(std::move(E), WithColor::error(errs(), Executable));
+  exit(EXIT_FAILURE);
+}
+
+std::string getMainExecutable(const char *Name) {
+  void *Ptr = (void *)(intptr_t)&getMainExecutable;
+  auto COWPath = sys::fs::getMainExecutable(Name, Ptr);
+  return sys::path::parent_path(COWPath).str();
+}
+
+Expected createTempFile(const ArgList &Args, const Twine &Prefix,
+   StringRef Extension) {
+  SmallString<128> OutputFile;
+  if (Args.hasArg(OPT_save_temps)) {
+// Generate a unique path name without creating a file
+sys::fs::createUniquePath(Prefix + "-%%." + Extension, OutputFile,
+  /*MakeAbsolute=*/false);
+  } else {
+if (std::error_code EC =
+sys::fs::createTemporaryFile(Prefix, Extension, OutputFile))
+  return createFileError(OutputFile, EC);
+  }
+
+  TempFiles.emplace_back(std::move(OutputFil

[clang] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-15 Thread Arvind Sudarsanam via cfe-commits

asudarsa wrote:

Hi @jhuber6 

I have addressed all the concerns to my best ability. Thanks very much for your 
guidance and feedback thus far.

Thanks
Sincerely

https://github.com/llvm/llvm-project/pull/112245
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-16 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa updated 
https://github.com/llvm/llvm-project/pull/112245

>From eff4a0300336c4c106e1d293b19e795f5ccbabc1 Mon Sep 17 00:00:00 2001
From: Arvind Sudarsanam 
Date: Fri, 27 Sep 2024 13:03:12 -0700
Subject: [PATCH 1/9] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link
 SYCL offloading device code

This PR is one of the many PRs in the SYCL upstreaming effort focusing on
device code linking during the SYCL offload compilation process.
RFC: 
https://discourse.llvm.org/t/rfc-offloading-design-for-sycl-offload-kind-and-spir-targets/74088

In this PR, we introduce a new tool that will be used to perform device code
linking for SYCL offload kind. It accepts SYCL device objects in LLVM IR bitcode
format and will generate a fully linked device object that can then be wrapped
and linked into the host object.

A primary use case for this tool is to perform device code linking for objects
with SYCL offload kind inside the clang-linker-wrapper.
It can also be invoked via clang driver as follows:

`clang --target=spirv64 --sycl-link input.bc`

Device code linking for SYCL offloading kind has a number of known quirks that
makes it difficult to use in a unified offloading setting.
Two of the primary issues are:
1. Several finalization steps are required to be run on the fully-linked LLVM
IR bitcode to gaurantee conformance to SYCL standards. This step is unique to
SYCL offloading compilation flow.
2. SPIR-V LLVM Translator tool is an extenal tool and hence SPIR-V IR code
generation cannot be done as part of LTO. This limitation will be lifted once
SPIR-V backend is available as a viable LLVM backend.

Hence, we introduce this new tool to provide a clean wrapper to perform SYCL
device linking.

Thanks

Signed-off-by: Arvind Sudarsanam 
---
 clang/docs/ClangSYCLLinkWrapper.rst   |  80 +++
 clang/docs/index.rst  |   1 +
 clang/include/clang/Driver/Options.td |   5 +-
 clang/lib/Driver/Driver.cpp   |   5 +
 clang/lib/Driver/ToolChains/SPIRV.cpp |  12 +
 clang/lib/Driver/ToolChains/SPIRV.h   |   5 +-
 clang/test/Driver/Inputs/libsycl-complex.bc   |   0
 clang/test/Driver/Inputs/libsycl-crt.bc   |   0
 .../Driver/clang-sycl-link-wrapper-test.cpp   |   9 +
 clang/test/Driver/sycl-link-spirv-target.cpp  |   7 +
 clang/tools/CMakeLists.txt|   1 +
 .../clang-sycl-link-wrapper/CMakeLists.txt|  28 +
 .../ClangSYCLLinkWrapper.cpp  | 530 ++
 .../clang-sycl-link-wrapper/SYCLLinkOpts.td   |  47 ++
 14 files changed, 727 insertions(+), 3 deletions(-)
 create mode 100644 clang/docs/ClangSYCLLinkWrapper.rst
 create mode 100644 clang/test/Driver/Inputs/libsycl-complex.bc
 create mode 100644 clang/test/Driver/Inputs/libsycl-crt.bc
 create mode 100644 clang/test/Driver/clang-sycl-link-wrapper-test.cpp
 create mode 100644 clang/test/Driver/sycl-link-spirv-target.cpp
 create mode 100644 clang/tools/clang-sycl-link-wrapper/CMakeLists.txt
 create mode 100644 clang/tools/clang-sycl-link-wrapper/ClangSYCLLinkWrapper.cpp
 create mode 100644 clang/tools/clang-sycl-link-wrapper/SYCLLinkOpts.td

diff --git a/clang/docs/ClangSYCLLinkWrapper.rst 
b/clang/docs/ClangSYCLLinkWrapper.rst
new file mode 100644
index 00..8ceb17f6af9d86
--- /dev/null
+++ b/clang/docs/ClangSYCLLinkWrapper.rst
@@ -0,0 +1,80 @@
+===
+Clang SYCL Link Wrapper
+===
+
+.. contents::
+   :local:
+
+.. _clang-sycl-link-wrapper:
+
+Introduction
+
+
+This tool works as a wrapper around the SYCL device code linking process.
+The purpose of this wrapper is to provide an interface to link SYCL device
+bitcode in LLVM IR format, SYCL device bitcode in SPIR-V IR format, and native
+binary objects, and then use the SPIR-V LLVM Translator tool on fully linked
+device objects to produce the final output.
+After the linking stage, the fully linked device code in LLVM IR format may
+undergo several SYCL-specific finalization steps before the SPIR-V code
+generation step.
+The wrapper will also support the Ahead-Of-Time (AOT) compilation flow. AOT
+compilation is the process of invoking the back-end at compile time to produce
+the final binary, as opposed to just-in-time (JIT) compilation when final code
+generation is deferred until application runtime.
+
+Device code linking for SYCL offloading has several known quirks that
+make it difficult to use in a unified offloading setting. Two of the primary
+issues are:
+1. Several finalization steps are required to be run on the fully linked LLVM
+IR bitcode to guarantee conformance to SYCL standards. This step is unique to
+the SYCL offloading compilation flow.
+2. The SPIR-V LLVM Translator tool is an external tool and hence SPIR-V IR code
+generation cannot be done as part of LTO. This limitation can be lifted once
+the SPIR-V backend is available as a viable LLVM backend.
+
+This tool has been proposed to work around these issu

[clang] [Clang][SYCL] Introduce clang-sycl-linker to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-16 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa edited 
https://github.com/llvm/llvm-project/pull/112245
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-16 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa updated 
https://github.com/llvm/llvm-project/pull/112245

>From eff4a0300336c4c106e1d293b19e795f5ccbabc1 Mon Sep 17 00:00:00 2001
From: Arvind Sudarsanam 
Date: Fri, 27 Sep 2024 13:03:12 -0700
Subject: [PATCH 01/10] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link
 SYCL offloading device code

This PR is one of the many PRs in the SYCL upstreaming effort focusing on
device code linking during the SYCL offload compilation process.
RFC: 
https://discourse.llvm.org/t/rfc-offloading-design-for-sycl-offload-kind-and-spir-targets/74088

In this PR, we introduce a new tool that will be used to perform device code
linking for SYCL offload kind. It accepts SYCL device objects in LLVM IR bitcode
format and will generate a fully linked device object that can then be wrapped
and linked into the host object.

A primary use case for this tool is to perform device code linking for objects
with SYCL offload kind inside the clang-linker-wrapper.
It can also be invoked via clang driver as follows:

`clang --target=spirv64 --sycl-link input.bc`

Device code linking for SYCL offloading kind has a number of known quirks that
makes it difficult to use in a unified offloading setting.
Two of the primary issues are:
1. Several finalization steps are required to be run on the fully-linked LLVM
IR bitcode to gaurantee conformance to SYCL standards. This step is unique to
SYCL offloading compilation flow.
2. SPIR-V LLVM Translator tool is an extenal tool and hence SPIR-V IR code
generation cannot be done as part of LTO. This limitation will be lifted once
SPIR-V backend is available as a viable LLVM backend.

Hence, we introduce this new tool to provide a clean wrapper to perform SYCL
device linking.

Thanks

Signed-off-by: Arvind Sudarsanam 
---
 clang/docs/ClangSYCLLinkWrapper.rst   |  80 +++
 clang/docs/index.rst  |   1 +
 clang/include/clang/Driver/Options.td |   5 +-
 clang/lib/Driver/Driver.cpp   |   5 +
 clang/lib/Driver/ToolChains/SPIRV.cpp |  12 +
 clang/lib/Driver/ToolChains/SPIRV.h   |   5 +-
 clang/test/Driver/Inputs/libsycl-complex.bc   |   0
 clang/test/Driver/Inputs/libsycl-crt.bc   |   0
 .../Driver/clang-sycl-link-wrapper-test.cpp   |   9 +
 clang/test/Driver/sycl-link-spirv-target.cpp  |   7 +
 clang/tools/CMakeLists.txt|   1 +
 .../clang-sycl-link-wrapper/CMakeLists.txt|  28 +
 .../ClangSYCLLinkWrapper.cpp  | 530 ++
 .../clang-sycl-link-wrapper/SYCLLinkOpts.td   |  47 ++
 14 files changed, 727 insertions(+), 3 deletions(-)
 create mode 100644 clang/docs/ClangSYCLLinkWrapper.rst
 create mode 100644 clang/test/Driver/Inputs/libsycl-complex.bc
 create mode 100644 clang/test/Driver/Inputs/libsycl-crt.bc
 create mode 100644 clang/test/Driver/clang-sycl-link-wrapper-test.cpp
 create mode 100644 clang/test/Driver/sycl-link-spirv-target.cpp
 create mode 100644 clang/tools/clang-sycl-link-wrapper/CMakeLists.txt
 create mode 100644 clang/tools/clang-sycl-link-wrapper/ClangSYCLLinkWrapper.cpp
 create mode 100644 clang/tools/clang-sycl-link-wrapper/SYCLLinkOpts.td

diff --git a/clang/docs/ClangSYCLLinkWrapper.rst 
b/clang/docs/ClangSYCLLinkWrapper.rst
new file mode 100644
index 00..8ceb17f6af9d86
--- /dev/null
+++ b/clang/docs/ClangSYCLLinkWrapper.rst
@@ -0,0 +1,80 @@
+===
+Clang SYCL Link Wrapper
+===
+
+.. contents::
+   :local:
+
+.. _clang-sycl-link-wrapper:
+
+Introduction
+
+
+This tool works as a wrapper around the SYCL device code linking process.
+The purpose of this wrapper is to provide an interface to link SYCL device
+bitcode in LLVM IR format, SYCL device bitcode in SPIR-V IR format, and native
+binary objects, and then use the SPIR-V LLVM Translator tool on fully linked
+device objects to produce the final output.
+After the linking stage, the fully linked device code in LLVM IR format may
+undergo several SYCL-specific finalization steps before the SPIR-V code
+generation step.
+The wrapper will also support the Ahead-Of-Time (AOT) compilation flow. AOT
+compilation is the process of invoking the back-end at compile time to produce
+the final binary, as opposed to just-in-time (JIT) compilation when final code
+generation is deferred until application runtime.
+
+Device code linking for SYCL offloading has several known quirks that
+make it difficult to use in a unified offloading setting. Two of the primary
+issues are:
+1. Several finalization steps are required to be run on the fully linked LLVM
+IR bitcode to guarantee conformance to SYCL standards. This step is unique to
+the SYCL offloading compilation flow.
+2. The SPIR-V LLVM Translator tool is an external tool and hence SPIR-V IR code
+generation cannot be done as part of LTO. This limitation can be lifted once
+the SPIR-V backend is available as a viable LLVM backend.
+
+This tool has been proposed to work around these is

[clang] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-16 Thread Arvind Sudarsanam via cfe-commits

asudarsa wrote:

clang/test/Driver is not the correct place to be testing for tool availability.

Thanks

https://github.com/llvm/llvm-project/pull/112245
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link SYCL offloadi… (PR #112245)

2024-10-14 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa created 
https://github.com/llvm/llvm-project/pull/112245

…ng device code

This PR is one of the many PRs in the SYCL upstreaming effort focusing on 
device code linking during the SYCL offload compilation process. RFC: 
https://discourse.llvm.org/t/rfc-offloading-design-for-sycl-offload-kind-and-spir-targets/74088

In this PR, we introduce a new tool that will be used to perform device code 
linking for SYCL offload kind. It accepts SYCL device objects in LLVM IR 
bitcode format and will generate a fully linked device object that can then be 
wrapped and linked into the host object.

A primary use case for this tool is to perform device code linking for objects 
with SYCL offload kind inside the clang-linker-wrapper. It can also be invoked 
via clang driver as follows:

`clang --target=spirv64 --sycl-link input.bc`

Device code linking for SYCL offloading kind has a number of known quirks that 
makes it difficult to use in a unified offloading setting. Two of the primary 
issues are:
1. Several finalization steps are required to be run on the fully-linked LLVM 
IR bitcode to gaurantee conformance to SYCL standards. This step is unique to 
SYCL offloading compilation flow.
2. SPIR-V LLVM Translator tool is an extenal tool and hence SPIR-V IR code 
generation cannot be done as part of LTO. This limitation will be lifted once 
SPIR-V backend is available as a viable LLVM backend.

Hence, we introduce this new tool to provide a clean wrapper to perform SYCL 
device linking.

Thanks

>From eff4a0300336c4c106e1d293b19e795f5ccbabc1 Mon Sep 17 00:00:00 2001
From: Arvind Sudarsanam 
Date: Fri, 27 Sep 2024 13:03:12 -0700
Subject: [PATCH] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link SYCL
 offloading device code

This PR is one of the many PRs in the SYCL upstreaming effort focusing on
device code linking during the SYCL offload compilation process.
RFC: 
https://discourse.llvm.org/t/rfc-offloading-design-for-sycl-offload-kind-and-spir-targets/74088

In this PR, we introduce a new tool that will be used to perform device code
linking for SYCL offload kind. It accepts SYCL device objects in LLVM IR bitcode
format and will generate a fully linked device object that can then be wrapped
and linked into the host object.

A primary use case for this tool is to perform device code linking for objects
with SYCL offload kind inside the clang-linker-wrapper.
It can also be invoked via clang driver as follows:

`clang --target=spirv64 --sycl-link input.bc`

Device code linking for SYCL offloading kind has a number of known quirks that
makes it difficult to use in a unified offloading setting.
Two of the primary issues are:
1. Several finalization steps are required to be run on the fully-linked LLVM
IR bitcode to gaurantee conformance to SYCL standards. This step is unique to
SYCL offloading compilation flow.
2. SPIR-V LLVM Translator tool is an extenal tool and hence SPIR-V IR code
generation cannot be done as part of LTO. This limitation will be lifted once
SPIR-V backend is available as a viable LLVM backend.

Hence, we introduce this new tool to provide a clean wrapper to perform SYCL
device linking.

Thanks

Signed-off-by: Arvind Sudarsanam 
---
 clang/docs/ClangSYCLLinkWrapper.rst   |  80 +++
 clang/docs/index.rst  |   1 +
 clang/include/clang/Driver/Options.td |   5 +-
 clang/lib/Driver/Driver.cpp   |   5 +
 clang/lib/Driver/ToolChains/SPIRV.cpp |  12 +
 clang/lib/Driver/ToolChains/SPIRV.h   |   5 +-
 clang/test/Driver/Inputs/libsycl-complex.bc   |   0
 clang/test/Driver/Inputs/libsycl-crt.bc   |   0
 .../Driver/clang-sycl-link-wrapper-test.cpp   |   9 +
 clang/test/Driver/sycl-link-spirv-target.cpp  |   7 +
 clang/tools/CMakeLists.txt|   1 +
 .../clang-sycl-link-wrapper/CMakeLists.txt|  28 +
 .../ClangSYCLLinkWrapper.cpp  | 530 ++
 .../clang-sycl-link-wrapper/SYCLLinkOpts.td   |  47 ++
 14 files changed, 727 insertions(+), 3 deletions(-)
 create mode 100644 clang/docs/ClangSYCLLinkWrapper.rst
 create mode 100644 clang/test/Driver/Inputs/libsycl-complex.bc
 create mode 100644 clang/test/Driver/Inputs/libsycl-crt.bc
 create mode 100644 clang/test/Driver/clang-sycl-link-wrapper-test.cpp
 create mode 100644 clang/test/Driver/sycl-link-spirv-target.cpp
 create mode 100644 clang/tools/clang-sycl-link-wrapper/CMakeLists.txt
 create mode 100644 clang/tools/clang-sycl-link-wrapper/ClangSYCLLinkWrapper.cpp
 create mode 100644 clang/tools/clang-sycl-link-wrapper/SYCLLinkOpts.td

diff --git a/clang/docs/ClangSYCLLinkWrapper.rst 
b/clang/docs/ClangSYCLLinkWrapper.rst
new file mode 100644
index 00..8ceb17f6af9d86
--- /dev/null
+++ b/clang/docs/ClangSYCLLinkWrapper.rst
@@ -0,0 +1,80 @@
+===
+Clang SYCL Link Wrapper
+===
+
+.. contents::
+   :local:
+
+.. _clang-sycl-link-wrapper:
+
+Introduction
+
+
+T

[clang] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link SYCL offloading device code (PR #112245)

2024-10-14 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa edited 
https://github.com/llvm/llvm-project/pull/112245
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link SYCL offloadi… (PR #112245)

2024-10-14 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa edited 
https://github.com/llvm/llvm-project/pull/112245
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-14 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa edited 
https://github.com/llvm/llvm-project/pull/112245
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-14 Thread Arvind Sudarsanam via cfe-commits

asudarsa wrote:

This PR is the first in the list of multiple PRs that will be submitted to add 
SYCL offloading support using the new offloading model. Upcoming PRs include:
1. SYCL finalization steps that will run after llvm-link will be added to the 
linking clow inside clang-sycl-link-wrapper. One of the finalization steps is 
device code splitting.
2. Changes to clang-linker-wrapper to invoke the clang-sycl-link-wrapper (via 
the clang driver call) for SYCL offloading case.
3. AOT compilation support for Intel, AMD and NVidia GPUs

Thanks
Sincerely

https://github.com/llvm/llvm-project/pull/112245
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-14 Thread Arvind Sudarsanam via cfe-commits

asudarsa wrote:

@jhuber6

Can you please take a look at this PR when convenient? Thanks for all your 
inputs in getting this PR ready.

Sincerely

https://github.com/llvm/llvm-project/pull/112245
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][SYCL] Introduce clang-sycl-linker to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-22 Thread Arvind Sudarsanam via cfe-commits

asudarsa wrote:

Hi @llvm-beanz 

Thanks so much for providing your valuable time and feedback thus far. I have 
addressed the concerns raised to the best of my ability. I have added more 
testing inside 'clang/test/Driver/clang-sycl-linker-test.cpp'. It will be great 
if you can take another look when convenient and let me know if there are 
additional concerns.

Thanks
Sincerely

P.S: About the location of this tool under clang/tools, I added the tool here 
as it is somewhat similar in functionality to tools like clang-linker-wrapper 
and clang-nvlink-wrapper. If there is strong opposition, we can surely move it 
to llvm/tools.

https://github.com/llvm/llvm-project/pull/112245
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][SYCL] Introduce clang-sycl-linker to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-22 Thread Arvind Sudarsanam via cfe-commits

asudarsa wrote:

@bader, thanks for adding the SYCL label. Can you please provide your feedback 
for this PR when convenient? For some reason, I am not able to add reviewers.

Thanks

https://github.com/llvm/llvm-project/pull/112245
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-linker-wrapper] Add error handling for missing linker path (PR #113613)

2024-10-24 Thread Arvind Sudarsanam via cfe-commits

asudarsa wrote:

@jhuber6 

Can you please take a look and comment if this change looks ok.

Thanks

https://github.com/llvm/llvm-project/pull/113613
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-linker-wrapper] Add error handling for missing linker path (PR #113613)

2024-10-24 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa created 
https://github.com/llvm/llvm-project/pull/113613

In clang-linker-wrapper, we do not explicitly check if --linker-path is 
provided.
This PR adds a check to capture this.

Thanks

>From 5b890e60ad8c349254d687e96452a8f575e5 Mon Sep 17 00:00:00 2001
From: Arvind Sudarsanam 
Date: Thu, 24 Oct 2024 12:46:18 -0700
Subject: [PATCH] [clang-linker-wrapper] Add error handling for missing linker
 path

Signed-off-by: Arvind Sudarsanam 
---
 clang/test/Driver/linker-wrapper.c  | 4 
 clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp | 2 ++
 2 files changed, 6 insertions(+)

diff --git a/clang/test/Driver/linker-wrapper.c 
b/clang/test/Driver/linker-wrapper.c
index 068ea2d7d3c663..4ab4051f37553e 100644
--- a/clang/test/Driver/linker-wrapper.c
+++ b/clang/test/Driver/linker-wrapper.c
@@ -250,3 +250,7 @@ __attribute__((visibility("protected"), used)) int x;
 //   MLLVM-SAME: -Xlinker -mllvm=-pass-remarks=foo,bar
 //  OFFLOAD-OPT-NOT: -Xlinker -mllvm=-pass-remarks=foo,bar
 // OFFLOAD-OPT-SAME: {{$}}
+
+// Error handling when --linker-path is not provided for clang-linker-wrapper
+// RUN: not clang-linker-wrapper 2>&1 | FileCheck 
--check-prefix=LINKER-PATH-NOT-PROVIDED %s
+// LINKER-PATH-NOT-PROVIDED: Host linker is not available
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 9fea1fdcd5fb46..8000d0e1f48dad 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -370,6 +370,8 @@ Error runLinker(ArrayRef Files, const ArgList 
&Args) {
   // Render the linker arguments and add the newly created image. We add it
   // after the output file to ensure it is linked with the correct libraries.
   StringRef LinkerPath = Args.getLastArgValue(OPT_linker_path_EQ);
+  if (LinkerPath.empty())
+return createStringError("Host linker is not available");
   ArgStringList NewLinkerArgs;
   for (const opt::Arg *Arg : Args) {
 // Do not forward arguments only intended for the linker wrapper.

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-linker-wrapper] Add error handling for missing linker path (PR #113613)

2024-10-24 Thread Arvind Sudarsanam via cfe-commits


@@ -370,6 +370,8 @@ Error runLinker(ArrayRef Files, const ArgList 
&Args) {
   // Render the linker arguments and add the newly created image. We add it
   // after the output file to ensure it is linked with the correct libraries.
   StringRef LinkerPath = Args.getLastArgValue(OPT_linker_path_EQ);
+  if (LinkerPath.empty())
+return createStringError("Host linker is not available");

asudarsa wrote:

Thanks. Addressed in next commit.

https://github.com/llvm/llvm-project/pull/113613
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][SYCL] Introduce clang-sycl-linker to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-29 Thread Arvind Sudarsanam via cfe-commits


@@ -0,0 +1,55 @@
+include "llvm/Option/OptParser.td"
+
+def WrapperOnlyOption : OptionFlag;
+
+def help : Flag<["-", "--"], "help">,
+  HelpText<"Display available options (--help-hidden for more)">;
+
+def help_hidden : Flag<["-", "--"], "help-hidden">,
+  HelpText<"Display all available options">;
+
+def verbose : Flag<["-"], "v">, HelpText<"Print verbose information">;
+def version : Flag<["--"], "version">,
+  HelpText<"Display the version number and exit">;
+
+def o : JoinedOrSeparate<["-"], "o">, MetaVarName<"">,
+  HelpText<"Path to file to write output">;
+def output : Separate<["--"], "output-file">, Alias, Flags<[HelpHidden]>,
+  HelpText<"Alias for -o">;
+
+def library_path_EQ : Joined<["--", "-"], "library-path=">,
+  Flags<[HelpHidden]>, HelpText<"Add  to the library search path">;
+
+def device_libs_EQ : CommaJoined<["--", "-"], "device-libs=">,
+  Flags<[WrapperOnlyOption]>,
+  HelpText<"A comma separated list of device libraries that are linked during 
the device link.">;
+
+def triple : Joined<["--"], "triple">,
+  HelpText<"The device target triple">;
+def arch : Separate<["--", "-"], "arch">,
+  HelpText<"Specify the name of the target architecture.">;
+
+def g : Flag<["-"], "g">, HelpText<"Specify that this was a debug compile.">;
+def debug : Flag<["--"], "debug">, Alias;

asudarsa wrote:

We 'might' need this at some stage. But I will remove this for now. Thanks

https://github.com/llvm/llvm-project/pull/112245
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][SYCL] Introduce clang-sycl-linker to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-29 Thread Arvind Sudarsanam via cfe-commits


@@ -0,0 +1,13 @@
+// Tests the driver when linking LLVM IR bitcode files and targeting SPIR-V
+// architecture.
+//
+// RUN: touch %t.bc
+// RUN: %clangxx --target=spirv64 --sycl-link -### %t.bc 2>&1 \
+// RUN:   | FileCheck %s -check-prefix=LINK
+// LINK: "{{.*}}clang-sycl-linker{{.*}}" "{{.*}}.bc" "-o" "a.out"
+//
+// Test that -Xlinker options are being passed to clang-sycl-linker.
+// RUN: %clangxx -### --target=spirv64 --sycl-link -Xlinker 
--llvm-spirv-path=/tmp \
+// RUN:   -Xlinker --library-path=/tmp -Xlinker --device-libs=lib1.bc,lib2.bc 
%t.bc 2>&1 \
+// RUN:   | FileCheck %s -check-prefix=XLINKEROPTS
+// XLINKEROPTS: "{{.*}}clang-sycl-linker{{.*}}" "--llvm-spirv-path=/tmp" 
"--library-path=/tmp" "--device-libs=lib1.bc,lib2.bc" "{{.*}}.bc" "-o" "a.out"

asudarsa wrote:

Ah. Sorry. Misinterpretation. I got it. 
I agree. I was incrementally adding these tests. I can drop the first one. 

Thanks

https://github.com/llvm/llvm-project/pull/112245
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][SYCL] Introduce clang-sycl-linker to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-29 Thread Arvind Sudarsanam via cfe-commits


@@ -0,0 +1,42 @@
+// Tests the clang-sycl-linker tool.
+//
+// Test a simple case without arguments.
+// RUN: %clangxx -fsycl -emit-llvm -c %s -o %t.bc
+// RUN: clang-sycl-linker --dry-run -triple spirv64 %t.bc -o a.spv 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=SIMPLE
+// SIMPLE: "{{.*}}llvm-link{{.*}}" {{.*}}.bc -o [[FIRSTLLVMLINKOUT:.*]].bc 
--suppress-warnings
+// SIMPLE-NEXT: "{{.*}}llvm-spirv{{.*}}" {{.*}}-o a.spv [[FIRSTLLVMLINKOUT]].bc
+//
+// Test a simple case with device library files specified.
+// RUN: echo ' ' > %T/lib1.bc
+// RUN: echo ' ' > %T/lib2.bc
+// RUN: clang-sycl-linker --dry-run -triple spirv64 %t.bc --library-path=%T 
--device-libs=lib1.bc,lib2.bc -o a.spv 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=DEVLIBS
+// DEVLIBS: "{{.*}}llvm-link{{.*}}" {{.*}}.bc -o [[FIRSTLLVMLINKOUT:.*]].bc 
--suppress-warnings
+// DEVLIBS-NEXT: "{{.*}}llvm-link{{.*}}" -only-needed [[FIRSTLLVMLINKOUT]].bc 
{{.*}}lib1.bc {{.*}}lib2.bc -o [[SECONDLLVMLINKOUT:.*]].bc --suppress-warnings
+// DEVLIBS-NEXT: "{{.*}}llvm-spirv{{.*}}" {{.*}}-o a.spv 
[[SECONDLLVMLINKOUT]].bc
+//
+// Test a simple case with .o (fat object) as input.
+// TODO: Remove this test once fat object support is added.

asudarsa wrote:

Currently, the flow is this. clang-inker-wrapper reads the fat object and 
extracts bitcode files. These bitcode files are then fed to clang++ as inputs.
We may eventually want to pass the fat object as is and let the 
clang-sycl-linker extract the bitcode file.

Thanks

https://github.com/llvm/llvm-project/pull/112245
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][SYCL] Introduce clang-sycl-linker to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-29 Thread Arvind Sudarsanam via cfe-commits


@@ -0,0 +1,13 @@
+// Tests the driver when linking LLVM IR bitcode files and targeting SPIR-V
+// architecture.
+//
+// RUN: touch %t.bc
+// RUN: %clangxx --target=spirv64 --sycl-link -### %t.bc 2>&1 \
+// RUN:   | FileCheck %s -check-prefix=LINK
+// LINK: "{{.*}}clang-sycl-linker{{.*}}" "{{.*}}.bc" "-o" "a.out"
+//
+// Test that -Xlinker options are being passed to clang-sycl-linker.
+// RUN: %clangxx -### --target=spirv64 --sycl-link -Xlinker 
--llvm-spirv-path=/tmp \
+// RUN:   -Xlinker --library-path=/tmp -Xlinker --device-libs=lib1.bc,lib2.bc 
%t.bc 2>&1 \
+// RUN:   | FileCheck %s -check-prefix=XLINKEROPTS
+// XLINKEROPTS: "{{.*}}clang-sycl-linker{{.*}}" "--llvm-spirv-path=/tmp" 
"--library-path=/tmp" "--device-libs=lib1.bc,lib2.bc" "{{.*}}.bc" "-o" "a.out"

asudarsa wrote:

First test is expected to test the set of commands invoked for 
'clang-sycl-linker --dry-run' 
and the second test is expected to test the correct invocation of 
slang-sycl-linker' for 'clang++ --sycl-link -target=spirv '.
Both these tests are expected to evolve as we add more support.

Thanks

https://github.com/llvm/llvm-project/pull/112245
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][SYCL] Introduce clang-sycl-linker to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-29 Thread Arvind Sudarsanam via cfe-commits


@@ -0,0 +1,55 @@
+include "llvm/Option/OptParser.td"
+
+def WrapperOnlyOption : OptionFlag;
+
+def help : Flag<["-", "--"], "help">,
+  HelpText<"Display available options (--help-hidden for more)">;
+
+def help_hidden : Flag<["-", "--"], "help-hidden">,
+  HelpText<"Display all available options">;
+
+def verbose : Flag<["-"], "v">, HelpText<"Print verbose information">;
+def version : Flag<["--"], "version">,
+  HelpText<"Display the version number and exit">;
+
+def o : JoinedOrSeparate<["-"], "o">, MetaVarName<"">,
+  HelpText<"Path to file to write output">;
+def output : Separate<["--"], "output-file">, Alias, Flags<[HelpHidden]>,
+  HelpText<"Alias for -o">;
+
+def library_path_EQ : Joined<["--", "-"], "library-path=">,
+  Flags<[HelpHidden]>, HelpText<"Add  to the library search path">;
+
+def device_libs_EQ : CommaJoined<["--", "-"], "device-libs=">,
+  Flags<[WrapperOnlyOption]>,
+  HelpText<"A comma separated list of device libraries that are linked during 
the device link.">;
+
+def triple : Joined<["--"], "triple">,
+  HelpText<"The device target triple">;
+def arch : Separate<["--", "-"], "arch">,
+  HelpText<"Specify the name of the target architecture.">;
+
+def g : Flag<["-"], "g">, HelpText<"Specify that this was a debug compile.">;
+def debug : Flag<["--"], "debug">, Alias;
+
+def save_temps : Flag<["--", "-"], "save-temps">,
+  Flags<[WrapperOnlyOption]>, HelpText<"Save intermediate results">;
+
+def dry_run : Flag<["--", "-"], "dry-run">, Flags<[WrapperOnlyOption]>,
+  HelpText<"Print generated commands without running.">;
+
+def spirv_dump_device_code_EQ : Joined<["--", "-"], "spirv-dump-device-code=">,
+  Flags<[WrapperOnlyOption]>,
+  HelpText<"Path to the folder where the tool dumps SPIR-V device code. Other 
formats aren't dumped.">;

asudarsa wrote:

We do intend to support a user-visible flag at clang-level to allow dumping of 
SPIR-V device code to a user-specific directory. This option will be fed by 
that.

Thanks

https://github.com/llvm/llvm-project/pull/112245
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][SYCL] Introduce clang-sycl-linker to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-29 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa updated 
https://github.com/llvm/llvm-project/pull/112245

>From eff4a0300336c4c106e1d293b19e795f5ccbabc1 Mon Sep 17 00:00:00 2001
From: Arvind Sudarsanam 
Date: Fri, 27 Sep 2024 13:03:12 -0700
Subject: [PATCH 01/11] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link
 SYCL offloading device code

This PR is one of the many PRs in the SYCL upstreaming effort focusing on
device code linking during the SYCL offload compilation process.
RFC: 
https://discourse.llvm.org/t/rfc-offloading-design-for-sycl-offload-kind-and-spir-targets/74088

In this PR, we introduce a new tool that will be used to perform device code
linking for SYCL offload kind. It accepts SYCL device objects in LLVM IR bitcode
format and will generate a fully linked device object that can then be wrapped
and linked into the host object.

A primary use case for this tool is to perform device code linking for objects
with SYCL offload kind inside the clang-linker-wrapper.
It can also be invoked via clang driver as follows:

`clang --target=spirv64 --sycl-link input.bc`

Device code linking for SYCL offloading kind has a number of known quirks that
makes it difficult to use in a unified offloading setting.
Two of the primary issues are:
1. Several finalization steps are required to be run on the fully-linked LLVM
IR bitcode to gaurantee conformance to SYCL standards. This step is unique to
SYCL offloading compilation flow.
2. SPIR-V LLVM Translator tool is an extenal tool and hence SPIR-V IR code
generation cannot be done as part of LTO. This limitation will be lifted once
SPIR-V backend is available as a viable LLVM backend.

Hence, we introduce this new tool to provide a clean wrapper to perform SYCL
device linking.

Thanks

Signed-off-by: Arvind Sudarsanam 
---
 clang/docs/ClangSYCLLinkWrapper.rst   |  80 +++
 clang/docs/index.rst  |   1 +
 clang/include/clang/Driver/Options.td |   5 +-
 clang/lib/Driver/Driver.cpp   |   5 +
 clang/lib/Driver/ToolChains/SPIRV.cpp |  12 +
 clang/lib/Driver/ToolChains/SPIRV.h   |   5 +-
 clang/test/Driver/Inputs/libsycl-complex.bc   |   0
 clang/test/Driver/Inputs/libsycl-crt.bc   |   0
 .../Driver/clang-sycl-link-wrapper-test.cpp   |   9 +
 clang/test/Driver/sycl-link-spirv-target.cpp  |   7 +
 clang/tools/CMakeLists.txt|   1 +
 .../clang-sycl-link-wrapper/CMakeLists.txt|  28 +
 .../ClangSYCLLinkWrapper.cpp  | 530 ++
 .../clang-sycl-link-wrapper/SYCLLinkOpts.td   |  47 ++
 14 files changed, 727 insertions(+), 3 deletions(-)
 create mode 100644 clang/docs/ClangSYCLLinkWrapper.rst
 create mode 100644 clang/test/Driver/Inputs/libsycl-complex.bc
 create mode 100644 clang/test/Driver/Inputs/libsycl-crt.bc
 create mode 100644 clang/test/Driver/clang-sycl-link-wrapper-test.cpp
 create mode 100644 clang/test/Driver/sycl-link-spirv-target.cpp
 create mode 100644 clang/tools/clang-sycl-link-wrapper/CMakeLists.txt
 create mode 100644 clang/tools/clang-sycl-link-wrapper/ClangSYCLLinkWrapper.cpp
 create mode 100644 clang/tools/clang-sycl-link-wrapper/SYCLLinkOpts.td

diff --git a/clang/docs/ClangSYCLLinkWrapper.rst 
b/clang/docs/ClangSYCLLinkWrapper.rst
new file mode 100644
index 00..8ceb17f6af9d86
--- /dev/null
+++ b/clang/docs/ClangSYCLLinkWrapper.rst
@@ -0,0 +1,80 @@
+===
+Clang SYCL Link Wrapper
+===
+
+.. contents::
+   :local:
+
+.. _clang-sycl-link-wrapper:
+
+Introduction
+
+
+This tool works as a wrapper around the SYCL device code linking process.
+The purpose of this wrapper is to provide an interface to link SYCL device
+bitcode in LLVM IR format, SYCL device bitcode in SPIR-V IR format, and native
+binary objects, and then use the SPIR-V LLVM Translator tool on fully linked
+device objects to produce the final output.
+After the linking stage, the fully linked device code in LLVM IR format may
+undergo several SYCL-specific finalization steps before the SPIR-V code
+generation step.
+The wrapper will also support the Ahead-Of-Time (AOT) compilation flow. AOT
+compilation is the process of invoking the back-end at compile time to produce
+the final binary, as opposed to just-in-time (JIT) compilation when final code
+generation is deferred until application runtime.
+
+Device code linking for SYCL offloading has several known quirks that
+make it difficult to use in a unified offloading setting. Two of the primary
+issues are:
+1. Several finalization steps are required to be run on the fully linked LLVM
+IR bitcode to guarantee conformance to SYCL standards. This step is unique to
+the SYCL offloading compilation flow.
+2. The SPIR-V LLVM Translator tool is an external tool and hence SPIR-V IR code
+generation cannot be done as part of LTO. This limitation can be lifted once
+the SPIR-V backend is available as a viable LLVM backend.
+
+This tool has been proposed to work around these is

[clang] [Clang][SYCL] Introduce clang-sycl-linker to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-29 Thread Arvind Sudarsanam via cfe-commits

asudarsa wrote:

Hi @bader 

Thanks very much for your feedback. I have addressed the concerns in the latest 
commit.
Thanks to @jhuber6 for some responses as well.

Sincerely

https://github.com/llvm/llvm-project/pull/112245
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][SYCL] Introduce clang-sycl-linker to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-29 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa updated 
https://github.com/llvm/llvm-project/pull/112245

>From eff4a0300336c4c106e1d293b19e795f5ccbabc1 Mon Sep 17 00:00:00 2001
From: Arvind Sudarsanam 
Date: Fri, 27 Sep 2024 13:03:12 -0700
Subject: [PATCH 01/12] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link
 SYCL offloading device code

This PR is one of the many PRs in the SYCL upstreaming effort focusing on
device code linking during the SYCL offload compilation process.
RFC: 
https://discourse.llvm.org/t/rfc-offloading-design-for-sycl-offload-kind-and-spir-targets/74088

In this PR, we introduce a new tool that will be used to perform device code
linking for SYCL offload kind. It accepts SYCL device objects in LLVM IR bitcode
format and will generate a fully linked device object that can then be wrapped
and linked into the host object.

A primary use case for this tool is to perform device code linking for objects
with SYCL offload kind inside the clang-linker-wrapper.
It can also be invoked via clang driver as follows:

`clang --target=spirv64 --sycl-link input.bc`

Device code linking for SYCL offloading kind has a number of known quirks that
makes it difficult to use in a unified offloading setting.
Two of the primary issues are:
1. Several finalization steps are required to be run on the fully-linked LLVM
IR bitcode to gaurantee conformance to SYCL standards. This step is unique to
SYCL offloading compilation flow.
2. SPIR-V LLVM Translator tool is an extenal tool and hence SPIR-V IR code
generation cannot be done as part of LTO. This limitation will be lifted once
SPIR-V backend is available as a viable LLVM backend.

Hence, we introduce this new tool to provide a clean wrapper to perform SYCL
device linking.

Thanks

Signed-off-by: Arvind Sudarsanam 
---
 clang/docs/ClangSYCLLinkWrapper.rst   |  80 +++
 clang/docs/index.rst  |   1 +
 clang/include/clang/Driver/Options.td |   5 +-
 clang/lib/Driver/Driver.cpp   |   5 +
 clang/lib/Driver/ToolChains/SPIRV.cpp |  12 +
 clang/lib/Driver/ToolChains/SPIRV.h   |   5 +-
 clang/test/Driver/Inputs/libsycl-complex.bc   |   0
 clang/test/Driver/Inputs/libsycl-crt.bc   |   0
 .../Driver/clang-sycl-link-wrapper-test.cpp   |   9 +
 clang/test/Driver/sycl-link-spirv-target.cpp  |   7 +
 clang/tools/CMakeLists.txt|   1 +
 .../clang-sycl-link-wrapper/CMakeLists.txt|  28 +
 .../ClangSYCLLinkWrapper.cpp  | 530 ++
 .../clang-sycl-link-wrapper/SYCLLinkOpts.td   |  47 ++
 14 files changed, 727 insertions(+), 3 deletions(-)
 create mode 100644 clang/docs/ClangSYCLLinkWrapper.rst
 create mode 100644 clang/test/Driver/Inputs/libsycl-complex.bc
 create mode 100644 clang/test/Driver/Inputs/libsycl-crt.bc
 create mode 100644 clang/test/Driver/clang-sycl-link-wrapper-test.cpp
 create mode 100644 clang/test/Driver/sycl-link-spirv-target.cpp
 create mode 100644 clang/tools/clang-sycl-link-wrapper/CMakeLists.txt
 create mode 100644 clang/tools/clang-sycl-link-wrapper/ClangSYCLLinkWrapper.cpp
 create mode 100644 clang/tools/clang-sycl-link-wrapper/SYCLLinkOpts.td

diff --git a/clang/docs/ClangSYCLLinkWrapper.rst 
b/clang/docs/ClangSYCLLinkWrapper.rst
new file mode 100644
index 00..8ceb17f6af9d86
--- /dev/null
+++ b/clang/docs/ClangSYCLLinkWrapper.rst
@@ -0,0 +1,80 @@
+===
+Clang SYCL Link Wrapper
+===
+
+.. contents::
+   :local:
+
+.. _clang-sycl-link-wrapper:
+
+Introduction
+
+
+This tool works as a wrapper around the SYCL device code linking process.
+The purpose of this wrapper is to provide an interface to link SYCL device
+bitcode in LLVM IR format, SYCL device bitcode in SPIR-V IR format, and native
+binary objects, and then use the SPIR-V LLVM Translator tool on fully linked
+device objects to produce the final output.
+After the linking stage, the fully linked device code in LLVM IR format may
+undergo several SYCL-specific finalization steps before the SPIR-V code
+generation step.
+The wrapper will also support the Ahead-Of-Time (AOT) compilation flow. AOT
+compilation is the process of invoking the back-end at compile time to produce
+the final binary, as opposed to just-in-time (JIT) compilation when final code
+generation is deferred until application runtime.
+
+Device code linking for SYCL offloading has several known quirks that
+make it difficult to use in a unified offloading setting. Two of the primary
+issues are:
+1. Several finalization steps are required to be run on the fully linked LLVM
+IR bitcode to guarantee conformance to SYCL standards. This step is unique to
+the SYCL offloading compilation flow.
+2. The SPIR-V LLVM Translator tool is an external tool and hence SPIR-V IR code
+generation cannot be done as part of LTO. This limitation can be lifted once
+the SPIR-V backend is available as a viable LLVM backend.
+
+This tool has been proposed to work around these is

[clang] [Clang][SYCL] Introduce clang-sycl-linker to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-29 Thread Arvind Sudarsanam via cfe-commits


@@ -0,0 +1,42 @@
+// Tests the clang-sycl-linker tool.
+//
+// Test a simple case without arguments.
+// RUN: %clangxx -fsycl -emit-llvm -c %s -o %t.bc
+// RUN: clang-sycl-linker --dry-run -triple spirv64 %t.bc -o a.spv 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=SIMPLE
+// SIMPLE: "{{.*}}llvm-link{{.*}}" {{.*}}.bc -o [[FIRSTLLVMLINKOUT:.*]].bc 
--suppress-warnings
+// SIMPLE-NEXT: "{{.*}}llvm-spirv{{.*}}" {{.*}}-o a.spv [[FIRSTLLVMLINKOUT]].bc
+//
+// Test a simple case with device library files specified.
+// RUN: echo ' ' > %T/lib1.bc
+// RUN: echo ' ' > %T/lib2.bc
+// RUN: clang-sycl-linker --dry-run -triple spirv64 %t.bc --library-path=%T 
--device-libs=lib1.bc,lib2.bc -o a.spv 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=DEVLIBS
+// DEVLIBS: "{{.*}}llvm-link{{.*}}" {{.*}}.bc -o [[FIRSTLLVMLINKOUT:.*]].bc 
--suppress-warnings
+// DEVLIBS-NEXT: "{{.*}}llvm-link{{.*}}" -only-needed [[FIRSTLLVMLINKOUT]].bc 
{{.*}}lib1.bc {{.*}}lib2.bc -o [[SECONDLLVMLINKOUT:.*]].bc --suppress-warnings
+// DEVLIBS-NEXT: "{{.*}}llvm-spirv{{.*}}" {{.*}}-o a.spv 
[[SECONDLLVMLINKOUT]].bc
+//
+// Test a simple case with .o (fat object) as input.
+// TODO: Remove this test once fat object support is added.

asudarsa wrote:

Adjusted comment. Thanks

https://github.com/llvm/llvm-project/pull/112245
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][SYCL] Introduce clang-sycl-linker to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-29 Thread Arvind Sudarsanam via cfe-commits


@@ -0,0 +1,500 @@
+//= clang-sycl-linker/ClangSYCLLinker.cpp - SYCL Linker util ---=//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===-===//
+//
+// This tool executes a sequence of steps required to link device code in SYCL
+// fat objects. SYCL device code linking requires a complex sequence of steps
+// that include linking of llvm bitcode files, linking device library files
+// with the fully linked source bitcode file(s), running several SYCL specific
+// post-link steps on the fully linked bitcode file(s), and finally generating
+// target-specific device code.
+//===-===//
+
+#include "clang/Basic/Version.h"
+
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/BinaryFormat/Magic.h"
+#include "llvm/Bitcode/BitcodeWriter.h"
+#include "llvm/CodeGen/CommandFlags.h"
+#include "llvm/IR/DiagnosticPrinter.h"
+#include "llvm/IRReader/IRReader.h"
+#include "llvm/LTO/LTO.h"
+#include "llvm/Object/Archive.h"
+#include "llvm/Object/ArchiveWriter.h"
+#include "llvm/Object/Binary.h"
+#include "llvm/Object/ELFObjectFile.h"
+#include "llvm/Object/IRObjectFile.h"
+#include "llvm/Object/ObjectFile.h"
+#include "llvm/Object/OffloadBinary.h"
+#include "llvm/Option/ArgList.h"
+#include "llvm/Option/OptTable.h"
+#include "llvm/Option/Option.h"
+#include "llvm/Remarks/HotnessThresholdParser.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/FileOutputBuffer.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/InitLLVM.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/Path.h"
+#include "llvm/Support/Program.h"
+#include "llvm/Support/Signals.h"
+#include "llvm/Support/StringSaver.h"
+#include "llvm/Support/TargetSelect.h"
+#include "llvm/Support/TimeProfiler.h"
+#include "llvm/Support/WithColor.h"
+
+using namespace llvm;
+using namespace llvm::opt;
+using namespace llvm::object;
+
+/// Save intermediary results.
+static bool SaveTemps = false;
+
+/// Print arguments without executing.
+static bool DryRun = false;
+
+/// Print verbose output.
+static bool Verbose = false;
+
+/// Filename of the output being created.
+static StringRef OutputFile;
+
+/// Directory to dump SPIR-V IR if requested by user.
+static SmallString<128> SPIRVDumpDir;
+
+static void printVersion(raw_ostream &OS) {
+  OS << clang::getClangToolFullVersion("clang-sycl-linker") << '\n';
+}
+
+/// The value of `argv[0]` when run.
+static const char *Executable;
+
+/// Temporary files to be cleaned up.
+static SmallVector> TempFiles;
+
+namespace {
+// Must not overlap with llvm::opt::DriverFlag.
+enum WrapperFlags { WrapperOnlyOption = (1 << 4) };
+
+enum ID {
+  OPT_INVALID = 0, // This is not an option ID.
+#define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
+#include "SYCLLinkOpts.inc"
+  LastOption
+#undef OPTION
+};
+
+#define PREFIX(NAME, VALUE)
\
+  static constexpr StringLiteral NAME##_init[] = VALUE;
\
+  static constexpr ArrayRef NAME(NAME##_init,   
\
+std::size(NAME##_init) - 1);
+#include "SYCLLinkOpts.inc"
+#undef PREFIX
+
+static constexpr OptTable::Info InfoTable[] = {
+#define OPTION(...) LLVM_CONSTRUCT_OPT_INFO(__VA_ARGS__),
+#include "SYCLLinkOpts.inc"
+#undef OPTION
+};
+
+class WrapperOptTable : public opt::GenericOptTable {

asudarsa wrote:

Will change this. Thanks. Also. I will change WrapperOnlyOption to 
LinkerOnlyOption.

Thanks

https://github.com/llvm/llvm-project/pull/112245
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][SYCL] Introduce clang-sycl-linker to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-30 Thread Arvind Sudarsanam via cfe-commits


@@ -0,0 +1,48 @@
+// Tests the clang-sycl-linker tool.
+//
+// Test a simple case without arguments.
+// RUN: %clangxx -emit-llvm -c %s -o %t_1.bc
+// RUN: %clangxx -emit-llvm -c %s -o %t_2.bc
+// RUN: clang-sycl-linker --dry-run -triple spirv64 %t_1.bc %t_2.bc -o a.spv 
2>&1 \
+// RUN:   | FileCheck %s --check-prefix=SIMPLE
+// SIMPLE: "{{.*}}llvm-link{{.*}}" {{.*}}.bc {{.*}}.bc -o 
[[FIRSTLLVMLINKOUT:.*]].bc --suppress-warnings
+// SIMPLE-NEXT: "{{.*}}llvm-spirv{{.*}}" {{.*}}-o a.spv [[FIRSTLLVMLINKOUT]].bc
+//
+// Test that llvm-link is not called when only one input is present.
+// RUN: clang-sycl-linker --dry-run -triple spirv64 %t_1.bc -o a.spv 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=SIMPLE-NO-LINK
+// SIMPLE-NO-LINK: "{{.*}}llvm-spirv{{.*}}" {{.*}}-o a.spv {{.*}}.bc
+//
+// Test a simple case with device library files specified.
+// RUN: touch %T/lib1.bc
+// RUN: touch %T/lib2.bc
+// RUN: clang-sycl-linker --dry-run -triple spirv64 %t_1.bc %t_2.bc 
--library-path=%T --device-libs=lib1.bc,lib2.bc -o a.spv 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=DEVLIBS
+// DEVLIBS: "{{.*}}llvm-link{{.*}}" {{.*}}.bc {{.*}}.bc -o 
[[FIRSTLLVMLINKOUT:.*]].bc --suppress-warnings
+// DEVLIBS-NEXT: "{{.*}}llvm-link{{.*}}" -only-needed [[FIRSTLLVMLINKOUT]].bc 
{{.*}}lib1.bc {{.*}}lib2.bc -o [[SECONDLLVMLINKOUT:.*]].bc --suppress-warnings
+// DEVLIBS-NEXT: "{{.*}}llvm-spirv{{.*}}" {{.*}}-o a.spv 
[[SECONDLLVMLINKOUT]].bc
+//
+// Test a simple case with .o (fat object) as input.
+// TODO: Remove this test once fat object support is added.

asudarsa wrote:

Can I please update this in one of the upcoming PRs? I do not want to trigger 
another round of testing here.

Thanks

https://github.com/llvm/llvm-project/pull/112245
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Initialize SmallVector variable (PR #114434)

2024-10-31 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa created 
https://github.com/llvm/llvm-project/pull/114434

None

>From 9cfc10768adf19e41b22cd6a9fb2c781fdf4498e Mon Sep 17 00:00:00 2001
From: Arvind Sudarsanam 
Date: Thu, 31 Oct 2024 10:34:06 -0700
Subject: [PATCH] Initialize SmallVector variable

Signed-off-by: Arvind Sudarsanam 
---
 clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp 
b/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
index 0639b95c76e218..fa37cbeb56316c 100644
--- a/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
+++ b/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
@@ -237,7 +237,7 @@ Expected 
linkDeviceInputFiles(ArrayRef InputFiles,
 // will be linked with input device files.
 // The list of files and its location are passed from driver.
 Expected> getSYCLDeviceLibs(const ArgList &Args) {
-  SmallVector DeviceLibFiles;
+  SmallVector DeviceLibFiles{};
   StringRef LibraryPath;
   if (Arg *A = Args.getLastArg(OPT_library_path_EQ))
 LibraryPath = A->getValue();

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-linker-wrapper] Add error handling for missing linker path (PR #113613)

2024-10-25 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa updated 
https://github.com/llvm/llvm-project/pull/113613

>From 5b890e60ad8c349254d687e96452a8f575e5 Mon Sep 17 00:00:00 2001
From: Arvind Sudarsanam 
Date: Thu, 24 Oct 2024 12:46:18 -0700
Subject: [PATCH 1/2] [clang-linker-wrapper] Add error handling for missing
 linker path

Signed-off-by: Arvind Sudarsanam 
---
 clang/test/Driver/linker-wrapper.c  | 4 
 clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp | 2 ++
 2 files changed, 6 insertions(+)

diff --git a/clang/test/Driver/linker-wrapper.c 
b/clang/test/Driver/linker-wrapper.c
index 068ea2d7d3c663..4ab4051f37553e 100644
--- a/clang/test/Driver/linker-wrapper.c
+++ b/clang/test/Driver/linker-wrapper.c
@@ -250,3 +250,7 @@ __attribute__((visibility("protected"), used)) int x;
 //   MLLVM-SAME: -Xlinker -mllvm=-pass-remarks=foo,bar
 //  OFFLOAD-OPT-NOT: -Xlinker -mllvm=-pass-remarks=foo,bar
 // OFFLOAD-OPT-SAME: {{$}}
+
+// Error handling when --linker-path is not provided for clang-linker-wrapper
+// RUN: not clang-linker-wrapper 2>&1 | FileCheck 
--check-prefix=LINKER-PATH-NOT-PROVIDED %s
+// LINKER-PATH-NOT-PROVIDED: Host linker is not available
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 9fea1fdcd5fb46..8000d0e1f48dad 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -370,6 +370,8 @@ Error runLinker(ArrayRef Files, const ArgList 
&Args) {
   // Render the linker arguments and add the newly created image. We add it
   // after the output file to ensure it is linked with the correct libraries.
   StringRef LinkerPath = Args.getLastArgValue(OPT_linker_path_EQ);
+  if (LinkerPath.empty())
+return createStringError("Host linker is not available");
   ArgStringList NewLinkerArgs;
   for (const opt::Arg *Arg : Args) {
 // Do not forward arguments only intended for the linker wrapper.

>From e85288b65bd804b28dc3eb5f6b0500873db112a7 Mon Sep 17 00:00:00 2001
From: Arvind Sudarsanam 
Date: Thu, 24 Oct 2024 14:13:11 -0700
Subject: [PATCH 2/2] Modify error message

Signed-off-by: Arvind Sudarsanam 
---
 clang/test/Driver/linker-wrapper.c  | 2 +-
 clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/test/Driver/linker-wrapper.c 
b/clang/test/Driver/linker-wrapper.c
index 4ab4051f37553e..470af4d5d70cac 100644
--- a/clang/test/Driver/linker-wrapper.c
+++ b/clang/test/Driver/linker-wrapper.c
@@ -253,4 +253,4 @@ __attribute__((visibility("protected"), used)) int x;
 
 // Error handling when --linker-path is not provided for clang-linker-wrapper
 // RUN: not clang-linker-wrapper 2>&1 | FileCheck 
--check-prefix=LINKER-PATH-NOT-PROVIDED %s
-// LINKER-PATH-NOT-PROVIDED: Host linker is not available
+// LINKER-PATH-NOT-PROVIDED: linker path missing, must pass 'linker-path'
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 8000d0e1f48dad..9fcecaee318a79 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -371,7 +371,7 @@ Error runLinker(ArrayRef Files, const ArgList 
&Args) {
   // after the output file to ensure it is linked with the correct libraries.
   StringRef LinkerPath = Args.getLastArgValue(OPT_linker_path_EQ);
   if (LinkerPath.empty())
-return createStringError("Host linker is not available");
+return createStringError("linker path missing, must pass 'linker-path'");
   ArgStringList NewLinkerArgs;
   for (const opt::Arg *Arg : Args) {
 // Do not forward arguments only intended for the linker wrapper.

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][SYCL] Introduce clang-sycl-linker to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-31 Thread Arvind Sudarsanam via cfe-commits

asudarsa wrote:

All the failures seem to be related to missing dependencies. I am working on a 
fix. Thanks

https://github.com/llvm/llvm-project/pull/112245
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][SYCL] Introduce clang-sycl-linker to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-31 Thread Arvind Sudarsanam via cfe-commits

asudarsa wrote:

Sanitizer test failures should be resolved by 
https://github.com/llvm/llvm-project/pull/114488 (under review).
Thanks to @sarnex for the timely fix.

Sincerely

https://github.com/llvm/llvm-project/pull/112245
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Initialize SmallVector variable (PR #114434)

2024-10-31 Thread Arvind Sudarsanam via cfe-commits

asudarsa wrote:

This is not the correct fix. Closing it.

Thanks

https://github.com/llvm/llvm-project/pull/114434
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Initialize SmallVector variable (PR #114434)

2024-10-31 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa closed 
https://github.com/llvm/llvm-project/pull/114434
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][CodeGen][SPIR-V] Fix incorrect SYCL usage, implement missing interface (PR #109415)

2024-09-24 Thread Arvind Sudarsanam via cfe-commits


@@ -188,6 +190,28 @@ void 
SPIRVTargetCodeGenInfo::setCUDAKernelCallingConvention(
   }
 }
 
+LangAS
+SPIRVTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
+ const VarDecl *D) const {
+  assert(!CGM.getLangOpts().OpenCL &&
+ !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) &&
+ "Address space agnostic languages only");
+  // If we're here it means that we're using the SPIRDefIsGen ASMap, hence for
+  // the global AS we can rely on either cuda_device or sycl_global to be
+  // correct; however, since this is not a CUDA Device context, we use
+  // sycl_global to prevent confusion with the assertion.
+  LangAS DefaultGlobalAS = getLangASFromTargetAS(
+  CGM.getContext().getTargetAddressSpace(LangAS::sycl_global));

asudarsa wrote:

Are there any tests available to check this behavior?

Thanks

https://github.com/llvm/llvm-project/pull/109415
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][CodeGen][SPIR-V] Fix incorrect SYCL usage, implement missing interface (PR #109415)

2024-09-25 Thread Arvind Sudarsanam via cfe-commits

asudarsa wrote:

> > Are there any tests available to check this behavior?
> 
> The reworked tests do verify / rely on this behaviour, but I can add an 
> individual test for both vanilla and AMDGCN flavoured SPIR-V, if that is 
> preferred (might be better anyway).

Thanks @AlexVlx 
I will approve this change. You can add the tests as part of this PR or a 
follow-up PR. Either way is fine.
Sincerely

https://github.com/llvm/llvm-project/pull/109415
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][CodeGen][SPIR-V] Fix incorrect SYCL usage, implement missing interface (PR #109415)

2024-09-25 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa approved this pull request.

LGTM. Addition of new tests can be done in this PR or a follow-up PR.
Thanks

https://github.com/llvm/llvm-project/pull/109415
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-15 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa updated 
https://github.com/llvm/llvm-project/pull/112245

>From eff4a0300336c4c106e1d293b19e795f5ccbabc1 Mon Sep 17 00:00:00 2001
From: Arvind Sudarsanam 
Date: Fri, 27 Sep 2024 13:03:12 -0700
Subject: [PATCH 1/7] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link
 SYCL offloading device code

This PR is one of the many PRs in the SYCL upstreaming effort focusing on
device code linking during the SYCL offload compilation process.
RFC: 
https://discourse.llvm.org/t/rfc-offloading-design-for-sycl-offload-kind-and-spir-targets/74088

In this PR, we introduce a new tool that will be used to perform device code
linking for SYCL offload kind. It accepts SYCL device objects in LLVM IR bitcode
format and will generate a fully linked device object that can then be wrapped
and linked into the host object.

A primary use case for this tool is to perform device code linking for objects
with SYCL offload kind inside the clang-linker-wrapper.
It can also be invoked via clang driver as follows:

`clang --target=spirv64 --sycl-link input.bc`

Device code linking for SYCL offloading kind has a number of known quirks that
makes it difficult to use in a unified offloading setting.
Two of the primary issues are:
1. Several finalization steps are required to be run on the fully-linked LLVM
IR bitcode to gaurantee conformance to SYCL standards. This step is unique to
SYCL offloading compilation flow.
2. SPIR-V LLVM Translator tool is an extenal tool and hence SPIR-V IR code
generation cannot be done as part of LTO. This limitation will be lifted once
SPIR-V backend is available as a viable LLVM backend.

Hence, we introduce this new tool to provide a clean wrapper to perform SYCL
device linking.

Thanks

Signed-off-by: Arvind Sudarsanam 
---
 clang/docs/ClangSYCLLinkWrapper.rst   |  80 +++
 clang/docs/index.rst  |   1 +
 clang/include/clang/Driver/Options.td |   5 +-
 clang/lib/Driver/Driver.cpp   |   5 +
 clang/lib/Driver/ToolChains/SPIRV.cpp |  12 +
 clang/lib/Driver/ToolChains/SPIRV.h   |   5 +-
 clang/test/Driver/Inputs/libsycl-complex.bc   |   0
 clang/test/Driver/Inputs/libsycl-crt.bc   |   0
 .../Driver/clang-sycl-link-wrapper-test.cpp   |   9 +
 clang/test/Driver/sycl-link-spirv-target.cpp  |   7 +
 clang/tools/CMakeLists.txt|   1 +
 .../clang-sycl-link-wrapper/CMakeLists.txt|  28 +
 .../ClangSYCLLinkWrapper.cpp  | 530 ++
 .../clang-sycl-link-wrapper/SYCLLinkOpts.td   |  47 ++
 14 files changed, 727 insertions(+), 3 deletions(-)
 create mode 100644 clang/docs/ClangSYCLLinkWrapper.rst
 create mode 100644 clang/test/Driver/Inputs/libsycl-complex.bc
 create mode 100644 clang/test/Driver/Inputs/libsycl-crt.bc
 create mode 100644 clang/test/Driver/clang-sycl-link-wrapper-test.cpp
 create mode 100644 clang/test/Driver/sycl-link-spirv-target.cpp
 create mode 100644 clang/tools/clang-sycl-link-wrapper/CMakeLists.txt
 create mode 100644 clang/tools/clang-sycl-link-wrapper/ClangSYCLLinkWrapper.cpp
 create mode 100644 clang/tools/clang-sycl-link-wrapper/SYCLLinkOpts.td

diff --git a/clang/docs/ClangSYCLLinkWrapper.rst 
b/clang/docs/ClangSYCLLinkWrapper.rst
new file mode 100644
index 00..8ceb17f6af9d86
--- /dev/null
+++ b/clang/docs/ClangSYCLLinkWrapper.rst
@@ -0,0 +1,80 @@
+===
+Clang SYCL Link Wrapper
+===
+
+.. contents::
+   :local:
+
+.. _clang-sycl-link-wrapper:
+
+Introduction
+
+
+This tool works as a wrapper around the SYCL device code linking process.
+The purpose of this wrapper is to provide an interface to link SYCL device
+bitcode in LLVM IR format, SYCL device bitcode in SPIR-V IR format, and native
+binary objects, and then use the SPIR-V LLVM Translator tool on fully linked
+device objects to produce the final output.
+After the linking stage, the fully linked device code in LLVM IR format may
+undergo several SYCL-specific finalization steps before the SPIR-V code
+generation step.
+The wrapper will also support the Ahead-Of-Time (AOT) compilation flow. AOT
+compilation is the process of invoking the back-end at compile time to produce
+the final binary, as opposed to just-in-time (JIT) compilation when final code
+generation is deferred until application runtime.
+
+Device code linking for SYCL offloading has several known quirks that
+make it difficult to use in a unified offloading setting. Two of the primary
+issues are:
+1. Several finalization steps are required to be run on the fully linked LLVM
+IR bitcode to guarantee conformance to SYCL standards. This step is unique to
+the SYCL offloading compilation flow.
+2. The SPIR-V LLVM Translator tool is an external tool and hence SPIR-V IR code
+generation cannot be done as part of LTO. This limitation can be lifted once
+the SPIR-V backend is available as a viable LLVM backend.
+
+This tool has been proposed to work around these issu

[clang] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-15 Thread Arvind Sudarsanam via cfe-commits

asudarsa wrote:

> At a minimum I think this change needs more tests. There's a lot of code 
> added with very minimal test coverage.
> 
> I'm uncomfortable with adding a design that is effectively a workaround with 
> an unwritten "TODO" to fix it up later, but I also don't want to needlessly 
> block things.
> 
> From the discourse post and everything I've found reading about the SYCL 
> tooling, it seems to me like this should really just all be integrated into 
> LLD and performed with the linking phase. It seems like a huge waste of IO to 
> read objects, rip out device-specific bits, process that separately, then 
> read the objects again in another process to link the host bits, then in a 
> third process read the linked host and device bits and combine them...

Hi @llvm-beanz 

I will revisit the testing coverage aspect and try to add more tests.

Thanks

https://github.com/llvm/llvm-project/pull/112245
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-15 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa updated 
https://github.com/llvm/llvm-project/pull/112245

>From eff4a0300336c4c106e1d293b19e795f5ccbabc1 Mon Sep 17 00:00:00 2001
From: Arvind Sudarsanam 
Date: Fri, 27 Sep 2024 13:03:12 -0700
Subject: [PATCH 1/8] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link
 SYCL offloading device code

This PR is one of the many PRs in the SYCL upstreaming effort focusing on
device code linking during the SYCL offload compilation process.
RFC: 
https://discourse.llvm.org/t/rfc-offloading-design-for-sycl-offload-kind-and-spir-targets/74088

In this PR, we introduce a new tool that will be used to perform device code
linking for SYCL offload kind. It accepts SYCL device objects in LLVM IR bitcode
format and will generate a fully linked device object that can then be wrapped
and linked into the host object.

A primary use case for this tool is to perform device code linking for objects
with SYCL offload kind inside the clang-linker-wrapper.
It can also be invoked via clang driver as follows:

`clang --target=spirv64 --sycl-link input.bc`

Device code linking for SYCL offloading kind has a number of known quirks that
makes it difficult to use in a unified offloading setting.
Two of the primary issues are:
1. Several finalization steps are required to be run on the fully-linked LLVM
IR bitcode to gaurantee conformance to SYCL standards. This step is unique to
SYCL offloading compilation flow.
2. SPIR-V LLVM Translator tool is an extenal tool and hence SPIR-V IR code
generation cannot be done as part of LTO. This limitation will be lifted once
SPIR-V backend is available as a viable LLVM backend.

Hence, we introduce this new tool to provide a clean wrapper to perform SYCL
device linking.

Thanks

Signed-off-by: Arvind Sudarsanam 
---
 clang/docs/ClangSYCLLinkWrapper.rst   |  80 +++
 clang/docs/index.rst  |   1 +
 clang/include/clang/Driver/Options.td |   5 +-
 clang/lib/Driver/Driver.cpp   |   5 +
 clang/lib/Driver/ToolChains/SPIRV.cpp |  12 +
 clang/lib/Driver/ToolChains/SPIRV.h   |   5 +-
 clang/test/Driver/Inputs/libsycl-complex.bc   |   0
 clang/test/Driver/Inputs/libsycl-crt.bc   |   0
 .../Driver/clang-sycl-link-wrapper-test.cpp   |   9 +
 clang/test/Driver/sycl-link-spirv-target.cpp  |   7 +
 clang/tools/CMakeLists.txt|   1 +
 .../clang-sycl-link-wrapper/CMakeLists.txt|  28 +
 .../ClangSYCLLinkWrapper.cpp  | 530 ++
 .../clang-sycl-link-wrapper/SYCLLinkOpts.td   |  47 ++
 14 files changed, 727 insertions(+), 3 deletions(-)
 create mode 100644 clang/docs/ClangSYCLLinkWrapper.rst
 create mode 100644 clang/test/Driver/Inputs/libsycl-complex.bc
 create mode 100644 clang/test/Driver/Inputs/libsycl-crt.bc
 create mode 100644 clang/test/Driver/clang-sycl-link-wrapper-test.cpp
 create mode 100644 clang/test/Driver/sycl-link-spirv-target.cpp
 create mode 100644 clang/tools/clang-sycl-link-wrapper/CMakeLists.txt
 create mode 100644 clang/tools/clang-sycl-link-wrapper/ClangSYCLLinkWrapper.cpp
 create mode 100644 clang/tools/clang-sycl-link-wrapper/SYCLLinkOpts.td

diff --git a/clang/docs/ClangSYCLLinkWrapper.rst 
b/clang/docs/ClangSYCLLinkWrapper.rst
new file mode 100644
index 00..8ceb17f6af9d86
--- /dev/null
+++ b/clang/docs/ClangSYCLLinkWrapper.rst
@@ -0,0 +1,80 @@
+===
+Clang SYCL Link Wrapper
+===
+
+.. contents::
+   :local:
+
+.. _clang-sycl-link-wrapper:
+
+Introduction
+
+
+This tool works as a wrapper around the SYCL device code linking process.
+The purpose of this wrapper is to provide an interface to link SYCL device
+bitcode in LLVM IR format, SYCL device bitcode in SPIR-V IR format, and native
+binary objects, and then use the SPIR-V LLVM Translator tool on fully linked
+device objects to produce the final output.
+After the linking stage, the fully linked device code in LLVM IR format may
+undergo several SYCL-specific finalization steps before the SPIR-V code
+generation step.
+The wrapper will also support the Ahead-Of-Time (AOT) compilation flow. AOT
+compilation is the process of invoking the back-end at compile time to produce
+the final binary, as opposed to just-in-time (JIT) compilation when final code
+generation is deferred until application runtime.
+
+Device code linking for SYCL offloading has several known quirks that
+make it difficult to use in a unified offloading setting. Two of the primary
+issues are:
+1. Several finalization steps are required to be run on the fully linked LLVM
+IR bitcode to guarantee conformance to SYCL standards. This step is unique to
+the SYCL offloading compilation flow.
+2. The SPIR-V LLVM Translator tool is an external tool and hence SPIR-V IR code
+generation cannot be done as part of LTO. This limitation can be lifted once
+the SPIR-V backend is available as a viable LLVM backend.
+
+This tool has been proposed to work around these issu

[clang] [Clang][SYCL] Introduce clang-sycl-link-wrapper to link SYCL offloading device code (Part 1 of many) (PR #112245)

2024-10-15 Thread Arvind Sudarsanam via cfe-commits

asudarsa wrote:

Hi @llvm-beanz and @jhuber6 

Thanks so much for all the feedback and discussion here. I am very much 
grateful for both your time and insights here. I will try to give my answers in 
this comment.
An overview of the SYCL compilation flow (with a bit more emphasis on the 
device code linking phases) can be seen in the RFC that we submitted earlier. 
RFC: 
https://discourse.llvm.org/t/rfc-offloading-design-for-sycl-offload-kind-and-spir-targets/74088

As shown in the RFC, SYCL device code linking process involves the following 
steps:
1. Linking of all LLVM IR bitcode device objects found in user code. llvm-link 
is used here.
2. Linking of fully linked LLVM IR device bitcode found in user code (output of 
the previous step) with SYCL specific device library files. llvm-link is used 
here.
3. Post-link steps: Some steps are required to conform to SYCL specification. 
Some kernels have kernel metadata (known as SYCL properties) that need to be 
communicated to SYCL runtime. This requires processing of LLVM IR metadata and 
generating some information that can be embedded into the device image and then 
processed by SYCL runtime. Also, we support device code splitting which is 
mostly done to improve run-time performance and also ensure SYCL spec 
conformance in some cases.
4. For SPIR-V based JIT and AOT targets, SPIR-V IR code generation is then 
performed using an external tool (llvm-spirv)
5. For AOT targets (both SPIR-V and non SPIR-V), we then invoke backend 
compilation tools (For e.g. ocloc for Intel graphics).

Quite a few of these steps have been fashioned to work around the fact that we 
do not have a regular SPIR-V backend in LLVM repo. Once the SPIR-V backend is 
ready to be included in compilation flows, we can remove the dependence on 
llvm-link and external tools like llvm-spirv and use the backend for LTO based 
linking and SPIR-V code generation.

We initially envisioned this flow to be embedded into clang-linker-wrapper. Due 
to specific variations from the community flow, especially the need for 
SYCL-specific post-link steps, we decided to move the logic into a stand-alone 
tool to do SYCL specific linking.

A primary reason for making this a clang-based tool was that it is quite 
similar in its functionality to existing tools like clang-linker-wrapper and 
clang-nvlink-wrapper. As @jhuber6 mentioned, I think it can be easily moved to 
llvm/tools if that's a blocking issue.

As far as testing support goes, the test that has been added in this PR 
'clang-sycl-linker-test.cpp' covers the testing of an very initial flow 
(linking of source llvm IR device bitcodes, linking of device library files, 
and spir-v code generation). This test is expected to grow as we add other 
pieces (post-link steps, AOT compilation support, etc).

I can provide pointers to some extended documentation if the RFC is not in 
sufficient detail.

Thanks again for great review comments and feedback here.
Sincerely

https://github.com/llvm/llvm-project/pull/112245
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL][DOC] Add documentation for SYCL compilation flow (PR #129973)

2025-03-05 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa closed 
https://github.com/llvm/llvm-project/pull/129973
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][SPIR-V] Use the SPIR-V backend by default (PR #129545)

2025-03-05 Thread Arvind Sudarsanam via cfe-commits


@@ -78,7 +77,7 @@ class LLVM_LIBRARY_VISIBILITY SPIRVToolChain : public 
ToolChain {
 
   bool useIntegratedAs() const override { return true; }
 
-  bool IsIntegratedBackendDefault() const override { return false; }
+  bool IsIntegratedBackendDefault() const override { return true; }

asudarsa wrote:

This was something introduced for use of SPIR-V backend in conjunction with the 
translator. may be cleanup at a later stage? https://reviews.llvm.org/D125679

Thanks

https://github.com/llvm/llvm-project/pull/129545
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][SPIR-V] Use the SPIR-V backend by default (PR #129545)

2025-03-05 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa approved this pull request.

LGTM

https://github.com/llvm/llvm-project/pull/129545
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [SYCL][SPIR-V Backend][clang-sycl-linker] Add SPIR-V backend support inside clang-sycl-linker (PR #133967)

2025-04-02 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa updated 
https://github.com/llvm/llvm-project/pull/133967

>From 0ab40315cd5ca320d84f87c99b8f23d9b6e8ed36 Mon Sep 17 00:00:00 2001
From: Arvind Sudarsanam 
Date: Mon, 31 Mar 2025 14:31:24 -0700
Subject: [PATCH 1/5] [SYCL][SPIR-V Backend][clang-sycl-linker] Add SPIR-V
 backend support inside clang-sycl-linker

This PR does the following:

1. Use SPIR-V backend to do LLVM to SPIR-V translation inside clang-sycl-linker
2. Remove llvm-spirv translator from clang-sycl-linker
Currently, no SPIR-V extensions are enabled for SYCL compilation flow. This 
will be updated in subsequent commits.

Thanks

Signed-off-by: Arvind Sudarsanam 
---
 clang/test/Driver/clang-sycl-linker-test.cpp  |  14 +-
 clang/test/Driver/sycl-link-spirv-target.cpp  |   6 +-
 clang/tools/clang-sycl-linker/CMakeLists.txt  |   5 +-
 .../clang-sycl-linker/ClangSYCLLinker.cpp | 187 +++---
 clang/tools/clang-sycl-linker/SYCLLinkOpts.td |   2 +-
 5 files changed, 76 insertions(+), 138 deletions(-)

diff --git a/clang/test/Driver/clang-sycl-linker-test.cpp 
b/clang/test/Driver/clang-sycl-linker-test.cpp
index 729561bd09cd8..2f860ae74e97d 100644
--- a/clang/test/Driver/clang-sycl-linker-test.cpp
+++ b/clang/test/Driver/clang-sycl-linker-test.cpp
@@ -6,7 +6,7 @@
 // RUN: clang-sycl-linker --dry-run -v -triple=spirv64 %t_1.bc %t_2.bc -o 
a.spv 2>&1 \
 // RUN:   | FileCheck %s --check-prefix=SIMPLE-FO
 // SIMPLE-FO: sycl-device-link: inputs: {{.*}}.bc, {{.*}}.bc  libfiles:  
output: [[LLVMLINKOUT:.*]].bc
-// SIMPLE-FO-NEXT: "{{.*}}llvm-spirv{{.*}}" {{.*}}-o a.spv [[LLVMLINKOUT]].bc
+// SIMPLE-FO-NEXT: SPIR-V Backend: input: [[LLVMLINKOUT]].bc, output: a.spv
 //
 // Test the dry run of a simple case with device library files specified.
 // RUN: touch %T/lib1.bc
@@ -14,7 +14,7 @@
 // RUN: clang-sycl-linker --dry-run -v -triple=spirv64 %t_1.bc %t_2.bc 
--library-path=%T --device-libs=lib1.bc,lib2.bc -o a.spv 2>&1 \
 // RUN:   | FileCheck %s --check-prefix=DEVLIBS
 // DEVLIBS: sycl-device-link: inputs: {{.*}}.bc  libfiles: {{.*}}lib1.bc, 
{{.*}}lib2.bc  output: [[LLVMLINKOUT:.*]].bc
-// DEVLIBS-NEXT: "{{.*}}llvm-spirv{{.*}}" {{.*}}-o a.spv [[LLVMLINKOUT]].bc
+// DEVLIBS-NEXT: SPIR-V Backend: input: [[LLVMLINKOUT]].bc, output: a.spv
 //
 // Test a simple case with a random file (not bitcode) as input.
 // RUN: touch %t.o
@@ -29,13 +29,3 @@
 // RUN: not clang-sycl-linker --dry-run -triple=spirv64 %t_1.bc %t_2.bc 
--library-path=%T --device-libs=lib1.bc,lib2.bc,lib3.bc -o a.spv 2>&1 \
 // RUN:   | FileCheck %s --check-prefix=DEVLIBSERR2
 // DEVLIBSERR2: '{{.*}}lib3.bc' SYCL device library file is not found
-//
-// Test if correct set of llvm-spirv options are emitted for windows 
environment.
-// RUN: clang-sycl-linker --dry-run -v -triple=spirv64 --is-windows-msvc-env 
%t_1.bc %t_2.bc -o a.spv 2>&1 \
-// RUN:   | FileCheck %s --check-prefix=LLVMOPTSWIN
-// LLVMOPTSWIN: -spirv-debug-info-version=ocl-100 
-spirv-allow-extra-diexpressions -spirv-allow-unknown-intrinsics=llvm.genx. 
-spirv-ext=
-//
-// Test if correct set of llvm-spirv options are emitted for linux environment.
-// RUN: clang-sycl-linker --dry-run -v -triple=spirv64  %t_1.bc %t_2.bc -o 
a.spv 2>&1 \
-// RUN:   | FileCheck %s --check-prefix=LLVMOPTSLIN
-// LLVMOPTSLIN: -spirv-debug-info-version=nonsemantic-shader-200 
-spirv-allow-unknown-intrinsics=llvm.genx. -spirv-ext=
diff --git a/clang/test/Driver/sycl-link-spirv-target.cpp 
b/clang/test/Driver/sycl-link-spirv-target.cpp
index 7585ef8b14a59..586adae619165 100644
--- a/clang/test/Driver/sycl-link-spirv-target.cpp
+++ b/clang/test/Driver/sycl-link-spirv-target.cpp
@@ -3,7 +3,7 @@
 //
 // Test that -Xlinker options are being passed to clang-sycl-linker.
 // RUN: touch %t.bc
-// RUN: %clangxx -### --target=spirv64 --sycl-link -Xlinker 
--llvm-spirv-path=/tmp \
-// RUN:   -Xlinker -triple=spirv64 -Xlinker --library-path=/tmp -Xlinker 
--device-libs=lib1.bc,lib2.bc %t.bc 2>&1 \
+// RUN: %clangxx -### --target=spirv64 --sycl-link -Xlinker -triple=spirv64 
-Xlinker --library-path=/tmp \
+// RUN:   -Xlinker --device-libs=lib1.bc,lib2.bc %t.bc 2>&1 \
 // RUN:   | FileCheck %s -check-prefix=XLINKEROPTS
-// XLINKEROPTS: "{{.*}}clang-sycl-linker{{.*}}" "--llvm-spirv-path=/tmp" 
"-triple=spirv64" "--library-path=/tmp" "--device-libs=lib1.bc,lib2.bc" 
"{{.*}}.bc" "-o" "a.out"
+// XLINKEROPTS: "{{.*}}clang-sycl-linker{{.*}}" "-triple=spirv64" 
"--library-path=/tmp" "--device-libs=lib1.bc,lib2.bc" "{{.*}}.bc" "-o" "a.out"
diff --git a/clang/tools/clang-sycl-linker/CMakeLists.txt 
b/clang/tools/clang-sycl-linker/CMakeLists.txt
index 382c0ca441940..ee89e8b0a5570 100644
--- a/clang/tools/clang-sycl-linker/CMakeLists.txt
+++ b/clang/tools/clang-sycl-linker/CMakeLists.txt
@@ -1,14 +1,17 @@
 set(LLVM_LINK_COMPONENTS
   ${LLVM_TARGETS_TO_BUILD}
+  Analysis
   BinaryFormat
   BitWriter
   Core
   IRReader
   Linker
+  MC
   Option
   Object
-  TargetParser
   Support
+  Target
+  TargetParser
   )
 
 set(LLVM_TARGET_DEFIN

[clang] [SYCL][SPIR-V Backend][clang-sycl-linker] Add SPIR-V backend support inside clang-sycl-linker (PR #133967)

2025-04-02 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa updated 
https://github.com/llvm/llvm-project/pull/133967

>From 0ab40315cd5ca320d84f87c99b8f23d9b6e8ed36 Mon Sep 17 00:00:00 2001
From: Arvind Sudarsanam 
Date: Mon, 31 Mar 2025 14:31:24 -0700
Subject: [PATCH 1/6] [SYCL][SPIR-V Backend][clang-sycl-linker] Add SPIR-V
 backend support inside clang-sycl-linker

This PR does the following:

1. Use SPIR-V backend to do LLVM to SPIR-V translation inside clang-sycl-linker
2. Remove llvm-spirv translator from clang-sycl-linker
Currently, no SPIR-V extensions are enabled for SYCL compilation flow. This 
will be updated in subsequent commits.

Thanks

Signed-off-by: Arvind Sudarsanam 
---
 clang/test/Driver/clang-sycl-linker-test.cpp  |  14 +-
 clang/test/Driver/sycl-link-spirv-target.cpp  |   6 +-
 clang/tools/clang-sycl-linker/CMakeLists.txt  |   5 +-
 .../clang-sycl-linker/ClangSYCLLinker.cpp | 187 +++---
 clang/tools/clang-sycl-linker/SYCLLinkOpts.td |   2 +-
 5 files changed, 76 insertions(+), 138 deletions(-)

diff --git a/clang/test/Driver/clang-sycl-linker-test.cpp 
b/clang/test/Driver/clang-sycl-linker-test.cpp
index 729561bd09cd8..2f860ae74e97d 100644
--- a/clang/test/Driver/clang-sycl-linker-test.cpp
+++ b/clang/test/Driver/clang-sycl-linker-test.cpp
@@ -6,7 +6,7 @@
 // RUN: clang-sycl-linker --dry-run -v -triple=spirv64 %t_1.bc %t_2.bc -o 
a.spv 2>&1 \
 // RUN:   | FileCheck %s --check-prefix=SIMPLE-FO
 // SIMPLE-FO: sycl-device-link: inputs: {{.*}}.bc, {{.*}}.bc  libfiles:  
output: [[LLVMLINKOUT:.*]].bc
-// SIMPLE-FO-NEXT: "{{.*}}llvm-spirv{{.*}}" {{.*}}-o a.spv [[LLVMLINKOUT]].bc
+// SIMPLE-FO-NEXT: SPIR-V Backend: input: [[LLVMLINKOUT]].bc, output: a.spv
 //
 // Test the dry run of a simple case with device library files specified.
 // RUN: touch %T/lib1.bc
@@ -14,7 +14,7 @@
 // RUN: clang-sycl-linker --dry-run -v -triple=spirv64 %t_1.bc %t_2.bc 
--library-path=%T --device-libs=lib1.bc,lib2.bc -o a.spv 2>&1 \
 // RUN:   | FileCheck %s --check-prefix=DEVLIBS
 // DEVLIBS: sycl-device-link: inputs: {{.*}}.bc  libfiles: {{.*}}lib1.bc, 
{{.*}}lib2.bc  output: [[LLVMLINKOUT:.*]].bc
-// DEVLIBS-NEXT: "{{.*}}llvm-spirv{{.*}}" {{.*}}-o a.spv [[LLVMLINKOUT]].bc
+// DEVLIBS-NEXT: SPIR-V Backend: input: [[LLVMLINKOUT]].bc, output: a.spv
 //
 // Test a simple case with a random file (not bitcode) as input.
 // RUN: touch %t.o
@@ -29,13 +29,3 @@
 // RUN: not clang-sycl-linker --dry-run -triple=spirv64 %t_1.bc %t_2.bc 
--library-path=%T --device-libs=lib1.bc,lib2.bc,lib3.bc -o a.spv 2>&1 \
 // RUN:   | FileCheck %s --check-prefix=DEVLIBSERR2
 // DEVLIBSERR2: '{{.*}}lib3.bc' SYCL device library file is not found
-//
-// Test if correct set of llvm-spirv options are emitted for windows 
environment.
-// RUN: clang-sycl-linker --dry-run -v -triple=spirv64 --is-windows-msvc-env 
%t_1.bc %t_2.bc -o a.spv 2>&1 \
-// RUN:   | FileCheck %s --check-prefix=LLVMOPTSWIN
-// LLVMOPTSWIN: -spirv-debug-info-version=ocl-100 
-spirv-allow-extra-diexpressions -spirv-allow-unknown-intrinsics=llvm.genx. 
-spirv-ext=
-//
-// Test if correct set of llvm-spirv options are emitted for linux environment.
-// RUN: clang-sycl-linker --dry-run -v -triple=spirv64  %t_1.bc %t_2.bc -o 
a.spv 2>&1 \
-// RUN:   | FileCheck %s --check-prefix=LLVMOPTSLIN
-// LLVMOPTSLIN: -spirv-debug-info-version=nonsemantic-shader-200 
-spirv-allow-unknown-intrinsics=llvm.genx. -spirv-ext=
diff --git a/clang/test/Driver/sycl-link-spirv-target.cpp 
b/clang/test/Driver/sycl-link-spirv-target.cpp
index 7585ef8b14a59..586adae619165 100644
--- a/clang/test/Driver/sycl-link-spirv-target.cpp
+++ b/clang/test/Driver/sycl-link-spirv-target.cpp
@@ -3,7 +3,7 @@
 //
 // Test that -Xlinker options are being passed to clang-sycl-linker.
 // RUN: touch %t.bc
-// RUN: %clangxx -### --target=spirv64 --sycl-link -Xlinker 
--llvm-spirv-path=/tmp \
-// RUN:   -Xlinker -triple=spirv64 -Xlinker --library-path=/tmp -Xlinker 
--device-libs=lib1.bc,lib2.bc %t.bc 2>&1 \
+// RUN: %clangxx -### --target=spirv64 --sycl-link -Xlinker -triple=spirv64 
-Xlinker --library-path=/tmp \
+// RUN:   -Xlinker --device-libs=lib1.bc,lib2.bc %t.bc 2>&1 \
 // RUN:   | FileCheck %s -check-prefix=XLINKEROPTS
-// XLINKEROPTS: "{{.*}}clang-sycl-linker{{.*}}" "--llvm-spirv-path=/tmp" 
"-triple=spirv64" "--library-path=/tmp" "--device-libs=lib1.bc,lib2.bc" 
"{{.*}}.bc" "-o" "a.out"
+// XLINKEROPTS: "{{.*}}clang-sycl-linker{{.*}}" "-triple=spirv64" 
"--library-path=/tmp" "--device-libs=lib1.bc,lib2.bc" "{{.*}}.bc" "-o" "a.out"
diff --git a/clang/tools/clang-sycl-linker/CMakeLists.txt 
b/clang/tools/clang-sycl-linker/CMakeLists.txt
index 382c0ca441940..ee89e8b0a5570 100644
--- a/clang/tools/clang-sycl-linker/CMakeLists.txt
+++ b/clang/tools/clang-sycl-linker/CMakeLists.txt
@@ -1,14 +1,17 @@
 set(LLVM_LINK_COMPONENTS
   ${LLVM_TARGETS_TO_BUILD}
+  Analysis
   BinaryFormat
   BitWriter
   Core
   IRReader
   Linker
+  MC
   Option
   Object
-  TargetParser
   Support
+  Target
+  TargetParser
   )
 
 set(LLVM_TARGET_DEFIN

[clang] [SYCL][SPIR-V Backend][clang-sycl-linker] Add SPIR-V backend support inside clang-sycl-linker (PR #133967)

2025-04-02 Thread Arvind Sudarsanam via cfe-commits

asudarsa wrote:

@sarnex, @jhuber6 

Thanks much for your kind feedbacks. This is ready to be merged. Can one of you 
please help?

Sincerely

https://github.com/llvm/llvm-project/pull/133967
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [SYCL][SPIR-V Backend][clang-sycl-linker] Add SPIR-V backend support inside clang-sycl-linker (PR #133967)

2025-04-02 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa updated 
https://github.com/llvm/llvm-project/pull/133967

>From 0ab40315cd5ca320d84f87c99b8f23d9b6e8ed36 Mon Sep 17 00:00:00 2001
From: Arvind Sudarsanam 
Date: Mon, 31 Mar 2025 14:31:24 -0700
Subject: [PATCH 1/6] [SYCL][SPIR-V Backend][clang-sycl-linker] Add SPIR-V
 backend support inside clang-sycl-linker

This PR does the following:

1. Use SPIR-V backend to do LLVM to SPIR-V translation inside clang-sycl-linker
2. Remove llvm-spirv translator from clang-sycl-linker
Currently, no SPIR-V extensions are enabled for SYCL compilation flow. This 
will be updated in subsequent commits.

Thanks

Signed-off-by: Arvind Sudarsanam 
---
 clang/test/Driver/clang-sycl-linker-test.cpp  |  14 +-
 clang/test/Driver/sycl-link-spirv-target.cpp  |   6 +-
 clang/tools/clang-sycl-linker/CMakeLists.txt  |   5 +-
 .../clang-sycl-linker/ClangSYCLLinker.cpp | 187 +++---
 clang/tools/clang-sycl-linker/SYCLLinkOpts.td |   2 +-
 5 files changed, 76 insertions(+), 138 deletions(-)

diff --git a/clang/test/Driver/clang-sycl-linker-test.cpp 
b/clang/test/Driver/clang-sycl-linker-test.cpp
index 729561bd09cd8..2f860ae74e97d 100644
--- a/clang/test/Driver/clang-sycl-linker-test.cpp
+++ b/clang/test/Driver/clang-sycl-linker-test.cpp
@@ -6,7 +6,7 @@
 // RUN: clang-sycl-linker --dry-run -v -triple=spirv64 %t_1.bc %t_2.bc -o 
a.spv 2>&1 \
 // RUN:   | FileCheck %s --check-prefix=SIMPLE-FO
 // SIMPLE-FO: sycl-device-link: inputs: {{.*}}.bc, {{.*}}.bc  libfiles:  
output: [[LLVMLINKOUT:.*]].bc
-// SIMPLE-FO-NEXT: "{{.*}}llvm-spirv{{.*}}" {{.*}}-o a.spv [[LLVMLINKOUT]].bc
+// SIMPLE-FO-NEXT: SPIR-V Backend: input: [[LLVMLINKOUT]].bc, output: a.spv
 //
 // Test the dry run of a simple case with device library files specified.
 // RUN: touch %T/lib1.bc
@@ -14,7 +14,7 @@
 // RUN: clang-sycl-linker --dry-run -v -triple=spirv64 %t_1.bc %t_2.bc 
--library-path=%T --device-libs=lib1.bc,lib2.bc -o a.spv 2>&1 \
 // RUN:   | FileCheck %s --check-prefix=DEVLIBS
 // DEVLIBS: sycl-device-link: inputs: {{.*}}.bc  libfiles: {{.*}}lib1.bc, 
{{.*}}lib2.bc  output: [[LLVMLINKOUT:.*]].bc
-// DEVLIBS-NEXT: "{{.*}}llvm-spirv{{.*}}" {{.*}}-o a.spv [[LLVMLINKOUT]].bc
+// DEVLIBS-NEXT: SPIR-V Backend: input: [[LLVMLINKOUT]].bc, output: a.spv
 //
 // Test a simple case with a random file (not bitcode) as input.
 // RUN: touch %t.o
@@ -29,13 +29,3 @@
 // RUN: not clang-sycl-linker --dry-run -triple=spirv64 %t_1.bc %t_2.bc 
--library-path=%T --device-libs=lib1.bc,lib2.bc,lib3.bc -o a.spv 2>&1 \
 // RUN:   | FileCheck %s --check-prefix=DEVLIBSERR2
 // DEVLIBSERR2: '{{.*}}lib3.bc' SYCL device library file is not found
-//
-// Test if correct set of llvm-spirv options are emitted for windows 
environment.
-// RUN: clang-sycl-linker --dry-run -v -triple=spirv64 --is-windows-msvc-env 
%t_1.bc %t_2.bc -o a.spv 2>&1 \
-// RUN:   | FileCheck %s --check-prefix=LLVMOPTSWIN
-// LLVMOPTSWIN: -spirv-debug-info-version=ocl-100 
-spirv-allow-extra-diexpressions -spirv-allow-unknown-intrinsics=llvm.genx. 
-spirv-ext=
-//
-// Test if correct set of llvm-spirv options are emitted for linux environment.
-// RUN: clang-sycl-linker --dry-run -v -triple=spirv64  %t_1.bc %t_2.bc -o 
a.spv 2>&1 \
-// RUN:   | FileCheck %s --check-prefix=LLVMOPTSLIN
-// LLVMOPTSLIN: -spirv-debug-info-version=nonsemantic-shader-200 
-spirv-allow-unknown-intrinsics=llvm.genx. -spirv-ext=
diff --git a/clang/test/Driver/sycl-link-spirv-target.cpp 
b/clang/test/Driver/sycl-link-spirv-target.cpp
index 7585ef8b14a59..586adae619165 100644
--- a/clang/test/Driver/sycl-link-spirv-target.cpp
+++ b/clang/test/Driver/sycl-link-spirv-target.cpp
@@ -3,7 +3,7 @@
 //
 // Test that -Xlinker options are being passed to clang-sycl-linker.
 // RUN: touch %t.bc
-// RUN: %clangxx -### --target=spirv64 --sycl-link -Xlinker 
--llvm-spirv-path=/tmp \
-// RUN:   -Xlinker -triple=spirv64 -Xlinker --library-path=/tmp -Xlinker 
--device-libs=lib1.bc,lib2.bc %t.bc 2>&1 \
+// RUN: %clangxx -### --target=spirv64 --sycl-link -Xlinker -triple=spirv64 
-Xlinker --library-path=/tmp \
+// RUN:   -Xlinker --device-libs=lib1.bc,lib2.bc %t.bc 2>&1 \
 // RUN:   | FileCheck %s -check-prefix=XLINKEROPTS
-// XLINKEROPTS: "{{.*}}clang-sycl-linker{{.*}}" "--llvm-spirv-path=/tmp" 
"-triple=spirv64" "--library-path=/tmp" "--device-libs=lib1.bc,lib2.bc" 
"{{.*}}.bc" "-o" "a.out"
+// XLINKEROPTS: "{{.*}}clang-sycl-linker{{.*}}" "-triple=spirv64" 
"--library-path=/tmp" "--device-libs=lib1.bc,lib2.bc" "{{.*}}.bc" "-o" "a.out"
diff --git a/clang/tools/clang-sycl-linker/CMakeLists.txt 
b/clang/tools/clang-sycl-linker/CMakeLists.txt
index 382c0ca441940..ee89e8b0a5570 100644
--- a/clang/tools/clang-sycl-linker/CMakeLists.txt
+++ b/clang/tools/clang-sycl-linker/CMakeLists.txt
@@ -1,14 +1,17 @@
 set(LLVM_LINK_COMPONENTS
   ${LLVM_TARGETS_TO_BUILD}
+  Analysis
   BinaryFormat
   BitWriter
   Core
   IRReader
   Linker
+  MC
   Option
   Object
-  TargetParser
   Support
+  Target
+  TargetParser
   )
 
 set(LLVM_TARGET_DEFIN

[clang] [Clang][SYCL] Add AOT compilation support for Intel GPUs in clang-sycl-linker (PR #133194)

2025-04-03 Thread Arvind Sudarsanam via cfe-commits

asudarsa wrote:

For the test failures, please resync with the tip of the repo and the test 
failure should disappear.

https://github.com/llvm/llvm-project/pull/133194
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][SYCL] Add AOT compilation support for Intel GPUs in clang-sycl-linker (PR #133194)

2025-04-03 Thread Arvind Sudarsanam via cfe-commits


@@ -163,6 +220,15 @@ static inline bool IsAMDOffloadArch(OffloadArch A) {
   return A >= OffloadArch::GFX600 && A < OffloadArch::Generic;
 }
 
+static inline bool IsIntelCPUArch(OffloadArch Arch) {

asudarsa wrote:

For sake of completion, can you please add IsIntelOffloadArch as well?

Thanks

https://github.com/llvm/llvm-project/pull/133194
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Disable RTTI for offloading at the frontend level (PR #127082)

2025-03-31 Thread Arvind Sudarsanam via cfe-commits


@@ -7117,6 +7117,13 @@ void Clang::ConstructJob(Compilation &C, const JobAction 
&JA,
 }
   }
 
+  // The offloading devices do not support RTTI.

asudarsa wrote:

I see that IsHIPDevice has been added to the check here (compared to what was 
deleted inside CodeGenModule.h). is that expected?

Thanks

https://github.com/llvm/llvm-project/pull/127082
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Introduce '--offload-targets=' to generically target toolchains (PR #125556)

2025-03-31 Thread Arvind Sudarsanam via cfe-commits

asudarsa wrote:

Hi @jhuber6 

Just a quick ping to check if this PR is still alive. I can take a look if it 
is.

Thanks

https://github.com/llvm/llvm-project/pull/125556
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [CMAKE][AMDGPU] fix build failure caused by PR #133619 (PR #133776)

2025-03-31 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa edited 
https://github.com/llvm/llvm-project/pull/133776
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [CMAKE][AMDGPU] fix build failure caused by PR #133619 (PR #133776)

2025-03-31 Thread Arvind Sudarsanam via cfe-commits

asudarsa wrote:

> > > @farzonl can you please revert the original commit? It seems to break 
> > > just any LLVM build, so I guess the CI testing of all new PRs should be 
> > > affected.
> > 
> > 
> > I don't see it breaking any build in LLVM, just the amd offload ones. Al 
> > the Premerge tests also passed. I suspect the `AMDGPUBot.cmake` is doing 
> > something unique. Will study it more
> > but the PR to revert is here #133795
> 
> Thanks! I think, it only affects the `-DBUILD_SHARED_LIBS=ON` builds, so, 
> indeed, not all the builds :)
> 
> A couple more examples: 
> https://lab.llvm.org/buildbot/#/builders/145/builds/6156 
> https://lab.llvm.org/buildbot/#/builders/89/builds/19738

Thanks @vzakhari, I also am building with -DBUILD_SHARED_LIBS=ON

https://github.com/llvm/llvm-project/pull/133776
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [CMAKE][AMDGPU] fix build failure caused by PR #133619 (PR #133776)

2025-03-31 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa approved this pull request.

LGTM

https://github.com/llvm/llvm-project/pull/133776
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-sycl-linker] Replace llvm-link with API calls (PR #133797)

2025-03-31 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa updated 
https://github.com/llvm/llvm-project/pull/133797

>From 675595e453b2452e49847ba3b949909367c7942a Mon Sep 17 00:00:00 2001
From: Arvind Sudarsanam 
Date: Mon, 31 Mar 2025 13:04:47 -0700
Subject: [PATCH 1/2] [clang-sycl-linker] Replace llvm-link with API calls

This PR has the following changes:

Replace llvm-link with calls to linkInModule to link device files
Add -print-linked-module option to dump linked module for testing
Added a test to verify that linking is working as expected.
We will eventually move to using thin LTO for linking device inputs.

Thanks

Signed-off-by: Arvind Sudarsanam 
---
 clang/test/Driver/Inputs/SYCL/bar.ll  |   7 +
 clang/test/Driver/Inputs/SYCL/baz.ll  |  15 ++
 clang/test/Driver/Inputs/SYCL/foo.ll  |  19 ++
 clang/test/Driver/Inputs/SYCL/libsycl.ll  |  13 ++
 clang/test/Driver/clang-sycl-linker-test.cpp  |  43 ++---
 clang/test/Driver/link-device-code.test   |  23 +++
 clang/test/Driver/sycl-link-spirv-target.cpp  |   4 +-
 clang/tools/clang-sycl-linker/CMakeLists.txt  |   4 +
 .../clang-sycl-linker/ClangSYCLLinker.cpp | 175 ++
 clang/tools/clang-sycl-linker/SYCLLinkOpts.td |  16 +-
 10 files changed, 210 insertions(+), 109 deletions(-)
 create mode 100644 clang/test/Driver/Inputs/SYCL/bar.ll
 create mode 100644 clang/test/Driver/Inputs/SYCL/baz.ll
 create mode 100644 clang/test/Driver/Inputs/SYCL/foo.ll
 create mode 100644 clang/test/Driver/Inputs/SYCL/libsycl.ll
 create mode 100644 clang/test/Driver/link-device-code.test

diff --git a/clang/test/Driver/Inputs/SYCL/bar.ll 
b/clang/test/Driver/Inputs/SYCL/bar.ll
new file mode 100644
index 0..d17221b8dca18
--- /dev/null
+++ b/clang/test/Driver/Inputs/SYCL/bar.ll
@@ -0,0 +1,7 @@
+target triple = "spirv64"
+
+define spir_func i32 @bar_func1(i32 %a, i32 %b) {
+entry:
+  %res = add nsw i32 %b, %a
+  ret i32 %res
+}
diff --git a/clang/test/Driver/Inputs/SYCL/baz.ll 
b/clang/test/Driver/Inputs/SYCL/baz.ll
new file mode 100644
index 0..6cdf3735ed77e
--- /dev/null
+++ b/clang/test/Driver/Inputs/SYCL/baz.ll
@@ -0,0 +1,15 @@
+target triple = "spirv64"
+
+define spir_func i32 @bar_func1(i32 %a, i32 %b) {
+entry:
+  %mul = shl nsw i32 %a, 1
+  %res = add nsw i32 %mul, %b
+  ret i32 %res
+}
+
+define spir_func i32 @baz_func1(i32 %a) {
+entry:
+  %add = add nsw i32 %a, 5
+  %res = tail call spir_func i32 @bar_func1(i32 %a, i32 %add)
+  ret i32 %res
+}
diff --git a/clang/test/Driver/Inputs/SYCL/foo.ll 
b/clang/test/Driver/Inputs/SYCL/foo.ll
new file mode 100644
index 0..43aaf1424ee2d
--- /dev/null
+++ b/clang/test/Driver/Inputs/SYCL/foo.ll
@@ -0,0 +1,19 @@
+target triple = "spirv64"
+
+define spir_func i32 @foo_func1(i32 %a, i32 %b) {
+entry:
+  %call = tail call spir_func i32 @addFive(i32 %b)
+  %res = tail call spir_func i32 @bar_func1(i32 %a, i32 %call)
+  ret i32 %res
+}
+
+declare spir_func i32 @bar_func1(i32, i32)
+
+declare spir_func i32 @addFive(i32)
+
+define spir_func i32 @foo_func2(i32 %c, i32 %d, i32 %e) {
+entry:
+  %call = tail call spir_func i32 @foo_func1(i32 %c, i32 %d)
+  %res = mul nsw i32 %call, %e
+  ret i32 %res
+}
diff --git a/clang/test/Driver/Inputs/SYCL/libsycl.ll 
b/clang/test/Driver/Inputs/SYCL/libsycl.ll
new file mode 100644
index 0..fdc4643e97b6a
--- /dev/null
+++ b/clang/test/Driver/Inputs/SYCL/libsycl.ll
@@ -0,0 +1,13 @@
+target triple = "spirv64"
+
+define spir_func i32 @addFive(i32 %a) {
+entry:
+  %res = add nsw i32 %a, 5
+  ret i32 %res
+}
+
+define spir_func i32 @unusedFunc(i32 %a) {
+entry:
+  %res = mul nsw i32 %a, 5
+  ret i32 %res
+}
diff --git a/clang/test/Driver/clang-sycl-linker-test.cpp 
b/clang/test/Driver/clang-sycl-linker-test.cpp
index f358900b4fbd8..729561bd09cd8 100644
--- a/clang/test/Driver/clang-sycl-linker-test.cpp
+++ b/clang/test/Driver/clang-sycl-linker-test.cpp
@@ -1,48 +1,41 @@
 // Tests the clang-sycl-linker tool.
 //
-// Test a simple case without arguments.
-// RUN: %clangxx -emit-llvm -c %s -o %t_1.bc
-// RUN: %clangxx -emit-llvm -c %s -o %t_2.bc
-// RUN: clang-sycl-linker --dry-run -triple spirv64 %t_1.bc %t_2.bc -o a.spv 
2>&1 \
-// RUN:   | FileCheck %s --check-prefix=SIMPLE
-// SIMPLE: "{{.*}}llvm-link{{.*}}" {{.*}}.bc {{.*}}.bc -o 
[[FIRSTLLVMLINKOUT:.*]].bc --suppress-warnings
-// SIMPLE-NEXT: "{{.*}}llvm-spirv{{.*}}" {{.*}}-o a.spv [[FIRSTLLVMLINKOUT]].bc
+// Test the dry run of a simple case to link two input files.
+// RUN: %clangxx -emit-llvm -c -target spirv64 %s -o %t_1.bc
+// RUN: %clangxx -emit-llvm -c -target spirv64 %s -o %t_2.bc
+// RUN: clang-sycl-linker --dry-run -v -triple=spirv64 %t_1.bc %t_2.bc -o 
a.spv 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=SIMPLE-FO
+// SIMPLE-FO: sycl-device-link: inputs: {{.*}}.bc, {{.*}}.bc  libfiles:  
output: [[LLVMLINKOUT:.*]].bc
+// SIMPLE-FO-NEXT: "{{.*}}llvm-spirv{{.*}}" {{.*}}-o a.spv [[LLVMLINKOUT]].bc
 //
-// Test that llvm-link is not called when only one input is present.
-// RUN:

[clang] [CMAKE][AMDGPU] fix build failure caused by PR #133619 (PR #133776)

2025-03-31 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa commented:

I tried to build it locally and I got a series of 'undefined reference' errors. 
Sorry, I will need to revert my approval.

Thanks

https://github.com/llvm/llvm-project/pull/133776
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][SYCL] Add support AOT compilation support for Intel GPUs in clang-sycl-linker (PR #133194)

2025-03-31 Thread Arvind Sudarsanam via cfe-commits

asudarsa wrote:

Please remove redundant 'support' from PR topic

https://github.com/llvm/llvm-project/pull/133194
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-sycl-linker] Replace llvm-link with API calls (PR #133797)

2025-03-31 Thread Arvind Sudarsanam via cfe-commits

asudarsa wrote:

> Shouldn't we be able to just use LTO after the SPIR-V backend left 
> experimental?

Hi @jhuber6 

Thanks for the ping back. That is the eventual path for us. However, we would 
like to do it in stages. Replacing llvm-link tool with linkInModule call helps 
us to achieve one of our initial goals: To replace calls to external tools with 
library calls, wherever possible. Once we have an end-to-end device code 
linking available upstream, we should be able to replace linkInModule calls by 
using LTO.

Thanks

https://github.com/llvm/llvm-project/pull/133797
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-sycl-linker] Replace llvm-link with API calls (PR #133797)

2025-03-31 Thread Arvind Sudarsanam via cfe-commits

asudarsa wrote:

@jhuber6 

Can you please take a look? This is one of the many changes we are making to 
upstream SYCL.

Thanks

https://github.com/llvm/llvm-project/pull/133797
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-sycl-linker] Replace llvm-link with API calls (PR #133797)

2025-04-01 Thread Arvind Sudarsanam via cfe-commits

https://github.com/asudarsa created 
https://github.com/llvm/llvm-project/pull/133797

This PR has the following changes:

Replace llvm-link with calls to linkInModule to link device files Add 
-print-linked-module option to dump linked module for testing Added a test to 
verify that linking is working as expected. We will eventually move to using 
thin LTO for linking device inputs.

Thanks

>From 84b1894733786434f27bc84f7acb7edf38274276 Mon Sep 17 00:00:00 2001
From: Arvind Sudarsanam 
Date: Mon, 31 Mar 2025 13:04:47 -0700
Subject: [PATCH] [clang-sycl-linker] Replace llvm-link with API calls

This PR has the following changes:

Replace llvm-link with calls to linkInModule to link device files
Add -print-linked-module option to dump linked module for testing
Added a test to verify that linking is working as expected.
We will eventually move to using thin LTO for linking device inputs.

Thanks

Signed-off-by: Arvind Sudarsanam 
---
 clang/test/Driver/Inputs/SYCL/bar.ll  |   7 +
 clang/test/Driver/Inputs/SYCL/baz.ll  |  15 ++
 clang/test/Driver/Inputs/SYCL/foo.ll  |  19 ++
 clang/test/Driver/Inputs/SYCL/libsycl.ll  |  13 ++
 clang/test/Driver/clang-sycl-linker-test.cpp  |  43 ++---
 clang/test/Driver/link-device-code.test   |  23 +++
 clang/test/Driver/sycl-link-spirv-target.cpp  |   4 +-
 clang/tools/clang-sycl-linker/CMakeLists.txt  |   4 +
 .../clang-sycl-linker/ClangSYCLLinker.cpp | 175 ++
 clang/tools/clang-sycl-linker/SYCLLinkOpts.td |  16 +-
 10 files changed, 210 insertions(+), 109 deletions(-)
 create mode 100644 clang/test/Driver/Inputs/SYCL/bar.ll
 create mode 100644 clang/test/Driver/Inputs/SYCL/baz.ll
 create mode 100644 clang/test/Driver/Inputs/SYCL/foo.ll
 create mode 100644 clang/test/Driver/Inputs/SYCL/libsycl.ll
 create mode 100644 clang/test/Driver/link-device-code.test

diff --git a/clang/test/Driver/Inputs/SYCL/bar.ll 
b/clang/test/Driver/Inputs/SYCL/bar.ll
new file mode 100644
index 0..d17221b8dca18
--- /dev/null
+++ b/clang/test/Driver/Inputs/SYCL/bar.ll
@@ -0,0 +1,7 @@
+target triple = "spirv64"
+
+define spir_func i32 @bar_func1(i32 %a, i32 %b) {
+entry:
+  %res = add nsw i32 %b, %a
+  ret i32 %res
+}
diff --git a/clang/test/Driver/Inputs/SYCL/baz.ll 
b/clang/test/Driver/Inputs/SYCL/baz.ll
new file mode 100644
index 0..6cdf3735ed77e
--- /dev/null
+++ b/clang/test/Driver/Inputs/SYCL/baz.ll
@@ -0,0 +1,15 @@
+target triple = "spirv64"
+
+define spir_func i32 @bar_func1(i32 %a, i32 %b) {
+entry:
+  %mul = shl nsw i32 %a, 1
+  %res = add nsw i32 %mul, %b
+  ret i32 %res
+}
+
+define spir_func i32 @baz_func1(i32 %a) {
+entry:
+  %add = add nsw i32 %a, 5
+  %res = tail call spir_func i32 @bar_func1(i32 %a, i32 %add)
+  ret i32 %res
+}
diff --git a/clang/test/Driver/Inputs/SYCL/foo.ll 
b/clang/test/Driver/Inputs/SYCL/foo.ll
new file mode 100644
index 0..43aaf1424ee2d
--- /dev/null
+++ b/clang/test/Driver/Inputs/SYCL/foo.ll
@@ -0,0 +1,19 @@
+target triple = "spirv64"
+
+define spir_func i32 @foo_func1(i32 %a, i32 %b) {
+entry:
+  %call = tail call spir_func i32 @addFive(i32 %b)
+  %res = tail call spir_func i32 @bar_func1(i32 %a, i32 %call)
+  ret i32 %res
+}
+
+declare spir_func i32 @bar_func1(i32, i32)
+
+declare spir_func i32 @addFive(i32)
+
+define spir_func i32 @foo_func2(i32 %c, i32 %d, i32 %e) {
+entry:
+  %call = tail call spir_func i32 @foo_func1(i32 %c, i32 %d)
+  %res = mul nsw i32 %call, %e
+  ret i32 %res
+}
diff --git a/clang/test/Driver/Inputs/SYCL/libsycl.ll 
b/clang/test/Driver/Inputs/SYCL/libsycl.ll
new file mode 100644
index 0..fdc4643e97b6a
--- /dev/null
+++ b/clang/test/Driver/Inputs/SYCL/libsycl.ll
@@ -0,0 +1,13 @@
+target triple = "spirv64"
+
+define spir_func i32 @addFive(i32 %a) {
+entry:
+  %res = add nsw i32 %a, 5
+  ret i32 %res
+}
+
+define spir_func i32 @unusedFunc(i32 %a) {
+entry:
+  %res = mul nsw i32 %a, 5
+  ret i32 %res
+}
diff --git a/clang/test/Driver/clang-sycl-linker-test.cpp 
b/clang/test/Driver/clang-sycl-linker-test.cpp
index f358900b4fbd8..729561bd09cd8 100644
--- a/clang/test/Driver/clang-sycl-linker-test.cpp
+++ b/clang/test/Driver/clang-sycl-linker-test.cpp
@@ -1,48 +1,41 @@
 // Tests the clang-sycl-linker tool.
 //
-// Test a simple case without arguments.
-// RUN: %clangxx -emit-llvm -c %s -o %t_1.bc
-// RUN: %clangxx -emit-llvm -c %s -o %t_2.bc
-// RUN: clang-sycl-linker --dry-run -triple spirv64 %t_1.bc %t_2.bc -o a.spv 
2>&1 \
-// RUN:   | FileCheck %s --check-prefix=SIMPLE
-// SIMPLE: "{{.*}}llvm-link{{.*}}" {{.*}}.bc {{.*}}.bc -o 
[[FIRSTLLVMLINKOUT:.*]].bc --suppress-warnings
-// SIMPLE-NEXT: "{{.*}}llvm-spirv{{.*}}" {{.*}}-o a.spv [[FIRSTLLVMLINKOUT]].bc
+// Test the dry run of a simple case to link two input files.
+// RUN: %clangxx -emit-llvm -c -target spirv64 %s -o %t_1.bc
+// RUN: %clangxx -emit-llvm -c -target spirv64 %s -o %t_2.bc
+// RUN: clang-sycl-linker --dry-run -v -triple=spirv64 %t_1.bc %t_2.bc -o 
a.spv 2>&1 \
+// RUN:   |