Skip to content

Commit

Permalink
Surround moore.mir.concat with conversion casts to pass values of the…
Browse files Browse the repository at this point in the history
… type the ops expect (#246)
  • Loading branch information
maerhart committed Jul 21, 2022
1 parent 064d55b commit cb0053b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 82 deletions.
5 changes: 5 additions & 0 deletions src/circt/src/moore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ pub fn dialect() -> DialectHandle {
DialectHandle::from_raw(unsafe { mlirGetDialectHandle__moore__() })
}

/// Check if the type is a simple bitvector type.
pub fn is_simple_bitvector_type(ty: Type) -> bool {
unsafe { mooreIsSimpleBitVectorType(ty.raw()) }
}

/// Get the size of a simple bit-vector in bits.
pub fn get_simple_bitvector_size(ty: Type) -> usize {
assert!(unsafe { mooreIsSimpleBitVectorType(ty.raw()) });
Expand Down
129 changes: 49 additions & 80 deletions src/svlog/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1902,11 +1902,13 @@ where
let llty = self.emit_type_both(mir.ty)?;
let mut fields = vec![];
for value in values.iter() {
fields.push(self.emit_mir_rvalue(value)?.1);
let emitted_value = self.emit_mir_rvalue(value)?.1;
fields.push(self.cast_integer_to_sbv(emitted_value));
}
let concat = circt::moore::ConcatOp::new(self.mlir_builder, fields).into();
(
self.emit_zero_for_type(&llty.0),
circt::moore::ConcatOp::new(self.mlir_builder, fields).into(),
self.cast_sbv_to_integer(concat),
)
}

Expand Down Expand Up @@ -3046,6 +3048,39 @@ where
}
}

fn cast_integer_to_sbv(&mut self, value: mlir::Value) -> mlir::Value {
assert!(mlir::is_integer_type(value.ty()));
self.one_op_ucc(
value,
circt::moore::get_simple_bitvector_type(
self.mcx,
false,
false,
mlir::integer_type_width(value.ty()),
),
)
}

fn cast_sbv_to_integer(&mut self, value: mlir::Value) -> mlir::Value {
assert!(circt::moore::is_simple_bitvector_type(value.ty()));
self.one_op_ucc(
value,
mlir::get_integer_type(
self.mcx,
circt::moore::get_simple_bitvector_size(value.ty()),
),
)
}

fn one_op_ucc(&mut self, value: mlir::Value, ty: mlir::Type) -> mlir::Value {
circt::builtin::UnrealizedConversionCastOp::new(
self.mlir_builder,
std::iter::once(value),
std::iter::once(ty),
)
.result(0)
}

fn mk_block(&mut self, name: Option<&str>) -> HybridBlock {
let bb = self.builder.block();
if let Some(name) = name {
Expand Down Expand Up @@ -3340,47 +3375,14 @@ where
amount: HybridValue,
arithmetic: bool,
) -> HybridValue {
let value_moore_type = circt::moore::get_simple_bitvector_type(
self.mcx,
false,
false,
circt::mlir::integer_type_width(value.1.ty()),
);
let amount_moore_type = circt::moore::get_simple_bitvector_type(
self.mcx,
false,
false,
circt::mlir::integer_type_width(amount.1.ty()),
);
let value_cast_to_moore = circt::builtin::UnrealizedConversionCastOp::new(
self.mlir_builder,
std::iter::once(value.1),
std::iter::once(value_moore_type),
)
.result(0);
let amount_cast_to_moore = circt::builtin::UnrealizedConversionCastOp::new(
self.mlir_builder,
std::iter::once(amount.1),
std::iter::once(amount_moore_type),
)
.result(0);
let shift = circt::moore::ShlOp::new(
self.mlir_builder,
value_cast_to_moore,
amount_cast_to_moore,
arithmetic,
)
.into();
let cast_to_llhd = circt::builtin::UnrealizedConversionCastOp::new(
self.mlir_builder,
std::iter::once(shift),
std::iter::once(value.1.ty()),
)
.result(0);

let value_casted = self.cast_integer_to_sbv(value.1);
let amount_casted = self.cast_integer_to_sbv(amount.1);
let shift =
circt::moore::ShlOp::new(self.mlir_builder, value_casted, amount_casted, arithmetic)
.into();
(
self.builder.ins().shl(value.0, hidden.0, amount.0),
cast_to_llhd,
self.cast_sbv_to_integer(shift),
)
}

Expand All @@ -3403,47 +3405,14 @@ where
amount: HybridValue,
arithmetic: bool,
) -> HybridValue {
let value_moore_type = circt::moore::get_simple_bitvector_type(
self.mcx,
false,
false,
circt::mlir::integer_type_width(value.1.ty()),
);
let amount_moore_type = circt::moore::get_simple_bitvector_type(
self.mcx,
false,
false,
circt::mlir::integer_type_width(amount.1.ty()),
);
let value_cast_to_moore = circt::builtin::UnrealizedConversionCastOp::new(
self.mlir_builder,
std::iter::once(value.1),
std::iter::once(value_moore_type),
)
.result(0);
let amount_cast_to_moore = circt::builtin::UnrealizedConversionCastOp::new(
self.mlir_builder,
std::iter::once(amount.1),
std::iter::once(amount_moore_type),
)
.result(0);
let shift = circt::moore::ShrOp::new(
self.mlir_builder,
value_cast_to_moore,
amount_cast_to_moore,
arithmetic,
)
.into();
let cast_to_llhd = circt::builtin::UnrealizedConversionCastOp::new(
self.mlir_builder,
std::iter::once(shift),
std::iter::once(value.1.ty()),
)
.result(0);

let value_casted = self.cast_integer_to_sbv(value.1);
let amount_casted = self.cast_integer_to_sbv(amount.1);
let shift =
circt::moore::ShrOp::new(self.mlir_builder, value_casted, amount_casted, arithmetic)
.into();
(
self.builder.ins().shr(value.0, hidden.0, amount.0),
cast_to_llhd,
self.cast_sbv_to_integer(shift),
)
}

Expand Down
3 changes: 1 addition & 2 deletions test/mlir/expressions.sv
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ endfunction
// CHECK-LABEL: func @Concat(
// CHECK-SAME: [[X:%.+]]: i4, [[Y:%.+]]: i2, [[Z:%.+]]: i1) -> i7 {
function bit [6:0] Concat(bit [3:0] x, bit [1:0] y, bit z);
// CHECK-NEXT: [[TMP:%.+]] = moore.mir.concat [[X]], [[Y]], [[Z]]
// CHECK-NEXT: return [[TMP]]
// CHECK: moore.mir.concat %{{.+}}, %{{.+}}, %{{.+}} : (!moore.packed<range<bit, 3:0>>, !moore.packed<range<bit, 1:0>>, !moore.packed<range<bit, 0:0>>) -> !moore.packed<range<bit, 6:0>>
return {x, y, z};
endfunction

Expand Down

0 comments on commit cb0053b

Please sign in to comment.