Skip to content

Commit 2981f3a

Browse files
authored
[Clang] Add timeout for GPU detection utilities (#94751)
Summary: The utilities `nvptx-arch` and `amdgpu-arch` are used to support `--offload-arch=native` among other utilities in clang. However, these rely on the GPU drivers to query the features. In certain cases these drivers can become locked up, which will lead to indefinate hangs on any compiler jobs running in the meantime. This patch adds a ten second timeout period for these utilities before it kills the job and errors out.
1 parent c5fcc2e commit 2981f3a

File tree

4 files changed

+8
-7
lines changed

4 files changed

+8
-7
lines changed

clang/include/clang/Driver/ToolChain.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,8 @@ class ToolChain {
205205

206206
/// Executes the given \p Executable and returns the stdout.
207207
llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>>
208-
executeToolChainProgram(StringRef Executable) const;
208+
executeToolChainProgram(StringRef Executable,
209+
unsigned SecondsToWait = 0) const;
209210

210211
void setTripleEnvironment(llvm::Triple::EnvironmentType Env);
211212

clang/lib/Driver/ToolChain.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ ToolChain::ToolChain(const Driver &D, const llvm::Triple &T,
104104
}
105105

106106
llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>>
107-
ToolChain::executeToolChainProgram(StringRef Executable) const {
107+
ToolChain::executeToolChainProgram(StringRef Executable,
108+
unsigned SecondsToWait) const {
108109
llvm::SmallString<64> OutputFile;
109110
llvm::sys::fs::createTemporaryFile("toolchain-program", "txt", OutputFile);
110111
llvm::FileRemover OutputRemover(OutputFile.c_str());
@@ -115,9 +116,8 @@ ToolChain::executeToolChainProgram(StringRef Executable) const {
115116
};
116117

117118
std::string ErrorMessage;
118-
if (llvm::sys::ExecuteAndWait(Executable, {}, {}, Redirects,
119-
/* SecondsToWait */ 0,
120-
/*MemoryLimit*/ 0, &ErrorMessage))
119+
if (llvm::sys::ExecuteAndWait(Executable, {}, {}, Redirects, SecondsToWait,
120+
/*MemoryLimit=*/0, &ErrorMessage))
121121
return llvm::createStringError(std::error_code(),
122122
Executable + ": " + ErrorMessage);
123123

clang/lib/Driver/ToolChains/AMDGPU.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ AMDGPUToolChain::getSystemGPUArchs(const ArgList &Args) const {
877877
else
878878
Program = GetProgramPath("amdgpu-arch");
879879

880-
auto StdoutOrErr = executeToolChainProgram(Program);
880+
auto StdoutOrErr = executeToolChainProgram(Program, /*SecondsToWait=*/10);
881881
if (!StdoutOrErr)
882882
return StdoutOrErr.takeError();
883883

clang/lib/Driver/ToolChains/Cuda.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ NVPTXToolChain::getSystemGPUArchs(const ArgList &Args) const {
826826
else
827827
Program = GetProgramPath("nvptx-arch");
828828

829-
auto StdoutOrErr = executeToolChainProgram(Program);
829+
auto StdoutOrErr = executeToolChainProgram(Program, /*SecondsToWait=*/10);
830830
if (!StdoutOrErr)
831831
return StdoutOrErr.takeError();
832832

0 commit comments

Comments
 (0)