Skip to content

[MLIR][Arith] add and(a, or(a,b)) folder #138998

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

wsmoses
Copy link
Member

@wsmoses wsmoses commented May 8, 2025

No description provided.

@llvmbot
Copy link
Member

llvmbot commented May 8, 2025

@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-mlir-arith

Author: William Moses (wsmoses)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/138998.diff

1 Files Affected:

  • (modified) mlir/lib/Dialect/Arith/IR/ArithOps.cpp (+12)
diff --git a/mlir/lib/Dialect/Arith/IR/ArithOps.cpp b/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
index 3b308716c84dc..7cf65cdd4f2da 100644
--- a/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
+++ b/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
@@ -896,6 +896,18 @@ OpFoldResult arith::AndIOp::fold(FoldAdaptor adaptor) {
   if (Value result = foldAndIofAndI(*this))
     return result;
 
+  /// and(a, or(a, b)) -> a
+   for (int i = 0; i < 2; i++) {
+     auto a = getOperand(1 - i);
+     if (auto orOp = getOperand(i).getDefiningOp<arith::OrIOp>()) {
+       for (int j = 0; j < 2; j++) {
+         if (orOp->getOperand(j) == a) {
+           return a;
+         }
+       }
+     }
+   }
+
   return constFoldBinaryOp<IntegerAttr>(
       adaptor.getOperands(),
       [](APInt a, const APInt &b) { return std::move(a) & b; });

@wsmoses wsmoses force-pushed the users/wmoses/arith branch from d1d3a1a to 6775523 Compare May 8, 2025 00:35
@krzysz00
Copy link
Contributor

krzysz00 commented May 8, 2025

Question - partly for the peanut gallery - should this be a canonicalization instead of a fold?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants