1 //===- SparseTensorPasses.cpp - Pass for autogen 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/Bufferization/IR/Bufferization.h"
10 #include "mlir/Dialect/Func/IR/FuncOps.h"
11 #include "mlir/Dialect/Func/Transforms/FuncConversions.h"
12 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
13 #include "mlir/Dialect/Linalg/Transforms/Transforms.h"
14 #include "mlir/Dialect/SparseTensor/IR/SparseTensor.h"
15 #include "mlir/Dialect/SparseTensor/Transforms/Passes.h"
16 #include "mlir/Transforms/GreedyPatternRewriteDriver.h"
17 
18 using namespace mlir;
19 using namespace mlir::sparse_tensor;
20 
21 namespace {
22 
23 //===----------------------------------------------------------------------===//
24 // Passes declaration.
25 //===----------------------------------------------------------------------===//
26 
27 #define GEN_PASS_CLASSES
28 #include "mlir/Dialect/SparseTensor/Transforms/Passes.h.inc"
29 
30 //===----------------------------------------------------------------------===//
31 // Passes implementation.
32 //===----------------------------------------------------------------------===//
33 
34 struct SparsificationPass : public SparsificationBase<SparsificationPass> {
35 
36   SparsificationPass() = default;
37   SparsificationPass(const SparsificationPass &pass) = default;
38   SparsificationPass(const SparsificationOptions &options) {
39     parallelization = static_cast<int32_t>(options.parallelizationStrategy);
40     vectorization = static_cast<int32_t>(options.vectorizationStrategy);
41     vectorLength = options.vectorLength;
42     enableSIMDIndex32 = options.enableSIMDIndex32;
43   }
44 
45   void runOnOperation() override {
46     auto *ctx = &getContext();
47     RewritePatternSet patterns(ctx);
48     // Translate strategy flags to strategy options.
49     SparsificationOptions options(
50         sparseParallelizationStrategy(parallelization),
51         sparseVectorizationStrategy(vectorization), vectorLength,
52         enableSIMDIndex32);
53     // Apply rewriting.
54     populateSparsificationPatterns(patterns, options);
55     vector::populateVectorToVectorCanonicalizationPatterns(patterns);
56     (void)applyPatternsAndFoldGreedily(getOperation(), std::move(patterns));
57   }
58 };
59 
60 class SparseTensorTypeConverter : public TypeConverter {
61 public:
62   SparseTensorTypeConverter() {
63     addConversion([](Type type) { return type; });
64     addConversion(convertSparseTensorTypes);
65   }
66   // Maps each sparse tensor type to an opaque pointer.
67   static Optional<Type> convertSparseTensorTypes(Type type) {
68     if (getSparseTensorEncoding(type) != nullptr)
69       return LLVM::LLVMPointerType::get(IntegerType::get(type.getContext(), 8));
70     return llvm::None;
71   }
72 };
73 
74 struct SparseTensorConversionPass
75     : public SparseTensorConversionBase<SparseTensorConversionPass> {
76   void runOnOperation() override {
77     auto *ctx = &getContext();
78     RewritePatternSet patterns(ctx);
79     SparseTensorTypeConverter converter;
80     ConversionTarget target(*ctx);
81     // Everything in the sparse dialect must go!
82     target.addIllegalDialect<SparseTensorDialect>();
83     // All dynamic rules below accept new function, call, return, and tensor
84     // dim and cast operations as legal output of the rewriting provided that
85     // all sparse tensor types have been fully rewritten.
86     target.addDynamicallyLegalOp<FuncOp>(
87         [&](FuncOp op) { return converter.isSignatureLegal(op.getType()); });
88     target.addDynamicallyLegalOp<func::CallOp>([&](func::CallOp op) {
89       return converter.isSignatureLegal(op.getCalleeType());
90     });
91     target.addDynamicallyLegalOp<func::ReturnOp>([&](func::ReturnOp op) {
92       return converter.isLegal(op.getOperandTypes());
93     });
94     target.addDynamicallyLegalOp<tensor::DimOp>([&](tensor::DimOp op) {
95       return converter.isLegal(op.getOperandTypes());
96     });
97     target.addDynamicallyLegalOp<tensor::CastOp>([&](tensor::CastOp op) {
98       return converter.isLegal(op.getOperand().getType());
99     });
100     // The following operations and dialects may be introduced by the
101     // rewriting rules, and are therefore marked as legal.
102     target.addLegalOp<arith::CmpFOp, arith::CmpIOp, arith::ConstantOp,
103                       arith::IndexCastOp, linalg::FillOp, linalg::YieldOp,
104                       tensor::ExtractOp>();
105     target
106         .addLegalDialect<bufferization::BufferizationDialect, LLVM::LLVMDialect,
107                          memref::MemRefDialect, scf::SCFDialect>();
108     // Populate with rules and apply rewriting rules.
109     populateFunctionOpInterfaceTypeConversionPattern<FuncOp>(patterns,
110                                                              converter);
111     populateCallOpTypeConversionPattern(patterns, converter);
112     populateSparseTensorConversionPatterns(converter, patterns);
113     if (failed(applyPartialConversion(getOperation(), target,
114                                       std::move(patterns))))
115       signalPassFailure();
116   }
117 };
118 
119 } // namespace
120 
121 SparseParallelizationStrategy
122 mlir::sparseParallelizationStrategy(int32_t flag) {
123   switch (flag) {
124   default:
125     return SparseParallelizationStrategy::kNone;
126   case 1:
127     return SparseParallelizationStrategy::kDenseOuterLoop;
128   case 2:
129     return SparseParallelizationStrategy::kAnyStorageOuterLoop;
130   case 3:
131     return SparseParallelizationStrategy::kDenseAnyLoop;
132   case 4:
133     return SparseParallelizationStrategy::kAnyStorageAnyLoop;
134   }
135 }
136 
137 SparseVectorizationStrategy mlir::sparseVectorizationStrategy(int32_t flag) {
138   switch (flag) {
139   default:
140     return SparseVectorizationStrategy::kNone;
141   case 1:
142     return SparseVectorizationStrategy::kDenseInnerLoop;
143   case 2:
144     return SparseVectorizationStrategy::kAnyStorageInnerLoop;
145   }
146 }
147 
148 std::unique_ptr<Pass> mlir::createSparsificationPass() {
149   return std::make_unique<SparsificationPass>();
150 }
151 
152 std::unique_ptr<Pass>
153 mlir::createSparsificationPass(const SparsificationOptions &options) {
154   return std::make_unique<SparsificationPass>(options);
155 }
156 
157 std::unique_ptr<Pass> mlir::createSparseTensorConversionPass() {
158   return std::make_unique<SparseTensorConversionPass>();
159 }
160