MLIR  19.0.0git
AffineExpandIndexOps.cpp
Go to the documentation of this file.
1 //===- AffineExpandIndexOps.cpp - Affine expand index ops pass ------------===//
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 // This file implements a pass to expand affine index ops into one or more more
10 // fundamental operations.
11 //===----------------------------------------------------------------------===//
12 
14 
19 
20 namespace mlir {
21 namespace affine {
22 #define GEN_PASS_DEF_AFFINEEXPANDINDEXOPS
23 #include "mlir/Dialect/Affine/Passes.h.inc"
24 } // namespace affine
25 } // namespace mlir
26 
27 using namespace mlir;
28 using namespace mlir::affine;
29 
30 namespace {
31 /// Lowers `affine.delinearize_index` into a sequence of division and remainder
32 /// operations.
33 struct LowerDelinearizeIndexOps
34  : public OpRewritePattern<AffineDelinearizeIndexOp> {
36  LogicalResult matchAndRewrite(AffineDelinearizeIndexOp op,
37  PatternRewriter &rewriter) const override {
38  FailureOr<SmallVector<Value>> multiIndex =
39  delinearizeIndex(rewriter, op->getLoc(), op.getLinearIndex(),
40  llvm::to_vector(op.getBasis()));
41  if (failed(multiIndex))
42  return failure();
43  rewriter.replaceOp(op, *multiIndex);
44  return success();
45  }
46 };
47 
48 class ExpandAffineIndexOpsPass
49  : public affine::impl::AffineExpandIndexOpsBase<ExpandAffineIndexOpsPass> {
50 public:
51  ExpandAffineIndexOpsPass() = default;
52 
53  void runOnOperation() override {
54  MLIRContext *context = &getContext();
55  RewritePatternSet patterns(context);
57  if (failed(
58  applyPatternsAndFoldGreedily(getOperation(), std::move(patterns))))
59  return signalPassFailure();
60  }
61 };
62 
63 } // namespace
64 
66  RewritePatternSet &patterns) {
67  patterns.insert<LowerDelinearizeIndexOps>(patterns.getContext());
68 }
69 
71  return std::make_unique<ExpandAffineIndexOpsPass>();
72 }
static MLIRContext * getContext(OpFoldResult val)
This class provides support for representing a failure result, or a valid value of type T.
Definition: LogicalResult.h:78
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
Location getLoc()
The source location the operation was defined or derived from.
Definition: Operation.h:223
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
Definition: PatternMatch.h:785
RewritePatternSet & insert(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
Definition: PatternMatch.h:930
MLIRContext * getContext() const
Definition: PatternMatch.h:822
virtual void replaceOp(Operation *op, ValueRange newValues)
Replace the results of the given (original) operation with the specified list of values (replacements...
FailureOr< SmallVector< Value > > delinearizeIndex(OpBuilder &b, Location loc, Value linearIndex, ArrayRef< Value > basis)
Generate the IR to delinearize linearIndex given the basis and return the multi-index.
Definition: Utils.cpp:1849
std::unique_ptr< Pass > createAffineExpandIndexOpsPass()
Creates a pass to expand affine index operations into more fundamental operations (not necessarily re...
void populateAffineExpandIndexOpsPatterns(RewritePatternSet &patterns)
Populate patterns that expand affine index operations into more fundamental operations (not necessari...
Include the generated interface declarations.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:62
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
LogicalResult applyPatternsAndFoldGreedily(Region &region, const FrozenRewritePatternSet &patterns, GreedyRewriteConfig config=GreedyRewriteConfig(), bool *changed=nullptr)
Rewrite ops in the given region, which must be isolated from above, by repeatedly applying the highes...
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.
Definition: LogicalResult.h:72
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:358