Skip to content

Commit b2a9f52

Browse files
committed
[RTG] Add on_context and context_switch operations
1 parent 2d97537 commit b2a9f52

File tree

21 files changed

+294
-33
lines changed

21 files changed

+294
-33
lines changed

include/circt-c/Dialect/RTG.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ rtgLabelVisibilityAttrGetValue(MlirAttribute attr);
9595
MLIR_CAPI_EXPORTED MlirAttribute
9696
rtgLabelVisibilityAttrGet(MlirContext ctxt, RTGLabelVisibility visibility);
9797

98+
/// If the attribute is an RTG default context.
99+
MLIR_CAPI_EXPORTED bool rtgAttrIsADefaultContextAttr(MlirAttribute attr);
100+
101+
/// Creates an RTG default context attribute in the context.
102+
MLIR_CAPI_EXPORTED MlirAttribute rtgDefaultContextAttrGet(MlirContext ctxt,
103+
MlirType type);
104+
98105
#ifdef __cplusplus
99106
}
100107
#endif

include/circt/Dialect/RTG/IR/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,8 @@ mlir_tablegen(RTGISAAssemblyOpInterfaces.h.inc -gen-op-interface-decls)
3838
mlir_tablegen(RTGISAAssemblyOpInterfaces.cpp.inc -gen-op-interface-defs)
3939
add_public_tablegen_target(CIRCTRTGISAAssemblyOpInterfacesIncGen)
4040
add_dependencies(circt-headers CIRCTRTGISAAssemblyOpInterfacesIncGen)
41+
42+
set(LLVM_TARGET_DEFINITIONS RTGAttributes.td)
43+
mlir_tablegen(RTGAttributes.h.inc -gen-attrdef-decls)
44+
mlir_tablegen(RTGAttributes.cpp.inc -gen-attrdef-defs)
45+
add_public_tablegen_target(CIRCTRTGAttributeIncGen)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===- RTGAttributes.h - RTG dialect attributes -----------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef CIRCT_DIALECT_RTG_IR_RTGATTRIBUTES_H
10+
#define CIRCT_DIALECT_RTG_IR_RTGATTRIBUTES_H
11+
12+
#include "circt/Dialect/RTG/IR/RTGAttrInterfaces.h"
13+
#include "mlir/IR/Attributes.h"
14+
#include "mlir/IR/BuiltinAttributes.h"
15+
16+
#define GET_ATTRDEF_CLASSES
17+
#include "circt/Dialect/RTG/IR/RTGAttributes.h.inc"
18+
19+
#endif // CIRCT_DIALECT_RTG_IR_RTGATTRIBUTES_H
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//===- RTGAttributes.td - RTG attributes -------------------*- tablegen -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This describes the RTG attributes.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef CIRCT_DIALECT_RTG_IR_RTGATTRIBUTES_TD
14+
#define CIRCT_DIALECT_RTG_IR_RTGATTRIBUTES_TD
15+
16+
include "circt/Dialect/RTG/IR/RTGDialect.td"
17+
include "circt/Dialect/RTG/IR/RTGInterfaces.td"
18+
include "mlir/IR/AttrTypeBase.td"
19+
include "mlir/IR/Interfaces.td"
20+
21+
class RTGAttrDef<string name, list<Trait> traits = []>
22+
: AttrDef<RTGDialect, name, traits>;
23+
24+
25+
def DefaultContextAttr : RTGAttrDef<"DefaultContext", [
26+
DeclareAttrInterfaceMethods<ContextResourceAttrInterface>,
27+
]> {
28+
let summary = "the default context of its type";
29+
let description = [{
30+
A target must specify a default value for each context resource type it
31+
uses. The tests matched against that target then start in that context
32+
initially and 'on_context' operations can be used to switch to another
33+
context within a test. Essentially, this attribute is used within tests to
34+
refer to the default context set in the target.
35+
}];
36+
37+
let mnemonic = "default";
38+
let parameters = (ins AttributeSelfTypeParameter<"">:$type);
39+
let assemblyFormat = "";
40+
}
41+
42+
#endif // CIRCT_DIALECT_RTG_IR_RTGATTRIBUTES_TD

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ def RTGDialect : Dialect {
2525
test can, for example, be a sequence of ISA instructions to test a CPU core.
2626
}];
2727

28+
let useDefaultAttributePrinterParser = 1;
2829
let useDefaultTypePrinterParser = 1;
2930
let cppNamespace = "::circt::rtg";
3031

3132
let extraClassDeclaration = [{
33+
void registerAttributes();
3234
void registerTypes();
3335
}];
3436
}

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

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,8 @@ include "mlir/IR/Interfaces.td"
1313
include "mlir/IR/OpBase.td"
1414
include "mlir/IR/BuiltinAttributeInterfaces.td"
1515

16-
def ContextResourceOpInterface : OpInterface<"ContextResourceOpInterface"> {
17-
let description = [{
18-
This interface should be implemented by operations that define
19-
context resources. The operation should define at least one SSA value of a type
20-
implementing the `ContextResourceTypeInterface`.
21-
}];
22-
let cppNamespace = "::circt::rtg";
23-
24-
let methods = [
25-
InterfaceMethod<[{
26-
Provides a unique identifier for the defined context resource at `idx`
27-
(not counting op results that are not of a type implementing the
28-
`ContextResourceTypeInterface`).
29-
For example, if the context resource are CPUs it could be the core ID.
30-
}],
31-
"size_t", "getIdentifier", (ins "size_t":$idx)>,
32-
];
33-
}
34-
3516
/// Context resources can only be defined inside the `rtg.target` operation.
3617
def ContextResourceDefining : TraitList<[
37-
DeclareOpInterfaceMethods<ContextResourceOpInterface>,
3818
HasParent<"::circt::rtg::TargetOp">,
3919
]>;
4020

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,57 @@ def LabelOp : RTGOp<"label", []> {
197197
let assemblyFormat = "$visibility $label attr-dict";
198198
}
199199

200+
//===- Context Operations -------------------------------------------------===//
201+
202+
def OnContextOp : RTGOp<"on_context", []> {
203+
let summary = "places a sequence on a context";
204+
let description = [{
205+
This operation takes a context and a fully substituted, but not yet
206+
randomized sequence and inserts the necessary instructions to switch from
207+
the current context to the provided context, randomizes and embeds the given
208+
sequence under the given context, and inserts instructions to switch back to
209+
the original context.
210+
211+
These instructions are provided by the 'rtg.context_switch' operation. If no
212+
'rtg.context_switch' for this transition is provided, the compiler will
213+
error out. If multiple such context switches apply, the most recently
214+
registered one takes precedence.
215+
}];
216+
217+
let arguments = (ins ContextResourceTypeInterface:$context,
218+
FullySubstitutedSequenceType:$sequence);
219+
220+
let assemblyFormat = [{
221+
$context `,` $sequence `:` qualified(type($context)) attr-dict
222+
}];
223+
}
224+
225+
def ContextSwitchOp : RTGOp<"context_switch", [
226+
HasParent<"rtg::TargetOp">,
227+
]> {
228+
let summary = "a specification of how to switch contexts";
229+
let description = [{
230+
This operation allows the user to specify a sequence of instructions to
231+
switch from context 'from' to context 'to', randomize and embed a provided
232+
sequence, and switch back from context 'to' to context 'from'. This
233+
sequence of instructions should be provided as the 'sequence' operand which
234+
is a sequence of the type '!rtg.sequence<context-type-interface,
235+
context-type-interface, !rtg.sequence>'. The first parameter is the 'from'
236+
context, the second one the 'to' context, and the third is the sequence to
237+
randomize and embed under the 'to' context.
238+
}];
239+
240+
let arguments = (ins ContextResourceAttrInterface:$from,
241+
ContextResourceAttrInterface:$to,
242+
SequenceType:$sequence);
243+
244+
let assemblyFormat = [{
245+
$from `->` $to `,` $sequence `:` qualified(type($sequence)) attr-dict
246+
}];
247+
248+
let hasVerifier = 1;
249+
}
250+
200251
//===- Set Operations ------------------------------------------------------===//
201252

202253
def SetCreateOp : RTGOp<"set_create", [Pure, SameTypeOperands]> {

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class RTGOpVisitor {
3535
// Bags
3636
BagCreateOp, BagSelectRandomOp, BagDifferenceOp, BagUnionOp,
3737
BagUniqueSizeOp,
38+
// Contexts
39+
OnContextOp, ContextSwitchOp,
3840
// Labels
3941
LabelDeclOp, LabelUniqueDeclOp, LabelOp,
4042
// Registers
@@ -51,10 +53,6 @@ class RTGOpVisitor {
5153
SetSizeOp>([&](auto expr) -> ResultType {
5254
return thisCast->visitOp(expr, args...);
5355
})
54-
.template Case<ContextResourceOpInterface>(
55-
[&](auto expr) -> ResultType {
56-
return thisCast->visitContextResourceOp(expr, args...);
57-
})
5856
.Default([&](auto expr) -> ResultType {
5957
if (op->getDialect() ==
6058
op->getContext()->getLoadedDialect<RTGDialect>())
@@ -75,11 +73,6 @@ class RTGOpVisitor {
7573
/// handled by the concrete visitor.
7674
ResultType visitUnhandledOp(Operation *op, ExtraArgs... args);
7775

78-
ResultType visitContextResourceOp(ContextResourceOpInterface op,
79-
ExtraArgs... args) {
80-
return static_cast<ConcreteType *>(this)->visitUnhandledOp(op, args...);
81-
}
82-
8376
ResultType visitExternalOp(Operation *op, ExtraArgs... args) {
8477
return ResultType();
8578
}
@@ -95,6 +88,8 @@ class RTGOpVisitor {
9588
HANDLE(RandomizeSequenceOp, Unhandled);
9689
HANDLE(EmbedSequenceOp, Unhandled);
9790
HANDLE(RandomNumberInRangeOp, Unhandled);
91+
HANDLE(OnContextOp, Unhandled);
92+
HANDLE(ContextSwitchOp, Unhandled);
9893
HANDLE(SetCreateOp, Unhandled);
9994
HANDLE(SetSelectRandomOp, Unhandled);
10095
HANDLE(SetDifferenceOp, Unhandled);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ def CPUDeclOp : RTGTestOp<"cpu_decl", [
2929
]> {
3030
let summary = "declare a CPU";
3131
let description = [{
32-
This operation is used to test the `ContextResourceOpInterface` and passes
33-
taking advantage of it.
32+
This operation is used to test the `ContextResourceAttrInterface` and
33+
`ContextResourceTypeInterface` passes taking advantage of it.
3434
}];
3535

3636
let arguments = (ins CPUAttr:$id);

integration_test/Bindings/Python/dialects/rtg.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,11 @@
208208
# CHECK: rtg.label_decl "label"
209209
# CHECK: rtg.label global {{%.+}}
210210
print(m)
211+
212+
with Context() as ctx, Location.unknown():
213+
circt.register_dialects(ctx)
214+
attr = rtg.DefaultContextAttr.get(rtgtest.CPUType.get())
215+
# CHECK: !rtgtest.cpu
216+
print(attr.type)
217+
# CHECK: #rtg.default : !rtgtest.cpu
218+
print(attr)

0 commit comments

Comments
 (0)