Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable net_size_check by default with different max sizes for c and cuda #649

Merged
merged 7 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/hello_world.bend
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def main():
with IO:
* <- IO/print("Hello, world!")
* <- IO/print("Hello, world!\n")
return wrap(0)
14 changes: 10 additions & 4 deletions src/hvm/check_net_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@ use super::tree_children;
use crate::{diagnostics::Diagnostics, fun::Name};
use hvm::ast::{Book, Net, Tree};

pub const MAX_NET_SIZE: usize = 64;
pub const MAX_NET_SIZE_C: usize = 4095;
pub const MAX_NET_SIZE_CUDA: usize = 64;

pub fn check_net_sizes(book: &Book, diagnostics: &mut Diagnostics, cmd: &str) -> Result<(), Diagnostics> {
KunalSin9h marked this conversation as resolved.
Show resolved Hide resolved
let net_size_bound = match cmd {
"run-cu" | "gen-cu" | "gen-hvm" => MAX_NET_SIZE_CUDA,
_ => MAX_NET_SIZE_C,
};

pub fn check_net_sizes(book: &Book, diagnostics: &mut Diagnostics) -> Result<(), Diagnostics> {
diagnostics.start_pass();

for (name, net) in &book.defs {
let nodes = count_nodes(net);
if nodes > MAX_NET_SIZE {
if nodes > net_size_bound {
diagnostics.add_rule_error(
format!("Definition is too large for hvm (size={nodes}, max size={MAX_NET_SIZE}). Please break it into smaller pieces."),
format!("Definition is too large for hvm (size={nodes}, max size={net_size_bound}). Please break it into smaller pieces."),
KunalSin9h marked this conversation as resolved.
Show resolved Hide resolved
Name::new(name),
);
}
Expand Down
15 changes: 8 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
fun::{book_to_hvm, net_to_term::net_to_term, term_to_net::Labels, Book, Ctx, Term},
hvm::{
add_recursive_priority::add_recursive_priority,
check_net_size::{check_net_sizes, MAX_NET_SIZE},
check_net_size::{check_net_sizes, MAX_NET_SIZE_CUDA},
eta_reduce::eta_reduce_hvm_net,
hvm_book_show_pretty,
inline::inline_hvm_book,
Expand Down Expand Up @@ -35,7 +35,7 @@ pub fn check_book(
compile_opts: CompileOpts,
) -> Result<Diagnostics, Diagnostics> {
// TODO: Do the checks without having to do full compilation
let res = compile_book(book, compile_opts, diagnostics_cfg, None)?;
let res = compile_book(book, compile_opts, diagnostics_cfg, None, None)?;
Ok(res.diagnostics)
}

Expand All @@ -44,6 +44,7 @@ pub fn compile_book(
opts: CompileOpts,
diagnostics_cfg: DiagnosticsConfig,
args: Option<Vec<Term>>,
cmd: Option<&str>,
) -> Result<CompileResult, Diagnostics> {
let mut diagnostics = desugar_book(book, opts.clone(), diagnostics_cfg, args)?;

Expand Down Expand Up @@ -72,8 +73,8 @@ pub fn compile_book(
prune_hvm_book(&mut hvm_book, &prune_entrypoints);
}

if opts.check_net_size {
check_net_sizes(&hvm_book, &mut diagnostics)?;
if opts.check_net_size && cmd.is_some() {
check_net_sizes(&hvm_book, &mut diagnostics, cmd.unwrap())?;
}

add_recursive_priority(&mut hvm_book);
Expand Down Expand Up @@ -143,7 +144,7 @@ pub fn desugar_book(
ctx.check_unbound_vars()?;

if opts.float_combinators {
ctx.book.float_combinators(MAX_NET_SIZE);
ctx.book.float_combinators(MAX_NET_SIZE_CUDA);
}
// sanity check
ctx.check_unbound_refs()?;
Expand Down Expand Up @@ -174,7 +175,7 @@ pub fn run_book(
cmd: &str,
) -> Result<Option<(Term, String, Diagnostics)>, Diagnostics> {
let CompileResult { hvm_book: core_book, labels, diagnostics } =
compile_book(&mut book, compile_opts.clone(), diagnostics_cfg, args)?;
compile_book(&mut book, compile_opts.clone(), diagnostics_cfg, args, Some(cmd))?;

// TODO: Printing should be taken care by the cli module, but we'd
// like to print any warnings before running so that the user can
Expand Down Expand Up @@ -412,7 +413,7 @@ impl Default for CompileOpts {
float_combinators: true,
merge: false,
inline: false,
check_net_size: false,
check_net_size: true,
adt_encoding: AdtEncoding::NumScott,
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ fn execute_cli_mode(mut cli: Cli) -> Result<(), Diagnostics> {
let opts = compile_opts_from_cli(&comp_opts);

let mut book = load_book(&path, diagnostics_cfg)?;
let compile_res = compile_book(&mut book, opts, diagnostics_cfg, None)?;
let compile_res = compile_book(&mut book, opts, diagnostics_cfg, None, Some("gen-hvm"))?;

eprint!("{}", compile_res.diagnostics);
println!("{}", hvm_book_show_pretty(&compile_res.hvm_book));
Expand Down Expand Up @@ -345,7 +345,7 @@ fn execute_cli_mode(mut cli: Cli) -> Result<(), Diagnostics> {
let opts = compile_opts_from_cli(&comp_opts);

let mut book = load_book(&path, diagnostics_cfg)?;
let compile_res = compile_book(&mut book, opts, diagnostics_cfg, None)?;
let compile_res = compile_book(&mut book, opts, diagnostics_cfg, None, Some(gen_cmd))?;

let out_path = ".out.hvm";
std::fs::write(out_path, hvm_book_show_pretty(&compile_res.hvm_book)).map_err(|x| x.to_string())?;
Expand Down
14 changes: 7 additions & 7 deletions tests/golden_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ fn compile_file() {
let compile_opts = CompileOpts::default();
let diagnostics_cfg = DiagnosticsConfig { unused_definition: Severity::Allow, ..Default::default() };

let res = compile_book(&mut book, compile_opts, diagnostics_cfg, None)?;
let res = compile_book(&mut book, compile_opts, diagnostics_cfg, None, None)?;
Ok(format!("{}{}", res.diagnostics, hvm_book_show_pretty(&res.hvm_book)))
})
}
Expand All @@ -115,7 +115,7 @@ fn compile_file_o_all() {
..Default::default()
};

let res = compile_book(&mut book, opts, diagnostics_cfg, None)?;
let res = compile_book(&mut book, opts, diagnostics_cfg, None, None)?;
Ok(format!("{}{}", res.diagnostics, hvm_book_show_pretty(&res.hvm_book)))
})
}
Expand All @@ -126,7 +126,7 @@ fn compile_file_o_no_all() {
let mut book = do_parse_book_default(code, path)?;
let compile_opts = CompileOpts::default().set_no_all();
let diagnostics_cfg = DiagnosticsConfig::default();
let res = compile_book(&mut book, compile_opts, diagnostics_cfg, None)?;
let res = compile_book(&mut book, compile_opts, diagnostics_cfg, None, None)?;
Ok(hvm_book_show_pretty(&res.hvm_book).to_string())
})
}
Expand Down Expand Up @@ -373,7 +373,7 @@ fn compile_entrypoint() {
let mut book = do_parse_book_default(code, path)?;
book.entrypoint = Some(Name::new("foo"));
let diagnostics_cfg = DiagnosticsConfig { ..DiagnosticsConfig::new(Severity::Error, true) };
let res = compile_book(&mut book, CompileOpts::default(), diagnostics_cfg, None)?;
let res = compile_book(&mut book, CompileOpts::default(), diagnostics_cfg, None, None)?;
Ok(format!("{}{}", res.diagnostics, hvm_book_show_pretty(&res.hvm_book)))
})
}
Expand Down Expand Up @@ -421,7 +421,7 @@ fn mutual_recursion() {
DiagnosticsConfig { recursion_cycle: Severity::Error, ..DiagnosticsConfig::new(Severity::Allow, true) };
let mut book = do_parse_book_default(code, path)?;
let opts = CompileOpts { merge: true, ..CompileOpts::default() };
let res = compile_book(&mut book, opts, diagnostics_cfg, None)?;
let res = compile_book(&mut book, opts, diagnostics_cfg, None, None)?;
Ok(format!("{}{}", res.diagnostics, hvm_book_show_pretty(&res.hvm_book)))
})
}
Expand Down Expand Up @@ -500,7 +500,7 @@ fn scott_triggers_unused() {
let opts = CompileOpts::default();
let diagnostics_cfg =
DiagnosticsConfig { unused_definition: Severity::Error, ..DiagnosticsConfig::default() };
let res = compile_book(&mut book, opts, diagnostics_cfg, None)?;
let res = compile_book(&mut book, opts, diagnostics_cfg, None, None)?;
Ok(format!("{}{}", res.diagnostics, hvm_book_show_pretty(&res.hvm_book)))
})
}
Expand All @@ -517,7 +517,7 @@ fn compile_long() {
..Default::default()
};

compile_book(&mut book, opts.clone(), diagnostics_cfg, None)?;
compile_book(&mut book, opts.clone(), diagnostics_cfg, None, None)?;
Ok("Compiled".to_string())
})
}