Skip to content

Commit

Permalink
Merge pull request #5652 from roc-lang/remove-box
Browse files Browse the repository at this point in the history
axe  box
  • Loading branch information
folkertdev authored Jul 9, 2023
2 parents b7e7059 + 6a19009 commit aa14784
Show file tree
Hide file tree
Showing 35 changed files with 155 additions and 1,583 deletions.
16 changes: 1 addition & 15 deletions crates/compiler/alias_analysis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1358,16 +1358,6 @@ fn expr_spec<'a>(

builder.add_make_named(block, MOD_APP, type_name, tag_value_id)
}
ExprBox { symbol } => {
let value_id = env.symbols[symbol];

with_new_heap_cell(builder, block, value_id)
}
ExprUnbox { symbol } => {
let tuple_id = env.symbols[symbol];

builder.add_get_tuple_field(block, tuple_id, BOX_VALUE_INDEX)
}
Struct(fields) => build_tuple_value(builder, env, block, fields),
UnionAtIndex {
index,
Expand Down Expand Up @@ -1622,7 +1612,7 @@ fn layout_spec_help<'a>(
}
}

Ptr(inner_layout) | Boxed(inner_layout) => {
Ptr(inner_layout) => {
let inner_type =
layout_spec_help(env, builder, interner, interner.get_repr(inner_layout))?;
let cell_type = builder.add_heap_cell_type();
Expand Down Expand Up @@ -1684,10 +1674,6 @@ fn static_list_type<TC: TypeContext>(builder: &mut TC) -> Result<TypeId> {
const LIST_CELL_INDEX: u32 = 0;
const LIST_BAG_INDEX: u32 = 1;

#[allow(dead_code)]
const BOX_CELL_INDEX: u32 = LIST_CELL_INDEX;
const BOX_VALUE_INDEX: u32 = LIST_BAG_INDEX;

const TAG_CELL_INDEX: u32 = 0;
const TAG_DATA_INDEX: u32 = 1;

Expand Down
4 changes: 4 additions & 0 deletions crates/compiler/gen_dev/src/generic64/aarch64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,10 @@ impl Assembler<AArch64GeneralReg, AArch64FloatReg> for AArch64Assembler {
todo!("saving floating point reg to base offset for AArch64");
}
#[inline(always)]
fn mov_base32_freg32(_buf: &mut Vec<'_, u8>, _offset: i32, _src: AArch64FloatReg) {
todo!("saving floating point reg to base offset for AArch64");
}
#[inline(always)]
fn movesd_mem64_offset32_freg64(
_buf: &mut Vec<'_, u8>,
_ptr: AArch64GeneralReg,
Expand Down
4 changes: 2 additions & 2 deletions crates/compiler/gen_dev/src/generic64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ pub trait Assembler<GeneralReg: RegTrait, FloatReg: RegTrait>: Sized + Copy {
fn mov_reg8_base32(buf: &mut Vec<'_, u8>, dst: GeneralReg, offset: i32);

fn mov_base32_freg64(buf: &mut Vec<'_, u8>, offset: i32, src: FloatReg);
fn mov_base32_freg32(buf: &mut Vec<'_, u8>, offset: i32, src: FloatReg);

fn mov_base32_reg64(buf: &mut Vec<'_, u8>, offset: i32, src: GeneralReg);
fn mov_base32_reg32(buf: &mut Vec<'_, u8>, offset: i32, src: GeneralReg);
Expand Down Expand Up @@ -4518,8 +4519,7 @@ macro_rules! single_register_layouts {
#[macro_export]
macro_rules! pointer_layouts {
() => {
LayoutRepr::Boxed(_)
| LayoutRepr::Ptr(_)
LayoutRepr::Ptr(_)
| LayoutRepr::RecursivePointer(_)
| LayoutRepr::Union(
UnionLayout::Recursive(_)
Expand Down
6 changes: 5 additions & 1 deletion crates/compiler/gen_dev/src/generic64/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,11 @@ impl<
let reg = self.load_to_float_reg(buf, sym);
ASM::mov_base32_freg64(buf, to_offset, reg);
}
FloatWidth::F32 => todo!(),
FloatWidth::F32 => {
debug_assert_eq!(to_offset % 4, 0);
let reg = self.load_to_float_reg(buf, sym);
ASM::mov_base32_freg64(buf, to_offset, reg);
}
},
Builtin::Bool => {
// same as 8-bit integer, but we special-case true/false because these symbols
Expand Down
41 changes: 41 additions & 0 deletions crates/compiler/gen_dev/src/generic64/x86_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1584,6 +1584,11 @@ impl Assembler<X86_64GeneralReg, X86_64FloatReg> for X86_64Assembler {
movsd_base64_offset32_freg64(buf, X86_64GeneralReg::RBP, offset, src)
}

#[inline(always)]
fn mov_base32_freg32(buf: &mut Vec<'_, u8>, offset: i32, src: X86_64FloatReg) {
movss_base32_offset32_freg32(buf, X86_64GeneralReg::RBP, offset, src)
}

#[inline(always)]
fn movesd_mem64_offset32_freg64(
buf: &mut Vec<'_, u8>,
Expand Down Expand Up @@ -3180,6 +3185,31 @@ fn movsd_base64_offset32_freg64(
buf.extend(offset.to_le_bytes());
}

// `MOVSS r/m64,xmm1` -> Move xmm1 to r/m64. where m64 references the base pointer.
#[inline(always)]
fn movss_base32_offset32_freg32(
buf: &mut Vec<'_, u8>,
base: X86_64GeneralReg,
offset: i32,
src: X86_64FloatReg,
) {
let rex = add_rm_extension(base, REX_W);
let rex = add_reg_extension(src, rex);
let src_mod = (src as u8 % 8) << 3;
let base_mod = base as u8 % 8;
buf.reserve(10);
buf.push(0xF3);
if src as u8 > 7 || base as u8 > 7 {
buf.push(rex);
}
buf.extend([0x0F, 0x11, 0x80 | src_mod | base_mod]);
// Using RSP or R12 requires a secondary index byte.
if base == X86_64GeneralReg::RSP || base == X86_64GeneralReg::R12 {
buf.push(0x24);
}
buf.extend(offset.to_le_bytes());
}

/// `MOVSD xmm1,r/m64` -> Move r/m64 to xmm1. where m64 references the base pointer.
#[inline(always)]
fn movsd_freg64_base64_offset32(
Expand Down Expand Up @@ -3966,6 +3996,17 @@ mod tests {
);
}

#[test]
fn test_movss_base64_offset32_freg64() {
disassembler_test!(
movss_base32_offset32_freg32,
|reg1, imm, reg2| format!("movss dword ptr [{} + 0x{:x}], {}", reg1, imm, reg2),
ALL_GENERAL_REGS,
[TEST_I32],
ALL_FLOAT_REGS
);
}

#[test]
fn test_mov_reg64_base64_offset32() {
disassembler_test!(
Expand Down
26 changes: 2 additions & 24 deletions crates/compiler/gen_dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,6 @@ impl<'a> LastSeenMap<'a> {
self.set_last_seen(*sym, stmt);
}
}
Expr::ExprBox { symbol } => {
self.set_last_seen(*symbol, stmt);
}
Expr::ExprUnbox { symbol } => {
self.set_last_seen(*symbol, stmt);
}
Expr::Struct(syms) => {
for sym in *syms {
self.set_last_seen(*sym, stmt);
Expand Down Expand Up @@ -394,7 +388,7 @@ trait Backend<'a> {
fn increment_fn_pointer(&mut self, layout: InLayout<'a>) -> Symbol {
let box_layout = self
.interner_mut()
.insert_direct_no_semantic(LayoutRepr::Boxed(layout));
.insert_direct_no_semantic(LayoutRepr::Ptr(layout));

let element_increment = self.debug_symbol("element_increment");
let element_increment_symbol = self.build_indirect_inc(layout);
Expand All @@ -414,7 +408,7 @@ trait Backend<'a> {
fn decrement_fn_pointer(&mut self, layout: InLayout<'a>) -> Symbol {
let box_layout = self
.interner_mut()
.insert_direct_no_semantic(LayoutRepr::Boxed(layout));
.insert_direct_no_semantic(LayoutRepr::Ptr(layout));

let element_decrement = self.debug_symbol("element_decrement");
let element_decrement_symbol = self.build_indirect_dec(layout);
Expand Down Expand Up @@ -842,21 +836,6 @@ trait Backend<'a> {
let reuse = reuse.map(|ru| ru.symbol);
self.tag(sym, arguments, tag_layout, *tag_id, reuse);
}
Expr::ExprBox { symbol: value } => {
let element_layout = match self.interner().get_repr(*layout) {
LayoutRepr::Boxed(boxed) => boxed,
_ => unreachable!("{:?}", self.interner().dbg(*layout)),
};

self.load_literal_symbols([*value].as_slice());
self.expr_box(*sym, *value, element_layout, None)
}
Expr::ExprUnbox { symbol: ptr } => {
let element_layout = *layout;

self.load_literal_symbols([*ptr].as_slice());
self.expr_unbox(*sym, *ptr, element_layout)
}
Expr::NullPointer => {
self.load_literal_i64(sym, 0);
}
Expand Down Expand Up @@ -1607,7 +1586,6 @@ trait Backend<'a> {
LowLevel::PtrStore => {
let element_layout = match self.interner().get_repr(arg_layouts[0]) {
LayoutRepr::Ptr(inner) => inner,
LayoutRepr::Boxed(inner) => inner,
_ => unreachable!("cannot write to {:?}", self.interner().dbg(*ret_layout)),
};

Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/gen_dev/src/object_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ fn build_exposed_generic_proc<'a, B: Backend<'a>>(backend: &mut B, proc: &Proc<'

let box_layout = backend
.interner_mut()
.insert_direct_no_semantic(roc_mono::layout::LayoutRepr::Boxed(proc.ret_layout));
.insert_direct_no_semantic(roc_mono::layout::LayoutRepr::Ptr(proc.ret_layout));

let mut args = bumpalo::collections::Vec::new_in(arena);
args.extend(proc.args);
Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/gen_llvm/src/llvm/align.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl<'a> LlvmAlignment<'a> for LayoutRepr<'a> {
.llvm_alignment_bytes(interner),
Builtin(builtin) => builtin.llvm_alignment_bytes(interner),
RecursivePointer(_) => interner.target_info().ptr_width() as u32,
Ptr(_) | Boxed(_) => interner.target_info().ptr_width() as u32,
Ptr(_) => interner.target_info().ptr_width() as u32,
}
}
}
Expand Down
36 changes: 0 additions & 36 deletions crates/compiler/gen_llvm/src/llvm/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1084,42 +1084,6 @@ pub(crate) fn build_exp_expr<'a, 'ctx>(
)
}

ExprBox { symbol } => {
let (value, layout) = scope.load_symbol_and_layout(symbol);
let basic_type =
basic_type_from_layout(env, layout_interner, layout_interner.get_repr(layout));
let allocation = reserve_with_refcount_help(
env,
basic_type,
layout_interner.stack_size(layout),
layout_interner.alignment_bytes(layout),
);

store_roc_value(
env,
layout_interner,
layout_interner.get_repr(layout),
allocation,
value,
);

allocation.into()
}

ExprUnbox { symbol } => {
let value = scope.load_symbol(symbol);

debug_assert!(value.is_pointer_value());

load_roc_value(
env,
layout_interner,
layout_interner.get_repr(layout),
value.into_pointer_value(),
"load_boxed_value",
)
}

Reset {
symbol,
update_mode,
Expand Down
4 changes: 2 additions & 2 deletions crates/compiler/gen_llvm/src/llvm/compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ fn build_eq<'a, 'ctx>(
rhs_val,
),

LayoutRepr::Ptr(inner_layout) | LayoutRepr::Boxed(inner_layout) => build_box_eq(
LayoutRepr::Ptr(inner_layout) => build_box_eq(
env,
layout_interner,
layout_ids,
Expand Down Expand Up @@ -379,7 +379,7 @@ fn build_neq<'a, 'ctx>(
result.into()
}

LayoutRepr::Ptr(inner_layout) | LayoutRepr::Boxed(inner_layout) => {
LayoutRepr::Ptr(inner_layout) => {
let is_equal = build_box_eq(
env,
layout_interner,
Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/gen_llvm/src/llvm/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn basic_type_from_layout<'a, 'ctx, 'env>(
layout_interner.get_repr(lambda_set.runtime_representation()),
),

Ptr(inner_layout) | Boxed(inner_layout) => {
Ptr(inner_layout) => {
let inner_type = basic_type_from_layout(
env,
layout_interner,
Expand Down
39 changes: 1 addition & 38 deletions crates/compiler/gen_llvm/src/llvm/expect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use roc_mono::layout::{
use roc_region::all::Region;

use super::build::BuilderExt;
use super::build::{add_func, load_roc_value, FunctionSpec, LlvmBackendMode};
use super::build::{add_func, FunctionSpec, LlvmBackendMode};
use super::convert::struct_type_from_union_layout;
use super::scope::Scope;
use super::struct_::RocStruct;
Expand Down Expand Up @@ -353,43 +353,6 @@ fn build_clone<'a, 'ctx>(
}
}

LayoutRepr::Boxed(inner_layout) => {
// write the offset
build_copy(env, ptr, cursors.offset, cursors.extra_offset.into());

let source = value.into_pointer_value();
let value = load_roc_value(
env,
layout_interner,
layout_interner.get_repr(inner_layout),
source,
"inner",
);

let inner_width = env
.ptr_int()
.const_int(layout_interner.stack_size(inner_layout) as u64, false);

let new_extra = env
.builder
.build_int_add(cursors.offset, inner_width, "new_extra");

let cursors = Cursors {
offset: cursors.extra_offset,
extra_offset: new_extra,
};

build_clone(
env,
layout_interner,
layout_ids,
ptr,
cursors,
value,
layout_interner.get_repr(inner_layout),
)
}

LayoutRepr::Ptr(_) => {
unreachable!("for internal use only")
}
Expand Down
Loading

0 comments on commit aa14784

Please sign in to comment.