Skip to content

Commit

Permalink
fix: compiler target
Browse files Browse the repository at this point in the history
  • Loading branch information
KunalSin9h committed Aug 7, 2024
1 parent c185987 commit b4e279d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 deletions.
13 changes: 7 additions & 6 deletions src/hvm/check_net_size.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -8,11 +8,12 @@ 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,
dbg!(&target);
let (net_size_bound, target_lang) = match target {
CompilerTarget::Cuda => (MAX_NET_SIZE_CUDA, "Cuda"),
_ => (MAX_NET_SIZE_C, "C"),
};

diagnostics.start_pass();
Expand All @@ -21,7 +22,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),
);
}
Expand Down
19 changes: 13 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
27 changes: 16 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -190,13 +190,10 @@ pub enum OptArgs {
AdtNumScott,
}

fn compile_opts_from_cli(args: &Vec<OptArgs>, cmd: Option<&str>) -> CompileOpts {
fn compile_opts_from_cli(args: &Vec<OptArgs>, 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 {
Expand Down Expand Up @@ -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)?;
Expand All @@ -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)?;
Expand All @@ -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();

Expand All @@ -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)?;
Expand Down Expand Up @@ -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)?;
Expand Down

0 comments on commit b4e279d

Please sign in to comment.