Skip to content

[clang-sycl-linker] Generate SymbolTable for each image#161287

Merged
sarnex merged 2 commits into
llvm:mainfrom
YuriPlyakhin:clang-sycl-linker-add-symbols
Sep 30, 2025
Merged

[clang-sycl-linker] Generate SymbolTable for each image#161287
sarnex merged 2 commits into
llvm:mainfrom
YuriPlyakhin:clang-sycl-linker-add-symbols

Conversation

@YuriPlyakhin

Copy link
Copy Markdown
Contributor

This PR adds extraction of kernel names for each image and stores them to the Image's StringData field.

This PR adds extraction of kernel names for each image and
stores them to the Image's StringData field.
@YuriPlyakhin YuriPlyakhin marked this pull request as ready for review September 29, 2025 22:15
@llvmbot llvmbot added the clang Clang issues not falling into any other category label Sep 29, 2025
@llvmbot

llvmbot commented Sep 29, 2025

Copy link
Copy Markdown
Member

@llvm/pr-subscribers-clang

Author: Yury Plyakhin (YuriPlyakhin)

Changes

This PR adds extraction of kernel names for each image and stores them to the Image's StringData field.


Full diff: https://github.com/llvm/llvm-project/pull/161287.diff

1 Files Affected:

  • (modified) clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp (+24-1)
diff --git a/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp b/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
index fde6b55165868..8b186e6e28618 100644
--- a/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
+++ b/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
@@ -466,6 +466,12 @@ static Error runAOTCompile(StringRef InputFile, StringRef OutputFile,
   return createStringError(inconvertibleErrorCode(), "Unsupported arch");
 }
 
+bool isKernel(const Function &F) {
+  const CallingConv::ID CC = F.getCallingConv();
+  return CC == CallingConv::SPIR_KERNEL || CC == CallingConv::AMDGPU_KERNEL ||
+         CC == CallingConv::PTX_Kernel;
+}
+
 /// Performs the following steps:
 /// 1. Link input device code (user code and SYCL device library code).
 /// 2. Run SPIR-V code generation.
@@ -486,6 +492,22 @@ Error runSYCLLink(ArrayRef<std::string> Files, const ArgList &Args) {
   SmallVector<std::string> SplitModules;
   SplitModules.emplace_back(*LinkedFile);
 
+  // Generate symbol table.
+  SmallVector<std::string> SymbolTable;
+  for (size_t I = 0, E = SplitModules.size(); I != E; ++I) {
+    Expected<std::unique_ptr<Module>> ModOrErr =
+        getBitcodeModule(SplitModules[I], C);
+    if (!ModOrErr)
+      return ModOrErr.takeError();
+
+    SmallVector<StringRef> Symbols;
+    for (Function &F : **ModOrErr) {
+      if (isKernel(F))
+        Symbols.push_back(F.getName());
+    }
+    SymbolTable.emplace_back(llvm::join(Symbols.begin(), Symbols.end(), "\n"));
+  }
+
   bool IsAOTCompileNeeded = IsIntelOffloadArch(
       StringToOffloadArch(Args.getLastArgValue(OPT_arch_EQ)));
 
@@ -523,12 +545,13 @@ Error runSYCLLink(ArrayRef<std::string> Files, const ArgList &Args) {
         return createFileError(File, EC);
     }
     OffloadingImage TheImage{};
-    TheImage.TheImageKind = IMG_Object;
+    TheImage.TheImageKind = IMG_None;
     TheImage.TheOffloadKind = OFK_SYCL;
     TheImage.StringData["triple"] =
         Args.MakeArgString(Args.getLastArgValue(OPT_triple_EQ));
     TheImage.StringData["arch"] =
         Args.MakeArgString(Args.getLastArgValue(OPT_arch_EQ));
+    TheImage.StringData["symbols"] = SymbolTable[I];
     TheImage.Image = std::move(*FileOrErr);
 
     llvm::SmallString<0> Buffer = OffloadBinary::write(TheImage);

@bader bader requested review from jhuber6 and sarnex September 29, 2025 22:19
@bader bader added the SYCL https://registry.khronos.org/SYCL label Sep 29, 2025
Comment thread clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
Comment thread clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
Comment thread clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
Comment thread clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp

@jhuber6 jhuber6 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing too offensive, but I feel like part of this should be refactored in the future. You should use LLVM-IR metadata to identify globals of interest and try to avoid needing to create a new string.

Comment thread clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
Comment thread clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
Comment thread clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp
@sarnex sarnex merged commit 0c1e7cc into llvm:main Sep 30, 2025
9 checks passed
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Oct 3, 2025
This PR adds extraction of kernel names for each image and stores them
to the Image's StringData field.
@YuriPlyakhin YuriPlyakhin deleted the clang-sycl-linker-add-symbols branch November 26, 2025 00:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang Clang issues not falling into any other category SYCL https://registry.khronos.org/SYCL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants