Compare commits

...

4 Commits

Author SHA1 Message Date
David Mak 966ce7a659 standalone: Add command line flags for target properties
For testing codegen for different platforms on the host system.
2023-09-12 18:45:46 +08:00
David Mak 5ea7a672e6 core: Switch to LLVM New Pass Manager 2023-09-12 18:45:19 +08:00
David Mak 894583eb13 core: Add target field to CodeGenLLVMOptions
For specifying the target machine options when optimizing and linking.
2023-09-12 18:45:19 +08:00
David Mak cb3939634e core: Add CodeGenTargetMachineOptions
Needed in a future commit.
2023-09-12 18:45:18 +08:00
4 changed files with 222 additions and 108 deletions

View File

@ -8,11 +8,11 @@ use std::sync::Arc;
use inkwell::{
memory_buffer::MemoryBuffer,
module::{Linkage, Module},
passes::{PassManager, PassManagerBuilder},
passes::PassBuilderOptions,
targets::*,
OptimizationLevel,
};
use nac3core::codegen::{CodeGenLLVMOptions, gen_func_impl};
use nac3core::codegen::{CodeGenLLVMOptions, CodeGenTargetMachineOptions, gen_func_impl};
use nac3core::toplevel::builtins::get_exn_constructor;
use nac3core::typecheck::typedef::{TypeEnum, Unifier};
use nac3parser::{
@ -654,44 +654,58 @@ impl Nac3 {
global_option = global.get_next_global();
}
let builder = PassManagerBuilder::create();
builder.set_optimization_level(OptimizationLevel::Aggressive);
let passes = PassManager::create(());
builder.set_inliner_with_threshold(255);
builder.populate_module_pass_manager(&passes);
passes.run_on(&main);
let target_machine = self.llvm_options.target
.create_target_machine(self.llvm_options.opt_level)
.expect("couldn't create target machine");
let pass_options = PassBuilderOptions::create();
pass_options.set_merge_functions(true);
let result = main.run_passes("default<O3>", &target_machine, pass_options);
if let Err(err) = result {
println!("Failed to run optimization for module `main`");
println!("{}", err.to_string());
panic!();
}
link_fn(&main)
}
fn get_llvm_target_machine(
&self,
) -> TargetMachine {
let (triple, features) = match self.isa {
Isa::Host => (
TargetMachine::get_default_triple(),
TargetMachine::get_host_cpu_features().to_string(),
),
Isa::RiscV32G => {
(TargetTriple::create("riscv32-unknown-linux"), "+a,+m,+f,+d".to_string())
}
Isa::RiscV32IMA => (TargetTriple::create("riscv32-unknown-linux"), "+a,+m".to_string()),
Isa::CortexA9 => (
TargetTriple::create("armv7-unknown-linux-gnueabihf"),
"+dsp,+fp16,+neon,+vfp3,+long-calls".to_string(),
),
};
let target =
Target::from_triple(&triple).expect("couldn't create target from target triple");
target
.create_target_machine(
&triple,
"",
&features,
self.llvm_options.opt_level,
RelocMode::PIC,
CodeModel::Default,
)
/// Returns the [TargetTriple] used for compiling to [isa].
fn get_llvm_target_triple(isa: Isa) -> TargetTriple {
match isa {
Isa::Host => TargetMachine::get_default_triple(),
Isa::RiscV32G | Isa::RiscV32IMA => TargetTriple::create("riscv32-unknown-linux"),
Isa::CortexA9 => TargetTriple::create("armv7-unknown-linux-gnueabihf"),
}
}
/// Returns the [String] representing the target features used for compiling to [isa].
fn get_llvm_target_features(isa: Isa) -> String {
match isa {
Isa::Host => TargetMachine::get_host_cpu_features().to_string(),
Isa::RiscV32G => "+a,+m,+f,+d".to_string(),
Isa::RiscV32IMA => "+a,+m".to_string(),
Isa::CortexA9 => "+dsp,+fp16,+neon,+vfp3,+long-calls".to_string(),
}
}
/// Returns an instance of [CodeGenTargetMachineOptions] representing the target machine
/// options used for compiling to [isa].
fn get_llvm_target_options(isa: Isa) -> CodeGenTargetMachineOptions {
CodeGenTargetMachineOptions {
triple: Nac3::get_llvm_target_triple(isa).as_str().to_string_lossy().into_owned(),
cpu: String::default(),
features: Nac3::get_llvm_target_features(isa),
reloc_mode: RelocMode::PIC,
..CodeGenTargetMachineOptions::from_host()
}
}
/// Returns an instance of [TargetMachine] used in compiling and linking of a program to the
/// target [isa].
fn get_llvm_target_machine(&self) -> TargetMachine {
Nac3::get_llvm_target_options(self.isa)
.create_target_machine(self.llvm_options.opt_level)
.expect("couldn't create target machine")
}
}
@ -894,6 +908,7 @@ impl Nac3 {
deferred_eval_store: DeferredEvaluationStore::new(),
llvm_options: CodeGenLLVMOptions {
opt_level: OptimizationLevel::Default,
target: Nac3::get_llvm_target_options(isa),
emit_llvm: false,
}
})

View File

@ -15,7 +15,8 @@ use inkwell::{
builder::Builder,
context::Context,
module::Module,
passes::{PassManager, PassManagerBuilder},
passes::{PassBuilderOptions},
targets::{CodeModel, RelocMode, Target, TargetMachine, TargetTriple},
types::{AnyType, BasicType, BasicTypeEnum},
values::{BasicValueEnum, FunctionValue, PhiValue, PointerValue},
debug_info::{
@ -64,10 +65,75 @@ lazy_static!(
pub struct CodeGenLLVMOptions {
/// The optimization level to apply on the generated LLVM IR.
pub opt_level: OptimizationLevel,
/// Options related to the target machine.
pub target: CodeGenTargetMachineOptions,
/// Whether to output the LLVM IR after generation is complete.
pub emit_llvm: bool,
}
/// Additional options for code generation for the target machine.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CodeGenTargetMachineOptions {
/// The target machine triple.
pub triple: String,
/// The target machine CPU.
pub cpu: String,
/// Additional target machine features.
pub features: String,
/// Relocation mode for code generation.
pub reloc_mode: RelocMode,
/// Code model for code generation.
pub code_model: CodeModel,
}
impl CodeGenTargetMachineOptions {
/// Creates an instance of [CodeGenTargetMachineOptions] using the triple of the host machine.
/// Other options are set to defaults.
pub fn from_host_triple() -> CodeGenTargetMachineOptions {
CodeGenTargetMachineOptions {
triple: TargetMachine::get_default_triple().as_str().to_string_lossy().into_owned(),
cpu: String::default(),
features: String::default(),
reloc_mode: RelocMode::Default,
code_model: CodeModel::Default,
}
}
/// Creates an instance of [CodeGenTargetMachineOptions] using the properties of the host
/// machine. Other options are set to defaults.
pub fn from_host() -> CodeGenTargetMachineOptions {
CodeGenTargetMachineOptions {
cpu: TargetMachine::get_host_cpu_name().to_string(),
features: TargetMachine::get_host_cpu_features().to_string(),
..CodeGenTargetMachineOptions::from_host_triple()
}
}
/// Creates a [TargetMachine] using the target options specified by this struct.
///
/// See [Target::create_target_machine].
pub fn create_target_machine(
&self,
level: OptimizationLevel,
) -> Option<TargetMachine> {
let triple = TargetTriple::create(self.triple.as_str());
let target = Target::from_triple(&triple)
.expect(format!("could not create target from target triple {}", self.triple).as_str());
target.create_target_machine(
&triple,
self.cpu.as_str(),
self.features.as_str(),
level,
self.reloc_mode,
self.code_model
)
}
}
pub struct CodeGenContext<'ctx, 'a> {
pub ctx: &'ctx Context,
pub builder: Builder<'ctx>,
@ -239,23 +305,11 @@ impl WorkerRegistry {
context.i32_type().const_int(4, false),
);
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
{
let _data = PASSES_INIT_LOCK.lock();
let pass_builder = PassManagerBuilder::create();
pass_builder.set_optimization_level(self.llvm_options.opt_level);
pass_builder.populate_function_pass_manager(&passes);
}
let mut errors = HashSet::new();
while let Some(task) = self.receiver.recv().unwrap() {
match gen_func(&context, generator, self, builder, module, task) {
Ok(result) => {
builder = result.0;
passes.run_on(&result.2);
module = result.1;
}
Err((old_builder, e)) => {
@ -279,6 +333,19 @@ impl WorkerRegistry {
panic!()
}
let pass_options = PassBuilderOptions::create();
let target_machine = self.llvm_options.target.create_target_machine(
self.llvm_options.opt_level
).expect(format!("could not create target machine from properties {:?}", self.llvm_options.target).as_str());
let passes = format!("default<O{}>", self.llvm_options.opt_level as u32);
let result = module.run_passes(passes.as_str(), &target_machine, pass_options);
if let Err(err) = result {
println!("Failed to run optimization for module `{}`", module.get_name().to_str().unwrap());
println!("{}", err.to_string());
panic!();
}
if self.llvm_options.emit_llvm {
println!("LLVM IR for {}", module.get_name().to_str().unwrap());
println!("{}", module.to_string());

View File

@ -1,7 +1,7 @@
use crate::{
codegen::{
concrete_type::ConcreteTypeStore, CodeGenContext, CodeGenLLVMOptions, CodeGenTask,
DefaultCodeGenerator, WithCall, WorkerRegistry,
concrete_type::ConcreteTypeStore, CodeGenContext, CodeGenLLVMOptions,
CodeGenTargetMachineOptions, CodeGenTask, DefaultCodeGenerator, WithCall, WorkerRegistry,
},
symbol_resolver::{SymbolResolver, ValueEnum},
toplevel::{
@ -13,7 +13,10 @@ use crate::{
},
};
use indoc::indoc;
use inkwell::OptimizationLevel;
use inkwell::{
targets::{InitializationConfig, Target},
OptimizationLevel,
};
use nac3parser::{
ast::{fold::Fold, StrRef},
parser::parse_program,
@ -148,6 +151,8 @@ fn test_primitives() {
personality_symbol: None,
});
Target::initialize_all(&InitializationConfig::default());
let task = CodeGenTask {
subst: Default::default(),
symbol_name: "testing".into(),
@ -181,24 +186,18 @@ fn test_primitives() {
let expected = indoc! {"
; ModuleID = 'test'
source_filename = \"test\"
define i32 @testing(i32 %0, i32 %1) !dbg !4 {
; Function Attrs: mustprogress nofree norecurse nosync nounwind readnone willreturn
define i32 @testing(i32 %0, i32 %1) local_unnamed_addr #0 !dbg !4 {
init:
%add = add i32 %0, %1, !dbg !9
%add = add i32 %1, %0, !dbg !9
%cmp = icmp eq i32 %add, 1, !dbg !10
br i1 %cmp, label %then, label %else, !dbg !10
then: ; preds = %init
br label %cont, !dbg !11
else: ; preds = %init
br label %cont, !dbg !12
cont: ; preds = %else, %then
%if_exp_result.0 = phi i32 [ %0, %then ], [ 0, %else ], !dbg !13
ret i32 %if_exp_result.0, !dbg !14
%. = select i1 %cmp, i32 %0, i32 0, !dbg !11
ret i32 %., !dbg !12
}
attributes #0 = { mustprogress nofree norecurse nosync nounwind readnone willreturn }
!llvm.module.flags = !{!0, !1}
!llvm.dbg.cu = !{!2}
@ -213,10 +212,8 @@ fn test_primitives() {
!8 = !{}
!9 = !DILocation(line: 1, column: 9, scope: !4)
!10 = !DILocation(line: 2, column: 15, scope: !4)
!11 = !DILocation(line: 2, column: 5, scope: !4)
!12 = !DILocation(line: 2, column: 22, scope: !4)
!13 = !DILocation(line: 0, scope: !4)
!14 = !DILocation(line: 3, column: 8, scope: !4)
!11 = !DILocation(line: 0, scope: !4)
!12 = !DILocation(line: 3, column: 8, scope: !4)
"}
.trim();
assert_eq!(expected, module.print_to_string().to_str().unwrap().trim());
@ -224,6 +221,7 @@ fn test_primitives() {
let llvm_options = CodeGenLLVMOptions {
opt_level: OptimizationLevel::Default,
target: CodeGenTargetMachineOptions::from_host_triple(),
emit_llvm: false,
};
let (registry, handles) = WorkerRegistry::create_workers(
@ -356,6 +354,8 @@ fn test_simple_call() {
personality_symbol: None,
});
Target::initialize_all(&InitializationConfig::default());
let task = CodeGenTask {
subst: Default::default(),
symbol_name: "testing".to_string(),
@ -372,22 +372,26 @@ fn test_simple_call() {
; ModuleID = 'test'
source_filename = \"test\"
define i32 @testing(i32 %0) !dbg !5 {
; Function Attrs: mustprogress nofree norecurse nosync nounwind readnone willreturn
define i32 @testing(i32 %0) local_unnamed_addr #0 !dbg !5 {
init:
%call = call i32 @foo.0(i32 %0), !dbg !10
%mul = mul i32 %call, 2, !dbg !11
ret i32 %mul, !dbg !11
%add.i = shl i32 %0, 1, !dbg !10
%mul = add i32 %add.i, 2, !dbg !10
ret i32 %mul, !dbg !10
}
define i32 @foo.0(i32 %0) !dbg !12 {
; Function Attrs: mustprogress nofree norecurse nosync nounwind readnone willreturn
define i32 @foo.0(i32 %0) local_unnamed_addr #0 !dbg !11 {
init:
%add = add i32 %0, 1, !dbg !13
ret i32 %add, !dbg !13
%add = add i32 %0, 1, !dbg !12
ret i32 %add, !dbg !12
}
attributes #0 = { mustprogress nofree norecurse nosync nounwind readnone willreturn }
!llvm.module.flags = !{!0, !1}
!llvm.dbg.cu = !{!2, !4}
!0 = !{i32 2, !\"Debug Info Version\", i32 3}
!1 = !{i32 2, !\"Dwarf Version\", i32 4}
!2 = distinct !DICompileUnit(language: DW_LANG_Python, file: !3, producer: \"NAC3\", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
@ -398,10 +402,9 @@ fn test_simple_call() {
!7 = !{!8}
!8 = !DIBasicType(name: \"_\", flags: DIFlagPublic)
!9 = !{}
!10 = !DILocation(line: 1, column: 9, scope: !5)
!11 = !DILocation(line: 2, column: 12, scope: !5)
!12 = distinct !DISubprogram(name: \"foo.0\", linkageName: \"foo.0\", scope: null, file: !3, line: 1, type: !6, scopeLine: 1, flags: DIFlagPublic, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !4, retainedNodes: !9)
!13 = !DILocation(line: 1, column: 12, scope: !12)
!10 = !DILocation(line: 2, column: 12, scope: !5)
!11 = distinct !DISubprogram(name: \"foo.0\", linkageName: \"foo.0\", scope: null, file: !3, line: 1, type: !6, scopeLine: 1, flags: DIFlagPublic, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !4, retainedNodes: !9)
!12 = !DILocation(line: 1, column: 12, scope: !11)
"}
.trim();
assert_eq!(expected, module.print_to_string().to_str().unwrap().trim());
@ -409,6 +412,7 @@ fn test_simple_call() {
let llvm_options = CodeGenLLVMOptions {
opt_level: OptimizationLevel::Default,
target: CodeGenTargetMachineOptions::from_host_triple(),
emit_llvm: false,
};
let (registry, handles) = WorkerRegistry::create_workers(

View File

@ -1,7 +1,7 @@
use clap::Parser;
use inkwell::{
memory_buffer::MemoryBuffer,
passes::{PassManager, PassManagerBuilder},
passes::PassBuilderOptions,
targets::*,
OptimizationLevel,
};
@ -10,8 +10,8 @@ use std::{borrow::Borrow, collections::HashMap, fs, path::Path, sync::Arc};
use nac3core::{
codegen::{
concrete_type::ConcreteTypeStore, irrt::load_irrt, CodeGenLLVMOptions, CodeGenTask,
DefaultCodeGenerator, WithCall, WorkerRegistry,
concrete_type::ConcreteTypeStore, irrt::load_irrt, CodeGenLLVMOptions,
CodeGenTargetMachineOptions, CodeGenTask, DefaultCodeGenerator, WithCall, WorkerRegistry,
},
symbol_resolver::SymbolResolver,
toplevel::{
@ -49,6 +49,18 @@ struct CommandLineArgs {
/// Whether to emit LLVM IR at the end of every module.
#[arg(long, default_value_t = false)]
emit_llvm: bool,
/// The target triple to compile for.
#[arg(long)]
triple: Option<String>,
/// The target CPU to compile for.
#[arg(long)]
mcpu: Option<String>,
/// Additional target features to enable/disable, specified using the `+`/`-` prefixes.
#[arg(long = "target-features")]
target_features: Option<String>,
}
fn handle_typevar_definition(
@ -177,7 +189,24 @@ fn handle_assignment_pattern(
fn main() {
let cli = CommandLineArgs::parse();
let CommandLineArgs { file_name, threads, opt_level, emit_llvm } = cli;
let CommandLineArgs {
file_name,
threads,
opt_level,
emit_llvm ,
triple,
mcpu,
target_features,
} = cli;
Target::initialize_all(&InitializationConfig::default());
let host_target_machine = CodeGenTargetMachineOptions::from_host();
let triple = triple.unwrap_or(host_target_machine.triple.clone());
let mcpu = mcpu
.map(|arg| if arg == "native" { host_target_machine.cpu.clone() } else { arg })
.unwrap_or_default();
let target_features = target_features.unwrap_or_default();
let opt_level = match opt_level {
0 => OptimizationLevel::None,
1 => OptimizationLevel::Less,
@ -186,8 +215,6 @@ fn main() {
_ => OptimizationLevel::Aggressive,
};
Target::initialize_all(&InitializationConfig::default());
let program = match fs::read_to_string(file_name.clone()) {
Ok(program) => program,
Err(err) => {
@ -272,8 +299,15 @@ fn main() {
let llvm_options = CodeGenLLVMOptions {
opt_level,
target: CodeGenTargetMachineOptions {
triple,
cpu: mcpu,
features: target_features,
..host_target_machine
},
emit_llvm,
};
let task = CodeGenTask {
subst: Default::default(),
symbol_name: "run".to_string(),
@ -323,25 +357,19 @@ fn main() {
function_iter = func.get_next_function();
}
let builder = PassManagerBuilder::create();
builder.set_optimization_level(OptimizationLevel::Aggressive);
let passes = PassManager::create(());
builder.set_inliner_with_threshold(255);
builder.populate_module_pass_manager(&passes);
passes.run_on(&main);
let triple = TargetMachine::get_default_triple();
let target = Target::from_triple(&triple).expect("couldn't create target from target triple");
let target_machine = target
.create_target_machine(
&triple,
"",
"",
opt_level,
RelocMode::Default,
CodeModel::Default,
)
let target_machine = llvm_options.target
.create_target_machine(llvm_options.opt_level)
.expect("couldn't create target machine");
let pass_options = PassBuilderOptions::create();
pass_options.set_merge_functions(true);
let result = main.run_passes("default<O3>", &target_machine, pass_options);
if let Err(err) = result {
println!("Failed to run optimization for module `main`");
println!("{}", err.to_string());
panic!();
}
target_machine
.write_to_file(&main, FileType::Object, Path::new("module.o"))
.expect("couldn't write module to file");