From 8d88ad2dae3d99fe7a404f2fa9ad43614c930ce7 Mon Sep 17 00:00:00 2001 From: z78078 Date: Mon, 4 Jul 2022 16:02:58 +0800 Subject: [PATCH 1/8] Bring lazy_static to the nac3core project --- nac3core/Cargo.toml | 1 + nac3core/src/lib.rs | 3 +++ 2 files changed, 4 insertions(+) diff --git a/nac3core/Cargo.toml b/nac3core/Cargo.toml index ec4cd3c1..12a72484 100644 --- a/nac3core/Cargo.toml +++ b/nac3core/Cargo.toml @@ -10,6 +10,7 @@ crossbeam = "0.8.1" parking_lot = "0.11.1" rayon = "1.5.1" nac3parser = { path = "../nac3parser" } +lazy_static = "1.4.0" [dependencies.inkwell] git = "https://github.com/TheDan64/inkwell.git" diff --git a/nac3core/src/lib.rs b/nac3core/src/lib.rs index bff6b5cc..ec45cbe9 100644 --- a/nac3core/src/lib.rs +++ b/nac3core/src/lib.rs @@ -1,6 +1,9 @@ #![warn(clippy::all)] #![allow(dead_code)] +#[macro_use] +extern crate lazy_static; + pub mod codegen; pub mod symbol_resolver; pub mod toplevel; -- 2.44.2 From e7f9a024bd389efdd753ac8d114be6eb080317e3 Mon Sep 17 00:00:00 2001 From: z78078 Date: Mon, 4 Jul 2022 16:03:33 +0800 Subject: [PATCH 2/8] modifed Cargo.lock --- Cargo.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.lock b/Cargo.lock index f53b5bfd..97aba67e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -521,6 +521,7 @@ dependencies = [ "inkwell", "insta", "itertools", + "lazy_static", "nac3parser", "parking_lot 0.11.2", "rayon", -- 2.44.2 From 1cf99a6499909ac1bddcd769d554d94cc23d29e5 Mon Sep 17 00:00:00 2001 From: z78078 Date: Mon, 4 Jul 2022 16:05:22 +0800 Subject: [PATCH 3/8] Add Mutex and critical section for the suspicious lines --- nac3core/src/codegen/mod.rs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/nac3core/src/codegen/mod.rs b/nac3core/src/codegen/mod.rs index fbd70150..c47398b4 100644 --- a/nac3core/src/codegen/mod.rs +++ b/nac3core/src/codegen/mod.rs @@ -52,6 +52,10 @@ pub struct StaticValueStore { pub type VarValue<'ctx> = (PointerValue<'ctx>, Option>, i64); +lazy_static!( + static ref PASSES_INIT_LOCK: Mutex = Mutex::new(AtomicBool::new(true)); +); + pub struct CodeGenContext<'ctx, 'a> { pub ctx: &'ctx Context, pub builder: Builder<'ctx>, @@ -144,13 +148,16 @@ impl WorkerRegistry { }); let mut handles = Vec::new(); + for mut generator in generators.into_iter() { let registry = registry.clone(); let registry2 = registry.clone(); let f = f.clone(); + let handle = thread::spawn(move || { registry.worker_thread(generator.as_mut(), f); }); + let handle = thread::spawn(move || { if let Err(e) = handle.join() { if let Some(e) = e.downcast_ref::<&'static str>() { @@ -162,6 +169,7 @@ impl WorkerRegistry { registry2.wait_condvar.notify_all(); } }); + handles.push(handle); } (registry, handles) @@ -202,11 +210,13 @@ impl WorkerRegistry { self.sender.send(Some(task)).unwrap(); } - fn worker_thread(&self, generator: &mut G, f: Arc) { + fn worker_thread( + &self, generator: + &mut G, f: Arc + ) { let context = Context::create(); let mut builder = context.create_builder(); let module = context.create_module(generator.get_name()); - module.add_basic_value_flag( "Debug Info Version", inkwell::module::FlagBehavior::Warning, @@ -218,10 +228,14 @@ impl WorkerRegistry { context.i32_type().const_int(4, false), ); - let pass_builder = PassManagerBuilder::create(); - pass_builder.set_optimization_level(OptimizationLevel::Default); let passes = PassManager::create(&module); - pass_builder.populate_function_pass_manager(&passes); + + { + let _data = PASSES_INIT_LOCK.lock(); + let pass_builder = PassManagerBuilder::create(); + pass_builder.set_optimization_level(OptimizationLevel::Default); + pass_builder.populate_function_pass_manager(&passes); + } let mut errors = HashSet::new(); while let Some(task) = self.receiver.recv().unwrap() { @@ -243,7 +257,6 @@ impl WorkerRegistry { if !errors.is_empty() { panic!("Codegen error: {}", errors.into_iter().sorted().join("\n----------\n")); } - let result = module.verify(); if let Err(err) = result { println!("{}", module.print_to_string().to_str().unwrap()); -- 2.44.2 From 4ea6f1e9288714b7366e1d6726e04ae7f7906e18 Mon Sep 17 00:00:00 2001 From: z78078 Date: Mon, 4 Jul 2022 16:17:04 +0800 Subject: [PATCH 4/8] update comments for the changes on nac3core/src/codegen/mod.rs --- nac3core/src/codegen/mod.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/nac3core/src/codegen/mod.rs b/nac3core/src/codegen/mod.rs index c47398b4..93271d53 100644 --- a/nac3core/src/codegen/mod.rs +++ b/nac3core/src/codegen/mod.rs @@ -53,6 +53,8 @@ pub struct StaticValueStore { pub type VarValue<'ctx> = (PointerValue<'ctx>, Option>, i64); lazy_static!( + // HACK: The Mutex is a work-around for issue + // https://git.m-labs.hk/M-Labs/nac3/issues/275 static ref PASSES_INIT_LOCK: Mutex = Mutex::new(AtomicBool::new(true)); ); @@ -230,7 +232,13 @@ impl WorkerRegistry { let passes = PassManager::create(&module); + + // HACK: This critical section is a work-around for issue + // https://git.m-labs.hk/M-Labs/nac3/issues/275 { + // the variable holder `_data` is required even we are not using the + // variable. The lock will release itself if the variable `_data` is not + // there let _data = PASSES_INIT_LOCK.lock(); let pass_builder = PassManagerBuilder::create(); pass_builder.set_optimization_level(OptimizationLevel::Default); -- 2.44.2 From 8fb413a7bb57b43a4e31c5f67af0a0ee12504b4a Mon Sep 17 00:00:00 2001 From: z78078 Date: Mon, 4 Jul 2022 16:33:03 +0800 Subject: [PATCH 5/8] remove the style changes in nac3core/src/codegen/mod.rs --- nac3core/src/codegen/mod.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/nac3core/src/codegen/mod.rs b/nac3core/src/codegen/mod.rs index 2b67f32e..a9c9dc33 100644 --- a/nac3core/src/codegen/mod.rs +++ b/nac3core/src/codegen/mod.rs @@ -159,7 +159,6 @@ impl WorkerRegistry { let handle = thread::spawn(move || { registry.worker_thread(generator.as_mut(), f); }); - let handle = thread::spawn(move || { if let Err(e) = handle.join() { if let Some(e) = e.downcast_ref::<&'static str>() { @@ -171,7 +170,6 @@ impl WorkerRegistry { registry2.wait_condvar.notify_all(); } }); - handles.push(handle); } (registry, handles) @@ -212,10 +210,7 @@ impl WorkerRegistry { self.sender.send(Some(task)).unwrap(); } - fn worker_thread( - &self, generator: - &mut G, f: Arc - ) { + fn worker_thread(&self, generator: &mut G, f: Arc) { let context = Context::create(); let mut builder = context.create_builder(); let mut module = context.create_module(generator.get_name()); -- 2.44.2 From c0e035f2f88e4328b4b30424e6922657fcc997ea Mon Sep 17 00:00:00 2001 From: z78078 Date: Mon, 4 Jul 2022 16:35:17 +0800 Subject: [PATCH 6/8] remove the style changes in nac3core/src/codegen/mod.rs --- nac3core/src/codegen/mod.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/nac3core/src/codegen/mod.rs b/nac3core/src/codegen/mod.rs index a9c9dc33..a0cbda1b 100644 --- a/nac3core/src/codegen/mod.rs +++ b/nac3core/src/codegen/mod.rs @@ -150,12 +150,10 @@ impl WorkerRegistry { }); let mut handles = Vec::new(); - for mut generator in generators.into_iter() { let registry = registry.clone(); let registry2 = registry.clone(); let f = f.clone(); - let handle = thread::spawn(move || { registry.worker_thread(generator.as_mut(), f); }); @@ -228,7 +226,6 @@ impl WorkerRegistry { let passes = PassManager::create(&module); - // HACK: This critical section is a work-around for issue // https://git.m-labs.hk/M-Labs/nac3/issues/275 { @@ -259,6 +256,7 @@ impl WorkerRegistry { if !errors.is_empty() { panic!("Codegen error: {}", errors.into_iter().sorted().join("\n----------\n")); } + let result = module.verify(); if let Err(err) = result { println!("{}", module.print_to_string().to_str().unwrap()); -- 2.44.2 From af99915ead2b9c7eb6eed0c268a9f24e68a8e22b Mon Sep 17 00:00:00 2001 From: z78078 Date: Mon, 4 Jul 2022 18:02:09 +0800 Subject: [PATCH 7/8] refractor the import statement --- nac3core/src/codegen/mod.rs | 3 +++ nac3core/src/lib.rs | 3 --- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/nac3core/src/codegen/mod.rs b/nac3core/src/codegen/mod.rs index a0cbda1b..4e9ed219 100644 --- a/nac3core/src/codegen/mod.rs +++ b/nac3core/src/codegen/mod.rs @@ -52,6 +52,9 @@ pub struct StaticValueStore { pub type VarValue<'ctx> = (PointerValue<'ctx>, Option>, i64); + +use lazy_static::lazy_static; + lazy_static!( // HACK: The Mutex is a work-around for issue // https://git.m-labs.hk/M-Labs/nac3/issues/275 diff --git a/nac3core/src/lib.rs b/nac3core/src/lib.rs index ec45cbe9..bff6b5cc 100644 --- a/nac3core/src/lib.rs +++ b/nac3core/src/lib.rs @@ -1,9 +1,6 @@ #![warn(clippy::all)] #![allow(dead_code)] -#[macro_use] -extern crate lazy_static; - pub mod codegen; pub mod symbol_resolver; pub mod toplevel; -- 2.44.2 From 35d81f6fdecafae6f1efd22a5f6cd68150bcdbb8 Mon Sep 17 00:00:00 2001 From: z78078 Date: Mon, 4 Jul 2022 18:05:40 +0800 Subject: [PATCH 8/8] refractor --- nac3core/src/codegen/mod.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/nac3core/src/codegen/mod.rs b/nac3core/src/codegen/mod.rs index 4e9ed219..a7be388b 100644 --- a/nac3core/src/codegen/mod.rs +++ b/nac3core/src/codegen/mod.rs @@ -31,6 +31,7 @@ use std::sync::{ Arc, }; use std::thread; +use lazy_static::lazy_static; pub mod concrete_type; pub mod expr; @@ -52,9 +53,6 @@ pub struct StaticValueStore { pub type VarValue<'ctx> = (PointerValue<'ctx>, Option>, i64); - -use lazy_static::lazy_static; - lazy_static!( // HACK: The Mutex is a work-around for issue // https://git.m-labs.hk/M-Labs/nac3/issues/275 -- 2.44.2