[flang-commits] [flang] [mlir] [mlir] Handle simple commutative cases in CSE. (PR #75274)

Matthias Springer via flang-commits flang-commits at lists.llvm.org
Wed Dec 13 20:15:30 PST 2023


================
@@ -798,15 +844,24 @@ OperationEquivalence::isRegionEquivalentTo(Region *lhs, Region *rhs,
     return false;
 
   // 2. Compare operands.
-  for (auto operandPair : llvm::zip(lhs->getOperands(), rhs->getOperands())) {
-    Value curArg = std::get<0>(operandPair);
-    Value otherArg = std::get<1>(operandPair);
-    if (curArg == otherArg)
-      continue;
-    if (curArg.getType() != otherArg.getType())
-      return false;
-    if (failed(checkEquivalent(curArg, otherArg)))
+  if (checkCommutativeEquivalent &&
+      lhs->hasTrait<mlir::OpTrait::IsCommutative>()) {
+    auto lhsRange = lhs->getOperands();
+    auto rhsRange = rhs->getOperands();
+    if (failed(checkCommutativeEquivalent(lhsRange, rhsRange)))
----------------
matthias-springer wrote:

Do we actually need `checkCommutativeEquivalent`? Can we do something like this if the op has the `IsCommutative` trait?

```c++
SmallVector<Value> rhsOperands(rhs->getOperands().begin(), rhs->getOperands().end());
for (Value lhsOperand : lhs->getOperands()) {
  auto it = llvm::find_if(rhsOperands, [&](Value rhsOperand) {
      return succeeded(checkEquivalent(lhsOperand, rhsOperand));
  });
  if (it == rhsOperands.end()) {
    // Could not find equivalent operand.
    return false;
  }
  rhsOperands.erase(it);
}
assert(rhsOperands.empty());
```


https://github.com/llvm/llvm-project/pull/75274


More information about the flang-commits mailing list