Skip to content

Commit 0f6c244

Browse files
authored
[Moore] Add moore.uarray_cmp op (#8416)
Add a new op to handle the comparison of unpacked arrays.
1 parent 539bba9 commit 0f6c244

File tree

5 files changed

+73
-3
lines changed

5 files changed

+73
-3
lines changed

include/circt/Dialect/Moore/MooreOps.td

+37
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,15 @@ def SExtOp : ExtOpBase<"sext"> {
614614
// Expressions
615615
//===----------------------------------------------------------------------===//
616616

617+
def UArrayCmpPredicateAttr : I64EnumAttr<
618+
"UArrayCmpPredicate", "",
619+
[
620+
I64EnumAttrCase<"eq", 0>,
621+
I64EnumAttrCase<"ne", 1>
622+
]> {
623+
let cppNamespace = "circt::moore";
624+
}
625+
617626
def NegOp : MooreOp<"neg", [Pure, SameOperandsAndResultType]> {
618627
let summary = "Arithmetic negation";
619628
let description = [{
@@ -889,6 +898,34 @@ def ShlOp : ShiftOpBase<"shl"> { let summary = "Logical left shift"; }
889898
def ShrOp : ShiftOpBase<"shr"> { let summary = "Logical right shift"; }
890899
def AShrOp : ShiftOpBase<"ashr"> { let summary = "Arithmetic right shift"; }
891900

901+
def UArrayCmpOp : MooreOp<"uarray_cmp", [
902+
Pure,
903+
Commutative,
904+
SameTypeOperands,
905+
]> {
906+
let description = [{
907+
Performs an elementwise comparison of two unpacked arrays
908+
using the specified predicate (for example, "eq" for equality or "ne" for inequality)
909+
and returns a single bit result.
910+
Its first argument is an attribute that defines which type of comparison is
911+
performed. The following comparisons are supported:
912+
913+
- equal (mnemonic: `"eq"`; integer value: `0`)
914+
- not equal (mnemonic: `"ne"`; integer value: `1`)
915+
916+
The result is `1` if the comparison is true and `0` otherwise.
917+
}];
918+
let arguments = (ins
919+
UArrayCmpPredicateAttr:$predicate,
920+
UnpackedArrayType:$lhs,
921+
UnpackedArrayType:$rhs
922+
);
923+
let results = (outs BitType:$result);
924+
let assemblyFormat = [{
925+
$predicate $lhs `,` $rhs attr-dict `:` type($lhs) `->` type($result)
926+
}];
927+
}
928+
892929
class LogicalEqOpBase<string mnemonic> : MooreOp<mnemonic, [
893930
Pure,
894931
Commutative,

include/circt/Dialect/Moore/MooreTypes.td

-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,6 @@ def QueueType : MooreTypeDef<"Queue", [], "moore::UnpackedType"> {
272272
];
273273
}
274274

275-
276275
//===----------------------------------------------------------------------===//
277276
// Structs
278277
//===----------------------------------------------------------------------===//

lib/Conversion/ImportVerilog/Expressions.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,17 @@ struct RvalueExprVisitor {
290290
}
291291

292292
case BinaryOperator::Equality:
293-
return createBinary<moore::EqOp>(lhs, rhs);
293+
if (isa<moore::UnpackedArrayType>(lhs.getType()))
294+
return builder.create<moore::UArrayCmpOp>(
295+
loc, moore::UArrayCmpPredicate::eq, lhs, rhs);
296+
else
297+
return createBinary<moore::EqOp>(lhs, rhs);
294298
case BinaryOperator::Inequality:
295-
return createBinary<moore::NeOp>(lhs, rhs);
299+
if (isa<moore::UnpackedArrayType>(lhs.getType()))
300+
return builder.create<moore::UArrayCmpOp>(
301+
loc, moore::UArrayCmpPredicate::ne, lhs, rhs);
302+
else
303+
return createBinary<moore::NeOp>(lhs, rhs);
296304
case BinaryOperator::CaseEquality:
297305
return createBinary<moore::CaseEqOp>(lhs, rhs);
298306
case BinaryOperator::CaseInequality:

test/Conversion/ImportVerilog/basic.sv

+22
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,10 @@ module Expressions;
729729
bit [1:0][31:0] arrayInt;
730730
// CHECK: %uarrayInt = moore.variable : <uarray<2 x i32>>
731731
bit [31:0] uarrayInt [2];
732+
// CHECK: %arr1 = moore.variable : <uarray<2 x i32>
733+
bit [31:0] arr1 [2];
734+
// CHECK: %arr2 = moore.variable : <uarray<2 x i32>
735+
bit [31:0] arr2 [2];
732736
// CHECK: %m = moore.variable : <l4>
733737
logic [3:0] m;
734738

@@ -1147,6 +1151,24 @@ module Expressions;
11471151
// CHECK: [[TMP2:%.+]] = moore.read %e
11481152
// CHECK: moore.eq [[TMP1]], [[TMP2]] : l32 -> l1
11491153
y = d == e;
1154+
1155+
// CHECK: [[TMP0:%.+]] = moore.constant 43
1156+
// CHECK: [[TMP1:%.+]] = moore.constant 9002
1157+
// CHECK: moore.array_create [[TMP0]], [[TMP1]] : !moore.i32, !moore.i32 -> uarray<2 x i32>
1158+
arr1 = '{43, 9002};
1159+
// CHECK: [[TMP0:%.+]] = moore.constant 43
1160+
// CHECK: [[TMP1:%.+]] = moore.constant 9002
1161+
// CHECK: moore.array_create [[TMP0]], [[TMP1]] : !moore.i32, !moore.i32 -> uarray<2 x i32>
1162+
arr2 = '{43, 9002};
1163+
// CHECK: [[TMP1:%.+]] = moore.read %arr1
1164+
// CHECK: [[TMP2:%.+]] = moore.read %arr2
1165+
// CHECK: moore.uarray_cmp eq [[TMP1]], [[TMP2]] : <2 x i32> -> i1
1166+
x = arr1 == arr2;
1167+
// CHECK: [[TMP3:%.+]] = moore.read %arr1
1168+
// CHECK: [[TMP4:%.+]] = moore.read %arr2
1169+
// CHECK: moore.uarray_cmp ne [[TMP3]], [[TMP4]] : <2 x i32> -> i1
1170+
x = arr1 != arr2;
1171+
11501172
// CHECK: [[TMP1:%.+]] = moore.read %a
11511173
// CHECK: [[TMP2:%.+]] = moore.read %b
11521174
// CHECK: moore.ne [[TMP1]], [[TMP2]] : i32 -> i1

test/Dialect/Moore/basic.mlir

+4
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,14 @@ moore.module @Expressions(
214214

215215
// CHECK: moore.eq [[A]], [[B]] : i32 -> i1
216216
moore.eq %a, %b : i32 -> i1
217+
// CHECK: moore.uarray_cmp eq [[ARRAY1]], [[ARRAY1]] : <4 x i8> -> i1
218+
moore.uarray_cmp eq %array1, %array1 : <4 x i8> -> i1
217219
// CHECK: moore.ne [[A]], [[B]] : i32 -> i1
218220
moore.ne %a, %b : i32 -> i1
219221
// CHECK: moore.ne [[C]], [[D]] : l32 -> l1
220222
moore.ne %c, %d : l32 -> l1
223+
// CHECK: moore.uarray_cmp ne [[ARRAY1]], [[ARRAY1]] : <4 x i8> -> i1
224+
moore.uarray_cmp ne %array1, %array1 : <4 x i8> -> i1
221225
// CHECK: moore.case_eq [[A]], [[B]] : i32
222226
moore.case_eq %a, %b : i32
223227
// CHECK: moore.case_ne [[A]], [[B]] : i32

0 commit comments

Comments
 (0)