Compare commits

..

6 Commits

Author SHA1 Message Date
David Mak a034844b1a flake: Enable thread-safe mode for LLVM
This is required as we use the LLVM APIs from multiple threads.
2023-10-13 15:02:50 +08:00
David Mak 3209f88adf Force single-threaded compilation if LLVM is not thread-safe 2023-10-13 15:02:48 +08:00
David Mak af77898390 standalone: Update help text for `--emit-llvm` 2023-10-13 15:00:39 +08:00
David Mak 8c7cf4f8f3 standalone: Fix run_demo script
- Link main and module*.bc together if using multiple threads
- Fix temporary files not being deleted
2023-10-13 14:57:16 +08:00
David Mak 48eb64403f standalone: Treat -T0 as using all available threads 2023-10-13 14:57:16 +08:00
David Mak 2c44b58bb8 standalone: Require use of `-T` for specifying thread count 2023-10-13 14:36:34 +08:00
2 changed files with 23 additions and 11 deletions

View File

@ -31,9 +31,8 @@ else
nac3standalone=../../target/x86_64-unknown-linux-gnu/release/nac3standalone
fi
rm -f ./*.o ./*.bc demo
if [ -z "$use_lli" ]; then
rm -f "*.o" demo
$nac3standalone "${nac3args[@]}"
clang -c -std=gnu11 -Wall -Wextra -O3 -o demo.o demo.c
@ -45,15 +44,19 @@ if [ -z "$use_lli" ]; then
./demo > "$outfile"
fi
else
rm -f "*.o" "*.bc" demo
$nac3standalone --emit-llvm "${nac3args[@]}"
clang -c -std=gnu11 -Wall -Wextra -O3 -emit-llvm -o demo.bc demo.c
if [ -z "$outfile" ]; then
lli --extra-module demo.bc --extra-module irrt.bc main.bc
if ls module*.bc >/dev/null 2>&1; then
llvm-link -o module.bc module*.bc main.bc
else
lli --extra-module demo.bc --extra-module irrt.bc main.bc > "$outfile"
cp main.bc module.bc
fi
if [ -z "$outfile" ]; then
lli --extra-module demo.bc --extra-module irrt.bc module.bc
else
lli --extra-module demo.bc --extra-module irrt.bc module.bc > "$outfile"
fi
fi

View File

@ -39,8 +39,9 @@ struct CommandLineArgs {
/// The name of the input file.
file_name: String,
/// The number of threads allocated to processing the source file.
#[arg(default_value_t = 1)]
/// The number of threads allocated to processing the source file. If 0 is passed to this
/// parameter, all available threads will be used for compilation.
#[arg(short = 'T', default_value_t = 1)]
threads: u32,
/// The level to optimize the LLVM IR.
@ -48,6 +49,8 @@ struct CommandLineArgs {
opt_level: u32,
/// Whether to emit LLVM IR at the end of every module.
///
/// If multithreaded compilation is also enabled, each thread will emit its own module.
#[arg(long, default_value_t = false)]
emit_llvm: bool,
@ -209,9 +212,15 @@ fn main() {
.unwrap_or_default();
let target_features = target_features.unwrap_or_default();
let threads = if is_multithreaded() {
threads
if threads == 0 {
std::thread::available_parallelism()
.map(|threads| threads.get() as u32)
.unwrap_or(1u32)
} else {
threads
}
} else {
if threads > 1 {
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