Skip to content

Commit 27cdb0b

Browse files
committed
clang - use clang++-version if able
1 parent 158fd43 commit 27cdb0b

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

backends/cuda/ceed-cuda-common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ static const char *cublasGetErrorName(cublasStatus_t error) {
6666

6767
typedef struct {
6868
int device_id;
69+
bool use_llvm_version;
70+
int llvm_version;
6971
cublasHandle_t cublas_handle;
7072
struct cudaDeviceProp device_prop;
7173
} Ceed_Cuda;

backends/cuda/ceed-cuda-compile.cpp

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <dirent.h>
1515
#include <nvrtc.h>
1616
#include <stdarg.h>
17+
#include <stdio.h>
1718
#include <string.h>
1819
#include <sys/stat.h>
1920
#include <sys/types.h>
@@ -44,18 +45,20 @@
4445
// Call system command and capture stdout + stderr
4546
//------------------------------------------------------------------------------
4647
static int CeedCallSystem_Core(Ceed ceed, const char *command, const char *message) {
47-
CeedDebug(ceed, "Running command:\n$ %s\n", command);
48+
CeedDebug(ceed, "Running command:\n$ %s", command);
4849
FILE *output_stream = popen((command + std::string(" 2>&1")).c_str(), "r");
4950

50-
CeedCheck(output_stream != nullptr, ceed, CEED_ERROR_BACKEND, "Failed to %s with command: %s", message, command);
51+
CeedCheck(output_stream != nullptr, ceed, CEED_ERROR_BACKEND, "Failed to %s with command:\n$ %s", message, command);
5152

52-
char output[4 * CEED_MAX_RESOURCE_LEN];
53+
char line[CEED_MAX_RESOURCE_LEN] = "";
54+
std::string output = "";
5355

54-
while (fgets(output, sizeof(output), output_stream) != nullptr) {
56+
while (fgets(line, sizeof(line), output_stream) != nullptr) {
57+
output += line;
5558
}
56-
CeedDebug(ceed, "Command output:\n%s\n", output);
59+
CeedDebug(ceed, "Command output:\n%s\n", output.c_str());
5760

58-
CeedCheck(pclose(output_stream) == 0, ceed, CEED_ERROR_BACKEND, "Failed to %s with command: %s\nand error: %s", message, command, output);
61+
CeedCheck(pclose(output_stream) == 0, ceed, CEED_ERROR_BACKEND, "Failed to %s\ncommand:\n$ %s\nerror:\n%s", message, command, output.c_str());
5962
return CEED_ERROR_SUCCESS;
6063
}
6164

@@ -280,10 +283,45 @@ static int CeedCompileCore_Cuda(Ceed ceed, const char *source, const bool throw_
280283
CeedCallSystem(ceed, command.c_str(), "build Rust crate");
281284
}
282285

286+
// Get Clang version
287+
bool use_llvm_version = ceed_data->use_llvm_version;
288+
int llvm_version = ceed_data->llvm_version;
289+
290+
if (llvm_version == 0) {
291+
command = "$(find $(rustup run " + std::string(rust_toolchain) + " rustc --print sysroot) -name llvm-link) --version 2>&1";
292+
CeedDebug(ceed, "Attempting to detect Rust LLVM version.\ncommand:\n$ %s", command.c_str());
293+
FILE *output_stream = popen(command.c_str(), "r");
294+
295+
CeedCheck(output_stream != nullptr, ceed, CEED_ERROR_BACKEND, "Failed to detect Rust LLVM version");
296+
297+
char line[CEED_MAX_RESOURCE_LEN] = "";
298+
std::string output = "";
299+
300+
while (fgets(line, sizeof(line), output_stream) != nullptr) {
301+
output += line;
302+
}
303+
CeedCheck(pclose(output_stream) == 0, ceed, CEED_ERROR_BACKEND, "Failed to detect Rust LLVM version\ncommand: $ %s\nerror: %s", command.c_str(),
304+
output.c_str());
305+
CeedDebug(ceed, "output:\n%s", output.c_str());
306+
307+
const char *version_substring = strstr(output.c_str(), "LLVM version ");
308+
309+
version_substring += 13;
310+
311+
char *next_dot = strchr((char *)version_substring, '.');
312+
313+
next_dot[0] = '\0';
314+
ceed_data->llvm_version = llvm_version = std::stoi(version_substring);
315+
CeedDebug(ceed, "Rust LLVM version number: %d\n", llvm_version);
316+
317+
output_stream = popen((std::string("clang++-") + std::to_string(llvm_version) + " --version 2>&1").c_str(), "r");
318+
ceed_data->use_llvm_version = use_llvm_version = pclose(output_stream) == 0;
319+
}
320+
283321
// Compile wrapper kernel
284-
command = "clang++ -flto=thin --cuda-gpu-arch=sm_" + std::to_string(prop.major) + std::to_string(prop.minor) +
285-
" --cuda-device-only -emit-llvm -S temp/kernel_" + std::to_string(build_id) + "_0_source.cu -o temp/kernel_" +
286-
std::to_string(build_id) + "_1_wrapped.ll ";
322+
command = "clang++" + (use_llvm_version ? (std::string("-") + std::to_string(llvm_version)) : "") + " -flto=thin --cuda-gpu-arch=sm_" +
323+
std::to_string(prop.major) + std::to_string(prop.minor) + " --cuda-device-only -emit-llvm -S temp/kernel_" + std::to_string(build_id) +
324+
"_0_source.cu -o temp/kernel_" + std::to_string(build_id) + "_1_wrapped.ll ";
287325
command += opts[4];
288326
CeedCallSystem(ceed, command.c_str(), "JiT kernel source");
289327
CeedCallSystem(ceed, ("chmod 0777 temp/kernel_" + std::to_string(build_id) + "_1_wrapped.ll").c_str(), "update JiT file permissions");

0 commit comments

Comments
 (0)