Skip to content

Commit cb0053b

Browse files
authored
Surround moore.mir.concat with conversion casts to pass values of the type the ops expect (#246)
1 parent 064d55b commit cb0053b

File tree

3 files changed

+55
-82
lines changed

3 files changed

+55
-82
lines changed

src/circt/src/moore.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ pub fn dialect() -> DialectHandle {
3232
DialectHandle::from_raw(unsafe { mlirGetDialectHandle__moore__() })
3333
}
3434

35+
/// Check if the type is a simple bitvector type.
36+
pub fn is_simple_bitvector_type(ty: Type) -> bool {
37+
unsafe { mooreIsSimpleBitVectorType(ty.raw()) }
38+
}
39+
3540
/// Get the size of a simple bit-vector in bits.
3641
pub fn get_simple_bitvector_size(ty: Type) -> usize {
3742
assert!(unsafe { mooreIsSimpleBitVectorType(ty.raw()) });

src/svlog/codegen.rs

Lines changed: 49 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1902,11 +1902,13 @@ where
19021902
let llty = self.emit_type_both(mir.ty)?;
19031903
let mut fields = vec![];
19041904
for value in values.iter() {
1905-
fields.push(self.emit_mir_rvalue(value)?.1);
1905+
let emitted_value = self.emit_mir_rvalue(value)?.1;
1906+
fields.push(self.cast_integer_to_sbv(emitted_value));
19061907
}
1908+
let concat = circt::moore::ConcatOp::new(self.mlir_builder, fields).into();
19071909
(
19081910
self.emit_zero_for_type(&llty.0),
1909-
circt::moore::ConcatOp::new(self.mlir_builder, fields).into(),
1911+
self.cast_sbv_to_integer(concat),
19101912
)
19111913
}
19121914

@@ -3046,6 +3048,39 @@ where
30463048
}
30473049
}
30483050

3051+
fn cast_integer_to_sbv(&mut self, value: mlir::Value) -> mlir::Value {
3052+
assert!(mlir::is_integer_type(value.ty()));
3053+
self.one_op_ucc(
3054+
value,
3055+
circt::moore::get_simple_bitvector_type(
3056+
self.mcx,
3057+
false,
3058+
false,
3059+
mlir::integer_type_width(value.ty()),
3060+
),
3061+
)
3062+
}
3063+
3064+
fn cast_sbv_to_integer(&mut self, value: mlir::Value) -> mlir::Value {
3065+
assert!(circt::moore::is_simple_bitvector_type(value.ty()));
3066+
self.one_op_ucc(
3067+
value,
3068+
mlir::get_integer_type(
3069+
self.mcx,
3070+
circt::moore::get_simple_bitvector_size(value.ty()),
3071+
),
3072+
)
3073+
}
3074+
3075+
fn one_op_ucc(&mut self, value: mlir::Value, ty: mlir::Type) -> mlir::Value {
3076+
circt::builtin::UnrealizedConversionCastOp::new(
3077+
self.mlir_builder,
3078+
std::iter::once(value),
3079+
std::iter::once(ty),
3080+
)
3081+
.result(0)
3082+
}
3083+
30493084
fn mk_block(&mut self, name: Option<&str>) -> HybridBlock {
30503085
let bb = self.builder.block();
30513086
if let Some(name) = name {
@@ -3340,47 +3375,14 @@ where
33403375
amount: HybridValue,
33413376
arithmetic: bool,
33423377
) -> HybridValue {
3343-
let value_moore_type = circt::moore::get_simple_bitvector_type(
3344-
self.mcx,
3345-
false,
3346-
false,
3347-
circt::mlir::integer_type_width(value.1.ty()),
3348-
);
3349-
let amount_moore_type = circt::moore::get_simple_bitvector_type(
3350-
self.mcx,
3351-
false,
3352-
false,
3353-
circt::mlir::integer_type_width(amount.1.ty()),
3354-
);
3355-
let value_cast_to_moore = circt::builtin::UnrealizedConversionCastOp::new(
3356-
self.mlir_builder,
3357-
std::iter::once(value.1),
3358-
std::iter::once(value_moore_type),
3359-
)
3360-
.result(0);
3361-
let amount_cast_to_moore = circt::builtin::UnrealizedConversionCastOp::new(
3362-
self.mlir_builder,
3363-
std::iter::once(amount.1),
3364-
std::iter::once(amount_moore_type),
3365-
)
3366-
.result(0);
3367-
let shift = circt::moore::ShlOp::new(
3368-
self.mlir_builder,
3369-
value_cast_to_moore,
3370-
amount_cast_to_moore,
3371-
arithmetic,
3372-
)
3373-
.into();
3374-
let cast_to_llhd = circt::builtin::UnrealizedConversionCastOp::new(
3375-
self.mlir_builder,
3376-
std::iter::once(shift),
3377-
std::iter::once(value.1.ty()),
3378-
)
3379-
.result(0);
3380-
3378+
let value_casted = self.cast_integer_to_sbv(value.1);
3379+
let amount_casted = self.cast_integer_to_sbv(amount.1);
3380+
let shift =
3381+
circt::moore::ShlOp::new(self.mlir_builder, value_casted, amount_casted, arithmetic)
3382+
.into();
33813383
(
33823384
self.builder.ins().shl(value.0, hidden.0, amount.0),
3383-
cast_to_llhd,
3385+
self.cast_sbv_to_integer(shift),
33843386
)
33853387
}
33863388

@@ -3403,47 +3405,14 @@ where
34033405
amount: HybridValue,
34043406
arithmetic: bool,
34053407
) -> HybridValue {
3406-
let value_moore_type = circt::moore::get_simple_bitvector_type(
3407-
self.mcx,
3408-
false,
3409-
false,
3410-
circt::mlir::integer_type_width(value.1.ty()),
3411-
);
3412-
let amount_moore_type = circt::moore::get_simple_bitvector_type(
3413-
self.mcx,
3414-
false,
3415-
false,
3416-
circt::mlir::integer_type_width(amount.1.ty()),
3417-
);
3418-
let value_cast_to_moore = circt::builtin::UnrealizedConversionCastOp::new(
3419-
self.mlir_builder,
3420-
std::iter::once(value.1),
3421-
std::iter::once(value_moore_type),
3422-
)
3423-
.result(0);
3424-
let amount_cast_to_moore = circt::builtin::UnrealizedConversionCastOp::new(
3425-
self.mlir_builder,
3426-
std::iter::once(amount.1),
3427-
std::iter::once(amount_moore_type),
3428-
)
3429-
.result(0);
3430-
let shift = circt::moore::ShrOp::new(
3431-
self.mlir_builder,
3432-
value_cast_to_moore,
3433-
amount_cast_to_moore,
3434-
arithmetic,
3435-
)
3436-
.into();
3437-
let cast_to_llhd = circt::builtin::UnrealizedConversionCastOp::new(
3438-
self.mlir_builder,
3439-
std::iter::once(shift),
3440-
std::iter::once(value.1.ty()),
3441-
)
3442-
.result(0);
3443-
3408+
let value_casted = self.cast_integer_to_sbv(value.1);
3409+
let amount_casted = self.cast_integer_to_sbv(amount.1);
3410+
let shift =
3411+
circt::moore::ShrOp::new(self.mlir_builder, value_casted, amount_casted, arithmetic)
3412+
.into();
34443413
(
34453414
self.builder.ins().shr(value.0, hidden.0, amount.0),
3446-
cast_to_llhd,
3415+
self.cast_sbv_to_integer(shift),
34473416
)
34483417
}
34493418

test/mlir/expressions.sv

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ endfunction
1616
// CHECK-LABEL: func @Concat(
1717
// CHECK-SAME: [[X:%.+]]: i4, [[Y:%.+]]: i2, [[Z:%.+]]: i1) -> i7 {
1818
function bit [6:0] Concat(bit [3:0] x, bit [1:0] y, bit z);
19-
// CHECK-NEXT: [[TMP:%.+]] = moore.mir.concat [[X]], [[Y]], [[Z]]
20-
// CHECK-NEXT: return [[TMP]]
19+
// 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>>
2120
return {x, y, z};
2221
endfunction
2322

0 commit comments

Comments
 (0)