MLIR  18.0.0git
TypeConsistency.h
Go to the documentation of this file.
1 //===- TypeConsistency.h - Rewrites to improve type consistency -----------===//
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 // Set of rewrites to improve the coherency of types within an LLVM dialect
10 // program. This will adjust operations around a given pointer so they interpret
11 // its pointee type as consistently as possible.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef MLIR_DIALECT_LLVMIR_TRANSFORMS_TYPECONSISTENCY_H
16 #define MLIR_DIALECT_LLVMIR_TRANSFORMS_TYPECONSISTENCY_H
17 
19 #include "mlir/IR/PatternMatch.h"
20 #include "mlir/Pass/Pass.h"
21 
22 namespace mlir {
23 namespace LLVM {
24 
25 #define GEN_PASS_DECL_LLVMTYPECONSISTENCY
26 #include "mlir/Dialect/LLVMIR/Transforms/Passes.h.inc"
27 
28 /// Creates a pass that adjusts operations operating on pointers so they
29 /// interpret pointee types as consistently as possible.
30 std::unique_ptr<Pass> createTypeConsistencyPass();
31 
32 /// Transforms uses of pointers to a whole struct to uses of pointers to the
33 /// first element of a struct. This is achieved by inserting a GEP to the first
34 /// element when possible.
35 template <class User>
37 public:
39 
41  PatternRewriter &rewriter) const override;
42 };
43 
44 /// Canonicalizes GEPs of which the base type and the pointer's type hint do not
45 /// match. This is done by replacing the original GEP into a GEP with the type
46 /// hint as a base type when an element of the hinted type aligns with the
47 /// original GEP.
49 public:
51 
53  PatternRewriter &rewriter) const override;
54 };
55 
56 /// Splits stores which write into multiple adjacent elements of an aggregate
57 /// through a pointer. Currently, integers and vector are split and stores
58 /// are generated for every element being stored to in a type-consistent manner.
59 /// This is done on a best-effort basis.
60 class SplitStores : public OpRewritePattern<StoreOp> {
61  unsigned maxVectorSplitSize;
62 
63 public:
64  SplitStores(MLIRContext *context, unsigned maxVectorSplitSize)
65  : OpRewritePattern(context), maxVectorSplitSize(maxVectorSplitSize) {}
66 
67  LogicalResult matchAndRewrite(StoreOp store,
68  PatternRewriter &rewrite) const override;
69 };
70 
71 /// Transforms type-inconsistent stores, aka stores where the type hint of
72 /// the address contradicts the value stored, by inserting a bitcast if
73 /// possible.
74 class BitcastStores : public OpRewritePattern<StoreOp> {
75 public:
77 
78  LogicalResult matchAndRewrite(StoreOp store,
79  PatternRewriter &rewriter) const override;
80 };
81 
82 /// Splits GEPs with more than two indices into multiple GEPs with exactly
83 /// two indices. The created GEPs are then guaranteed to index into only
84 /// one aggregate at a time.
85 class SplitGEP : public OpRewritePattern<GEPOp> {
86 public:
88 
89  LogicalResult matchAndRewrite(GEPOp gepOp,
90  PatternRewriter &rewriter) const override;
91 };
92 
93 } // namespace LLVM
94 } // namespace mlir
95 
96 #endif // MLIR_DIALECT_LLVMIR_TRANSFORMS_TYPECONSISTENCY_H
Transforms uses of pointers to a whole struct to uses of pointers to the first element of a struct.
LogicalResult matchAndRewrite(User user, PatternRewriter &rewriter) const override
Transforms type-inconsistent stores, aka stores where the type hint of the address contradicts the va...
LogicalResult matchAndRewrite(StoreOp store, PatternRewriter &rewriter) const override
Canonicalizes GEPs of which the base type and the pointer's type hint do not match.
LogicalResult matchAndRewrite(GEPOp gep, PatternRewriter &rewriter) const override
Splits GEPs with more than two indices into multiple GEPs with exactly two indices.
LogicalResult matchAndRewrite(GEPOp gepOp, PatternRewriter &rewriter) const override
Splits stores which write into multiple adjacent elements of an aggregate through a pointer.
SplitStores(MLIRContext *context, unsigned maxVectorSplitSize)
LogicalResult matchAndRewrite(StoreOp store, PatternRewriter &rewrite) const override
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
Definition: PatternMatch.h:727
std::unique_ptr< Pass > createTypeConsistencyPass()
Creates a pass that adjusts operations operating on pointers so they interpret pointee types as consi...
Include the generated interface declarations.
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...
Definition: PatternMatch.h:357
OpRewritePattern(MLIRContext *context, PatternBenefit benefit=1, ArrayRef< StringRef > generatedNames={})
Patterns must specify the root operation name they match against, and can also specify the benefit of...
Definition: PatternMatch.h:361
void rewrite(Operation *op, PatternRewriter &rewriter) const final
Wrappers around the RewritePattern methods that pass the derived op type.
Definition: PatternMatch.h:322