Optimization for fold compare_GT(maximum(a,b), a) into compare_GT(b,a). Is it needed?

91 views
Skip to first unread message

Alexander Pivovarov

unread,
Jan 10, 2024, 2:11:03 PMJan 10
to OpenXLA Discuss

I'm working on a compiler frontend which is based on OpenXLA
Internally we have an optimization to fold compare_GT(maximum(a,b), a) into compare_GT(b,a)
The optimization is added to algebraic_simplifier.cc::HandleCompare().

Full list of similar optimizations:

GT(min(a,b), _) -> FALSE
GT(max(a,b), a) -> GT(b,a)
GT(max(a,b), b) -> GT(a,b)

GT(a, min(a,b)) -> GT(a,b)
GT(b, min(a,b)) -> GT(b,a)
GT(_, max(a,b)) -> FALSE

GE(min(a,b), a) -> GE(b,a)
GE(min(a,b), b) -> GE(a,b)
GE(max(a,b), _) -> TRUE

GE(_, min(a,b)) -> TRUE
GE(a, max(a,b)) -> GE(a,b)
GE(b, max(a,b)) -> GE(b,a)

LT(min(a,b), a) -> LT(b,a)
LT(min(a,b), b) -> LT(a,b)
LT(max(a,b), _) -> FALSE

LT(_, min(a,b)) -> FALSE
LT(a, max(a,b)) -> LT(a,b)
LT(b, max(a,b)) -> LT(b,a)

LE(a, min(a,b)) -> LE(a,b)
LE(b, min(a,b)) -> LE(b,a)
LE(_, max(a,b)) -> TRUE

LE(min(a,b), _) -> TRUE
LE(max(a,b), a) -> LE(b,a)
LE(max(a,b), b) -> LE(a,b)

How useful would it be to contribute these optimizations to OpenXLA main?

We tested GT(max(a,b), a) optimization on Resnet50 model internally
Overall, we observed the following benefits of adding this optimization:

  • VmRSS usage is 14% less
  • Number of instructions - 13% less
  • Memory locations - 22% less

Paul Baumstarck

unread,
Jan 12, 2024, 1:15:54 PMJan 12
to OpenXLA Discuss, Alexander Pivovarov
Hi, Alexander,

Thanks for attaching the numbers you're seeing; this seems like it could be a useful optimization and simplification. Feel free to submit a pull request and we can check the impact on other benchmarks.

Regards,
Paul

Alexander Pivovarov

unread,
Feb 6, 2024, 12:39:54 AMFeb 6
to Paul Baumstarck, OpenXLA Discuss
Opened PR: Simplify Gt(Max(a,b), a) -> Gt(b,a) #9201
Reply all
Reply to author
Forward
0 new messages