Skip to content

Commit 232c95c

Browse files
authored
[SYCL][NewOffloadingModel] Add sycl-dump-device-code command line option (#14827)
This option allows to save generated SPIRV files in the specified directory.
1 parent 6127715 commit 232c95c

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-0
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11212,6 +11212,14 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
1121211212
CmdArgs.push_back(Args.MakeArgString(
1121311213
Twine("-sycl-device-library-location=") + DeviceLibDir));
1121411214

11215+
if (C.getDriver().isDumpDeviceCodeEnabled()) {
11216+
SmallString<128> DumpDir;
11217+
Arg *A = C.getArgs().getLastArg(options::OPT_fsycl_dump_device_code_EQ);
11218+
DumpDir = A ? A->getValue() : "";
11219+
CmdArgs.push_back(
11220+
Args.MakeArgString(Twine("-sycl-dump-device-code=") + DumpDir));
11221+
}
11222+
1121511223
auto appendOption = [](SmallString<128> &OptString, StringRef AddOpt) {
1121611224
if (!OptString.empty())
1121711225
OptString += " ";

clang/test/Driver/linker-wrapper-sycl.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
// 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
4141
// CHK-SHARED: "{{.*}}llc"{{.*}} -relocation-model=pic
4242

43+
// RUN: rm %T/linker_wrapper_dump || true
44+
// 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
45+
// RUN: ls %T/linker_wrapper_dump | FileCheck -check-prefix=CHK-SYCL-DUMP-DEVICE %s
46+
// CHK-SYCL-DUMP-DEVICE: {{.*}}.spv
47+
4348
/// Check for list of commands for standalone clang-linker-wrapper run for sycl (AOT for Intel GPU)
4449
// -------
4550
// Generate .o file as linker wrapper input.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// SYCL offloading tests using -fsycl-dump-device-code
2+
3+
// Verify that -fsycl-dump-device-code passes the option to
4+
// clang-linker-wrapper in the new offload model.
5+
6+
// clang -fsycl --offload-new-driver -target x86_64-unknown-linux-gnu
7+
// RUN: %clang -fsycl --offload-new-driver -fno-sycl-instrument-device-code -fno-sycl-device-lib=all -target x86_64-unknown-linux-gnu -fsycl-dump-device-code=/user/input/path %s -### 2>&1 \
8+
// RUN: | FileCheck %s --check-prefixes=CHK-FSYCL-DUMP-DEVICE-CODE-NEW-OFFLOAD
9+
10+
// clang -fsycl --offload-new-driver -fsycl-targets=spir64-unknown-unknown
11+
// RUN: %clang -fsycl --offload-new-driver -fsycl-targets=spir64-unknown-unknown -target x86_64-unknown-linux-gnu -fsycl-dump-device-code=/user/input/path %s -### 2>&1 \
12+
// RUN: | FileCheck %s --check-prefixes=CHK-FSYCL-DUMP-DEVICE-CODE-NEW-OFFLOAD
13+
14+
// clang --driver-mode=g++
15+
// RUN: %clangxx -fsycl --offload-new-driver -fsycl-targets=spir64-unknown-unknown -target x86_64-unknown-linux-gnu -fsycl-dump-device-code=/user/input/path %s -### 2>&1 \
16+
// RUN: | FileCheck %s --check-prefixes=CHK-FSYCL-DUMP-DEVICE-CODE-NEW-OFFLOAD
17+
18+
// Windows
19+
// RUN: %clang_cl -fsycl --offload-new-driver -fsycl-dump-device-code=/user/input/path %s -### 2>&1 \
20+
// RUN: | FileCheck %s --check-prefixes=CHK-FSYCL-DUMP-DEVICE-CODE-WIN-NEW-OFFLOAD
21+
22+
// CHK-FSYCL-DUMP-DEVICE-CODE-NEW-OFFLOAD: clang-linker-wrapper{{.*}} "-sycl-dump-device-code=/user/input/path"
23+
// CHK-FSYCL-DUMP-DEVICE-CODE-WIN-NEW-OFFLOAD: clang-linker-wrapper{{.*}} "-sycl-dump-device-code=/user/input/path"
24+
25+
// Linux
26+
// RUN: %clang -fsycl --offload-new-driver -fsycl-targets=spir64-unknown-unknown -target x86_64-unknown-linux-gnu -fsycl-dump-device-code= %s -### 2>&1 \
27+
// RUN: | FileCheck %s --check-prefixes=CHK-FSYCL-DUMP-DEVICE-CODE-CWD-NEW-OFFLOAD
28+
29+
// Windows
30+
// RUN: %clang_cl -fsycl --offload-new-driver -fsycl-dump-device-code= %s -### 2>&1 \
31+
// RUN: | FileCheck %s --check-prefixes=CHK-FSYCL-DUMP-DEVICE-CODE-WIN-CWD-NEW-OFFLOAD
32+
33+
// CHK-FSYCL-DUMP-DEVICE-CODE-CWD-NEW-OFFLOAD: clang-linker-wrapper{{.*}} "-sycl-dump-device-code="
34+
// CHK-FSYCL-DUMP-DEVICE-CODE-WIN-CWD-NEW-OFFLOAD: clang-linker-wrapper{{.*}} "-sycl-dump-device-code="

clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ static std::atomic<bool> LTOError;
103103

104104
static std::optional<llvm::module_split::IRSplitMode> SYCLModuleSplitMode;
105105

106+
SmallString<128> SPIRVDumpDir;
107+
106108
using OffloadingImage = OffloadBinary::OffloadingImage;
107109

108110
namespace llvm {
@@ -861,6 +863,30 @@ static Expected<StringRef> runLLVMToSPIRVTranslation(StringRef File,
861863
CmdArgs.push_back(File);
862864
if (Error Err = executeCommands(*LLVMToSPIRVPath, CmdArgs))
863865
return std::move(Err);
866+
867+
if (!SPIRVDumpDir.empty()) {
868+
std::error_code EC =
869+
llvm::sys::fs::create_directory(SPIRVDumpDir, /*IgnoreExisting*/ true);
870+
if (EC)
871+
return createStringError(
872+
EC,
873+
formatv("failed to create dump directory. path: {0}, error_code: {1}",
874+
SPIRVDumpDir, EC.value()));
875+
876+
StringRef Sep = llvm::sys::path::get_separator();
877+
StringRef Path = *TempFileOrErr;
878+
StringRef Filename = Path.rsplit(Sep).second;
879+
SmallString<128> CopyPath = SPIRVDumpDir;
880+
CopyPath.append(Filename);
881+
EC = llvm::sys::fs::copy_file(Path, CopyPath);
882+
if (EC)
883+
return createStringError(
884+
EC,
885+
formatv(
886+
"failed to copy file. original: {0}, copy: {1}, error_code: {2}",
887+
Path, CopyPath, EC.value()));
888+
}
889+
864890
return *TempFileOrErr;
865891
}
866892

@@ -2662,6 +2688,17 @@ int main(int Argc, char **Argv) {
26622688
StrMode)));
26632689
}
26642690

2691+
if (Args.hasArg(OPT_sycl_dump_device_code_EQ)) {
2692+
Arg *A = Args.getLastArg(OPT_sycl_dump_device_code_EQ);
2693+
SmallString<128> Dir(A->getValue());
2694+
if (Dir.empty())
2695+
llvm::sys::path::native(Dir = "./");
2696+
else
2697+
Dir.append(llvm::sys::path::get_separator());
2698+
2699+
SPIRVDumpDir = Dir;
2700+
}
2701+
26652702
{
26662703
llvm::TimeTraceScope TimeScope("Execute linker wrapper");
26672704

clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,7 @@ Flags<[WrapperOnlyOption]>, HelpText<"Link SYCL device code using thinLTO">;
220220

221221
def sycl_embed_ir : Flag<["--", "-"], "sycl-embed-ir">,
222222
Flags<[WrapperOnlyOption]>, HelpText<"Embed LLVM IR for runtime kernel fusion">;
223+
224+
def sycl_dump_device_code_EQ : Joined<["--", "-"], "sycl-dump-device-code=">,
225+
Flags<[WrapperOnlyOption]>,
226+
HelpText<"Path to the folder where the tool dumps SPIR-V device code. Other formats aren't dumped.">;

0 commit comments

Comments
 (0)