Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang-linker-wrapper] Dump any device code format #17546

Merged
merged 5 commits into from
Mar 26, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions clang/test/Driver/linker-wrapper-sycl.cpp
Original file line number Diff line number Diff line change
@@ -47,11 +47,6 @@
// RUN: clang-linker-wrapper -sycl-device-libraries=%t.devicelib.o -sycl-post-link-options="SYCL_POST_LINK_OPTIONS" -llvm-spirv-options="LLVM_SPIRV_OPTIONS" "--host-triple=x86_64-unknown-linux-gnu" "--triple=spir64" "--linker-path=/usr/bin/ld" -shared "--" HOST_LINKER_FLAGS "-dynamic-linker" HOST_DYN_LIB "-o" "a.out" HOST_LIB_PATH HOST_STAT_LIB %t.o --dry-run 2>&1 | FileCheck -check-prefix=CHK-SHARED %s
// CHK-SHARED: "{{.*}}clang"{{.*}} -fPIC

// RUN: rm %T/linker_wrapper_dump || true
// RUN: clang-linker-wrapper -sycl-dump-device-code=%T/linker_wrapper_dump -sycl-device-libraries=%t.devicelib.o "--host-triple=x86_64-unknown-linux-gnu" "--triple=spir64" "--linker-path=/usr/bin/ld" -shared "--" HOST_LINKER_FLAGS "-dynamic-linker" HOST_DYN_LIB "-o" "a.out" HOST_LIB_PATH HOST_STAT_LIB %t.o --dry-run
// RUN: ls %T/linker_wrapper_dump | FileCheck -check-prefix=CHK-SYCL-DUMP-DEVICE %s
// CHK-SYCL-DUMP-DEVICE: {{.*}}.spv

/// Check for list of commands for standalone clang-linker-wrapper run for sycl (AOT for Intel GPU)
// -------
// Generate .o file as linker wrapper input.
47 changes: 17 additions & 30 deletions clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
Original file line number Diff line number Diff line change
@@ -151,7 +151,7 @@ static std::optional<llvm::module_split::IRSplitMode> SYCLModuleSplitMode;

static bool UseSYCLPostLinkTool;

static SmallString<128> SPIRVDumpDir;
static SmallString<128> OffloadImageDumpDir;

using OffloadingImage = OffloadBinary::OffloadingImage;

@@ -923,29 +923,6 @@ static Expected<StringRef> runLLVMToSPIRVTranslation(StringRef File,
if (Error Err = executeCommands(*LLVMToSPIRVPath, CmdArgs))
return std::move(Err);

if (!SPIRVDumpDir.empty()) {
std::error_code EC =
llvm::sys::fs::create_directory(SPIRVDumpDir, /*IgnoreExisting*/ true);
if (EC)
return createStringError(
EC,
formatv("failed to create dump directory. path: {0}, error_code: {1}",
SPIRVDumpDir, EC.value()));

StringRef Sep = llvm::sys::path::get_separator();
StringRef Path = *TempFileOrErr;
StringRef Filename = Path.rsplit(Sep).second;
SmallString<128> CopyPath = SPIRVDumpDir;
CopyPath.append(Filename);
EC = llvm::sys::fs::copy_file(Path, CopyPath);
if (EC)
return createStringError(
EC,
formatv(
"failed to copy file. original: {0}, copy: {1}, error_code: {2}",
Path, CopyPath, EC.value()));
}

return *TempFileOrErr;
}

@@ -1103,6 +1080,18 @@ wrapSYCLBinariesFromFile(std::vector<module_split::SplitModule> &SplitModules,
RegularTarget = "spir64";

for (auto &SI : SplitModules) {
if (!OffloadImageDumpDir.empty()) {
StringRef CopyFrom = SI.ModuleFilePath;
SmallString<128> CopyTo = OffloadImageDumpDir;
StringRef Filename = sys::path::filename(CopyFrom);
CopyTo.append(Filename);
std::error_code EC = sys::fs::copy_file(CopyFrom, CopyTo);
if (EC)
return createStringError(EC, formatv("failed to copy file. From: "
"{0} to: {1}, error_code: {2}",
CopyFrom, CopyTo, EC.value()));
}

auto MBOrDesc = MemoryBuffer::getFile(SI.ModuleFilePath);
if (!MBOrDesc)
return createFileError(SI.ModuleFilePath, MBOrDesc.getError());
@@ -2618,13 +2607,11 @@ int main(int Argc, char **Argv) {

if (Args.hasArg(OPT_sycl_dump_device_code_EQ)) {
Arg *A = Args.getLastArg(OPT_sycl_dump_device_code_EQ);
SmallString<128> Dir(A->getValue());
if (Dir.empty())
llvm::sys::path::native(Dir = "./");
OffloadImageDumpDir = A->getValue();
if (OffloadImageDumpDir.empty())
sys::path::native(OffloadImageDumpDir = "./");
else
Dir.append(llvm::sys::path::get_separator());

SPIRVDumpDir = Dir;
OffloadImageDumpDir.append(sys::path::get_separator());
}

{
2 changes: 1 addition & 1 deletion clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td
Original file line number Diff line number Diff line change
@@ -242,7 +242,7 @@ Flags<[WrapperOnlyOption]>, HelpText<"Embed LLVM IR for runtime kernel fusion">

def sycl_dump_device_code_EQ : Joined<["--", "-"], "sycl-dump-device-code=">,
Flags<[WrapperOnlyOption]>,
HelpText<"Path to the folder where the tool dumps SPIR-V device code. Other formats aren't dumped.">;
HelpText<"Directory to dump offloading images to.">;
Copy link
Contributor

Choose a reason for hiding this comment

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

This option mirrors the user facing option 'sycl-dump-device-code' which was introduced for the old offloading model here: #11132. In that PR, I see that we use this only for dumping SPIR-V code. If the idea is to extend this option to cover all possible device outputs, may be we should do this for the old offloading model as well?

Thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure if there is any interest in supporting additional formats for "old offloading model".
The old offloading model is supposed to be replaced with the new offloading model soon. Right?

FYI: non-SPIR-V targets seems to be happy with -fsycl-device-only output. See #17390.

If we have customers for this use case, please, file a feature request to handle it separately.


// Options to enable/disable device dynamic linking.
def sycl_allow_device_image_dependencies : Flag<["--", "-"], "sycl-allow-device-image-dependencies">,