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
14#include "mlir/Dialect/Arith/IR/Arith.h"
15#include "mlir/Dialect/Vector/IR/VectorOps.h"
16#include "mlir/Dialect/Vector/Transforms/LoweringPatterns.h"
17#include "mlir/IR/PatternMatch.h"
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>(Val: 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 C: llvm::seq(Size: elementCount), F: [computeWidth, storageWidth](int64_t i) {
59 return APInt(computeWidth, i, /*isSigned=*/false,
60 /*implicitTrunc=*/true)
61 .zext(width: storageWidth);
62 });
63 rewriter.replaceOpWithNewOp<arith::ConstantOp>(
64 op: stepOp, args: DenseElementsAttr::get(type: resultType, values: indices));
65 return success();
66 }
67
68 unsigned indexBitwidth;
69};
70} // namespace
71
72void mlir::vector::populateVectorStepLoweringPatterns(
73 RewritePatternSet &patterns, unsigned indexBitwidth,
74 PatternBenefit benefit) {
75 patterns.add<StepToArithConstantOpRewrite>(arg: patterns.getContext(),
76 args&: indexBitwidth, args&: benefit);
77}
78