From 7fc2a30c147dd151b7a01b8cd2d936b07f4a3e49 Mon Sep 17 00:00:00 2001 From: David Mak Date: Fri, 13 Oct 2023 11:28:30 +0800 Subject: [PATCH] Force single-threaded compilation if LLVM is not thread-safe --- nac3artiq/src/lib.rs | 4 +++- nac3standalone/src/main.rs | 18 +++++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/nac3artiq/src/lib.rs b/nac3artiq/src/lib.rs index 68e837bd..bef1e73a 100644 --- a/nac3artiq/src/lib.rs +++ b/nac3artiq/src/lib.rs @@ -9,6 +9,7 @@ use inkwell::{ memory_buffer::MemoryBuffer, module::{Linkage, Module}, passes::PassBuilderOptions, + support::is_multithreaded, targets::*, OptimizationLevel, }; @@ -582,7 +583,8 @@ impl Nac3 { membuffer.lock().push(buffer); }))); let size_t = if self.isa == Isa::Host { 64 } else { 32 }; - let thread_names: Vec = (0..4).map(|_| "main".to_string()).collect(); + let num_threads = if is_multithreaded() { 4 } else { 1 }; + let thread_names: Vec = (0..num_threads).map(|_| "main".to_string()).collect(); let threads: Vec<_> = thread_names .iter() .map(|s| Box::new(ArtiqCodeGenerator::new(s.to_string(), size_t, self.time_fns))) diff --git a/nac3standalone/src/main.rs b/nac3standalone/src/main.rs index 97d8d9ec..9deb34ec 100644 --- a/nac3standalone/src/main.rs +++ b/nac3standalone/src/main.rs @@ -2,6 +2,7 @@ use clap::Parser; use inkwell::{ memory_buffer::MemoryBuffer, passes::PassBuilderOptions, + support::is_multithreaded, targets::*, OptimizationLevel, }; @@ -210,12 +211,19 @@ fn main() { .map(|arg| if arg == "native" { host_target_machine.cpu.clone() } else { arg }) .unwrap_or_default(); let target_features = target_features.unwrap_or_default(); - let threads = if threads == 0 { - std::thread::available_parallelism() - .map(|threads| threads.get() as u32) - .unwrap_or(1u32) + let threads = if is_multithreaded() { + if threads == 0 { + std::thread::available_parallelism() + .map(|threads| threads.get() as u32) + .unwrap_or(1u32) + } else { + threads + } } else { - threads + if threads != 1 { + println!("Warning: Number of threads specified in command-line but multithreading is disabled in LLVM at build time! Defaulting to single-threaded compilation") + } + 1 }; let opt_level = match opt_level { 0 => OptimizationLevel::None,