Skip to content

Commit 0c1e7cc

Browse files
authored
[clang-sycl-linker] Generate SymbolTable for each image (#161287)
This PR adds extraction of kernel names for each image and stores them to the Image's StringData field.
1 parent 4aba9f2 commit 0c1e7cc

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,13 @@ static Error runAOTCompile(StringRef InputFile, StringRef OutputFile,
466466
return createStringError(inconvertibleErrorCode(), "Unsupported arch");
467467
}
468468

469+
// TODO: Consider using LLVM-IR metadata to identify globals of interest
470+
bool isKernel(const Function &F) {
471+
const CallingConv::ID CC = F.getCallingConv();
472+
return CC == CallingConv::SPIR_KERNEL || CC == CallingConv::AMDGPU_KERNEL ||
473+
CC == CallingConv::PTX_Kernel;
474+
}
475+
469476
/// Performs the following steps:
470477
/// 1. Link input device code (user code and SYCL device library code).
471478
/// 2. Run SPIR-V code generation.
@@ -486,6 +493,22 @@ Error runSYCLLink(ArrayRef<std::string> Files, const ArgList &Args) {
486493
SmallVector<std::string> SplitModules;
487494
SplitModules.emplace_back(*LinkedFile);
488495

496+
// Generate symbol table.
497+
SmallVector<std::string> SymbolTable;
498+
for (size_t I = 0, E = SplitModules.size(); I != E; ++I) {
499+
Expected<std::unique_ptr<Module>> ModOrErr =
500+
getBitcodeModule(SplitModules[I], C);
501+
if (!ModOrErr)
502+
return ModOrErr.takeError();
503+
504+
SmallVector<StringRef> Symbols;
505+
for (Function &F : **ModOrErr) {
506+
if (isKernel(F))
507+
Symbols.push_back(F.getName());
508+
}
509+
SymbolTable.emplace_back(llvm::join(Symbols.begin(), Symbols.end(), "\n"));
510+
}
511+
489512
bool IsAOTCompileNeeded = IsIntelOffloadArch(
490513
StringToOffloadArch(Args.getLastArgValue(OPT_arch_EQ)));
491514

@@ -523,12 +546,19 @@ Error runSYCLLink(ArrayRef<std::string> Files, const ArgList &Args) {
523546
return createFileError(File, EC);
524547
}
525548
OffloadingImage TheImage{};
526-
TheImage.TheImageKind = IMG_Object;
549+
// TODO: TheImageKind should be
550+
// `IsAOTCompileNeeded ? IMG_Object : IMG_SPIRV;`
551+
// For that we need to update SYCL Runtime to align with the ImageKind enum.
552+
// Temporarily it is initalized to IMG_None, because in that case, SYCL
553+
// Runtime has a heuristic to understand what the Image Kind is, so at least
554+
// it works.
555+
TheImage.TheImageKind = IMG_None;
527556
TheImage.TheOffloadKind = OFK_SYCL;
528557
TheImage.StringData["triple"] =
529558
Args.MakeArgString(Args.getLastArgValue(OPT_triple_EQ));
530559
TheImage.StringData["arch"] =
531560
Args.MakeArgString(Args.getLastArgValue(OPT_arch_EQ));
561+
TheImage.StringData["symbols"] = SymbolTable[I];
532562
TheImage.Image = std::move(*FileOrErr);
533563

534564
llvm::SmallString<0> Buffer = OffloadBinary::write(TheImage);

0 commit comments

Comments
 (0)