Skip to content

Commit

Permalink
[MooreToCore] Lower unpacked array type to hw.array
Browse files Browse the repository at this point in the history
The semantics do not exactly align between these two types, but they do
match closely enough. There is also `hw.uarray`, which could seem like a
more suitable choice; however, there are no operations defined for this
type yet.
  • Loading branch information
slowlime committed Nov 24, 2024
1 parent 87d10b7 commit 5584aa1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/Conversion/MooreToCore/MooreToCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1447,6 +1447,15 @@ static void populateTypeConversion(TypeConverter &typeConverter) {
return {};
});

// FIXME: Unpacked arrays support more element types than their packed
// variants, and as such, mapping them to hw::Array is somewhat naive. See
// also the analogous note below concerning unpacked struct type conversion.
typeConverter.addConversion([&](UnpackedArrayType type) -> std::optional<Type> {
if (auto elementType = typeConverter.convertType(type.getElementType()))
return hw::ArrayType::get(elementType, type.getSize());
return {};
});

typeConverter.addConversion([&](StructType type) -> std::optional<Type> {
SmallVector<hw::StructType::FieldInfo> fields;
for (auto field : type.getMembers()) {
Expand Down
22 changes: 22 additions & 0 deletions test/Conversion/MooreToCore/basic.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,28 @@ moore.module @Net() {
moore.assign %a, %3 : i32
}

// CHECK-LABEL: hw.module @UnpackedArray
moore.module @UnpackedArray(in %arr : !moore.uarray<2 x i32>, in %sel : !moore.i1, out c : !moore.i32) {
// CHECK: hw.array_get %arr[%sel] : !hw.array<2xi32>, i1
%0 = moore.dyn_extract %arr from %sel : !moore.uarray<2 x i32>, !moore.i1 -> !moore.i32

// CHECK: [[TRUE:%.+]] = hw.constant true
// CHECK: hw.array_get %arr[[[TRUE]]] : !hw.array<2xi32>, i1
%1 = moore.extract %arr from 1 : !moore.uarray<2 x i32> -> !moore.i32

// CHECK: [[C0:%.+]] = hw.constant 0 : i128
// CHECK: [[INIT:%.+]] = hw.bitcast [[C0]] : (i128) -> !hw.array<4xi32>
// CHECK: [[SIG:%.+]] = llhd.sig [[INIT]] : !hw.array<4xi32>
%2 = moore.variable : <uarray<4 x i32>>

// CHECK: [[C1:%.+]] = hw.constant 1 : i2
// CHECK: llhd.sig.array_get [[SIG]][[[C1]]] : !hw.inout<array<4xi32>>
%3 = moore.extract_ref %2 from 1 : !moore.ref<!moore.uarray<4 x i32>> -> !moore.ref<!moore.i32>
moore.assign %3, %0 : i32

moore.output %0 : !moore.i32
}

// CHECK-LABEL: hw.module @Struct
moore.module @Struct(in %a : !moore.i32, in %b : !moore.i32, in %arg0 : !moore.struct<{exp_bits: i32, man_bits: i32}>, in %arg1 : !moore.ref<!moore.struct<{exp_bits: i32, man_bits: i32}>>, out a : !moore.i32, out b : !moore.struct<{exp_bits: i32, man_bits: i32}>, out c : !moore.struct<{exp_bits: i32, man_bits: i32}>) {
// CHECK: hw.struct_extract %arg0["exp_bits"] : !hw.struct<exp_bits: i32, man_bits: i32>
Expand Down

0 comments on commit 5584aa1

Please sign in to comment.