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

variadic identity #40

Merged
merged 8 commits into from
Jul 16, 2020
Merged

variadic identity #40

merged 8 commits into from
Jul 16, 2020

Conversation

drom
Copy link
Contributor

@drom drom commented Jul 15, 2020

Work towards #33
Added identity transformations for variadic operations (and, or, xor, add, mul)

@drom drom added the HW Involving the `hw` dialect label Jul 15, 2020
@drom drom requested a review from lattner July 15, 2020 15:38
@@ -159,6 +160,7 @@ OpFoldResult ExtractOp::fold(ArrayRef<Attribute> operands) {

OpFoldResult AndOp::fold(ArrayRef<Attribute> operands) {
auto size = inputs().size();
assert(size > 0 && "rtl.and should take 1 or more operands");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit, but this should go into the verification hook for rtl.and. As a separate patch, could you write a testcase (in .mlir file) that is invalid and make sure the verifier catches this? If not, we should add verification support. @amaleewilson may be more familiar with this in MLIR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added simple verifier

Copy link
Collaborator

@lattner lattner left a comment

Choose a reason for hiding this comment

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

Nice work! Some minor changes requested

APInt value;

// and(..., '1) -> and(...) -- identity
if (matchPattern(inputs[size - 1], m_RConstant(value)) &&
Copy link
Collaborator

Choose a reason for hiding this comment

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

"inputs.back()" would be more idiomatic here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed


// and(..., '1) -> and(...) -- identity
if (matchPattern(inputs[size - 1], m_RConstant(value)) &&
value.isAllOnesValue()) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should check that size() > 1 to avoid making a zero operand And

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We get here after verifier, that will check (size < 1) and folder, that will fold (size == 1)
So we should not have (size < 2) cases at this point.
Do you want to put an assertion here?

Copy link
Collaborator

Choose a reason for hiding this comment

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

oh right, if folders are guaranteed to run first, then an assertion would be great.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added size > 1 assertion

auto size = inputs.size();
APInt value;

if (size > 1) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit, but I'd sink the "size > 1" check down into the match pattern line since it is specific to that check.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tend to remove this condition similar to the previous question.

/// TODO: mul(a, mul(...)) -> mul(a, ...) -- flatten
if (matchPattern(inputs()[size - 1], m_RConstant(value)) &&
value.isNullValue())
return inputs()[size - 1];
Copy link
Collaborator

Choose a reason for hiding this comment

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

inputs().back()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed


func @mul_identity(%arg0: i11, %arg1: i11) -> i11 {
%c1_i11 = rtl.constant(1 : i11) : i11
%0 = rtl.mul %arg0, %c1_i11, %arg1 : i11
Copy link
Collaborator

Choose a reason for hiding this comment

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

nice. Can you add one of these checks with the constant on the left hand side? The generic canonicalization stuff should move the constant to the right so we should handle that, but we should add one test for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

modified the test

@drom drom requested a review from amaleewilson July 16, 2020 03:10
Copy link
Collaborator

@lattner lattner left a comment

Choose a reason for hiding this comment

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

looks great, minor comments, feel free to commit after addressing them.

let hasFolder = 1;
let verifier = [{
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice, thank you for this.

Please pull it out to an out-of-line method (e.g. see how the other custom verifier hooks are defined in the FIRRTL dialect), and possible a separate patch

Copy link
Collaborator

Choose a reason for hiding this comment

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

Please also add a failing testcase for this to test/rtl/errors.mlir

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added negative test for zero operands case

let verifier = [{
auto size = inputs().size();
if (size < 1)
return emitOpError("requres 1 or more args");
Copy link
Collaborator

Choose a reason for hiding this comment

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

typo requres

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed


// and(..., '1) -> and(...) -- identity
if (matchPattern(inputs[size - 1], m_RConstant(value)) &&
value.isAllOnesValue()) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

oh right, if folders are guaranteed to run first, then an assertion would be great.

let verifier = [{
auto size = inputs().size();
if (size < 1)
return emitOpError("requres 1 or more args");
Copy link
Contributor

Choose a reason for hiding this comment

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

Or better yet, this should be a generic constraint. Can you propose something in mlir/include/mlir/IR/OpBase.td?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Variadics can have 1 argument, but this case is eliminated by folder

auto inputs = op.inputs();
auto size = inputs.size();

APInt value, value1;
Copy link
Contributor

Choose a reason for hiding this comment

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

value1 not used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@drom drom merged commit 2045dc3 into master Jul 16, 2020
@youngar youngar deleted the RTL-variadic-identity branch April 7, 2021 19:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
HW Involving the `hw` dialect
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants