-
Notifications
You must be signed in to change notification settings - Fork 298
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
[Comb]: Canonicalize and(x,y) and or(x,y) when x and y are defined by opposite comparisons #6374
Conversation
lib/Dialect/Comb/CombFolds.cpp
Outdated
@@ -743,6 +743,11 @@ static bool canonicalizeLogicalCstWithConcat(Operation *logicalOp, | |||
return true; | |||
} | |||
|
|||
static bool canCombineOppositeCmpIntoConstant(ICmpOp lhs, ICmpOp rhs) { |
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.
This is only valid for 'bin' flagged ops. The bin flag marks the op as only caring about the binary truth-table (rather than the vhdl or verilog truth-tables)
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.
Thanks, hadn't thought about that. I've added a check for that. This wouldn't affect the andOp
or orOp
itself, correct?
This is a not-uncommon problem with the variadic forms of these ops. If you had purely binary versions of these ops (like llvm), then you would have the same problem, it just is expressed as needing to search through the or-op tree to find these things. |
40bff69
to
c17652d
Compare
c17652d
to
335a67d
Compare
To reduce the time complexity perhaps we can do some sort of caching. We can create a set for seen ICmp values ( I had a semifinished branch for the this canonicalization |
a993e1b
to
250268e
Compare
@uenoku Thanks for the review! I've updated this PR with a lot of influence from your branch, and I think this implementation should be a lot nicer. I did not include the |
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.
LGTM, thanks!
lib/Dialect/Comb/CombFolds.cpp
Outdated
@@ -827,6 +849,14 @@ OpFoldResult AndOp::fold(FoldAdaptor adaptor) { | |||
} | |||
} | |||
|
|||
// x0 = icmp(x, y) | |||
// x1 = n_icmp(x, y) |
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.
nit: n_icmp
might not be easy to understand at the first look so could you write more descriptive comment here like icmp(pred, x, y), icmp(nagated(pred), x, y)) -> 0
?
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've changed it to pred
and !pred
which hopefully should be clear enough.
250268e
to
fb4e342
Compare
… opposite comparisons - and(x,y) -> 0 - or(x,y) -> 1 - Only affects `and` and `or` of operands of binary `ICmpOp` define to handle definite 0 or 1 cases Resolves llvm#5729
fb4e342
to
77b98dc
Compare
Thanks! |
This is my first time working on the Comb dialect, so this might not be the most optimal fold implementation. This could be generalized to apply to ands and ors of more than 2 values, but I wasn't sure how to hook this up into the
m_Complement
matcher on line 776 (forAndOp
). That would also have an average complexity ofO(n^2)
which isn't ideal.