Skip to content

Commit

Permalink
feat: clear globalstate cache
Browse files Browse the repository at this point in the history
Signed-off-by: he1pa <18012015693@163.com>
  • Loading branch information
He1pa committed Apr 12, 2024
1 parent 3d03cd4 commit 29bfa67
Show file tree
Hide file tree
Showing 23 changed files with 513 additions and 206 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 @@ -212,7 +212,7 @@ impl KclvmServiceImpl {
},
module_cache,
scope_cache,
GlobalState::default(),
&mut GlobalState::default(),
)?;
if args.with_ast_index {
// Thread local options
Expand Down
10 changes: 6 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,10 @@ 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());
// let gs = Namer::find_symbols(&program, gs);
Namer::find_symbols(&program, gs);
// let gs = AdvancedResolver::resolve_program(&program, gs, node_ty_map.clone());
AdvancedResolver::resolve_program(&program, gs, node_ty_map);
(program, prog_scope.handler.diagnostics.clone(), gs)
} else {
(parse_result.program, IndexSet::default(), gs)
Expand Down
41 changes: 24 additions & 17 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, time::Instant};

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,
) -> GlobalState {
gs: &'ctx mut GlobalState,
node_ty_map: Rc<RefCell<NodeTyMap>>,
) {
let mut advanced_resolver = Self {
gs,
ctx: Context {
Expand All @@ -111,9 +113,8 @@ 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){
if !advanced_resolver.gs.new_or_invalidate_pkgs.contains(name) {
continue;
}
advanced_resolver.ctx.current_pkgpath = Some(name.clone());
Expand All @@ -139,7 +140,7 @@ impl<'ctx> AdvancedResolver<'ctx> {

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

fn enter_root_scope(
Expand Down Expand Up @@ -294,8 +295,9 @@ 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();
// let gs = Namer::find_symbols(&program, gs);
Namer::find_symbols(&program, &mut gs);

let node_ty_map = resolver::resolve_program_with_opts(
&mut program,
Expand All @@ -307,7 +309,8 @@ mod tests {
None,
)
.node_ty_map;
let gs = AdvancedResolver::resolve_program(&program, gs, node_ty_map);
// let gs = AdvancedResolver::resolve_program(&program, gs, node_ty_map);
AdvancedResolver::resolve_program(&program, &mut gs, node_ty_map);
let base_path = Path::new(".").canonicalize().unwrap();
// print_symbols_info(&gs);
let except_symbols = vec![
Expand Down Expand Up @@ -1233,10 +1236,12 @@ 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();
// let gs = Namer::find_symbols(&program, gs);
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);
// let gs = AdvancedResolver::resolve_program(&program, gs, node_ty_map);
AdvancedResolver::resolve_program(&program, &mut gs, node_ty_map);
let base_path = Path::new(".").canonicalize().unwrap();

let test_cases = vec![
Expand Down Expand Up @@ -1310,10 +1315,12 @@ 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();
// let gs = Namer::find_symbols(&program, gs);
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);
// let gs = AdvancedResolver::resolve_program(&program, gs, node_ty_map);
AdvancedResolver::resolve_program(&program, &mut gs, node_ty_map);
let base_path = Path::new(".").canonicalize().unwrap();

let scope_test_cases = vec![
Expand Down
Loading

0 comments on commit 29bfa67

Please sign in to comment.