Skip to content

Commit 7f8c80b

Browse files
authored
[RTG] Add set_size op for sets and bag_unique_size op for bags (#7920)
1 parent daf1bda commit 7f8c80b

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

include/circt/Dialect/RTG/IR/RTGOps.td

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,17 @@ def SetUnionOp : RTGOp<"set_union", [
159159
}];
160160
}
161161

162+
def SetSizeOp : RTGOp<"set_size", [Pure]> {
163+
let summary = "returns the number of elements in the set";
164+
165+
let arguments = (ins SetType:$set);
166+
let results = (outs Index:$result);
167+
168+
let assemblyFormat = [{
169+
$set `:` qualified(type($set)) attr-dict
170+
}];
171+
}
172+
162173
//===- Bag Operations ------------------------------------------------------===//
163174

164175
def BagCreateOp : RTGOp<"bag_create", [Pure, SameVariadicOperandSize]> {
@@ -241,6 +252,21 @@ def BagUnionOp : RTGOp<"bag_union", [
241252
}];
242253
}
243254

255+
def BagUniqueSizeOp : RTGOp<"bag_unique_size", [Pure]> {
256+
let summary = "returns the number of unique elements in the bag";
257+
let description = [{
258+
This operation returns the number of unique elements in the bag, i.e., for
259+
the bag `{a, a, b, c, c}` it returns 3.
260+
}];
261+
262+
let arguments = (ins BagType:$bag);
263+
let results = (outs Index:$result);
264+
265+
let assemblyFormat = [{
266+
$bag `:` qualified(type($bag)) attr-dict
267+
}];
268+
}
269+
244270
//===- Test Specification Operations --------------------------------------===//
245271

246272
def TestOp : RTGOp<"test", [

include/circt/Dialect/RTG/IR/RTGVisitors.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ class RTGOpVisitor {
3232
auto *thisCast = static_cast<ConcreteType *>(this);
3333
return TypeSwitch<Operation *, ResultType>(op)
3434
.template Case<SequenceOp, SequenceClosureOp, SetCreateOp,
35-
SetSelectRandomOp, SetDifferenceOp, SetUnionOp, TestOp,
36-
InvokeSequenceOp, BagCreateOp, BagSelectRandomOp,
37-
BagDifferenceOp, BagUnionOp, TargetOp, YieldOp>(
35+
SetSelectRandomOp, SetDifferenceOp, SetUnionOp,
36+
SetSizeOp, TestOp, InvokeSequenceOp, BagCreateOp,
37+
BagSelectRandomOp, BagDifferenceOp, BagUnionOp,
38+
BagUniqueSizeOp, TargetOp, YieldOp>(
3839
[&](auto expr) -> ResultType {
3940
return thisCast->visitOp(expr, args...);
4041
})
@@ -90,10 +91,12 @@ class RTGOpVisitor {
9091
HANDLE(SetSelectRandomOp, Unhandled);
9192
HANDLE(SetDifferenceOp, Unhandled);
9293
HANDLE(SetUnionOp, Unhandled);
94+
HANDLE(SetSizeOp, Unhandled);
9395
HANDLE(BagCreateOp, Unhandled);
9496
HANDLE(BagSelectRandomOp, Unhandled);
9597
HANDLE(BagDifferenceOp, Unhandled);
9698
HANDLE(BagUnionOp, Unhandled);
99+
HANDLE(BagUniqueSizeOp, Unhandled);
97100
HANDLE(TestOp, Unhandled);
98101
HANDLE(TargetOp, Unhandled);
99102
HANDLE(YieldOp, Unhandled);

test/Dialect/RTG/IR/basic.mlir

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ func.func @sets(%arg0: i32, %arg1: i32) {
3232
// CHECK: [[EMPTY:%.+]] = rtg.set_create : i32
3333
// CHECK: [[DIFF:%.+]] = rtg.set_difference [[SET]], [[EMPTY]] : !rtg.set<i32>
3434
// CHECK: rtg.set_union [[SET]], [[DIFF]] : !rtg.set<i32>
35+
// CHECK: rtg.set_size [[SET]] : !rtg.set<i32>
3536
%set = rtg.set_create %arg0, %arg1 : i32
3637
%r = rtg.set_select_random %set : !rtg.set<i32>
3738
%empty = rtg.set_create : i32
3839
%diff = rtg.set_difference %set, %empty : !rtg.set<i32>
3940
%union = rtg.set_union %set, %diff : !rtg.set<i32>
41+
%size = rtg.set_size %set : !rtg.set<i32>
4042

4143
return
4244
}
@@ -50,12 +52,14 @@ rtg.sequence @bags {
5052
// CHECK: [[DIFF:%.+]] = rtg.bag_difference [[BAG]], [[EMPTY]] : !rtg.bag<i32> {rtg.some_attr}
5153
// CHECK: rtg.bag_difference [[BAG]], [[EMPTY]] inf : !rtg.bag<i32>
5254
// CHECK: rtg.bag_union [[BAG]], [[EMPTY]], [[DIFF]] : !rtg.bag<i32>
55+
// CHECK: rtg.bag_unique_size [[BAG]] : !rtg.bag<i32>
5356
%bag = rtg.bag_create (%arg2 x %arg0, %arg2 x %arg1) : i32 {rtg.some_attr}
5457
%r = rtg.bag_select_random %bag : !rtg.bag<i32> {rtg.some_attr}
5558
%empty = rtg.bag_create : i32
5659
%diff = rtg.bag_difference %bag, %empty : !rtg.bag<i32> {rtg.some_attr}
5760
%diff2 = rtg.bag_difference %bag, %empty inf : !rtg.bag<i32>
5861
%union = rtg.bag_union %bag, %empty, %diff : !rtg.bag<i32>
62+
%size = rtg.bag_unique_size %bag : !rtg.bag<i32>
5963
}
6064

6165
// CHECK-LABEL: rtg.target @empty_target : !rtg.dict<> {

0 commit comments

Comments
 (0)