[Mlir-commits] [mlir] 1f54b84 - [mlir][scf] Add scf.execute_region to the structural type conversions (#234405)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Fri Oct 23 16:02:39 PDT 2026


Author: Jared Hoberock
Date: 2026-10-24T02:02:35+03:00
New Revision: 1f54b8458a00666e04831071b718befca263e559

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

LOG: [mlir][scf] Add scf.execute_region to the structural type conversions (#234405)

This change adds support for `scf.execute_region` to
`populateSCFStructuralTypeConversions`. `scf.execute_region` seems to be
an omission, so this change simply fills a hole.

Add `ConvertExecuteRegionOpTypes` (patterned after `ConvertIfOpTypes`)
to convert the types of `scf.execute_region` and add that to the list of
patterns in `populateSCFStructuralTypeConversions` and legality in
`populateSCFStructuralTypeConversionTarget`.

Also adds a unit test to check for type conversion in
`scf.execute_region`.

Assisted-by: Claude

Added: 
    

Modified: 
    mlir/lib/Dialect/SCF/Transforms/StructuralTypeConversions.cpp
    mlir/test/Transforms/test-legalize-type-conversion.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Dialect/SCF/Transforms/StructuralTypeConversions.cpp b/mlir/lib/Dialect/SCF/Transforms/StructuralTypeConversions.cpp
index 7c72a5de4f15f..fe8fdba0857c9 100644
--- a/mlir/lib/Dialect/SCF/Transforms/StructuralTypeConversions.cpp
+++ b/mlir/lib/Dialect/SCF/Transforms/StructuralTypeConversions.cpp
@@ -217,6 +217,30 @@ class ConvertIndexSwitchOpTypes
     return newOp;
   }
 };
+
+class ConvertExecuteRegionOpTypes
+    : public Structural1ToNConversionPattern<ExecuteRegionOp,
+                                             ConvertExecuteRegionOpTypes> {
+public:
+  using Structural1ToNConversionPattern::Structural1ToNConversionPattern;
+
+  std::optional<ExecuteRegionOp>
+  convertSourceOp(ExecuteRegionOp op, OneToNOpAdaptor adaptor,
+                  ConversionPatternRewriter &rewriter,
+                  TypeRange dstTypes) const {
+    // The entry block has no arguments, but the region may have further blocks
+    // whose arguments need converting.
+    if (failed(rewriter.convertRegionTypes(&op.getRegion(), *typeConverter)))
+      return std::nullopt;
+
+    auto newOp = ExecuteRegionOp::create(rewriter, op.getLoc(), dstTypes,
+                                         op.getNoInline());
+    newOp->setDiscardableAttrs(op->getDiscardableAttrDictionary().getValue());
+    rewriter.inlineRegionBefore(op.getRegion(), newOp.getRegion(),
+                                newOp.getRegion().end());
+    return newOp;
+  }
+};
 } // namespace
 
 namespace {
@@ -255,18 +279,19 @@ void mlir::scf::populateSCFStructuralTypeConversions(
     PatternBenefit benefit) {
   patterns.add<ConvertForOpTypes, ConvertIfOpTypes, ConvertYieldOpTypes,
                ConvertWhileOpTypes, ConvertConditionOpTypes,
-               ConvertIndexSwitchOpTypes>(typeConverter, patterns.getContext(),
-                                          benefit);
+               ConvertIndexSwitchOpTypes, ConvertExecuteRegionOpTypes>(
+      typeConverter, patterns.getContext(), benefit);
 }
 
 void mlir::scf::populateSCFStructuralTypeConversionTarget(
     const TypeConverter &typeConverter, ConversionTarget &target) {
-  target.addDynamicallyLegalOp<ForOp, IfOp, IndexSwitchOp>(
+  target.addDynamicallyLegalOp<ForOp, IfOp, IndexSwitchOp, ExecuteRegionOp>(
       [&](Operation *op) { return typeConverter.isLegal(op->getResults()); });
   target.addDynamicallyLegalOp<scf::YieldOp>([&](scf::YieldOp op) {
     // We only have conversions for a subset of ops that use scf.yield
     // terminators.
-    if (!isa<ForOp, IfOp, WhileOp, IndexSwitchOp>(op->getParentOp()))
+    if (!isa<ForOp, IfOp, WhileOp, IndexSwitchOp, ExecuteRegionOp>(
+            op->getParentOp()))
       return true;
     return typeConverter.isLegal(op.getOperands());
   });

diff  --git a/mlir/test/Transforms/test-legalize-type-conversion.mlir b/mlir/test/Transforms/test-legalize-type-conversion.mlir
index 352483520ff74..ee1c37906f60f 100644
--- a/mlir/test/Transforms/test-legalize-type-conversion.mlir
+++ b/mlir/test/Transforms/test-legalize-type-conversion.mlir
@@ -140,6 +140,27 @@ func.func @test_scf_if_dropped_condition() {
 
 // -----
 
+// CHECK-LABEL: func @test_scf_execute_region(
+//  CHECK-SAME:     %[[ARG:.*]]: f64)
+//       CHECK:   %[[RESULT:.*]] = scf.execute_region -> f64 {
+//       CHECK:     cf.br ^[[BB1:.*]](%[[ARG]] : f64)
+//       CHECK:   ^[[BB1]](%[[VALUE:.*]]: f64):
+//       CHECK:     scf.yield %[[VALUE]] : f64
+//       CHECK:   } {test.attr}
+//       CHECK:   %[[CAST:.*]] = "test.cast"(%[[RESULT]]) : (f64) -> f32
+//       CHECK:   "test.type_consumer"(%[[CAST]]) : (f32) -> ()
+func.func @test_scf_execute_region(%arg0: f32) {
+  %result = scf.execute_region -> f32 {
+    cf.br ^bb1(%arg0 : f32)
+  ^bb1(%value: f32):
+    scf.yield %value : f32
+  } {test.attr}
+  "test.type_consumer"(%result) : (f32) -> ()
+  return
+}
+
+// -----
+
 // CHECK-LABEL: @recursive_type_conversion
 func.func @recursive_type_conversion() {
   // CHECK:  !test.test_rec<outer_converted_type, smpla>


        


More information about the Mlir-commits mailing list