Skip to content

Commit fb8421d

Browse files
authored
[RTG] Add BagType CAPI and Python Bindings (#7888)
1 parent 69b551d commit fb8421d

File tree

5 files changed

+51
-1
lines changed

5 files changed

+51
-1
lines changed

include/circt-c/Dialect/RTG.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ MLIR_CAPI_EXPORTED bool rtgTypeIsASet(MlirType type);
3737
/// Creates an RTG set type in the context.
3838
MLIR_CAPI_EXPORTED MlirType rtgSetTypeGet(MlirType elementType);
3939

40+
/// If the type is an RTG bag.
41+
MLIR_CAPI_EXPORTED bool rtgTypeIsABag(MlirType type);
42+
43+
/// Creates an RTG bag type in the context.
44+
MLIR_CAPI_EXPORTED MlirType rtgBagTypeGet(MlirType elementType);
45+
4046
/// If the type is an RTG dict.
4147
MLIR_CAPI_EXPORTED bool rtgTypeIsADict(MlirType type);
4248

integration_test/Bindings/Python/dialects/rtg.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import circt
55

66
from circt.dialects import rtg, rtgtest
7-
from circt.ir import Context, Location, Module, InsertionPoint, Block, StringAttr, TypeAttr
7+
from circt.ir import Context, Location, Module, InsertionPoint, Block, StringAttr, TypeAttr, IndexType
88
from circt.passmanager import PassManager
99
from circt import rtgtool_support as rtgtool
1010

@@ -77,3 +77,18 @@
7777
# CHECK: rtg.test @test_name : !rtg.dict<> {
7878
# CHECK-NEXT: }
7979
print(m)
80+
81+
with Context() as ctx, Location.unknown():
82+
circt.register_dialects(ctx)
83+
m = Module.create()
84+
with InsertionPoint(m.body):
85+
indexTy = IndexType.get()
86+
sequenceTy = rtg.SequenceType.get()
87+
setTy = rtg.SetType.get(indexTy)
88+
bagTy = rtg.BagType.get(indexTy)
89+
seq = rtg.SequenceOp('seq')
90+
Block.create_at_start(seq.bodyRegion, [sequenceTy, setTy, bagTy])
91+
92+
# CHECK: rtg.sequence @seq
93+
# CHECK: (%{{.*}}: !rtg.sequence, %{{.*}}: !rtg.set<index>, %{{.*}}: !rtg.bag<index>):
94+
print(m)

lib/Bindings/Python/RTGModule.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ void circt::python::populateDialectRTGSubmodule(py::module &m) {
4040
},
4141
py::arg("self"), py::arg("element_type"));
4242

43+
mlir_type_subclass(m, "BagType", rtgTypeIsABag)
44+
.def_classmethod(
45+
"get",
46+
[](py::object cls, MlirType elementType) {
47+
return cls(rtgBagTypeGet(elementType));
48+
},
49+
py::arg("self"), py::arg("element_type"));
50+
4351
mlir_type_subclass(m, "DictType", rtgTypeIsADict)
4452
.def_classmethod(
4553
"get",

lib/CAPI/Dialect/RTG.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ MlirType rtgSetTypeGet(MlirType elementType) {
4646
return wrap(SetType::get(ty.getContext(), ty));
4747
}
4848

49+
// BagType
50+
//===----------------------------------------------------------------------===//
51+
52+
bool rtgTypeIsABag(MlirType type) { return isa<BagType>(unwrap(type)); }
53+
54+
MlirType rtgBagTypeGet(MlirType elementType) {
55+
auto ty = unwrap(elementType);
56+
return wrap(BagType::get(ty.getContext(), ty));
57+
}
58+
4959
// DictType
5060
//===----------------------------------------------------------------------===//
5161

test/CAPI/rtg.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ static void testSetType(MlirContext ctx) {
3434
mlirTypeDump(setTy);
3535
}
3636

37+
static void testBagType(MlirContext ctx) {
38+
MlirType elTy = mlirIntegerTypeGet(ctx, 32);
39+
MlirType bagTy = rtgBagTypeGet(elTy);
40+
41+
// CHECK: is_bag
42+
fprintf(stderr, rtgTypeIsABag(bagTy) ? "is_bag\n" : "isnot_bag\n");
43+
// CHECK: !rtg.bag<i32>
44+
mlirTypeDump(bagTy);
45+
}
46+
3747
static void testDictType(MlirContext ctx) {
3848
MlirType elTy = mlirIntegerTypeGet(ctx, 32);
3949
MlirAttribute name0 =
@@ -62,6 +72,7 @@ int main(int argc, char **argv) {
6272

6373
testSequenceType(ctx);
6474
testSetType(ctx);
75+
testBagType(ctx);
6576
testDictType(ctx);
6677

6778
mlirContextDestroy(ctx);

0 commit comments

Comments
 (0)