Compare commits
7 Commits
47c507453c
...
443f7d37e1
Author | SHA1 | Date |
---|---|---|
|
443f7d37e1 | |
|
f2e57dd1d9 | |
|
1bd82b24e6 | |
|
d3b326846e | |
|
676d07657a | |
|
2482a1ef9b | |
|
eb63f2ad48 |
|
@ -8,6 +8,7 @@ members = [
|
|||
"nac3artiq",
|
||||
"runkernel",
|
||||
]
|
||||
resolver = "2"
|
||||
|
||||
[profile.release]
|
||||
debug = true
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
name = "nac3artiq"
|
||||
version = "0.1.0"
|
||||
authors = ["M-Labs"]
|
||||
edition = "2018"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
name = "nac3artiq"
|
||||
|
|
|
@ -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,67 @@ 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 CPU used for compiling to [isa].
|
||||
fn get_llvm_target_cpu(isa: Isa) -> String {
|
||||
match isa {
|
||||
Isa::Host => TargetMachine::get_host_cpu_name().to_string(),
|
||||
Isa::RiscV32G | Isa::RiscV32IMA => "generic-rv32".to_string(),
|
||||
Isa::CortexA9 => "cortex-a9".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
/// 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: Nac3::get_llvm_target_cpu(isa),
|
||||
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 +917,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,
|
||||
}
|
||||
})
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
name = "nac3ast"
|
||||
version = "0.1.0"
|
||||
authors = ["RustPython Team", "M-Labs"]
|
||||
edition = "2018"
|
||||
edition = "2021"
|
||||
|
||||
[features]
|
||||
default = ["constant-optimization", "fold"]
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
name = "nac3core"
|
||||
version = "0.1.0"
|
||||
authors = ["M-Labs"]
|
||||
edition = "2018"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
itertools = "0.11"
|
||||
|
|
|
@ -15,7 +15,8 @@ use inkwell::{
|
|||
builder::Builder,
|
||||
context::Context,
|
||||
module::Module,
|
||||
passes::{PassManager, PassManagerBuilder},
|
||||
passes::{PassBuilderOptions, PassManager, PassManagerBuilder},
|
||||
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,33 @@ impl WorkerRegistry {
|
|||
context.i32_type().const_int(4, false),
|
||||
);
|
||||
|
||||
let passes = PassManager::create(&module);
|
||||
let passes = if self.llvm_options.legacy_pm {
|
||||
let pm = 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);
|
||||
}
|
||||
// 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(&pm);
|
||||
}
|
||||
|
||||
Some(pm)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
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);
|
||||
|
||||
if let Some(pm) = &passes {
|
||||
pm.run_on(&result.2);
|
||||
};
|
||||
|
||||
module = result.1;
|
||||
}
|
||||
Err((old_builder, e)) => {
|
||||
|
@ -279,6 +355,21 @@ impl WorkerRegistry {
|
|||
panic!()
|
||||
}
|
||||
|
||||
if !self.llvm_options.legacy_pm {
|
||||
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());
|
||||
|
|
|
@ -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::{
|
||||
|
@ -224,6 +224,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(
|
||||
|
@ -409,6 +410,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(
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
name = "nac3ld"
|
||||
version = "0.1.0"
|
||||
authors = ["M-Labs"]
|
||||
edition = "2018"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
byteorder = { version = "1.4", default-features = false }
|
||||
|
|
|
@ -5,7 +5,7 @@ description = "Parser for python code."
|
|||
authors = [ "RustPython Team", "M-Labs" ]
|
||||
build = "build.rs"
|
||||
license = "MIT"
|
||||
edition = "2018"
|
||||
edition = "2021"
|
||||
|
||||
[build-dependencies]
|
||||
lalrpop = "0.20"
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
name = "nac3standalone"
|
||||
version = "0.1.0"
|
||||
authors = ["M-Labs"]
|
||||
edition = "2018"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
parking_lot = "0.12"
|
||||
|
|
|
@ -6,7 +6,7 @@ count=0
|
|||
for demo in src/*.py; do
|
||||
echo -n "checking $demo... "
|
||||
./interpret_demo.py $demo > interpreted.log
|
||||
./run_demo.sh $demo > run.log
|
||||
./run_demo.sh "$@" $demo > run.log
|
||||
diff -Nau interpreted.log run.log
|
||||
echo "ok"
|
||||
let "count+=1"
|
||||
|
|
|
@ -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: 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");
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
name = "runkernel"
|
||||
version = "0.1.0"
|
||||
authors = ["M-Labs"]
|
||||
edition = "2018"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
libloading = "0.8"
|
||||
|
|
Loading…
Reference in New Issue