From 458e670f382d5cef4948512a4b9b2a997856c420 Mon Sep 17 00:00:00 2001 From: Andrzej Janik Date: Tue, 2 Sep 2025 16:51:09 +0000 Subject: [PATCH] Add fake ptxas binary --- Cargo.lock | 11 ++++++++ Cargo.toml | 1 + compiler/src/main.rs | 21 ++++++++------ ptxas/Cargo.toml | 18 ++++++++++++ ptxas/src/main.rs | 65 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 107 insertions(+), 9 deletions(-) create mode 100644 ptxas/Cargo.toml create mode 100644 ptxas/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 26efea5..398db3b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2649,6 +2649,17 @@ dependencies = [ "syn 2.0.89", ] +[[package]] +name = "ptxas" +version = "0.0.0" +dependencies = [ + "bpaf", + "comgr", + "hip_runtime-sys", + "ptx", + "ptx_parser", +] + [[package]] name = "quick-error" version = "1.2.3" diff --git a/Cargo.toml b/Cargo.toml index 9415c8f..d6fa904 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ members = [ "ptx_parser", "ptx_parser_macros", "ptx_parser_macros_impl", + "ptxas", "xtask", "zluda", "zluda_bindgen", diff --git a/compiler/src/main.rs b/compiler/src/main.rs index aa5007d..fb8feb0 100644 --- a/compiler/src/main.rs +++ b/compiler/src/main.rs @@ -10,6 +10,7 @@ use bpaf::Bpaf; mod error; use error::CompilerError; +use hip_runtime_sys::{hipDeviceProp_tR0600, hipGetDevicePropertiesR0600, hipInit}; const DEFAULT_ARCH: &'static str = "gfx1100"; @@ -58,9 +59,14 @@ fn main_core() -> Result<(), CompilerError> { let arch: String = match opts.arch { Some(s) => s, - None => get_gpu_arch() - .map(String::from) - .unwrap_or(DEFAULT_ARCH.to_owned()), + None => { + unsafe { hipInit(0) }?; + let mut dev_props: hipDeviceProp_tR0600 = unsafe { mem::zeroed() }; + unsafe { hipGetDevicePropertiesR0600(&mut dev_props, 0) }?; + get_gpu_arch(&mut dev_props) + .map(String::from) + .unwrap_or(DEFAULT_ARCH.to_owned()) + } }; let ptx = fs::read(&ptx_path).map_err(CompilerError::from)?; @@ -78,8 +84,8 @@ fn main_core() -> Result<(), CompilerError> { &comgr, &arch, &llvm.bitcode, - &llvm.attributes_bitcode, &llvm.linked_bitcode, + &llvm.attributes_bitcode, Some(&comgr_hook), ) .map_err(CompilerError::from)?; @@ -116,11 +122,8 @@ struct LLVMArtifacts { llvm_ir: Vec, } -fn get_gpu_arch() -> Result<&'static str, CompilerError> { - use hip_runtime_sys::*; - unsafe { hipInit(0) }?; - let mut dev_props: hipDeviceProp_tR0600 = unsafe { mem::zeroed() }; - unsafe { hipGetDevicePropertiesR0600(&mut dev_props, 0) }?; +fn get_gpu_arch<'a>(dev_props: &'a mut hipDeviceProp_tR0600) -> Result<&'a str, CompilerError> { + unsafe { hipGetDevicePropertiesR0600(dev_props, 0) }?; let gcn_arch_name = &dev_props.gcnArchName; let gcn_arch_name = unsafe { CStr::from_ptr(gcn_arch_name.as_ptr()) }; let gcn_arch_name = gcn_arch_name.to_str(); diff --git a/ptxas/Cargo.toml b/ptxas/Cargo.toml new file mode 100644 index 0000000..b99ac96 --- /dev/null +++ b/ptxas/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "ptxas" +version = "0.0.0" +authors = ["Andrzej Janik "] +edition = "2021" + +[[bin]] +name = "ptxas" +path = "src/main.rs" + +[dependencies] +comgr = { path = "../comgr" } +ptx = { path = "../ptx" } +ptx_parser = { path = "../ptx_parser" } +hip_runtime-sys = { path = "../ext/hip_runtime-sys" } +bpaf = { version = "0.9.19", features = ["derive"] } + +[package.metadata.zluda] diff --git a/ptxas/src/main.rs b/ptxas/src/main.rs new file mode 100644 index 0000000..b2dd278 --- /dev/null +++ b/ptxas/src/main.rs @@ -0,0 +1,65 @@ +use bpaf::{any, doc::Style, Bpaf, Parser}; +use hip_runtime_sys::{hipDeviceProp_tR0600, hipGetDevicePropertiesR0600}; +use std::{ffi::CStr, mem}; + +#[derive(Debug, Clone, Bpaf)] +#[allow(dead_code)] +#[bpaf(options, version("V12.8.0"))] +pub struct Options { + #[bpaf(short, long)] + output: String, + warn_on_spills: bool, + #[bpaf(short, long)] + verbose: bool, + #[bpaf(external)] + gpu_name: String, + #[bpaf(long, short('O'), fallback(3))] + opt_level: usize, + #[bpaf(positional)] + input: String, +} + +// #[bpaf(long, long("gpu_name"), fallback_with(default_arch))] +fn gpu_name() -> impl Parser { + any("", move |s: String| { + Some(s.strip_prefix("-arch=")?.to_owned()) + }) + .metavar(&[("-arch=", Style::Literal), ("ARG", Style::Metavar)]) + .anywhere() + .fallback_with(|| Ok::("sm_52".to_string())) +} + +fn main() { + let options = options().run(); + let comgr = comgr::Comgr::new().unwrap(); + unsafe { hip_runtime_sys::hipInit(0) }.unwrap(); + let mut dev_props: hipDeviceProp_tR0600 = unsafe { mem::zeroed() }; + let (gpu_arch, clock_rate) = get_gpu_arch_and_clock_rate(&mut dev_props); + let input = std::fs::read_to_string(options.input).unwrap(); + let ast = ptx_parser::parse_module_checked(&input).unwrap(); + let llvm = ptx::to_llvm_module( + ast, + ptx::Attributes { + clock_rate: clock_rate as u32, + }, + ) + .unwrap(); + let elf_binary = comgr::compile_bitcode( + &comgr, + gpu_arch, + &*llvm.llvm_ir.write_bitcode_to_memory(), + &*llvm.linked_bitcode(), + &*llvm.attributes_ir.write_bitcode_to_memory(), + None, + ) + .unwrap(); + std::fs::write(options.output, elf_binary).unwrap(); +} + +fn get_gpu_arch_and_clock_rate<'a>(dev_props: &'a mut hipDeviceProp_tR0600) -> (&'a str, i32) { + unsafe { hipGetDevicePropertiesR0600(dev_props, 0) }.unwrap(); + let gcn_arch_name = &dev_props.gcnArchName; + let gcn_arch_name = unsafe { CStr::from_ptr(gcn_arch_name.as_ptr()) }; + let gcn_arch_name = gcn_arch_name.to_str(); + (gcn_arch_name.unwrap(), dev_props.clockRate) +}