Skip to content

Commit 16e06ff

Browse files
[Driver] Save PTX files for SYCL kernels in the user input directory. (#12422)
Save `PTX` files generated (for `SYCL` kernels) during PTX target processing for CUDA backend using the `-fsycl-dump-device-code` option. Example usage: `clang++ -fsycl -fsycl-targets=nvptx64-nvidia-cuda -fsycl-dmp-device-code=/path/to/ptx syclfile.cpp` The `PTX` files (`.s` files for each kernel) will be saved under `/path/to/ptx`
1 parent db07a26 commit 16e06ff

File tree

3 files changed

+73
-5
lines changed

3 files changed

+73
-5
lines changed

clang/lib/Driver/Compilation.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,17 +185,22 @@ bool Compilation::CleanupFile(const char *File, bool IssueErrors) const {
185185
// able to remove), or non-regular files. Underlying tools may have
186186
// intentionally not overwritten them.
187187

188-
// Save the device code files(spv files) only if -fsycl-dump-device-code
189-
// option is enabled.
188+
// Save the device code files if -fsycl-dump-device-code option is enabled.
190189
if (TheDriver.isDumpDeviceCodeEnabled()) {
191190
Arg *DumpDeviceCodeArg =
192191
getArgs().getLastArg(options::OPT_fsycl_dump_device_code_EQ);
193192
std::string ExpectedDir =
194193
DumpDeviceCodeArg ? DumpDeviceCodeArg->getValue() : "";
195194
std::string ActualFile(File);
196-
if (ActualFile.find(ExpectedDir) != std::string::npos &&
197-
llvm::sys::path::extension(ActualFile).equals(".spv"))
198-
return false;
195+
196+
if (ActualFile.find(ExpectedDir) != std::string::npos) {
197+
// Save PTX files generated by LLVM NVPTX Back-End,
198+
// when the nvptx*-nvidia-cuda is passed to -fsycl-targets.
199+
if (DefaultToolChain.getTriple().isNVPTX())
200+
return false;
201+
if (llvm::sys::path::extension(ActualFile).equals(".spv"))
202+
return false;
203+
}
199204
}
200205

201206
if (!llvm::sys::fs::can_write(File) || !llvm::sys::fs::is_regular_file(File))

clang/lib/Driver/ToolChains/SYCL.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,26 @@ void SYCL::constructLLVMForeachCommand(Compilation &C, const JobAction &JA,
111111
C.getArgs().MakeArgString("--out-dir=" + OutputDirName));
112112
}
113113

114+
// If fsycl-dump-device-code is passed, put the PTX files
115+
// into the path provided in fsycl-dump-device-code.
116+
if (T->getToolChain().getTriple().isNVPTX() &&
117+
C.getDriver().isDumpDeviceCodeEnabled() && Ext.equals("s")) {
118+
SmallString<128> OutputDir;
119+
120+
Arg *DumpDeviceCodeArg =
121+
C.getArgs().getLastArg(options::OPT_fsycl_dump_device_code_EQ);
122+
123+
OutputDir = (DumpDeviceCodeArg ? DumpDeviceCodeArg->getValue() : "");
124+
125+
// If the output directory path is empty, put the PTX files in the
126+
// current directory.
127+
if (OutputDir.empty())
128+
llvm::sys::path::native(OutputDir = "./");
129+
else
130+
OutputDir.append(llvm::sys::path::get_separator());
131+
ForeachArgs.push_back(C.getArgs().MakeArgString("--out-dir=" + OutputDir));
132+
}
133+
114134
ForeachArgs.push_back(C.getArgs().MakeArgString("--"));
115135
ForeachArgs.push_back(
116136
C.getArgs().MakeArgString(InputCommand->getExecutable()));

clang/test/Driver/save-ptx-files.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Save PTX files during PTX target processing using -fsycl-dump-device-code option.
2+
3+
// Verify that -fsycl-dump-device-code saves PTX files in the user provided directory
4+
// while targeting CUDA enabled GPUs.
5+
6+
// Linux
7+
// RUN: %clang -### -fsycl -fsycl-targets=nvptx64-nvidia-cuda,spir64-unknown-unknown -target x86_64-unknown-linux-gnu --cuda-path=%S/Inputs/CUDA/usr/local/cuda -fsycl-dump-device-code=/user/input/path %s 2>&1 \
8+
// RUN: | FileCheck %s --check-prefixes=CHECK-PTX-FILES,CHECK-SPIRV-FILES
9+
10+
// clang --driver-mode=g++
11+
// RUN: %clangxx -### -fsycl -fsycl-targets=nvptx64-nvidia-cuda -target x86_64-unknown-linux-gnu --cuda-path=%S/Inputs/CUDA/usr/local/cuda -fsycl-dump-device-code=/user/input/path %s 2>&1 \
12+
// RUN: | FileCheck %s --check-prefixes=CHECK-PTX-FILES
13+
14+
// RUN: %clang -### -fsycl -fsycl-targets=nvptx64-nvidia-cuda,spir64-unknown-unknown -target x86_64-unknown-linux-gnu --cuda-path=%S/Inputs/CUDA/usr/local/cuda -fsycl-dump-device-code= %s 2>&1 \
15+
// RUN: | FileCheck %s --check-prefixes=CHECK-PTX-FILES-CWD,CHECK-SPIRV-FILES-CWD
16+
17+
// CHECK-PTX-FILES: llvm-foreach{{.*}} "--out-ext=s"{{.*}} "--out-dir=/user/input/path{{(/|\\\\)}}" "--" "{{.*}}clang{{.*}}" {{.*}} "-fsycl-is-device" {{.*}}.s{{.*}}
18+
// CHECK-SPIRV-FILES: llvm-foreach{{.*}} "--out-dir=/user/input/path{{(/|\\\\)}}" "--" "{{.*}}llvm-spirv"
19+
// CHECK-PTX-FILES-CWD: llvm-foreach{{.*}} "--out-ext=s"{{.*}} "--out-dir=.{{(/|\\\\)}}" "--" "{{.*}}clang{{.*}}" {{.*}} "-fsycl-is-device"
20+
// CHECK-SPIRV-FILES-CWD: llvm-foreach{{.*}} "--out-dir=.{{(/|\\\\)}}" "--" "{{.*}}llvm-spirv"
21+
22+
// Windows - Check if PTX files are saved in the user provided path.
23+
// RUN: %clang_cl -### -fsycl \
24+
// RUN: -fsycl-targets=nvptx64-nvidia-cuda --cuda-path=%S/Inputs/CUDA/usr/local/cuda \
25+
// RUN: -fsycl-dump-device-code=/user/input/path %s 2>&1 \
26+
// RUN: | FileCheck -check-prefix=CHECK-PTX-WIN %s
27+
28+
// Windows - Check if PTX and SPV files are saved in user provided path.
29+
// RUN: %clang_cl -### -fsycl \
30+
// RUN: -fsycl-targets=nvptx64-nvidia-cuda,spir64-unknown-unknown --cuda-path=%S/Inputs/CUDA/usr/local/cuda \
31+
// RUN: -fsycl-dump-device-code=/user/input/path %s 2>&1 \
32+
// RUN: | FileCheck -check-prefixes=CHECK-PTX-WIN,CHECK-SPV-WIN %s
33+
34+
// Windows - Check PTX files saved in current working directory when -fsycl-dump-device-code
35+
// is empty.
36+
// RUN: %clang_cl -### -fsycl \
37+
// RUN: -fsycl-targets=nvptx64-nvidia-cuda --cuda-path=%S/Inputs/CUDA/usr/local/cuda \
38+
// RUN: -fsycl-dump-device-code= %s 2>&1 \
39+
// RUN: | FileCheck -check-prefix=CHECK-PTX-WIN-CWD %s
40+
41+
// CHECK-PTX-WIN: llvm-foreach{{.*}} "--out-ext=s"{{.*}} "--out-dir=/user/input/path{{(/|\\\\)}}" "--" "{{.*}}clang{{.*}}" {{.*}} "-fsycl-is-device" {{.*}}.asm{{.*}}
42+
// CHECK-PTX-WIN-CWD: llvm-foreach{{.*}} "--out-ext=s"{{.*}} "--out-dir=.{{(/|\\\\)}}" "--" "{{.*}}clang{{.*}}" {{.*}} "-fsycl-is-device" {{.*}}.asm{{.*}}
43+
// CHECK-SPV-WIN: llvm-foreach{{.*}} "--out-dir=/user/input/path{{(/|\\\\)}}" "--" "{{.*}}llvm-spirv"

0 commit comments

Comments
 (0)