core: Switch to LLVM New Pass Manager
This commit is contained in:
parent
70cb0bd898
commit
5722f3397e
|
@ -8,7 +8,7 @@ use std::sync::Arc;
|
|||
use inkwell::{
|
||||
memory_buffer::MemoryBuffer,
|
||||
module::{Linkage, Module},
|
||||
passes::{PassManager, PassManagerBuilder},
|
||||
passes::{PassBuilderOptions, PassManager, PassManagerBuilder},
|
||||
targets::*,
|
||||
OptimizationLevel,
|
||||
};
|
||||
|
@ -654,12 +654,27 @@ impl Nac3 {
|
|||
global_option = global.get_next_global();
|
||||
}
|
||||
|
||||
let target_machine = self.llvm_options.target
|
||||
.create_target_machine(self.llvm_options.opt_level)
|
||||
.expect("couldn't create target machine");
|
||||
|
||||
if self.llvm_options.legacy_pm {
|
||||
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);
|
||||
} else {
|
||||
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)
|
||||
}
|
||||
|
@ -902,7 +917,8 @@ impl Nac3 {
|
|||
deferred_eval_store: DeferredEvaluationStore::new(),
|
||||
llvm_options: CodeGenLLVMOptions {
|
||||
opt_level: OptimizationLevel::Default,
|
||||
legacy_pm: true,
|
||||
// FIXME(Derppening): Add a field to device_db.py for modifying this option
|
||||
legacy_pm: false,
|
||||
target: Nac3::get_llvm_target_options(isa),
|
||||
emit_llvm: false,
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ 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},
|
||||
|
@ -308,7 +308,8 @@ 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
|
||||
|
@ -316,15 +317,24 @@ impl WorkerRegistry {
|
|||
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);
|
||||
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)) => {
|
||||
|
@ -348,6 +358,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());
|
||||
|
|
|
@ -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,
|
||||
|
@ -21,6 +24,7 @@ use nac3parser::{
|
|||
use parking_lot::RwLock;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::sync::Arc;
|
||||
use test_case::test_case;
|
||||
|
||||
struct Resolver {
|
||||
id_to_type: HashMap<StrRef, Type>,
|
||||
|
@ -77,8 +81,100 @@ impl SymbolResolver for Resolver {
|
|||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_primitives() {
|
||||
struct CodeGenTestInput {
|
||||
expected: String,
|
||||
legacy_pm: bool
|
||||
}
|
||||
|
||||
#[test_case(
|
||||
vec![
|
||||
CodeGenTestInput {
|
||||
expected: indoc! {"
|
||||
; ModuleID = 'test'
|
||||
source_filename = \"test\"
|
||||
|
||||
; 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 %1, %0, !dbg !9
|
||||
%cmp = icmp eq i32 %add, 1, !dbg !10
|
||||
%. = 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}
|
||||
|
||||
!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)
|
||||
!3 = !DIFile(filename: \"unknown\", directory: \"\")
|
||||
!4 = distinct !DISubprogram(name: \"testing\", linkageName: \"testing\", scope: null, file: !3, line: 1, type: !5, scopeLine: 1, flags: DIFlagPublic, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !8)
|
||||
!5 = !DISubroutineType(flags: DIFlagPublic, types: !6)
|
||||
!6 = !{!7}
|
||||
!7 = !DIBasicType(name: \"_\", flags: DIFlagPublic)
|
||||
!8 = !{}
|
||||
!9 = !DILocation(line: 1, column: 9, scope: !4)
|
||||
!10 = !DILocation(line: 2, column: 15, scope: !4)
|
||||
!11 = !DILocation(line: 0, scope: !4)
|
||||
!12 = !DILocation(line: 3, column: 8, scope: !4)
|
||||
"}.trim().to_string(),
|
||||
legacy_pm: false,
|
||||
},
|
||||
CodeGenTestInput {
|
||||
expected: indoc! {"
|
||||
; ModuleID = 'test'
|
||||
source_filename = \"test\"
|
||||
|
||||
define i32 @testing(i32 %0, i32 %1) !dbg !4 {
|
||||
init:
|
||||
%add = add i32 %0, %1, !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
|
||||
}
|
||||
|
||||
!llvm.module.flags = !{!0, !1}
|
||||
!llvm.dbg.cu = !{!2}
|
||||
|
||||
!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)
|
||||
!3 = !DIFile(filename: \"unknown\", directory: \"\")
|
||||
!4 = distinct !DISubprogram(name: \"testing\", linkageName: \"testing\", scope: null, file: !3, line: 1, type: !5, scopeLine: 1, flags: DIFlagPublic, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !8)
|
||||
!5 = !DISubroutineType(flags: DIFlagPublic, types: !6)
|
||||
!6 = !{!7}
|
||||
!7 = !DIBasicType(name: \"_\", flags: DIFlagPublic)
|
||||
!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)
|
||||
"}.trim().to_string(),
|
||||
legacy_pm: true,
|
||||
}
|
||||
];
|
||||
"primitives codegen"
|
||||
)]
|
||||
fn test_primitives(inputs: Vec<CodeGenTestInput>) {
|
||||
Target::initialize_all(&InitializationConfig::default());
|
||||
|
||||
for input in inputs {
|
||||
let CodeGenTestInput { expected, legacy_pm } = input;
|
||||
|
||||
let source = indoc! { "
|
||||
c = a + b
|
||||
d = a if c == 1 else 0
|
||||
|
@ -159,7 +255,8 @@ fn test_primitives() {
|
|||
signature,
|
||||
id: 0,
|
||||
};
|
||||
let f = Arc::new(WithCall::new(Box::new(|module| {
|
||||
|
||||
let f = Arc::new(WithCall::new(Box::new(move |module| {
|
||||
// the following IR is equivalent to
|
||||
// ```
|
||||
// ; ModuleID = 'test.ll'
|
||||
|
@ -177,54 +274,12 @@ fn test_primitives() {
|
|||
// attributes #0 = { norecurse nounwind readnone }
|
||||
// ```
|
||||
// after O2 optimization
|
||||
|
||||
let expected = indoc! {"
|
||||
; ModuleID = 'test'
|
||||
source_filename = \"test\"
|
||||
|
||||
define i32 @testing(i32 %0, i32 %1) !dbg !4 {
|
||||
init:
|
||||
%add = add i32 %0, %1, !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
|
||||
}
|
||||
|
||||
!llvm.module.flags = !{!0, !1}
|
||||
!llvm.dbg.cu = !{!2}
|
||||
|
||||
!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)
|
||||
!3 = !DIFile(filename: \"unknown\", directory: \"\")
|
||||
!4 = distinct !DISubprogram(name: \"testing\", linkageName: \"testing\", scope: null, file: !3, line: 1, type: !5, scopeLine: 1, flags: DIFlagPublic, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !8)
|
||||
!5 = !DISubroutineType(flags: DIFlagPublic, types: !6)
|
||||
!6 = !{!7}
|
||||
!7 = !DIBasicType(name: \"_\", flags: DIFlagPublic)
|
||||
!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)
|
||||
"}
|
||||
.trim();
|
||||
assert_eq!(expected, module.print_to_string().to_str().unwrap().trim());
|
||||
})));
|
||||
|
||||
let llvm_options = CodeGenLLVMOptions {
|
||||
opt_level: OptimizationLevel::Default,
|
||||
legacy_pm: true,
|
||||
legacy_pm,
|
||||
target: CodeGenTargetMachineOptions::from_host_triple(),
|
||||
emit_llvm: false,
|
||||
};
|
||||
|
@ -237,9 +292,97 @@ fn test_primitives() {
|
|||
registry.add_task(task);
|
||||
registry.wait_tasks_complete(handles);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_simple_call() {
|
||||
#[test_case(
|
||||
vec![
|
||||
CodeGenTestInput {
|
||||
expected: indoc! {"
|
||||
; ModuleID = 'test'
|
||||
source_filename = \"test\"
|
||||
|
||||
; Function Attrs: mustprogress nofree norecurse nosync nounwind readnone willreturn
|
||||
define i32 @testing(i32 %0) local_unnamed_addr #0 !dbg !5 {
|
||||
init:
|
||||
%add.i = shl i32 %0, 1, !dbg !10
|
||||
%mul = add i32 %add.i, 2, !dbg !10
|
||||
ret i32 %mul, !dbg !10
|
||||
}
|
||||
|
||||
; 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 !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)
|
||||
!3 = !DIFile(filename: \"unknown\", directory: \"\")
|
||||
!4 = distinct !DICompileUnit(language: DW_LANG_Python, file: !3, producer: \"NAC3\", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
|
||||
!5 = distinct !DISubprogram(name: \"testing\", linkageName: \"testing\", scope: null, file: !3, line: 1, type: !6, scopeLine: 1, flags: DIFlagPublic, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !9)
|
||||
!6 = !DISubroutineType(flags: DIFlagPublic, types: !7)
|
||||
!7 = !{!8}
|
||||
!8 = !DIBasicType(name: \"_\", flags: DIFlagPublic)
|
||||
!9 = !{}
|
||||
!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().to_string(),
|
||||
legacy_pm: false,
|
||||
},
|
||||
CodeGenTestInput {
|
||||
expected: indoc! {"
|
||||
; ModuleID = 'test'
|
||||
source_filename = \"test\"
|
||||
|
||||
define i32 @testing(i32 %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
|
||||
}
|
||||
|
||||
define i32 @foo.0(i32 %0) !dbg !12 {
|
||||
init:
|
||||
%add = add i32 %0, 1, !dbg !13
|
||||
ret i32 %add, !dbg !13
|
||||
}
|
||||
|
||||
!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)
|
||||
!3 = !DIFile(filename: \"unknown\", directory: \"\")
|
||||
!4 = distinct !DICompileUnit(language: DW_LANG_Python, file: !3, producer: \"NAC3\", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
|
||||
!5 = distinct !DISubprogram(name: \"testing\", linkageName: \"testing\", scope: null, file: !3, line: 1, type: !6, scopeLine: 1, flags: DIFlagPublic, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !9)
|
||||
!6 = !DISubroutineType(flags: DIFlagPublic, types: !7)
|
||||
!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)
|
||||
"}.trim().to_string(),
|
||||
legacy_pm: true,
|
||||
}
|
||||
];
|
||||
"primitives codegen"
|
||||
)]
|
||||
fn test_simple_call(inputs: Vec<CodeGenTestInput>) {
|
||||
Target::initialize_all(&InitializationConfig::default());
|
||||
|
||||
for input in inputs {
|
||||
let CodeGenTestInput { expected, legacy_pm } = input;
|
||||
let source_1 = indoc! { "
|
||||
a = foo(a)
|
||||
return a * 2
|
||||
|
@ -369,49 +512,14 @@ fn test_simple_call() {
|
|||
store,
|
||||
id: 0,
|
||||
};
|
||||
let f = Arc::new(WithCall::new(Box::new(|module| {
|
||||
let expected = indoc! {"
|
||||
; ModuleID = 'test'
|
||||
source_filename = \"test\"
|
||||
|
||||
define i32 @testing(i32 %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
|
||||
}
|
||||
|
||||
define i32 @foo.0(i32 %0) !dbg !12 {
|
||||
init:
|
||||
%add = add i32 %0, 1, !dbg !13
|
||||
ret i32 %add, !dbg !13
|
||||
}
|
||||
|
||||
!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)
|
||||
!3 = !DIFile(filename: \"unknown\", directory: \"\")
|
||||
!4 = distinct !DICompileUnit(language: DW_LANG_Python, file: !3, producer: \"NAC3\", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
|
||||
!5 = distinct !DISubprogram(name: \"testing\", linkageName: \"testing\", scope: null, file: !3, line: 1, type: !6, scopeLine: 1, flags: DIFlagPublic, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !9)
|
||||
!6 = !DISubroutineType(flags: DIFlagPublic, types: !7)
|
||||
!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)
|
||||
"}
|
||||
.trim();
|
||||
let f = Arc::new(WithCall::new(Box::new(move |module| {
|
||||
assert_eq!(expected, module.print_to_string().to_str().unwrap().trim());
|
||||
})));
|
||||
|
||||
let llvm_options = CodeGenLLVMOptions {
|
||||
opt_level: OptimizationLevel::Default,
|
||||
legacy_pm: true,
|
||||
legacy_pm,
|
||||
target: CodeGenTargetMachineOptions::from_host_triple(),
|
||||
emit_llvm: false,
|
||||
};
|
||||
|
@ -424,3 +532,4 @@ fn test_simple_call() {
|
|||
registry.add_task(task);
|
||||
registry.wait_tasks_complete(handles);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use clap::Parser;
|
||||
use inkwell::{
|
||||
memory_buffer::MemoryBuffer,
|
||||
passes::{PassManager, PassManagerBuilder},
|
||||
passes::{PassBuilderOptions, PassManager, PassManagerBuilder},
|
||||
targets::*,
|
||||
OptimizationLevel,
|
||||
};
|
||||
|
@ -49,6 +49,10 @@ struct CommandLineArgs {
|
|||
/// Whether to emit LLVM IR at the end of every module.
|
||||
#[arg(long, default_value_t = false)]
|
||||
emit_llvm: bool,
|
||||
|
||||
/// Whether to use LLVM's Legacy Pass Manager for optimizations.
|
||||
#[arg(long, default_value_t = false)]
|
||||
legacy_pm: bool,
|
||||
}
|
||||
|
||||
fn handle_typevar_definition(
|
||||
|
@ -177,7 +181,13 @@ 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,
|
||||
legacy_pm,
|
||||
} = cli;
|
||||
let opt_level = match opt_level {
|
||||
0 => OptimizationLevel::None,
|
||||
1 => OptimizationLevel::Less,
|
||||
|
@ -272,7 +282,7 @@ fn main() {
|
|||
|
||||
let llvm_options = CodeGenLLVMOptions {
|
||||
opt_level,
|
||||
legacy_pm: true,
|
||||
legacy_pm,
|
||||
target: CodeGenTargetMachineOptions::from_host_triple(),
|
||||
emit_llvm,
|
||||
};
|
||||
|
@ -326,16 +336,28 @@ fn main() {
|
|||
function_iter = func.get_next_function();
|
||||
}
|
||||
|
||||
let target_machine = llvm_options.target
|
||||
.create_target_machine(llvm_options.opt_level)
|
||||
.expect("couldn't create target machine");
|
||||
|
||||
if legacy_pm {
|
||||
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);
|
||||
} else {
|
||||
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!();
|
||||
}
|
||||
}
|
||||
|
||||
let target_machine = llvm_options.target
|
||||
.create_target_machine(llvm_options.opt_level)
|
||||
.expect("couldn't create target machine");
|
||||
target_machine
|
||||
.write_to_file(&main, FileType::Object, Path::new("module.o"))
|
||||
.expect("couldn't write module to file");
|
||||
|
|
Loading…
Reference in New Issue