Skip to content

Commit

Permalink
Delete AliasMask, which was unused. [Issue 14] will make this obsol…
Browse files Browse the repository at this point in the history
…ete.

[Issue 14]: #14
  • Loading branch information
Alistair Turnbull committed Apr 13, 2023
1 parent 1b7f33c commit d088989
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 131 deletions.
33 changes: 13 additions & 20 deletions src/beetle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use memoffset::{offset_of};

use super::code::{self, UnaryOp, BinaryOp, Width, Register, REGISTERS, Global, EBB, Marshal};
use super::code::{UnaryOp, BinaryOp, Width, Register, REGISTERS, Global, EBB, Marshal};
use UnaryOp::*;
use BinaryOp::*;
use Width::*;
Expand Down Expand Up @@ -46,13 +46,6 @@ const NOT_IMPLEMENTED: i64 = 0;
/// Dummy return code which should never actually occur.
const UNDEFINED: i64 = i64::MAX;

/// Beetle's address space is unified, so we always use the same `AliasMask`.
const AM_MEMORY: code::AliasMask = code::AliasMask(0x1);

/// Beetle's registers are not in Beetle's memory, so we use a different
/// `AliasMask`.
const AM_REGISTER: code::AliasMask = code::AliasMask(0x2);

//-----------------------------------------------------------------------------

/// Computes into `BI` the native address corresponding to `addr`.
Expand All @@ -63,13 +56,13 @@ fn native_address(b: &mut Builder<EntryId>, addr: Register) {
/// Loads `dest` from `addr`. `BI` is corrupted.
fn load(b: &mut Builder<EntryId>, dest: Register, addr: Register) {
native_address(b, addr);
b.load(dest, (BI, 0), Four, AM_MEMORY);
b.load(dest, (BI, 0), Four);
}

/// Stores `dest` at `addr`. `BI` is corrupted.
fn store(b: &mut Builder<EntryId>, src: Register, addr: Register) {
native_address(b, addr);
b.store(src, (BI, 0), Four, AM_MEMORY);
b.store(src, (BI, 0), Four);
}

/// Pops `dest` from the stack at `sp`. `BI` is corrupted.
Expand Down Expand Up @@ -97,19 +90,19 @@ impl<T: Target> Beetle<T> {
let mut jit = Jit::new(target, 2);
let marshal = Marshal {
prologue: build_block(|b| {
b.load(BEP, register!(ep), Four, AM_REGISTER);
b.load(BI, register!(i), Four, AM_REGISTER);
b.load(BA, register!(a), Four, AM_REGISTER);
b.load(BSP, register!(sp), Four, AM_REGISTER);
b.load(BRP, register!(rp), Four, AM_REGISTER);
b.load(BEP, register!(ep), Four);
b.load(BI, register!(i), Four);
b.load(BA, register!(a), Four);
b.load(BSP, register!(sp), Four);
b.load(BRP, register!(rp), Four);
b.move_(M0, Global(1));
}),
epilogue: build_block(|b| {
b.store(BEP, register!(ep), Four, AM_REGISTER);
b.store(BI, register!(i), Four, AM_REGISTER);
b.store(BA, register!(a), Four, AM_REGISTER);
b.store(BSP, register!(sp), Four, AM_REGISTER);
b.store(BRP, register!(rp), Four, AM_REGISTER);
b.store(BEP, register!(ep), Four);
b.store(BI, register!(i), Four);
b.store(BA, register!(a), Four);
b.store(BSP, register!(sp), Four);
b.store(BRP, register!(rp), Four);
b.move_(Global(1), M0);
}),
};
Expand Down
10 changes: 5 additions & 5 deletions src/code/action.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Register, Variable, Precision, UnaryOp, BinaryOp, Width, AliasMask};
use super::{Register, Variable, Precision, UnaryOp, BinaryOp, Width};

/// Called by [`Action::Debug`].
#[no_mangle]
Expand All @@ -19,10 +19,10 @@ pub enum Action {
/// dest <- op(src1, src2)
Binary(BinaryOp, Precision, Register, Variable, Variable),
/// dest <- \[addr]
Load(Register, (Variable, Width), AliasMask),
Load(Register, (Variable, Width)),
/// dest <- addr; \[addr] <- \[src]
/// `dest` exists to make the optimizer allocate a temporary register.
Store(Register, Variable, (Variable, Width), AliasMask),
Store(Register, Variable, (Variable, Width)),
/// sp <- sp - 16; \[sp] <- src1; \[sp + 8] <- src2
/// If either `src` is `None`, push a dead value.
Push(Option<Variable>, Option<Variable>),
Expand All @@ -43,9 +43,9 @@ impl std::fmt::Debug for Action {
write!(f, "{:?}_{:?} {:?}, {:?}", op, prec, dest, src),
Action::Binary(op, prec, dest, src1, src2) =>
write!(f, "{:?}_{:?} {:?}, {:?}, {:?}", op, prec, dest, src1, src2),
Action::Load(dest, (addr, width), _) =>
Action::Load(dest, (addr, width)) =>
write!(f, "Load_{:?} {:?}, [{:?}]", width, dest, addr),
Action::Store(dest, src, (addr, width), _) =>
Action::Store(dest, src, (addr, width)) =>
write!(f, "Store_{:?} {:?}, {:?}, [{:?}]", width, dest, src, addr),
Action::Push(src1, src2) =>
write!(f, "Push ({:?}, {:?})", src1, src2),
Expand Down
20 changes: 7 additions & 13 deletions src/code/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! be useful.

use super::{
UnaryOp, BinaryOp, Precision, Width, AliasMask,
UnaryOp, BinaryOp, Precision, Width,
Register, REGISTERS, Variable,
Action, Switch, EBB, Ending,
};
Expand Down Expand Up @@ -177,11 +177,10 @@ impl<T> Builder<T> {
dest: impl Into<Register>,
addr: (impl Into<Variable>, i64),
width: Width,
am: AliasMask,
) {
let dest = dest.into();
self.const_binary64(Add, dest, addr.0, addr.1);
self.actions.push(Action::Load(dest, (dest.into(), width), am));
self.actions.push(Action::Load(dest, (dest.into(), width)));
}

/// Assembles `Action`s to compute `addr.0 + addr.1` into `dest` and to
Expand All @@ -192,10 +191,9 @@ impl<T> Builder<T> {
src: impl Into<Variable>,
addr: (impl Into<Variable>, i64),
width: Width,
am: AliasMask,
) {
self.const_binary64(Add, TEMP, addr.0, addr.1);
self.actions.push(Action::Store(TEMP, src.into(), (TEMP.into(), width), am));
self.actions.push(Action::Store(TEMP, src.into(), (TEMP.into(), width)));
}

/// Assembles `Action`s to load `dest` from `addr.0 + width * addr.1`.
Expand All @@ -205,11 +203,10 @@ impl<T> Builder<T> {
dest: impl Into<Register>,
addr: (impl Into<Variable>, impl Into<Variable>),
width: Width,
am: AliasMask,
) {
self.const_binary64(Lsl, TEMP, addr.1, width as i64);
self.binary64(Add, TEMP, addr.0, TEMP);
self.actions.push(Action::Load(dest.into(), (TEMP.into(), width), am));
self.actions.push(Action::Load(dest.into(), (TEMP.into(), width)));
}

/// Assembles `Action`s to compute `addr.0 + width * addr.1` into `dest` and
Expand All @@ -220,11 +217,10 @@ impl<T> Builder<T> {
src: impl Into<Variable>,
addr: (impl Into<Variable>, impl Into<Variable>),
width: Width,
am: AliasMask,
) {
self.const_binary64(Lsl, TEMP, addr.1, width as i64);
self.binary64(Add, TEMP, addr.0, TEMP);
self.actions.push(Action::Store(TEMP, src.into(), (TEMP.into(), width), am));
self.actions.push(Action::Store(TEMP, src.into(), (TEMP.into(), width)));
}

fn increment (
Expand Down Expand Up @@ -256,12 +252,11 @@ impl<T> Builder<T> {
dest: impl Into<Register>,
addr: impl Into<Register>,
width: Width,
am: AliasMask,
) {
let dest = dest.into();
let addr = addr.into();
self.increment(increment, addr, 1 << (width as usize), |b| {
b.load(dest, (addr, 0), width, am);
b.load(dest, (addr, 0), width);
});
}

Expand All @@ -274,12 +269,11 @@ impl<T> Builder<T> {
src: impl Into<Variable>,
addr: impl Into<Register>,
width: Width,
am: AliasMask,
) {
let src = src.into();
let addr = addr.into();
self.increment(increment, addr, 1 << (width as usize), |b| {
b.store(src, (addr, 0), width, am);
b.store(src, (addr, 0), width);
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/code/convention.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ impl Propagator {
self.insert(src1);
self.insert(src2);
},
Load(dest, (addr, _), _) => {
Load(dest, (addr, _)) => {
self.remove(dest);
self.insert(addr);
},
Store(dest, src, (addr, _), _) => {
Store(dest, src, (addr, _)) => {
self.remove(dest);
self.insert(src);
self.insert(addr);
Expand Down
51 changes: 0 additions & 51 deletions src/code/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,54 +55,3 @@ pub enum Width {
Four = 2,
Eight = 3,
}

//-----------------------------------------------------------------------------

/// Indicates which parts of memory overlap with each other. More precisely,
/// indicates whether the value loaded from one address can be affected by a
/// store to another address.
///
/// Every [`Action::Load`] and [`Action::Store`] instruction is annotated with
/// an `AliasMask`, which is internally a bitmask. If the `AliasMask`s of two
/// memory accesses have any set bits in common, and one of them is a `Store`,
/// and if the optmizer cannot prove that they access different addresses, then
/// the optimizer will not reorder the two instructions.
///
/// It is allowed, but unhelpful, for every `AliasMask` to have all bits set.
/// This will force all memory accesses to occur in the order they are written.
///
/// If all stores to some address precede all loads from it, then it is
/// encouraged to give all those memory accesses an `AliasMask` of zero.
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct AliasMask(pub u32);

impl AliasMask {
/// Tests whether `self` and `other` have any bits in common.
pub fn can_alias(self, other: Self) -> bool {
self.0 & other.0 != 0
}
}

impl std::ops::BitAnd for AliasMask {
type Output = Self;

fn bitand(self, rhs: Self) -> Self::Output {
AliasMask(self.0 & rhs.0)
}
}

impl std::ops::BitOr for AliasMask {
type Output = Self;

fn bitor(self, rhs: Self) -> Self::Output {
AliasMask(self.0 | rhs.0)
}
}

impl std::ops::BitXor for AliasMask {
type Output = Self;

fn bitxor(self, rhs: Self) -> Self::Output {
AliasMask(self.0 ^ rhs.0)
}
}
2 changes: 1 addition & 1 deletion src/code/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ mod variable;
pub use variable::{Register, REGISTERS, Global, Slot, Variable, IntoVariable};

mod enums;
pub use enums::{Precision, UnaryOp, BinaryOp, Width, AliasMask};
pub use enums::{Precision, UnaryOp, BinaryOp, Width};

mod action;
pub use action::{Action, debug_word};
Expand Down
4 changes: 2 additions & 2 deletions src/optimizer/builder/fill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ pub fn with_fill<T>(
mod tests {
use super::*;
use super::super::{code, Op};
use code::{Precision, BinaryOp, Width, AliasMask};
use code::{Precision, BinaryOp, Width};
use Precision::*;
use crate::util::{AsUsize};

Expand All @@ -161,7 +161,7 @@ mod tests {
let add = df.add_node(Op::Binary(P64, BinaryOp::Add), &[], &[a, b], 1);
let c = df.outs(add).next().unwrap();
let exit1 = df.add_node(Op::Convention, &[guard, entry], &[c], 0);
let store = df.add_node(Op::Store(Width::Eight, AliasMask(1)), &[entry, guard], &[b, a], 1);
let store = df.add_node(Op::Store(Width::Eight), &[entry, guard], &[b, a], 1);
let d = df.outs(store).next().unwrap();
let exit2 = df.add_node(Op::Convention, &[guard, store], &[d], 0);
let _ = df.add_node(Op::Binary(P64, BinaryOp::Mul), &[], &[b, b], 1);
Expand Down
4 changes: 2 additions & 2 deletions src/optimizer/cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ pub fn op_cost(op: Op) -> &'static Cost {
Lsl | Lsr | Asr => &SHIFT_COST,
Lt | Ult | Eq | Max | Min => &CONDITIONAL_COST,
},
Load(_, _) => &LOAD_COST,
Store(_, _) => &STORE_COST,
Load(_) => &LOAD_COST,
Store(_) => &STORE_COST,
Debug => &DEBUG_COST,
}
}
14 changes: 7 additions & 7 deletions src/optimizer/op.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::code::{Register, Variable, Precision, UnaryOp, BinaryOp, Width, AliasMask, Action};
use super::code::{Register, Variable, Precision, UnaryOp, BinaryOp, Width, Action};

/// Annotates a [`Node`] of a [`Dataflow`] graph.
///
Expand All @@ -18,8 +18,8 @@ pub enum Op {
Constant(i64),
Unary(Precision, UnaryOp),
Binary(Precision, BinaryOp),
Load(Width, AliasMask),
Store(Width, AliasMask),
Load(Width),
Store(Width),
Debug,
}

Expand Down Expand Up @@ -47,15 +47,15 @@ impl Op {
assert_eq!(ins.len(), 2);
Action::Binary(op, prec, outs[0], ins[0], ins[1])
},
Op::Load(width, alias) => {
Op::Load(width) => {
assert_eq!(outs.len(), 1);
assert_eq!(ins.len(), 1);
Action::Load(outs[0], (ins[0], width), alias)
Action::Load(outs[0], (ins[0], width))
},
Op::Store(width, alias) => {
Op::Store(width) => {
assert_eq!(outs.len(), 1);
assert_eq!(ins.len(), 2);
Action::Store(outs[0], ins[0], (ins[1], width), alias)
Action::Store(outs[0], ins[0], (ins[1], width))
},
Op::Debug => {
assert_eq!(outs.len(), 0);
Expand Down
10 changes: 4 additions & 6 deletions src/optimizer/simulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,16 @@ impl Simulation {
Action::Binary(bin_op, prec, dest, src1, src2) => {
let _ = self.op(dataflow, Op::Binary(prec, bin_op), &[], &[src1, src2], &[dest]);
},
Action::Load(dest, (addr, width), alias_mask) => {
// TODO: Use AliasMask.
let node = self.op(dataflow, Op::Load(width, alias_mask), &[self.sequence, self.store], &[addr], &[dest]);
Action::Load(dest, (addr, width)) => {
let node = self.op(dataflow, Op::Load(width), &[self.sequence, self.store], &[addr], &[dest]);
self.loads.push(node);
},
Action::Store(dest, src, (addr, width), alias_mask) => {
// TODO: Use AliasMask.
Action::Store(dest, src, (addr, width)) => {
let mut deps = Vec::new();
std::mem::swap(&mut deps, &mut self.loads);
deps.push(self.sequence);
deps.push(self.store);
let node = self.op(dataflow, Op::Store(width, alias_mask), &deps, &[src, addr], &[dest]);
let node = self.op(dataflow, Op::Store(width), &deps, &[src, addr], &[dest]);
self.move_(dest.into(), src);
self.store = node;
},
Expand Down
4 changes: 2 additions & 2 deletions src/target/aarch64/lowerer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,13 +440,13 @@ impl<B: Buffer> super::Lower for Lowerer<B> {
Action::Binary(op, prec, dest, src1, src2) => {
self.binary_op(op, prec, dest, src1, src2);
},
Action::Load(dest, (addr, width), _) => {
Action::Load(dest, (addr, width)) => {
let dest = dest.into();
let base = self.src_to_register(addr, dest);
let offset = Offset::new(width, 0).unwrap();
self.a.mem(LDR, dest, (base, offset));
},
Action::Store(dest, src, (addr, width), _) => {
Action::Store(dest, src, (addr, width)) => {
let dest = Register::from(dest);
let src = self.src_to_register(src, TEMP0);
let base = if dest == src {
Expand Down

0 comments on commit d088989

Please sign in to comment.