From f40bcfa46c6c2800a181148c16f2ef1614e2df42 Mon Sep 17 00:00:00 2001 From: Kunal Singh Date: Sat, 27 Jul 2024 12:27:32 +0530 Subject: [PATCH 1/6] enalbe net_size_check by default with different max sizes for c and cuda --- examples/hello_world.bend | 2 +- src/hvm/check_net_size.rs | 14 ++++++++++---- src/lib.rs | 15 ++++++++------- src/main.rs | 4 ++-- tests/golden_tests.rs | 14 +++++++------- 5 files changed, 28 insertions(+), 21 deletions(-) diff --git a/examples/hello_world.bend b/examples/hello_world.bend index a1e3ce631..81ad13e50 100644 --- a/examples/hello_world.bend +++ b/examples/hello_world.bend @@ -1,4 +1,4 @@ def main(): with IO: - * <- IO/print("Hello, world!") + * <- IO/print("Hello, world!\n") return wrap(0) \ No newline at end of file diff --git a/src/hvm/check_net_size.rs b/src/hvm/check_net_size.rs index ff733be60..28bf2b78b 100644 --- a/src/hvm/check_net_size.rs +++ b/src/hvm/check_net_size.rs @@ -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> { + 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."), Name::new(name), ); } diff --git a/src/lib.rs b/src/lib.rs index 2af6bf988..e2f90fa6e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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, @@ -35,7 +35,7 @@ pub fn check_book( compile_opts: CompileOpts, ) -> Result { // 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) } @@ -44,6 +44,7 @@ pub fn compile_book( opts: CompileOpts, diagnostics_cfg: DiagnosticsConfig, args: Option>, + cmd: Option<&str>, ) -> Result { let mut diagnostics = desugar_book(book, opts.clone(), diagnostics_cfg, args)?; @@ -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); @@ -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()?; @@ -174,7 +175,7 @@ pub fn run_book( cmd: &str, ) -> Result, 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 @@ -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, } } diff --git a/src/main.rs b/src/main.rs index 82f3cc230..5e44e067e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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)); @@ -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())?; diff --git a/tests/golden_tests.rs b/tests/golden_tests.rs index a377c24ff..bd7e0b2ea 100644 --- a/tests/golden_tests.rs +++ b/tests/golden_tests.rs @@ -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))) }) } @@ -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))) }) } @@ -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()) }) } @@ -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))) }) } @@ -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))) }) } @@ -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))) }) } @@ -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()) }) } From c1859879a5761c07d32aaf4ba3c0254075849acf Mon Sep 17 00:00:00 2001 From: Kunal Singh Date: Wed, 7 Aug 2024 23:06:02 +0530 Subject: [PATCH 2/6] fix: decoupled bend lib and cli --- src/hvm/check_net_size.rs | 10 +++++++--- src/lib.rs | 15 +++++++++----- src/main.rs | 20 +++++++++++-------- tests/golden_tests.rs | 16 +++++++-------- .../compile_long__huge_tree.bend.snap | 4 +++- 5 files changed, 40 insertions(+), 25 deletions(-) diff --git a/src/hvm/check_net_size.rs b/src/hvm/check_net_size.rs index 28bf2b78b..2f4918146 100644 --- a/src/hvm/check_net_size.rs +++ b/src/hvm/check_net_size.rs @@ -1,12 +1,16 @@ 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_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> { - let net_size_bound = match cmd { +pub fn check_net_sizes( + book: &Book, + diagnostics: &mut Diagnostics, + opts: &CompileOpts, +) -> 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, }; diff --git a/src/lib.rs b/src/lib.rs index e2f90fa6e..92a0da231 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,7 +35,7 @@ pub fn check_book( compile_opts: CompileOpts, ) -> Result { // TODO: Do the checks without having to do full compilation - let res = compile_book(book, compile_opts, diagnostics_cfg, None, None)?; + let res = compile_book(book, compile_opts, diagnostics_cfg, None)?; Ok(res.diagnostics) } @@ -44,7 +44,6 @@ pub fn compile_book( opts: CompileOpts, diagnostics_cfg: DiagnosticsConfig, args: Option>, - cmd: Option<&str>, ) -> Result { let mut diagnostics = desugar_book(book, opts.clone(), diagnostics_cfg, args)?; @@ -73,8 +72,8 @@ pub fn compile_book( prune_hvm_book(&mut hvm_book, &prune_entrypoints); } - if opts.check_net_size && cmd.is_some() { - check_net_sizes(&hvm_book, &mut diagnostics, cmd.unwrap())?; + if opts.check_net_size { + check_net_sizes(&hvm_book, &mut diagnostics, &opts)?; } add_recursive_priority(&mut hvm_book); @@ -175,7 +174,7 @@ pub fn run_book( cmd: &str, ) -> Result, Diagnostics> { let CompileResult { hvm_book: core_book, labels, diagnostics } = - compile_book(&mut book, compile_opts.clone(), diagnostics_cfg, args, Some(cmd))?; + compile_book(&mut book, compile_opts.clone(), diagnostics_cfg, args)?; // 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 @@ -332,6 +331,9 @@ impl OptLevel { #[derive(Clone, Debug)] pub struct CompileOpts { + /// The Bend command + pub command: String, + /// Enables [hvm::eta_reduce]. pub eta: bool, @@ -362,6 +364,7 @@ impl CompileOpts { #[must_use] pub fn set_all(self) -> Self { Self { + command: self.command, eta: true, prune: true, float_combinators: true, @@ -377,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, @@ -407,6 +411,7 @@ 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, diff --git a/src/main.rs b/src/main.rs index 5e44e067e..270c527e0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -190,10 +190,14 @@ pub enum OptArgs { AdtNumScott, } -fn compile_opts_from_cli(args: &Vec) -> CompileOpts { +fn compile_opts_from_cli(args: &Vec, 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(), @@ -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)?; @@ -300,10 +304,10 @@ 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, Some("gen-hvm"))?; + let compile_res = compile_book(&mut book, opts, diagnostics_cfg, None)?; eprint!("{}", compile_res.diagnostics); println!("{}", hvm_book_show_pretty(&compile_res.hvm_book)); @@ -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(); @@ -342,10 +346,10 @@ 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, Some(gen_cmd))?; + let compile_res = compile_book(&mut book, opts, diagnostics_cfg, None)?; let out_path = ".out.hvm"; std::fs::write(out_path, hvm_book_show_pretty(&compile_res.hvm_book)).map_err(|x| x.to_string())?; @@ -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)?; diff --git a/tests/golden_tests.rs b/tests/golden_tests.rs index bd7e0b2ea..f7d2cf5e3 100644 --- a/tests/golden_tests.rs +++ b/tests/golden_tests.rs @@ -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, None)?; + let res = compile_book(&mut book, compile_opts, diagnostics_cfg, None)?; Ok(format!("{}{}", res.diagnostics, hvm_book_show_pretty(&res.hvm_book))) }) } @@ -115,7 +115,7 @@ fn compile_file_o_all() { ..Default::default() }; - let res = compile_book(&mut book, opts, diagnostics_cfg, None, None)?; + let res = compile_book(&mut book, opts, diagnostics_cfg, None)?; Ok(format!("{}{}", res.diagnostics, hvm_book_show_pretty(&res.hvm_book))) }) } @@ -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, None)?; + let res = compile_book(&mut book, compile_opts, diagnostics_cfg, None)?; Ok(hvm_book_show_pretty(&res.hvm_book).to_string()) }) } @@ -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, None)?; + let res = compile_book(&mut book, CompileOpts::default(), diagnostics_cfg, None)?; Ok(format!("{}{}", res.diagnostics, hvm_book_show_pretty(&res.hvm_book))) }) } @@ -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, None)?; + let res = compile_book(&mut book, opts, diagnostics_cfg, None)?; Ok(format!("{}{}", res.diagnostics, hvm_book_show_pretty(&res.hvm_book))) }) } @@ -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, None)?; + let res = compile_book(&mut book, opts, diagnostics_cfg, None)?; Ok(format!("{}{}", res.diagnostics, hvm_book_show_pretty(&res.hvm_book))) }) } @@ -517,7 +517,7 @@ fn compile_long() { ..Default::default() }; - compile_book(&mut book, opts.clone(), diagnostics_cfg, None, 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))) }) } diff --git a/tests/snapshots/compile_long__huge_tree.bend.snap b/tests/snapshots/compile_long__huge_tree.bend.snap index d5a8a014d..aa3ff1d4f 100644 --- a/tests/snapshots/compile_long__huge_tree.bend.snap +++ b/tests/snapshots/compile_long__huge_tree.bend.snap @@ -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. From a167cf1205f85fe5fdc1bd870977c12cb02775aa Mon Sep 17 00:00:00 2001 From: Kunal Singh Date: Thu, 8 Aug 2024 01:12:49 +0530 Subject: [PATCH 3/6] fix: compiler target --- src/hvm/check_net_size.rs | 12 ++++++------ src/lib.rs | 19 +++++++++++++------ src/main.rs | 27 ++++++++++++++++----------- 3 files changed, 35 insertions(+), 23 deletions(-) diff --git a/src/hvm/check_net_size.rs b/src/hvm/check_net_size.rs index 2f4918146..f524ff121 100644 --- a/src/hvm/check_net_size.rs +++ b/src/hvm/check_net_size.rs @@ -1,5 +1,5 @@ use super::tree_children; -use crate::{diagnostics::Diagnostics, fun::Name, CompileOpts}; +use crate::{diagnostics::Diagnostics, fun::Name, CompilerTarget}; use hvm::ast::{Book, Net, Tree}; pub const MAX_NET_SIZE_C: usize = 4095; @@ -8,11 +8,11 @@ pub const MAX_NET_SIZE_CUDA: usize = 64; pub fn check_net_sizes( book: &Book, diagnostics: &mut Diagnostics, - opts: &CompileOpts, + target: &CompilerTarget, ) -> 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, + let (net_size_bound, target_lang) = match target { + CompilerTarget::Cuda => (MAX_NET_SIZE_CUDA, "Cuda"), + _ => (MAX_NET_SIZE_C, "C"), }; diagnostics.start_pass(); @@ -21,7 +21,7 @@ pub fn check_net_sizes( let nodes = count_nodes(net); if nodes > net_size_bound { diagnostics.add_rule_error( - format!("Definition is too large for hvm (size={nodes}, max size={net_size_bound}). Please break it into smaller pieces."), + format!("Definition is too large for HVM {target_lang} (size={nodes}, max size={net_size_bound}). Please break it into smaller pieces."), Name::new(name), ); } diff --git a/src/lib.rs b/src/lib.rs index 92a0da231..1deec0176 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -73,7 +73,7 @@ pub fn compile_book( } if opts.check_net_size { - check_net_sizes(&hvm_book, &mut diagnostics, &opts)?; + check_net_sizes(&hvm_book, &mut diagnostics, &opts.target_architecture)?; } add_recursive_priority(&mut hvm_book); @@ -329,10 +329,17 @@ impl OptLevel { } } +#[derive(Clone, Debug)] +pub enum CompilerTarget { + C, + Cuda, + Unknown, +} + #[derive(Clone, Debug)] pub struct CompileOpts { - /// The Bend command - pub command: String, + /// The Compiler target architecture + pub target_architecture: CompilerTarget, /// Enables [hvm::eta_reduce]. pub eta: bool, @@ -364,7 +371,7 @@ impl CompileOpts { #[must_use] pub fn set_all(self) -> Self { Self { - command: self.command, + target_architecture: self.target_architecture, eta: true, prune: true, float_combinators: true, @@ -380,7 +387,7 @@ impl CompileOpts { #[must_use] pub fn set_no_all(self) -> Self { Self { - command: self.command, + target_architecture: self.target_architecture, eta: false, prune: false, linearize_matches: OptLevel::Disabled, @@ -411,7 +418,7 @@ impl Default for CompileOpts { /// Uses num-scott ADT encoding. fn default() -> Self { Self { - command: String::from("run"), + target_architecture: CompilerTarget::Cuda, eta: true, prune: false, linearize_matches: OptLevel::Enabled, diff --git a/src/main.rs b/src/main.rs index 270c527e0..caa03ae9b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ use bend::{ fun::{Book, Name}, hvm::hvm_book_show_pretty, imports::DefaultLoader, - load_file_to_book, run_book, AdtEncoding, CompileOpts, OptLevel, RunOpts, + load_file_to_book, run_book, AdtEncoding, CompileOpts, CompilerTarget, OptLevel, RunOpts, }; use clap::{Args, CommandFactory, Parser, Subcommand}; use std::{ @@ -190,13 +190,10 @@ pub enum OptArgs { AdtNumScott, } -fn compile_opts_from_cli(args: &Vec, cmd: Option<&str>) -> CompileOpts { +fn compile_opts_from_cli(args: &Vec, compiler_target: CompilerTarget) -> CompileOpts { use OptArgs::*; let mut opts = CompileOpts::default(); - - if let Some(cmd) = cmd { - opts.command = cmd.to_string(); - } + opts.target_architecture = compiler_target; for arg in args { match arg { @@ -292,10 +289,18 @@ fn execute_cli_mode(mut cli: Cli) -> Result<(), Diagnostics> { _ => "run-c", }; + let compiler_target = match &cli.mode { + Mode::RunC(..) => CompilerTarget::C, + Mode::GenC(..) => CompilerTarget::C, + Mode::RunCu(..) => CompilerTarget::Cuda, + Mode::GenCu(..) => CompilerTarget::Cuda, + _ => CompilerTarget::Unknown, + }; + 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, None); + let compile_opts = compile_opts_from_cli(&comp_opts, compiler_target); let mut book = load_book(&path, diagnostics_cfg)?; let diagnostics = check_book(&mut book, diagnostics_cfg, compile_opts)?; @@ -304,7 +309,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, Some("gen-hvm")); + let opts = compile_opts_from_cli(&comp_opts, compiler_target); let mut book = load_book(&path, diagnostics_cfg)?; let compile_res = compile_book(&mut book, opts, diagnostics_cfg, None)?; @@ -321,7 +326,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, Some(run_cmd)); + let compile_opts = compile_opts_from_cli(&comp_opts, compiler_target); compile_opts.check_for_strict(); @@ -346,7 +351,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, Some(gen_cmd)); + let opts = compile_opts_from_cli(&comp_opts, compiler_target); let mut book = load_book(&path, diagnostics_cfg)?; let compile_res = compile_book(&mut book, opts, diagnostics_cfg, None)?; @@ -377,7 +382,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, None); + let opts = compile_opts_from_cli(&comp_opts, compiler_target); let mut book = load_book(&path, diagnostics_cfg)?; let diagnostics = desugar_book(&mut book, opts, diagnostics_cfg, None)?; From 431732bced881deb23f00cd9bd17fd720f49ac45 Mon Sep 17 00:00:00 2001 From: Kunal Singh Date: Thu, 8 Aug 2024 16:33:07 +0530 Subject: [PATCH 4/6] fix: default compiler target to unknown --- src/lib.rs | 2 +- tests/golden_tests.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1deec0176..209d32bf2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -418,7 +418,7 @@ impl Default for CompileOpts { /// Uses num-scott ADT encoding. fn default() -> Self { Self { - target_architecture: CompilerTarget::Cuda, + target_architecture: CompilerTarget::Unknown, eta: true, prune: false, linearize_matches: OptLevel::Enabled, diff --git a/tests/golden_tests.rs b/tests/golden_tests.rs index f7d2cf5e3..a377c24ff 100644 --- a/tests/golden_tests.rs +++ b/tests/golden_tests.rs @@ -517,7 +517,7 @@ fn compile_long() { ..Default::default() }; - let res = compile_book(&mut book, opts.clone(), diagnostics_cfg, None)?; - Ok(format!("{}{}", res.diagnostics, hvm_book_show_pretty(&res.hvm_book))) + compile_book(&mut book, opts.clone(), diagnostics_cfg, None)?; + Ok("Compiled".to_string()) }) } From 2decd5d757ed9d10474bf4da2552b218be622cdb Mon Sep 17 00:00:00 2001 From: Kunal Singh Date: Thu, 8 Aug 2024 16:40:00 +0530 Subject: [PATCH 5/6] fix: clippy warning --- src/main.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index caa03ae9b..02f5b2743 100644 --- a/src/main.rs +++ b/src/main.rs @@ -192,8 +192,7 @@ pub enum OptArgs { fn compile_opts_from_cli(args: &Vec, compiler_target: CompilerTarget) -> CompileOpts { use OptArgs::*; - let mut opts = CompileOpts::default(); - opts.target_architecture = compiler_target; + let mut opts = CompileOpts { target_architecture: compiler_target, ..CompileOpts::default() }; for arg in args { match arg { From 305c9446192f30f615645d7e5e1a501ed1af1b19 Mon Sep 17 00:00:00 2001 From: Kunal Singh Date: Thu, 8 Aug 2024 19:38:33 +0530 Subject: [PATCH 6/6] update test snapshorts --- .../cli__net_size_too_large.bend.snap | 170 +++++++++++++++++- .../compile_long__huge_tree.bend.snap | 2 +- .../compile_long__long_str_file.bend.snap | 4 +- 3 files changed, 171 insertions(+), 5 deletions(-) diff --git a/tests/snapshots/cli__net_size_too_large.bend.snap b/tests/snapshots/cli__net_size_too_large.bend.snap index 0d3f91caf..60d1eeacc 100644 --- a/tests/snapshots/cli__net_size_too_large.bend.snap +++ b/tests/snapshots/cli__net_size_too_large.bend.snap @@ -2,6 +2,170 @@ source: tests/golden_tests.rs input_file: tests/golden_tests/cli/net_size_too_large.bend --- -Errors: -In definition 'Radix': - Definition is too large for hvm (size=120, max size=64). Please break it into smaller pieces. +@Arr/Leaf = (a ((@Arr/Leaf/tag (a b)) b)) + +@Arr/Leaf/tag = 1 + +@Arr/Node = (a (b ((@Arr/Node/tag (a (b c))) c))) + +@Arr/Node/tag = 2 + +@Arr/Null = ((@Arr/Null/tag a) a) + +@Arr/Null/tag = 0 + +@Gen = (a b) + & @Gen.go ~ (a (0 b)) + +@Gen.go = (?((@Gen.go__C0 @Gen.go__C1) a) a) + +@Gen.go__C0 = a + & @Arr/Leaf ~ a + +@Gen.go__C1 = ({a d} ({$([*0x0000002] $([|0x0000001] e)) $([*0x0000002] b)} g)) + & @Arr/Node ~ (c (f g)) + &!@Gen.go ~ (a (b c)) + &!@Gen.go ~ (d (e f)) + +@Map_/Both = (a (b ((@Map_/Both/tag (a (b c))) c))) + +@Map_/Both/tag = 2 + +@Map_/Free = ((@Map_/Free/tag a) a) + +@Map_/Free/tag = 0 + +@Map_/Used = ((@Map_/Used/tag a) a) + +@Map_/Used/tag = 1 + +@Merge = ((@Merge__C13 a) a) + +@Merge__C0 = (* a) + & @Map_/Both ~ a + +@Merge__C1 = (?((@Map_/Used @Merge__C0) a) a) + +@Merge__C10 = (* (a (b ((@Merge__C8 (a (b c))) c)))) + +@Merge__C11 = ((@Merge__C2 a) a) + +@Merge__C12 = (?((@Merge__C9 @Merge__C10) a) a) + +@Merge__C13 = (?((@Merge__C11 @Merge__C12) a) a) + +@Merge__C2 = (?((@Map_/Free @Merge__C1) a) a) + +@Merge__C3 = (?((@Map_/Used *) a) a) + +@Merge__C4 = (?((@Map_/Used @Merge__C3) a) a) + +@Merge__C5 = (* (b (e (a (d g))))) + & @Map_/Both ~ (c (f g)) + &!@Merge ~ (a (b c)) + &!@Merge ~ (d (e f)) + +@Merge__C6 = a + & @Map_/Both ~ a + +@Merge__C7 = (?((* @Merge__C5) a) a) + +@Merge__C8 = (?((@Merge__C6 @Merge__C7) a) a) + +@Merge__C9 = ((@Merge__C4 a) a) + +@Radix = ({$([&0x0800000] a) {$([&0x0400000] b) {$([&0x0200000] c) {$([&0x0100000] d) {$([&0x0080000] e) {$([&0x0040000] f) {$([&0x0020000] g) {$([&0x0010000] h) {$([&0x0008000] i) {$([&0x0004000] j) {$([&0x0002000] k) {$([&0x0001000] l) {$([&0x0000800] m) {$([&0x0000400] n) {$([&0x0000200] o) {$([&0x0000100] p) {$([&0x0000080] q) {$([&0x0000040] r) {$([&0x0000020] s) {$([&0x0000010] t) {$([&0x0000008] u) {$([&0x0000004] v) {$([&0x0000002] w) $([&0x0000001] x)}}}}}}}}}}}}}}}}}}}}}}} vb) + & @Swap ~ (a (ub (@Map_/Free vb))) + & @Swap ~ (b (tb (@Map_/Free ub))) + & @Swap ~ (c (sb (@Map_/Free tb))) + & @Swap ~ (d (rb (@Map_/Free sb))) + & @Swap ~ (e (qb (@Map_/Free rb))) + & @Swap ~ (f (pb (@Map_/Free qb))) + & @Swap ~ (g (ob (@Map_/Free pb))) + & @Swap ~ (h (nb (@Map_/Free ob))) + & @Swap ~ (i (mb (@Map_/Free nb))) + & @Swap ~ (j (lb (@Map_/Free mb))) + & @Swap ~ (k (kb (@Map_/Free lb))) + & @Swap ~ (l (jb (@Map_/Free kb))) + & @Swap ~ (m (ib (@Map_/Free jb))) + & @Swap ~ (n (hb (@Map_/Free ib))) + & @Swap ~ (o (gb (@Map_/Free hb))) + & @Swap ~ (p (fb (@Map_/Free gb))) + & @Swap ~ (q (eb (@Map_/Free fb))) + & @Swap ~ (r (db (@Map_/Free eb))) + & @Swap ~ (s (cb (@Map_/Free db))) + & @Swap ~ (t (bb (@Map_/Free cb))) + & @Swap ~ (u (ab (@Map_/Free bb))) + & @Swap ~ (v (z (@Map_/Free ab))) + & @Swap ~ (w (y (@Map_/Free z))) + & @Swap ~ (x (@Map_/Used (@Map_/Free y))) + +@Reverse = ((@Reverse__C3 a) a) + +@Reverse__C0 = a + & @Arr/Leaf ~ a + +@Reverse__C1 = (* (c (a e))) + & @Arr/Node ~ (b (d e)) + &!@Reverse ~ (a b) + &!@Reverse ~ (c d) + +@Reverse__C2 = (?((@Reverse__C0 @Reverse__C1) a) a) + +@Reverse__C3 = (?((@Arr/Null @Reverse__C2) a) a) + +@Sort = (a c) + & @ToArr ~ (0 (b c)) + & @ToMap ~ (a b) + +@Sum = ((@Sum__C2 a) a) + +@Sum__C0 = (* (a (b d))) + &!@Sum ~ (a $([+] $(c d))) + &!@Sum ~ (b c) + +@Sum__C1 = (?(((a a) @Sum__C0) b) b) + +@Sum__C2 = (?((0 @Sum__C1) a) a) + +@Swap = (?((@Swap__C0 @Swap__C1) a) a) + +@Swap__C0 = a + & @Map_/Both ~ a + +@Swap__C1 = (* (b (a c))) + & @Map_/Both ~ (a (b c)) + +@ToArr = (a ((@ToArr__C3 (a b)) b)) + +@ToArr__C0 = a + & @Arr/Leaf ~ a + +@ToArr__C1 = (* (b (e ({$([*0x0000002] $([+0x0000001] d)) $([*0x0000002] $([+0x0000000] a))} g)))) + & @Arr/Node ~ (c (f g)) + &!@ToArr ~ (a (b c)) + &!@ToArr ~ (d (e f)) + +@ToArr__C2 = (?((@ToArr__C0 @ToArr__C1) a) a) + +@ToArr__C3 = (?(((* @Arr/Null) @ToArr__C2) a) a) + +@ToMap = ((@ToMap__C3 a) a) + +@ToMap__C0 = a + & @Radix ~ a + +@ToMap__C1 = (* (a (c e))) + & @Merge ~ (b (d e)) + &!@ToMap ~ (a b) + &!@ToMap ~ (c d) + +@ToMap__C2 = (?((@ToMap__C0 @ToMap__C1) a) a) + +@ToMap__C3 = (?((@Map_/Free @ToMap__C2) a) a) + +@main = d + & @Sum ~ (c d) + & @Sort ~ (b c) + & @Reverse ~ (a b) + & @Gen ~ (4 a) diff --git a/tests/snapshots/compile_long__huge_tree.bend.snap b/tests/snapshots/compile_long__huge_tree.bend.snap index aa3ff1d4f..ee6c230f5 100644 --- a/tests/snapshots/compile_long__huge_tree.bend.snap +++ b/tests/snapshots/compile_long__huge_tree.bend.snap @@ -4,4 +4,4 @@ input_file: tests/golden_tests/compile_long/huge_tree.bend --- Errors: In definition 'main': - Definition is too large for hvm (size=120002, max size=4095). Please break it into smaller pieces. + Definition is too large for HVM C (size=120002, max size=4095). Please break it into smaller pieces. diff --git a/tests/snapshots/compile_long__long_str_file.bend.snap b/tests/snapshots/compile_long__long_str_file.bend.snap index 5f780d9ea..b4e9f10d8 100644 --- a/tests/snapshots/compile_long__long_str_file.bend.snap +++ b/tests/snapshots/compile_long__long_str_file.bend.snap @@ -2,4 +2,6 @@ source: tests/golden_tests.rs input_file: tests/golden_tests/compile_long/long_str_file.bend --- -Compiled +Errors: +In definition 'main': + Definition is too large for HVM C (size=1461028, max size=4095). Please break it into smaller pieces.