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

[Comb] Truth table operation #5404

Merged
merged 2 commits into from
Jun 15, 2023
Merged
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
30 changes: 30 additions & 0 deletions include/circt/Dialect/Comb/Combinational.td
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,33 @@ def MuxOp : CombOp<"mux",
let hasFolder = true;
let hasCanonicalizer = true;
}

teqdruid marked this conversation as resolved.
Show resolved Hide resolved
def TruthTableOp : CombOp<"truth_table", [Pure]> {
let summary = "Return a true/false based on a lookup table";
let description = [{
```
%a = ... : i1
%b = ... : i1
%0 = comb.truth_table %a, %b -> [false, true, true, false]
```

This operation assumes a fully elaborated table -- 2^n entries. Inputs are
sorted MSB -> LSB from left to right and the offset into `lookupTable` is
computed from them. The table is sorted from 0 -> (2^n - 1) from left to
right.

No difference from array_get into an array of constants except for xprop
behavior. If one of the inputs is unknown, but said input doesn't make a
difference in the output (based on the lookup table) the result should not
be 'x' -- it should be the well-known result.
}];

let arguments = (ins Variadic<I1>:$inputs, BoolArrayAttr:$lookupTable);
let results = (outs I1:$result);

let assemblyFormat = [{
$inputs `->` $lookupTable attr-dict
}];

let hasVerifier = 1;
}
12 changes: 12 additions & 0 deletions lib/Dialect/Comb/CombOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,18 @@ LogicalResult ExtractOp::verify() {
return success();
}

LogicalResult TruthTableOp::verify() {
size_t numInputs = getInputs().size();
if (numInputs >= sizeof(size_t) * 8)
return emitOpError("Truth tables support a maximum of ")
<< sizeof(size_t) * 8 - 1 << " inputs on your platform";

ArrayAttr table = getLookupTable();
if (table.size() != (1ull << numInputs))
return emitOpError("Expected lookup table of 2^n length");
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: emitOpError("Expected lookup table of 2^n length") << ", n = " << numInputs;

return success();
}

//===----------------------------------------------------------------------===//
// TableGen generated logic.
//===----------------------------------------------------------------------===//
Expand Down
13 changes: 13 additions & 0 deletions test/Dialect/Comb/errors.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// RUN: circt-opt %s -split-input-file -verify-diagnostics

hw.module @err(%a: i1, %b: i1) -> () {
// expected-error @+1 {{Expected lookup table of 2^n length}}
%0 = comb.truth_table %a, %b -> [true, false]
}

// -----

hw.module @err(%a: i1, %b: i1) -> () {
// expected-error @+1 {{Truth tables support a maximum of 63 inputs}}
%0 = comb.truth_table %a, %b, %a, %b, %a, %b, %a, %b, %a, %b, %a, %b, %a, %b, %a, %b, %a, %b, %a, %b, %a, %b, %a, %b, %a, %b, %a, %b, %a, %b, %a, %b, %a, %b, %a, %b, %a, %b, %a, %b, %a, %b, %a, %b, %a, %b, %a, %b, %a, %b, %a, %b, %a, %b, %a, %b, %a, %b, %a, %b, %a, %b, %a, %b -> [false]
}