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 3 commits
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)
20 changes: 15 additions & 5 deletions src/hvm/check_net_size.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
use super::tree_children;
use crate::{diagnostics::Diagnostics, fun::Name};
use crate::{diagnostics::Diagnostics, fun::Name, CompileOpts};
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,
opts: &CompileOpts,
KunalSin9h marked this conversation as resolved.
Show resolved Hide resolved
) -> Result<(), Diagnostics> {
let net_size_bound = match opts.command.as_str() {
"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
14 changes: 10 additions & 4 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 @@ -73,7 +73,7 @@ pub fn compile_book(
}

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

add_recursive_priority(&mut hvm_book);
Expand Down Expand Up @@ -143,7 +143,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 @@ -331,6 +331,9 @@ impl OptLevel {

#[derive(Clone, Debug)]
pub struct CompileOpts {
/// The Bend command
pub command: String,
KunalSin9h marked this conversation as resolved.
Show resolved Hide resolved

/// Enables [hvm::eta_reduce].
pub eta: bool,

Expand Down Expand Up @@ -361,6 +364,7 @@ impl CompileOpts {
#[must_use]
pub fn set_all(self) -> Self {
Self {
command: self.command,
eta: true,
prune: true,
float_combinators: true,
Expand All @@ -376,6 +380,7 @@ impl CompileOpts {
#[must_use]
pub fn set_no_all(self) -> Self {
Self {
command: self.command,
eta: false,
prune: false,
linearize_matches: OptLevel::Disabled,
Expand Down Expand Up @@ -406,13 +411,14 @@ impl Default for CompileOpts {
/// Uses num-scott ADT encoding.
fn default() -> Self {
Self {
command: String::from("run"),
eta: true,
prune: false,
linearize_matches: OptLevel::Enabled,
float_combinators: true,
merge: false,
inline: false,
check_net_size: false,
check_net_size: true,
adt_encoding: AdtEncoding::NumScott,
}
}
Expand Down
16 changes: 10 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,14 @@ pub enum OptArgs {
AdtNumScott,
}

fn compile_opts_from_cli(args: &Vec<OptArgs>) -> CompileOpts {
fn compile_opts_from_cli(args: &Vec<OptArgs>, cmd: Option<&str>) -> CompileOpts {
use OptArgs::*;
let mut opts = CompileOpts::default();

if let Some(cmd) = cmd {
opts.command = cmd.to_string();
}

for arg in args {
match arg {
All => opts = opts.set_all(),
Expand Down Expand Up @@ -291,7 +295,7 @@ fn execute_cli_mode(mut cli: Cli) -> Result<(), Diagnostics> {
match cli.mode {
Mode::Check { comp_opts, warn_opts, path } => {
let diagnostics_cfg = set_warning_cfg_from_cli(DiagnosticsConfig::default(), warn_opts);
let compile_opts = compile_opts_from_cli(&comp_opts);
let compile_opts = compile_opts_from_cli(&comp_opts, None);

let mut book = load_book(&path, diagnostics_cfg)?;
let diagnostics = check_book(&mut book, diagnostics_cfg, compile_opts)?;
Expand All @@ -300,7 +304,7 @@ fn execute_cli_mode(mut cli: Cli) -> Result<(), Diagnostics> {

Mode::GenHvm(GenArgs { comp_opts, warn_opts, path, .. }) => {
let diagnostics_cfg = set_warning_cfg_from_cli(DiagnosticsConfig::default(), warn_opts);
let opts = compile_opts_from_cli(&comp_opts);
let opts = compile_opts_from_cli(&comp_opts, Some("gen-hvm"));

let mut book = load_book(&path, diagnostics_cfg)?;
let compile_res = compile_book(&mut book, opts, diagnostics_cfg, None)?;
Expand All @@ -317,7 +321,7 @@ fn execute_cli_mode(mut cli: Cli) -> Result<(), Diagnostics> {
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);
let compile_opts = compile_opts_from_cli(&comp_opts, Some(run_cmd));

compile_opts.check_for_strict();

Expand All @@ -342,7 +346,7 @@ fn execute_cli_mode(mut cli: Cli) -> Result<(), Diagnostics> {
Mode::GenC(GenArgs { comp_opts, warn_opts, path })
| Mode::GenCu(GenArgs { comp_opts, warn_opts, path }) => {
let diagnostics_cfg = set_warning_cfg_from_cli(DiagnosticsConfig::default(), warn_opts);
let opts = compile_opts_from_cli(&comp_opts);
let opts = compile_opts_from_cli(&comp_opts, Some(gen_cmd));

let mut book = load_book(&path, diagnostics_cfg)?;
let compile_res = compile_book(&mut book, opts, diagnostics_cfg, None)?;
Expand Down Expand Up @@ -373,7 +377,7 @@ fn execute_cli_mode(mut cli: Cli) -> Result<(), Diagnostics> {
Mode::Desugar { path, comp_opts, warn_opts, pretty } => {
let diagnostics_cfg = set_warning_cfg_from_cli(DiagnosticsConfig::default(), warn_opts);

let opts = compile_opts_from_cli(&comp_opts);
let opts = compile_opts_from_cli(&comp_opts, None);

let mut book = load_book(&path, diagnostics_cfg)?;
let diagnostics = desugar_book(&mut book, opts, diagnostics_cfg, None)?;
Expand Down
4 changes: 2 additions & 2 deletions tests/golden_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ fn compile_long() {
..Default::default()
};

compile_book(&mut book, opts.clone(), diagnostics_cfg, None)?;
Ok("Compiled".to_string())
let res = compile_book(&mut book, opts.clone(), diagnostics_cfg, None)?;
Ok(format!("{}{}", res.diagnostics, hvm_book_show_pretty(&res.hvm_book)))
KunalSin9h marked this conversation as resolved.
Show resolved Hide resolved
})
}
4 changes: 3 additions & 1 deletion tests/snapshots/compile_long__huge_tree.bend.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
source: tests/golden_tests.rs
input_file: tests/golden_tests/compile_long/huge_tree.bend
---
Compiled
Errors:
In definition 'main':
Definition is too large for hvm (size=120002, max size=4095). Please break it into smaller pieces.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is weird, it should be breaking this thing in pieces of the size we passed to the float_combinators pass (64, MAX_NET_SIZE_CUDA)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll have a look into why this is later