Skip to content

Commit f7f3a04

Browse files
committed
[RTGTest] Add representation for immediates
1 parent c4e458f commit f7f3a04

File tree

10 files changed

+327
-0
lines changed

10 files changed

+327
-0
lines changed

include/circt-c/Dialect/RTGTest.h

+54
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,27 @@ MLIR_CAPI_EXPORTED bool rtgtestTypeIsACPU(MlirType type);
3131
/// Creates an RTGTest CPU type in the context.
3232
MLIR_CAPI_EXPORTED MlirType rtgtestCPUTypeGet(MlirContext ctxt);
3333

34+
// Immediates.
35+
//===----------------------------------------------------------------------===//
36+
37+
/// If the type is an RTGTest Imm12Type.
38+
MLIR_CAPI_EXPORTED bool rtgtestTypeIsAImm12(MlirType type);
39+
40+
/// Creates an RTGTest Imm12 type in the context.
41+
MLIR_CAPI_EXPORTED MlirType rtgtestImm12TypeGet(MlirContext ctxt);
42+
43+
/// If the type is an RTGTest Imm21Type.
44+
MLIR_CAPI_EXPORTED bool rtgtestTypeIsAImm21(MlirType type);
45+
46+
/// Creates an RTGTest Imm21 type in the context.
47+
MLIR_CAPI_EXPORTED MlirType rtgtestImm21TypeGet(MlirContext ctxt);
48+
49+
/// If the type is an RTGTest Imm32Type.
50+
MLIR_CAPI_EXPORTED bool rtgtestTypeIsAImm32(MlirType type);
51+
52+
/// Creates an RTGTest Imm32 type in the context.
53+
MLIR_CAPI_EXPORTED MlirType rtgtestImm32TypeGet(MlirContext ctxt);
54+
3455
//===----------------------------------------------------------------------===//
3556
// Attribute API.
3657
//===----------------------------------------------------------------------===//
@@ -240,6 +261,39 @@ MLIR_CAPI_EXPORTED bool rtgtestAttrIsARegT6(MlirAttribute attr);
240261
/// Creates an RTGTest RegT6 attribute in the context.
241262
MLIR_CAPI_EXPORTED MlirAttribute rtgtestRegT6AttrGet(MlirContext ctxt);
242263

264+
// Immediates.
265+
//===----------------------------------------------------------------------===//
266+
267+
/// If the attribute is an RTGTest Imm12Attr.
268+
MLIR_CAPI_EXPORTED bool rtgtestAttrIsAImm12(MlirAttribute attr);
269+
270+
/// Creates an RTGTest Imm12 attribute in the context.
271+
MLIR_CAPI_EXPORTED MlirAttribute rtgtestImm12AttrGet(MlirContext ctxt,
272+
unsigned value);
273+
274+
/// Returns the value represented by the Imm12 attribute.
275+
MLIR_CAPI_EXPORTED unsigned rtgtestImm12AttrGetValue(MlirAttribute attr);
276+
277+
/// If the attribute is an RTGTest Imm21Attr.
278+
MLIR_CAPI_EXPORTED bool rtgtestAttrIsAImm21(MlirAttribute attr);
279+
280+
/// Creates an RTGTest Imm21 attribute in the context.
281+
MLIR_CAPI_EXPORTED MlirAttribute rtgtestImm21AttrGet(MlirContext ctxt,
282+
unsigned value);
283+
284+
/// Returns the value represented by the Imm21 attribute.
285+
MLIR_CAPI_EXPORTED unsigned rtgtestImm21AttrGetValue(MlirAttribute attr);
286+
287+
/// If the attribute is an RTGTest Imm32Attr.
288+
MLIR_CAPI_EXPORTED bool rtgtestAttrIsAImm32(MlirAttribute attr);
289+
290+
/// Creates an RTGTest Imm32 attribute in the context.
291+
MLIR_CAPI_EXPORTED MlirAttribute rtgtestImm32AttrGet(MlirContext ctxt,
292+
unsigned value);
293+
294+
/// Returns the value represented by the Imm32 attribute.
295+
MLIR_CAPI_EXPORTED unsigned rtgtestImm32AttrGetValue(MlirAttribute attr);
296+
243297
#ifdef __cplusplus
244298
}
245299
#endif

include/circt/Dialect/RTGTest/IR/RTGTestAttributes.td

+34
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,40 @@ def CPUAttr : RTGTestAttrDef<"CPU", [ContextResourceAttrInterface]> {
3535
}];
3636
}
3737

38+
class ImmediateAttrBase<int width> : RTGTestAttrDef<"Imm" # width, [
39+
DeclareAttrInterfaceMethods<TypedAttrInterface>,
40+
]> {
41+
let summary = "represents a " # width # "-bit immediate value";
42+
43+
let mnemonic = "imm" # width;
44+
let parameters = (ins "uint32_t":$value);
45+
46+
let assemblyFormat = "`<` $value `>`";
47+
48+
let extraClassDefinition = [{
49+
Type $cppClass::getType() const {
50+
return Imm}] # width # [{Type::get(getContext());
51+
}
52+
53+
LogicalResult $cppClass::verify(
54+
::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError,
55+
uint32_t value) {
56+
57+
if (32 - llvm::countl_zero(value) > }] # width # [{)
58+
return emitError() << "cannot represent " << value << " with }] #
59+
width # [{ bits";
60+
61+
return success();
62+
}
63+
}];
64+
65+
let genVerifyDecl = 1;
66+
}
67+
68+
def Imm12 : ImmediateAttrBase<12>;
69+
def Imm21 : ImmediateAttrBase<21>;
70+
def Imm32 : ImmediateAttrBase<32>;
71+
3872
class IntegerRegisterAttrBase<string cppName, string name, int classIndex>
3973
: RTGTestAttrDef<cppName, [RegisterAttrInterface]> {
4074

include/circt/Dialect/RTGTest/IR/RTGTestOps.td

+15
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ def CPUDeclOp : RTGTestOp<"cpu_decl", [
3939
let hasFolder = 1;
4040
}
4141

42+
def ImmediateOp : RTGTestOp<"immediate", [
43+
Pure,
44+
ConstantLike,
45+
FirstAttrDerivedResultType,
46+
DeclareOpInterfaceMethods<InferTypeOpInterface>,
47+
]> {
48+
let summary = "declare an immediate value";
49+
50+
let arguments = (ins AnyAttrOf<[Imm12, Imm21, Imm32]>:$imm);
51+
let results = (outs AnyType:$result);
52+
53+
let assemblyFormat = "$imm attr-dict";
54+
let hasFolder = 1;
55+
}
56+
4257
def ConstantTestOp : RTGTestOp<"constant_test", [
4358
Pure, ConstantLike,
4459
]> {

include/circt/Dialect/RTGTest/IR/RTGTestTypes.td

+9
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ def CPUType : RTGTestTypeDef<"CPU", [ContextResourceTypeInterface]> {
3333
let assemblyFormat = "";
3434
}
3535

36+
class ImmTypeBase<int width> : TypeDef<RTGTestDialect, "Imm" # width, []> {
37+
let summary = "represents a " # width # "-bit immediate";
38+
let mnemonic = "imm" # width;
39+
}
40+
41+
def Imm12Type : ImmTypeBase<12>;
42+
def Imm21Type : ImmTypeBase<21>;
43+
def Imm32Type : ImmTypeBase<32>;
44+
3645
def IntegerRegisterType : TypeDef<RTGTestDialect, "IntegerRegister", [
3746
RegisterTypeInterface
3847
]> {

integration_test/Bindings/Python/dialects/rtg.py

+13
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,16 @@
165165
rtg.FixedRegisterOp(rtgtest.RegT6Attr.get())
166166

167167
print(m)
168+
169+
with Context() as ctx, Location.unknown():
170+
circt.register_dialects(ctx)
171+
m = Module.create()
172+
with InsertionPoint(m.body):
173+
# CHECK: rtgtest.immediate #rtgtest.imm12<3> : !rtgtest.imm12
174+
rtgtest.ImmediateOp(rtgtest.Imm12Attr.get(3))
175+
# CHECK: rtgtest.immediate #rtgtest.imm21<3> : !rtgtest.imm21
176+
rtgtest.ImmediateOp(rtgtest.Imm21Attr.get(3))
177+
# CHECK: rtgtest.immediate #rtgtest.imm32<3> : !rtgtest.imm32
178+
rtgtest.ImmediateOp(rtgtest.Imm32Attr.get(3))
179+
180+
print(m)

lib/Bindings/Python/RTGTestModule.cpp

+57
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,30 @@ void circt::python::populateDialectRTGTestSubmodule(nb::module_ &m) {
3030
},
3131
nb::arg("self"), nb::arg("ctxt") = nullptr);
3232

33+
mlir_type_subclass(m, "Imm12Type", rtgtestTypeIsAImm12)
34+
.def_classmethod(
35+
"get",
36+
[](nb::object cls, MlirContext ctxt) {
37+
return cls(rtgtestImm12TypeGet(ctxt));
38+
},
39+
nb::arg("self"), nb::arg("ctxt") = nullptr);
40+
41+
mlir_type_subclass(m, "Imm21Type", rtgtestTypeIsAImm21)
42+
.def_classmethod(
43+
"get",
44+
[](nb::object cls, MlirContext ctxt) {
45+
return cls(rtgtestImm21TypeGet(ctxt));
46+
},
47+
nb::arg("self"), nb::arg("ctxt") = nullptr);
48+
49+
mlir_type_subclass(m, "Imm32Type", rtgtestTypeIsAImm32)
50+
.def_classmethod(
51+
"get",
52+
[](nb::object cls, MlirContext ctxt) {
53+
return cls(rtgtestImm32TypeGet(ctxt));
54+
},
55+
nb::arg("self"), nb::arg("ctxt") = nullptr);
56+
3357
mlir_attribute_subclass(m, "CPUAttr", rtgtestAttrIsACPU)
3458
.def_classmethod(
3559
"get",
@@ -295,4 +319,37 @@ void circt::python::populateDialectRTGTestSubmodule(nb::module_ &m) {
295319
return cls(rtgtestRegT6AttrGet(ctxt));
296320
},
297321
nb::arg("self"), nb::arg("ctxt") = nullptr);
322+
323+
mlir_attribute_subclass(m, "Imm12Attr", rtgtestAttrIsAImm12)
324+
.def_classmethod(
325+
"get",
326+
[](nb::object cls, unsigned value, MlirContext ctxt) {
327+
return cls(rtgtestImm12AttrGet(ctxt, value));
328+
},
329+
nb::arg("self"), nb::arg("value"), nb::arg("ctxt") = nullptr)
330+
.def_property_readonly("value", [](MlirAttribute self) {
331+
return rtgtestImm12AttrGetValue(self);
332+
});
333+
334+
mlir_attribute_subclass(m, "Imm21Attr", rtgtestAttrIsAImm21)
335+
.def_classmethod(
336+
"get",
337+
[](nb::object cls, unsigned value, MlirContext ctxt) {
338+
return cls(rtgtestImm21AttrGet(ctxt, value));
339+
},
340+
nb::arg("self"), nb::arg("value"), nb::arg("ctxt") = nullptr)
341+
.def_property_readonly("value", [](MlirAttribute self) {
342+
return rtgtestImm21AttrGetValue(self);
343+
});
344+
345+
mlir_attribute_subclass(m, "Imm32Attr", rtgtestAttrIsAImm32)
346+
.def_classmethod(
347+
"get",
348+
[](nb::object cls, unsigned value, MlirContext ctxt) {
349+
return cls(rtgtestImm32AttrGet(ctxt, value));
350+
},
351+
nb::arg("self"), nb::arg("value"), nb::arg("ctxt") = nullptr)
352+
.def_property_readonly("value", [](MlirAttribute self) {
353+
return rtgtestImm32AttrGetValue(self);
354+
});
298355
}

lib/CAPI/Dialect/RTGTest.cpp

+60
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,27 @@ MlirType rtgtestCPUTypeGet(MlirContext ctxt) {
3232
return wrap(CPUType::get(unwrap(ctxt)));
3333
}
3434

35+
// Immediates.
36+
//===----------------------------------------------------------------------===//
37+
38+
bool rtgtestTypeIsAImm12(MlirType type) { return isa<Imm12Type>(unwrap(type)); }
39+
40+
MlirType rtgtestImm12TypeGet(MlirContext ctxt) {
41+
return wrap(Imm12Type::get(unwrap(ctxt)));
42+
}
43+
44+
bool rtgtestTypeIsAImm21(MlirType type) { return isa<Imm21Type>(unwrap(type)); }
45+
46+
MlirType rtgtestImm21TypeGet(MlirContext ctxt) {
47+
return wrap(Imm21Type::get(unwrap(ctxt)));
48+
}
49+
50+
bool rtgtestTypeIsAImm32(MlirType type) { return isa<Imm32Type>(unwrap(type)); }
51+
52+
MlirType rtgtestImm32TypeGet(MlirContext ctxt) {
53+
return wrap(Imm32Type::get(unwrap(ctxt)));
54+
}
55+
3556
//===----------------------------------------------------------------------===//
3657
// Attribute API.
3758
//===----------------------------------------------------------------------===//
@@ -306,3 +327,42 @@ bool rtgtestAttrIsARegT6(MlirAttribute attr) {
306327
MlirAttribute rtgtestRegT6AttrGet(MlirContext ctxt) {
307328
return wrap(RegT6Attr::get(unwrap(ctxt)));
308329
}
330+
331+
// Immediates.
332+
//===----------------------------------------------------------------------===//
333+
334+
bool rtgtestAttrIsAImm12(MlirAttribute attr) {
335+
return isa<Imm12Attr>(unwrap(attr));
336+
}
337+
338+
MlirAttribute rtgtestImm12AttrGet(MlirContext ctxt, unsigned value) {
339+
return wrap(Imm12Attr::get(unwrap(ctxt), value));
340+
}
341+
342+
unsigned rtgtestImm12AttrGetValue(MlirAttribute attr) {
343+
return cast<Imm12Attr>(unwrap(attr)).getValue();
344+
}
345+
346+
bool rtgtestAttrIsAImm21(MlirAttribute attr) {
347+
return isa<Imm21Attr>(unwrap(attr));
348+
}
349+
350+
MlirAttribute rtgtestImm21AttrGet(MlirContext ctxt, unsigned value) {
351+
return wrap(Imm21Attr::get(unwrap(ctxt), value));
352+
}
353+
354+
unsigned rtgtestImm21AttrGetValue(MlirAttribute attr) {
355+
return cast<Imm21Attr>(unwrap(attr)).getValue();
356+
}
357+
358+
bool rtgtestAttrIsAImm32(MlirAttribute attr) {
359+
return isa<Imm32Attr>(unwrap(attr));
360+
}
361+
362+
MlirAttribute rtgtestImm32AttrGet(MlirContext ctxt, unsigned value) {
363+
return wrap(Imm32Attr::get(unwrap(ctxt), value));
364+
}
365+
366+
unsigned rtgtestImm32AttrGetValue(MlirAttribute attr) {
367+
return cast<Imm32Attr>(unwrap(attr)).getValue();
368+
}

lib/Dialect/RTGTest/IR/RTGTestOps.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,23 @@ size_t CPUDeclOp::getIdentifier(size_t idx) { return getId().getId(); }
2525

2626
mlir::OpFoldResult CPUDeclOp::fold(FoldAdaptor adaptor) { return getId(); }
2727

28+
//===----------------------------------------------------------------------===//
29+
// ImmediateOp
30+
//===----------------------------------------------------------------------===//
31+
32+
mlir::OpFoldResult ImmediateOp::fold(FoldAdaptor adaptor) {
33+
return getImmAttr();
34+
}
35+
36+
LogicalResult ImmediateOp::inferReturnTypes(
37+
MLIRContext *context, std::optional<Location> loc, ValueRange operands,
38+
DictionaryAttr attributes, OpaqueProperties properties,
39+
mlir::RegionRange regions, SmallVectorImpl<Type> &inferredReturnTypes) {
40+
inferredReturnTypes.push_back(
41+
cast<TypedAttr>(properties.as<Properties *>()->getImm()).getType());
42+
return success();
43+
}
44+
2845
//===----------------------------------------------------------------------===//
2946
// ConstantTestOp
3047
//===----------------------------------------------------------------------===//

test/CAPI/rtgtest.c

+51
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,64 @@ static void testRegisters(MlirContext ctx) {
260260
}
261261
}
262262

263+
static void testImmediates(MlirContext ctx) {
264+
MlirType imm12Type = rtgtestImm12TypeGet(ctx);
265+
// CHECK: is_imm12
266+
fprintf(stderr,
267+
rtgtestTypeIsAImm12(imm12Type) ? "is_imm12\n" : "isnot_imm12\n");
268+
// CHECK: !rtgtest.imm12
269+
mlirTypeDump(imm12Type);
270+
271+
MlirType imm21Type = rtgtestImm21TypeGet(ctx);
272+
// CHECK: is_imm21
273+
fprintf(stderr,
274+
rtgtestTypeIsAImm21(imm21Type) ? "is_imm21\n" : "isnot_imm21\n");
275+
// CHECK: !rtgtest.imm21
276+
mlirTypeDump(imm21Type);
277+
278+
MlirType imm32Type = rtgtestImm32TypeGet(ctx);
279+
// CHECK: is_imm32
280+
fprintf(stderr,
281+
rtgtestTypeIsAImm32(imm32Type) ? "is_imm32\n" : "isnot_imm32\n");
282+
// CHECK: !rtgtest.imm32
283+
mlirTypeDump(imm32Type);
284+
285+
MlirAttribute imm12Attr = rtgtestImm12AttrGet(ctx, 3);
286+
// CHECK: is_imm12
287+
fprintf(stderr,
288+
rtgtestAttrIsAImm12(imm12Attr) ? "is_imm12\n" : "isnot_imm12\n");
289+
// CHECK: 3
290+
fprintf(stderr, "%u\n", rtgtestImm12AttrGetValue(imm12Attr));
291+
// CHECK: #rtgtest.imm12<3>
292+
mlirAttributeDump(imm12Attr);
293+
294+
MlirAttribute imm21Attr = rtgtestImm21AttrGet(ctx, 3);
295+
// CHECK: is_imm21
296+
fprintf(stderr,
297+
rtgtestAttrIsAImm21(imm21Attr) ? "is_imm21\n" : "isnot_imm21\n");
298+
// CHECK: 3
299+
fprintf(stderr, "%u\n", rtgtestImm21AttrGetValue(imm21Attr));
300+
// CHECK: #rtgtest.imm21<3>
301+
mlirAttributeDump(imm21Attr);
302+
303+
MlirAttribute imm32Attr = rtgtestImm32AttrGet(ctx, 3);
304+
// CHECK: is_imm32
305+
fprintf(stderr,
306+
rtgtestAttrIsAImm32(imm32Attr) ? "is_imm32\n" : "isnot_imm32\n");
307+
// CHECK: 3
308+
fprintf(stderr, "%u\n", rtgtestImm32AttrGetValue(imm32Attr));
309+
// CHECK: #rtgtest.imm32<3>
310+
mlirAttributeDump(imm32Attr);
311+
}
312+
263313
int main(int argc, char **argv) {
264314
MlirContext ctx = mlirContextCreate();
265315
mlirDialectHandleLoadDialect(mlirGetDialectHandle__rtgtest__(), ctx);
266316

267317
testCPUType(ctx);
268318
testCPUAttr(ctx);
269319
testRegisters(ctx);
320+
testImmediates(ctx);
270321

271322
mlirContextDestroy(ctx);
272323

0 commit comments

Comments
 (0)