[Mlir-commits] [mlir] 3cbae16 - [mlir][sparse] Lower affine before SCF to CF in sparsifier (#219860)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue Sep 15 09:49:49 PDT 2026


Author: Kohei Yamaguchi
Date: 2026-09-15T09:49:44-07:00
New Revision: 3cbae1632e6ca6f8436fcbc49d059cc924f883b0

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

LOG: [mlir][sparse] Lower affine before SCF to CF in sparsifier (#219860)

Fix the lowering order in the sparsifier pipeline by running
`convert-scf-to-cf` after `lower-affine`.

Previously, `convert-scf-to-cf` ran while Affine control-flow operations
could still be present. For example, lowering an `scf.if` nested inside
an `affine.for` introduces multiple blocks into the loop body, violating
`affine.for`'s single-block region requirement and causing verification
to fail.

Running `lower-affine` first converts Affine control flow to SCF. The
subsequent `convert-scf-to-cf` pass can then lower both the existing SCF
operations and those introduced by Affine lowering to CFG-based control
flow.

Fixes #217895

Added: 
    mlir/test/Dialect/SparseTensor/sparsifier_lower_affine.mlir

Modified: 
    mlir/lib/Dialect/SparseTensor/Pipelines/SparseTensorPipelines.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Dialect/SparseTensor/Pipelines/SparseTensorPipelines.cpp b/mlir/lib/Dialect/SparseTensor/Pipelines/SparseTensorPipelines.cpp
index 5e39ef13742c8..c95ecdf20dc22 100644
--- a/mlir/lib/Dialect/SparseTensor/Pipelines/SparseTensorPipelines.cpp
+++ b/mlir/lib/Dialect/SparseTensor/Pipelines/SparseTensorPipelines.cpp
@@ -78,9 +78,9 @@ void mlir::sparse_tensor::buildSparsifier(OpPassManager &pm,
   pm.addNestedPass<func::FuncOp>(createConvertLinalgToLoopsPass());
   pm.addNestedPass<func::FuncOp>(createConvertVectorToSCFPass());
   pm.addNestedPass<func::FuncOp>(memref::createExpandReallocPass());
-  pm.addNestedPass<func::FuncOp>(createSCFToControlFlowPass());
   pm.addPass(memref::createExpandStridedMetadataPass());
   pm.addPass(createLowerAffinePass());
+  pm.addNestedPass<func::FuncOp>(createSCFToControlFlowPass());
   pm.addPass(
       createConvertVectorToLLVMPass(options.convertVectorToLLVMOptions()));
   pm.addNestedPass<func::FuncOp>(createConvertComplexToStandardPass());

diff  --git a/mlir/test/Dialect/SparseTensor/sparsifier_lower_affine.mlir b/mlir/test/Dialect/SparseTensor/sparsifier_lower_affine.mlir
new file mode 100644
index 0000000000000..5662f9be709c9
--- /dev/null
+++ b/mlir/test/Dialect/SparseTensor/sparsifier_lower_affine.mlir
@@ -0,0 +1,20 @@
+// RUN: mlir-opt %s --sparsifier | FileCheck %s
+
+// Verify that SCF control flow nested in an affine loop can be lowered by the
+// sparsifier pipeline without creating an invalid multi-block affine region.
+// CHECK-LABEL: llvm.func @nested_scf_in_affine
+// CHECK-NOT: affine.for
+func.func @nested_scf_in_affine(%cond: i1) -> i64 {
+  %zero = arith.constant 0 : i64
+  %one = arith.constant 1 : i64
+  %result = affine.for %i = 0 to 4 iter_args(%acc = %zero) -> i64 {
+    %value = scf.if %cond -> i64 {
+      %sum = arith.addi %acc, %one : i64
+      scf.yield %sum : i64
+    } else {
+      scf.yield %acc : i64
+    }
+    affine.yield %value : i64
+  }
+  return %result : i64
+}


        


More information about the Mlir-commits mailing list