Skip to content

Commit

Permalink
Replace llhd shifts with dynamic extracts and comb shifts (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
maerhart committed Jul 22, 2022
1 parent cb0053b commit fd59d66
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 104 deletions.
39 changes: 36 additions & 3 deletions src/circt/src/comb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def_simple_binary_operation!(DivUOp, "comb.divu");
def_simple_binary_operation!(DivSOp, "comb.divs");
def_simple_binary_operation!(ModUOp, "comb.modu");
def_simple_binary_operation!(ModSOp, "comb.mods");
def_simple_binary_operation!(ShlOp, "comb.shl");
def_simple_binary_operation!(ShrUOp, "comb.shru");
def_simple_binary_operation!(ShrSOp, "comb.shrs");
def_operation_single_result!(ICmpOp, "comb.icmp");
def_operation_single_result!(MuxOp, "comb.mux");
def_operation_single_result!(ExtractOp, "comb.extract");
Expand Down Expand Up @@ -106,22 +109,52 @@ impl ConcatOp {
}
}

impl ShrUOp {
pub fn with_sizes(builder: &mut Builder, value: Value, amount: Value) -> Self {
let amount = trunc_or_zext(builder, amount, value.ty());
ShrUOp::new(builder, value, amount)
}
}

impl ShrSOp {
pub fn with_sizes(builder: &mut Builder, value: Value, amount: Value) -> Self {
let amount = trunc_or_zext(builder, amount, value.ty());
ShrSOp::new(builder, value, amount)
}
}

impl ShlOp {
pub fn with_sizes(builder: &mut Builder, value: Value, amount: Value) -> Self {
let amount = trunc_or_zext(builder, amount, value.ty());
ShlOp::new(builder, value, amount)
}
}

pub(crate) fn clog2(value: usize) -> usize {
usize::BITS as usize - value.next_power_of_two().leading_zeros() as usize - 1
}

pub(crate) fn type_clog2(ty: Type) -> usize {
clog2(if is_array_type(ty) {
pub(crate) fn type_width(ty: Type) -> usize {
if is_array_type(ty) {
array_type_size(ty)
} else if is_integer_type(ty) {
integer_type_width(ty)
} else {
panic!("unsupported indexing target type {}", ty)
})
}
}

pub(crate) fn type_clog2(ty: Type) -> usize {
clog2(type_width(ty))
}

pub(crate) fn trunc_or_zext_to_clog2(builder: &mut Builder, index: Value, into_ty: Type) -> Value {
let target_width = std::cmp::max(type_clog2(into_ty), 1);
trunc_or_zext(builder, index, get_integer_type(builder.cx, target_width))
}

pub(crate) fn trunc_or_zext(builder: &mut Builder, index: Value, into_ty: Type) -> Value {
let target_width = type_width(into_ty);
let actual_width = integer_type_width(index.ty());
if target_width < actual_width {
ExtractOp::with_sizes(builder, index, 0, target_width).into()
Expand Down
26 changes: 0 additions & 26 deletions src/circt/src/llhd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,6 @@ def_operation_single_result!(LoadOp, "llhd.load");
def_operation!(StoreOp, "llhd.store");
def_operation!(HaltOp, "llhd.halt");
def_operation!(WaitOp, "llhd.wait");
def_operation_single_result!(ShlOp, "llhd.shl");
def_operation_single_result!(ShrOp, "llhd.shr");
def_operation_single_result!(SigArrayGetOp, "llhd.sig.array_get");
def_operation_single_result!(PtrArrayGetOp, "llhd.ptr.array_get");
def_binary_operation_explicit_result!(SigArraySliceOp, "llhd.sig.array_slice");
Expand Down Expand Up @@ -400,30 +398,6 @@ impl WaitOp {
}
}

impl ShlOp {
/// Shift a value to the left.
pub fn new(builder: &mut Builder, value: Value, hidden: Value, amount: Value) -> Self {
builder.build_with(|_, state| {
state.add_operand(value);
state.add_operand(hidden);
state.add_operand(amount);
state.add_result(value.ty());
})
}
}

impl ShrOp {
/// Shift a value to the right.
pub fn new(builder: &mut Builder, value: Value, hidden: Value, amount: Value) -> Self {
builder.build_with(|_, state| {
state.add_operand(value);
state.add_operand(hidden);
state.add_operand(amount);
state.add_result(value.ty());
})
}
}

impl SigExtractOp {
pub fn with_sizes(builder: &mut Builder, value: Value, offset: Value, length: usize) -> Self {
let inner_ty = signal_type_element(value.ty());
Expand Down
Loading

0 comments on commit fd59d66

Please sign in to comment.