Merge pull request #3732 from rtfeldman/roc-load-cleanup

roc load cleanup
This commit is contained in:
Folkert de Vries 2022-08-09 15:37:13 +02:00 committed by GitHub
commit 4cb5de4428
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 35 deletions

View file

@ -128,4 +128,7 @@ flags! {
/// Print load phases as they complete.
ROC_PRINT_LOAD_LOG
/// Don't build and use the subs cache (speeds up compilation of load and previous crates)
ROC_SKIP_SUBS_CACHE
}

View file

@ -4,6 +4,13 @@ use bumpalo::Bump;
use roc_load_internal::file::{LoadingProblem, Threading};
use roc_module::symbol::ModuleId;
const SKIP_SUBS_CACHE: bool = {
match option_env!("ROC_SKIP_SUBS_CACHE") {
Some(s) => s.len() == 1 && s.as_bytes()[0] == b'1',
None => false,
}
};
const MODULES: &[(ModuleId, &str)] = &[
(ModuleId::BOOL, "Bool.roc"),
(ModuleId::RESULT, "Result.roc"),
@ -37,33 +44,39 @@ fn write_subs_for_module(module_id: ModuleId, filename: &str) {
let source = roc_builtins::roc::module_source(module_id);
let target_info = roc_target::TargetInfo::default_x86_64();
let res_module = roc_load_internal::file::load_and_typecheck_str(
&arena,
PathBuf::from(filename),
source,
src_dir,
Default::default(),
target_info,
roc_reporting::report::RenderTarget::ColorTerminal,
Threading::AllAvailable,
);
let module = match res_module {
Ok(v) => v,
Err(LoadingProblem::FormattedReport(report)) => {
panic!("{}", report);
}
Err(other) => {
panic!("build_file failed with error:\n{:?}", other);
}
};
let subs = module.solved.inner();
let exposed_vars_by_symbol: Vec<_> = module.exposed_to_host.into_iter().collect();
let mut output_path = PathBuf::from(std::env::var("OUT_DIR").unwrap());
output_path.extend(&[filename]);
output_path.set_extension("dat");
let mut file = std::fs::File::create(&output_path).unwrap();
subs.serialize(&exposed_vars_by_symbol, &mut file).unwrap();
if SKIP_SUBS_CACHE {
// write out a dummy file
std::fs::write(output_path, &[]).unwrap();
} else {
let res_module = roc_load_internal::file::load_and_typecheck_str(
&arena,
PathBuf::from(filename),
source,
src_dir,
Default::default(),
target_info,
roc_reporting::report::RenderTarget::ColorTerminal,
Threading::AllAvailable,
);
let module = match res_module {
Ok(v) => v,
Err(LoadingProblem::FormattedReport(report)) => {
panic!("{}", report);
}
Err(other) => {
panic!("build_file failed with error:\n{:?}", other);
}
};
let subs = module.solved.inner();
let exposed_vars_by_symbol: Vec<_> = module.exposed_to_host.into_iter().collect();
let mut file = std::fs::File::create(&output_path).unwrap();
subs.serialize(&exposed_vars_by_symbol, &mut file).unwrap();
}
}

View file

@ -7,6 +7,13 @@ use roc_target::TargetInfo;
use roc_types::subs::{Subs, Variable};
use std::path::PathBuf;
const SKIP_SUBS_CACHE: bool = {
match option_env!("ROC_SKIP_SUBS_CACHE") {
Some(s) => s.len() == 1 && s.as_bytes()[0] == b'1',
None => false,
}
};
pub use roc_load_internal::docs;
pub use roc_load_internal::file::{
EntryPoint, ExecutionMode, Expectations, LoadConfig, LoadResult, LoadStart, LoadedModule,
@ -147,7 +154,7 @@ fn read_cached_subs() -> MutMap<ModuleId, (Subs, Vec<(Symbol, Variable)>)> {
// Wasm seems to re-order definitions between build time and runtime, but only in release mode.
// That is very strange, but we can solve it separately
if !cfg!(target_family = "wasm") {
if !cfg!(target_family = "wasm") && !SKIP_SUBS_CACHE {
output.insert(ModuleId::BOOL, deserialize_help(BOOL));
output.insert(ModuleId::RESULT, deserialize_help(RESULT));
output.insert(ModuleId::NUM, deserialize_help(NUM));

View file

@ -892,7 +892,6 @@ struct State<'a> {
pub procedures: MutMap<(Symbol, ProcLayout<'a>), Proc<'a>>,
pub toplevel_expects: VecMap<Symbol, Region>,
pub exposed_to_host: ExposedToHost,
pub goal_phase: Phase,
/// This is the "final" list of IdentIds, after canonicalization and constraint gen
/// have completed for a given module.
@ -934,6 +933,10 @@ struct State<'a> {
type CachedSubs = Arc<Mutex<MutMap<ModuleId, (Subs, Vec<(Symbol, Variable)>)>>>;
impl<'a> State<'a> {
fn goal_phase(&self) -> Phase {
self.exec_mode.goal_phase()
}
#[allow(clippy::too_many_arguments)]
fn new(
root_id: ModuleId,
@ -948,15 +951,13 @@ impl<'a> State<'a> {
) -> Self {
let arc_shorthands = Arc::new(Mutex::new(MutMap::default()));
let goal_phase = exec_mode.goal_phase();
let dependencies = Dependencies::new(goal_phase);
let dependencies = Dependencies::new(exec_mode.goal_phase());
Self {
root_id,
root_subs: None,
target_info,
platform_data: None,
goal_phase,
output_path: None,
platform_path: PlatformPath::NotSpecified,
module_cache: ModuleCache::default(),
@ -2225,7 +2226,7 @@ fn update<'a>(
work.extend(state.dependencies.add_module(
header.module_id,
&header.package_qualified_imported_modules,
state.goal_phase,
state.exec_mode.goal_phase(),
));
state.module_cache.headers.insert(header.module_id, header);
@ -2378,7 +2379,7 @@ fn update<'a>(
.extend(solved_module.aliases.keys().copied());
}
if is_host_exposed && state.goal_phase == Phase::SolveTypes {
if is_host_exposed && state.goal_phase() == Phase::SolveTypes {
debug_assert!(work.is_empty());
debug_assert!(state.dependencies.solved_all());
@ -2420,7 +2421,7 @@ fn update<'a>(
},
);
if state.goal_phase > Phase::SolveTypes {
if state.goal_phase() > Phase::SolveTypes {
let layout_cache = state
.layout_caches
.pop()
@ -2507,7 +2508,7 @@ fn update<'a>(
layout_cache,
..
} => {
debug_assert!(state.goal_phase == Phase::MakeSpecializations);
debug_assert!(state.goal_phase() == Phase::MakeSpecializations);
log!("made specializations for {:?}", module_id);