[Mlir-commits] [mlir] bf59cd3 - [mlir][sparse] fix error when sparse kernel is nested in a scf structrual operator.

Peiming Liu llvmlistbot at llvm.org
Wed Jul 27 09:12:29 PDT 2022


Author: Peiming Liu
Date: 2022-07-27T16:12:23Z
New Revision: bf59cd320e483471421691813989d0036427bb37

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

LOG: [mlir][sparse] fix error when sparse kernel is nested in a scf structrual operator.

Sparse compiler failed on the provided test (when the sparse kernel is nested in a scf structrual operator).

Reviewed By: bixia

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

Added: 
    mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_scf_nested.mlir

Modified: 
    mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorPasses.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorPasses.cpp b/mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorPasses.cpp
index 2265989301ea6..2014781610536 100644
--- a/mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorPasses.cpp
+++ b/mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorPasses.cpp
@@ -14,6 +14,7 @@
 #include "mlir/Dialect/Func/Transforms/FuncConversions.h"
 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
 #include "mlir/Dialect/Linalg/Transforms/Transforms.h"
+#include "mlir/Dialect/SCF/Transforms/Transforms.h"
 #include "mlir/Dialect/SparseTensor/IR/SparseTensor.h"
 #include "mlir/Dialect/SparseTensor/Transforms/Passes.h"
 #include "mlir/Dialect/Tensor/IR/Tensor.h"
@@ -148,6 +149,8 @@ struct SparseTensorConversionPass
     populateFunctionOpInterfaceTypeConversionPattern<func::FuncOp>(patterns,
                                                                    converter);
     populateCallOpTypeConversionPattern(patterns, converter);
+    scf::populateSCFStructuralTypeConversionsAndLegality(converter, patterns,
+                                                         target);
     populateSparseTensorConversionPatterns(converter, patterns, options);
     if (failed(applyPartialConversion(getOperation(), target,
                                       std::move(patterns))))

diff  --git a/mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_scf_nested.mlir b/mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_scf_nested.mlir
new file mode 100644
index 0000000000000..333724f678f73
--- /dev/null
+++ b/mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_scf_nested.mlir
@@ -0,0 +1,82 @@
+// RUN: mlir-opt %s --sparse-compiler | \
+// RUN: mlir-cpu-runner \
+// RUN:  -e entry -entry-point-result=void  \
+// RUN:  -shared-libs=%mlir_integration_test_dir/libmlir_c_runner_utils%shlibext | \
+// RUN: FileCheck %s
+
+#map = affine_map<(d0, d1, d2) -> (d0, d1, d2)>
+#SparseMatrix = #sparse_tensor.encoding<{ dimLevelType = [ "compressed", "compressed", "compressed" ] }>
+
+module @func_sparse.2 {
+  // Do elementwise x+1 when true, x-1 when false
+  func.func public @condition(%cond: i1, %arg0: tensor<2x3x4xf64, #SparseMatrix>) -> tensor<2x3x4xf64, #SparseMatrix> {
+    %1 = scf.if %cond -> (tensor<2x3x4xf64, #SparseMatrix>) {
+      %cst_2 = arith.constant dense<1.000000e+00> : tensor<f64>
+      %cst_3 = arith.constant dense<1.000000e+00> : tensor<2x3x4xf64>
+      %2 = bufferization.alloc_tensor() : tensor<2x3x4xf64, #SparseMatrix>
+      %3 = linalg.generic {
+        indexing_maps = [#map, #map, #map],
+        iterator_types = ["parallel", "parallel", "parallel"]}
+        ins(%arg0, %cst_3 : tensor<2x3x4xf64, #SparseMatrix>, tensor<2x3x4xf64>)
+        outs(%2 : tensor<2x3x4xf64, #SparseMatrix>) {
+          ^bb0(%arg1: f64, %arg2: f64, %arg3: f64):
+            %4 = arith.subf %arg1, %arg2 : f64
+            linalg.yield %4 : f64
+          } -> tensor<2x3x4xf64, #SparseMatrix>
+        scf.yield %3 : tensor<2x3x4xf64, #SparseMatrix>
+    } else {
+      %cst_2 = arith.constant dense<1.000000e+00> : tensor<f64>
+      %cst_3 = arith.constant dense<1.000000e+00> : tensor<2x3x4xf64>
+      %2 = bufferization.alloc_tensor() : tensor<2x3x4xf64, #SparseMatrix>
+      %3 = linalg.generic {
+        indexing_maps = [#map, #map, #map],
+        iterator_types = ["parallel", "parallel", "parallel"]}
+        ins(%arg0, %cst_3 : tensor<2x3x4xf64, #SparseMatrix>, tensor<2x3x4xf64>)
+        outs(%2 : tensor<2x3x4xf64, #SparseMatrix>) {
+          ^bb0(%arg1: f64, %arg2: f64, %arg3: f64):
+            %4 = arith.addf %arg1, %arg2 : f64
+            linalg.yield %4 : f64
+          } -> tensor<2x3x4xf64, #SparseMatrix>
+        scf.yield %3 : tensor<2x3x4xf64, #SparseMatrix>
+    }
+    return %1 : tensor<2x3x4xf64, #SparseMatrix>
+  }
+
+  func.func @dump(%arg0: tensor<2x3x4xf64, #SparseMatrix>) {
+    %d0 = arith.constant 0.0 : f64
+    %c0 = arith.constant 0 : index
+    %dm = sparse_tensor.convert %arg0 : tensor<2x3x4xf64, #SparseMatrix> to tensor<2x3x4xf64>
+    %0 = vector.transfer_read %dm[%c0, %c0, %c0], %d0: tensor<2x3x4xf64>, vector<2x3x4xf64>
+    vector.print %0 : vector<2x3x4xf64>
+    return
+  }
+
+  func.func public @entry() {
+    %src = arith.constant dense<[
+     [  [  1.0,  2.0,  3.0,  4.0 ],
+        [  5.0,  6.0,  7.0,  8.0 ],
+        [  9.0, 10.0, 11.0, 12.0 ] ],
+     [  [ 13.0, 14.0, 15.0, 16.0 ],
+        [ 17.0, 18.0, 19.0, 20.0 ],
+        [ 21.0, 22.0, 23.0, 24.0 ] ]
+    ]> : tensor<2x3x4xf64>
+
+    %t = arith.constant 1 : i1
+    %f = arith.constant 0 : i1
+
+    %sm = sparse_tensor.convert %src : tensor<2x3x4xf64> to tensor<2x3x4xf64, #SparseMatrix>
+
+    %sm_t = call @condition(%t, %sm) : (i1, tensor<2x3x4xf64, #SparseMatrix>) -> tensor<2x3x4xf64, #SparseMatrix>
+    %sm_f = call @condition(%f, %sm) : (i1, tensor<2x3x4xf64, #SparseMatrix>) -> tensor<2x3x4xf64, #SparseMatrix>
+
+    // CHECK:      ( ( ( 0, 1, 2, 3 ), ( 4, 5, 6, 7 ), ( 8, 9, 10, 11 ) ), ( ( 12, 13, 14, 15 ), ( 16, 17, 18, 19 ), ( 20, 21, 22, 23 ) ) )
+    // CHECK-NEXT: ( ( ( 2, 3, 4, 5 ), ( 6, 7, 8, 9 ), ( 10, 11, 12, 13 ) ), ( ( 14, 15, 16, 17 ), ( 18, 19, 20, 21 ), ( 22, 23, 24, 25 ) ) )
+    call @dump(%sm_t) : (tensor<2x3x4xf64, #SparseMatrix>) -> ()
+    call @dump(%sm_f) : (tensor<2x3x4xf64, #SparseMatrix>) -> ()
+
+    bufferization.dealloc_tensor %sm : tensor<2x3x4xf64, #SparseMatrix>
+    bufferization.dealloc_tensor %sm_t : tensor<2x3x4xf64, #SparseMatrix>
+    bufferization.dealloc_tensor %sm_f : tensor<2x3x4xf64, #SparseMatrix>
+    return
+  }
+}
\ No newline at end of file


        


More information about the Mlir-commits mailing list