From e95d32e821c005ac09028665c8e38f190ad86fa4 Mon Sep 17 00:00:00 2001 From: Brian Carroll Date: Mon, 14 Feb 2022 22:24:51 +0000 Subject: [PATCH] DEBUG HACKS, DO NOT MERGE --- Cargo.lock | 3 ++ compiler/load/Cargo.toml | 1 + compiler/load/src/file.rs | 58 ++++++++++++++++++++++++++++++++---- compiler/solve/Cargo.toml | 1 + compiler/solve/src/module.rs | 19 ++++++++++++ compiler/solve/src/solve.rs | 18 +++++++++++ repl_eval/Cargo.toml | 1 + repl_eval/src/gen.rs | 18 +++++++++++ repl_wasm/src/lib.rs | 4 ++- repl_www/build.sh | 4 ++- 10 files changed, 120 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1414f20e6f..f7b31922f9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3584,6 +3584,7 @@ dependencies = [ "roc_unify", "tempfile", "ven_pretty", + "wasm-bindgen", ] [[package]] @@ -3698,6 +3699,7 @@ dependencies = [ "roc_reporting", "roc_target", "roc_types", + "wasm-bindgen", ] [[package]] @@ -3763,6 +3765,7 @@ dependencies = [ "roc_types", "roc_unify", "tempfile", + "wasm-bindgen", ] [[package]] diff --git a/compiler/load/Cargo.toml b/compiler/load/Cargo.toml index 57f4ffd530..81d84b7890 100644 --- a/compiler/load/Cargo.toml +++ b/compiler/load/Cargo.toml @@ -26,6 +26,7 @@ bumpalo = { version = "3.8.0", features = ["collections"] } parking_lot = { version = "0.11.2" } crossbeam = "0.8.1" num_cpus = "1.13.0" +wasm-bindgen = "0.2.79" [dev-dependencies] tempfile = "3.2.0" diff --git a/compiler/load/src/file.rs b/compiler/load/src/file.rs index 0e515ec254..8fa886f43a 100644 --- a/compiler/load/src/file.rs +++ b/compiler/load/src/file.rs @@ -45,13 +45,26 @@ use std::path::{Path, PathBuf}; use std::str::from_utf8_unchecked; use std::sync::Arc; use std::{env, fs}; +use wasm_bindgen::prelude::wasm_bindgen; + +#[wasm_bindgen] +extern "C" { + #[wasm_bindgen(js_namespace = console)] + fn log(s: &str); +} + +// In-browser debugging +#[allow(unused_macros)] +macro_rules! console_log { + ($($t:tt)*) => (log(&format_args!($($t)*).to_string())) +} use crate::work::{Dependencies, Phase}; -#[cfg(not(target_family = "wasm"))] -use std::time::{Duration, SystemTime}; #[cfg(target_family = "wasm")] use crate::wasm_system_time::{Duration, SystemTime}; +#[cfg(not(target_family = "wasm"))] +use std::time::{Duration, SystemTime}; /// Default name for the binary generated for an app, if an invalid one was specified. const DEFAULT_APP_OUTPUT_PATH: &str = "app"; @@ -718,6 +731,7 @@ enum BuildTask<'a> { }, } +#[derive(Debug)] enum WorkerMsg { Shutdown, TaskAdded, @@ -831,6 +845,8 @@ pub fn load_and_monomorphize_from_str<'a>( ) -> Result, LoadingProblem<'a>> { use LoadResult::*; + console_log!("load_and_monomorphize_from_str"); + let load_start = LoadStart::from_str(arena, filename, src)?; match load( @@ -997,6 +1013,8 @@ fn load<'a>( goal_phase: Phase, target_info: TargetInfo, ) -> Result, LoadingProblem<'a>> { + console_log!("load start"); + let LoadStart { arc_modules, ident_ids_by_module, @@ -1008,10 +1026,14 @@ fn load<'a>( let (msg_tx, msg_rx) = bounded(1024); + console_log!("before msg_tx.send"); + msg_tx .send(root_msg) .map_err(|_| LoadingProblem::MsgChannelDied)?; + console_log!("after msg_tx.send"); + let mut state = State { root_id, target_info, @@ -1038,15 +1060,21 @@ fn load<'a>( // We'll add tasks to this, and then worker threads will take tasks from it. let injector = Injector::new(); + console_log!("after injector"); + let (worker_msg_tx, worker_msg_rx) = bounded(1024); let worker_listener = worker_msg_tx; let worker_listeners = arena.alloc([worker_listener]); + console_log!("before Worker lifo"); + let worker = Worker::new_lifo(); let stealer = worker.stealer(); let stealers = &[stealer]; + console_log!("before loop"); loop { + console_log!("inside loop"); match state_thread_step(arena, state, worker_listeners, &injector, &msg_tx, &msg_rx) { Ok(ControlFlow::Break(done)) => return Ok(done), Ok(ControlFlow::Continue(new_state)) => { @@ -1054,8 +1082,9 @@ fn load<'a>( } Err(e) => return Err(e), } + console_log!("after match"); - match worker_task_step( + let control_flow = worker_task_step( arena, &worker, &injector, @@ -1064,7 +1093,11 @@ fn load<'a>( &msg_tx, src_dir, target_info, - ) { + ); + + console_log!("control flow {:?}", control_flow); + + match control_flow { Ok(ControlFlow::Break(())) => panic!("the worker should not break!"), Ok(ControlFlow::Continue(())) => { // progress was made @@ -1453,7 +1486,10 @@ fn worker_task_step<'a>( src_dir: &Path, target_info: TargetInfo, ) -> Result, LoadingProblem<'a>> { - match worker_msg_rx.try_recv() { + console_log!("worker_task_step"); + let recv = worker_msg_rx.try_recv(); + console_log!("recv {:?}", &recv); + match recv { Ok(msg) => { match msg { WorkerMsg::Shutdown => { @@ -1474,8 +1510,10 @@ fn worker_task_step<'a>( // added. In that case, do nothing, and keep waiting // until we receive a Shutdown message. if let Some(task) = find_task(&worker, injector, stealers) { + console_log!("found Some task {:?}", task); let result = run_task(task, worker_arena, src_dir, msg_tx.clone(), target_info); + console_log!("run_task result {:?}", &result); match result { Ok(()) => {} @@ -1495,6 +1533,7 @@ fn worker_task_step<'a>( } } } + console_log!("after if let Some(task)"); Ok(ControlFlow::Continue(())) } @@ -3129,12 +3168,15 @@ fn run_solve<'a>( dep_idents: MutMap, unused_imports: MutMap, ) -> Msg<'a> { + + console_log!("run_solve"); // We have more constraining work to do now, so we'll add it to our timings. let constrain_start = SystemTime::now(); // Finish constraining the module by wrapping the existing Constraint // in the ones we just computed. We can do this off the main thread. let constraint = constrain_imports(imported_symbols, constraint, &mut var_store); + console_log!("after constrain_imports"); let constrain_end = SystemTime::now(); @@ -3154,12 +3196,16 @@ fn run_solve<'a>( let (solved_subs, solved_env, problems) = roc_solve::module::run_solve(aliases, rigid_variables, constraint, var_store); + console_log!("after run_solve"); + let mut exposed_vars_by_symbol: MutMap = solved_env.vars_by_symbol.clone(); exposed_vars_by_symbol.retain(|k, _| exposed_symbols.contains(k)); let solved_types = roc_solve::module::make_solved_types(&solved_env, &solved_subs, &exposed_vars_by_symbol); + console_log!("after make_solved_types"); + let solved_module = SolvedModule { exposed_vars_by_symbol, exposed_symbols: exposed_symbols.into_iter().collect::>(), @@ -3175,6 +3221,8 @@ fn run_solve<'a>( module_timing.constrain += constrain_elapsed; module_timing.solve = solve_end.duration_since(constrain_end).unwrap(); + console_log!("creating Msg::SolvedTypes"); + // Send the subs to the main thread for processing, Msg::SolvedTypes { module_id, diff --git a/compiler/solve/Cargo.toml b/compiler/solve/Cargo.toml index ebb9e4a615..7c27ea42af 100644 --- a/compiler/solve/Cargo.toml +++ b/compiler/solve/Cargo.toml @@ -14,6 +14,7 @@ roc_can = { path = "../can" } roc_unify = { path = "../unify" } arrayvec = "0.7.2" bumpalo = { version = "3.8.0", features = ["collections"] } +wasm-bindgen = "0.2.79" [dev-dependencies] roc_load = { path = "../load" } diff --git a/compiler/solve/src/module.rs b/compiler/solve/src/module.rs index 920cbbdd58..36922e5295 100644 --- a/compiler/solve/src/module.rs +++ b/compiler/solve/src/module.rs @@ -7,6 +7,20 @@ use roc_types::solved_types::{Solved, SolvedType}; use roc_types::subs::{Subs, VarStore, Variable}; use roc_types::types::Alias; +use wasm_bindgen::prelude::wasm_bindgen; + +#[wasm_bindgen] +extern "C" { + #[wasm_bindgen(js_namespace = console)] + fn log(s: &str); +} + +// In-browser debugging +#[allow(unused_macros)] +macro_rules! console_log { + ($($t:tt)*) => (log(&format_args!($($t)*).to_string())) +} + #[derive(Debug)] pub struct SolvedModule { pub solved_types: MutMap, @@ -22,16 +36,20 @@ pub fn run_solve( constraint: Constraint, var_store: VarStore, ) -> (Solved, solve::Env, Vec) { + console_log!("run_solve"); + let env = solve::Env { vars_by_symbol: MutMap::default(), aliases, }; let mut subs = Subs::new_from_varstore(var_store); + console_log!("after new_from_varstore"); for (var, name) in rigid_variables { subs.rigid_var(var, name); } + console_log!("after rigid_var"); // Now that the module is parsed, canonicalized, and constrained, // we need to type check it. @@ -39,6 +57,7 @@ pub fn run_solve( // Run the solver to populate Subs. let (solved_subs, solved_env) = solve::run(&env, &mut problems, subs, &constraint); + console_log!("after solve::run"); (solved_subs, solved_env, problems) } diff --git a/compiler/solve/src/solve.rs b/compiler/solve/src/solve.rs index 8c2baee3e5..fa3632c2c1 100644 --- a/compiler/solve/src/solve.rs +++ b/compiler/solve/src/solve.rs @@ -15,6 +15,22 @@ use roc_types::types::{gather_fields_unsorted_iter, Alias, Category, ErrorType, use roc_unify::unify::{unify, Mode, Unified::*}; use std::collections::hash_map::Entry; + +use wasm_bindgen::prelude::wasm_bindgen; + +#[wasm_bindgen] +extern "C" { + #[wasm_bindgen(js_namespace = console)] + fn log(s: &str); +} + +// In-browser debugging +#[allow(unused_macros)] +macro_rules! console_log { + ($($t:tt)*) => (log(&format_args!($($t)*).to_string())) +} + + // Type checking system adapted from Elm by Evan Czaplicki, BSD-3-Clause Licensed // https://github.com/elm/compiler // Thank you, Evan! @@ -155,6 +171,7 @@ pub fn run_in_place( subs: &mut Subs, constraint: &Constraint, ) -> Env { + console_log!("run_in_place"); let mut pools = Pools::default(); let state = State { env: env.clone(), @@ -186,6 +203,7 @@ fn solve( subs: &mut Subs, constraint: &Constraint, ) -> State { + console_log!("solve constraint {:?}", constraint); match constraint { True => state, SaveTheEnvironment => { diff --git a/repl_eval/Cargo.toml b/repl_eval/Cargo.toml index 562cb27c9e..ca81ab5994 100644 --- a/repl_eval/Cargo.toml +++ b/repl_eval/Cargo.toml @@ -7,6 +7,7 @@ version = "0.1.0" [dependencies] bumpalo = {version = "3.8.0", features = ["collections"]} +wasm-bindgen = "0.2.79" roc_builtins = {path = "../compiler/builtins"} roc_can = {path = "../compiler/can"} diff --git a/repl_eval/src/gen.rs b/repl_eval/src/gen.rs index a11ba966b8..453d9cc6df 100644 --- a/repl_eval/src/gen.rs +++ b/repl_eval/src/gen.rs @@ -8,9 +8,22 @@ use roc_load::file::{LoadingProblem, MonomorphizedModule}; use roc_parse::ast::Expr; use roc_region::all::LineInfo; use roc_target::TargetInfo; +use wasm_bindgen::prelude::wasm_bindgen; use crate::eval::ToAstProblem; +#[wasm_bindgen] +extern "C" { + #[wasm_bindgen(js_namespace = console)] + fn log(s: &str); +} + +// In-browser debugging +#[allow(unused_macros)] +macro_rules! console_log { + ($($t:tt)*) => (log(&format_args!($($t)*).to_string())) +} + pub enum ReplOutput { Problems(Vec), NoProblems { expr: String, expr_type: String }, @@ -45,6 +58,8 @@ pub fn compile_to_mono<'a>( src: &str, target_info: TargetInfo, ) -> Result, Vec> { + console_log!("compile_to_mono"); + use roc_reporting::report::{ can_problem, mono_problem, type_problem, RocDocAllocator, DEFAULT_PALETTE, }; @@ -56,6 +71,9 @@ pub fn compile_to_mono<'a>( let module_src = arena.alloc(promote_expr_to_module(src)); let exposed_types = MutMap::default(); + + console_log!("before load_and_monomorphize_from_str"); + let loaded = roc_load::file::load_and_monomorphize_from_str( arena, filename, diff --git a/repl_wasm/src/lib.rs b/repl_wasm/src/lib.rs index 48910defba..037572ba7f 100644 --- a/repl_wasm/src/lib.rs +++ b/repl_wasm/src/lib.rs @@ -31,7 +31,7 @@ extern "C" { fn js_get_result_and_memory(buffer_alloc_addr: *mut u8) -> usize; #[wasm_bindgen(js_namespace = console)] - fn log(s: &str); + pub fn log(s: &str); } // In-browser debugging @@ -158,8 +158,10 @@ impl<'a> ReplApp<'a> for WasmReplApp<'a> { #[wasm_bindgen] pub async fn entrypoint_from_js(src: String) -> Result { + console_log!("entrypoint_from_js"); let arena = &Bump::new(); let pre_linked_binary: &'static [u8] = include_bytes!("../data/pre_linked_binary.o"); + console_log!("pre_linked_binary {}", pre_linked_binary.len()); // Compile the app let target_info = TargetInfo::default_wasm32(); diff --git a/repl_www/build.sh b/repl_www/build.sh index 2b9c6e4023..f8ee4c63a0 100755 --- a/repl_www/build.sh +++ b/repl_www/build.sh @@ -17,7 +17,9 @@ WWW_DIR="repl_www/build" mkdir -p $WWW_DIR cp repl_www/public/* $WWW_DIR -# Pass all script arguments through to wasm-pack (such as --release) +# Pass all script arguments through to wasm-pack +# For debugging, pass the --profiling option, which enables optimizations + debug info +# (We need optimizations to get rid of dead code that otherwise causes compile errors!) wasm-pack build --target web "$@" repl_wasm cp repl_wasm/pkg/*.wasm $WWW_DIR