Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove dead code in the assembler. #531

Merged
merged 1 commit into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 4 additions & 28 deletions kclvm/runner/src/assembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,10 @@ use std::{
collections::HashMap,
env,
path::{Path, PathBuf},
sync::mpsc::channel,
};
use threadpool::ThreadPool;

/// IR code file suffix.
const DEFAULT_IR_FILE: &str = "_a.out";
/// Default codegen timeout.
const DEFAULT_TIME_OUT: u64 = 50;
/// Default thread count.
const DEFAULT_THREAD_COUNT: usize = 1;

/// LibAssembler trait is used to indicate the general interface
/// that must be implemented when different intermediate codes are assembled
Expand Down Expand Up @@ -189,7 +183,6 @@ impl LibAssembler for LlvmLibAssembler {
/// KclvmAssembler provides an atomic operation for generating a dynamic link library for a single file
/// through KclvmLibAssembler for each thread.
pub(crate) struct KclvmAssembler {
thread_count: usize,
program: ast::Program,
scope: ProgramScope,
entry_file: String,
Expand All @@ -208,7 +201,6 @@ impl KclvmAssembler {
single_file_assembler: KclvmLibAssembler,
) -> Self {
Self {
thread_count: DEFAULT_THREAD_COUNT,
program,
scope,
entry_file,
Expand Down Expand Up @@ -298,11 +290,8 @@ impl KclvmAssembler {
),
);
}
let pool = ThreadPool::new(self.thread_count);
let (tx, rx) = channel();
let prog_count = compile_progs.len();
let mut lib_paths = vec![];
for (pkgpath, (compile_prog, import_names, cache_dir)) in compile_progs {
let tx = tx.clone();
// Clone a single file assembler for one thread.
let assembler = self.single_file_assembler.clone();
// Generate paths for some intermediate files (*.o, *.lock).
Expand All @@ -319,7 +308,7 @@ impl KclvmAssembler {
let code_file_path = assembler.add_code_file_suffix(&code_file);
let lock_file_path = format!("{}.lock", code_file_path);
let target = self.target.clone();
pool.execute(move || {
{
// Locking file for parallel code generation.
let mut file_lock = fslock::LockFile::open(&lock_file_path)
.unwrap_or_else(|_| panic!("{} not found", lock_file_path));
Expand Down Expand Up @@ -377,21 +366,8 @@ impl KclvmAssembler {
}
};
file_lock.unlock().unwrap();
tx.send(file_path)
.expect("channel will be there waiting for the pool");
});
}
// Get all codegen results from the channel with timeout
let timeout: u64 = match env::var("KCLVM_CODE_GEN_TIMEOUT") {
Ok(timeout_str) => timeout_str.parse().unwrap_or(DEFAULT_TIME_OUT),
Err(_) => DEFAULT_TIME_OUT,
};
let mut lib_paths = vec![];
for _ in 0..prog_count {
let lib_path = rx
.recv_timeout(std::time::Duration::from_secs(timeout))
.unwrap();
lib_paths.push(lib_path);
lib_paths.push(file_path);
};
}
self.single_file_assembler.clean_lock_file(&self.entry_file);
lib_paths
Expand Down
38 changes: 0 additions & 38 deletions kclvm/runner/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ use kclvm_parser::load_program;
use kclvm_parser::ParseSession;
use kclvm_sema::resolver::resolve_program;
use std::fs::create_dir_all;
use std::panic::catch_unwind;
use std::panic::set_hook;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::thread;
Expand Down Expand Up @@ -296,38 +294,6 @@ fn test_kclvm_runner_execute() {
}
}

fn test_kclvm_runner_execute_timeout() {
set_hook(Box::new(|_| {}));
let result_time_out = catch_unwind(|| {
gen_libs_for_test(
&Path::new("test")
.join("no_exist_path")
.display()
.to_string(),
&Path::new(".")
.join("src")
.join("test_datas")
.join("multi_file_compilation")
.join("import_abs_path")
.join("app-main")
.join("main.k")
.display()
.to_string(),
);
});
let timeout_panic_msg = "called `Result::unwrap()` on an `Err` value: Timeout";
match result_time_out {
Err(panic_err) => {
if let Some(s) = panic_err.downcast_ref::<String>() {
assert_eq!(s, timeout_panic_msg)
}
}
_ => {
unreachable!()
}
}
}

#[test]
fn test_assemble_lib_llvm() {
for case in TEST_CASES {
Expand Down Expand Up @@ -554,10 +520,6 @@ fn test_exec() {
test_kclvm_runner_execute();
println!("test_kclvm_runner_execute - PASS");

test_kclvm_runner_execute_timeout();
println!("test_kclvm_runner_execute_timeout - PASS");
fs::remove_dir_all(Path::new("__main__")).unwrap();

test_custom_manifests_output();
println!("test_custom_manifests_output - PASS");

Expand Down