diff --git a/ast/src/module.rs b/ast/src/module.rs index 769df5d162..59a02185be 100644 --- a/ast/src/module.rs +++ b/ast/src/module.rs @@ -1,10 +1,9 @@ +use bumpalo::Bump; +use roc_load::{LoadedModule, Threading}; +use roc_target::TargetInfo; use std::path::Path; -use bumpalo::Bump; -use roc_load::LoadedModule; -use roc_target::TargetInfo; - -pub fn load_module(src_file: &Path) -> LoadedModule { +pub fn load_module(src_file: &Path, threading: Threading) -> LoadedModule { let subs_by_module = Default::default(); let arena = Bump::new(); @@ -20,6 +19,7 @@ pub fn load_module(src_file: &Path) -> LoadedModule { subs_by_module, TargetInfo::default_x86_64(), roc_reporting::report::RenderTarget::ColorTerminal, + threading, ); match loaded { diff --git a/cli/src/build.rs b/cli/src/build.rs index be2bd0736e..12e11bc409 100644 --- a/cli/src/build.rs +++ b/cli/src/build.rs @@ -4,7 +4,7 @@ use roc_build::{ program::{self, Problems}, }; use roc_builtins::bitcode; -use roc_load::LoadingProblem; +use roc_load::{LoadingProblem, Threading}; use roc_mono::ir::OptLevel; use roc_reporting::report::RenderTarget; use roc_target::TargetInfo; @@ -40,6 +40,7 @@ pub fn build_file<'a>( surgically_link: bool, precompiled: bool, target_valgrind: bool, + threading: Threading, ) -> Result> { let compilation_start = SystemTime::now(); let target_info = TargetInfo::from(target); @@ -55,6 +56,7 @@ pub fn build_file<'a>( target_info, // TODO: expose this from CLI? RenderTarget::ColorTerminal, + threading, )?; use target_lexicon::Architecture; @@ -366,6 +368,7 @@ pub fn check_file( target_info, // TODO: expose this from CLI? RenderTarget::ColorTerminal, + Threading::Multi, )?; let buf = &mut String::with_capacity(1024); diff --git a/cli/src/lib.rs b/cli/src/lib.rs index b9d5e60f6a..d15e88e779 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -7,7 +7,7 @@ use clap::Command; use clap::{Arg, ArgMatches}; use roc_build::link::LinkType; use roc_error_macros::user_error; -use roc_load::LoadingProblem; +use roc_load::{LoadingProblem, Threading}; use roc_mono::ir::OptLevel; use std::env; use std::io; @@ -386,6 +386,7 @@ pub fn build(matches: &ArgMatches, config: BuildConfig) -> io::Result { surgically_link, precompiled, target_valgrind, + Threading::Multi, ); match res_binary_path { diff --git a/compiler/load/build.rs b/compiler/load/build.rs index ed424eb6eb..e0e5d643f8 100644 --- a/compiler/load/build.rs +++ b/compiler/load/build.rs @@ -1,6 +1,7 @@ use std::path::PathBuf; use bumpalo::Bump; +use roc_load_internal::file::Threading; use roc_module::symbol::ModuleId; const MODULES: &[(ModuleId, &str)] = &[ @@ -37,6 +38,7 @@ fn write_subs_for_module(module_id: ModuleId, filename: &str) { Default::default(), target_info, roc_reporting::report::RenderTarget::ColorTerminal, + Threading::Multi, ); let module = res_module.unwrap(); diff --git a/compiler/load/src/lib.rs b/compiler/load/src/lib.rs index 5d5c09daa4..bd5b215c4b 100644 --- a/compiler/load/src/lib.rs +++ b/compiler/load/src/lib.rs @@ -1,3 +1,5 @@ +pub use roc_load_internal::file::Threading; + use bumpalo::Bump; use roc_collections::all::MutMap; use roc_constrain::module::ExposedByModule; @@ -20,6 +22,7 @@ fn load<'a>( goal_phase: Phase, target_info: TargetInfo, render: RenderTarget, + threading: Threading, ) -> Result, LoadingProblem<'a>> { let cached_subs = read_cached_subs(); @@ -32,6 +35,7 @@ fn load<'a>( target_info, cached_subs, render, + threading, ) } @@ -67,6 +71,7 @@ pub fn load_and_monomorphize_from_str<'a>( exposed_types: ExposedByModule, target_info: TargetInfo, render: RenderTarget, + threading: Threading, ) -> Result, LoadingProblem<'a>> { use LoadResult::*; @@ -80,6 +85,7 @@ pub fn load_and_monomorphize_from_str<'a>( Phase::MakeSpecializations, target_info, render, + threading, )? { Monomorphized(module) => Ok(module), TypeChecked(_) => unreachable!(""), @@ -93,6 +99,7 @@ pub fn load_and_monomorphize<'a>( exposed_types: ExposedByModule, target_info: TargetInfo, render: RenderTarget, + threading: Threading, ) -> Result, LoadingProblem<'a>> { use LoadResult::*; @@ -106,6 +113,7 @@ pub fn load_and_monomorphize<'a>( Phase::MakeSpecializations, target_info, render, + threading, )? { Monomorphized(module) => Ok(module), TypeChecked(_) => unreachable!(""), @@ -119,6 +127,7 @@ pub fn load_and_typecheck<'a>( exposed_types: ExposedByModule, target_info: TargetInfo, render: RenderTarget, + threading: Threading, ) -> Result> { use LoadResult::*; @@ -132,6 +141,7 @@ pub fn load_and_typecheck<'a>( Phase::SolveTypes, target_info, render, + threading, )? { Monomorphized(_) => unreachable!(""), TypeChecked(module) => Ok(module), diff --git a/compiler/load_internal/src/file.rs b/compiler/load_internal/src/file.rs index fef510bdad..1905c85c52 100644 --- a/compiler/load_internal/src/file.rs +++ b/compiler/load_internal/src/file.rs @@ -956,6 +956,7 @@ pub fn load_and_typecheck_str<'a>( exposed_types: ExposedByModule, target_info: TargetInfo, render: RenderTarget, + threading: Threading, ) -> Result> { use LoadResult::*; @@ -974,6 +975,7 @@ pub fn load_and_typecheck_str<'a>( target_info, cached_subs, render, + threading, )? { Monomorphized(_) => unreachable!(""), TypeChecked(module) => Ok(module), @@ -1091,6 +1093,12 @@ pub enum LoadResult<'a> { Monomorphized(MonomorphizedModule<'a>), } +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub enum Threading { + Single, + Multi, +} + /// The loading process works like this, starting from the given filename (e.g. "main.roc"): /// /// 1. Open the file. @@ -1144,10 +1152,11 @@ pub fn load<'a>( target_info: TargetInfo, cached_subs: MutMap)>, render: RenderTarget, + threading: Threading, ) -> Result, LoadingProblem<'a>> { // When compiling to wasm, we cannot spawn extra threads // so we have a single-threaded implementation - if cfg!(target_family = "wasm") { + if threading == Threading::Single || cfg!(target_family = "wasm") { load_single_threaded( arena, load_start, diff --git a/compiler/load_internal/tests/test_load.rs b/compiler/load_internal/tests/test_load.rs index ae018333c1..de77f89913 100644 --- a/compiler/load_internal/tests/test_load.rs +++ b/compiler/load_internal/tests/test_load.rs @@ -19,6 +19,7 @@ mod test_load { use roc_can::def::Declaration::*; use roc_can::def::Def; use roc_constrain::module::ExposedByModule; + use roc_load_internal::file::Threading; use roc_load_internal::file::{LoadResult, LoadStart, LoadedModule, LoadingProblem, Phase}; use roc_module::ident::ModuleName; use roc_module::symbol::{Interns, ModuleId}; @@ -53,6 +54,7 @@ mod test_load { target_info, Default::default(), // these tests will re-compile the builtins RenderTarget::Generic, + Threading::Single, )? { Monomorphized(_) => unreachable!(""), TypeChecked(module) => Ok(module), diff --git a/compiler/test_gen/src/helpers/dev.rs b/compiler/test_gen/src/helpers/dev.rs index 1210ce98e3..ab7ce02c2d 100644 --- a/compiler/test_gen/src/helpers/dev.rs +++ b/compiler/test_gen/src/helpers/dev.rs @@ -2,6 +2,7 @@ use libloading::Library; use roc_build::link::{link, LinkType}; use roc_builtins::bitcode; use roc_collections::all::MutMap; +use roc_load::Threading; use roc_region::all::LineInfo; use tempfile::tempdir; @@ -55,6 +56,7 @@ pub fn helper( Default::default(), roc_target::TargetInfo::default_x86_64(), roc_reporting::report::RenderTarget::ColorTerminal, + Threading::Single, ); let mut loaded = loaded.expect("failed to load module"); diff --git a/compiler/test_gen/src/helpers/llvm.rs b/compiler/test_gen/src/helpers/llvm.rs index aeba51ad8c..d4c89f25e6 100644 --- a/compiler/test_gen/src/helpers/llvm.rs +++ b/compiler/test_gen/src/helpers/llvm.rs @@ -5,6 +5,7 @@ use roc_build::link::module_to_dylib; use roc_build::program::FunctionIterator; use roc_collections::all::MutSet; use roc_gen_llvm::llvm::externs::add_default_roc_externs; +use roc_load::Threading; use roc_mono::ir::OptLevel; use roc_region::all::LineInfo; use roc_reporting::report::RenderTarget; @@ -59,6 +60,7 @@ fn create_llvm_module<'a>( Default::default(), target_info, RenderTarget::ColorTerminal, + Threading::Multi, ); let mut loaded = match loaded { diff --git a/compiler/test_mono/src/tests.rs b/compiler/test_mono/src/tests.rs index bcbaa4812f..1f64689c9d 100644 --- a/compiler/test_mono/src/tests.rs +++ b/compiler/test_mono/src/tests.rs @@ -16,9 +16,9 @@ const EXPANDED_STACK_SIZE: usize = 8 * 1024 * 1024; use test_mono_macros::*; use roc_collections::all::MutMap; +use roc_load::Threading; use roc_module::symbol::Symbol; use roc_mono::ir::Proc; - use roc_mono::ir::ProcLayout; const TARGET_INFO: roc_target::TargetInfo = roc_target::TargetInfo::default_x86_64(); @@ -99,6 +99,7 @@ fn compiles_to_ir(test_name: &str, src: &str) { Default::default(), TARGET_INFO, roc_reporting::report::RenderTarget::Generic, + Threading::Single, ); let mut loaded = match loaded { diff --git a/docs/src/lib.rs b/docs/src/lib.rs index 4a22876060..bff952e22a 100644 --- a/docs/src/lib.rs +++ b/docs/src/lib.rs @@ -10,7 +10,7 @@ use roc_highlight::highlight_parser::{highlight_defs, highlight_expr}; use roc_load::docs::DocEntry::DocDef; use roc_load::docs::{DocEntry, TypeAnnotation}; use roc_load::docs::{ModuleDocumentation, RecordField}; -use roc_load::{LoadedModule, LoadingProblem}; +use roc_load::{LoadedModule, LoadingProblem, Threading}; use roc_module::symbol::{IdentIdsByModule, Interns, ModuleId}; use roc_parse::ident::{parse_ident, Ident}; use roc_parse::state::State; @@ -435,6 +435,7 @@ pub fn load_modules_for_files(filenames: Vec) -> Vec { Default::default(), roc_target::TargetInfo::default_x86_64(), // This is just type-checking for docs, so "target" doesn't matter roc_reporting::report::RenderTarget::ColorTerminal, + Threading::Multi, ) { Ok(loaded) => modules.push(loaded), Err(LoadingProblem::FormattedReport(report)) => { diff --git a/editor/src/editor/main.rs b/editor/src/editor/main.rs index cac5843445..8c6219d984 100644 --- a/editor/src/editor/main.rs +++ b/editor/src/editor/main.rs @@ -26,6 +26,7 @@ use pipelines::RectResources; use roc_ast::lang::env::Env; use roc_ast::mem_pool::pool::Pool; use roc_ast::module::load_module; +use roc_load::Threading; use roc_module::symbol::IdentIds; use roc_types::subs::VarStore; use std::collections::HashSet; @@ -128,7 +129,7 @@ fn run_event_loop(project_dir_path_opt: Option<&Path>) -> Result<(), Box( exposed_types, target_info, roc_reporting::report::RenderTarget::ColorTerminal, + Threading::Single, ); let mut loaded = match loaded { diff --git a/reporting/tests/test_reporting.rs b/reporting/tests/test_reporting.rs index a88ad14ecb..fa950fdaac 100644 --- a/reporting/tests/test_reporting.rs +++ b/reporting/tests/test_reporting.rs @@ -12,7 +12,7 @@ mod test_reporting { use bumpalo::Bump; use indoc::indoc; use roc_can::abilities::AbilitiesStore; - use roc_load::{self, LoadedModule, LoadingProblem}; + use roc_load::{self, LoadedModule, LoadingProblem, Threading}; use roc_module::symbol::{Interns, ModuleId}; use roc_region::all::LineInfo; use roc_reporting::report::{ @@ -92,6 +92,7 @@ mod test_reporting { exposed_types, roc_target::TargetInfo::default_x86_64(), RenderTarget::Generic, + Threading::Single, ); drop(file);