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

[FIRRTL] Introduce element-wise operations #4636

Merged
merged 1 commit into from
May 1, 2023
Merged

Conversation

uenoku
Copy link
Member

@uenoku uenoku commented Feb 7, 2023

This PR introduces element-wise operations, firrrtl.elementwise_or, firrtl.elementwise_and and firrtl.elementwise_xor to represent logical operations over 1d vector types.

  • FIRRTLOps -- type inference is added.
  • Vectorization pass -- canonicalizer vector_create (a[0] or b[0], a[1] or b[1], a[2] or b[2], ...) -> elementwise_or a, b is added.
  • LowerToHW -- elementwise_or a, b -> bitcast(comb.or (bitcast a), (bitcast b))

Example:

circuit Foo:
  module Foo:
    input a: UInt<3>[2]
    input b: UInt<3>[2]
    output c: UInt<3>[2][3]

    c[0][0] <= or(a[0], b[0])
    c[0][1] <= or(a[1], b[1])
    c[1][0] <= and(a[0], b[0])
    c[1][1] <= and(a[1], b[1])
    c[2][0] <= xor(a[0], b[0])
    c[2][1] <= xor(a[1], b[1])
firrtl.module @Foo(in %a: !firrtl.vector<uint<3>, 2>, in %b: !firrtl.vector<uint<3>, 2>, out %c_0: !firrtl.vector<uint<3>, 2>, out %c_1: !firrtl.vector<uint<3>, 2>, out %c_2: !firrtl.vector<uint<3>, 2>) {
  %0 = firrtl.elementwise_or %a, %b : (!firrtl.vector<uint<3>, 2>, !firrtl.vector<uint<3>, 2>) -> !firrtl.vector<uint<3>, 2>
  firrtl.strictconnect %c_0, %0 : !firrtl.vector<uint<3>, 2>
  %1 = firrtl.elementwise_and %a, %b : (!firrtl.vector<uint<3>, 2>, !firrtl.vector<uint<3>, 2>) -> !firrtl.vector<uint<3>, 2>
  firrtl.strictconnect %c_1, %1 : !firrtl.vector<uint<3>, 2>
  %2 = firrtl.elementwise_xor %a, %b : (!firrtl.vector<uint<3>, 2>, !firrtl.vector<uint<3>, 2>) -> !firrtl.vector<uint<3>, 2>
  firrtl.strictconnect %c_2, %2 : !firrtl.vector<uint<3>, 2>
}
module Foo(
  input  [1:0][2:0] a,
                    b,
  output [1:0][2:0] c_0,
                    c_1,
                    c_2
);

  wire [5:0] _GEN = /*cast(bit[5:0])*/a;
  wire [5:0] _GEN_0 = /*cast(bit[5:0])*/b;
  assign c_0 = /*cast(bit[1:0][2:0])*/_GEN | _GEN_0;
  assign c_1 = /*cast(bit[1:0][2:0])*/_GEN & _GEN_0;
  assign c_2 = /*ca

These temporary wires _GEN and _GEN_0 are unnecessary so ExportVerilog needs to be improved but probably we want to introduce vector operations to HW instead of relying on bitcast.

@uenoku uenoku force-pushed the dev/uenoku/elementwise branch 3 times, most recently from 1be80de to 073ff1a Compare February 8, 2023 13:58
lib/Dialect/FIRRTL/FIRRTLFolds.cpp Outdated Show resolved Hide resolved
lib/Dialect/FIRRTL/FIRRTLOps.cpp Outdated Show resolved Hide resolved
lib/Dialect/FIRRTL/Transforms/LowerTypes.cpp Show resolved Hide resolved
tools/firtool/firtool.cpp Outdated Show resolved Hide resolved
@uenoku uenoku changed the title [FIRRTL] Introduce element-wise logical operations [FIRRTL] Introduce element-wise operations Feb 9, 2023
@uenoku uenoku force-pushed the dev/uenoku/elementwise branch 3 times, most recently from e984631 to 9f88c62 Compare February 16, 2023 15:06
@uenoku
Copy link
Member Author

uenoku commented Feb 16, 2023

  • Separated the canonicalizer to Vectorization pass
  • Added a test to lowertypes.

@uenoku uenoku force-pushed the dev/uenoku/elementwise branch 3 times, most recently from c9cb22d to 893da10 Compare March 16, 2023 14:54
Copy link
Contributor

@darthscsi darthscsi left a comment

Choose a reason for hiding this comment

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

LGTM

include/circt/Dialect/FIRRTL/FIRRTLExpressions.td Outdated Show resolved Hide resolved
/// attribute. If a replaced op has a "name" attribute, this function propagates
/// the name to the new value.
template <typename OpTy, typename... Args>
static OpTy replaceOpWithNewOpAndCopyName(PatternRewriter &rewriter,
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know that we need to promote this. I know you use it below, but it's not clear that you need to (see later comments)

lib/Conversion/FIRRTLToHW/LowerToHW.cpp Show resolved Hide resolved
lib/Dialect/FIRRTL/FIRRTLFolds.cpp Outdated Show resolved Hide resolved
op->getLoc(), vectorCreateOp.getType(), lhs);
auto rhsVec = rewriter.createOrFold<VectorCreateOp>(
op->getLoc(), vectorCreateOp.getType(), rhs);
replaceOpWithNewOpAndCopyName<ResultOpType>(rewriter, op, lhsVec, rhsVec);
Copy link
Contributor

Choose a reason for hiding this comment

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

Vector create isn't going to have a name, since it can't come from Chisel/FIRRTL.

Copy link
Member Author

Choose a reason for hiding this comment

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

Vector create could have names if they are created by canonicalizing vector wires

@darthscsi
Copy link
Contributor

Status?

@uenoku uenoku merged commit 17401f2 into main May 1, 2023
@uenoku uenoku deleted the dev/uenoku/elementwise branch May 1, 2023 03:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants