Compare commits

..

3 Commits

Author SHA1 Message Date
David Mak 1d0356e3c4 meta: Update run_demo.sh
- Allow more than one argument to nac3standalone executable
- Abort linking process if standalone executable exits with error
2023-09-06 20:25:37 +08:00
David Mak 95222331b6 standalone: Expose flags in command-line 2023-09-06 20:25:35 +08:00
David Mak 5165e61507 core: Add opt_level and emit_llvm to CodeGen options 2023-09-06 20:25:23 +08:00
5 changed files with 24 additions and 45 deletions

View File

@ -12,7 +12,7 @@ use inkwell::{
targets::*,
OptimizationLevel,
};
use nac3core::codegen::{CodeGenLLVMOptions, gen_func_impl};
use nac3core::codegen::gen_func_impl;
use nac3core::toplevel::builtins::get_exn_constructor;
use nac3core::typecheck::typedef::{TypeEnum, Unifier};
use nac3parser::{
@ -97,8 +97,7 @@ struct Nac3 {
top_levels: Vec<TopLevelComponent>,
string_store: Arc<RwLock<HashMap<String, i32>>>,
exception_ids: Arc<RwLock<HashMap<usize, usize>>>,
deferred_eval_store: DeferredEvaluationStore,
llvm_options: CodeGenLLVMOptions,
deferred_eval_store: DeferredEvaluationStore
}
create_exception!(nac3artiq, CompileError, exceptions::PyException);
@ -552,7 +551,6 @@ impl Nac3 {
unifier_index: instance.unifier_id,
calls: instance.calls,
id: 0,
llvm_options: self.llvm_options,
};
let mut store = ConcreteTypeStore::new();
@ -570,7 +568,6 @@ impl Nac3 {
unifier_index: instance.unifier_id,
calls: Arc::new(Default::default()),
id: 0,
llvm_options: self.llvm_options,
};
let membuffers: Arc<Mutex<Vec<Vec<u8>>>> = Default::default();
@ -888,10 +885,6 @@ impl Nac3 {
string_store: Default::default(),
exception_ids: Default::default(),
deferred_eval_store: DeferredEvaluationStore::new(),
llvm_options: CodeGenLLVMOptions {
opt_level: OptimizationLevel::Default,
emit_llvm: false,
}
})
}

View File

@ -601,7 +601,8 @@ pub fn gen_func_instance<'ctx, 'a>(
store,
unifier_index: instance.unifier_id,
id,
llvm_options: ctx.llvm_options,
opt_level: ctx.opt_level,
emit_llvm: ctx.emit_llvm,
});
Ok(symbol)
} else {

View File

@ -59,15 +59,6 @@ lazy_static!(
static ref PASSES_INIT_LOCK: Mutex<AtomicBool> = Mutex::new(AtomicBool::new(true));
);
/// Additional options for LLVM during codegen.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct CodeGenLLVMOptions {
/// The optimization level to apply on the generated LLVM IR.
pub opt_level: OptimizationLevel,
/// Whether to output the LLVM IR after generation is complete.
pub emit_llvm: bool,
}
pub struct CodeGenContext<'ctx, 'a> {
pub ctx: &'ctx Context,
pub builder: Builder<'ctx>,
@ -98,8 +89,10 @@ pub struct CodeGenContext<'ctx, 'a> {
Option<(Vec<Option<BasicValueEnum<'ctx>>>, BasicBlock<'ctx>, PhiValue<'ctx>)>,
pub need_sret: bool,
pub current_loc: Location,
/// Additional LLVM options for Codegen
pub llvm_options: CodeGenLLVMOptions,
/// The optimization level to apply on the generated LLVM IR.
pub opt_level: OptimizationLevel,
/// Whether to output the LLVM IR after generation is complete.
pub emit_llvm: bool,
}
impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
@ -245,11 +238,11 @@ impl WorkerRegistry {
{
let _data = PASSES_INIT_LOCK.lock();
let pass_builder = PassManagerBuilder::create();
pass_builder.set_optimization_level(task.llvm_options.opt_level);
pass_builder.set_optimization_level(task.opt_level);
pass_builder.populate_function_pass_manager(&passes);
}
let emit_llvm = task.llvm_options.emit_llvm;
let emit_llvm = task.emit_llvm;
match gen_func(&context, generator, self, builder, module, task) {
Ok(result) => {
@ -301,8 +294,10 @@ pub struct CodeGenTask {
pub unifier_index: usize,
pub resolver: Arc<dyn SymbolResolver + Send + Sync>,
pub id: usize,
/// Additional LLVM options for Codegen
pub llvm_options: CodeGenLLVMOptions,
/// The optimization level to apply on the generated LLVM IR.
pub opt_level: OptimizationLevel,
/// Whether to output the LLVM IR after generation is complete.
pub emit_llvm: bool,
}
fn get_llvm_type<'ctx>(
@ -646,7 +641,7 @@ pub fn gen_func_impl<'ctx, G: CodeGenerator, F: FnOnce(&mut G, &mut CodeGenConte
),
/* directory */ "",
/* producer */ "NAC3",
/* is_optimized */ task.llvm_options.opt_level != OptimizationLevel::None,
/* is_optimized */ task.opt_level != OptimizationLevel::None,
/* compiler command line flags */ "",
/* runtime_ver */ 0,
/* split_name */ "",
@ -681,7 +676,7 @@ pub fn gen_func_impl<'ctx, G: CodeGenerator, F: FnOnce(&mut G, &mut CodeGenConte
/* is_definition */ true,
/* scope_line */ row as u32,
/* flags */ inkwell::debug_info::DIFlags::PUBLIC,
/* is_optimized */ task.llvm_options.opt_level != OptimizationLevel::None,
/* is_optimized */ task.opt_level != OptimizationLevel::None,
);
fn_val.set_subprogram(func_scope);
@ -708,7 +703,8 @@ pub fn gen_func_impl<'ctx, G: CodeGenerator, F: FnOnce(&mut G, &mut CodeGenConte
need_sret: has_sret,
current_loc: Default::default(),
debug_info: (dibuilder, compile_unit, func_scope.as_debug_info_scope()),
llvm_options: task.llvm_options,
opt_level: task.opt_level,
emit_llvm: task.emit_llvm,
};
let loc = code_gen_context.debug_info.0.create_debug_location(

View File

@ -1,7 +1,7 @@
use crate::{
codegen::{
concrete_type::ConcreteTypeStore, CodeGenContext, CodeGenLLVMOptions, CodeGenTask,
DefaultCodeGenerator, WithCall, WorkerRegistry,
concrete_type::ConcreteTypeStore, CodeGenContext, CodeGenTask, DefaultCodeGenerator,
WithCall, WorkerRegistry,
},
symbol_resolver::{SymbolResolver, ValueEnum},
toplevel::{
@ -13,7 +13,6 @@ use crate::{
},
};
use indoc::indoc;
use inkwell::OptimizationLevel;
use nac3parser::{
ast::{fold::Fold, StrRef},
parser::parse_program,
@ -158,10 +157,6 @@ fn test_primitives() {
store,
signature,
id: 0,
llvm_options: CodeGenLLVMOptions {
opt_level: OptimizationLevel::Default,
emit_llvm: false,
},
};
let f = Arc::new(WithCall::new(Box::new(|module| {
// the following IR is equivalent to
@ -360,10 +355,6 @@ fn test_simple_call() {
signature,
store,
id: 0,
llvm_options: CodeGenLLVMOptions {
opt_level: OptimizationLevel::Default,
emit_llvm: false,
},
};
let f = Arc::new(WithCall::new(Box::new(|module| {
let expected = indoc! {"

View File

@ -11,8 +11,8 @@ use std::mem::transmute;
use nac3core::{
codegen::{
concrete_type::ConcreteTypeStore, irrt::load_irrt, CodeGenLLVMOptions, CodeGenTask,
DefaultCodeGenerator, WithCall, WorkerRegistry,
concrete_type::ConcreteTypeStore, irrt::load_irrt, CodeGenTask, DefaultCodeGenerator,
WithCall, WorkerRegistry,
},
symbol_resolver::SymbolResolver,
toplevel::{
@ -275,10 +275,8 @@ fn main() {
unifier_index: instance.unifier_id,
calls: instance.calls,
id: 0,
llvm_options: CodeGenLLVMOptions {
opt_level,
emit_llvm,
},
opt_level,
emit_llvm,
};
let membuffers: Arc<Mutex<Vec<Vec<u8>>>> = Default::default();