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

Group constants for commutative ops #301

Merged
merged 2 commits into from
Feb 2, 2025
Merged

Conversation

Pangoraw
Copy link
Collaborator

@Pangoraw Pangoraw commented Feb 1, 2025

Closes #297.

auto lhs = op.getLhs();
auto rhs = op.getRhs();

auto lhsConst = lhs.template getDefiningOp<stablehlo::ConstantOp>();
Copy link
Member

Choose a reason for hiding this comment

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

can you do this as a matchPattern like for other constants above?

@@ -2922,6 +2922,56 @@ struct AddSimplify : public OpRewritePattern<mlir::stablehlo::AddOp> {
}
};

// ((add x cst0) cst1) -> (add x1 (add cst0 cst1))
template <typename T> struct BinOpConstSimplify : public OpRewritePattern<T> {
using OpRewritePattern<T>::OpRewritePattern;
Copy link
Member

Choose a reason for hiding this comment

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

you should template this with a fastmath flag, since while this is always valid for ints, for floats this may violate IEEE standards

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I applied only to integers for now.

// Only apply to integers
if (!isa<IntegerType>(op.getType().getElementType()))
return failure();

Copy link
Member

Choose a reason for hiding this comment

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

Fair nuff, open an issue for extending to floats?

@wsmoses wsmoses merged commit 26719d4 into EnzymeAD:main Feb 2, 2025
2 of 6 checks passed
@Pangoraw Pangoraw deleted the binop-const branch February 4, 2025 10:23
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.

Group commutative ops with constants
2 participants