From ab75a1d659632be853a3a0b5edad04675ec76e94 Mon Sep 17 00:00:00 2001 From: Praneet Rathi <prrathi10@gmail.com> Date: Fri, 17 Jan 2025 16:56:10 -0600 Subject: [PATCH] cuda --- Cargo.lock | 1 + hercules_opt/src/pass.rs | 51 +++++++++++++++++++++++----------------- hercules_rt/build.rs | 1 + juno_build/Cargo.toml | 3 ++- 4 files changed, 33 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ea295fdf..67731f60 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -718,6 +718,7 @@ name = "juno_build" version = "0.1.0" dependencies = [ "hercules_ir", + "hercules_rt", "juno_frontend", "with_builtin_macros", ] diff --git a/hercules_opt/src/pass.rs b/hercules_opt/src/pass.rs index a9654b06..a8e33a38 100644 --- a/hercules_opt/src/pass.rs +++ b/hercules_opt/src/pass.rs @@ -974,6 +974,9 @@ impl PassManager { println!("{}", cuda_ir); println!("{}", rust_rt); + let output_archive = format!("{}/lib{}.a", output_dir, module_name); + println!("{}", output_archive); + // Write the LLVM IR into a temporary file. let tmp_dir = TempDir::new().unwrap(); let mut llvm_path = tmp_dir.path().to_path_buf(); @@ -999,32 +1002,36 @@ impl PassManager { .expect("Error running clang. Is it installed?"); assert!(clang_process.wait().unwrap().success()); - // Write the CUDA IR into a temporary file. - let mut cuda_path = tmp_dir.path().to_path_buf(); - cuda_path.push(format!("{}.cu", module_name)); - let mut file = File::create(&cuda_path) - .expect("PANIC: Unable to open output CUDA IR file."); - file.write_all(cuda_ir.as_bytes()) - .expect("PANIC: Unable to write output CUDA IR file contents."); + let mut ar_args = vec!["crus", &output_archive, &llvm_object]; let cuda_object = format!("{}/{}_cuda.o", tmp_dir.path().to_str().unwrap(), module_name); - let mut nvcc_process = Command::new("nvcc") - .arg("-c") - .arg("-O3") - .arg("-o") - .arg(&cuda_object) - .arg(&cuda_path) - .spawn() - .expect("Error running nvcc. Is it installed?"); - assert!(nvcc_process.wait().unwrap().success()); + if cfg!(feature = "cuda") { + // Write the CUDA IR into a temporary file. + let mut cuda_path = tmp_dir.path().to_path_buf(); + cuda_path.push(format!("{}.cu", module_name)); + let mut file = File::create(&cuda_path) + .expect("PANIC: Unable to open output CUDA IR file."); + file.write_all(cuda_ir.as_bytes()) + .expect("PANIC: Unable to write output CUDA IR file contents."); + + let mut nvcc_process = Command::new("nvcc") + .arg("-c") + .arg("-O3") + .arg("-lcudart") + .arg("-L/usr/lib/x86_64-linux-gnu") + .arg("-L/usr/local/cuda/lib64") + .arg("-o") + .arg(&cuda_object) + .arg(&cuda_path) + .spawn() + .expect("Error running nvcc. Is it installed?"); + assert!(nvcc_process.wait().unwrap().success()); + + ar_args.push(&cuda_object); + } - let output_archive = format!("{}/lib{}.a", output_dir, module_name); - println!("{}", output_archive); let mut ar_process = Command::new("ar") - .arg("crus") - .arg(&output_archive) - .arg(&llvm_object) - .arg(&cuda_object) + .args(&ar_args) .spawn() .expect("Error running ar. Is it installed?"); assert!(ar_process.wait().unwrap().success()); diff --git a/hercules_rt/build.rs b/hercules_rt/build.rs index 15b9f639..6db177de 100644 --- a/hercules_rt/build.rs +++ b/hercules_rt/build.rs @@ -18,6 +18,7 @@ fn main() { println!("cargo::rustc-link-search=native={}", out_dir); println!("cargo::rustc-link-search=native=/usr/lib/x86_64-linux-gnu/"); + println!("cargo:rustc-link-search=native=/usr/local/cuda/lib64"); println!("cargo::rustc-link-lib=static=rtdefs"); println!("cargo::rustc-link-lib=cudart"); println!("cargo::rerun-if-changed=src/rtdefs.cu"); diff --git a/juno_build/Cargo.toml b/juno_build/Cargo.toml index 13889171..67b5bd7e 100644 --- a/juno_build/Cargo.toml +++ b/juno_build/Cargo.toml @@ -5,9 +5,10 @@ authors = ["Aaron Councilman <aaronjc4@illinois.edu>"] edition = "2021" [features] -cuda = ["juno_frontend/cuda"] +cuda = ["juno_frontend/cuda", "hercules_rt/cuda"] [dependencies] juno_frontend = { path = "../juno_frontend" } +hercules_rt = { path = "../hercules_rt", optional = true } hercules_ir = { path = "../hercules_ir" } with_builtin_macros = "0.1.0" -- GitLab