|
14 | 14 | #include <dirent.h> |
15 | 15 | #include <nvrtc.h> |
16 | 16 | #include <stdarg.h> |
| 17 | +#include <stdio.h> |
17 | 18 | #include <string.h> |
18 | 19 | #include <sys/stat.h> |
19 | 20 | #include <sys/types.h> |
|
44 | 45 | // Call system command and capture stdout + stderr |
45 | 46 | //------------------------------------------------------------------------------ |
46 | 47 | 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); |
48 | 49 | FILE *output_stream = popen((command + std::string(" 2>&1")).c_str(), "r"); |
49 | 50 |
|
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); |
51 | 52 |
|
52 | | - char output[4 * CEED_MAX_RESOURCE_LEN]; |
| 53 | + char line[CEED_MAX_RESOURCE_LEN] = ""; |
| 54 | + std::string output = ""; |
53 | 55 |
|
54 | | - while (fgets(output, sizeof(output), output_stream) != nullptr) { |
| 56 | + while (fgets(line, sizeof(line), output_stream) != nullptr) { |
| 57 | + output += line; |
55 | 58 | } |
56 | | - CeedDebug(ceed, "Command output:\n%s\n", output); |
| 59 | + CeedDebug(ceed, "Command output:\n%s\n", output.c_str()); |
57 | 60 |
|
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()); |
59 | 62 | return CEED_ERROR_SUCCESS; |
60 | 63 | } |
61 | 64 |
|
@@ -280,10 +283,45 @@ static int CeedCompileCore_Cuda(Ceed ceed, const char *source, const bool throw_ |
280 | 283 | CeedCallSystem(ceed, command.c_str(), "build Rust crate"); |
281 | 284 | } |
282 | 285 |
|
| 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 | + |
283 | 321 | // 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 "; |
287 | 325 | command += opts[4]; |
288 | 326 | CeedCallSystem(ceed, command.c_str(), "JiT kernel source"); |
289 | 327 | CeedCallSystem(ceed, ("chmod 0777 temp/kernel_" + std::to_string(build_id) + "_1_wrapped.ll").c_str(), "update JiT file permissions"); |
|
0 commit comments