Use fast calling convention internally

This commit is contained in:
Richard Feldman 2020-08-11 09:02:26 -04:00
parent 84a8b69437
commit 12ed20b5cd
4 changed files with 53 additions and 74 deletions

View file

@ -10,7 +10,7 @@ use roc_can::expected::Expected;
use roc_can::expr::{canonicalize_expr, Output};
use roc_can::operator;
use roc_can::scope::Scope;
use roc_collections::all::{ImMap, ImSet, MutMap, SendMap, SendSet};
use roc_collections::all::{ImMap, ImSet, MutMap, MutSet, SendMap, SendSet};
use roc_constrain::expr::constrain_expr;
use roc_constrain::module::{constrain_imported_values, load_builtin_aliases, Import};
use roc_gen::layout_id::LayoutIds;
@ -241,6 +241,7 @@ pub fn gen(src: &[u8], target: Triple, opt_level: OptLevel) -> Result<(String, S
module,
ptr_bytes,
leak: false,
exposed_to_host: MutSet::default(),
};
let mut procs = Procs::default();
let mut ident_ids = env.interns.all_ident_ids.remove(&home).unwrap();
@ -321,8 +322,7 @@ pub fn gen(src: &[u8], target: Triple, opt_level: OptLevel) -> Result<(String, S
// 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());
let cc = roc_gen::llvm::build::FAST_CALL_CONV;
main_fn.set_call_conventions(cc);

View file

@ -1,15 +1,12 @@
use bumpalo::Bump;
use inkwell::context::Context;
use inkwell::module::Linkage;
use inkwell::targets::{
CodeModel, FileType, InitializationConfig, RelocMode, Target, TargetTriple,
};
use inkwell::OptimizationLevel;
use roc_collections::all::default_hasher;
use roc_gen::layout_id::LayoutIds;
use roc_gen::llvm::build::{
build_proc, build_proc_header, get_call_conventions, module_from_builtins, OptLevel,
};
use roc_gen::llvm::build::{build_proc, build_proc_header, module_from_builtins, OptLevel};
use roc_load::file::LoadedModule;
use roc_mono::ir::{Env, PartialProc, Procs};
use roc_mono::layout::{Layout, LayoutCache};
@ -23,7 +20,7 @@ use target_lexicon::{Architecture, OperatingSystem, Triple, Vendor};
#[allow(clippy::cognitive_complexity)]
pub fn gen(
arena: &Bump,
loaded: LoadedModule,
mut loaded: LoadedModule,
filename: PathBuf,
target: Triple,
dest_filename: &Path,
@ -74,17 +71,14 @@ pub fn gen(
let ptr_bytes = target.pointer_width().unwrap().bytes() as u32;
// Compile and add all the Procs before adding main
let mut env = roc_gen::llvm::build::Env {
arena: &arena,
builder: &builder,
context: &context,
interns: loaded.interns,
module,
ptr_bytes,
leak: false,
};
let mut ident_ids = env.interns.all_ident_ids.remove(&home).unwrap();
let mut exposed_to_host =
HashSet::with_capacity_and_hasher(loaded.exposed_vars_by_symbol.len(), default_hasher());
for (symbol, _) in loaded.exposed_vars_by_symbol {
exposed_to_host.insert(symbol);
}
let mut ident_ids = loaded.interns.all_ident_ids.remove(&home).unwrap();
let mut layout_ids = LayoutIds::default();
let mut procs = Procs::default();
let mut mono_problems = std::vec::Vec::new();
@ -96,12 +90,6 @@ pub fn gen(
home,
ident_ids: &mut ident_ids,
};
let mut exposed_symbols =
HashSet::with_capacity_and_hasher(loaded.exposed_vars_by_symbol.len(), default_hasher());
for (symbol, _) in loaded.exposed_vars_by_symbol {
exposed_symbols.insert(symbol);
}
// Add modules' decls to Procs
for (_, mut decls) in decls_by_id
@ -124,7 +112,7 @@ pub fn gen(
// register it as such. Otherwise, since it
// never gets called by Roc code, it will never
// get specialized!
if exposed_symbols.contains(&symbol) {
if exposed_to_host.contains(&symbol) {
let mut pattern_vars =
bumpalo::collections::Vec::with_capacity_in(
loc_args.len(),
@ -173,7 +161,7 @@ pub fn gen(
// register it as such. Otherwise, since it
// never gets called by Roc code, it will never
// get specialized!
if exposed_symbols.contains(&symbol) {
if exposed_to_host.contains(&symbol) {
let pattern_vars = bumpalo::collections::Vec::new_in(arena);
let ret_layout = layout_cache.from_var(mono_env.arena, annotation, mono_env.subs).unwrap_or_else(|err|
todo!("TODO gracefully handle the situation where we expose a function to the host which doesn't have a valid layout (e.g. maybe the function wasn't monomorphic): {:?}", err)
@ -220,6 +208,18 @@ pub fn gen(
}
}
// Compile and add all the Procs before adding main
let mut env = roc_gen::llvm::build::Env {
arena: &arena,
builder: &builder,
context: &context,
interns: loaded.interns,
module,
ptr_bytes,
leak: false,
exposed_to_host,
};
// Populate Procs further and get the low-level Expr from the canonical Expr
let mut headers = {
let num_headers = match &procs.pending_specializations {
@ -269,27 +269,6 @@ pub fn gen(
}
}
// Set exposed functions to external linkage and C calling conventions
{
let cc = get_call_conventions(target.default_calling_convention().unwrap());
let interns = &env.interns;
for symbol in exposed_symbols {
// Since it was exposed, it must have been monomorphic,
// meaning its LLVM name will be ___#1 (e.g. "main#1")
let fn_name = format!("{}#1", symbol.ident_string(interns));
let fn_val = env.module.get_function(&fn_name).unwrap_or_else(|| {
panic!(
"module.get_function({:?}) did not find a function registered with LLVM",
fn_name
)
});
fn_val.set_linkage(Linkage::External);
fn_val.set_call_conventions(cc);
}
}
mpm.run_on(module);
// Verify the module

View file

@ -16,12 +16,11 @@ use inkwell::values::BasicValueEnum::{self, *};
use inkwell::values::{FloatValue, FunctionValue, IntValue, PointerValue, StructValue};
use inkwell::AddressSpace;
use inkwell::{IntPredicate, OptimizationLevel};
use roc_collections::all::ImMap;
use roc_collections::all::{ImMap, MutSet};
use roc_module::low_level::LowLevel;
use roc_module::symbol::{Interns, Symbol};
use roc_mono::ir::JoinPointId;
use roc_mono::layout::{Builtin, Layout, MemoryMode};
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!
@ -78,6 +77,7 @@ pub struct Env<'a, 'ctx, 'env> {
pub interns: Interns,
pub ptr_bytes: u32,
pub leak: bool,
pub exposed_to_host: MutSet<Symbol>,
}
impl<'a, 'ctx, 'env> Env<'a, 'ctx, 'env> {
@ -150,6 +150,8 @@ fn add_intrinsic<'ctx>(
) -> FunctionValue<'ctx> {
let fn_val = module.add_function(intrinsic_name, fn_type, None);
// LLVM intrinsics always use the C calling convention, because
// they are implemented in C libraries
fn_val.set_call_conventions(C_CALL_CONV);
fn_val
@ -320,11 +322,13 @@ pub fn build_exp_expr<'a, 'ctx, 'env>(
}
};
// TODO FIXME this should not be hardcoded!
// Need to look up what calling convention is the right one for that function.
// If this is an external-facing function, it'll use the C calling convention.
// If it's an internal-only function, it should (someday) use the fast calling conention.
if env.exposed_to_host.contains(name) {
// If this is an external-facing function, use the C calling convention.
call.set_call_convention(C_CALL_CONV);
} else {
// If it's an internal-only function, use the fast calling conention.
call.set_call_convention(FAST_CALL_CONV);
}
call.try_as_basic_value()
.left()
@ -1371,7 +1375,15 @@ pub fn build_proc_header<'a, 'ctx, 'env>(
.module
.add_function(fn_name.as_str(), fn_type, Some(Linkage::Private));
fn_val.set_call_conventions(fn_val.get_call_conventions());
if env.exposed_to_host.contains(&symbol) {
// If this is an external-facing function, it'll use the C calling convention
// and external linkage.
fn_val.set_linkage(Linkage::External);
fn_val.set_call_conventions(C_CALL_CONV);
} else {
// If it's an internal-only function, it should use the fast calling conention.
fn_val.set_call_conventions(FAST_CALL_CONV);
}
(fn_val, arg_basic_types)
}
@ -2492,22 +2504,9 @@ fn list_set<'a, 'ctx, 'env>(
)
}
/// 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 => C_CALL_CONV,
WasmBasicCAbi => C_CALL_CONV,
WindowsFastcall => C_CALL_CONV,
}
}
/// Source: https://llvm.org/doxygen/namespacellvm_1_1CallingConv.html
pub static C_CALL_CONV: u32 = 0;
pub static FAST_CALL_CONV: u32 = 8;
pub static COLD_CALL_CONV: u32 = 9;
fn run_low_level<'a, 'ctx, 'env>(

View file

@ -1,3 +1,4 @@
use roc_collections::all::MutSet;
use roc_types::subs::Subs;
pub fn helper_without_uniqueness<'a>(
@ -88,6 +89,7 @@ pub fn helper_without_uniqueness<'a>(
module,
ptr_bytes,
leak: leak,
exposed_to_host: MutSet::default(),
};
let mut procs = roc_mono::ir::Procs::default();
let mut ident_ids = env.interns.all_ident_ids.remove(&home).unwrap();
@ -160,8 +162,7 @@ pub fn helper_without_uniqueness<'a>(
// 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());
let cc = roc_gen::llvm::build::FAST_CALL_CONV;
main_fn.set_call_conventions(cc);
@ -280,6 +281,7 @@ pub fn helper_with_uniqueness<'a>(
module,
ptr_bytes,
leak: leak,
exposed_to_host: MutSet::default(),
};
let mut procs = roc_mono::ir::Procs::default();
let mut ident_ids = env.interns.all_ident_ids.remove(&home).unwrap();
@ -351,8 +353,7 @@ pub fn helper_with_uniqueness<'a>(
// 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());
let cc = roc_gen::llvm::build::FAST_CALL_CONV;
main_fn.set_call_conventions(cc);