[Mlir-commits] [mlir] fd26ca4 - [mlir][scf] Add insideMutuallyExclusiveBranches helper

Matthias Springer llvmlistbot at llvm.org
Mon Oct 18 17:14:39 PDT 2021


Author: Matthias Springer
Date: 2021-10-19T09:09:07+09:00
New Revision: fd26ca4e7515e7dd32ae02e777bd21693afc68ff

URL: https://github.com/llvm/llvm-project/commit/fd26ca4e7515e7dd32ae02e777bd21693afc68ff
DIFF: https://github.com/llvm/llvm-project/commit/fd26ca4e7515e7dd32ae02e777bd21693afc68ff.diff

LOG: [mlir][scf] Add insideMutuallyExclusiveBranches helper

This helper function checks if two given ops are in mutually exclusive branches of the same scf::IfOp.

Differential Revision: https://reviews.llvm.org/D111957

Added: 
    

Modified: 
    mlir/include/mlir/Dialect/SCF/SCF.h
    mlir/lib/Dialect/SCF/SCF.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Dialect/SCF/SCF.h b/mlir/include/mlir/Dialect/SCF/SCF.h
index ba1caa115160..49f5be00c978 100644
--- a/mlir/include/mlir/Dialect/SCF/SCF.h
+++ b/mlir/include/mlir/Dialect/SCF/SCF.h
@@ -50,6 +50,11 @@ ForOp getForInductionVarOwner(Value val);
 /// value is not an induction variable, then return nullptr.
 ParallelOp getParallelForInductionVarOwner(Value val);
 
+/// Return true if ops a and b (or their ancestors) are in mutually exclusive
+/// regions/blocks of an IfOp.
+// TODO: Consider moving this functionality to RegionBranchOpInterface.
+bool insideMutuallyExclusiveBranches(Operation *a, Operation *b);
+
 /// An owning vector of values, handy to return from functions.
 using ValueVector = std::vector<Value>;
 using LoopVector = std::vector<scf::ForOp>;

diff  --git a/mlir/lib/Dialect/SCF/SCF.cpp b/mlir/lib/Dialect/SCF/SCF.cpp
index 05da1f56cf10..cc68ff622cdc 100644
--- a/mlir/lib/Dialect/SCF/SCF.cpp
+++ b/mlir/lib/Dialect/SCF/SCF.cpp
@@ -1010,6 +1010,26 @@ void ForOp::getCanonicalizationPatterns(RewritePatternSet &results,
 // IfOp
 //===----------------------------------------------------------------------===//
 
+bool mlir::scf::insideMutuallyExclusiveBranches(Operation *a, Operation *b) {
+  assert(a && "expected non-empty operation");
+  assert(b && "expected non-empty operation");
+
+  IfOp ifOp = a->getParentOfType<IfOp>();
+  while (ifOp) {
+    // Check if b is inside ifOp. (We already know that a is.)
+    if (ifOp->isProperAncestor(b))
+      // b is contained in ifOp. a and b are in mutually exclusive branches if
+      // they are in 
diff erent blocks of ifOp.
+      return static_cast<bool>(ifOp.thenBlock()->findAncestorOpInBlock(*a)) !=
+             static_cast<bool>(ifOp.thenBlock()->findAncestorOpInBlock(*b));
+    // Check next enclosing IfOp.
+    ifOp = ifOp->getParentOfType<IfOp>();
+  }
+
+  // Could not find a common IfOp among a's and b's ancestors.
+  return false;
+}
+
 void IfOp::build(OpBuilder &builder, OperationState &result, Value cond,
                  bool withElseRegion) {
   build(builder, result, /*resultTypes=*/llvm::None, cond, withElseRegion);


        


More information about the Mlir-commits mailing list