• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- LinalgToStandard.h - Utils to convert from the linalg dialect ------===//
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 #ifndef MLIR_CONVERSION_LINALGTOSTANDARD_LINALGTOSTANDARD_H_
10 #define MLIR_CONVERSION_LINALGTOSTANDARD_LINALGTOSTANDARD_H_
11 
12 #include "mlir/Dialect/Linalg/IR/LinalgOps.h"
13 #include "mlir/Transforms/DialectConversion.h"
14 
15 namespace mlir {
16 class ModuleOp;
17 template <typename T>
18 class OperationPass;
19 
20 namespace linalg {
21 
22 //===----------------------------------------------------------------------===//
23 // Patterns to convert a LinalgOp to std.call @external library implementation.
24 //===----------------------------------------------------------------------===//
25 // These patterns are exposed individually because they are expected to be
26 // typically used individually.
27 
28 // Create a new call to the type-canonicalized `LinalgOp::getLibraryCallName()`
29 // function. The implementation of the function can be either in the same module
30 // or in an externally linked library.
31 // This is a generic entry point for all LinalgOp, except for CopyOp and
32 // IndexedGenericOp, for which omre specialized patterns are provided.
33 class LinalgOpToLibraryCallRewrite : public RewritePattern {
34 public:
LinalgOpToLibraryCallRewrite()35   LinalgOpToLibraryCallRewrite()
36       : RewritePattern(/*benefit=*/1, MatchAnyOpTypeTag()) {}
37 
38   LogicalResult matchAndRewrite(Operation *op,
39                                 PatternRewriter &rewriter) const override;
40 };
41 
42 /// Rewrite pattern specialization for CopyOp, kicks in when both input and
43 /// output permutations are left unspecified or are the identity.
44 class CopyOpToLibraryCallRewrite : public OpRewritePattern<CopyOp> {
45 public:
46   using OpRewritePattern<CopyOp>::OpRewritePattern;
47   LogicalResult matchAndRewrite(CopyOp op,
48                                 PatternRewriter &rewriter) const override;
49 };
50 
51 /// Rewrite CopyOp with permutations into a sequence of TransposeOp and
52 /// permutation-free CopyOp. This interplays with TransposeOpConversion and
53 /// LinalgConversion<CopyOp> to create a path to the LLVM dialect.
54 class CopyTransposeRewrite : public OpRewritePattern<CopyOp> {
55 public:
56   using OpRewritePattern<CopyOp>::OpRewritePattern;
57   LogicalResult matchAndRewrite(CopyOp op,
58                                 PatternRewriter &rewriter) const override;
59 };
60 
61 /// Conversion pattern specialization for IndexedGenericOp, has special handling
62 /// for the extra index operands.
63 class IndexedGenericOpToLibraryCallRewrite
64     : public OpRewritePattern<IndexedGenericOp> {
65 public:
66   using OpRewritePattern<IndexedGenericOp>::OpRewritePattern;
67   LogicalResult matchAndRewrite(IndexedGenericOp op,
68                                 PatternRewriter &rewriter) const override;
69 };
70 
71 /// Populate the given list with patterns that convert from Linalg to Standard.
72 void populateLinalgToStandardConversionPatterns(
73     OwningRewritePatternList &patterns, MLIRContext *ctx);
74 
75 } // namespace linalg
76 
77 /// Create a pass to convert Linalg operations to the Standard dialect.
78 std::unique_ptr<OperationPass<ModuleOp>> createConvertLinalgToStandardPass();
79 
80 } // namespace mlir
81 
82 #endif // MLIR_CONVERSION_LINALGTOSTANDARD_LINALGTOSTANDARD_H_
83