Skip to content

Commit

Permalink
ptx: start nom parser (parser2) and pest ast walker
Browse files Browse the repository at this point in the history
  • Loading branch information
romnn committed Apr 13, 2024
1 parent 4b0794c commit 73cbb99
Show file tree
Hide file tree
Showing 11 changed files with 1,134 additions and 632 deletions.
1 change: 1 addition & 0 deletions ptx/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ benchmarks/
cuda-samples-12.4/
cuda-samples-12.4.tar.gz

callgrind.out
bison/bindings.rs

bison/src/ptx.lex.h
Expand Down
3 changes: 1 addition & 2 deletions ptx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ color-eyre = "0"
thiserror = "1"
pest = "2"
pest_derive = "2"
pest-ast = "0"
from-pest = "0"
nom = "7"
num = "0"
itertools = "0"

Expand Down
9 changes: 7 additions & 2 deletions ptx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,10 @@ The provided libraries may in the future be used for
docker run -v "$PWD/kernels/:/out" ptxsamples
```

i = 194
[ptx/src/parser.rs:1909:13] &kernel.path() = "/Users/roman/dev/box/ptx/kernels/cuda_12_3_r123compiler33567101_0_sm50_newdelete.1.sm_50.ptx"
```bash
brew tap LouisBrunner/valgrind
brew install --HEAD LouisBrunner/valgrind/valgrind
brew install qcachegrind

valgrind --tool=callgrind --callgrind-out-file="callgrind.out" target/release/ptx parse-ptx ./kernels/vectoradd.sm_52.ptx
```
61 changes: 46 additions & 15 deletions ptx/bison/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ fn enable_diagnostics_color(build: &mut cc::Build) {
}
}


fn configure_debug_mode(build: &mut cc::Build) {
if is_debug() {
build.opt_level(0).debug(true).flag("-ggdb3");
Expand Down Expand Up @@ -98,12 +97,22 @@ fn generate_bindings() -> eyre::Result<()> {
Ok(())
}


fn build_ptx_parser() -> eyre::Result<()> {
let out_dir = output_path().join("generated");
std::fs::create_dir(&out_dir).ok();

let lex_input_files = [(PathBuf::from("./src/ptx.l"), out_dir.join("ptx.lex.h"), out_dir.join("ptx.lex.c")), (PathBuf::from("./src/ptxinfo.l"), out_dir.join("ptxinfo.lex.h"), out_dir.join("ptxinfo.lex.c"))];
let lex_input_files = [
(
PathBuf::from("./src/ptx.l"),
out_dir.join("ptx.lex.h"),
out_dir.join("ptx.lex.c"),
),
(
PathBuf::from("./src/ptxinfo.l"),
out_dir.join("ptxinfo.lex.h"),
out_dir.join("ptxinfo.lex.c"),
),
];

for (lex_input_file, lex_output_header, lex_output_file) in &lex_input_files {
assert!(lex_input_file.is_file());
Expand All @@ -128,7 +137,18 @@ fn build_ptx_parser() -> eyre::Result<()> {
}
}

let bison_input_files = [(PathBuf::from("./src/ptx.y"), out_dir.join("ptx.parser"), "ptx_"), (PathBuf::from("./src/ptxinfo.y"), out_dir.join("ptxinfo.parser"), "ptxinfo_")];
let bison_input_files = [
(
PathBuf::from("./src/ptx.y"),
out_dir.join("ptx.parser"),
"ptx_",
),
(
PathBuf::from("./src/ptxinfo.y"),
out_dir.join("ptxinfo.parser"),
"ptxinfo_",
),
];

for (bison_input_file, bison_output_file, prefix) in &bison_input_files {
let args = [
Expand Down Expand Up @@ -158,13 +178,22 @@ fn build_ptx_parser() -> eyre::Result<()> {
let source_dir = PathBuf::from("./src/");
// let generated_ptx_lexer = out_dir.join("ptx.lex.c");
// let generated_ptx_parser = out_dir.join("ptx.parser.tab.c");
let generated_files: Vec<_> = lex_input_files.iter()
.map(|(_, _, generated)| generated).cloned()
.chain(bison_input_files.iter()
.map(|(_, generated, _)| generated)
.map(|p| p.with_file_name(
format!("{}.tab.c", p.file_name().unwrap_or_default().to_string_lossy())
))).collect();
let generated_files: Vec<_> = lex_input_files
.iter()
.map(|(_, _, generated)| generated)
.cloned()
.chain(
bison_input_files
.iter()
.map(|(_, generated, _)| generated)
.map(|p| {
p.with_file_name(format!(
"{}.tab.c",
p.file_name().unwrap_or_default().to_string_lossy()
))
}),
)
.collect();

dbg!(&generated_files);
// vec![
Expand Down Expand Up @@ -199,10 +228,12 @@ fn build_ptx_parser() -> eyre::Result<()> {

if std::env::var("DUMP").unwrap_or_default().as_str() == "yes" {
// move to source dir
for (generated_path, file_name) in generated_files.iter().filter_map(|p| match p.file_name() {
Some(file_name) => Some((p, file_name)),
None => None
}) {
for (generated_path, file_name) in
generated_files.iter().filter_map(|p| match p.file_name() {
Some(file_name) => Some((p, file_name)),
None => None,
})
{
let src = generated_path.with_extension("h");
let dest = source_dir.join(file_name).with_extension("h");
println!("cargo:warning=copy {} to {}", src.display(), dest.display());
Expand Down
16 changes: 10 additions & 6 deletions ptx/bison/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use color_eyre::eyre;
use clap::Parser;
use std::path::PathBuf;
use color_eyre::eyre;
use std::ffi::CString;
use std::path::PathBuf;
use std::time::Instant;

#[derive(Parser, Debug, Clone)]
Expand All @@ -25,18 +25,22 @@ fn main() -> eyre::Result<()> {
let options = Options::parse();

match options.command {
Command::ParsePTX(ParsePTXOptions {ptx_path}) => {
Command::ParsePTX(ParsePTXOptions { ptx_path }) => {
let code_size_bytes = std::fs::metadata(&ptx_path)?.len();
let path = CString::new(ptx_path.to_string_lossy().as_bytes())?;
let start = Instant::now();
unsafe { ptxbison::bindings::load_ptx_from_filename(path.as_c_str().as_ptr()) };
let dur = start.elapsed();
let dur_millis = dur.as_millis();
let dur_secs = dur.as_secs_f64();
let code_size_mib = code_size_bytes as f64 / (1024.0*1024.0);
let code_size_mib = code_size_bytes as f64 / (1024.0 * 1024.0);
let mib_per_sec = code_size_mib / dur_secs;
println!("parsing {} took {} ms ({:3.3} MiB/s)", ptx_path.display(), dur_millis, mib_per_sec);

println!(
"parsing {} took {} ms ({:3.3} MiB/s)",
ptx_path.display(),
dur_millis,
mib_per_sec
);
}
}

Expand Down
Loading

0 comments on commit 73cbb99

Please sign in to comment.