[Mlir-commits] [mlir] c8e96bc - [mlir][OpenACC] Fold constant-false host_data with an empty region (#236052)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Oct 29 06:36:33 PDT 2026


Author: Akshay Khadse
Date: 2026-10-29T06:36:26-07:00
New Revision: c8e96bc52e2f74e6b2a97948eb2daeb77eba4261

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

LOG: [mlir][OpenACC] Fold constant-false host_data with an empty region (#236052)

`RemoveConstantIfConditionWithRegion` replaces a construct whose `if`
condition is constant false with the contents of its region. The helper
it uses asserts that the region has exactly one block, but
`acc.host_data` accepts an empty region, so canonicalizing one crashed.

An empty region has nothing to inline, so the op is now simply erased in
that case, which matches what happens for a non-empty region.
Multi-block regions now bail out instead of asserting.

Fixes #231828

Added: 
    

Modified: 
    mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp
    mlir/test/Dialect/OpenACC/canonicalize.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp b/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp
index 69c0baef8918b7..8462e6e3bc98f2 100644
--- a/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp
+++ b/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp
@@ -1367,11 +1367,25 @@ struct RemoveConstantIfConditionWithRegion : public OpRewritePattern<OpTy> {
     IntegerAttr constAttr;
     if (!matchPattern(ifCond, m_Constant(&constAttr)))
       return failure();
-    if (constAttr.getInt())
+    if (constAttr.getInt()) {
       rewriter.modifyOpInPlace(op, [&]() { op.getIfCondMutable().erase(0); });
-    else
-      replaceOpWithRegion(rewriter, op, op.getRegion());
+      return success();
+    }
+
+    // With a constant false condition the construct has no effect, so it is
+    // replaced by its body. An empty region has no body, so the op is simply
+    // erased.
+    Region &region = op.getRegion();
+    if (region.empty()) {
+      if (op->getNumResults() != 0)
+        return rewriter.notifyMatchFailure(op, "empty region with results");
+      rewriter.eraseOp(op);
+      return success();
+    }
+    if (!region.hasOneBlock())
+      return rewriter.notifyMatchFailure(op, "expected single-block region");
 
+    replaceOpWithRegion(rewriter, op, region);
     return success();
   }
 };

diff  --git a/mlir/test/Dialect/OpenACC/canonicalize.mlir b/mlir/test/Dialect/OpenACC/canonicalize.mlir
index c7b03b3e6a044b..bf53595d62c25f 100644
--- a/mlir/test/Dialect/OpenACC/canonicalize.mlir
+++ b/mlir/test/Dialect/OpenACC/canonicalize.mlir
@@ -147,6 +147,23 @@ func.func @testhostdataop(%a: memref<f32>, %ifCond: i1) -> () {
 
 // -----
 
+// A constant false condition removes the construct even when its region is
+// empty.
+
+func.func @testhostdataop_empty_region(%a: memref<f32>) -> () {
+  %0 = acc.use_device varPtr(%a : memref<f32>) -> memref<f32>
+  %false = arith.constant false
+  acc.host_data dataOperands(%0 : memref<f32>) if(%false) {
+  }
+  return
+}
+
+// CHECK-LABEL: func.func @testhostdataop_empty_region
+// CHECK-NOT: acc.host_data
+// CHECK: return
+
+// -----
+
 func.func @update_no_op(%x : memref<i32>) {
   acc.atomic.update %x : memref<i32> {
   ^bb0(%xval : i32):


        


More information about the Mlir-commits mailing list