Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RTG] Add set type and operations #7848

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions include/circt/Dialect/RTG/IR/RTGOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,48 @@ def InvokeSequenceOp : RTGOp<"invoke_sequence", []> {

let assemblyFormat = "$sequence attr-dict";
}

//===- Set Operations ------------------------------------------------------===//

def SetCreateOp : RTGOp<"set_create", [Pure, SameTypeOperands]> {
let summary = "constructs a set of the given values";

let arguments = (ins Variadic<AnyType>:$elements);
let results = (outs SetType:$set);

let hasCustomAssemblyFormat = 1;
let hasVerifier = 1;
}

def SetSelectRandomOp : RTGOp<"set_select_random", [
Pure,
TypesMatchWith<"output must be of the element type of input set",
"set", "output",
"llvm::cast<rtg::SetType>($_self).getElementType()">
]> {
let summary = "selects an element uniformly at random from a set";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this return if the set is empty?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is currently undefined behavior. My plan is to add another operation to query the number of elements in the set. scf.if could then be used to skip random selections from empty sets. Alternatively, we could pass a default value to be chosen if the set is empty. Feel free to share your thoughts/opinion on how this should ideally be handled.

let description = [{
This operation returns an element from the given set uniformly at random.
Applying this operation to an empty set is undefined behavior.
}];

let arguments = (ins SetType:$set);
let results = (outs AnyType:$output);

let assemblyFormat = "$set `:` qualified(type($set)) attr-dict";
}

def SetDifferenceOp : RTGOp<"set_difference", [
Pure,
AllTypesMatch<["original", "diff", "output"]>
]> {
let summary = "computes the difference of two sets";

let arguments = (ins SetType:$original,
SetType:$diff);
let results = (outs SetType:$output);

let assemblyFormat = [{
$original `,` $diff `:` qualified(type($output)) attr-dict
}];
}
18 changes: 18 additions & 0 deletions include/circt/Dialect/RTG/IR/RTGTypes.td
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,22 @@ def SequenceType : RTGTypeDef<"Sequence"> {
let assemblyFormat = "";
}

def SetType : RTGTypeDef<"Set"> {
let summary = "a set of values";
let description = [{
This type represents a standard set datastructure. It does not make any
assumptions about the underlying implementation. Thus a hash set, tree set,
etc. can be used in a backend.
}];

let parameters = (ins "::mlir::Type":$elementType);

let mnemonic = "set";
let assemblyFormat = "`<` $elementType `>`";
}

class SetTypeOf<Type elementType> : ContainerType<
elementType, SetType.predicate,
"llvm::cast<rtg::SetType>($_self).getElementType()", "set">;

#endif // CIRCT_DIALECT_RTG_IR_RTGTYPES_TD
40 changes: 40 additions & 0 deletions lib/Dialect/RTG/IR/RTGOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,46 @@ SequenceClosureOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
return success();
}

//===----------------------------------------------------------------------===//
// SetCreateOp
//===----------------------------------------------------------------------===//

ParseResult SetCreateOp::parse(OpAsmParser &parser, OperationState &result) {
llvm::SmallVector<OpAsmParser::UnresolvedOperand, 16> operands;
Type elemType;

if (parser.parseOperandList(operands) ||
parser.parseOptionalAttrDict(result.attributes) || parser.parseColon() ||
parser.parseType(elemType))
return failure();

result.addTypes({SetType::get(result.getContext(), elemType)});

for (auto operand : operands)
if (parser.resolveOperand(operand, elemType, result.operands))
return failure();

return success();
}

void SetCreateOp::print(OpAsmPrinter &p) {
p << " ";
p.printOperands(getElements());
p.printOptionalAttrDict((*this)->getAttrs());
p << " : " << getSet().getType().getElementType();
}

LogicalResult SetCreateOp::verify() {
if (getElements().size() > 0) {
// We only need to check the first element because of the `SameTypeOperands`
// trait.
if (getElements()[0].getType() != getSet().getType().getElementType())
return emitOpError() << "operand types must match set element type";
}

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 @@ -24,3 +24,17 @@ rtg.sequence @invocations {
rtg.invoke_sequence %0
rtg.invoke_sequence %1
}

// CHECK-LABEL: @sets
func.func @sets(%arg0: i32, %arg1: i32) {
// CHECK: [[SET:%.+]] = rtg.set_create %arg0, %arg1 : i32
// CHECK: [[R:%.+]] = rtg.set_select_random [[SET]] : !rtg.set<i32>
// CHECK: [[EMPTY:%.+]] = rtg.set_create : i32
// CHECK: rtg.set_difference [[SET]], [[EMPTY]] : !rtg.set<i32>
%set = rtg.set_create %arg0, %arg1 : i32
%r = rtg.set_select_random %set : !rtg.set<i32>
%empty = rtg.set_create : i32
%diff = rtg.set_difference %set, %empty : !rtg.set<i32>

return
}
Loading