core: Add target field to CodeGenLLVMOptions

For specifying the target machine options when optimizing and linking.

This field is currently unused but will be required in a future
commit.
This commit is contained in:
David Mak 2023-09-11 13:12:46 +08:00 committed by David Mak
parent 2482a1ef9b
commit 676d07657a
4 changed files with 15 additions and 15 deletions

View File

@ -902,6 +902,7 @@ impl Nac3 {
deferred_eval_store: DeferredEvaluationStore::new(), deferred_eval_store: DeferredEvaluationStore::new(),
llvm_options: CodeGenLLVMOptions { llvm_options: CodeGenLLVMOptions {
opt_level: OptimizationLevel::Default, opt_level: OptimizationLevel::Default,
target: Nac3::get_llvm_target_options(isa),
emit_llvm: false, emit_llvm: false,
} }
}) })

View File

@ -65,6 +65,10 @@ lazy_static!(
pub struct CodeGenLLVMOptions { pub struct CodeGenLLVMOptions {
/// The optimization level to apply on the generated LLVM IR. /// The optimization level to apply on the generated LLVM IR.
pub opt_level: OptimizationLevel, pub opt_level: OptimizationLevel,
/// Options related to the target machine.
pub target: CodeGenTargetMachineOptions,
/// Whether to output the LLVM IR after generation is complete. /// Whether to output the LLVM IR after generation is complete.
pub emit_llvm: bool, pub emit_llvm: bool,
} }

View File

@ -1,7 +1,7 @@
use crate::{ use crate::{
codegen::{ codegen::{
concrete_type::ConcreteTypeStore, CodeGenContext, CodeGenLLVMOptions, CodeGenTask, concrete_type::ConcreteTypeStore, CodeGenContext, CodeGenLLVMOptions,
DefaultCodeGenerator, WithCall, WorkerRegistry, CodeGenTargetMachineOptions, CodeGenTask, DefaultCodeGenerator, WithCall, WorkerRegistry,
}, },
symbol_resolver::{SymbolResolver, ValueEnum}, symbol_resolver::{SymbolResolver, ValueEnum},
toplevel::{ toplevel::{
@ -224,6 +224,7 @@ fn test_primitives() {
let llvm_options = CodeGenLLVMOptions { let llvm_options = CodeGenLLVMOptions {
opt_level: OptimizationLevel::Default, opt_level: OptimizationLevel::Default,
target: CodeGenTargetMachineOptions::from_host_triple(),
emit_llvm: false, emit_llvm: false,
}; };
let (registry, handles) = WorkerRegistry::create_workers( let (registry, handles) = WorkerRegistry::create_workers(
@ -409,6 +410,7 @@ fn test_simple_call() {
let llvm_options = CodeGenLLVMOptions { let llvm_options = CodeGenLLVMOptions {
opt_level: OptimizationLevel::Default, opt_level: OptimizationLevel::Default,
target: CodeGenTargetMachineOptions::from_host_triple(),
emit_llvm: false, emit_llvm: false,
}; };
let (registry, handles) = WorkerRegistry::create_workers( let (registry, handles) = WorkerRegistry::create_workers(

View File

@ -10,8 +10,8 @@ use std::{borrow::Borrow, collections::HashMap, fs, path::Path, sync::Arc};
use nac3core::{ use nac3core::{
codegen::{ codegen::{
concrete_type::ConcreteTypeStore, irrt::load_irrt, CodeGenLLVMOptions, CodeGenTask, concrete_type::ConcreteTypeStore, irrt::load_irrt, CodeGenLLVMOptions,
DefaultCodeGenerator, WithCall, WorkerRegistry, CodeGenTargetMachineOptions, CodeGenTask, DefaultCodeGenerator, WithCall, WorkerRegistry,
}, },
symbol_resolver::SymbolResolver, symbol_resolver::SymbolResolver,
toplevel::{ toplevel::{
@ -272,8 +272,10 @@ fn main() {
let llvm_options = CodeGenLLVMOptions { let llvm_options = CodeGenLLVMOptions {
opt_level, opt_level,
target: CodeGenTargetMachineOptions::from_host_triple(),
emit_llvm, emit_llvm,
}; };
let task = CodeGenTask { let task = CodeGenTask {
subst: Default::default(), subst: Default::default(),
symbol_name: "run".to_string(), symbol_name: "run".to_string(),
@ -330,17 +332,8 @@ fn main() {
builder.populate_module_pass_manager(&passes); builder.populate_module_pass_manager(&passes);
passes.run_on(&main); passes.run_on(&main);
let triple = TargetMachine::get_default_triple(); let target_machine = llvm_options.target
let target = Target::from_triple(&triple).expect("couldn't create target from target triple"); .create_target_machine(llvm_options.opt_level)
let target_machine = target
.create_target_machine(
&triple,
"",
"",
opt_level,
RelocMode::Default,
CodeModel::Default,
)
.expect("couldn't create target machine"); .expect("couldn't create target machine");
target_machine target_machine
.write_to_file(&main, FileType::Object, Path::new("module.o")) .write_to_file(&main, FileType::Object, Path::new("module.o"))