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

@ -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.
call.set_call_convention(C_CALL_CONV);
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>(