Add run c and run cuda commands

This commit is contained in:
imaqtkatt 2024-05-14 11:07:13 -03:00
parent 04233d3c43
commit b0fd7a7f71
9 changed files with 133 additions and 44 deletions

View File

@ -144,12 +144,13 @@ pub fn desugar_book(
if !ctx.info.has_errors() { Ok(ctx.info) } else { Err(ctx.info) }
}
pub fn run_book(
pub fn run_book_with_fn(
mut book: Book,
run_opts: RunOpts,
compile_opts: CompileOpts,
diagnostics_cfg: DiagnosticsConfig,
args: Option<Vec<Term>>,
run_fn: impl Fn(&str) -> Result<Output, String>,
) -> Result<(Term, String, Diagnostics), Diagnostics> {
let CompileResult { core_book, labels, diagnostics } =
compile_book(&mut book, compile_opts.clone(), diagnostics_cfg, args)?;
@ -161,11 +162,7 @@ pub fn run_book(
let out_path = ".out.hvm";
std::fs::write(out_path, core_book.to_string()).map_err(|x| x.to_string())?;
let Output { status, stdout, stderr } = std::process::Command::new("hvm")
.arg("run")
.arg(out_path)
.output()
.map_err(|x| format!("While running hvm: {x}"))?;
let Output { status, stdout, stderr } = run_fn(out_path)?;
let out = String::from_utf8_lossy(&stdout);
let err = String::from_utf8_lossy(&stderr);
@ -185,6 +182,23 @@ pub fn run_book(
Ok((term, stats.to_string(), diags))
}
pub fn run_book(
book: Book,
run_opts: RunOpts,
compile_opts: CompileOpts,
diagnostics_cfg: DiagnosticsConfig,
args: Option<Vec<Term>>,
) -> Result<(Term, String, Diagnostics), Diagnostics> {
let run_fn = |out_path: &str| {
std::process::Command::new("hvm")
.arg("run")
.arg(out_path)
.output()
.map_err(|e| format!("While running hvm: {e}"))
};
run_book_with_fn(book, run_opts, compile_opts, diagnostics_cfg, args, run_fn)
}
pub fn readback_hvm_net(net: &Net, book: &Book, labels: &Labels, linear: bool) -> (Term, Diagnostics) {
let mut diags = Diagnostics::default();
let net = hvmc_to_net(net);

View File

@ -2,7 +2,7 @@ use bend::{
check_book, compile_book, desugar_book,
diagnostics::{Diagnostics, DiagnosticsConfig, Severity},
fun::{Book, Name},
load_file_to_book, run_book, CompileOpts, OptLevel, RunOpts,
load_file_to_book, run_book, run_book_with_fn, CompileOpts, OptLevel, RunOpts,
};
use clap::{Args, CommandFactory, Parser, Subcommand};
use std::path::{Path, PathBuf};
@ -40,7 +40,7 @@ enum Mode {
path: PathBuf,
},
/// Compiles the program to hvmc and prints to stdout.
Compile {
GenHvm {
#[arg(
short = 'O',
value_delimiter = ' ',
@ -56,32 +56,12 @@ enum Mode {
#[arg(help = "Path to the input file")]
path: PathBuf,
},
/// Compiles the program and runs it with the hvm.
Run {
#[arg(short = 'p', help = "Debug and normalization pretty printing")]
pretty: bool,
#[command(flatten)]
run_opts: RunArgs,
#[arg(
short = 'O',
value_delimiter = ' ',
action = clap::ArgAction::Append,
long_help = r#"Enables or disables the given optimizations
float_combinators is enabled by default on strict mode."#,
)]
comp_opts: Vec<OptArgs>,
#[command(flatten)]
warn_opts: CliWarnOpts,
#[arg(help = "Path to the input file")]
path: PathBuf,
#[arg(value_parser = |arg: &str| bend::fun::parser::TermParser::new(arg).parse_term())]
arguments: Option<Vec<bend::fun::Term>>,
},
/// Compiles the program and runs it with the Rust HVM implementation.
Run(RunArgs2),
/// Compiles the program and runs it with the C HVM implementation.
RunC(RunArgs2),
/// Compiles the program and runs it with the Cuda HVM implementation.
RunCu(RunArgs2),
/// Runs the lambda-term level desugaring passes.
Desugar {
#[arg(
@ -104,6 +84,33 @@ enum Mode {
},
}
#[derive(Args, Clone, Debug)]
struct RunArgs2 {
#[arg(short = 'p', help = "Debug and normalization pretty printing")]
pretty: bool,
#[command(flatten)]
run_opts: RunArgs,
#[arg(
short = 'O',
value_delimiter = ' ',
action = clap::ArgAction::Append,
long_help = r#"Enables or disables the given optimizations
float_combinators is enabled by default on strict mode."#,
)]
comp_opts: Vec<OptArgs>,
#[command(flatten)]
warn_opts: CliWarnOpts,
#[arg(help = "Path to the input file")]
path: PathBuf,
#[arg(value_parser = |arg: &str| bend::fun::parser::TermParser::new(arg).parse_term())]
arguments: Option<Vec<bend::fun::Term>>,
}
#[derive(Args, Clone, Debug)]
struct RunArgs {
#[arg(short = 'l', help = "Linear readback (show explicit dups)")]
@ -238,7 +245,7 @@ fn execute_cli_mode(mut cli: Cli) -> Result<(), Diagnostics> {
eprintln!("{}", diagnostics);
}
Mode::Compile { path, comp_opts, warn_opts } => {
Mode::GenHvm { path, comp_opts, warn_opts } => {
let diagnostics_cfg = set_warning_cfg_from_cli(DiagnosticsConfig::default(), warn_opts);
let opts = compile_opts_from_cli(&comp_opts);
@ -265,7 +272,7 @@ fn execute_cli_mode(mut cli: Cli) -> Result<(), Diagnostics> {
}
}
Mode::Run { run_opts, pretty, comp_opts, warn_opts, arguments, path } => {
Mode::Run(RunArgs2 { pretty, run_opts, comp_opts, warn_opts, path, arguments }) => {
let RunArgs { linear, print_stats } = run_opts;
let diagnostics_cfg =
@ -290,6 +297,74 @@ fn execute_cli_mode(mut cli: Cli) -> Result<(), Diagnostics> {
println!("{stats}");
}
}
Mode::RunC(RunArgs2 { pretty, run_opts, comp_opts, warn_opts, path, arguments }) => {
let RunArgs { linear, print_stats } = run_opts;
let diagnostics_cfg =
set_warning_cfg_from_cli(DiagnosticsConfig::new(Severity::Allow, arg_verbose), warn_opts);
let compile_opts = compile_opts_from_cli(&comp_opts);
compile_opts.check_for_strict();
let run_opts = RunOpts { linear_readback: linear, pretty };
let book = load_book(&path)?;
let run_fn = |out_path: &str| {
std::process::Command::new("hvm")
.arg("run-c")
.arg(out_path)
.output()
.map_err(|e| format!("While running hvm: {e}"))
};
let (term, stats, diags) =
run_book_with_fn(book, run_opts, compile_opts, diagnostics_cfg, arguments, run_fn)?;
eprint!("{diags}");
if pretty {
println!("Result:\n{}", term.display_pretty(0));
} else {
println!("Result: {}", term);
}
if print_stats {
println!("{stats}");
}
}
Mode::RunCu(RunArgs2 { pretty, run_opts, comp_opts, warn_opts, path, arguments }) => {
let RunArgs { linear, print_stats } = run_opts;
let diagnostics_cfg =
set_warning_cfg_from_cli(DiagnosticsConfig::new(Severity::Allow, arg_verbose), warn_opts);
let compile_opts = compile_opts_from_cli(&comp_opts);
compile_opts.check_for_strict();
let run_opts = RunOpts { linear_readback: linear, pretty };
let book = load_book(&path)?;
let run_fn = |out_path: &str| {
std::process::Command::new("hvm")
.arg("run-cu")
.arg(out_path)
.output()
.map_err(|e| format!("While running hvm: {e}"))
};
let (term, stats, diags) =
run_book_with_fn(book, run_opts, compile_opts, diagnostics_cfg, arguments, run_fn)?;
eprint!("{diags}");
if pretty {
println!("Result:\n{}", term.display_pretty(0));
} else {
println!("Result: {}", term);
}
if print_stats {
println!("{stats}");
}
}
};
Ok(())
}

View File

@ -1,3 +1,3 @@
compile
gen-hvm
tests/golden_tests/cli/compile_all.hvm
-Oall

View File

@ -1,3 +1,3 @@
compile
gen-hvm
tests/golden_tests/cli/compile_inline.hvm
-Oinline

View File

@ -1,4 +1,4 @@
compile
gen-hvm
tests/golden_tests/cli/compile_no_opts.hvm
-Ono-all
-Ono-eta

View File

@ -1,3 +1,3 @@
compile
gen-hvm
tests/golden_tests/cli/compile_pre_reduce.hvm
-Opre-reduce

View File

@ -1,4 +1,4 @@
compile
gen-hvm
tests/golden_tests/cli/compile_strict_loop.hvm
-Ono-all
-Arecursion-cycle
-Arecursion-cycle

View File

@ -1,3 +1,3 @@
compile
gen-hvm
tests/golden_tests/cli/compile_wrong_opt.hvm
-Ofoo

View File

@ -1,2 +1,2 @@
compile
gen-hvm
tests/golden_tests/cli/warn_and_err.hvm