MLIR 23.0.0git
LowerVectorStep.cpp
Go to the documentation of this file.
1//===- LowerVectorStep.cpp - Lower 'vector.step' operation ----------------===//
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 target-independent rewrites and utilities to lower the
10// 'vector.step' operation.
11//
12//===----------------------------------------------------------------------===//
13
18
19#define DEBUG_TYPE "vector-step-lowering"
20
21using namespace mlir;
22using namespace mlir::vector;
23
24namespace {
25
26struct StepToArithConstantOpRewrite final : OpRewritePattern<vector::StepOp> {
27 StepToArithConstantOpRewrite(MLIRContext *context, unsigned indexBitwidth,
28 PatternBenefit benefit)
29 : OpRewritePattern(context, benefit), indexBitwidth(indexBitwidth) {
30 assert(indexBitwidth <= IndexType::kInternalStorageBitWidth &&
31 "indexBitwidth cannot exceed the index storage bitwidth");
32 }
33
34 LogicalResult matchAndRewrite(vector::StepOp stepOp,
35 PatternRewriter &rewriter) const override {
36 auto resultType = cast<VectorType>(stepOp.getType());
37 if (resultType.isScalable()) {
38 return failure();
39 }
40 Type elementType = resultType.getElementType();
41 // An `indexBitwidth` of 0 means "leave `index`-typed steps alone": the
42 // index bitwidth is target-dependent, so callers that don't know it (and
43 // defer to a later lowering, e.g. to `llvm.intr.stepvector`) opt out.
44 if (elementType.isIndex() && indexBitwidth == 0) {
45 return failure();
46 }
47 // Values wrap around at `computeWidth`. `index` elements are stored in a
48 // `DenseElementsAttr` using the internal storage bitwidth, so the wrapped
49 // value is widened to it; integer elements use their own bitwidth.
50 unsigned computeWidth = elementType.isIndex()
51 ? indexBitwidth
52 : elementType.getIntOrFloatBitWidth();
53 unsigned storageWidth = elementType.isIndex()
54 ? IndexType::kInternalStorageBitWidth
55 : computeWidth;
56 int64_t elementCount = resultType.getNumElements();
57 SmallVector<APInt> indices = llvm::map_to_vector(
58 llvm::seq(elementCount), [computeWidth, storageWidth](int64_t i) {
59 return APInt(computeWidth, i, /*isSigned=*/false,
60 /*implicitTrunc=*/true)
61 .zext(storageWidth);
62 });
63 rewriter.replaceOpWithNewOp<arith::ConstantOp>(
64 stepOp, DenseElementsAttr::get(resultType, indices));
65 return success();
66 }
67
68 unsigned indexBitwidth;
69};
70} // namespace
71
73 RewritePatternSet &patterns, unsigned indexBitwidth,
74 PatternBenefit benefit) {
75 patterns.add<StepToArithConstantOpRewrite>(patterns.getContext(),
76 indexBitwidth, benefit);
77}
return success()
static DenseElementsAttr get(ShapedType type, ArrayRef< Attribute > values)
Constructs a dense elements attribute from an array of element values.
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
This class represents the benefit of a pattern match in a unitless scheme that ranges from 0 (very li...
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
MLIRContext * getContext() const
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replace the results of the given (original) op with a new op that is created without verification (re...
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
bool isIndex() const
Definition Types.cpp:56
unsigned getIntOrFloatBitWidth() const
Return the bit width of an integer or a float type, assert failure on other types.
Definition Types.cpp:124
void populateVectorStepLoweringPatterns(RewritePatternSet &patterns, unsigned indexBitwidth=64, PatternBenefit benefit=1)
Populate the pattern set with the following patterns:
Include the generated interface declarations.
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...