Skip to content

Commit

Permalink
feat: advance resolver incremental compile (#1209)
Browse files Browse the repository at this point in the history
feat: advanced resolver incremental compile. 1. Namer and Advanced Resolver support incremental compilation, clear cache by pkg and only update new pkg
2. Add gs cache in lsp state
3. Namer and Advanced Resolver modify gs in place(&mut) to reduce clone

Signed-off-by: he1pa <[email protected]>
  • Loading branch information
He1pa authored Jun 3, 2024
1 parent d0c1eae commit 61477e8
Show file tree
Hide file tree
Showing 23 changed files with 538 additions and 178 deletions.
2 changes: 1 addition & 1 deletion kclvm/api/src/service/service_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ impl KclvmServiceImpl {
},
module_cache,
scope_cache,
GlobalState::default(),
&mut GlobalState::default(),
)?;
if args.with_ast_index {
// Thread local options
Expand Down
8 changes: 4 additions & 4 deletions kclvm/loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub fn load_packages(opts: &LoadPackageOptions) -> Result<Packages> {
opts,
KCLModuleCache::default(),
KCLScopeCache::default(),
GlobalState::default(),
&mut GlobalState::default(),
)
}

Expand All @@ -126,7 +126,7 @@ pub fn load_packages_with_cache(
opts: &LoadPackageOptions,
module_cache: KCLModuleCache,
scope_cache: KCLScopeCache,
gs: GlobalState,
gs: &mut GlobalState,
) -> Result<Packages> {
let sess = ParseSessionRef::default();
let paths: Vec<&str> = opts.paths.iter().map(|s| s.as_str()).collect();
Expand All @@ -149,8 +149,8 @@ pub fn load_packages_with_cache(
Some(scope_cache),
);
let node_ty_map = prog_scope.node_ty_map;
let gs = Namer::find_symbols(&program, gs);
let gs = AdvancedResolver::resolve_program(&program, gs, node_ty_map.clone())?;
Namer::find_symbols(&program, gs);
AdvancedResolver::resolve_program(&program, gs, node_ty_map.clone())?;
(program, prog_scope.handler.diagnostics.clone(), gs)
} else {
(parse_result.program, IndexSet::default(), gs)
Expand Down
37 changes: 21 additions & 16 deletions kclvm/sema/src/advanced_resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
└─────────────────────┘
*/

use std::{cell::RefCell, rc::Rc};

use indexmap::IndexSet;
use kclvm_error::Position;

Expand All @@ -60,12 +62,12 @@ mod node;
/// so that toolchain can query semantic information about the AST
pub struct AdvancedResolver<'ctx> {
pub(crate) ctx: Context<'ctx>,
pub(crate) gs: GlobalState,
pub(crate) gs: &'ctx mut GlobalState,
}

pub struct Context<'ctx> {
pub program: &'ctx Program,
node_ty_map: NodeTyMap,
node_ty_map: Rc<RefCell<NodeTyMap>>,
scopes: Vec<ScopeRef>,
current_pkgpath: Option<String>,
current_filename: Option<String>,
Expand Down Expand Up @@ -93,9 +95,9 @@ impl<'ctx> Context<'ctx> {
impl<'ctx> AdvancedResolver<'ctx> {
pub fn resolve_program(
program: &'ctx Program,
gs: GlobalState,
node_ty_map: NodeTyMap,
) -> anyhow::Result<GlobalState> {
gs: &'ctx mut GlobalState,
node_ty_map: Rc<RefCell<NodeTyMap>>,
) -> anyhow::Result<()> {
let mut advanced_resolver = Self {
gs,
ctx: Context {
Expand All @@ -111,8 +113,10 @@ impl<'ctx> AdvancedResolver<'ctx> {
maybe_def: false,
},
};

for (name, modules) in advanced_resolver.ctx.program.pkgs.iter() {
if !advanced_resolver.gs.new_or_invalidate_pkgs.contains(name) {
continue;
}
advanced_resolver.ctx.current_pkgpath = Some(name.clone());
if let Some(pkg_info) = advanced_resolver.gs.get_packages().get_package_info(name) {
if modules.is_empty() {
Expand All @@ -135,7 +139,8 @@ impl<'ctx> AdvancedResolver<'ctx> {
}

advanced_resolver.gs.build_sema_db();
Ok(advanced_resolver.gs)
advanced_resolver.gs.new_or_invalidate_pkgs.clear();
Ok(())
}

fn enter_root_scope(
Expand Down Expand Up @@ -290,8 +295,8 @@ mod tests {
let mut program = load_program(sess.clone(), &[&path], None, None)
.unwrap()
.program;
let gs = GlobalState::default();
let gs = Namer::find_symbols(&program, gs);
let mut gs = GlobalState::default();
Namer::find_symbols(&program, &mut gs);

let node_ty_map = resolver::resolve_program_with_opts(
&mut program,
Expand All @@ -303,7 +308,7 @@ mod tests {
None,
)
.node_ty_map;
let gs = AdvancedResolver::resolve_program(&program, gs, node_ty_map).unwrap();
AdvancedResolver::resolve_program(&program, &mut gs, node_ty_map).unwrap();
let base_path = Path::new(".").canonicalize().unwrap();
// print_symbols_info(&gs);
let except_symbols = vec![
Expand Down Expand Up @@ -1229,10 +1234,10 @@ mod tests {
let mut program = load_program(sess.clone(), &[&path], None, None)
.unwrap()
.program;
let gs = GlobalState::default();
let gs = Namer::find_symbols(&program, gs);
let mut gs = GlobalState::default();
Namer::find_symbols(&program, &mut gs);
let node_ty_map = resolver::resolve_program(&mut program).node_ty_map;
let gs = AdvancedResolver::resolve_program(&program, gs, node_ty_map).unwrap();
AdvancedResolver::resolve_program(&program, &mut gs, node_ty_map).unwrap();
let base_path = Path::new(".").canonicalize().unwrap();

let test_cases = vec![
Expand Down Expand Up @@ -1306,10 +1311,10 @@ mod tests {
let mut program = load_program(sess.clone(), &[&path], None, None)
.unwrap()
.program;
let gs = GlobalState::default();
let gs = Namer::find_symbols(&program, gs);
let mut gs = GlobalState::default();
Namer::find_symbols(&program, &mut gs);
let node_ty_map = resolver::resolve_program(&mut program).node_ty_map;
let gs = AdvancedResolver::resolve_program(&program, gs, node_ty_map).unwrap();
AdvancedResolver::resolve_program(&program, &mut gs, node_ty_map).unwrap();
let base_path = Path::new(".").canonicalize().unwrap();

let scope_test_cases = vec![
Expand Down
Loading

0 comments on commit 61477e8

Please sign in to comment.