Skip to content

[mlir][IR] DominanceInfo: Fix inconsistency in proper block/op dominance #115413

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

Merged
merged 1 commit into from
Nov 10, 2024

Conversation

matthias-springer
Copy link
Member

@matthias-springer matthias-springer commented Nov 8, 2024

An operation is considered to properly dominate itself in a graph region. That's because there is no concept of "dominance" in a graph region. (dominates returns "true" for all pairs of ops in the same block. It makes sense to do the same for properlyDominates.)

Previously, a block was not considered to dominate itself in a graph region. This commit fixes this asymmetry between ops and blocks: both are now properly dominating themselves in a graph region.

@llvmbot llvmbot added mlir:core MLIR Core Infrastructure mlir labels Nov 8, 2024
@llvmbot
Copy link
Member

llvmbot commented Nov 8, 2024

@llvm/pr-subscribers-mlir-core

@llvm/pr-subscribers-mlir

Author: Matthias Springer (matthias-springer)

Changes

An operation is considered to properly dominate itself in a graph region. That's because there is no concept of "dominance" in a graph region. (dominates returns "true" for all pairs of ops in the same block. It makes sense to do the same for properlyDominates.)

Previously, a block was not considered to dominate itself in a graph region. This commit fixes thise asymmetry between ops and blocks: both are now properly dominating themselves in a graph region.


Full diff: https://github.com/llvm/llvm-project/pull/115413.diff

2 Files Affected:

  • (modified) mlir/include/mlir/IR/Dominance.h (+4-3)
  • (modified) mlir/lib/IR/Dominance.cpp (+6-4)
diff --git a/mlir/include/mlir/IR/Dominance.h b/mlir/include/mlir/IR/Dominance.h
index 2536ce585b3fdd..66fdf1c4c6851b 100644
--- a/mlir/include/mlir/IR/Dominance.h
+++ b/mlir/include/mlir/IR/Dominance.h
@@ -141,8 +141,8 @@ class DominanceInfo : public detail::DominanceInfoBase</*IsPostDom=*/false> {
   /// are in the same block and A properly dominates B within the block, or if
   /// the block that contains A properly dominates the block that contains B. In
   /// an SSACFG region, Operation A dominates Operation B in the same block if A
-  /// preceeds B. In a Graph region, all operations in a block dominate all
-  /// other operations in the same block.
+  /// preceeds B. In a Graph region, all operations in a block properly dominate
+  /// all operations in the same block.
   ///
   /// The `enclosingOpOk` flag says whether we should return true if the B op
   /// is enclosed by a region on A.
@@ -178,7 +178,8 @@ class DominanceInfo : public detail::DominanceInfoBase</*IsPostDom=*/false> {
   /// contains block B or some parent of block B and block A dominates that
   /// block in that kind of region. In an SSACFG region, block A dominates
   /// block B if all control flow paths from the entry block to block B flow
-  /// through block A. In a Graph region, all blocks dominate all other blocks.
+  /// through block A. In a Graph region, all blocks properly dominate all
+  /// blocks.
   bool properlyDominates(Block *a, Block *b) const {
     return super::properlyDominates(a, b);
   }
diff --git a/mlir/lib/IR/Dominance.cpp b/mlir/lib/IR/Dominance.cpp
index 2b138ae223546e..31f7e7dbc925ce 100644
--- a/mlir/lib/IR/Dominance.cpp
+++ b/mlir/lib/IR/Dominance.cpp
@@ -34,7 +34,8 @@ DominanceInfoBase<IsPostDom>::~DominanceInfoBase() {
     delete entry.second.getPointer();
 }
 
-template <bool IsPostDom> void DominanceInfoBase<IsPostDom>::invalidate() {
+template <bool IsPostDom>
+void DominanceInfoBase<IsPostDom>::invalidate() {
   for (auto entry : dominanceInfos)
     delete entry.second.getPointer();
   dominanceInfos.clear();
@@ -217,9 +218,10 @@ template <bool IsPostDom>
 bool DominanceInfoBase<IsPostDom>::properlyDominates(Block *a, Block *b) const {
   assert(a && b && "null blocks not allowed");
 
-  // A block dominates itself but does not properly dominate itself.
+  // A block dominates, but does not properly dominate, itself unless this
+  // is a graph region.
   if (a == b)
-    return false;
+    return !hasSSADominance(a);
 
   // If both blocks are not in the same region, `a` properly dominates `b` if
   // `b` is defined in an operation region that (recursively) ends up being
@@ -269,7 +271,7 @@ bool DominanceInfo::properlyDominatesImpl(Operation *a, Operation *b,
   Block *aBlock = a->getBlock(), *bBlock = b->getBlock();
   assert(aBlock && bBlock && "operations must be in a block");
 
-  // An instruction dominates, but does not properlyDominate, itself unless this
+  // An operation dominates, but does not properly dominate, itself unless this
   // is a graph region.
   if (a == b)
     return !hasSSADominance(aBlock);

@matthias-springer matthias-springer force-pushed the users/matthias-springer/properly_dominate branch from 9d46f80 to 502ea36 Compare November 8, 2024 02:54
/// In an SSACFG region, block A dominates block B if all control flow paths
/// from the entry block to block B flow through block A.
///
/// Graph regions have only a single block. To be consistent with "proper
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This could also go the other way. We could say that an operation does not dominate itself in a graph region. But I find the current definition a bit more intuitive. (Considering that "dominance" does not exist in a graph region, so it does not matter at all which ops we are "comparing" in the same region.)

@matthias-springer matthias-springer force-pushed the users/matthias-springer/properly_dominate branch from 502ea36 to e8c5e29 Compare November 8, 2024 02:56
@matthias-springer matthias-springer changed the title [mlir][IR] Fix inconsistency in block/op dominance [mlir][IR] DominanceInfo: Fix inconsistency in proper block/op dominance Nov 8, 2024
@joker-eph
Copy link
Collaborator

Any way to exercise his in a test?

@matthias-springer matthias-springer force-pushed the users/matthias-springer/properly_dominate branch from e8c5e29 to 68c43c6 Compare November 10, 2024 05:57
@matthias-springer
Copy link
Member Author

Any way to exercise his in a test?

I updated the test-dominance.mlir test to also include the enclosing builtin.module (which has a graph region). Also printing proper block dominance now. Also some general improvements to the test such as printing the computed block IDs to make it easier to read the test output.

An operation is considered to properly dominate itself in a graph region. That's because there is no concept of "dominance" in a graph region. (`dominates` returns "true" for all pairs of ops in the same block.)

Previously, a block was *not* considered to dominate itself in a graph region. This commit fixes thise asymmetry between ops and blocks: both are now properly dominating themselves in a graph region.
@matthias-springer matthias-springer force-pushed the users/matthias-springer/properly_dominate branch from 68c43c6 to 2052c65 Compare November 10, 2024 06:11
@matthias-springer matthias-springer merged commit e4c1419 into main Nov 10, 2024
6 of 8 checks passed
@matthias-springer matthias-springer deleted the users/matthias-springer/properly_dominate branch November 10, 2024 06:20
Groverkss pushed a commit to iree-org/llvm-project that referenced this pull request Nov 15, 2024
…nance (llvm#115413)

An operation is considered to properly dominate itself in a graph
region. That's because there is no concept of "dominance" in a graph
region. (`dominates` returns "true" for all pairs of ops in the same
block. It makes sense to do the same for `properlyDominates`.)

Previously, a block was *not* considered to dominate itself in a graph
region. This commit fixes this asymmetry between ops and blocks: both
are now properly dominating themselves in a graph region.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mlir:core MLIR Core Infrastructure mlir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants