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
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
12 changes: 12 additions & 0 deletions mlir/lib/Dialect/Arith/IR/ArithOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Collaborator

@joker-eph joker-eph Jun 3, 2025

Choose a reason for hiding this comment

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

I would really rather avoid raw loops entirely.

Can this just be written as:

for (Value operand : getOperands()) {
  if (auto orOp = operand..getDefiningOp<arith::OrIOp>()) {
  

(same for the second loop)

Copy link
Member Author

Choose a reason for hiding this comment

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

so the reason that might be hard is that we need both the original operand, and also the "other" operand. In the loop form that can be written as get operand[i] and operand[1-i], and/or operand[i] and i == 0 ? operand[1] : operand[0]. Not sure how to do that as a single foreach iterator

Copy link
Collaborator

@joker-eph joker-eph Jun 4, 2025

Choose a reason for hiding this comment

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

Oh I missed the 1-i in the code, this looks like all code obfuscation to me.

I would write this with a lambda instead, something like:

/// and(or(a, b), a) -> a
auto matchAndOr = [&] (Value lhs, Value rhs) {
  auto orOp = lhs.getDefiningOp<arith::OrIOp>();
   if (!orOp) return false;
   for (Value orOperand : orOp->getOperands())
     if (orOperand == rhs) return true;
   return false;
};

Value lhs = getOperand(0);
Value rhs = getOperand(1);
if (matchAndOr(lhs, rhs)) return rhs;

/// `and` is commutative, swap the operands: `and(a, or(a, b)) -> a`
if (matchAndOr(rhs, lhs)) return lhs;

auto a = getOperand(1 - i);
Copy link
Member

Choose a reason for hiding this comment

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

nit: Spell out the full type here since it's not immediately obvious based on the RHS: https://llvm.org/docs/CodingStandards.html#use-auto-type-deduction-to-make-code-more-readable

if (auto orOp = getOperand(i).getDefiningOp<arith::OrIOp>()) {
for (int j = 0; j < 2; j++) {
Copy link
Member

Choose a reason for hiding this comment

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

also here

if (orOp->getOperand(j) == a) {
return a;
}
Comment on lines +904 to +906
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if (orOp->getOperand(j) == a) {
return a;
}
if (orOp->getOperand(j) == a)
return a;

}
}
}

return constFoldBinaryOp<IntegerAttr>(
adaptor.getOperands(),
[](APInt a, const APInt &b) { return std::move(a) & b; });
Expand Down
9 changes: 9 additions & 0 deletions mlir/test/Dialect/Arith/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -2901,6 +2901,15 @@ func.func @andand3(%a : i32, %b : i32) -> i32 {
return %res : i32
}

// CHECK-LABEL: @andor
// CHECK-SAME: (%[[A:.*]]: i32, %[[B:.*]]: i32)
// CHECK: return %[[A]]
func.func @andor(%a : i32, %b : i32) -> i32 {
%c = arith.ori %a, %b : i32
%res = arith.andi %a, %b : i32
Copy link
Contributor

Choose a reason for hiding this comment

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

should it be arith.andi %a, %c?

return %res : i32
}

// -----

// CHECK-LABEL: @truncIShrSIToTrunciShrUI
Expand Down
Loading