-
Notifications
You must be signed in to change notification settings - Fork 13.3k
/
Copy pathLowerVectorStep.cpp
49 lines (42 loc) · 1.77 KB
/
LowerVectorStep.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//===- LowerVectorStep.cpp - Lower 'vector.step' operation ----------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements target-independent rewrites and utilities to lower the
// 'vector.step' operation.
//
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/Vector/IR/VectorOps.h"
#include "mlir/Dialect/Vector/Transforms/LoweringPatterns.h"
#include "mlir/IR/PatternMatch.h"
#define DEBUG_TYPE "vector-step-lowering"
using namespace mlir;
using namespace mlir::vector;
namespace {
struct StepToArithConstantOpRewrite final : OpRewritePattern<vector::StepOp> {
using OpRewritePattern::OpRewritePattern;
LogicalResult matchAndRewrite(vector::StepOp stepOp,
PatternRewriter &rewriter) const override {
auto resultType = cast<VectorType>(stepOp.getType());
if (resultType.isScalable()) {
return failure();
}
int64_t elementCount = resultType.getNumElements();
SmallVector<APInt> indices =
llvm::map_to_vector(llvm::seq(elementCount),
[](int64_t i) { return APInt(/*width=*/64, i); });
rewriter.replaceOpWithNewOp<arith::ConstantOp>(
stepOp, DenseElementsAttr::get(resultType, indices));
return success();
}
};
} // namespace
void mlir::vector::populateVectorStepLoweringPatterns(
RewritePatternSet &patterns, PatternBenefit benefit) {
patterns.add<StepToArithConstantOpRewrite>(patterns.getContext(), benefit);
}