Skip to content

Commit

Permalink
[mlir][sparse] Expose SparseTensor passes as enums instead of opaque …
Browse files Browse the repository at this point in the history
…numbers for vectorization and parallelization options.

The SparseTensor passes currently use opaque numbers for the CLI, despite using an enum internally. This patch exposes the enums instead of numbered items that are matched back to the enum.

Fixes #53389

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

Please also see:
https://reviews.llvm.org/D118379
https://reviews.llvm.org/D117919
  • Loading branch information
nkreeger committed Sep 4, 2022
1 parent 9fc679b commit 30ceb78
Show file tree
Hide file tree
Showing 27 changed files with 122 additions and 105 deletions.
4 changes: 2 additions & 2 deletions mlir/benchmark/python/common.py
Expand Up @@ -13,8 +13,8 @@ def setup_passes(mlir_module):
"""Setup pass pipeline parameters for benchmark functions.
"""
opt = (
"parallelization-strategy=0"
" vectorization-strategy=0 vl=1 enable-simd-index32=False"
"parallelization-strategy=none"
" vectorization-strategy=none vl=1 enable-simd-index32=False"
)
pipeline = f"sparse-compiler{{{opt}}}"
PassManager.parse(pipeline).run(mlir_module)
Expand Down
47 changes: 38 additions & 9 deletions mlir/include/mlir/Dialect/SparseTensor/Pipelines/Passes.h
Expand Up @@ -30,12 +30,43 @@ namespace sparse_tensor {
struct SparseCompilerOptions
: public PassPipelineOptions<SparseCompilerOptions> {
// These options must be kept in sync with `SparsificationBase`.
PassOptions::Option<int32_t> parallelization{
// TODO(57514): These options are duplicated in Passes.td.
PassOptions::Option<mlir::SparseParallelizationStrategy> parallelization{
*this, "parallelization-strategy",
desc("Set the parallelization strategy"), init(0)};
PassOptions::Option<int32_t> vectorization{
*this, "vectorization-strategy", desc("Set the vectorization strategy"),
init(0)};
::llvm::cl::desc("Set the parallelization strategy"),
::llvm::cl::init(mlir::SparseParallelizationStrategy::kNone),
llvm::cl::values(
clEnumValN(mlir::SparseParallelizationStrategy::kNone, "none",
"Turn off sparse parallelization."),
clEnumValN(mlir::SparseParallelizationStrategy::kDenseOuterLoop,
"dense-outer-loop",
"Enable dense outer loop sparse parallelization."),
clEnumValN(mlir::SparseParallelizationStrategy::kAnyStorageOuterLoop,
"any-storage-outer-loop",
"Enable sparse parallelization regardless of storage for "
"the outer loop."),
clEnumValN(mlir::SparseParallelizationStrategy::kDenseAnyLoop,
"dense-any-loop",
"Enable dense parallelization for any loop."),
clEnumValN(
mlir::SparseParallelizationStrategy::kAnyStorageAnyLoop,
"any-storage-any-loop",
"Enable sparse parallelization for any storage and loop."))};
PassOptions::Option<mlir::SparseVectorizationStrategy> vectorization{
*this, "vectorization-strategy",
::llvm::cl::desc("Set the vectorization strategy"),
::llvm::cl::init(mlir::SparseVectorizationStrategy::kNone),
llvm::cl::values(
clEnumValN(mlir::SparseVectorizationStrategy::kNone, "none",
"Turn off sparse vectorization."),
clEnumValN(mlir::SparseVectorizationStrategy::kDenseInnerLoop,
"dense-inner-loop",
"Enable vectorization for dense inner loops."),
clEnumValN(mlir::SparseVectorizationStrategy::kAnyStorageInnerLoop,
"any-storage-inner-loop",
"Enable sparse vectorization for inner loops with any "
"storage."))};

PassOptions::Option<int32_t> vectorLength{
*this, "vl", desc("Set the vector length"), init(1)};
PassOptions::Option<bool> enableSIMDIndex32{
Expand All @@ -50,10 +81,8 @@ struct SparseCompilerOptions

/// Projects out the options for `createSparsificationPass`.
SparsificationOptions sparsificationOptions() const {
return SparsificationOptions(sparseParallelizationStrategy(parallelization),
sparseVectorizationStrategy(vectorization),
vectorLength, enableSIMDIndex32,
enableVLAVectorization);
return SparsificationOptions(parallelization, vectorization, vectorLength,
enableSIMDIndex32, enableVLAVectorization);
}

// These options must be kept in sync with `SparseTensorConversionBase`.
Expand Down
14 changes: 4 additions & 10 deletions mlir/include/mlir/Dialect/SparseTensor/Transforms/Passes.h
Expand Up @@ -26,11 +26,6 @@ namespace bufferization {
struct OneShotBufferizationOptions;
} // namespace bufferization

#define GEN_PASS_DECL_SPARSIFICATIONPASS
#define GEN_PASS_DECL_SPARSETENSORCONVERSIONPASS
#define GEN_PASS_DECL_SPARSETENSORCODEGEN
#include "mlir/Dialect/SparseTensor/Transforms/Passes.h.inc"

//===----------------------------------------------------------------------===//
// The Sparsification pass.
//===----------------------------------------------------------------------===//
Expand All @@ -49,9 +44,6 @@ enum class SparseParallelizationStrategy {
// TODO: support reduction parallelization too?
};

/// Converts command-line parallelization flag to the strategy enum.
SparseParallelizationStrategy sparseParallelizationStrategy(int32_t flag);

/// Defines a vectorization strategy. Any inner loop is a candidate (full SIMD
/// for parallel loops and horizontal SIMD for reduction loops). A loop is
/// actually vectorized if (1) allowed by the strategy, and (2) the emitted
Expand All @@ -62,8 +54,10 @@ enum class SparseVectorizationStrategy {
kAnyStorageInnerLoop
};

/// Converts command-line vectorization flag to the strategy enum.
SparseVectorizationStrategy sparseVectorizationStrategy(int32_t flag);
#define GEN_PASS_DECL_SPARSIFICATIONPASS
#define GEN_PASS_DECL_SPARSETENSORCONVERSIONPASS
#define GEN_PASS_DECL_SPARSETENSORCODEGEN
#include "mlir/Dialect/SparseTensor/Transforms/Passes.h.inc"

/// Options for the Sparsification pass.
struct SparsificationOptions {
Expand Down
33 changes: 29 additions & 4 deletions mlir/include/mlir/Dialect/SparseTensor/Transforms/Passes.td
Expand Up @@ -62,11 +62,36 @@ def SparsificationPass : Pass<"sparsification", "ModuleOp"> {
"sparse_tensor::SparseTensorDialect",
"vector::VectorDialect",
];
// TODO(57514): These enum options are duplicated in Passes.h.
let options = [
Option<"parallelization", "parallelization-strategy", "int32_t", "0",
"Set the parallelization strategy">,
Option<"vectorization", "vectorization-strategy", "int32_t", "0",
"Set the vectorization strategy">,
Option<"parallelization", "parallelization-strategy", "mlir::SparseParallelizationStrategy",
"mlir::SparseParallelizationStrategy::kNone",
"Set the parallelization strategy", [{llvm::cl::values(
clEnumValN(mlir::SparseParallelizationStrategy::kNone, "none",
"Turn off sparse parallelization."),
clEnumValN(mlir::SparseParallelizationStrategy::kDenseOuterLoop,
"dense-outer-loop",
"Enable dense outer loop sparse parallelization."),
clEnumValN(mlir::SparseParallelizationStrategy::kAnyStorageOuterLoop,
"any-storage-outer-loop",
"Enable sparse parallelization regardless of storage for the outer loop."),
clEnumValN(mlir::SparseParallelizationStrategy::kDenseAnyLoop,
"dense-any-loop",
"Enable dense parallelization for any loop."),
clEnumValN(mlir::SparseParallelizationStrategy::kAnyStorageAnyLoop,
"any-storage-any-loop",
"Enable sparse parallelization for any storage and loop."))}]>,
Option<"vectorization", "vectorization-strategy", "mlir::SparseVectorizationStrategy",
"mlir::SparseVectorizationStrategy::kNone",
"Set the vectorization strategy", [{llvm::cl::values(
clEnumValN(mlir::SparseVectorizationStrategy::kNone, "none",
"Turn off sparse vectorization."),
clEnumValN(mlir::SparseVectorizationStrategy::kDenseInnerLoop,
"dense-inner-loop",
"Enable vectorization for dense inner loops."),
clEnumValN(mlir::SparseVectorizationStrategy::kAnyStorageInnerLoop,
"any-storage-inner-loop",
"Enable sparse vectorization for inner loops with any storage."))}]>,
Option<"vectorLength", "vl", "int32_t", "1",
"Set the vector length">,
Option<"enableSIMDIndex32", "enable-simd-index32", "bool", "false",
Expand Down
37 changes: 4 additions & 33 deletions mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorPasses.cpp
Expand Up @@ -43,8 +43,8 @@ struct SparsificationPass
SparsificationPass() = default;
SparsificationPass(const SparsificationPass &pass) = default;
SparsificationPass(const SparsificationOptions &options) {
parallelization = static_cast<int32_t>(options.parallelizationStrategy);
vectorization = static_cast<int32_t>(options.vectorizationStrategy);
parallelization = options.parallelizationStrategy;
vectorization = options.vectorizationStrategy;
vectorLength = options.vectorLength;
enableSIMDIndex32 = options.enableSIMDIndex32;
enableVLAVectorization = options.enableVLAVectorization;
Expand All @@ -57,10 +57,8 @@ struct SparsificationPass
populateSparseTensorRewriting(prePatterns);
(void)applyPatternsAndFoldGreedily(getOperation(), std::move(prePatterns));
// Translate strategy flags to strategy options.
SparsificationOptions options(
sparseParallelizationStrategy(parallelization),
sparseVectorizationStrategy(vectorization), vectorLength,
enableSIMDIndex32, enableVLAVectorization);
SparsificationOptions options(parallelization, vectorization, vectorLength,
enableSIMDIndex32, enableVLAVectorization);
// Apply sparsification and vector cleanup rewriting.
RewritePatternSet patterns(ctx);
populateSparsificationPatterns(patterns, options);
Expand Down Expand Up @@ -238,33 +236,6 @@ struct SparseTensorStorageExpansionPass
// Strategy flag methods.
//===----------------------------------------------------------------------===//

SparseParallelizationStrategy
mlir::sparseParallelizationStrategy(int32_t flag) {
switch (flag) {
default:
return SparseParallelizationStrategy::kNone;
case 1:
return SparseParallelizationStrategy::kDenseOuterLoop;
case 2:
return SparseParallelizationStrategy::kAnyStorageOuterLoop;
case 3:
return SparseParallelizationStrategy::kDenseAnyLoop;
case 4:
return SparseParallelizationStrategy::kAnyStorageAnyLoop;
}
}

SparseVectorizationStrategy mlir::sparseVectorizationStrategy(int32_t flag) {
switch (flag) {
default:
return SparseVectorizationStrategy::kNone;
case 1:
return SparseVectorizationStrategy::kDenseInnerLoop;
case 2:
return SparseVectorizationStrategy::kAnyStorageInnerLoop;
}
}

SparseToSparseConversionStrategy
mlir::sparseToSparseConversionStrategy(int32_t flag) {
switch (flag) {
Expand Down
10 changes: 5 additions & 5 deletions mlir/test/Dialect/SparseTensor/sparse_parallel.mlir
@@ -1,12 +1,12 @@
// RUN: mlir-opt %s -sparsification="parallelization-strategy=0" | \
// RUN: mlir-opt %s -sparsification="parallelization-strategy=none" | \
// RUN: FileCheck %s --check-prefix=CHECK-PAR0
// RUN: mlir-opt %s -sparsification="parallelization-strategy=1" | \
// RUN: mlir-opt %s -sparsification="parallelization-strategy=dense-outer-loop" | \
// RUN: FileCheck %s --check-prefix=CHECK-PAR1
// RUN: mlir-opt %s -sparsification="parallelization-strategy=2" | \
// RUN: mlir-opt %s -sparsification="parallelization-strategy=any-storage-outer-loop" | \
// RUN: FileCheck %s --check-prefix=CHECK-PAR2
// RUN: mlir-opt %s -sparsification="parallelization-strategy=3" | \
// RUN: mlir-opt %s -sparsification="parallelization-strategy=dense-any-loop" | \
// RUN: FileCheck %s --check-prefix=CHECK-PAR3
// RUN: mlir-opt %s -sparsification="parallelization-strategy=4" | \
// RUN: mlir-opt %s -sparsification="parallelization-strategy=any-storage-any-loop" | \
// RUN: FileCheck %s --check-prefix=CHECK-PAR4

#DenseMatrix = #sparse_tensor.encoding<{
Expand Down
10 changes: 5 additions & 5 deletions mlir/test/Dialect/SparseTensor/sparse_vector.mlir
@@ -1,12 +1,12 @@
// RUN: mlir-opt %s -sparsification="vectorization-strategy=0 vl=16" -cse -split-input-file | \
// RUN: mlir-opt %s -sparsification="vectorization-strategy=none vl=16" -cse -split-input-file | \
// RUN: FileCheck %s --check-prefix=CHECK-VEC0
// RUN: mlir-opt %s -sparsification="vectorization-strategy=1 vl=16" -cse -split-input-file | \
// RUN: mlir-opt %s -sparsification="vectorization-strategy=dense-inner-loop vl=16" -cse -split-input-file | \
// RUN: FileCheck %s --check-prefix=CHECK-VEC1
// RUN: mlir-opt %s -sparsification="vectorization-strategy=2 vl=16" -cse -split-input-file | \
// RUN: mlir-opt %s -sparsification="vectorization-strategy=any-storage-inner-loop vl=16" -cse -split-input-file | \
// RUN: FileCheck %s --check-prefix=CHECK-VEC2
// RUN: mlir-opt %s -sparsification="vectorization-strategy=2 vl=16 enable-simd-index32=true" -cse -split-input-file | \
// RUN: mlir-opt %s -sparsification="vectorization-strategy=any-storage-inner-loop vl=16 enable-simd-index32=true" -cse -split-input-file | \
// RUN: FileCheck %s --check-prefix=CHECK-VEC3
// RUN: mlir-opt %s -sparsification="vectorization-strategy=2 vl=4 enable-vla-vectorization=true" -cse -split-input-file | \
// RUN: mlir-opt %s -sparsification="vectorization-strategy=any-storage-inner-loop vl=4 enable-vla-vectorization=true" -cse -split-input-file | \
// RUN: FileCheck %s --check-prefix=CHECK-VEC4

#DenseVector = #sparse_tensor.encoding<{ dimLevelType = [ "dense" ] }>
Expand Down
2 changes: 1 addition & 1 deletion mlir/test/Dialect/SparseTensor/sparse_vector_chain.mlir
@@ -1,6 +1,6 @@
// NOTE: Assertions have been autogenerated by utils/generate-test-checks.py

// RUN: mlir-opt %s -sparsification="vectorization-strategy=2 vl=8" -canonicalize | \
// RUN: mlir-opt %s -sparsification="vectorization-strategy=any-storage-inner-loop vl=8" -canonicalize | \
// RUN: FileCheck %s

#SparseMatrix = #sparse_tensor.encoding<{dimLevelType = ["dense","compressed"]}>
Expand Down
2 changes: 1 addition & 1 deletion mlir/test/Dialect/SparseTensor/sparse_vector_index.mlir
Expand Up @@ -5,7 +5,7 @@
// about what constitutes a good test! The CHECK should be
// minimized and named to reflect the test intent.

// RUN: mlir-opt %s -sparsification="vectorization-strategy=2 vl=8" -canonicalize | \
// RUN: mlir-opt %s -sparsification="vectorization-strategy=any-storage-inner-loop vl=8" -canonicalize | \
// RUN: FileCheck %s

#SparseVector = #sparse_tensor.encoding<{
Expand Down
2 changes: 1 addition & 1 deletion mlir/test/Dialect/SparseTensor/sparse_vector_peeled.mlir
@@ -1,4 +1,4 @@
// RUN: mlir-opt %s -sparsification="vectorization-strategy=2 vl=16" -scf-for-loop-peeling -canonicalize | \
// RUN: mlir-opt %s -sparsification="vectorization-strategy=any-storage-inner-loop vl=16" -scf-for-loop-peeling -canonicalize | \
// RUN: FileCheck %s

#SparseVector = #sparse_tensor.encoding<{
Expand Down
Expand Up @@ -6,7 +6,7 @@
//
// Do the same run, but now with SIMDization as well. This should not change the outcome.
//
// RUN: mlir-opt %s --sparse-compiler="vectorization-strategy=2 vl=2" | \
// RUN: mlir-opt %s --sparse-compiler="vectorization-strategy=any-storage-inner-loop vl=2" | \
// RUN: mlir-cpu-runner \
// RUN: -e entry -entry-point-result=void \
// RUN: -shared-libs=%mlir_integration_test_dir/libmlir_c_runner_utils%shlibext | \
Expand Down
Expand Up @@ -5,7 +5,7 @@
//
// Do the same run, but now with SIMDization as well. This should not change the outcome.
//
// RUN: mlir-opt %s --sparse-compiler="vectorization-strategy=2 vl=2" | \
// RUN: mlir-opt %s --sparse-compiler="vectorization-strategy=any-storage-inner-loop vl=2" | \
// RUN: mlir-cpu-runner -e entry -entry-point-result=void \
// RUN: -shared-libs=%mlir_integration_test_dir/libmlir_c_runner_utils%shlibext | \
// RUN: FileCheck %s
Expand Down
Expand Up @@ -7,7 +7,7 @@
//
// Do the same run, but now with SIMDization as well. This should not change the outcome.
//
// RUN: mlir-opt %s --sparse-compiler="vectorization-strategy=2 vl=4" | \
// RUN: mlir-opt %s --sparse-compiler="vectorization-strategy=any-storage-inner-loop vl=4" | \
// RUN: TENSOR0="%mlir_integration_test_dir/data/test.tns" \
// RUN: mlir-cpu-runner \
// RUN: -e entry -entry-point-result=void \
Expand Down
Expand Up @@ -5,7 +5,7 @@
//
// Do the same run, but now with SIMDization as well. This should not change the outcome.
//
// RUN: mlir-opt %s --sparse-compiler="vectorization-strategy=2 vl=4" | \
// RUN: mlir-opt %s --sparse-compiler="vectorization-strategy=any-storage-inner-loop vl=4" | \
// RUN: mlir-cpu-runner -e entry -entry-point-result=void \
// RUN: -shared-libs=%mlir_integration_test_dir/libmlir_c_runner_utils%shlibext | \
// RUN: FileCheck %s
Expand Down
Expand Up @@ -8,7 +8,7 @@
// Do the same run, but now with SIMDization as well. This should not change the outcome.
//
// RUN: mlir-opt %s \
// RUN: --sparse-compiler="vectorization-strategy=2 vl=16 enable-simd-index32" | \
// RUN: --sparse-compiler="vectorization-strategy=any-storage-inner-loop vl=16 enable-simd-index32" | \
// RUN: TENSOR0="%mlir_integration_test_dir/data/wide.mtx" \
// RUN: mlir-cpu-runner \
// RUN: -e entry -entry-point-result=void \
Expand Down
Expand Up @@ -7,7 +7,7 @@
//
// Do the same run, but now with SIMDization as well. This should not change the outcome.
//
// RUN: mlir-opt %s --sparse-compiler="vectorization-strategy=2 vl=4" | \
// RUN: mlir-opt %s --sparse-compiler="vectorization-strategy=any-storage-inner-loop vl=4" | \
// RUN: TENSOR0="%mlir_integration_test_dir/data/mttkrp_b.tns" \
// RUN: mlir-cpu-runner \
// RUN: -e entry -entry-point-result=void \
Expand Down
Expand Up @@ -7,7 +7,7 @@
//
// Do the same run, but now with SIMDization as well. This should not change the outcome.
//
// RUN: mlir-opt %s --sparse-compiler="vectorization-strategy=2 vl=4" | \
// RUN: mlir-opt %s --sparse-compiler="vectorization-strategy=any-storage-inner-loop vl=4" | \
// RUN: TENSOR0="%mlir_integration_test_dir/data/test.mtx" \
// RUN: mlir-cpu-runner \
// RUN: -e entry -entry-point-result=void \
Expand Down
Expand Up @@ -5,7 +5,7 @@
//
// Do the same run, but now with SIMDization as well. This should not change the outcome.
//
// RUN: mlir-opt %s --sparse-compiler="vectorization-strategy=2 vl=2" | \
// RUN: mlir-opt %s --sparse-compiler="vectorization-strategy=any-storage-inner-loop vl=2" | \
// RUN: mlir-cpu-runner -e entry -entry-point-result=void \
// RUN: -shared-libs=%mlir_integration_test_dir/libmlir_c_runner_utils%shlibext | \
// RUN: FileCheck %s
Expand Down
Expand Up @@ -5,7 +5,7 @@
//
// Do the same run, but now with SIMDization as well. This should not change the outcome.
//
// RUN: mlir-opt %s --sparse-compiler="vectorization-strategy=2 vl=8" | \
// RUN: mlir-opt %s -sparse-compiler="vectorization-strategy=any-storage-inner-loop vl=8" | \
// RUN: mlir-cpu-runner -e entry -entry-point-result=void \
// RUN: -shared-libs=%mlir_integration_test_dir/libmlir_c_runner_utils%shlibext | \
// RUN: FileCheck %s
Expand Down
Expand Up @@ -8,7 +8,7 @@
// Do the same run, but now with SIMDization as well. This should not change the outcome.
//
// RUN: mlir-opt %s \
// RUN: --sparse-compiler="vectorization-strategy=2 vl=4 enable-simd-index32" | \
// RUN: --sparse-compiler="vectorization-strategy=any-storage-inner-loop vl=4 enable-simd-index32" | \
// RUN: TENSOR0="%mlir_integration_test_dir/data/test.mtx" \
// RUN: mlir-cpu-runner \
// RUN: -e entry -entry-point-result=void \
Expand Down
Expand Up @@ -5,7 +5,7 @@
//
// Do the same run, but now with SIMDization as well. This should not change the outcome.
//
// RUN: mlir-opt %s --sparse-compiler="vectorization-strategy=2 vl=8" | \
// RUN: mlir-opt %s -sparse-compiler="vl=8" | \
// RUN: mlir-cpu-runner -e entry -entry-point-result=void \
// RUN: -shared-libs=%mlir_integration_test_dir/libmlir_c_runner_utils%shlibext | \
// RUN: FileCheck %s
Expand Down
Expand Up @@ -6,7 +6,7 @@
//
// Do the same run, but now with SIMDization as well. This should not change the outcome.
//
// RUN: mlir-opt %s --sparse-compiler="vectorization-strategy=2 vl=4" | \
// RUN: mlir-opt %s --sparse-compiler="vectorization-strategy=any-storage-inner-loop vl=4" | \
// RUN: mlir-cpu-runner \
// RUN: -e entry -entry-point-result=void \
// RUN: -shared-libs=%mlir_integration_test_dir/libmlir_c_runner_utils%shlibext | \
Expand Down
Expand Up @@ -7,7 +7,7 @@
//
// Do the same run, but now with SIMDization as well. This should not change the outcome.
//
// RUN: mlir-opt %s --sparse-compiler="vectorization-strategy=2 vl=2" | \
// RUN: mlir-opt %s --sparse-compiler="vectorization-strategy=any-storage-inner-loop vl=2" | \
// RUN: TENSOR0="%mlir_integration_test_dir/data/wide.mtx" \
// RUN: mlir-cpu-runner \
// RUN: -e entry -entry-point-result=void \
Expand Down
Expand Up @@ -7,7 +7,7 @@
//
// Do the same run, but now with SIMDization as well. This should not change the outcome.
//
// RUN: mlir-opt %s --sparse-compiler="vectorization-strategy=2 vl=2" | \
// RUN: mlir-opt %s --sparse-compiler="vectorization-strategy=any-storage-inner-loop vl=2" | \
// RUN: TENSOR0="%mlir_integration_test_dir/data/test_symmetric.mtx" \
// RUN: mlir-cpu-runner \
// RUN: -e entry -entry-point-result=void \
Expand Down

0 comments on commit 30ceb78

Please sign in to comment.