Skip to content

Commit

Permalink
[RTG] Add sequence_closure and invoke_sequence operations
Browse files Browse the repository at this point in the history
Co-authored-by: Andrew Lenharth <andrew@lenharth.org>
  • Loading branch information
maerhart and darthscsi committed Nov 19, 2024
1 parent c13f07e commit ea51f85
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
41 changes: 41 additions & 0 deletions include/circt/Dialect/RTG/IR/RTGOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,44 @@ def SequenceOp : RTGOp<"sequence", [
$sym_name attr-dict-with-keyword $bodyRegion
}];
}

def SequenceClosureOp : RTGOp<"sequence_closure", [
Pure,
DeclareOpInterfaceMethods<SymbolUserOpInterface>
]> {
let summary = "create a sequence closure with the provided arguments";
let description = [{
This operation creates a closure object for the provided sequence and
arguments. This allows sequences to be passed around as an SSA value.
For example, it can be inserted into a set and selected at random which
is on of the main ways to do randomization. Not having to deal with
sequence arguments after randomly selecting a sequence simplifies the
problem of coming up with values to pass as arguments, but also provides a
way for the user to constrain the arguments at the location where they are
added to the set. In the future, it can also be possible to add sequence
handles directly to a set and randomly pick arguments at the invokation
site.
}];

let arguments = (ins SymbolNameAttr:$sequence, Variadic<AnyType>:$args);
let results = (outs SequenceType:$ref);

let assemblyFormat = [{
$sequence (`(` $args^ `:` qualified(type($args)) `)`)? attr-dict
}];
}

def InvokeSequenceOp : RTGOp<"invoke_sequence", []> {
let summary = "invoke a sequence of instructions";
let description = [{
This operation takes a sequence closure as operand and acts as a placeholder
for that sequence instantiated with the arguments in the closure in place.
In particular, this is not any kind of function call, it doesn't set up a
stack frame, etc. It behaves as if the sequence of instructions it refers to
were directly inlined relacing this operation.
}];

let arguments = (ins SequenceType:$sequence);

let assemblyFormat = "$sequence attr-dict";
}
20 changes: 20 additions & 0 deletions lib/Dialect/RTG/IR/RTGOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,26 @@ using namespace mlir;
using namespace circt;
using namespace rtg;

//===----------------------------------------------------------------------===//
// SequenceClosureOp
//===----------------------------------------------------------------------===//

LogicalResult
SequenceClosureOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
SequenceOp seq =
symbolTable.lookupNearestSymbolFrom<SequenceOp>(*this, getSequenceAttr());
if (!seq)
return emitOpError()
<< "'" << getSequence()
<< "' does not reference a valid 'rtg.sequence' operation";

if (seq.getBodyRegion().getArgumentTypes() != getArgs().getTypes())
return emitOpError("referenced 'rtg.sequence' op's argument types must "
"match 'args' types");

return success();
}

//===----------------------------------------------------------------------===//
// TableGen generated logic.
//===----------------------------------------------------------------------===//
Expand Down
14 changes: 14 additions & 0 deletions test/Dialect/RTG/IR/basic.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,17 @@ rtg.sequence @seq0 attributes {rtg.some_attr} {
rtg.sequence @seq1 {
^bb0(%arg0: i32, %arg1: !rtg.sequence):
}

// CHECK-LABEL: rtg.sequence @invokations
rtg.sequence @invokations {
// CHECK: [[V0:%.+]] = rtg.sequence_closure @seq0
// CHECK: [[C0:%.+]] = arith.constant 0 : i32
// CHECK: [[V1:%.+]] = rtg.sequence_closure @seq1([[C0]], [[V0]] : i32, !rtg.sequence)
// CHECK: rtg.invoke_sequence [[V0]]
// CHECK: rtg.invoke_sequence [[V1]]
%0 = rtg.sequence_closure @seq0
%c0_i32 = arith.constant 0 : i32
%1 = rtg.sequence_closure @seq1(%c0_i32, %0 : i32, !rtg.sequence)
rtg.invoke_sequence %0
rtg.invoke_sequence %1
}
18 changes: 18 additions & 0 deletions test/Dialect/RTG/IR/errors.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// RUN: circt-opt %s --split-input-file --verify-diagnostics


func.func @seq0() {
return
}

// expected-error @below {{'seq0' does not reference a valid 'rtg.sequence' operation}}
rtg.sequence_closure @seq0

// -----

rtg.sequence @seq0 {
^bb0(%arg0: i32):
}

// expected-error @below {{referenced 'rtg.sequence' op's argument types must match 'args' types}}
rtg.sequence_closure @seq0

0 comments on commit ea51f85

Please sign in to comment.