Add fake ptxas binary

This commit is contained in:
Andrzej Janik 2025-09-02 16:51:09 +00:00
commit 458e670f38
5 changed files with 107 additions and 9 deletions

11
Cargo.lock generated
View file

@ -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"

View file

@ -16,6 +16,7 @@ members = [
"ptx_parser",
"ptx_parser_macros",
"ptx_parser_macros_impl",
"ptxas",
"xtask",
"zluda",
"zluda_bindgen",

View file

@ -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<u8>,
}
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();

18
ptxas/Cargo.toml Normal file
View file

@ -0,0 +1,18 @@
[package]
name = "ptxas"
version = "0.0.0"
authors = ["Andrzej Janik <vosen@vosen.pl>"]
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]

65
ptxas/src/main.rs Normal file
View file

@ -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<String> {
any("", move |s: String| {
Some(s.strip_prefix("-arch=")?.to_owned())
})
.metavar(&[("-arch=", Style::Literal), ("ARG", Style::Metavar)])
.anywhere()
.fallback_with(|| Ok::<String, &'static str>("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)
}