-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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); | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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++) { | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
} | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
return constFoldBinaryOp<IntegerAttr>( | ||||||||||||
adaptor.getOperands(), | ||||||||||||
[](APInt a, const APInt &b) { return std::move(a) & b; }); | ||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should it be |
||
return %res : i32 | ||
} | ||
|
||
// ----- | ||
|
||
// CHECK-LABEL: @truncIShrSIToTrunciShrUI | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use pre-increment: https://llvm.org/docs/CodingStandards.html#prefer-preincrement
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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:
(same for the second loop)
There was a problem hiding this comment.
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
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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: