Skip to content

Commit

Permalink
refactor: upgrade salsa to 0.15
Browse files Browse the repository at this point in the history
  • Loading branch information
baszalmstra committed Jul 17, 2020
1 parent 98979ca commit 9ad7519
Show file tree
Hide file tree
Showing 50 changed files with 444 additions and 390 deletions.
2 changes: 1 addition & 1 deletion crates/mun_codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mun_target = { version = "=0.2.0", path = "../mun_target" }
mun_lld = { version = "=70.2.0", path = "../mun_lld" }
anyhow = "1.0.31"
thiserror = "1.0.19"
salsa="0.14"
salsa = "0.15.0"
md5="0.6.1"
array-init="0.1.0"
tempfile = "3"
Expand Down
2 changes: 1 addition & 1 deletion crates/mun_codegen/src/assembly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl Assembly {
}

/// Create a new temporary file that contains the linked object
pub fn assembly_query(db: &impl IrDatabase, file_id: hir::FileId) -> Arc<Assembly> {
pub fn assembly_query(db: &dyn IrDatabase, file_id: hir::FileId) -> Arc<Assembly> {
let file = NamedTempFile::new().expect("could not create temp file for shared object");

let module_builder = ModuleBuilder::new(db, file_id).expect("could not create ModuleBuilder");
Expand Down
12 changes: 6 additions & 6 deletions crates/mun_codegen/src/code_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,17 @@ impl ObjectFile {
}

/// A struct that can be used to build an LLVM `Module`.
pub struct ModuleBuilder<'a, D: IrDatabase> {
db: &'a D,
pub struct ModuleBuilder<'a> {
db: &'a dyn IrDatabase,
file_id: FileId,
_target: inkwell::targets::Target,
target_machine: inkwell::targets::TargetMachine,
assembly_module: Arc<inkwell::module::Module>,
}

impl<'a, D: IrDatabase> ModuleBuilder<'a, D> {
impl<'a> ModuleBuilder<'a> {
/// Constructs module for the given `hir::FileId` at the specified output file location.
pub fn new(db: &'a D, file_id: FileId) -> Result<Self, anyhow::Error> {
pub fn new(db: &'a dyn IrDatabase, file_id: FileId) -> Result<Self, anyhow::Error> {
let target = db.target();

// Construct a module for the assembly
Expand Down Expand Up @@ -197,14 +197,14 @@ fn optimize_module(module: &Module, optimization_lvl: OptimizationLevel) {
}

/// Create an inkwell TargetData from the target in the database
pub(crate) fn target_data_query(db: &impl IrDatabase) -> Arc<TargetData> {
pub(crate) fn target_data_query(db: &dyn IrDatabase) -> Arc<TargetData> {
Arc::new(TargetData::create(&db.target().data_layout))
}

/// Returns a mapping from struct type to a struct type in the context. This is a query because the
/// value of struct type depends on the target we compile for.
pub(crate) fn type_to_struct_mapping_query(
db: &impl IrDatabase,
db: &dyn IrDatabase,
) -> by_address::ByAddress<Arc<StructMapping>> {
let _ = db.target_data();
by_address::ByAddress(Arc::new(RwLock::new(HashMap::default())))
Expand Down
26 changes: 13 additions & 13 deletions crates/mun_codegen/src/code_gen/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ use std::collections::HashSet;
use std::ffi::CString;

/// Construct a `MunFunctionPrototype` struct for the specified HIR function.
fn gen_prototype_from_function<D: IrDatabase>(
db: &D,
fn gen_prototype_from_function(
db: &dyn IrDatabase,
context: &IrValueContext,
function: hir::Function,
) -> ir::FunctionPrototype {
let module = context.module;
let name = function.name(db).to_string();
let name = function.name(db.upcast()).to_string();

// Internalize the name of the function prototype
let name_str = CString::new(name.clone())
.expect("function prototype name is not a valid CString")
.intern(format!("fn_sig::<{}>::name", &name), context);

// Get the `ir::TypeInfo` pointer for the return type of the function
let fn_sig = function.ty(db).callable_sig(db).unwrap();
let fn_sig = function.ty(db.upcast()).callable_sig(db.upcast()).unwrap();
let return_type = gen_signature_return_type(db, context, fn_sig.ret().clone());

// Construct an array of pointers to `ir::TypeInfo`s for the arguments of the prototype
Expand Down Expand Up @@ -95,8 +95,8 @@ fn gen_prototype_from_dispatch_entry(

/// Given a function, construct a pointer to a `ir::TypeInfo` global that represents the return type
/// of the function; or `null` if the return type is empty.
fn gen_signature_return_type<D: IrDatabase>(
db: &D,
fn gen_signature_return_type(
db: &dyn IrDatabase,
context: &IrValueContext,
ret_type: Ty,
) -> Value<*const ir::TypeInfo> {
Expand Down Expand Up @@ -126,15 +126,15 @@ fn gen_signature_return_type_from_type_info(

/// Construct a global that holds a reference to all functions. e.g.:
/// MunFunctionDefinition[] definitions = { ... }
fn get_function_definition_array<'a, D: IrDatabase>(
db: &D,
fn get_function_definition_array<'a>(
db: &dyn IrDatabase,
context: &IrValueContext,
functions: impl Iterator<Item = &'a hir::Function>,
) -> Global<[ir::FunctionDefinition]> {
let module = context.module;
functions
.map(|f| {
let name = f.name(db).to_string();
let name = f.name(db.upcast()).to_string();

// Get the function from the cloned module and modify the linkage of the function.
let value = module
Expand Down Expand Up @@ -198,7 +198,7 @@ fn gen_dispatch_table(
/// `get_info` is constructed that returns a struct `MunAssemblyInfo`. See the `mun_abi` crate
/// for the ABI that `get_info` exposes.
pub(super) fn gen_reflection_ir(
db: &impl IrDatabase,
db: &dyn IrDatabase,
context: &IrValueContext,
api: &HashSet<hir::Function>,
dispatch_table: &DispatchTable,
Expand Down Expand Up @@ -237,7 +237,7 @@ pub(super) fn gen_reflection_ir(

/// Construct the actual `get_info` function.
fn gen_get_info_fn(
db: &impl IrDatabase,
db: &dyn IrDatabase,
context: &IrValueContext,
module_info: ir::ModuleInfo,
dispatch_table: ir::DispatchTable,
Expand Down Expand Up @@ -327,7 +327,7 @@ fn gen_get_info_fn(
/// Generates a method `void set_allocator_handle(void*)` that stores the argument into the global
/// `allocatorHandle`. This global is used internally to reference the allocator used by this
/// munlib.
fn gen_set_allocator_handle_fn(db: &impl IrDatabase, context: &IrValueContext) {
fn gen_set_allocator_handle_fn(db: &dyn IrDatabase, context: &IrValueContext) {
let set_allocator_handle_fn = context.module.add_function(
"set_allocator_handle",
Value::<fn(*const u8)>::get_ir_type(context.type_context),
Expand All @@ -352,7 +352,7 @@ fn gen_set_allocator_handle_fn(db: &impl IrDatabase, context: &IrValueContext) {

/// Generates a `get_version` method that returns the current abi version.
/// Specifically, it returns the abi version the function was generated in.
fn gen_get_version_fn(db: &impl IrDatabase, context: &IrValueContext) {
fn gen_get_version_fn(db: &dyn IrDatabase, context: &IrValueContext) {
let get_version_fn = context.module.add_function(
abi::GET_VERSION_FN_NAME,
Value::<fn() -> u32>::get_ir_type(context.type_context),
Expand Down
2 changes: 1 addition & 1 deletion crates/mun_codegen/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub type StructMapping = RwLock<HashMap<(&'static str, TypeId), StructType>>;
/// The `IrDatabase` enables caching of intermediate in the process of LLVM IR generation. It uses
/// [salsa](https://github.com/salsa-rs/salsa) for this purpose.
#[salsa::query_group(IrDatabaseStorage)]
pub trait IrDatabase: hir::HirDatabase {
pub trait IrDatabase: hir::HirDatabase + hir::Upcast<dyn hir::HirDatabase> {
/// Get the LLVM context that should be used for all generation steps.
#[salsa::input]
fn context(&self) -> Arc<Context>;
Expand Down
6 changes: 3 additions & 3 deletions crates/mun_codegen/src/ir/adt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ use crate::ir::try_convert_any_to_basic;
use crate::{CodeGenParams, IrDatabase};
use inkwell::types::{BasicTypeEnum, StructType};

pub(super) fn gen_struct_decl(db: &impl IrDatabase, s: hir::Struct) -> StructType {
pub(super) fn gen_struct_decl(db: &dyn IrDatabase, s: hir::Struct) -> StructType {
let struct_type = db.struct_ty(s);
if struct_type.is_opaque() {
let field_types: Vec<BasicTypeEnum> = s
.fields(db)
.fields(db.upcast())
.iter()
.map(|field| {
let field_type = field.ty(db);
let field_type = field.ty(db.upcast());
try_convert_any_to_basic(db.type_ir(
field_type,
CodeGenParams {
Expand Down
Loading

0 comments on commit 9ad7519

Please sign in to comment.