mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-29 14:54:47 +00:00
Merge remote-tracking branch 'origin/trunk' into report-problems
This commit is contained in:
commit
84ec100208
32 changed files with 2792 additions and 161 deletions
|
@ -38,6 +38,7 @@ inlinable_string = "0.1.0"
|
|||
# change the tag value in this Cargo.toml to point to that tag, and `cargo update`.
|
||||
# This way, GitHub Actions works and nobody's builds get broken.
|
||||
inkwell = { git = "https://github.com/rtfeldman/inkwell", tag = "llvm8-0.release2" }
|
||||
target-lexicon = "0.10"
|
||||
|
||||
[dev-dependencies]
|
||||
roc_can = { path = "../can" }
|
||||
|
|
|
@ -3,6 +3,7 @@ use bumpalo::Bump;
|
|||
use inkwell::builder::Builder;
|
||||
use inkwell::context::Context;
|
||||
use inkwell::module::{Linkage, Module};
|
||||
use inkwell::passes::PassManager;
|
||||
use inkwell::types::{BasicTypeEnum, IntType, StructType};
|
||||
use inkwell::values::BasicValueEnum::{self, *};
|
||||
use inkwell::values::{FunctionValue, IntValue, PointerValue, StructValue};
|
||||
|
@ -15,6 +16,7 @@ use roc_collections::all::ImMap;
|
|||
use roc_module::symbol::{Interns, Symbol};
|
||||
use roc_mono::expr::{Expr, Proc, Procs};
|
||||
use roc_mono::layout::{Builtin, Layout};
|
||||
use target_lexicon::CallingConvention;
|
||||
|
||||
/// This is for Inkwell's FunctionValue::verify - we want to know the verification
|
||||
/// output in debug builds, but we don't want it to print to stdout in release builds!
|
||||
|
@ -45,6 +47,25 @@ impl<'a, 'ctx, 'env> Env<'a, 'ctx, 'env> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn add_passes(fpm: &PassManager<FunctionValue<'_>>) {
|
||||
// tail-call elimination is always on
|
||||
fpm.add_instruction_combining_pass();
|
||||
fpm.add_tail_call_elimination_pass();
|
||||
|
||||
// Enable more optimizations when running cargo test --release
|
||||
if !cfg!(debug_assertions) {
|
||||
fpm.add_reassociate_pass();
|
||||
fpm.add_basic_alias_analysis_pass();
|
||||
fpm.add_promote_memory_to_register_pass();
|
||||
fpm.add_cfg_simplification_pass();
|
||||
fpm.add_gvn_pass();
|
||||
// TODO figure out why enabling any of these (even alone) causes LLVM to segfault
|
||||
// fpm.add_strip_dead_prototypes_pass();
|
||||
// fpm.add_dead_arg_elimination_pass();
|
||||
// fpm.add_function_inlining_pass();
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::cognitive_complexity)]
|
||||
pub fn build_expr<'a, 'ctx, 'env>(
|
||||
env: &Env<'a, 'ctx, 'env>,
|
||||
|
@ -1277,3 +1298,17 @@ fn list_set<'a, 'ctx, 'env>(
|
|||
ret_type.into(),
|
||||
)
|
||||
}
|
||||
|
||||
/// Translates a target_lexicon::Triple to a LLVM calling convention u32
|
||||
/// as described in https://llvm.org/doxygen/namespacellvm_1_1CallingConv.html
|
||||
pub fn get_call_conventions(cc: CallingConvention) -> u32 {
|
||||
use CallingConvention::*;
|
||||
|
||||
// For now, we're returning 0 for the C calling convention on all of these.
|
||||
// Not sure if we should be picking something more specific!
|
||||
match cc {
|
||||
SystemV => 0,
|
||||
WasmBasicCAbi => 0,
|
||||
WindowsFastcall => 0,
|
||||
}
|
||||
}
|
||||
|
|
4
compiler/gen/test.asm
Normal file
4
compiler/gen/test.asm
Normal file
|
@ -0,0 +1,4 @@
|
|||
.text
|
||||
.file "my_module"
|
||||
|
||||
.section ".note.GNU-stack","",@progbits
|
|
@ -1,42 +1,8 @@
|
|||
// Pointer size on current system
|
||||
pub const POINTER_SIZE: u32 = std::mem::size_of::<usize>() as u32;
|
||||
|
||||
// 0 is the C calling convention - see https://llvm.org/doxygen/namespacellvm_1_1CallingConv.html
|
||||
pub const MAIN_CALLING_CONVENTION: u32 = 0;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! get_fpm {
|
||||
($module:expr) => {{
|
||||
let fpm = PassManager::create(&$module);
|
||||
|
||||
// tail-call elimination is always on
|
||||
fpm.add_instruction_combining_pass();
|
||||
fpm.add_tail_call_elimination_pass();
|
||||
|
||||
// Enable more optimizations when running cargo test --release
|
||||
if !cfg!(debug_assertions) {
|
||||
fpm.add_reassociate_pass();
|
||||
fpm.add_basic_alias_analysis_pass();
|
||||
fpm.add_promote_memory_to_register_pass();
|
||||
fpm.add_cfg_simplification_pass();
|
||||
fpm.add_gvn_pass();
|
||||
// TODO figure out why enabling any of these (even alone) causes LLVM to segfault
|
||||
// fpm.add_strip_dead_prototypes_pass();
|
||||
// fpm.add_dead_arg_elimination_pass();
|
||||
// fpm.add_function_inlining_pass();
|
||||
}
|
||||
|
||||
fpm.initialize();
|
||||
|
||||
// TODO when should we call initialize, and then finalize?
|
||||
|
||||
fpm
|
||||
}};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! assert_llvm_evals_to {
|
||||
($src:expr, $expected:expr, $ty:ty, $transform:expr) => {
|
||||
let target = target_lexicon::Triple::host();
|
||||
let ptr_bytes = target.pointer_width().unwrap().bytes() as u32;
|
||||
let arena = Bump::new();
|
||||
let CanExprOut { loc_expr, var_store, var, constraint, home, interns, .. } = can_expr($src);
|
||||
let subs = Subs::new(var_store.into());
|
||||
|
@ -46,18 +12,20 @@ macro_rules! assert_llvm_evals_to {
|
|||
let context = Context::create();
|
||||
let module = context.create_module("app");
|
||||
let builder = context.create_builder();
|
||||
let fpm = { get_fpm!(module) };
|
||||
let fpm = inkwell::passes::PassManager::create(&module);
|
||||
|
||||
roc_gen::llvm::build::add_passes(&fpm);
|
||||
|
||||
fpm.initialize();
|
||||
|
||||
// Compute main_fn_type before moving subs to Env
|
||||
let layout = Layout::from_content(&arena, content, &subs, $crate::helpers::eval::POINTER_SIZE)
|
||||
let layout = Layout::from_content(&arena, content, &subs, ptr_bytes)
|
||||
.unwrap_or_else(|err| panic!("Code gen error in test: could not convert to layout. Err was {:?} and Subs were {:?}", err, subs));
|
||||
let execution_engine =
|
||||
module
|
||||
.create_jit_execution_engine(OptimizationLevel::None)
|
||||
.expect("Error creating JIT execution engine for test");
|
||||
|
||||
let ptr_bytes = execution_engine.get_target_data().get_pointer_byte_size(None);
|
||||
|
||||
let main_fn_type = basic_type_from_layout(&arena, &context, &layout, ptr_bytes)
|
||||
.fn_type(&[], false);
|
||||
let main_fn_name = "$Test.main";
|
||||
|
@ -75,7 +43,7 @@ macro_rules! assert_llvm_evals_to {
|
|||
let mut ident_ids = env.interns.all_ident_ids.remove(&home).unwrap();
|
||||
|
||||
// Populate Procs and get the low-level Expr from the canonical Expr
|
||||
let main_body = Expr::new(&arena, &mut subs, loc_expr.value, &mut procs, home, &mut ident_ids, $crate::helpers::eval::POINTER_SIZE);
|
||||
let main_body = Expr::new(&arena, &mut subs, loc_expr.value, &mut procs, home, &mut ident_ids, ptr_bytes);
|
||||
|
||||
// Put this module's ident_ids back in the interns, so we can use them in Env.
|
||||
env.interns.all_ident_ids.insert(home, ident_ids);
|
||||
|
@ -111,8 +79,9 @@ macro_rules! assert_llvm_evals_to {
|
|||
|
||||
// Add main to the module.
|
||||
let main_fn = env.module.add_function(main_fn_name, main_fn_type, None);
|
||||
let cc = roc_gen::llvm::build::get_call_conventions(target.default_calling_convention().unwrap());
|
||||
|
||||
main_fn.set_call_conventions($crate::helpers::eval::MAIN_CALLING_CONVENTION);
|
||||
main_fn.set_call_conventions(cc);
|
||||
|
||||
// Add main's body
|
||||
let basic_block = context.append_basic_block(main_fn, "entry");
|
||||
|
@ -165,6 +134,8 @@ macro_rules! assert_llvm_evals_to {
|
|||
macro_rules! assert_opt_evals_to {
|
||||
($src:expr, $expected:expr, $ty:ty, $transform:expr) => {
|
||||
let arena = Bump::new();
|
||||
let target = target_lexicon::Triple::host();
|
||||
let ptr_bytes = target.pointer_width().unwrap().bytes() as u32;
|
||||
let (loc_expr, _output, _problems, subs, var, constraint, home, interns) = uniq_expr($src);
|
||||
|
||||
let mut unify_problems = Vec::new();
|
||||
|
@ -173,10 +144,142 @@ macro_rules! assert_opt_evals_to {
|
|||
let context = Context::create();
|
||||
let module = context.create_module("app");
|
||||
let builder = context.create_builder();
|
||||
let fpm = { get_fpm!(module) };
|
||||
let fpm = PassManager::create(&module);
|
||||
|
||||
roc_gen::llvm::build::add_passes(&fpm);
|
||||
|
||||
fpm.initialize();
|
||||
|
||||
// Compute main_fn_type before moving subs to Env
|
||||
let layout = Layout::from_content(&arena, content, &subs, $crate::helpers::eval::POINTER_SIZE)
|
||||
let layout = Layout::from_content(&arena, content, &subs, ptr_bytes)
|
||||
.unwrap_or_else(|err| panic!("Code gen error in test: could not convert to layout. Err was {:?} and Subs were {:?}", err, subs));
|
||||
|
||||
let execution_engine =
|
||||
module
|
||||
.create_jit_execution_engine(OptimizationLevel::None)
|
||||
.expect("Error creating JIT execution engine for test");
|
||||
|
||||
let main_fn_type = basic_type_from_layout(&arena, &context, &layout, ptr_bytes)
|
||||
.fn_type(&[], false);
|
||||
let main_fn_name = "$Test.main";
|
||||
|
||||
// Compile and add all the Procs before adding main
|
||||
let mut env = roc_gen::llvm::build::Env {
|
||||
arena: &arena,
|
||||
builder: &builder,
|
||||
context: &context,
|
||||
interns,
|
||||
module: arena.alloc(module),
|
||||
ptr_bytes
|
||||
};
|
||||
let mut procs = Procs::default();
|
||||
let mut ident_ids = env.interns.all_ident_ids.remove(&home).unwrap();
|
||||
|
||||
// Populate Procs and get the low-level Expr from the canonical Expr
|
||||
let main_body = Expr::new(&arena, &mut subs, loc_expr.value, &mut procs, home, &mut ident_ids, ptr_bytes);
|
||||
|
||||
// Put this module's ident_ids back in the interns, so we can use them in Env.
|
||||
env.interns.all_ident_ids.insert(home, ident_ids);
|
||||
|
||||
let mut headers = Vec::with_capacity(procs.len());
|
||||
|
||||
// Add all the Proc headers to the module.
|
||||
// We have to do this in a separate pass first,
|
||||
// because their bodies may reference each other.
|
||||
for (symbol, opt_proc) in procs.as_map().into_iter() {
|
||||
if let Some(proc) = opt_proc {
|
||||
let (fn_val, arg_basic_types) = build_proc_header(&env, symbol, &proc);
|
||||
|
||||
headers.push((proc, fn_val, arg_basic_types));
|
||||
}
|
||||
}
|
||||
|
||||
// Build each proc using its header info.
|
||||
for (proc, fn_val, arg_basic_types) in headers {
|
||||
// NOTE: This is here to be uncommented in case verification fails.
|
||||
// (This approach means we don't have to defensively clone name here.)
|
||||
//
|
||||
// println!("\n\nBuilding and then verifying function {}\n\n", name);
|
||||
build_proc(&env, proc, &procs, fn_val, arg_basic_types);
|
||||
|
||||
if fn_val.verify(true) {
|
||||
fpm.run_on(&fn_val);
|
||||
} else {
|
||||
// NOTE: If this fails, uncomment the above println to debug.
|
||||
panic!("Non-main function failed LLVM verification. Uncomment the above println to debug!");
|
||||
}
|
||||
}
|
||||
|
||||
// Add main to the module.
|
||||
let main_fn = env.module.add_function(main_fn_name, main_fn_type, None);
|
||||
let cc = roc_gen::llvm::build::get_call_conventions(target.default_calling_convention().unwrap());
|
||||
|
||||
main_fn.set_call_conventions(cc);
|
||||
|
||||
// Add main's body
|
||||
let basic_block = context.append_basic_block(main_fn, "entry");
|
||||
|
||||
builder.position_at_end(basic_block);
|
||||
|
||||
let ret = roc_gen::llvm::build::build_expr(
|
||||
&env,
|
||||
&ImMap::default(),
|
||||
main_fn,
|
||||
&main_body,
|
||||
&mut Procs::default(),
|
||||
);
|
||||
|
||||
builder.build_return(Some(&ret));
|
||||
|
||||
// Uncomment this to see the module's un-optimized LLVM instruction output:
|
||||
// env.module.print_to_stderr();
|
||||
|
||||
if main_fn.verify(true) {
|
||||
fpm.run_on(&main_fn);
|
||||
} else {
|
||||
panic!("Function {} failed LLVM verification.", main_fn_name);
|
||||
}
|
||||
|
||||
// Verify the module
|
||||
if let Err(errors) = env.module.verify() {
|
||||
panic!("Errors defining module: {:?}", errors);
|
||||
}
|
||||
|
||||
// Uncomment this to see the module's optimized LLVM instruction output:
|
||||
// env.module.print_to_stderr();
|
||||
|
||||
unsafe {
|
||||
let main: JitFunction<unsafe extern "C" fn() -> $ty> = execution_engine
|
||||
.get_function(main_fn_name)
|
||||
.ok()
|
||||
.ok_or(format!("Unable to JIT compile `{}`", main_fn_name))
|
||||
.expect("errored");
|
||||
|
||||
assert_eq!($transform(main.call()), $expected);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! emit_expr {
|
||||
($src:expr, $expected:expr, $ty:ty, $transform:expr) => {
|
||||
let arena = Bump::new();
|
||||
let (loc_expr, _output, _problems, subs, var, constraint, home, interns) = uniq_expr($src);
|
||||
|
||||
let mut unify_problems = Vec::new();
|
||||
let (content, mut subs) = infer_expr(subs, &mut unify_problems, &constraint, var);
|
||||
|
||||
let context = Context::create();
|
||||
let module = context.create_module("app");
|
||||
let builder = context.create_builder();
|
||||
let fpm = PassManager::create(&module);
|
||||
|
||||
roc_gen::llvm::build::add_passes(&fpm);
|
||||
|
||||
fpm.initialize();
|
||||
|
||||
// Compute main_fn_type before moving subs to Env
|
||||
let layout = Layout::from_content(&arena, content, &subs, ptr_bytes)
|
||||
.unwrap_or_else(|err| panic!("Code gen error in test: could not convert to layout. Err was {:?} and Subs were {:?}", err, subs));
|
||||
|
||||
let execution_engine =
|
||||
|
@ -184,7 +287,6 @@ macro_rules! assert_opt_evals_to {
|
|||
.create_jit_execution_engine(OptimizationLevel::None)
|
||||
.expect("Error creating JIT execution engine for test");
|
||||
|
||||
let ptr_bytes = execution_engine.get_target_data().get_pointer_byte_size(None);
|
||||
let main_fn_type = basic_type_from_layout(&arena, &context, &layout, ptr_bytes)
|
||||
.fn_type(&[], false);
|
||||
let main_fn_name = "$Test.main";
|
||||
|
@ -218,7 +320,6 @@ macro_rules! assert_opt_evals_to {
|
|||
|
||||
headers.push((proc, fn_val, arg_basic_types));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Build each proc using its header info.
|
||||
|
|
|
@ -11,12 +11,12 @@ roc_module = { path = "../module" }
|
|||
roc_types = { path = "../types" }
|
||||
roc_can = { path = "../can" }
|
||||
roc_unify = { path = "../unify" }
|
||||
roc_problem = { path = "../problem" }
|
||||
bumpalo = { version = "3.2", features = ["collections"] }
|
||||
|
||||
[dev-dependencies]
|
||||
roc_constrain = { path = "../constrain" }
|
||||
roc_builtins = { path = "../builtins" }
|
||||
roc_problem = { path = "../problem" }
|
||||
roc_parse = { path = "../parse" }
|
||||
roc_solve = { path = "../solve" }
|
||||
pretty_assertions = "0.5.1 "
|
||||
|
|
|
@ -340,31 +340,43 @@ fn pattern_to_when<'a>(
|
|||
// for underscore we generate a dummy Symbol
|
||||
(env.fresh_symbol(), body)
|
||||
}
|
||||
|
||||
Shadowed(_, _) | UnsupportedPattern(_) => {
|
||||
// create the runtime error here, instead of delegating to When.
|
||||
// UnsupportedPattern should then never occcur in When
|
||||
panic!("TODO generate runtime error here");
|
||||
Shadowed(region, loc_ident) => {
|
||||
let error = roc_problem::can::RuntimeError::Shadowing {
|
||||
original_region: *region,
|
||||
shadow: loc_ident.clone(),
|
||||
};
|
||||
(env.fresh_symbol(), Located::at_zero(RuntimeError(error)))
|
||||
}
|
||||
|
||||
AppliedTag {..} | RecordDestructure {..} => {
|
||||
UnsupportedPattern(region) => {
|
||||
// create the runtime error here, instead of delegating to When.
|
||||
// UnsupportedPattern should then never occcur in When
|
||||
let error = roc_problem::can::RuntimeError::UnsupportedPattern(*region);
|
||||
(env.fresh_symbol(), Located::at_zero(RuntimeError(error)))
|
||||
}
|
||||
|
||||
AppliedTag { .. } | RecordDestructure { .. } => {
|
||||
let symbol = env.fresh_symbol();
|
||||
|
||||
let wrapped_body = When {
|
||||
cond_var: pattern_var,
|
||||
expr_var: body_var,
|
||||
loc_cond: Box::new(Located::at_zero(Var(symbol))),
|
||||
branches: vec![WhenBranch{ patterns: vec![pattern], value: body, guard: None }],
|
||||
branches: vec![WhenBranch {
|
||||
patterns: vec![pattern],
|
||||
value: body,
|
||||
guard: None,
|
||||
}],
|
||||
};
|
||||
|
||||
(symbol, Located::at_zero(wrapped_body))
|
||||
}
|
||||
|
||||
// These patters are refutable, and thus should never occur outside a `when` expression
|
||||
IntLiteral(_) | NumLiteral(_,_) | FloatLiteral(_) | StrLiteral(_) => {
|
||||
IntLiteral(_) | NumLiteral(_, _) | FloatLiteral(_) | StrLiteral(_) => {
|
||||
// These patters are refutable, and thus should never occur outside a `when` expression
|
||||
// They should have been replaced with `UnsupportedPattern` during canonicalization
|
||||
unreachable!("refutable pattern {:?} where irrefutable pattern is expected. This should never happen!", pattern.value)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1019,7 +1031,7 @@ fn from_can_when<'a>(
|
|||
if branches.is_empty() {
|
||||
// A when-expression with no branches is a runtime error.
|
||||
// We can't know what to return!
|
||||
panic!("TODO compile a 0-branch when-expression to a RuntimeError");
|
||||
Expr::RuntimeError("Hit a 0-branch when expression")
|
||||
} else if branches.len() == 1 && branches[0].patterns.len() == 1 && branches[0].guard.is_none()
|
||||
{
|
||||
let first = branches.remove(0);
|
||||
|
|
|
@ -34,6 +34,8 @@ pub enum RuntimeError {
|
|||
original_region: Region,
|
||||
shadow: Located<Ident>,
|
||||
},
|
||||
// Example: (5 = 1 + 2) is an unsupported pattern in an assignment; Int patterns aren't allowed in assignments!
|
||||
UnsupportedPattern(Region),
|
||||
UnrecognizedFunctionName(Located<InlinableString>),
|
||||
LookupNotInScope(Located<InlinableString>),
|
||||
ValueNotExposed {
|
||||
|
|
|
@ -2526,4 +2526,27 @@ mod test_solve {
|
|||
"List a -> List a",
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn rigid_record_quantification() {
|
||||
// the ext here is qualified on the outside (because we have rank 1 types, not rank 2).
|
||||
// That means e.g. `f : { bar : String, foo : Int } -> Bool }` is a valid argument. but
|
||||
// that function could not be applied to the `{ foo : Int }` list. Therefore, this function
|
||||
// is not allowed.
|
||||
//
|
||||
// should hit a debug_assert! in debug mode, and produce a type error in release mode
|
||||
infer_eq_without_problem(
|
||||
indoc!(
|
||||
r#"
|
||||
test : ({ foo : Int }ext -> Bool), { foo : Int } -> Bool
|
||||
test = \fn, a -> fn a
|
||||
|
||||
test
|
||||
"#
|
||||
),
|
||||
"should fail",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue