1 //===- SparseTensorPipelines.cpp - Pipelines for sparse tensor code -------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "mlir/Dialect/SparseTensor/Pipelines/Passes.h"
10 
11 #include "mlir/Conversion/Passes.h"
12 #include "mlir/Dialect/Arithmetic/Transforms/Passes.h"
13 #include "mlir/Dialect/Bufferization/Transforms/Passes.h"
14 #include "mlir/Dialect/Linalg/Passes.h"
15 #include "mlir/Dialect/SparseTensor/IR/SparseTensor.h"
16 #include "mlir/Dialect/SparseTensor/Transforms/Passes.h"
17 #include "mlir/Dialect/StandardOps/Transforms/Passes.h"
18 #include "mlir/Dialect/Tensor/Transforms/Passes.h"
19 #include "mlir/Dialect/Vector/Transforms/Passes.h"
20 #include "mlir/Pass/PassManager.h"
21 
22 using namespace mlir;
23 using namespace mlir::sparse_tensor;
24 
25 //===----------------------------------------------------------------------===//
26 // Pipeline implementation.
27 //===----------------------------------------------------------------------===//
28 
29 void mlir::sparse_tensor::buildSparseCompiler(
30     OpPassManager &pm, const SparseCompilerOptions &options) {
31   // TODO(wrengr): ensure the original `pm` is for ModuleOp
32   pm.addPass(createSparsificationPass(options.sparsificationOptions()));
33   pm.addPass(createSparseTensorConversionPass());
34   pm.addNestedPass<FuncOp>(createLinalgBufferizePass());
35   pm.addNestedPass<FuncOp>(vector::createVectorBufferizePass());
36   pm.addNestedPass<FuncOp>(createConvertLinalgToLoopsPass());
37   pm.addNestedPass<FuncOp>(createConvertVectorToSCFPass());
38   pm.addNestedPass<FuncOp>(createConvertSCFToCFPass());
39   pm.addPass(createFuncBufferizePass());
40   pm.addPass(arith::createConstantBufferizePass());
41   pm.addNestedPass<FuncOp>(createTensorBufferizePass());
42   pm.addNestedPass<FuncOp>(
43       mlir::bufferization::createFinalizingBufferizePass());
44   pm.addPass(createLowerAffinePass());
45   pm.addPass(createConvertVectorToLLVMPass(options.lowerVectorToLLVMOptions()));
46   pm.addPass(createMemRefToLLVMPass());
47   pm.addNestedPass<FuncOp>(createConvertMathToLLVMPass());
48   pm.addPass(createLowerToLLVMPass()); // --convert-std-to-llvm
49   pm.addPass(createReconcileUnrealizedCastsPass());
50 }
51 
52 //===----------------------------------------------------------------------===//
53 // Pipeline registration.
54 //===----------------------------------------------------------------------===//
55 
56 void mlir::sparse_tensor::registerSparseTensorPipelines() {
57   PassPipelineRegistration<SparseCompilerOptions>(
58       "sparse-compiler",
59       "The standard pipeline for taking sparsity-agnostic IR using the"
60       " sparse-tensor type, and lowering it to LLVM IR with concrete"
61       " representations and algorithms for sparse tensors.",
62       buildSparseCompiler);
63 }
64