Skip to content

Commit c545b62

Browse files
committed
[RTG][CAPI] Expose getter for bag and set element type
1 parent 77fcae2 commit c545b62

File tree

6 files changed

+65
-9
lines changed

6 files changed

+65
-9
lines changed

include/circt-c/Dialect/RTG.h

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

61+
/// Return the element type of the RTG set.
62+
MLIR_CAPI_EXPORTED MlirType rtgSetTypeGetElementType(MlirType type);
63+
6164
/// If the type is an RTG bag.
6265
MLIR_CAPI_EXPORTED bool rtgTypeIsABag(MlirType type);
6366

6467
/// Creates an RTG bag type in the context.
6568
MLIR_CAPI_EXPORTED MlirType rtgBagTypeGet(MlirType elementType);
6669

70+
/// Return the element type of the RTG bag.
71+
MLIR_CAPI_EXPORTED MlirType rtgBagTypeGetElementType(MlirType type);
72+
6773
/// If the type is an RTG dict.
6874
MLIR_CAPI_EXPORTED bool rtgTypeIsADict(MlirType type);
6975

integration_test/Bindings/Python/dialects/rtg.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
m = Module.create()
1414
with InsertionPoint(m.body):
1515
cpuTy = rtgtest.CPUType.get()
16-
dictTy = rtg.DictType.get(ctx, [(StringAttr.get('cpu0'), cpuTy),
17-
(StringAttr.get('cpu1'), cpuTy)])
16+
dictTy = rtg.DictType.get([(StringAttr.get('cpu0'), cpuTy),
17+
(StringAttr.get('cpu1'), cpuTy)], ctx)
1818

1919
target = rtg.TargetOp('target_name', TypeAttr.get(dictTy))
2020
targetBlock = Block.create_at_start(target.bodyRegion, [])
@@ -44,6 +44,9 @@
4444
seq = rtg.SequenceOp('seq', TypeAttr.get(rtg.SequenceType.get([setTy])))
4545
seqBlock = Block.create_at_start(seq.bodyRegion, [setTy])
4646

47+
# CHECK: !rtg.sequence{{$}}
48+
print(setTy.element_type)
49+
4750
# CHECK: rtg.sequence @seq(%{{.*}}: !rtg.set<!rtg.sequence>) {
4851
# CHECK: }
4952
print(m)
@@ -100,6 +103,9 @@
100103
seq.bodyRegion,
101104
[sequenceTy, labelTy, setTy, bagTy, ireg, randomizedSequenceTy])
102105

106+
# CHECK: index{{$}}
107+
print(bagTy.element_type)
108+
103109
# CHECK: rtg.sequence @seq(%{{.*}}: !rtg.sequence, %{{.*}}: !rtg.label, %{{.*}}: !rtg.set<index>, %{{.*}}: !rtg.bag<index>, %{{.*}}: !rtgtest.ireg, %{{.*}}: !rtg.randomized_sequence)
104110
print(m)
105111

lib/Bindings/Python/RTGModule.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,28 @@ void circt::python::populateDialectRTGSubmodule(nb::module_ &m) {
6262
[](nb::object cls, MlirType elementType) {
6363
return cls(rtgSetTypeGet(elementType));
6464
},
65-
nb::arg("self"), nb::arg("element_type"));
65+
nb::arg("self"), nb::arg("element_type"))
66+
.def_property_readonly("element_type", [](MlirType self) {
67+
return rtgSetTypeGetElementType(self);
68+
});
6669

6770
mlir_type_subclass(m, "BagType", rtgTypeIsABag)
6871
.def_classmethod(
6972
"get",
7073
[](nb::object cls, MlirType elementType) {
7174
return cls(rtgBagTypeGet(elementType));
7275
},
73-
nb::arg("self"), nb::arg("element_type"));
76+
nb::arg("self"), nb::arg("element_type"))
77+
.def_property_readonly("element_type", [](MlirType self) {
78+
return rtgBagTypeGetElementType(self);
79+
});
7480

7581
mlir_type_subclass(m, "DictType", rtgTypeIsADict)
7682
.def_classmethod(
7783
"get",
78-
[](nb::object cls, MlirContext ctxt,
79-
const std::vector<std::pair<MlirAttribute, MlirType>> &entries) {
84+
[](nb::object cls,
85+
const std::vector<std::pair<MlirAttribute, MlirType>> &entries,
86+
MlirContext ctxt) {
8087
std::vector<MlirAttribute> names;
8188
std::vector<MlirType> types;
8289
for (auto entry : entries) {
@@ -86,9 +93,10 @@ void circt::python::populateDialectRTGSubmodule(nb::module_ &m) {
8693
return cls(
8794
rtgDictTypeGet(ctxt, types.size(), names.data(), types.data()));
8895
},
89-
nb::arg("self"), nb::arg("ctxt") = nullptr,
96+
nb::arg("self"),
9097
nb::arg("entries") =
91-
std::vector<std::pair<MlirAttribute, MlirType>>());
98+
std::vector<std::pair<MlirAttribute, MlirType>>(),
99+
nb::arg("ctxt") = nullptr);
92100

93101
nb::enum_<RTGLabelVisibility>(m, "LabelVisibility")
94102
.value("LOCAL", RTG_LABEL_VISIBILITY_LOCAL)

lib/Bindings/Python/support.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def type_to_pytype(t) -> ir.Type:
8888
if t.__class__ != ir.Type:
8989
return t
9090

91-
from .dialects import esi, hw, seq
91+
from .dialects import esi, hw, seq, rtg
9292
try:
9393
return ir.IntegerType(t)
9494
except ValueError:
@@ -129,6 +129,30 @@ def type_to_pytype(t) -> ir.Type:
129129
return esi.BundleType(t)
130130
except ValueError:
131131
pass
132+
try:
133+
return rtg.LabelType(t)
134+
except ValueError:
135+
pass
136+
try:
137+
return rtg.SetType(t)
138+
except ValueError:
139+
pass
140+
try:
141+
return rtg.BagType(t)
142+
except ValueError:
143+
pass
144+
try:
145+
return rtg.SequenceType(t)
146+
except ValueError:
147+
pass
148+
try:
149+
return rtg.RandomizedSequenceType(t)
150+
except ValueError:
151+
pass
152+
try:
153+
return rtg.DictType(t)
154+
except ValueError:
155+
pass
132156

133157
raise TypeError(f"Cannot convert {repr(t)} to python type")
134158

lib/CAPI/Dialect/RTG.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ MlirType rtgSetTypeGet(MlirType elementType) {
7979
return wrap(SetType::get(ty.getContext(), ty));
8080
}
8181

82+
MlirType rtgSetTypeGetElementType(MlirType type) {
83+
return wrap(cast<SetType>(unwrap(type)).getElementType());
84+
}
85+
8286
// BagType
8387
//===----------------------------------------------------------------------===//
8488

@@ -89,6 +93,10 @@ MlirType rtgBagTypeGet(MlirType elementType) {
8993
return wrap(BagType::get(ty.getContext(), ty));
9094
}
9195

96+
MlirType rtgBagTypeGetElementType(MlirType type) {
97+
return wrap(cast<BagType>(unwrap(type)).getElementType());
98+
}
99+
92100
// DictType
93101
//===----------------------------------------------------------------------===//
94102

test/CAPI/rtg.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ static void testSetType(MlirContext ctx) {
6464
fprintf(stderr, rtgTypeIsASet(setTy) ? "is_set\n" : "isnot_set\n");
6565
// CHECK: !rtg.set<i32>
6666
mlirTypeDump(setTy);
67+
// CHECK: i32{{$}}
68+
mlirTypeDump(rtgSetTypeGetElementType(setTy));
6769
}
6870

6971
static void testBagType(MlirContext ctx) {
@@ -74,6 +76,8 @@ static void testBagType(MlirContext ctx) {
7476
fprintf(stderr, rtgTypeIsABag(bagTy) ? "is_bag\n" : "isnot_bag\n");
7577
// CHECK: !rtg.bag<i32>
7678
mlirTypeDump(bagTy);
79+
// CHECK: i32{{$}}
80+
mlirTypeDump(rtgBagTypeGetElementType(bagTy));
7781
}
7882

7983
static void testDictType(MlirContext ctx) {

0 commit comments

Comments
 (0)